History

tccpe: support pe32+ (x86_64) target

grischka committed 17 years ago

Commit f366cb21 parent fe8def31 file · +82 −36Browse files

Unified · Split

tccpe.c+82−36
@@ -36,6 +36,12 @@ · 35 unmodified lines
36#define PE_MERGE_DATA36#define PE_MERGE_DATA
37// #define PE_PRINT_SECTIONS37// #define PE_PRINT_SECTIONS
38 38
39#ifdef TCC_TARGET_X86_64
40# define ADDR3264 ULONGLONG
41#else
42# define ADDR3264 DWORD
43#endif
44
39#if defined _WIN32 && (defined _WIN64) == (defined TCC_TARGET_X86_64)45#if defined _WIN32 && (defined _WIN64) == (defined TCC_TARGET_X86_64)
40#define TCC_IS_NATIVE46#define TCC_IS_NATIVE
41#endif47#endif
@@ -48,6 +54,7 @@ · 6 unmodified lines
48typedef unsigned char BYTE;54typedef unsigned char BYTE;
49typedef unsigned short WORD;55typedef unsigned short WORD;
50typedef unsigned long DWORD;56typedef unsigned long DWORD;
57typedef unsigned long long ULONGLONG;
51#pragma pack(push, 1)58#pragma pack(push, 1)
52 59
53typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */60typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */
@@ -104,10 +111,11 @@ typedef struct _IMAGE_OPTIONAL_HEADER { · 50 unmodified lines
104 DWORD SizeOfUninitializedData;111 DWORD SizeOfUninitializedData;
105 DWORD AddressOfEntryPoint;112 DWORD AddressOfEntryPoint;
106 DWORD BaseOfCode;113 DWORD BaseOfCode;
114#ifndef TCC_TARGET_X86_64
107 DWORD BaseOfData;115 DWORD BaseOfData;
108 116#endif
109 /* NT additional fields. */117 /* NT additional fields. */
110 DWORD ImageBase;118 ADDR3264 ImageBase;
111 DWORD SectionAlignment;119 DWORD SectionAlignment;
112 DWORD FileAlignment;120 DWORD FileAlignment;
113 WORD MajorOperatingSystemVersion;121 WORD MajorOperatingSystemVersion;
@@ -122,16 +130,14 @@ typedef struct _IMAGE_OPTIONAL_HEADER { · 8 unmodified lines
122 DWORD CheckSum;130 DWORD CheckSum;
123 WORD Subsystem;131 WORD Subsystem;
124 WORD DllCharacteristics;132 WORD DllCharacteristics;
125 DWORD SizeOfStackReserve;133 ADDR3264 SizeOfStackReserve;
126 DWORD SizeOfStackCommit;134 ADDR3264 SizeOfStackCommit;
127 DWORD SizeOfHeapReserve;135 ADDR3264 SizeOfHeapReserve;
128 DWORD SizeOfHeapCommit;136 ADDR3264 SizeOfHeapCommit;
129 DWORD LoaderFlags;137 DWORD LoaderFlags;
130 DWORD NumberOfRvaAndSizes;138 DWORD NumberOfRvaAndSizes;
131 IMAGE_DATA_DIRECTORY DataDirectory[16];139 IMAGE_DATA_DIRECTORY DataDirectory[16];
132 140} IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, IMAGE_OPTIONAL_HEADER;
133} IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;
134
135 141
136#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */142#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
137#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */143#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
@@ -193,7 +199,6 @@ typedef struct _IMAGE_BASE_RELOCATION { · 55 unmodified lines
193/* ----------------------------------------------------------- */199/* ----------------------------------------------------------- */
194#endif /* ndef IMAGE_NT_SIGNATURE */200#endif /* ndef IMAGE_NT_SIGNATURE */
195/* ----------------------------------------------------------- */201/* ----------------------------------------------------------- */
196
197#pragma pack(push, 1)202#pragma pack(push, 1)
198 203
199struct pe_header204struct pe_header
@@ -202,7 +207,15 @@ struct pe_header · 2 unmodified lines
202 BYTE dosstub[0x40];207 BYTE dosstub[0x40];
203 DWORD nt_sig;208 DWORD nt_sig;
204 IMAGE_FILE_HEADER filehdr;209 IMAGE_FILE_HEADER filehdr;
210#ifdef TCC_TARGET_X86_64
211 IMAGE_OPTIONAL_HEADER64 opthdr;
212#else
213#ifdef _WIN64
214 IMAGE_OPTIONAL_HEADER32 opthdr;
215#else
205 IMAGE_OPTIONAL_HEADER opthdr;216 IMAGE_OPTIONAL_HEADER opthdr;
217#endif
218#endif
206};219};
207 220
208struct pe_import_header {221struct pe_import_header {
@@ -280,17 +293,32 @@ ST_DATA struct pe_header pe_header = { · 71 unmodified lines
280 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */293 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */
281{294{
282 /* IMAGE_FILE_HEADER filehdr */295 /* IMAGE_FILE_HEADER filehdr */
296#ifdef TCC_TARGET_X86_64
297 0x8664, /*WORD Machine; */
298#else
283 0x014C, /*WORD Machine; */299 0x014C, /*WORD Machine; */
300#endif
284 0x0003, /*WORD NumberOfSections; */301 0x0003, /*WORD NumberOfSections; */
285 0x00000000, /*DWORD TimeDateStamp; */302 0x00000000, /*DWORD TimeDateStamp; */
286 0x00000000, /*DWORD PointerToSymbolTable; */303 0x00000000, /*DWORD PointerToSymbolTable; */
287 0x00000000, /*DWORD NumberOfSymbols; */304 0x00000000, /*DWORD NumberOfSymbols; */
305#ifdef TCC_TARGET_X86_64
306 0x00F0, /*WORD SizeOfOptionalHeader; */
307 0x022F /*WORD Characteristics; */
308#define CHARACTERISTICS_DLL 0x222E
309#else
288 0x00E0, /*WORD SizeOfOptionalHeader; */310 0x00E0, /*WORD SizeOfOptionalHeader; */
289 0x030F /*WORD Characteristics; */311 0x030F /*WORD Characteristics; */
312#define CHARACTERISTICS_DLL 0x230E
313#endif
290},{314},{
291 /* IMAGE_OPTIONAL_HEADER opthdr */315 /* IMAGE_OPTIONAL_HEADER opthdr */
292 /* Standard fields. */316 /* Standard fields. */
317#ifdef TCC_TARGET_X86_64
318 0x020B, /*WORD Magic; */
319#else
293 0x010B, /*WORD Magic; */320 0x010B, /*WORD Magic; */
321#endif
294 0x06, /*BYTE MajorLinkerVersion; */322 0x06, /*BYTE MajorLinkerVersion; */
295 0x00, /*BYTE MinorLinkerVersion; */323 0x00, /*BYTE MinorLinkerVersion; */
296 0x00000000, /*DWORD SizeOfCode; */324 0x00000000, /*DWORD SizeOfCode; */
@@ -298,8 +326,9 @@ ST_DATA struct pe_header pe_header = { · 1 unmodified line
298 0x00000000, /*DWORD SizeOfUninitializedData; */326 0x00000000, /*DWORD SizeOfUninitializedData; */
299 0x00000000, /*DWORD AddressOfEntryPoint; */327 0x00000000, /*DWORD AddressOfEntryPoint; */
300 0x00000000, /*DWORD BaseOfCode; */328 0x00000000, /*DWORD BaseOfCode; */
329#ifndef TCC_TARGET_X86_64
301 0x00000000, /*DWORD BaseOfData; */330 0x00000000, /*DWORD BaseOfData; */
302 331#endif
303 /* NT additional fields. */332 /* NT additional fields. */
304 0x00400000, /*DWORD ImageBase; */333 0x00400000, /*DWORD ImageBase; */
305 0x00001000, /*DWORD SectionAlignment; */334 0x00001000, /*DWORD SectionAlignment; */
@@ -317,7 +346,12 @@ ST_DATA struct pe_header pe_header = { · 11 unmodified lines
317 0x0002, /*WORD Subsystem; */346 0x0002, /*WORD Subsystem; */
318 0x0000, /*WORD DllCharacteristics; */347 0x0000, /*WORD DllCharacteristics; */
319 0x00100000, /*DWORD SizeOfStackReserve; */348 0x00100000, /*DWORD SizeOfStackReserve; */
349#ifdef TCC_TARGET_X86_64
350 // need to have a __chkstk eventually
351 0x00008000, /*DWORD SizeOfStackCommit; */
352#else
320 0x00001000, /*DWORD SizeOfStackCommit; */353 0x00001000, /*DWORD SizeOfStackCommit; */
354#endif
321 0x00100000, /*DWORD SizeOfHeapReserve; */355 0x00100000, /*DWORD SizeOfHeapReserve; */
322 0x00001000, /*DWORD SizeOfHeapCommit; */356 0x00001000, /*DWORD SizeOfHeapCommit; */
323 0x00000000, /*DWORD LoaderFlags; */357 0x00000000, /*DWORD LoaderFlags; */
@@ -561,7 +595,9 @@ ST_FN int pe_write(struct pe_info *pe) · 237 unmodified lines
561 break;595 break;
562 596
563 case sec_data:597 case sec_data:
598#ifndef TCC_TARGET_X86_64
564 pe_header.opthdr.BaseOfData = addr;599 pe_header.opthdr.BaseOfData = addr;
600#endif
565 break;601 break;
566 602
567 case sec_bss:603 case sec_bss:
@@ -614,7 +650,7 @@ ST_FN int pe_write(struct pe_info *pe) · 46 unmodified lines
614 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;650 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;
615 pe_header.opthdr.ImageBase = pe->imagebase;651 pe_header.opthdr.ImageBase = pe->imagebase;
616 if (PE_DLL == pe->type)652 if (PE_DLL == pe->type)
617 pe_header.filehdr.Characteristics = 0x230E;653 pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL;
618 else if (PE_GUI != pe->type)654 else if (PE_GUI != pe->type)
619 pe_header.opthdr.Subsystem = 3;655 pe_header.opthdr.Subsystem = 3;
620 656
@@ -708,7 +744,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 87 unmodified lines
708 pe->imp_offs = dll_ptr = pe->thunk->data_offset;744 pe->imp_offs = dll_ptr = pe->thunk->data_offset;
709 pe->imp_size = (ndlls + 1) * sizeof(struct pe_import_header);745 pe->imp_size = (ndlls + 1) * sizeof(struct pe_import_header);
710 pe->iat_offs = dll_ptr + pe->imp_size;746 pe->iat_offs = dll_ptr + pe->imp_size;
711 pe->iat_size = (sym_cnt + ndlls) * sizeof(DWORD);747 pe->iat_size = (sym_cnt + ndlls) * sizeof(ADDR3264);
712 section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size);748 section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size);
713 749
714 thk_ptr = pe->iat_offs;750 thk_ptr = pe->iat_offs;
@@ -717,7 +753,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 2 unmodified lines
717 for (i = 0; i < pe->imp_count; ++i) {753 for (i = 0; i < pe->imp_count; ++i) {
718 struct pe_import_header *hdr;754 struct pe_import_header *hdr;
719 int k, n;755 int k, n;
720 DWORD v;756 ADDR3264 v;
721 struct pe_import_info *p = pe->imp_info[i];757 struct pe_import_info *p = pe->imp_info[i];
722 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;758 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;
723 759
@@ -747,7 +783,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 23 unmodified lines
747 DLLReference *dllref = pe->s1->loaded_dlls[imp_sym->st_value-1];783 DLLReference *dllref = pe->s1->loaded_dlls[imp_sym->st_value-1];
748 if ( !dllref->handle )784 if ( !dllref->handle )
749 dllref->handle = LoadLibrary(dllref->name);785 dllref->handle = LoadLibrary(dllref->name);
750 v = (DWORD)GetProcAddress(dllref->handle, name);786 v = (ADDR3264)GetProcAddress(dllref->handle, name);
751 if (!v)787 if (!v)
752 error_noabort("undefined symbol '%s'", name);788 error_noabort("undefined symbol '%s'", name);
753 }789 }
@@ -755,10 +791,10 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 1 unmodified line
755 } else {791 } else {
756 v = 0; /* last entry is zero */792 v = 0; /* last entry is zero */
757 }793 }
758 *(DWORD*)(pe->thunk->data+thk_ptr) =794 *(ADDR3264*)(pe->thunk->data+thk_ptr) =
759 *(DWORD*)(pe->thunk->data+ent_ptr) = v;795 *(ADDR3264*)(pe->thunk->data+ent_ptr) = v;
760 thk_ptr += sizeof (DWORD);796 thk_ptr += sizeof (ADDR3264);
761 ent_ptr += sizeof (DWORD);797 ent_ptr += sizeof (ADDR3264);
762 }798 }
763 dll_ptr += sizeof(struct pe_import_header);799 dll_ptr += sizeof(struct pe_import_header);
764 dynarray_reset(&p->symbols, &p->sym_count);800 dynarray_reset(&p->symbols, &p->sym_count);
@@ -1129,11 +1165,15 @@ ST_FN int pe_check_symbols(struct pe_info *pe) · 364 unmodified lines
1129 1165
1130 } else {1166 } else {
1131 char buffer[100];1167 char buffer[100];
1168 WORD *p;
1132 1169
1133 offset = text_section->data_offset;1170 offset = text_section->data_offset;
1134 /* add the 'jmp IAT[x]' instruction */1171 /* add the 'jmp IAT[x]' instruction */
1135 *(WORD*)section_ptr_add(text_section, 8) = 0x25FF;1172 p = section_ptr_add(text_section, 8);
1136 1173 *p = 0x25FF;
1174#ifdef TCC_TARGET_X86_64
1175 *(DWORD*)(p+1) = (DWORD)-4;
1176#endif
1137 /* add a helper symbol, will be patched later in1177 /* add a helper symbol, will be patched later in
1138 pe_build_imports */1178 pe_build_imports */
1139 sprintf(buffer, "IAT.%s", name);1179 sprintf(buffer, "IAT.%s", name);
@@ -1306,7 +1346,7 @@ ST_FN void pe_print_sections(TCCState *s1, const char *fname) · 166 unmodified lines
1306 Section *s;1346 Section *s;
1307 FILE *f;1347 FILE *f;
1308 int i;1348 int i;
1309 f = fopen(fname, "wt");1349 f = fopen(fname, "w");
1310 for (i = 1; i < s1->nb_sections; ++i) {1350 for (i = 1; i < s1->nb_sections; ++i) {
1311 s = s1->sections[i];1351 s = s1->sections[i];
1312 pe_print_section(f, s);1352 pe_print_section(f, s);
@@ -1454,6 +1494,11 @@ quit: · 141 unmodified lines
1454}1494}
1455 1495
1456/* ------------------------------------------------------------- */1496/* ------------------------------------------------------------- */
1497#ifdef TCC_TARGET_X86_64
1498#define PE_STDSYM(n,s) n
1499#else
1500#define PE_STDSYM(n,s) "_" n s
1501#endif
1457 1502
1458ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)1503ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
1459{1504{
@@ -1461,7 +1506,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 1 unmodified line
1461 unsigned long addr = 0;1506 unsigned long addr = 0;
1462 int pe_type = 0;1507 int pe_type = 0;
1463 1508
1464 if (find_elf_sym(symtab_section, "_WinMain@16"))1509 if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16")))
1465 pe_type = PE_GUI;1510 pe_type = PE_GUI;
1466 else1511 else
1467 if (TCC_OUTPUT_DLL == s1->output_type) {1512 if (TCC_OUTPUT_DLL == s1->output_type) {
@@ -1473,7 +1518,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 5 unmodified lines
1473 start_symbol =1518 start_symbol =
1474 TCC_OUTPUT_MEMORY == s1->output_type1519 TCC_OUTPUT_MEMORY == s1->output_type
1475 ? PE_GUI == pe_type ? "_runwinmain" : NULL1520 ? PE_GUI == pe_type ? "_runwinmain" : NULL
1476 : PE_DLL == pe_type ? "__dllstart@12"1521 : PE_DLL == pe_type ? PE_STDSYM("_dllstart","@12")
1477 : PE_GUI == pe_type ? "_winstart" : "_start"1522 : PE_GUI == pe_type ? "_winstart" : "_start"
1478 ;1523 ;
1479 1524
@@ -1485,22 +1530,21 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 5 unmodified lines
1485 SHN_UNDEF, start_symbol);1530 SHN_UNDEF, start_symbol);
1486 1531
1487 if (0 == s1->nostdlib) {1532 if (0 == s1->nostdlib) {
1488 tcc_add_library(s1, "tcc1");1533 static const char *libs[] = {
1489#ifdef __CYGWIN__1534 "tcc1", "msvcrt", "kernel32", "", "user32", "gdi32", NULL
1490 tcc_add_library(s1, "cygwin1");1535 };
1491#else1536 const char **pp, *p;
1492 tcc_add_library(s1, "msvcrt");1537 for (pp = libs; 0 != (p = *pp); ++pp) {
1493#endif1538 if (0 == *p) {
1494 tcc_add_library(s1, "kernel32");1539 if (PE_DLL != pe_type && PE_GUI != pe_type)
1495 if (PE_DLL == pe_type || PE_GUI == pe_type) {1540 break;
1496 tcc_add_library(s1, "user32");1541 } else if (tcc_add_library(s1, p) < 0)
1497 tcc_add_library(s1, "gdi32");1542 error_noabort("cannot find library: %s", p);
1498 }1543 }
1499 }1544 }
1500 pe->type = pe_type;
1501 1545
1502 if (start_symbol) {1546 if (start_symbol) {
1503 addr = (unsigned long)tcc_get_symbol_err(s1, start_symbol);1547 addr = (ADDR3264)tcc_get_symbol_err(s1, start_symbol);
1504 if (s1->output_type == TCC_OUTPUT_MEMORY && addr)1548 if (s1->output_type == TCC_OUTPUT_MEMORY && addr)
1505 /* for -run GUI's, put '_runwinmain' instead of 'main' */1549 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1506 add_elf_sym(symtab_section,1550 add_elf_sym(symtab_section,
@@ -1508,6 +1552,8 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 1 unmodified line
1508 ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,1552 ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1509 text_section->sh_num, "main");1553 text_section->sh_num, "main");
1510 }1554 }
1555
1556 pe->type = pe_type;
1511 pe->start_addr = addr;1557 pe->start_addr = addr;
1512}1558}
1513 1559

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