History

better st_other support

bellard committed 21 years ago

Commit d733dc71 parent ef156f02 files · +47 −43Browse files

Unified · Split

2 files
  1. tccelf.c +23 −14
  2. tccpe.c +24 −29
tccelf.c+23−14
@@ -178,7 +178,7 @@ void *tcc_get_symbol_err(TCCState *s, const char *name) · 177 unmodified lines
178178 /* add an elf symbol : check if it is already defined and patch
179179 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
180180 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
181− int info, int sh_num, const char *name)
181+ int info, int other, int sh_num, const char *name)
182182 {
183183 Elf32_Sym *esym;
184184 int sym_bind, sym_index, sym_type, esym_bind;
@@ -217,11 +217,12 @@ static int add_elf_sym(Section *s, unsigned long value, unsigned long size, · 32 unmodified lines
217217 esym->st_shndx = sh_num;
218218 esym->st_value = value;
219219 esym->st_size = size;
220+ esym->st_other = other;
220221 }
221222 } else {
222223 do_def:
223224 sym_index = put_elf_sym(s, value, size,
224− ELF32_ST_INFO(sym_bind, sym_type), 0,
225+ ELF32_ST_INFO(sym_bind, sym_type), other,
225226 sh_num, name);
226227 }
227228 return sym_index;
@@ -675,7 +676,7 @@ static void build_got(TCCState *s1) · 447 unmodified lines
675676 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
676677 s1->got->sh_entsize = 4;
677678 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
678− s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
679+ 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
679680 ptr = section_ptr_add(s1->got, 3 * sizeof(int));
680681 /* keep space for _DYNAMIC pointer, if present */
681682 put32(ptr, 0);
@@ -943,11 +944,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) · 261 unmodified lines
943944
944945 add_elf_sym(symtab_section,
945946 0, 0,
946− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
947+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
947948 s->sh_num, sym_start);
948949 add_elf_sym(symtab_section,
949950 end_offset, 0,
950− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
951+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
951952 s->sh_num, sym_end);
952953 }
953954
@@ -967,7 +968,7 @@ static void tcc_add_runtime(TCCState *s1) · 13 unmodified lines
967968 ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
968969 *ptr = 0;
969970 add_elf_sym(symtab_section, 0, 0,
970− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
971+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
971972 bounds_section->sh_num, "__bounds_start");
972973 /* add bound check code */
973974 snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o");
@@ -1010,15 +1011,15 @@ static void tcc_add_linker_symbols(TCCState *s1) · 36 unmodified lines
10101011
10111012 add_elf_sym(symtab_section,
10121013 text_section->data_offset, 0,
1013− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1014+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10141015 text_section->sh_num, "_etext");
10151016 add_elf_sym(symtab_section,
10161017 data_section->data_offset, 0,
1017− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1018+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10181019 data_section->sh_num, "_edata");
10191020 add_elf_sym(symtab_section,
10201021 bss_section->data_offset, 0,
1021− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1022+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10221023 bss_section->sh_num, "_end");
10231024 /* horrible new standard ldscript defines */
10241025 add_init_array_defines(s1, ".preinit_array");
@@ -1047,12 +1048,12 @@ static void tcc_add_linker_symbols(TCCState *s1) · 22 unmodified lines
10471048 snprintf(buf, sizeof(buf), "__start_%s", s->name);
10481049 add_elf_sym(symtab_section,
10491050 0, 0,
1050− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1051+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10511052 s->sh_num, buf);
10521053 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
10531054 add_elf_sym(symtab_section,
10541055 s->data_offset, 0,
1055− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1056+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10561057 s->sh_num, buf);
10571058 }
10581059 next_sec: ;
@@ -1919,7 +1920,8 @@ static int tcc_load_object_file(TCCState *s1, · 860 unmodified lines
19191920 /* add symbol */
19201921 name = strtab + sym->st_name;
19211922 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size,
1922− sym->st_info, sym->st_shndx, name);
1923+ sym->st_info, sym->st_other,
1924+ sym->st_shndx, name);
19231925 old_to_new_syms[i] = sym_index;
19241926 }
19251927
@@ -2093,7 +2095,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 167 unmodified lines
20932095 {
20942096 Elf32_Ehdr ehdr;
20952097 Elf32_Shdr *shdr, *sh, *sh1;
2096− int i, nb_syms, nb_dts, sym_bind, ret;
2098+ int i, nb_syms, nb_dts, sym_bind, ret, other;
20972099 Elf32_Sym *sym, *dynsym;
20982100 Elf32_Dyn *dt, *dynamic;
20992101 unsigned char *dynstr;
@@ -2173,8 +2175,15 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 73 unmodified lines
21732175 if (sym_bind == STB_LOCAL)
21742176 continue;
21752177 name = dynstr + sym->st_name;
2178+#ifdef TCC_TARGET_PE
2179+ /* in the PE format we need to know the DLL from which the
2180+ symbol comes. XXX: add a new array for that ? */
2181+ other = s1->nb_loaded_dlls - 1;
2182+#else
2183+ other = sym->st_other;
2184+#endif
21762185 add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,
2177− sym->st_info, sym->st_shndx, name);
2186+ sym->st_info, other, sym->st_shndx, name);
21782187 }
21792188
21802189 /* load all referenced DLLs */
tccpe.c+24−29
@@ -414,24 +414,25 @@ void *resolve_sym(struct TCCState *s1, const char *symbol, int type) · 413 unmodified lines
414414 char buffer[100], *p = buffer;
415415 void *a = NULL;
416416 int sym_index = pe_find_import(s1, symbol, p);
417+ int dll_index;
418+ const char *dll_name;
419+ void *hm;
420+
417421 if (sym_index) {
418− int dll_index =
419− ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index].
422+ dll_index = ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index].
420423 st_other;
421− if (dll_index) {
422− const char *dll_name = s1->loaded_dlls[dll_index - 1]->name;
423− void *hm = GetModuleHandleA(dll_name);
424− if (NULL == hm)
425− hm = LoadLibraryA(dll_name);
426− if (hm) {
427− a = GetProcAddress(hm, buffer);
428− if (a && STT_OBJECT == type) {
429− // need to return a pointer to the address for data objects
430− dynarray_add(&pe_imp, &nb_pe_imp, a);
431− a = &pe_imp[nb_pe_imp - 1];
432− }
433− }
434− }
424+ dll_name = s1->loaded_dlls[dll_index]->name;
425+ hm = GetModuleHandleA(dll_name);
426+ if (NULL == hm)
427+ hm = LoadLibraryA(dll_name);
428+ if (hm) {
429+ a = GetProcAddress(hm, buffer);
430+ if (a && STT_OBJECT == type) {
431+ // need to return a pointer to the address for data objects
432+ dynarray_add(&pe_imp, &nb_pe_imp, a);
433+ a = &pe_imp[nb_pe_imp - 1];
434+ }
435+ }
435436 }
436437 return a;
437438 }
@@ -642,9 +643,6 @@ ST int pe_add_import(struct pe_info *pe, int sym_index, DWORD offset) · 204 unmodified lines
642643 dll_index =
643644 ((Elf32_Sym *) pe->s1->dynsymtab_section->data)[sym_index].
644645 st_other;
645− if (0 == dll_index)
646− return 0;
647−
648646 i = dynarray_assoc((void **) pe->imp_info, pe->imp_count, dll_index);
649647 if (-1 != i) {
650648 p = pe->imp_info[i];
@@ -694,7 +692,7 @@ ST void pe_build_imports(struct pe_info *pe) · 43 unmodified lines
694692 struct pe_import_header *hdr;
695693 int k, n, v;
696694 struct pe_import_info *p = pe->imp_info[i];
697− const char *name = pe->s1->loaded_dlls[p->dll_index - 1]->name;
695+ const char *name = pe->s1->loaded_dlls[p->dll_index]->name;
698696
699697 /* put the dll name into the import header */
700698 if (0 == strncmp(name, "lib", 3))
@@ -1079,7 +1077,7 @@ int pe_load_def_file(TCCState * s1, FILE * fp) · 378 unmodified lines
10791077 {
10801078 DLLReference *dllref;
10811079 int f = 0, sym_index;
1082− char *p, *p_other, line[120], dllname[40];
1080+ char *p, line[120], dllname[40];
10831081 while (fgets(line, sizeof line, fp)) {
10841082 p = strchr(line, 0);
10851083 while (p > line && p[-1] <= ' ')
@@ -1116,16 +1114,13 @@ int pe_load_def_file(TCCState * s1, FILE * fp) · 30 unmodified lines
11161114 ++f;
11171115
11181116 default:
1117+ /* tccpe needs to know from what dll it should import
1118+ the sym */
11191119 sym_index = add_elf_sym(s1->dynsymtab_section,
11201120 0, 0, ELF32_ST_INFO(STB_GLOBAL,
11211121 STT_FUNC),
1122+ s1->nb_loaded_dlls - 1,
11221123 text_section->sh_num, p);
1123− /*gr: tccpe needs to know from what dll it should import the sym */
1124− p_other =
1125− &((Elf32_Sym *) s1->dynsymtab_section->
1126− data)[sym_index].st_other;
1127− if (0 == *p_other)
1128− *p_other = s1->nb_loaded_dlls;
11291124 continue;
11301125 }
11311126 }
@@ -1151,7 +1146,7 @@ unsigned long pe_add_runtime(TCCState * s1) · 19 unmodified lines
11511146 if (start_symbol)
11521147 add_elf_sym(symtab_section,
11531148 0, 0,
1154− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1149+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
11551150 SHN_UNDEF, start_symbol);
11561151
11571152 if (0 == s1->nostdlib) {
@@ -1171,7 +1166,7 @@ unsigned long pe_add_runtime(TCCState * s1) · 13 unmodified lines
11711166 /* for -run GUI's, put '_runwinmain' instead of 'main' */
11721167 add_elf_sym(symtab_section,
11731168 addr, 0,
1174− ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
1169+ ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
11751170 text_section->sh_num, "main");
11761171
11771172 /* FreeConsole(); */

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