From d733dc752e8d19bdebca46678c70d04d4d1f6f76 Mon Sep 17 00:00:00 2001 From: bellard Date: Wed, 13 Apr 2005 21:37:06 +0000 Subject: [PATCH] better st_other support --- tccelf.c | 37 +++++++++++++++++++++++-------------- tccpe.c | 53 ++++++++++++++++++++++++----------------------------- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/tccelf.c b/tccelf.c index f835f31a..e1ca9013 100644 --- a/tccelf.c +++ b/tccelf.c @@ -178,7 +178,7 @@ void *tcc_get_symbol_err(TCCState *s, const char *name) /* add an elf symbol : check if it is already defined and patch it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */ static int add_elf_sym(Section *s, unsigned long value, unsigned long size, - int info, int sh_num, const char *name) + int info, int other, int sh_num, const char *name) { Elf32_Sym *esym; 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, esym->st_shndx = sh_num; esym->st_value = value; esym->st_size = size; + esym->st_other = other; } } else { do_def: sym_index = put_elf_sym(s, value, size, - ELF32_ST_INFO(sym_bind, sym_type), 0, + ELF32_ST_INFO(sym_bind, sym_type), other, sh_num, name); } return sym_index; @@ -675,7 +676,7 @@ static void build_got(TCCState *s1) s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); s1->got->sh_entsize = 4; add_elf_sym(symtab_section, 0, 4, ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT), - s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_"); + 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_"); ptr = section_ptr_add(s1->got, 3 * sizeof(int)); /* keep space for _DYNAMIC pointer, if present */ put32(ptr, 0); @@ -943,11 +944,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) add_elf_sym(symtab_section, 0, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, sym_start); add_elf_sym(symtab_section, end_offset, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, sym_end); } @@ -967,7 +968,7 @@ static void tcc_add_runtime(TCCState *s1) ptr = section_ptr_add(bounds_section, sizeof(unsigned long)); *ptr = 0; add_elf_sym(symtab_section, 0, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, bounds_section->sh_num, "__bounds_start"); /* add bound check code */ snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o"); @@ -1010,15 +1011,15 @@ static void tcc_add_linker_symbols(TCCState *s1) add_elf_sym(symtab_section, text_section->data_offset, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, text_section->sh_num, "_etext"); add_elf_sym(symtab_section, data_section->data_offset, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, data_section->sh_num, "_edata"); add_elf_sym(symtab_section, bss_section->data_offset, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, bss_section->sh_num, "_end"); /* horrible new standard ldscript defines */ add_init_array_defines(s1, ".preinit_array"); @@ -1047,12 +1048,12 @@ static void tcc_add_linker_symbols(TCCState *s1) snprintf(buf, sizeof(buf), "__start_%s", s->name); add_elf_sym(symtab_section, 0, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, buf); snprintf(buf, sizeof(buf), "__stop_%s", s->name); add_elf_sym(symtab_section, s->data_offset, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, buf); } next_sec: ; @@ -1919,7 +1920,8 @@ static int tcc_load_object_file(TCCState *s1, /* add symbol */ name = strtab + sym->st_name; sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size, - sym->st_info, sym->st_shndx, name); + sym->st_info, sym->st_other, + sym->st_shndx, name); old_to_new_syms[i] = sym_index; } @@ -2093,7 +2095,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) { Elf32_Ehdr ehdr; Elf32_Shdr *shdr, *sh, *sh1; - int i, nb_syms, nb_dts, sym_bind, ret; + int i, nb_syms, nb_dts, sym_bind, ret, other; Elf32_Sym *sym, *dynsym; Elf32_Dyn *dt, *dynamic; unsigned char *dynstr; @@ -2173,8 +2175,15 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) if (sym_bind == STB_LOCAL) continue; name = dynstr + sym->st_name; +#ifdef TCC_TARGET_PE + /* in the PE format we need to know the DLL from which the + symbol comes. XXX: add a new array for that ? */ + other = s1->nb_loaded_dlls - 1; +#else + other = sym->st_other; +#endif add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size, - sym->st_info, sym->st_shndx, name); + sym->st_info, other, sym->st_shndx, name); } /* load all referenced DLLs */ diff --git a/tccpe.c b/tccpe.c index 8ac4b505..338021fc 100644 --- a/tccpe.c +++ b/tccpe.c @@ -414,24 +414,25 @@ void *resolve_sym(struct TCCState *s1, const char *symbol, int type) char buffer[100], *p = buffer; void *a = NULL; int sym_index = pe_find_import(s1, symbol, p); + int dll_index; + const char *dll_name; + void *hm; + if (sym_index) { - int dll_index = - ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index]. + dll_index = ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index]. st_other; - if (dll_index) { - const char *dll_name = s1->loaded_dlls[dll_index - 1]->name; - void *hm = GetModuleHandleA(dll_name); - if (NULL == hm) - hm = LoadLibraryA(dll_name); - if (hm) { - a = GetProcAddress(hm, buffer); - if (a && STT_OBJECT == type) { - // need to return a pointer to the address for data objects - dynarray_add(&pe_imp, &nb_pe_imp, a); - a = &pe_imp[nb_pe_imp - 1]; - } - } - } + dll_name = s1->loaded_dlls[dll_index]->name; + hm = GetModuleHandleA(dll_name); + if (NULL == hm) + hm = LoadLibraryA(dll_name); + if (hm) { + a = GetProcAddress(hm, buffer); + if (a && STT_OBJECT == type) { + // need to return a pointer to the address for data objects + dynarray_add(&pe_imp, &nb_pe_imp, a); + a = &pe_imp[nb_pe_imp - 1]; + } + } } return a; } @@ -642,9 +643,6 @@ ST int pe_add_import(struct pe_info *pe, int sym_index, DWORD offset) dll_index = ((Elf32_Sym *) pe->s1->dynsymtab_section->data)[sym_index]. st_other; - if (0 == dll_index) - return 0; - i = dynarray_assoc((void **) pe->imp_info, pe->imp_count, dll_index); if (-1 != i) { p = pe->imp_info[i]; @@ -694,7 +692,7 @@ ST void pe_build_imports(struct pe_info *pe) struct pe_import_header *hdr; int k, n, v; struct pe_import_info *p = pe->imp_info[i]; - const char *name = pe->s1->loaded_dlls[p->dll_index - 1]->name; + const char *name = pe->s1->loaded_dlls[p->dll_index]->name; /* put the dll name into the import header */ if (0 == strncmp(name, "lib", 3)) @@ -1079,7 +1077,7 @@ int pe_load_def_file(TCCState * s1, FILE * fp) { DLLReference *dllref; int f = 0, sym_index; - char *p, *p_other, line[120], dllname[40]; + char *p, line[120], dllname[40]; while (fgets(line, sizeof line, fp)) { p = strchr(line, 0); while (p > line && p[-1] <= ' ') @@ -1116,16 +1114,13 @@ int pe_load_def_file(TCCState * s1, FILE * fp) ++f; default: + /* tccpe needs to know from what dll it should import + the sym */ sym_index = add_elf_sym(s1->dynsymtab_section, 0, 0, ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), + s1->nb_loaded_dlls - 1, text_section->sh_num, p); - /*gr: tccpe needs to know from what dll it should import the sym */ - p_other = - &((Elf32_Sym *) s1->dynsymtab_section-> - data)[sym_index].st_other; - if (0 == *p_other) - *p_other = s1->nb_loaded_dlls; continue; } } @@ -1151,7 +1146,7 @@ unsigned long pe_add_runtime(TCCState * s1) if (start_symbol) add_elf_sym(symtab_section, 0, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, SHN_UNDEF, start_symbol); if (0 == s1->nostdlib) { @@ -1171,7 +1166,7 @@ unsigned long pe_add_runtime(TCCState * s1) /* for -run GUI's, put '_runwinmain' instead of 'main' */ add_elf_sym(symtab_section, addr, 0, - ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, text_section->sh_num, "main"); /* FreeConsole(); */ -- 2.11.4.GIT