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
212int tcc_output_coff(TCCState *s1, FILE *f);212int tcc_output_coff(TCCState *s1, FILE *f);
213 213
214/* tccpe.c */214/* tccpe.c */
215void *resolve_sym(TCCState *s1, const char *sym, int type);
216int pe_load_def_file(struct TCCState *s1, int fd);215int pe_load_def_file(struct TCCState *s1, int fd);
217int pe_test_res_file(void *v, int size);216int pe_test_res_file(void *v, int size);
218int pe_load_res_file(struct TCCState *s1, int fd);217int pe_load_res_file(struct TCCState *s1, int fd);
219void pe_add_runtime(struct TCCState *s1);
220void pe_guess_outfile(char *objfilename, int output_type);
221int pe_output_file(struct TCCState *s1, const char *filename);218int pe_output_file(struct TCCState *s1, const char *filename);
222 219
223/* tccasm.c */220/* tccasm.c */
@@ -306,7 +303,7 @@ static inline int toup(int c) · 82 unmodified lines
306 return c;303 return c;
307}304}
308 305
309void *resolve_sym(TCCState *s1, const char *sym, int type);306void *resolve_sym(TCCState *s1, const char *sym);
310 307
311/********************************************************/308/********************************************************/
312 309
@@ -412,7 +409,7 @@ void *resolve_sym(TCCState *s1, const char *symbol, int type) · 99 unmodified lines
412 409
413#include <dlfcn.h>410#include <dlfcn.h>
414 411
415void *resolve_sym(TCCState *s1, const char *sym, int type)412void *resolve_sym(TCCState *s1, const char *sym)
416{413{
417 return dlsym(RTLD_DEFAULT, sym);414 return dlsym(RTLD_DEFAULT, sym);
418}415}
@@ -1666,15 +1663,15 @@ int tcc_relocate(TCCState *s1, void *ptr) · 1247 unmodified lines
1666 s1->runtime_added = 1;1663 s1->runtime_added = 1;
1667 s1->nb_errors = 0;1664 s1->nb_errors = 0;
1668#ifdef TCC_TARGET_PE1665#ifdef TCC_TARGET_PE
1669 pe_add_runtime(s1);1666 pe_output_file(s1, NULL);
1670 relocate_common_syms();
1671 tcc_add_linker_symbols(s1);
1672#else1667#else
1673 tcc_add_runtime(s1);1668 tcc_add_runtime(s1);
1674 relocate_common_syms();1669 relocate_common_syms();
1675 tcc_add_linker_symbols(s1);1670 tcc_add_linker_symbols(s1);
1676 build_got_entries(s1);1671 build_got_entries(s1);
1677#endif1672#endif
1673 if (s1->nb_errors)
1674 return -1;
1678 }1675 }
1679 1676
1680 offset = 0, mem = (unsigned long)ptr;1677 offset = 0, mem = (unsigned long)ptr;
tcc.h+1−0
@@ -46,6 +46,7 @@ · 45 unmodified lines
46#include <direct.h> /* getcwd */46#include <direct.h> /* getcwd */
47#define inline __inline47#define inline __inline
48#define inp next_inp48#define inp next_inp
49#define dlclose FreeLibrary
49#endif50#endif
50 51
51#ifndef _WIN3252#ifndef _WIN32
tccelf.c+4−2
@@ -432,7 +432,6 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 431 unmodified lines
432 ElfW(Sym) *sym, *esym, *sym_end;432 ElfW(Sym) *sym, *esym, *sym_end;
433 int sym_bind, sh_num, sym_index;433 int sym_bind, sh_num, sym_index;
434 const char *name;434 const char *name;
435 unsigned long addr;
436 435
437 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);436 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
438 for(sym = (ElfW(Sym) *)symtab_section->data + 1; 437 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
@@ -442,12 +441,15 @@ static void relocate_syms(TCCState *s1, int do_resolve) · 3 unmodified lines
442 if (sh_num == SHN_UNDEF) {441 if (sh_num == SHN_UNDEF) {
443 name = strtab_section->data + sym->st_name;442 name = strtab_section->data + sym->st_name;
444 if (do_resolve) {443 if (do_resolve) {
444#ifndef _WIN32
445 unsigned long addr;
445 name = symtab_section->link->data + sym->st_name;446 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);
447 if (addr) {448 if (addr) {
448 sym->st_value = addr;449 sym->st_value = addr;
449 goto found;450 goto found;
450 }451 }
452#endif
451 } else if (s1->dynsym) {453 } else if (s1->dynsym) {
452 /* if dynamic symbol exist, then use it */454 /* if dynamic symbol exist, then use it */
453 sym_index = find_elf_sym(s1->dynsym, name);455 sym_index = find_elf_sym(s1->dynsym, name);
tccpe.c+32−58
@@ -36,6 +36,10 @@ · 35 unmodified lines
36#define PE_MERGE_DATA36#define PE_MERGE_DATA
37// #define PE_PRINT_SECTIONS37// #define PE_PRINT_SECTIONS
38 38
39#if defined _WIN32 && (defined _WIN64) == (defined TCC_TARGET_X86_64)
40#define TCC_IS_NATIVE
41#endif
42
39/* ----------------------------------------------------------- */43/* ----------------------------------------------------------- */
40#ifndef IMAGE_NT_SIGNATURE44#ifndef IMAGE_NT_SIGNATURE
41/* ----------------------------------------------------------- */45/* ----------------------------------------------------------- */
@@ -411,6 +415,7 @@ struct pe_info { · 369 unmodified lines
411#define PE_DLL 1415#define PE_DLL 1
412#define PE_GUI 2416#define PE_GUI 2
413#define PE_EXE 3417#define PE_EXE 3
418#define PE_RUN 4
414 419
415void error_noabort(const char *, ...);420void error_noabort(const char *, ...);
416 421
@@ -455,54 +460,11 @@ ST_FN int pe_find_import(TCCState * s1, const char *symbol) · 38 unmodified lines
455 do {460 do {
456 s = n ? get_alt_symbol(buffer, symbol) : symbol;461 s = n ? get_alt_symbol(buffer, symbol) : symbol;
457 sym_index = find_elf_sym(s1->dynsymtab_section, s);462 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);
459 } while (0 == sym_index && ++n < 2);464 } while (0 == sym_index && ++n < 2);
460 return sym_index;465 return sym_index;
461}466}
462 467
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 */
474void *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
506/*----------------------------------------------------------------------------*/468/*----------------------------------------------------------------------------*/
507 469
508ST_FN int dynarray_assoc(void **pp, int n, int key)470ST_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
729 691
730 for (i = 0; i < pe->imp_count; ++i) {692 for (i = 0; i < pe->imp_count; ++i) {
731 struct pe_import_header *hdr;693 struct pe_import_header *hdr;
732 int k, n, v;694 int k, n;
695 DWORD v;
733 struct pe_import_info *p = pe->imp_info[i];696 struct pe_import_info *p = pe->imp_info[i];
734 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;697 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;
735 698
@@ -743,7 +706,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 7 unmodified lines
743 706
744 for (k = 0, n = p->sym_count; k <= n; ++k) {707 for (k = 0, n = p->sym_count; k <= n; ++k) {
745 if (k < n) {708 if (k < n) {
746 DWORD iat_index = p->symbols[k]->iat_index;709 int iat_index = p->symbols[k]->iat_index;
747 int sym_index = p->symbols[k]->sym_index;710 int sym_index = p->symbols[k]->sym_index;
748 Elf32_Sym *imp_sym = (Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index;711 Elf32_Sym *imp_sym = (Elf32_Sym *)pe->s1->dynsymtab_section->data + sym_index;
749 Elf32_Sym *org_sym = (Elf32_Sym *)symtab_section->data + iat_index;712 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
754 v = pe->thunk->data_offset + rva_base;717 v = pe->thunk->data_offset + rva_base;
755 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */718 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
756 put_elf_str(pe->thunk, name);719 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
758 } else {730 } else {
759 v = 0; /* last entry is zero */731 v = 0; /* last entry is zero */
760 }732 }
@@ -1488,6 +1460,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 727 unmodified lines
1488 tcc_add_library(s1, "gdi32");1460 tcc_add_library(s1, "gdi32");
1489 }1461 }
1490 }1462 }
1463 pe->type = pe_type;
1491 1464
1492 if (start_symbol) {1465 if (start_symbol) {
1493 addr = (unsigned long)tcc_get_symbol_err(s1, start_symbol);1466 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
1498 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,1471 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1499 text_section->sh_num, "main");1472 text_section->sh_num, "main");
1500 }1473 }
1501 1474 pe->start_addr = addr;
1502 if (pe) {
1503 pe->type = pe_type;
1504 pe->start_addr = addr;
1505 }
1506}
1507
1508PUB_FN void pe_add_runtime(TCCState *s1)
1509{
1510 pe_add_runtime_ex(s1, NULL);
1511}1475}
1512 1476
1513PUB_FN int pe_output_file(TCCState * s1, const char *filename)1477PUB_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
1525 tcc_add_linker_symbols(s1);1489 tcc_add_linker_symbols(s1);
1526 1490
1527 ret = pe_check_symbols(&pe);1491 ret = pe_check_symbols(&pe);
1528 if (0 == ret) {1492 if (ret)
1493 return ret;
1494
1495 if (filename) {
1529 if (PE_DLL == pe.type) {1496 if (PE_DLL == pe.type) {
1530 pe.reloc = new_section(pe.s1, ".reloc", SHT_PROGBITS, 0);1497 pe.reloc = new_section(pe.s1, ".reloc", SHT_PROGBITS, 0);
1531 pe.imagebase = 0x10000000;1498 pe.imagebase = 0x10000000;
@@ -1546,6 +1513,13 @@ PUB_FN int pe_output_file(TCCState * s1, const char *filename) · 14 unmodified lines
1546 else1513 else
1547 ret = pe_write(&pe);1514 ret = pe_write(&pe);
1548 tcc_free(pe.sec_info);1515 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);
1549 }1523 }
1550 1524
1551#ifdef PE_PRINT_SECTIONS1525#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