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
178/* add an elf symbol : check if it is already defined and patch178/* add an elf symbol : check if it is already defined and patch
179 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */179 it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
180static int add_elf_sym(Section *s, unsigned long value, unsigned long size,180static 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)
182{182{
183 Elf32_Sym *esym;183 Elf32_Sym *esym;
184 int sym_bind, sym_index, sym_type, esym_bind;184 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
217 esym->st_shndx = sh_num;217 esym->st_shndx = sh_num;
218 esym->st_value = value;218 esym->st_value = value;
219 esym->st_size = size;219 esym->st_size = size;
220 esym->st_other = other;
220 }221 }
221 } else {222 } else {
222 do_def:223 do_def:
223 sym_index = put_elf_sym(s, value, size, 224 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,
225 sh_num, name);226 sh_num, name);
226 }227 }
227 return sym_index;228 return sym_index;
@@ -675,7 +676,7 @@ static void build_got(TCCState *s1) · 447 unmodified lines
675 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);676 s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
676 s1->got->sh_entsize = 4;677 s1->got->sh_entsize = 4;
677 add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT), 678 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_");
679 ptr = section_ptr_add(s1->got, 3 * sizeof(int));680 ptr = section_ptr_add(s1->got, 3 * sizeof(int));
680 /* keep space for _DYNAMIC pointer, if present */681 /* keep space for _DYNAMIC pointer, if present */
681 put32(ptr, 0);682 put32(ptr, 0);
@@ -943,11 +944,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) · 261 unmodified lines
943 944
944 add_elf_sym(symtab_section, 945 add_elf_sym(symtab_section,
945 0, 0,946 0, 0,
946 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),947 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
947 s->sh_num, sym_start);948 s->sh_num, sym_start);
948 add_elf_sym(symtab_section, 949 add_elf_sym(symtab_section,
949 end_offset, 0,950 end_offset, 0,
950 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),951 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
951 s->sh_num, sym_end);952 s->sh_num, sym_end);
952}953}
953 954
@@ -967,7 +968,7 @@ static void tcc_add_runtime(TCCState *s1) · 13 unmodified lines
967 ptr = section_ptr_add(bounds_section, sizeof(unsigned long));968 ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
968 *ptr = 0;969 *ptr = 0;
969 add_elf_sym(symtab_section, 0, 0, 970 add_elf_sym(symtab_section, 0, 0,
970 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),971 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
971 bounds_section->sh_num, "__bounds_start");972 bounds_section->sh_num, "__bounds_start");
972 /* add bound check code */973 /* add bound check code */
973 snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o");974 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
1010 1011
1011 add_elf_sym(symtab_section, 1012 add_elf_sym(symtab_section,
1012 text_section->data_offset, 0,1013 text_section->data_offset, 0,
1013 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1014 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1014 text_section->sh_num, "_etext");1015 text_section->sh_num, "_etext");
1015 add_elf_sym(symtab_section, 1016 add_elf_sym(symtab_section,
1016 data_section->data_offset, 0,1017 data_section->data_offset, 0,
1017 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1018 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1018 data_section->sh_num, "_edata");1019 data_section->sh_num, "_edata");
1019 add_elf_sym(symtab_section, 1020 add_elf_sym(symtab_section,
1020 bss_section->data_offset, 0,1021 bss_section->data_offset, 0,
1021 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1022 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1022 bss_section->sh_num, "_end");1023 bss_section->sh_num, "_end");
1023 /* horrible new standard ldscript defines */1024 /* horrible new standard ldscript defines */
1024 add_init_array_defines(s1, ".preinit_array");1025 add_init_array_defines(s1, ".preinit_array");
@@ -1047,12 +1048,12 @@ static void tcc_add_linker_symbols(TCCState *s1) · 22 unmodified lines
1047 snprintf(buf, sizeof(buf), "__start_%s", s->name);1048 snprintf(buf, sizeof(buf), "__start_%s", s->name);
1048 add_elf_sym(symtab_section, 1049 add_elf_sym(symtab_section,
1049 0, 0,1050 0, 0,
1050 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1051 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1051 s->sh_num, buf);1052 s->sh_num, buf);
1052 snprintf(buf, sizeof(buf), "__stop_%s", s->name);1053 snprintf(buf, sizeof(buf), "__stop_%s", s->name);
1053 add_elf_sym(symtab_section,1054 add_elf_sym(symtab_section,
1054 s->data_offset, 0,1055 s->data_offset, 0,
1055 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1056 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1056 s->sh_num, buf);1057 s->sh_num, buf);
1057 }1058 }
1058 next_sec: ;1059 next_sec: ;
@@ -1919,7 +1920,8 @@ static int tcc_load_object_file(TCCState *s1, · 860 unmodified lines
1919 /* add symbol */1920 /* add symbol */
1920 name = strtab + sym->st_name;1921 name = strtab + sym->st_name;
1921 sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size, 1922 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);
1923 old_to_new_syms[i] = sym_index;1925 old_to_new_syms[i] = sym_index;
1924 }1926 }
1925 1927
@@ -2093,7 +2095,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 167 unmodified lines
2093{ 2095{
2094 Elf32_Ehdr ehdr;2096 Elf32_Ehdr ehdr;
2095 Elf32_Shdr *shdr, *sh, *sh1;2097 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;
2097 Elf32_Sym *sym, *dynsym;2099 Elf32_Sym *sym, *dynsym;
2098 Elf32_Dyn *dt, *dynamic;2100 Elf32_Dyn *dt, *dynamic;
2099 unsigned char *dynstr;2101 unsigned char *dynstr;
@@ -2173,8 +2175,15 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 73 unmodified lines
2173 if (sym_bind == STB_LOCAL)2175 if (sym_bind == STB_LOCAL)
2174 continue;2176 continue;
2175 name = dynstr + sym->st_name;2177 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
2176 add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size,2185 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);
2178 }2187 }
2179 2188
2180 /* load all referenced DLLs */2189 /* 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
414 char buffer[100], *p = buffer;414 char buffer[100], *p = buffer;
415 void *a = NULL;415 void *a = NULL;
416 int sym_index = pe_find_import(s1, symbol, p);416 int sym_index = pe_find_import(s1, symbol, p);
417 int dll_index;
418 const char *dll_name;
419 void *hm;
420
417 if (sym_index) {421 if (sym_index) {
418 int dll_index =422 dll_index = ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index].
419 ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index].
420 st_other;423 st_other;
421 if (dll_index) {424 dll_name = s1->loaded_dlls[dll_index]->name;
422 const char *dll_name = s1->loaded_dlls[dll_index - 1]->name;425 hm = GetModuleHandleA(dll_name);
423 void *hm = GetModuleHandleA(dll_name);426 if (NULL == hm)
424 if (NULL == hm)427 hm = LoadLibraryA(dll_name);
425 hm = LoadLibraryA(dll_name);428 if (hm) {
426 if (hm) {429 a = GetProcAddress(hm, buffer);
427 a = GetProcAddress(hm, buffer);430 if (a && STT_OBJECT == type) {
428 if (a && STT_OBJECT == type) {431 // need to return a pointer to the address for data objects
429 // need to return a pointer to the address for data objects432 dynarray_add(&pe_imp, &nb_pe_imp, a);
430 dynarray_add(&pe_imp, &nb_pe_imp, a);433 a = &pe_imp[nb_pe_imp - 1];
431 a = &pe_imp[nb_pe_imp - 1];434 }
432 }435 }
433 }
434 }
435 }436 }
436 return a;437 return a;
437}438}
@@ -642,9 +643,6 @@ ST int pe_add_import(struct pe_info *pe, int sym_index, DWORD offset) · 204 unmodified lines
642 dll_index =643 dll_index =
643 ((Elf32_Sym *) pe->s1->dynsymtab_section->data)[sym_index].644 ((Elf32_Sym *) pe->s1->dynsymtab_section->data)[sym_index].
644 st_other;645 st_other;
645 if (0 == dll_index)
646 return 0;
647
648 i = dynarray_assoc((void **) pe->imp_info, pe->imp_count, dll_index);646 i = dynarray_assoc((void **) pe->imp_info, pe->imp_count, dll_index);
649 if (-1 != i) {647 if (-1 != i) {
650 p = pe->imp_info[i];648 p = pe->imp_info[i];
@@ -694,7 +692,7 @@ ST void pe_build_imports(struct pe_info *pe) · 43 unmodified lines
694 struct pe_import_header *hdr;692 struct pe_import_header *hdr;
695 int k, n, v;693 int k, n, v;
696 struct pe_import_info *p = pe->imp_info[i];694 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;
698 696
699 /* put the dll name into the import header */697 /* put the dll name into the import header */
700 if (0 == strncmp(name, "lib", 3))698 if (0 == strncmp(name, "lib", 3))
@@ -1079,7 +1077,7 @@ int pe_load_def_file(TCCState * s1, FILE * fp) · 378 unmodified lines
1079{1077{
1080 DLLReference *dllref;1078 DLLReference *dllref;
1081 int f = 0, sym_index;1079 int f = 0, sym_index;
1082 char *p, *p_other, line[120], dllname[40];1080 char *p, line[120], dllname[40];
1083 while (fgets(line, sizeof line, fp)) {1081 while (fgets(line, sizeof line, fp)) {
1084 p = strchr(line, 0);1082 p = strchr(line, 0);
1085 while (p > line && p[-1] <= ' ')1083 while (p > line && p[-1] <= ' ')
@@ -1116,16 +1114,13 @@ int pe_load_def_file(TCCState * s1, FILE * fp) · 30 unmodified lines
1116 ++f;1114 ++f;
1117 1115
1118 default:1116 default:
1117 /* tccpe needs to know from what dll it should import
1118 the sym */
1119 sym_index = add_elf_sym(s1->dynsymtab_section,1119 sym_index = add_elf_sym(s1->dynsymtab_section,
1120 0, 0, ELF32_ST_INFO(STB_GLOBAL,1120 0, 0, ELF32_ST_INFO(STB_GLOBAL,
1121 STT_FUNC),1121 STT_FUNC),
1122 s1->nb_loaded_dlls - 1,
1122 text_section->sh_num, p);1123 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;
1129 continue;1124 continue;
1130 }1125 }
1131 }1126 }
@@ -1151,7 +1146,7 @@ unsigned long pe_add_runtime(TCCState * s1) · 19 unmodified lines
1151 if (start_symbol)1146 if (start_symbol)
1152 add_elf_sym(symtab_section,1147 add_elf_sym(symtab_section,
1153 0, 0,1148 0, 0,
1154 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1149 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1155 SHN_UNDEF, start_symbol);1150 SHN_UNDEF, start_symbol);
1156 1151
1157 if (0 == s1->nostdlib) {1152 if (0 == s1->nostdlib) {
@@ -1171,7 +1166,7 @@ unsigned long pe_add_runtime(TCCState * s1) · 13 unmodified lines
1171 /* for -run GUI's, put '_runwinmain' instead of 'main' */1166 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1172 add_elf_sym(symtab_section,1167 add_elf_sym(symtab_section,
1173 addr, 0,1168 addr, 0,
1174 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),1169 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1175 text_section->sh_num, "main");1170 text_section->sh_num, "main");
1176 1171
1177 /* FreeConsole(); */1172 /* 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