History

tccgen_begin/end_file

This is supposed to make compilation and linking with multiple source files (tcc f1.c f2.S ...) behave just the same as linking object files. tccgen.c:put_extern_sym2(): - use put_elf_sym to enter new symbols unconditionally tccelf.c: - save section state before compilation - disable symbol hashing during compilation - merge symbols and update relocations after compilation tccpe.c: - re-create s1->uw_sym for each compilation (because it may change)

grischka committed 8 years ago

Commit 1a4d4b71 parent 8490c546 files · +81 −68Browse files

Unified · Split

6 files
  1. libtcc.c +3 −4
  2. tcc.h +3 −1
  3. tccelf.c +66 −54
  4. tccgen.c +1 −1
  5. tccpe.c +7 −7
  6. tccrun.c +1 −1
libtcc.c+3−4
@@ -486,7 +486,7 @@ static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap) · 485 unmodified lines
486486 for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
487487 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
488488 (*pf)->filename, (*pf)->line_num);
489− if (f->line_num > 0) {
489+ if (s1->error_set_jmp_enabled) {
490490 strcat_printf(buf, sizeof(buf), "%s:%d: ",
491491 f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
492492 } else {
@@ -629,6 +629,7 @@ static int tcc_compile(TCCState *s1) · 136 unmodified lines
629629 define_start = define_stack;
630630 filetype = s1->filetype;
631631 is_asm = filetype == AFF_TYPE_ASM || filetype == AFF_TYPE_ASMPP;
632+ tccelf_begin_file(s1);
632633
633634 if (setjmp(s1->error_jmp_buf) == 0) {
634635 s1->nb_errors = 0;
@@ -655,6 +656,7 @@ static int tcc_compile(TCCState *s1) · 20 unmodified lines
655656 free_defines(define_start);
656657 sym_pop(&global_stack, NULL, 0);
657658 sym_pop(&local_stack, NULL, 0);
659+ tccelf_end_file(s1);
658660 return s1->nb_errors != 0 ? -1 : 0;
659661 }
660662
@@ -1016,9 +1018,6 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 355 unmodified lines
10161018 obj_type = tcc_object_type(fd, &ehdr);
10171019 lseek(fd, 0, SEEK_SET);
10181020
1019− /* do not display line number if error */
1020− file->line_num = 0;
1021−
10221021 #ifdef TCC_TARGET_MACHO
10231022 if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
10241023 obj_type = AFF_BINTYPE_DYN;
tcc.h+3−1
@@ -1397,6 +1397,8 @@ ST_DATA Section *stab_section, *stabstr_section; · 1396 unmodified lines
13971397 ST_FUNC void tccelf_new(TCCState *s);
13981398 ST_FUNC void tccelf_delete(TCCState *s);
13991399 ST_FUNC void tccelf_stab_new(TCCState *s);
1400+ST_FUNC void tccelf_begin_file(TCCState *s1);
1401+ST_FUNC void tccelf_end_file(TCCState *s1);
14001402
14011403 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
14021404 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
@@ -1425,7 +1427,7 @@ ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigne · 22 unmodified lines
14251427 ST_FUNC void put_stabn(int type, int other, int desc, int value);
14261428 ST_FUNC void put_stabd(int type, int other, int desc);
14271429
1428−ST_FUNC void resolve_regular_syms(void);
1430+ST_FUNC void resolve_common_syms(TCCState *s1);
14291431 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve);
14301432 ST_FUNC void relocate_section(TCCState *s1, Section *s);
14311433
tccelf.c+66−54
@@ -130,6 +130,59 @@ ST_FUNC void tccelf_delete(TCCState *s1) · 129 unmodified lines
130130 tcc_free(s1->sym_attrs);
131131 }
132132
133+/* save section data state */
134+ST_FUNC void tccelf_begin_file(TCCState *s1)
135+{
136+ Section *s; int i;
137+ for (i = 1; i < s1->nb_sections; i++) {
138+ s = s1->sections[i];
139+ s->sh_offset = s->data_offset;
140+ }
141+ /* disable symbol hashing during compilation */
142+ s = s1->symtab, s->reloc = s->hash, s->hash = NULL;
143+#if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE
144+ s1->uw_sym = 0;
145+#endif
146+}
147+
148+/* At the end of compilation, convert any UNDEF syms to global, and merge
149+ with previously existing symbols */
150+ST_FUNC void tccelf_end_file(TCCState *s1)
151+{
152+ Section *s = s1->symtab;
153+ int first_sym, nb_syms, *tr, i;
154+
155+ first_sym = s->sh_offset / sizeof (ElfSym);
156+ nb_syms = s->data_offset / sizeof (ElfSym) - first_sym;
157+ s->data_offset = s->sh_offset;
158+ s->link->data_offset = s->link->sh_offset;
159+ s->hash = s->reloc, s->reloc = NULL;
160+ tr = tcc_mallocz(nb_syms * sizeof *tr);
161+
162+ for (i = 0; i < nb_syms; ++i) {
163+ ElfSym *sym = (ElfSym*)s->data + first_sym + i;
164+ if (sym->st_shndx == SHN_UNDEF
165+ && ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)
166+ sym->st_info = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info));
167+ tr[i] = set_elf_sym(s, sym->st_value, sym->st_size, sym->st_info,
168+ sym->st_other, sym->st_shndx, s->link->data + sym->st_name);
169+ }
170+ /* now update relocations */
171+ for (i = 1; i < s1->nb_sections; i++) {
172+ Section *sr = s1->sections[i];
173+ if (sr->sh_type == SHT_RELX && sr->link == s) {
174+ ElfW_Rel *rel = (ElfW_Rel*)(sr->data + sr->sh_offset);
175+ ElfW_Rel *rel_end = (ElfW_Rel*)(sr->data + sr->data_offset);
176+ for (; rel < rel_end; ++rel) {
177+ int n = ELFW(R_SYM)(rel->r_info) - first_sym;
178+ //if (n < 0) tcc_error("internal: invalid symbol index in relocation");
179+ rel->r_info = ELFW(R_INFO)(tr[n], ELFW(R_TYPE)(rel->r_info));
180+ }
181+ }
182+ }
183+ tcc_free(tr);
184+}
185+
133186 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
134187 {
135188 Section *sec;
@@ -269,7 +322,7 @@ ST_FUNC int put_elf_str(Section *s, const char *sym) · 133 unmodified lines
269322 len = strlen(sym) + 1;
270323 offset = s->data_offset;
271324 ptr = section_ptr_add(s, len);
272− memcpy(ptr, sym, len);
325+ memmove(ptr, sym, len);
273326 return offset;
274327 }
275328
@@ -335,7 +388,7 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, · 59 unmodified lines
335388 Section *hs;
336389
337390 sym = section_ptr_add(s, sizeof(ElfW(Sym)));
338− if (name)
391+ if (name && name[0])
339392 name_offset = put_elf_str(s->link, name);
340393 else
341394 name_offset = 0;
@@ -356,7 +409,7 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, · 14 unmodified lines
356409 if (ELFW(ST_BIND)(info) != STB_LOCAL) {
357410 /* add another hashing entry */
358411 nbuckets = base[0];
359− h = elf_hash((unsigned char *) name) % nbuckets;
412+ h = elf_hash((unsigned char *)s->link->data + name_offset) % nbuckets;
360413 *ptr = base[2 + h];
361414 base[2 + h] = sym_index;
362415 base[1]++;
@@ -373,7 +426,7 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, · 10 unmodified lines
373426 return sym_index;
374427 }
375428
376−static int find_elf_sym_1(Section *s, const char *name, int onlydef)
429+ST_FUNC int find_elf_sym(Section *s, const char *name)
377430 {
378431 ElfW(Sym) *sym;
379432 Section *hs;
@@ -389,21 +442,13 @@ static int find_elf_sym_1(Section *s, const char *name, int onlydef) · 9 unmodified lines
389442 while (sym_index != 0) {
390443 sym = &((ElfW(Sym) *)s->data)[sym_index];
391444 name1 = (char *) s->link->data + sym->st_name;
392− if (!strcmp(name, name1) && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL
393− && (!onlydef || sym->st_shndx != SHN_UNDEF))
445+ if (!strcmp(name, name1))
394446 return sym_index;
395447 sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
396448 }
397449 return 0;
398450 }
399451
400−/* find global ELF symbol 'name' and return its index. Return 0 if not
401− found. */
402−ST_FUNC int find_elf_sym(Section *s, const char *name)
403−{
404− return find_elf_sym_1(s, name, 0);
405−}
406−
407452 /* return elf symbol value, signal error if 'err' is nonzero */
408453 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err)
409454 {
@@ -447,17 +492,15 @@ ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, · 37 unmodified lines
447492 sym_type = ELFW(ST_TYPE)(info);
448493 sym_vis = ELFW(ST_VISIBILITY)(other);
449494
450− sym_index = find_elf_sym(s, name);
451− esym = &((ElfW(Sym) *)s->data)[sym_index];
452− if (sym_index && esym->st_value == value && esym->st_size == size
453− && esym->st_info == info && esym->st_other == other
454− && esym->st_shndx == shndx)
455− return sym_index;
456−
457495 if (sym_bind != STB_LOCAL) {
458496 /* we search global or weak symbols */
497+ sym_index = find_elf_sym(s, name);
459498 if (!sym_index)
460499 goto do_def;
500+ esym = &((ElfW(Sym) *)s->data)[sym_index];
501+ if (esym->st_value == value && esym->st_size == size && esym->st_info == info
502+ && esym->st_other == other && esym->st_shndx == shndx)
503+ return sym_index;
461504 if (esym->st_shndx != SHN_UNDEF) {
462505 esym_bind = ELFW(ST_BIND)(esym->st_info);
463506 /* propagate the most constraining visibility */
@@ -1220,11 +1263,8 @@ static void tcc_add_linker_symbols(TCCState *s1) · 756 unmodified lines
12201263 }
12211264 }
12221265
1223−/* Do final regular symbol preparation (for those coming from .c/.o/.s files,
1224− not from shared libs) */
1225−ST_FUNC void resolve_regular_syms(void)
1266+ST_FUNC void resolve_common_syms(TCCState *s1)
12261267 {
1227− int rebuild = 0;
12281268 ElfW(Sym) *sym;
12291269
12301270 /* Allocate common symbols in BSS. */
@@ -1238,27 +1278,7 @@ ST_FUNC void resolve_regular_syms(void) · 7 unmodified lines
12381278 }
12391279
12401280 /* Now assign linker provided symbols their value. */
1241− tcc_add_linker_symbols(tcc_state);
1242−
1243− /* And finally resolve still UNDEF symbols (for multi-file mode),
1244− and globalize those that are still UNDEF. */
1245− rebuild_hash(symtab_section, 0);
1246− for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
1247− if (sym->st_shndx == SHN_UNDEF) {
1248− const char *name = (char *) symtab_section->link->data + sym->st_name;
1249− int symndx = find_elf_sym_1(symtab_section, name, 1);
1250− if (symndx) {
1251− *sym = ((ElfSym *)symtab_section->data)[symndx];
1252− rebuild = 1;
1253− } else if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
1254− sym->st_info
1255− = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info));
1256− rebuild = 1;
1257− }
1258− }
1259− }
1260− if (rebuild)
1261− rebuild_hash(symtab_section, 0);
1281+ tcc_add_linker_symbols(s1);
12621282 }
12631283
12641284 static void tcc_output_binary(TCCState *s1, FILE *f,
@@ -2051,7 +2071,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 786 unmodified lines
20512071 if (file_type != TCC_OUTPUT_OBJ) {
20522072 /* if linking, also link in runtime libraries (libc, libgcc, etc.) */
20532073 tcc_add_runtime(s1);
2054− resolve_regular_syms();
2074+ resolve_common_syms(s1);
20552075
20562076 if (!s1->static_link) {
20572077 if (file_type == TCC_OUTPUT_EXE) {
@@ -2092,14 +2112,6 @@ static int elf_output_file(TCCState *s1, const char *filename) · 34 unmodified lines
20922112 }
20932113 }
20942114 build_got_entries(s1);
2095− } else {
2096− for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
2097− if (sym->st_shndx == SHN_UNDEF
2098− && ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
2099− sym->st_info
2100− = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info));
2101− }
2102− }
21032115 }
21042116
21052117 /* we add a section for symbols */
tccgen.c+1−1
@@ -418,7 +418,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, · 417 unmodified lines
418418 if (sym->asm_label)
419419 name = get_tok_str(sym->asm_label, NULL);
420420 info = ELFW(ST_INFO)(sym_bind, sym_type);
421− sym->c = set_elf_sym(symtab_section, value, size, info, other, sh_num, name);
421+ sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
422422 } else {
423423 esym = elfsym(sym);
424424 esym->st_value = value;
tccpe.c+7−7
@@ -1633,7 +1633,7 @@ static int pe_load_res(TCCState *s1, int fd) · 1632 unmodified lines
16331633 {
16341634 struct pe_rsrc_header hdr;
16351635 Section *rsrc_section;
1636− int i, ret = -1;
1636+ int i, ret = -1, sym_index;
16371637 BYTE *ptr;
16381638 unsigned offs;
16391639
@@ -1651,8 +1651,8 @@ static int pe_load_res(TCCState *s1, int fd) · 11 unmodified lines
16511651 if (!read_mem(fd, offs, ptr, hdr.sectionhdr.SizeOfRawData))
16521652 goto quit;
16531653 offs = hdr.sectionhdr.PointerToRelocations;
1654− for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i)
1655− {
1654+ sym_index = put_elf_sym(symtab_section, 0, 0, 0, 0, rsrc_section->sh_num, ".rsrc");
1655+ for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i) {
16561656 struct pe_rsrc_reloc rel;
16571657 if (!read_mem(fd, offs, &rel, sizeof rel))
16581658 goto quit;
@@ -1660,7 +1660,7 @@ static int pe_load_res(TCCState *s1, int fd) · 1 unmodified line
16601660 if (rel.type != RSRC_RELTYPE)
16611661 goto quit;
16621662 put_elf_reloc(symtab_section, rsrc_section,
1663− rel.offset, R_XXX_RELATIVE, 0);
1663+ rel.offset, R_XXX_RELATIVE, sym_index);
16641664 offs += sizeof rel;
16651665 }
16661666 ret = 0;
@@ -1779,9 +1779,9 @@ static unsigned pe_add_uwwind_info(TCCState *s1) · 112 unmodified lines
17791779 if (NULL == s1->uw_pdata) {
17801780 s1->uw_pdata = find_section(tcc_state, ".pdata");
17811781 s1->uw_pdata->sh_addralign = 4;
1782− s1->uw_sym = put_elf_sym(symtab_section, 0, 0, 0, 0, text_section->sh_num, NULL);
17831782 }
1784−
1783+ if (0 == s1->uw_sym)
1784+ s1->uw_sym = put_elf_sym(symtab_section, 0, 0, 0, 0, text_section->sh_num, ".uw_base");
17851785 if (0 == s1->uw_offs) {
17861786 /* As our functions all have the same stackframe, we use one entry for all */
17871787 static const unsigned char uw_info[] = {
@@ -1970,7 +1970,7 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename) · 182 unmodified lines
19701970
19711971 tcc_add_bcheck(s1);
19721972 pe_add_runtime(s1, &pe);
1973− resolve_regular_syms();
1973+ resolve_common_syms(s1);
19741974 pe_set_options(s1, &pe);
19751975
19761976 ret = pe_check_symbols(&pe);
tccrun.c+1−1
@@ -188,7 +188,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff) · 187 unmodified lines
188188 pe_output_file(s1, NULL);
189189 #else
190190 tcc_add_runtime(s1);
191− resolve_regular_syms();
191+ resolve_common_syms(s1);
192192 build_got_entries(s1);
193193 #endif
194194 if (s1->nb_errors)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t