History

tccpe: build IAT vector with with -run too

This prepares for x86_64 where we need the vector for far jumps. Also, resolve DLL symbols in place

grischka committed 17 years ago

Commit 370cdeb1 parent bed17844 files · +42 −68Browse files

Unified · Split

4 files
  1. libtcc.c +5 −8
  2. tcc.h +1 −0
  3. tccelf.c +4 −2
  4. tccpe.c +32 −58
libtcc.c+5−8
@@ -212,12 +212,9 @@ static int tcc_add_file_internal(TCCState *s, const char *filename, int flags); · 211 unmodified lines
212212 int tcc_output_coff(TCCState *s1, FILE *f);
213213
214214 /* tccpe.c */
215−void *resolve_sym(TCCState *s1, const char *sym, int type);
216215 int pe_load_def_file(struct TCCState *s1, int fd);
217216 int pe_test_res_file(void *v, int size);
218217 int pe_load_res_file(struct TCCState *s1, int fd);
219−void pe_add_runtime(struct TCCState *s1);
220−void pe_guess_outfile(char *objfilename, int output_type);
221218 int pe_output_file(struct TCCState *s1, const char *filename);
222219
223220 /* tccasm.c */
@@ -306,7 +303,7 @@ static inline int toup(int c) · 82 unmodified lines
306303 return c;
307304 }
308305
309−void *resolve_sym(TCCState *s1, const char *sym, int type);
306+void *resolve_sym(TCCState *s1, const char *sym);
310307
311308 /********************************************************/
312309
@@ -412,7 +409,7 @@ void *resolve_sym(TCCState *s1, const char *symbol, int type) · 99 unmodified lines
412409
413410 #include <dlfcn.h>
414411
415−void *resolve_sym(TCCState *s1, const char *sym, int type)
412+void *resolve_sym(TCCState *s1, const char *sym)
416413 {
417414 return dlsym(RTLD_DEFAULT, sym);
418415 }
@@ -1666,15 +1663,15 @@ int tcc_relocate(TCCState *s1, void *ptr) · 1247 unmodified lines
16661663 s1->runtime_added = 1;
16671664 s1->nb_errors = 0;
16681665 #ifdef TCC_TARGET_PE
1669− pe_add_runtime(s1);
1670− relocate_common_syms();
1671− tcc_add_linker_symbols(s1);
1666+ pe_output_file(s1, NULL);
16721667 #else
16731668 tcc_add_runtime(s1);
16741669 relocate_common_syms();
16751670 tcc_add_linker_symbols(s1);
16761671 build_got_entries(s1);
16771672 #endif
1673+ if (s1->nb_errors)
1674+ return -1;
16781675 }
16791676
16801677 offset = 0, mem = (unsigned long)ptr;
tcc.h+1−0
@@ -46,6 +46,7 @@ · 45 unmodified lines
4646 #include <direct.h> /* getcwd */
4747 #define inline __inline
4848 #define inp next_inp
49+#define dlclose FreeLibrary
4950 #endif
5051
5152 #ifndef _WIN32
tccelf.c+4−2
@@ -432,7 +432,6 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 431 unmodified lines
432432 ElfW(Sym) *sym, *esym, *sym_end;
433433 int sym_bind, sh_num, sym_index;
434434 const char *name;
435− unsigned long addr;
436435
437436 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
438437 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
@@ -442,12 +441,15 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 3 unmodified lines
442441 if (sh_num == SHN_UNDEF) {
443442 name = strtab_section->data + sym->st_name;
444443 if (do_resolve) {
444+#ifndef _WIN32
445+ unsigned long addr;
445446 name = symtab_section->link->data + sym->st_name;
446− addr = (unsigned long)resolve_sym(s1, name, ELFW(ST_TYPE)(sym->st_info));
447+ addr = (unsigned long)resolve_sym(s1, name);
447448 if (addr) {
448449 sym->st_value = addr;
449450 goto found;
450451 }
452+#endif
451453 } else if (s1->dynsym) {
452454 /* if dynamic symbol exist, then use it */
453455 sym_index = find_elf_sym(s1->dynsym, name);
tccpe.c+32−58
@@ -36,6 +36,10 @@ · 35 unmodified lines
3636 #define PE_MERGE_DATA
3737 // #define PE_PRINT_SECTIONS
3838
39+#if defined _WIN32 && (defined _WIN64) == (defined TCC_TARGET_X86_64)
40+#define TCC_IS_NATIVE
41+#endif
42+
3943 /* ----------------------------------------------------------- */
4044 #ifndef IMAGE_NT_SIGNATURE
4145 /* ----------------------------------------------------------- */
@@ -411,6 +415,7 @@ struct pe_info { · 369 unmodified lines
411415 #define PE_DLL 1
412416 #define PE_GUI 2
413417 #define PE_EXE 3
418+#define PE_RUN 4
414419
415420 void error_noabort(const char *, ...);
416421
@@ -455,54 +460,11 @@ ST_FN int pe_find_import(TCCState * s1, const char *symbol) · 38 unmodified lines
455460 do {
456461 s = n ? get_alt_symbol(buffer, symbol) : symbol;
457462 sym_index = find_elf_sym(s1->dynsymtab_section, s);
458− // printf("find %d %s\n", sym_index, s);
463+ // printf("find (%d) %d %s\n", n, sym_index, s);
459464 } while (0 == sym_index && ++n < 2);
460465 return sym_index;
461466 }
462467
463−#if defined _WIN32 || defined __CYGWIN__
464−
465−#ifdef __CYGWIN__
466−# include <dlfcn.h>
467−# define LoadLibrary(s) dlopen(s, RTLD_NOW)
468−# define GetProcAddress(h,s) dlsym(h, s)
469−#else
470−# define dlclose(h) FreeLibrary(h)
471−#endif
472−
473−/* for the -run option: dynamically load symbol from dll */
474−void *resolve_sym(struct TCCState *s1, const char *symbol, int type)
475−{
476− char buffer[100];
477− int sym_index, dll_index;
478− void *addr, **m;
479− DLLReference *dllref;
480−
481− sym_index = pe_find_import(s1, symbol);
482− if (0 == sym_index)
483− return NULL;
484− dll_index = ((Elf32_Sym *)s1->dynsymtab_section->data + sym_index)->st_value;
485− dllref = s1->loaded_dlls[dll_index-1];
486− if ( !dllref->handle )
487− {
488− dllref->handle = LoadLibrary(dllref->name);
489− }
490− addr = GetProcAddress(dllref->handle, symbol);
491− if (NULL == addr)
492− addr = GetProcAddress(dllref->handle, get_alt_symbol(buffer, symbol));
493−
494− if (addr && STT_OBJECT == type) {
495− /* need to return a pointer to the address for data objects */
496− m = (void**)tcc_malloc(sizeof addr), *m = addr, addr = m;
497−#ifdef MEM_DEBUG
498− /* yep, we don't free it */
499− mem_cur_size -= sizeof (void*);
500−#endif
501− }
502− return addr;
503−}
504−#endif
505−
506468 /*----------------------------------------------------------------------------*/
507469
508470 ST_FN int dynarray_assoc(void **pp, int n, int key)
@@ -729,7 +691,8 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 220 unmodified lines
729691
730692 for (i = 0; i < pe->imp_count; ++i) {
731693 struct pe_import_header *hdr;
732− int k, n, v;
694+ int k, n;
695+ DWORD v;
733696 struct pe_import_info *p = pe->imp_info[i];
734697 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;
735698
@@ -743,7 +706,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 7 unmodified lines
743706
744707 for (k = 0, n = p->sym_count; k <= n; ++k) {
745708 if (k < n) {
746− DWORD iat_index = p->symbols[k]->iat_index;
709+ int iat_index = p->symbols[k]->iat_index;
747710 int sym_index = p->symbols[k]->sym_index;
748711 Elf32_Sym *imp_sym = (Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index;
749712 Elf32_Sym *org_sym = (Elf32_Sym *)symtab_section->data + iat_index;
@@ -754,7 +717,16 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 4 unmodified lines
754717 v = pe->thunk->data_offset + rva_base;
755718 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
756719 put_elf_str(pe->thunk, name);
757−
720+#ifdef TCC_IS_NATIVE
721+ if (pe->type == PE_RUN) {
722+ DLLReference *dllref = pe->s1->loaded_dlls[imp_sym->st_value-1];
723+ if ( !dllref->handle )
724+ dllref->handle = LoadLibrary(dllref->name);
725+ v = (DWORD)GetProcAddress(dllref->handle, name);
726+ if (!v)
727+ error_noabort("undefined symbol '%s'", name);
728+ }
729+#endif
758730 } else {
759731 v = 0; /* last entry is zero */
760732 }
@@ -1488,6 +1460,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 727 unmodified lines
14881460 tcc_add_library(s1, "gdi32");
14891461 }
14901462 }
1463+ pe->type = pe_type;
14911464
14921465 if (start_symbol) {
14931466 addr = (unsigned long)tcc_get_symbol_err(s1, start_symbol);
@@ -1498,16 +1471,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 4 unmodified lines
14981471 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
14991472 text_section->sh_num, "main");
15001473 }
1501−
1502− if (pe) {
1503− pe->type = pe_type;
1504− pe->start_addr = addr;
1505− }
1506−}
1507−
1508−PUB_FN void pe_add_runtime(TCCState *s1)
1509−{
1510− pe_add_runtime_ex(s1, NULL);
1474+ pe->start_addr = addr;
15111475 }
15121476
15131477 PUB_FN int pe_output_file(TCCState * s1, const char *filename)
@@ -1525,7 +1489,10 @@ PUB_FN int pe_output_file(TCCState * s1, const char *filename) · 11 unmodified lines
15251489 tcc_add_linker_symbols(s1);
15261490
15271491 ret = pe_check_symbols(&pe);
1528− if (0 == ret) {
1492+ if (ret)
1493+ return ret;
1494+
1495+ if (filename) {
15291496 if (PE_DLL == pe.type) {
15301497 pe.reloc = new_section(pe.s1, ".reloc", SHT_PROGBITS, 0);
15311498 pe.imagebase = 0x10000000;
@@ -1546,6 +1513,13 @@ PUB_FN int pe_output_file(TCCState * s1, const char *filename) · 14 unmodified lines
15461513 else
15471514 ret = pe_write(&pe);
15481515 tcc_free(pe.sec_info);
1516+ } else {
1517+#ifndef TCC_IS_NATIVE
1518+ error_noabort("-run supported only on native platform");
1519+#endif
1520+ pe.type = PE_RUN;
1521+ pe.thunk = data_section;
1522+ pe_build_imports(&pe);
15491523 }
15501524
15511525 #ifdef PE_PRINT_SECTIONS

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