History

Rename add_elf_sym to set_elf_sym

add_elf_sym is a confusing name because it is not clear what the function does compared to put_elf_sym. As a matter of fact, put_elf_sym also adds a symbol in a symbol table. Besides, "add_elf_sym" fails to convey that the function can be used to update a symbol (for instance its value). "set_elf_sym" seems like a more appropriate name: it will set a symbol to a given set of properties (value, size, etc.) and create a new one if non exist for that name as one would expect.

Thomas Preud'homme committed 10 years ago

Commit 64b5ee21 parent 9e6610b5 files · +29 −29Browse files

Unified · Split

5 files
  1. libtcc.c +1 −1
  2. tcc.h +1 −1
  3. tccelf.c +24 −24
  4. tccgen.c +1 −1
  5. tccpe.c +2 −2
libtcc.c+1−1
@@ -1199,7 +1199,7 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val) · 1198 unmodified lines
1199 So it is handled here as if it were in a DLL. */1199 So it is handled here as if it were in a DLL. */
1200 pe_putimport(s, 0, name, (uintptr_t)val);1200 pe_putimport(s, 0, name, (uintptr_t)val);
1201#else1201#else
1202 add_elf_sym(symtab_section, (uintptr_t)val, 0,1202 set_elf_sym(symtab_section, (uintptr_t)val, 0,
1203 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1203 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1204 SHN_ABS, name);1204 SHN_ABS, name);
1205#endif1205#endif
tcc.h+1−1
@@ -1339,7 +1339,7 @@ ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_ · 1338 unmodified lines
1339 1339
1340ST_FUNC int put_elf_str(Section *s, const char *sym);1340ST_FUNC int put_elf_str(Section *s, const char *sym);
1341ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);1341ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1342ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int sh_num, const char *name);1342ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1343ST_FUNC int find_elf_sym(Section *s, const char *name);1343ST_FUNC int find_elf_sym(Section *s, const char *name);
1344ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);1344ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1345ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);1345ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
tccelf.c+24−24
@@ -383,8 +383,8 @@ ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name) · 382 unmodified lines
383 383
384/* add an elf symbol : check if it is already defined and patch384/* add an elf symbol : check if it is already defined and patch
385 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */385 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
386ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size,386ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
387 int info, int other, int sh_num, const char *name)387 int info, int other, int shndx, const char *name)
388{388{
389 ElfW(Sym) *esym;389 ElfW(Sym) *esym;
390 int sym_bind, sym_index, sym_type, esym_bind;390 int sym_bind, sym_index, sym_type, esym_bind;
@@ -398,7 +398,7 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, · 7 unmodified lines
398 esym = &((ElfW(Sym) *)s->data)[sym_index];398 esym = &((ElfW(Sym) *)s->data)[sym_index];
399 if (sym_index && esym->st_value == value && esym->st_size == size399 if (sym_index && esym->st_value == value && esym->st_size == size
400 && esym->st_info == info && esym->st_other == other400 && esym->st_info == info && esym->st_other == other
401 && esym->st_shndx == sh_num)401 && esym->st_shndx == shndx)
402 return sym_index;402 return sym_index;
403 403
404 if (sym_bind != STB_LOCAL) {404 if (sym_bind != STB_LOCAL) {
@@ -420,7 +420,7 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, · 15 unmodified lines
420 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))420 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
421 | new_vis;421 | new_vis;
422 other = esym->st_other; /* in case we have to patch esym */422 other = esym->st_other; /* in case we have to patch esym */
423 if (sh_num == SHN_UNDEF) {423 if (shndx == SHN_UNDEF) {
424 /* ignore adding of undefined symbol if the424 /* ignore adding of undefined symbol if the
425 corresponding symbol is already defined */425 corresponding symbol is already defined */
426 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {426 } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
@@ -434,25 +434,25 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, · 7 unmodified lines
434 /* ignore hidden symbols after */434 /* ignore hidden symbols after */
435 } else if ((esym->st_shndx == SHN_COMMON435 } else if ((esym->st_shndx == SHN_COMMON
436 || esym->st_shndx == bss_section->sh_num)436 || esym->st_shndx == bss_section->sh_num)
437 && (sh_num < SHN_LORESERVE437 && (shndx < SHN_LORESERVE
438 && sh_num != bss_section->sh_num)) {438 && shndx != bss_section->sh_num)) {
439 /* data symbol gets precedence over common/bss */439 /* data symbol gets precedence over common/bss */
440 goto do_patch;440 goto do_patch;
441 } else if (sh_num == SHN_COMMON || sh_num == bss_section->sh_num) {441 } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
442 /* data symbol keeps precedence over common/bss */442 /* data symbol keeps precedence over common/bss */
443 } else if (s == tcc_state->dynsymtab_section) {443 } else if (s == tcc_state->dynsymtab_section) {
444 /* we accept that two DLL define the same symbol */444 /* we accept that two DLL define the same symbol */
445 } else {445 } else {
446#if 0446#if 0
447 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",447 printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
448 sym_bind, sh_num, new_vis, esym_bind, esym->st_shndx, esym_vis);448 sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
449#endif449#endif
450 tcc_error_noabort("'%s' defined twice", name);450 tcc_error_noabort("'%s' defined twice", name);
451 }451 }
452 } else {452 } else {
453 do_patch:453 do_patch:
454 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);454 esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
455 esym->st_shndx = sh_num;455 esym->st_shndx = shndx;
456 new_undef_sym = 1;456 new_undef_sym = 1;
457 esym->st_value = value;457 esym->st_value = value;
458 esym->st_size = size;458 esym->st_size = size;
@@ -462,7 +462,7 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, · 3 unmodified lines
462 do_def:462 do_def:
463 sym_index = put_elf_sym(s, value, size,463 sym_index = put_elf_sym(s, value, size,
464 ELFW(ST_INFO)(sym_bind, sym_type), other,464 ELFW(ST_INFO)(sym_bind, sym_type), other,
465 sh_num, name);465 shndx, name);
466 }466 }
467 return sym_index;467 return sym_index;
468}468}
@@ -1274,7 +1274,7 @@ static void build_got(TCCState *s1) · 805 unmodified lines
1274 /* if no got, then create it */1274 /* if no got, then create it */
1275 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);1275 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
1276 s1->got->sh_entsize = 4;1276 s1->got->sh_entsize = 4;
1277 add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),1277 set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
1278 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");1278 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
1279 ptr = section_ptr_add(s1->got, 3 * PTR_SIZE);1279 ptr = section_ptr_add(s1->got, 3 * PTR_SIZE);
1280#if PTR_SIZE == 41280#if PTR_SIZE == 4
@@ -1760,11 +1760,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) · 479 unmodified lines
1760 end_offset = s->data_offset;1760 end_offset = s->data_offset;
1761 }1761 }
1762 1762
1763 add_elf_sym(symtab_section,1763 set_elf_sym(symtab_section,
1764 0, 0,1764 0, 0,
1765 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1765 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1766 s->sh_num, sym_start);1766 s->sh_num, sym_start);
1767 add_elf_sym(symtab_section,1767 set_elf_sym(symtab_section,
1768 end_offset, 0,1768 end_offset, 0,
1769 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1769 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1770 s->sh_num, sym_end);1770 s->sh_num, sym_end);
@@ -1789,11 +1789,11 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1) · 18 unmodified lines
1789 /* XXX: add an object file to do that */1789 /* XXX: add an object file to do that */
1790 ptr = section_ptr_add(bounds_section, sizeof(*ptr));1790 ptr = section_ptr_add(bounds_section, sizeof(*ptr));
1791 *ptr = 0;1791 *ptr = 0;
1792 add_elf_sym(symtab_section, 0, 0,1792 set_elf_sym(symtab_section, 0, 0,
1793 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1793 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1794 bounds_section->sh_num, "__bounds_start");1794 bounds_section->sh_num, "__bounds_start");
1795 /* pull bcheck.o from libtcc1.a */1795 /* pull bcheck.o from libtcc1.a */
1796 sym_index = add_elf_sym(symtab_section, 0, 0,1796 sym_index = set_elf_sym(symtab_section, 0, 0,
1797 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1797 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1798 SHN_UNDEF, "__bound_init");1798 SHN_UNDEF, "__bound_init");
1799 if (s1->output_type != TCC_OUTPUT_MEMORY) {1799 if (s1->output_type != TCC_OUTPUT_MEMORY) {
@@ -1838,15 +1838,15 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) · 38 unmodified lines
1838 int i;1838 int i;
1839 Section *s;1839 Section *s;
1840 1840
1841 add_elf_sym(symtab_section,1841 set_elf_sym(symtab_section,
1842 text_section->data_offset, 0,1842 text_section->data_offset, 0,
1843 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1843 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1844 text_section->sh_num, "_etext");1844 text_section->sh_num, "_etext");
1845 add_elf_sym(symtab_section,1845 set_elf_sym(symtab_section,
1846 data_section->data_offset, 0,1846 data_section->data_offset, 0,
1847 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1847 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1848 data_section->sh_num, "_edata");1848 data_section->sh_num, "_edata");
1849 add_elf_sym(symtab_section,1849 set_elf_sym(symtab_section,
1850 bss_section->data_offset, 0,1850 bss_section->data_offset, 0,
1851 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1851 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1852 bss_section->sh_num, "_end");1852 bss_section->sh_num, "_end");
@@ -1877,12 +1877,12 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) · 24 unmodified lines
1877 p++;1877 p++;
1878 }1878 }
1879 snprintf(buf, sizeof(buf), "__start_%s", s->name);1879 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1880 add_elf_sym(symtab_section,1880 set_elf_sym(symtab_section,
1881 0, 0,1881 0, 0,
1882 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1882 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1883 s->sh_num, buf);1883 s->sh_num, buf);
1884 snprintf(buf, sizeof(buf), "__stop_%s", s->name);1884 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1885 add_elf_sym(symtab_section,1885 set_elf_sym(symtab_section,
1886 s->data_offset, 0,1886 s->data_offset, 0,
1887 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1887 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1888 s->sh_num, buf);1888 s->sh_num, buf);
@@ -2063,7 +2063,7 @@ static void bind_exe_dynsyms(TCCState *s1) · 174 unmodified lines
2063 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {2063 } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
2064 /* if -rdynamic option, then export all non local symbols */2064 /* if -rdynamic option, then export all non local symbols */
2065 name = (char *) symtab_section->link->data + sym->st_name;2065 name = (char *) symtab_section->link->data + sym->st_name;
2066 add_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,2066 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
2067 0, sym->st_shndx, name);2067 0, sym->st_shndx, name);
2068 }2068 }
2069 }2069 }
@@ -2087,7 +2087,7 @@ static void bind_libs_dynsyms(TCCState *s1) · 17 unmodified lines
2087 -rdynamic ? */2087 -rdynamic ? */
2088 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];2088 sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
2089 if (sym_index && sym->st_shndx != SHN_UNDEF)2089 if (sym_index && sym->st_shndx != SHN_UNDEF)
2090 add_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,2090 set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info,
2091 0, sym->st_shndx, name);2091 0, sym->st_shndx, name);
2092 else if (esym->st_shndx == SHN_UNDEF) {2092 else if (esym->st_shndx == SHN_UNDEF) {
2093 /* weak symbols can stay undefined */2093 /* weak symbols can stay undefined */
@@ -3159,7 +3159,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, · 1065 unmodified lines
3159 }3159 }
3160 /* add symbol */3160 /* add symbol */
3161 name = (char *) strtab + sym->st_name;3161 name = (char *) strtab + sym->st_name;
3162 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size,3162 sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size,
3163 sym->st_info, sym->st_other,3163 sym->st_info, sym->st_other,
3164 sym->st_shndx, name);3164 sym->st_shndx, name);
3165 old_to_new_syms[i] = sym_index;3165 old_to_new_syms[i] = sym_index;
@@ -3430,7 +3430,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 264 unmodified lines
3430 if (sym_bind == STB_LOCAL)3430 if (sym_bind == STB_LOCAL)
3431 continue;3431 continue;
3432 name = (char *) dynstr + sym->st_name;3432 name = (char *) dynstr + sym->st_name;
3433 add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,3433 set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
3434 sym->st_info, sym->st_other, sym->st_shndx, name);3434 sym->st_info, sym->st_other, sym->st_shndx, name);
3435 }3435 }
3436 3436
tccgen.c+1−1
@@ -300,7 +300,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, · 299 unmodified lines
300 name = get_tok_str(sym->asm_label, NULL);300 name = get_tok_str(sym->asm_label, NULL);
301 }301 }
302 info = ELFW(ST_INFO)(sym_bind, sym_type);302 info = ELFW(ST_INFO)(sym_bind, sym_type);
303 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);303 sym->c = set_elf_sym(symtab_section, value, size, info, other, sh_num, name);
304 } else {304 } else {
305 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];305 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
306 esym->st_value = value;306 esym->st_value = value;
tccpe.c+2−2
@@ -1494,7 +1494,7 @@ ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2) · 1493 unmodified lines
1494 1494
1495ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)1495ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)
1496{1496{
1497 return add_elf_sym(1497 return set_elf_sym(
1498 s1->dynsymtab_section,1498 s1->dynsymtab_section,
1499 value,1499 value,
1500 dllindex, /* st_size */1500 dllindex, /* st_size */
@@ -1770,7 +1770,7 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 269 unmodified lines
1770 1770
1771 /* grab the startup code from libtcc1 */1771 /* grab the startup code from libtcc1 */
1772 if (TCC_OUTPUT_MEMORY != s1->output_type || PE_GUI == pe_type)1772 if (TCC_OUTPUT_MEMORY != s1->output_type || PE_GUI == pe_type)
1773 add_elf_sym(symtab_section,1773 set_elf_sym(symtab_section,
1774 0, 0,1774 0, 0,
1775 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,1775 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1776 SHN_UNDEF, start_symbol);1776 SHN_UNDEF, start_symbol);

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