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
3636 #define PE_MERGE_DATA
3737 // #define PE_PRINT_SECTIONS
3838
39+#ifdef TCC_TARGET_X86_64
40+# define ADDR3264 ULONGLONG
41+#else
42+# define ADDR3264 DWORD
43+#endif
44+
3945 #if defined _WIN32 && (defined _WIN64) == (defined TCC_TARGET_X86_64)
4046 #define TCC_IS_NATIVE
4147 #endif
@@ -48,6 +54,7 @@ · 6 unmodified lines
4854 typedef unsigned char BYTE;
4955 typedef unsigned short WORD;
5056 typedef unsigned long DWORD;
57+typedef unsigned long long ULONGLONG;
5158 #pragma pack(push, 1)
5259
5360 typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */
@@ -104,10 +111,11 @@ typedef struct _IMAGE_OPTIONAL_HEADER { · 50 unmodified lines
104111 DWORD SizeOfUninitializedData;
105112 DWORD AddressOfEntryPoint;
106113 DWORD BaseOfCode;
114+#ifndef TCC_TARGET_X86_64
107115 DWORD BaseOfData;
108−
116+#endif
109117 /* NT additional fields. */
110− DWORD ImageBase;
118+ ADDR3264 ImageBase;
111119 DWORD SectionAlignment;
112120 DWORD FileAlignment;
113121 WORD MajorOperatingSystemVersion;
@@ -122,16 +130,14 @@ typedef struct _IMAGE_OPTIONAL_HEADER { · 8 unmodified lines
122130 DWORD CheckSum;
123131 WORD Subsystem;
124132 WORD DllCharacteristics;
125− DWORD SizeOfStackReserve;
126− DWORD SizeOfStackCommit;
127− DWORD SizeOfHeapReserve;
128− DWORD SizeOfHeapCommit;
133+ ADDR3264 SizeOfStackReserve;
134+ ADDR3264 SizeOfStackCommit;
135+ ADDR3264 SizeOfHeapReserve;
136+ ADDR3264 SizeOfHeapCommit;
129137 DWORD LoaderFlags;
130138 DWORD NumberOfRvaAndSizes;
131139 IMAGE_DATA_DIRECTORY DataDirectory[16];
132−
133−} IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;
134−
140+} IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, IMAGE_OPTIONAL_HEADER;
135141
136142 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
137143 #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
@@ -193,7 +199,6 @@ typedef struct _IMAGE_BASE_RELOCATION { · 55 unmodified lines
193199 /* ----------------------------------------------------------- */
194200 #endif /* ndef IMAGE_NT_SIGNATURE */
195201 /* ----------------------------------------------------------- */
196−
197202 #pragma pack(push, 1)
198203
199204 struct pe_header
@@ -202,7 +207,15 @@ struct pe_header · 2 unmodified lines
202207 BYTE dosstub[0x40];
203208 DWORD nt_sig;
204209 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
205216 IMAGE_OPTIONAL_HEADER opthdr;
217+#endif
218+#endif
206219 };
207220
208221 struct pe_import_header {
@@ -280,17 +293,32 @@ ST_DATA struct pe_header pe_header = { · 71 unmodified lines
280293 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */
281294 {
282295 /* IMAGE_FILE_HEADER filehdr */
296+#ifdef TCC_TARGET_X86_64
297+ 0x8664, /*WORD Machine; */
298+#else
283299 0x014C, /*WORD Machine; */
300+#endif
284301 0x0003, /*WORD NumberOfSections; */
285302 0x00000000, /*DWORD TimeDateStamp; */
286303 0x00000000, /*DWORD PointerToSymbolTable; */
287304 0x00000000, /*DWORD NumberOfSymbols; */
305+#ifdef TCC_TARGET_X86_64
306+ 0x00F0, /*WORD SizeOfOptionalHeader; */
307+ 0x022F /*WORD Characteristics; */
308+#define CHARACTERISTICS_DLL 0x222E
309+#else
288310 0x00E0, /*WORD SizeOfOptionalHeader; */
289311 0x030F /*WORD Characteristics; */
312+#define CHARACTERISTICS_DLL 0x230E
313+#endif
290314 },{
291315 /* IMAGE_OPTIONAL_HEADER opthdr */
292316 /* Standard fields. */
317+#ifdef TCC_TARGET_X86_64
318+ 0x020B, /*WORD Magic; */
319+#else
293320 0x010B, /*WORD Magic; */
321+#endif
294322 0x06, /*BYTE MajorLinkerVersion; */
295323 0x00, /*BYTE MinorLinkerVersion; */
296324 0x00000000, /*DWORD SizeOfCode; */
@@ -298,8 +326,9 @@ ST_DATA struct pe_header pe_header = { · 1 unmodified line
298326 0x00000000, /*DWORD SizeOfUninitializedData; */
299327 0x00000000, /*DWORD AddressOfEntryPoint; */
300328 0x00000000, /*DWORD BaseOfCode; */
329+#ifndef TCC_TARGET_X86_64
301330 0x00000000, /*DWORD BaseOfData; */
302−
331+#endif
303332 /* NT additional fields. */
304333 0x00400000, /*DWORD ImageBase; */
305334 0x00001000, /*DWORD SectionAlignment; */
@@ -317,7 +346,12 @@ ST_DATA struct pe_header pe_header = { · 11 unmodified lines
317346 0x0002, /*WORD Subsystem; */
318347 0x0000, /*WORD DllCharacteristics; */
319348 0x00100000, /*DWORD SizeOfStackReserve; */
349+#ifdef TCC_TARGET_X86_64
350+ // need to have a __chkstk eventually
351+ 0x00008000, /*DWORD SizeOfStackCommit; */
352+#else
320353 0x00001000, /*DWORD SizeOfStackCommit; */
354+#endif
321355 0x00100000, /*DWORD SizeOfHeapReserve; */
322356 0x00001000, /*DWORD SizeOfHeapCommit; */
323357 0x00000000, /*DWORD LoaderFlags; */
@@ -561,7 +595,9 @@ ST_FN int pe_write(struct pe_info *pe) · 237 unmodified lines
561595 break;
562596
563597 case sec_data:
598+#ifndef TCC_TARGET_X86_64
564599 pe_header.opthdr.BaseOfData = addr;
600+#endif
565601 break;
566602
567603 case sec_bss:
@@ -614,7 +650,7 @@ ST_FN int pe_write(struct pe_info *pe) · 46 unmodified lines
614650 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;
615651 pe_header.opthdr.ImageBase = pe->imagebase;
616652 if (PE_DLL == pe->type)
617− pe_header.filehdr.Characteristics = 0x230E;
653+ pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL;
618654 else if (PE_GUI != pe->type)
619655 pe_header.opthdr.Subsystem = 3;
620656
@@ -708,7 +744,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 87 unmodified lines
708744 pe->imp_offs = dll_ptr = pe->thunk->data_offset;
709745 pe->imp_size = (ndlls + 1) * sizeof(struct pe_import_header);
710746 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);
712748 section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size);
713749
714750 thk_ptr = pe->iat_offs;
@@ -717,7 +753,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 2 unmodified lines
717753 for (i = 0; i < pe->imp_count; ++i) {
718754 struct pe_import_header *hdr;
719755 int k, n;
720− DWORD v;
756+ ADDR3264 v;
721757 struct pe_import_info *p = pe->imp_info[i];
722758 const char *name = pe->s1->loaded_dlls[p->dll_index-1]->name;
723759
@@ -747,7 +783,7 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 23 unmodified lines
747783 DLLReference *dllref = pe->s1->loaded_dlls[imp_sym->st_value-1];
748784 if ( !dllref->handle )
749785 dllref->handle = LoadLibrary(dllref->name);
750− v = (DWORD)GetProcAddress(dllref->handle, name);
786+ v = (ADDR3264)GetProcAddress(dllref->handle, name);
751787 if (!v)
752788 error_noabort("undefined symbol '%s'", name);
753789 }
@@ -755,10 +791,10 @@ ST_FN void pe_build_imports(struct pe_info *pe) · 1 unmodified line
755791 } else {
756792 v = 0; /* last entry is zero */
757793 }
758− *(DWORD*)(pe->thunk->data+thk_ptr) =
759− *(DWORD*)(pe->thunk->data+ent_ptr) = v;
760− thk_ptr += sizeof (DWORD);
761− ent_ptr += sizeof (DWORD);
794+ *(ADDR3264*)(pe->thunk->data+thk_ptr) =
795+ *(ADDR3264*)(pe->thunk->data+ent_ptr) = v;
796+ thk_ptr += sizeof (ADDR3264);
797+ ent_ptr += sizeof (ADDR3264);
762798 }
763799 dll_ptr += sizeof(struct pe_import_header);
764800 dynarray_reset(&p->symbols, &p->sym_count);
@@ -1129,11 +1165,15 @@ ST_FN int pe_check_symbols(struct pe_info *pe) · 364 unmodified lines
11291165
11301166 } else {
11311167 char buffer[100];
1168+ WORD *p;
11321169
11331170 offset = text_section->data_offset;
11341171 /* add the 'jmp IAT[x]' instruction */
1135− *(WORD*)section_ptr_add(text_section, 8) = 0x25FF;
1136−
1172+ p = section_ptr_add(text_section, 8);
1173+ *p = 0x25FF;
1174+#ifdef TCC_TARGET_X86_64
1175+ *(DWORD*)(p+1) = (DWORD)-4;
1176+#endif
11371177 /* add a helper symbol, will be patched later in
11381178 pe_build_imports */
11391179 sprintf(buffer, "IAT.%s", name);
@@ -1306,7 +1346,7 @@ ST_FN void pe_print_sections(TCCState *s1, const char *fname) · 166 unmodified lines
13061346 Section *s;
13071347 FILE *f;
13081348 int i;
1309− f = fopen(fname, "wt");
1349+ f = fopen(fname, "w");
13101350 for (i = 1; i < s1->nb_sections; ++i) {
13111351 s = s1->sections[i];
13121352 pe_print_section(f, s);
@@ -1454,6 +1494,11 @@ quit: · 141 unmodified lines
14541494 }
14551495
14561496 /* ------------------------------------------------------------- */
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
14571502
14581503 ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
14591504 {
@@ -1461,7 +1506,7 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 1 unmodified line
14611506 unsigned long addr = 0;
14621507 int pe_type = 0;
14631508
1464− if (find_elf_sym(symtab_section, "_WinMain@16"))
1509+ if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16")))
14651510 pe_type = PE_GUI;
14661511 else
14671512 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
14731518 start_symbol =
14741519 TCC_OUTPUT_MEMORY == s1->output_type
14751520 ? PE_GUI == pe_type ? "_runwinmain" : NULL
1476− : PE_DLL == pe_type ? "__dllstart@12"
1521+ : PE_DLL == pe_type ? PE_STDSYM("_dllstart","@12")
14771522 : PE_GUI == pe_type ? "_winstart" : "_start"
14781523 ;
14791524
@@ -1485,22 +1530,21 @@ ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe) · 5 unmodified lines
14851530 SHN_UNDEF, start_symbol);
14861531
14871532 if (0 == s1->nostdlib) {
1488− tcc_add_library(s1, "tcc1");
1489−#ifdef __CYGWIN__
1490− tcc_add_library(s1, "cygwin1");
1491−#else
1492− tcc_add_library(s1, "msvcrt");
1493−#endif
1494− tcc_add_library(s1, "kernel32");
1495− if (PE_DLL == pe_type || PE_GUI == pe_type) {
1496− tcc_add_library(s1, "user32");
1497− tcc_add_library(s1, "gdi32");
1533+ static const char *libs[] = {
1534+ "tcc1", "msvcrt", "kernel32", "", "user32", "gdi32", NULL
1535+ };
1536+ const char **pp, *p;
1537+ for (pp = libs; 0 != (p = *pp); ++pp) {
1538+ if (0 == *p) {
1539+ if (PE_DLL != pe_type && PE_GUI != pe_type)
1540+ break;
1541+ } else if (tcc_add_library(s1, p) < 0)
1542+ error_noabort("cannot find library: %s", p);
14981543 }
14991544 }
1500− pe->type = pe_type;
15011545
15021546 if (start_symbol) {
1503− addr = (unsigned long)tcc_get_symbol_err(s1, start_symbol);
1547+ addr = (ADDR3264)tcc_get_symbol_err(s1, start_symbol);
15041548 if (s1->output_type == TCC_OUTPUT_MEMORY && addr)
15051549 /* for -run GUI's, put '_runwinmain' instead of 'main' */
15061550 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
15081552 ELFW_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
15091553 text_section->sh_num, "main");
15101554 }
1555+
1556+ pe->type = pe_type;
15111557 pe->start_addr = addr;
15121558 }
15131559

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