fixed dylib typo in libtcc.c
[tinycc.git] / tccpe.c
bloba67023dd2ee203f125e572f2a6c53377c9bc1ec1
1 /*
2 * TCCPE.C - PE file output for the Tiny C Compiler
4 * Copyright (c) 2005-2007 grischka
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 #define PE_MERGE_DATA
24 /* #define PE_PRINT_SECTIONS */
26 #ifndef _WIN32
27 #define stricmp strcasecmp
28 #define strnicmp strncasecmp
29 #include <sys/stat.h> /* chmod() */
30 #endif
32 #ifdef TCC_TARGET_X86_64
33 # define ADDR3264 ULONGLONG
34 # define PE_IMAGE_REL IMAGE_REL_BASED_DIR64
35 # define REL_TYPE_DIRECT R_X86_64_64
36 # define R_XXX_THUNKFIX R_X86_64_PC32
37 # define R_XXX_RELATIVE R_X86_64_RELATIVE
38 # define IMAGE_FILE_MACHINE 0x8664
39 # define RSRC_RELTYPE 3
41 #elif defined TCC_TARGET_ARM
42 # define ADDR3264 DWORD
43 # define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW
44 # define REL_TYPE_DIRECT R_ARM_ABS32
45 # define R_XXX_THUNKFIX R_ARM_ABS32
46 # define R_XXX_RELATIVE R_ARM_RELATIVE
47 # define IMAGE_FILE_MACHINE 0x01C0
48 # define RSRC_RELTYPE 7 /* ??? (not tested) */
50 #elif defined TCC_TARGET_I386
51 # define ADDR3264 DWORD
52 # define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW
53 # define REL_TYPE_DIRECT R_386_32
54 # define R_XXX_THUNKFIX R_386_32
55 # define R_XXX_RELATIVE R_386_RELATIVE
56 # define IMAGE_FILE_MACHINE 0x014C
57 # define RSRC_RELTYPE 7 /* DIR32NB */
59 #endif
61 #ifndef IMAGE_NT_SIGNATURE
62 /* ----------------------------------------------------------- */
63 /* definitions below are from winnt.h */
65 typedef unsigned char BYTE;
66 typedef unsigned short WORD;
67 typedef unsigned int DWORD;
68 typedef unsigned long long ULONGLONG;
69 #pragma pack(push, 1)
71 typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */
72 WORD e_magic; /* Magic number */
73 WORD e_cblp; /* Bytes on last page of file */
74 WORD e_cp; /* Pages in file */
75 WORD e_crlc; /* Relocations */
76 WORD e_cparhdr; /* Size of header in paragraphs */
77 WORD e_minalloc; /* Minimum extra paragraphs needed */
78 WORD e_maxalloc; /* Maximum extra paragraphs needed */
79 WORD e_ss; /* Initial (relative) SS value */
80 WORD e_sp; /* Initial SP value */
81 WORD e_csum; /* Checksum */
82 WORD e_ip; /* Initial IP value */
83 WORD e_cs; /* Initial (relative) CS value */
84 WORD e_lfarlc; /* File address of relocation table */
85 WORD e_ovno; /* Overlay number */
86 WORD e_res[4]; /* Reserved words */
87 WORD e_oemid; /* OEM identifier (for e_oeminfo) */
88 WORD e_oeminfo; /* OEM information; e_oemid specific */
89 WORD e_res2[10]; /* Reserved words */
90 DWORD e_lfanew; /* File address of new exe header */
91 } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
93 #define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
94 #define SIZE_OF_NT_SIGNATURE 4
96 typedef struct _IMAGE_FILE_HEADER {
97 WORD Machine;
98 WORD NumberOfSections;
99 DWORD TimeDateStamp;
100 DWORD PointerToSymbolTable;
101 DWORD NumberOfSymbols;
102 WORD SizeOfOptionalHeader;
103 WORD Characteristics;
104 } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
107 #define IMAGE_SIZEOF_FILE_HEADER 20
109 typedef struct _IMAGE_DATA_DIRECTORY {
110 DWORD VirtualAddress;
111 DWORD Size;
112 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
115 typedef struct _IMAGE_OPTIONAL_HEADER {
116 /* Standard fields. */
117 WORD Magic;
118 BYTE MajorLinkerVersion;
119 BYTE MinorLinkerVersion;
120 DWORD SizeOfCode;
121 DWORD SizeOfInitializedData;
122 DWORD SizeOfUninitializedData;
123 DWORD AddressOfEntryPoint;
124 DWORD BaseOfCode;
125 #ifndef TCC_TARGET_X86_64
126 DWORD BaseOfData;
127 #endif
128 /* NT additional fields. */
129 ADDR3264 ImageBase;
130 DWORD SectionAlignment;
131 DWORD FileAlignment;
132 WORD MajorOperatingSystemVersion;
133 WORD MinorOperatingSystemVersion;
134 WORD MajorImageVersion;
135 WORD MinorImageVersion;
136 WORD MajorSubsystemVersion;
137 WORD MinorSubsystemVersion;
138 DWORD Win32VersionValue;
139 DWORD SizeOfImage;
140 DWORD SizeOfHeaders;
141 DWORD CheckSum;
142 WORD Subsystem;
143 WORD DllCharacteristics;
144 ADDR3264 SizeOfStackReserve;
145 ADDR3264 SizeOfStackCommit;
146 ADDR3264 SizeOfHeapReserve;
147 ADDR3264 SizeOfHeapCommit;
148 DWORD LoaderFlags;
149 DWORD NumberOfRvaAndSizes;
150 IMAGE_DATA_DIRECTORY DataDirectory[16];
151 } IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, IMAGE_OPTIONAL_HEADER;
153 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
154 #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
155 #define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */
156 #define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */
157 #define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */
158 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */
159 #define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */
160 /* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */
161 #define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */
162 #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */
163 #define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */
164 #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */
165 #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */
166 #define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
167 #define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */
168 #define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */
170 /* Section header format. */
171 #define IMAGE_SIZEOF_SHORT_NAME 8
173 typedef struct _IMAGE_SECTION_HEADER {
174 BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
175 union {
176 DWORD PhysicalAddress;
177 DWORD VirtualSize;
178 } Misc;
179 DWORD VirtualAddress;
180 DWORD SizeOfRawData;
181 DWORD PointerToRawData;
182 DWORD PointerToRelocations;
183 DWORD PointerToLinenumbers;
184 WORD NumberOfRelocations;
185 WORD NumberOfLinenumbers;
186 DWORD Characteristics;
187 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
189 #define IMAGE_SIZEOF_SECTION_HEADER 40
191 typedef struct _IMAGE_EXPORT_DIRECTORY {
192 DWORD Characteristics;
193 DWORD TimeDateStamp;
194 WORD MajorVersion;
195 WORD MinorVersion;
196 DWORD Name;
197 DWORD Base;
198 DWORD NumberOfFunctions;
199 DWORD NumberOfNames;
200 DWORD AddressOfFunctions;
201 DWORD AddressOfNames;
202 DWORD AddressOfNameOrdinals;
203 } IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY;
205 typedef struct _IMAGE_IMPORT_DESCRIPTOR {
206 union {
207 DWORD Characteristics;
208 DWORD OriginalFirstThunk;
210 DWORD TimeDateStamp;
211 DWORD ForwarderChain;
212 DWORD Name;
213 DWORD FirstThunk;
214 } IMAGE_IMPORT_DESCRIPTOR;
216 typedef struct _IMAGE_BASE_RELOCATION {
217 DWORD VirtualAddress;
218 DWORD SizeOfBlock;
219 // WORD TypeOffset[1];
220 } IMAGE_BASE_RELOCATION;
222 #define IMAGE_SIZEOF_BASE_RELOCATION 8
224 #define IMAGE_REL_BASED_ABSOLUTE 0
225 #define IMAGE_REL_BASED_HIGH 1
226 #define IMAGE_REL_BASED_LOW 2
227 #define IMAGE_REL_BASED_HIGHLOW 3
228 #define IMAGE_REL_BASED_HIGHADJ 4
229 #define IMAGE_REL_BASED_MIPS_JMPADDR 5
230 #define IMAGE_REL_BASED_SECTION 6
231 #define IMAGE_REL_BASED_REL32 7
232 #define IMAGE_REL_BASED_DIR64 10
234 #pragma pack(pop)
236 /* ----------------------------------------------------------- */
237 #endif /* ndef IMAGE_NT_SIGNATURE */
238 /* ----------------------------------------------------------- */
240 #ifndef IMAGE_REL_BASED_DIR64
241 # define IMAGE_REL_BASED_DIR64 10
242 #endif
244 #pragma pack(push, 1)
245 struct pe_header
247 IMAGE_DOS_HEADER doshdr;
248 BYTE dosstub[0x40];
249 DWORD nt_sig;
250 IMAGE_FILE_HEADER filehdr;
251 #ifdef TCC_TARGET_X86_64
252 IMAGE_OPTIONAL_HEADER64 opthdr;
253 #else
254 #ifdef _WIN64
255 IMAGE_OPTIONAL_HEADER32 opthdr;
256 #else
257 IMAGE_OPTIONAL_HEADER opthdr;
258 #endif
259 #endif
262 struct pe_reloc_header {
263 DWORD offset;
264 DWORD size;
267 struct pe_rsrc_header {
268 struct _IMAGE_FILE_HEADER filehdr;
269 struct _IMAGE_SECTION_HEADER sectionhdr;
272 struct pe_rsrc_reloc {
273 DWORD offset;
274 DWORD size;
275 WORD type;
277 #pragma pack(pop)
279 /* ------------------------------------------------------------- */
280 /* internal temporary structures */
283 #define IMAGE_SCN_CNT_CODE 0x00000020
284 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
285 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
286 #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
287 #define IMAGE_SCN_MEM_SHARED 0x10000000
288 #define IMAGE_SCN_MEM_EXECUTE 0x20000000
289 #define IMAGE_SCN_MEM_READ 0x40000000
290 #define IMAGE_SCN_MEM_WRITE 0x80000000
293 enum {
294 sec_text = 0,
295 sec_data ,
296 sec_bss ,
297 sec_idata ,
298 sec_pdata ,
299 sec_other ,
300 sec_rsrc ,
301 sec_stab ,
302 sec_reloc ,
303 sec_last
306 static const DWORD pe_sec_flags[] = {
307 0x60000020, /* ".text" , */
308 0xC0000040, /* ".data" , */
309 0xC0000080, /* ".bss" , */
310 0x40000040, /* ".idata" , */
311 0x40000040, /* ".pdata" , */
312 0xE0000060, /* < other > , */
313 0x40000040, /* ".rsrc" , */
314 0x42000802, /* ".stab" , */
315 0x42000040, /* ".reloc" , */
318 struct section_info {
319 int cls, ord;
320 char name[32];
321 DWORD sh_addr;
322 DWORD sh_size;
323 DWORD sh_flags;
324 unsigned char *data;
325 DWORD data_size;
326 IMAGE_SECTION_HEADER ish;
329 struct import_symbol {
330 int sym_index;
331 int iat_index;
332 int thk_offset;
335 struct pe_import_info {
336 int dll_index;
337 int sym_count;
338 struct import_symbol **symbols;
341 struct pe_info {
342 TCCState *s1;
343 Section *reloc;
344 Section *thunk;
345 const char *filename;
346 int type;
347 DWORD sizeofheaders;
348 ADDR3264 imagebase;
349 const char *start_symbol;
350 DWORD start_addr;
351 DWORD imp_offs;
352 DWORD imp_size;
353 DWORD iat_offs;
354 DWORD iat_size;
355 DWORD exp_offs;
356 DWORD exp_size;
357 int subsystem;
358 DWORD section_align;
359 DWORD file_align;
360 struct section_info *sec_info;
361 int sec_count;
362 struct pe_import_info **imp_info;
363 int imp_count;
366 #define PE_NUL 0
367 #define PE_DLL 1
368 #define PE_GUI 2
369 #define PE_EXE 3
370 #define PE_RUN 4
372 /* --------------------------------------------*/
374 static const char *pe_export_name(TCCState *s1, ElfW(Sym) *sym)
376 const char *name = (char*)symtab_section->link->data + sym->st_name;
377 if (s1->leading_underscore && name[0] == '_' && !(sym->st_other & ST_PE_STDCALL))
378 return name + 1;
379 return name;
382 static int pe_find_import(TCCState * s1, ElfW(Sym) *sym)
384 char buffer[200];
385 const char *s, *p;
386 int sym_index = 0, n = 0;
387 int a, err = 0;
389 do {
390 s = pe_export_name(s1, sym);
391 a = 0;
392 if (n) {
393 /* second try: */
394 if (sym->st_other & ST_PE_STDCALL) {
395 /* try w/0 stdcall deco (windows API convention) */
396 p = strrchr(s, '@');
397 if (!p || s[0] != '_')
398 break;
399 strcpy(buffer, s+1)[p-s-1] = 0;
400 } else if (s[0] != '_') { /* try non-ansi function */
401 buffer[0] = '_', strcpy(buffer + 1, s);
402 } else if (0 == memcmp(s, "__imp_", 6)) { /* mingw 2.0 */
403 strcpy(buffer, s + 6), a = 1;
404 } else if (0 == memcmp(s, "_imp__", 6)) { /* mingw 3.7 */
405 strcpy(buffer, s + 6), a = 1;
406 } else {
407 continue;
409 s = buffer;
411 sym_index = find_elf_sym(s1->dynsymtab_section, s);
412 // printf("find (%d) %d %s\n", n, sym_index, s);
413 if (sym_index
414 && ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT
415 && 0 == (sym->st_other & ST_PE_IMPORT)
416 && 0 == a
417 ) err = -1, sym_index = 0;
418 } while (0 == sym_index && ++n < 2);
419 return n == 2 ? err : sym_index;
422 /*----------------------------------------------------------------------------*/
424 static int dynarray_assoc(void **pp, int n, int key)
426 int i;
427 for (i = 0; i < n; ++i, ++pp)
428 if (key == **(int **) pp)
429 return i;
430 return -1;
433 #if 0
434 ST_FN DWORD umin(DWORD a, DWORD b)
436 return a < b ? a : b;
438 #endif
440 static DWORD umax(DWORD a, DWORD b)
442 return a < b ? b : a;
445 static DWORD pe_file_align(struct pe_info *pe, DWORD n)
447 return (n + (pe->file_align - 1)) & ~(pe->file_align - 1);
450 static DWORD pe_virtual_align(struct pe_info *pe, DWORD n)
452 return (n + (pe->section_align - 1)) & ~(pe->section_align - 1);
455 static void pe_align_section(Section *s, int a)
457 int i = s->data_offset & (a-1);
458 if (i)
459 section_ptr_add(s, a - i);
462 static void pe_set_datadir(struct pe_header *hdr, int dir, DWORD addr, DWORD size)
464 hdr->opthdr.DataDirectory[dir].VirtualAddress = addr;
465 hdr->opthdr.DataDirectory[dir].Size = size;
468 static int pe_fwrite(void *data, unsigned len, FILE *fp, DWORD *psum)
470 if (psum) {
471 DWORD sum = *psum;
472 WORD *p = data;
473 int i;
474 for (i = len; i > 0; i -= 2) {
475 sum += (i >= 2) ? *p++ : *(BYTE*)p;
476 sum = (sum + (sum >> 16)) & 0xFFFF;
478 *psum = sum;
480 return len == fwrite(data, 1, len, fp) ? 0 : -1;
483 static void pe_fpad(FILE *fp, DWORD new_pos)
485 DWORD pos = ftell(fp);
486 while (++pos <= new_pos)
487 fputc(0, fp);
490 /*----------------------------------------------------------------------------*/
491 static int pe_write(struct pe_info *pe)
493 static const struct pe_header pe_template = {
495 /* IMAGE_DOS_HEADER doshdr */
496 0x5A4D, /*WORD e_magic; Magic number */
497 0x0090, /*WORD e_cblp; Bytes on last page of file */
498 0x0003, /*WORD e_cp; Pages in file */
499 0x0000, /*WORD e_crlc; Relocations */
501 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */
502 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */
503 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */
504 0x0000, /*WORD e_ss; Initial (relative) SS value */
506 0x00B8, /*WORD e_sp; Initial SP value */
507 0x0000, /*WORD e_csum; Checksum */
508 0x0000, /*WORD e_ip; Initial IP value */
509 0x0000, /*WORD e_cs; Initial (relative) CS value */
510 0x0040, /*WORD e_lfarlc; File address of relocation table */
511 0x0000, /*WORD e_ovno; Overlay number */
512 {0,0,0,0}, /*WORD e_res[4]; Reserved words */
513 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */
514 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */
515 {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */
516 0x00000080 /*DWORD e_lfanew; File address of new exe header */
518 /* BYTE dosstub[0x40] */
519 /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */
520 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68,
521 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f,
522 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20,
523 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
525 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */
527 /* IMAGE_FILE_HEADER filehdr */
528 IMAGE_FILE_MACHINE, /*WORD Machine; */
529 0x0003, /*WORD NumberOfSections; */
530 0x00000000, /*DWORD TimeDateStamp; */
531 0x00000000, /*DWORD PointerToSymbolTable; */
532 0x00000000, /*DWORD NumberOfSymbols; */
533 #if defined(TCC_TARGET_X86_64)
534 0x00F0, /*WORD SizeOfOptionalHeader; */
535 0x022F /*WORD Characteristics; */
536 #define CHARACTERISTICS_DLL 0x222E
537 #elif defined(TCC_TARGET_I386)
538 0x00E0, /*WORD SizeOfOptionalHeader; */
539 0x030F /*WORD Characteristics; */
540 #define CHARACTERISTICS_DLL 0x230E
541 #elif defined(TCC_TARGET_ARM)
542 0x00E0, /*WORD SizeOfOptionalHeader; */
543 0x010F, /*WORD Characteristics; */
544 #define CHARACTERISTICS_DLL 0x230F
545 #endif
547 /* IMAGE_OPTIONAL_HEADER opthdr */
548 /* Standard fields. */
549 #ifdef TCC_TARGET_X86_64
550 0x020B, /*WORD Magic; */
551 #else
552 0x010B, /*WORD Magic; */
553 #endif
554 0x06, /*BYTE MajorLinkerVersion; */
555 0x00, /*BYTE MinorLinkerVersion; */
556 0x00000000, /*DWORD SizeOfCode; */
557 0x00000000, /*DWORD SizeOfInitializedData; */
558 0x00000000, /*DWORD SizeOfUninitializedData; */
559 0x00000000, /*DWORD AddressOfEntryPoint; */
560 0x00000000, /*DWORD BaseOfCode; */
561 #ifndef TCC_TARGET_X86_64
562 0x00000000, /*DWORD BaseOfData; */
563 #endif
564 /* NT additional fields. */
565 #if defined(TCC_TARGET_ARM)
566 0x00100000, /*DWORD ImageBase; */
567 #else
568 0x00400000, /*DWORD ImageBase; */
569 #endif
570 0x00001000, /*DWORD SectionAlignment; */
571 0x00000200, /*DWORD FileAlignment; */
572 0x0004, /*WORD MajorOperatingSystemVersion; */
573 0x0000, /*WORD MinorOperatingSystemVersion; */
574 0x0000, /*WORD MajorImageVersion; */
575 0x0000, /*WORD MinorImageVersion; */
576 0x0004, /*WORD MajorSubsystemVersion; */
577 0x0000, /*WORD MinorSubsystemVersion; */
578 0x00000000, /*DWORD Win32VersionValue; */
579 0x00000000, /*DWORD SizeOfImage; */
580 0x00000200, /*DWORD SizeOfHeaders; */
581 0x00000000, /*DWORD CheckSum; */
582 0x0002, /*WORD Subsystem; */
583 0x0000, /*WORD DllCharacteristics; */
584 0x00100000, /*DWORD SizeOfStackReserve; */
585 0x00001000, /*DWORD SizeOfStackCommit; */
586 0x00100000, /*DWORD SizeOfHeapReserve; */
587 0x00001000, /*DWORD SizeOfHeapCommit; */
588 0x00000000, /*DWORD LoaderFlags; */
589 0x00000010, /*DWORD NumberOfRvaAndSizes; */
591 /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */
592 {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
593 {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}
596 struct pe_header pe_header = pe_template;
598 int i;
599 FILE *op;
600 DWORD file_offset, sum;
601 struct section_info *si;
602 IMAGE_SECTION_HEADER *psh;
604 op = fopen(pe->filename, "wb");
605 if (NULL == op) {
606 tcc_error_noabort("could not write '%s': %s", pe->filename, strerror(errno));
607 return -1;
610 pe->sizeofheaders = pe_file_align(pe,
611 sizeof (struct pe_header)
612 + pe->sec_count * sizeof (IMAGE_SECTION_HEADER)
615 file_offset = pe->sizeofheaders;
617 if (2 == pe->s1->verbose)
618 printf("-------------------------------"
619 "\n virt file size section" "\n");
620 for (i = 0; i < pe->sec_count; ++i) {
621 DWORD addr, size;
622 const char *sh_name;
624 si = pe->sec_info + i;
625 sh_name = si->name;
626 addr = si->sh_addr - pe->imagebase;
627 size = si->sh_size;
628 psh = &si->ish;
630 if (2 == pe->s1->verbose)
631 printf("%6x %6x %6x %s\n",
632 (unsigned)addr, (unsigned)file_offset, (unsigned)size, sh_name);
634 switch (si->cls) {
635 case sec_text:
636 pe_header.opthdr.BaseOfCode = addr;
637 break;
639 case sec_data:
640 #ifndef TCC_TARGET_X86_64
641 pe_header.opthdr.BaseOfData = addr;
642 #endif
643 break;
645 case sec_bss:
646 break;
648 case sec_reloc:
649 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size);
650 break;
652 case sec_rsrc:
653 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size);
654 break;
656 case sec_pdata:
657 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXCEPTION, addr, size);
658 break;
660 case sec_stab:
661 break;
664 if (pe->thunk == pe->s1->sections[si->ord]) {
665 if (pe->imp_size) {
666 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IMPORT,
667 pe->imp_offs + addr, pe->imp_size);
668 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IAT,
669 pe->iat_offs + addr, pe->iat_size);
671 if (pe->exp_size) {
672 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXPORT,
673 pe->exp_offs + addr, pe->exp_size);
677 strncpy((char*)psh->Name, sh_name, sizeof psh->Name);
679 psh->Characteristics = pe_sec_flags[si->cls];
680 psh->VirtualAddress = addr;
681 psh->Misc.VirtualSize = size;
682 pe_header.opthdr.SizeOfImage =
683 umax(pe_virtual_align(pe, size + addr), pe_header.opthdr.SizeOfImage);
685 if (si->data_size) {
686 psh->PointerToRawData = file_offset;
687 file_offset = pe_file_align(pe, file_offset + si->data_size);
688 psh->SizeOfRawData = file_offset - psh->PointerToRawData;
689 if (si->cls == sec_text)
690 pe_header.opthdr.SizeOfCode += psh->SizeOfRawData;
691 else
692 pe_header.opthdr.SizeOfInitializedData += psh->SizeOfRawData;
696 //pe_header.filehdr.TimeDateStamp = time(NULL);
697 pe_header.filehdr.NumberOfSections = pe->sec_count;
698 pe_header.opthdr.AddressOfEntryPoint = pe->start_addr;
699 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;
700 pe_header.opthdr.ImageBase = pe->imagebase;
701 pe_header.opthdr.Subsystem = pe->subsystem;
702 if (pe->s1->pe_stack_size)
703 pe_header.opthdr.SizeOfStackReserve = pe->s1->pe_stack_size;
704 if (PE_DLL == pe->type)
705 pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL;
706 pe_header.filehdr.Characteristics |= pe->s1->pe_characteristics;
708 sum = 0;
709 pe_fwrite(&pe_header, sizeof pe_header, op, &sum);
710 for (i = 0; i < pe->sec_count; ++i)
711 pe_fwrite(&pe->sec_info[i].ish, sizeof(IMAGE_SECTION_HEADER), op, &sum);
712 pe_fpad(op, pe->sizeofheaders);
713 for (i = 0; i < pe->sec_count; ++i) {
714 si = pe->sec_info + i;
715 psh = &si->ish;
716 if (si->data_size) {
717 pe_fwrite(si->data, si->data_size, op, &sum);
718 file_offset = psh->PointerToRawData + psh->SizeOfRawData;
719 pe_fpad(op, file_offset);
723 pe_header.opthdr.CheckSum = sum + file_offset;
724 fseek(op, offsetof(struct pe_header, opthdr.CheckSum), SEEK_SET);
725 pe_fwrite(&pe_header.opthdr.CheckSum, sizeof pe_header.opthdr.CheckSum, op, NULL);
726 fclose (op);
727 #ifndef _WIN32
728 chmod(pe->filename, 0777);
729 #endif
731 if (2 == pe->s1->verbose)
732 printf("-------------------------------\n");
733 if (pe->s1->verbose)
734 printf("<- %s (%u bytes)\n", pe->filename, (unsigned)file_offset);
736 return 0;
739 /*----------------------------------------------------------------------------*/
741 static struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index)
743 int i;
744 int dll_index;
745 struct pe_import_info *p;
746 struct import_symbol *s;
747 ElfW(Sym) *isym;
749 isym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
750 dll_index = isym->st_size;
752 i = dynarray_assoc ((void**)pe->imp_info, pe->imp_count, dll_index);
753 if (-1 != i) {
754 p = pe->imp_info[i];
755 goto found_dll;
757 p = tcc_mallocz(sizeof *p);
758 p->dll_index = dll_index;
759 dynarray_add(&pe->imp_info, &pe->imp_count, p);
761 found_dll:
762 i = dynarray_assoc ((void**)p->symbols, p->sym_count, sym_index);
763 if (-1 != i)
764 return p->symbols[i];
766 s = tcc_mallocz(sizeof *s);
767 dynarray_add(&p->symbols, &p->sym_count, s);
768 s->sym_index = sym_index;
769 return s;
772 void pe_free_imports(struct pe_info *pe)
774 int i;
775 for (i = 0; i < pe->imp_count; ++i) {
776 struct pe_import_info *p = pe->imp_info[i];
777 dynarray_reset(&p->symbols, &p->sym_count);
779 dynarray_reset(&pe->imp_info, &pe->imp_count);
782 /*----------------------------------------------------------------------------*/
783 static void pe_build_imports(struct pe_info *pe)
785 int thk_ptr, ent_ptr, dll_ptr, sym_cnt, i;
786 DWORD rva_base = pe->thunk->sh_addr - pe->imagebase;
787 int ndlls = pe->imp_count;
789 for (sym_cnt = i = 0; i < ndlls; ++i)
790 sym_cnt += pe->imp_info[i]->sym_count;
792 if (0 == sym_cnt)
793 return;
795 pe_align_section(pe->thunk, 16);
797 pe->imp_offs = dll_ptr = pe->thunk->data_offset;
798 pe->imp_size = (ndlls + 1) * sizeof(IMAGE_IMPORT_DESCRIPTOR);
799 pe->iat_offs = dll_ptr + pe->imp_size;
800 pe->iat_size = (sym_cnt + ndlls) * sizeof(ADDR3264);
801 section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size);
803 thk_ptr = pe->iat_offs;
804 ent_ptr = pe->iat_offs + pe->iat_size;
806 for (i = 0; i < pe->imp_count; ++i) {
807 IMAGE_IMPORT_DESCRIPTOR *hdr;
808 int k, n, dllindex;
809 ADDR3264 v;
810 struct pe_import_info *p = pe->imp_info[i];
811 const char *name;
812 DLLReference *dllref;
814 dllindex = p->dll_index;
815 if (dllindex)
816 name = (dllref = pe->s1->loaded_dlls[dllindex-1])->name;
817 else
818 name = "", dllref = NULL;
820 /* put the dll name into the import header */
821 v = put_elf_str(pe->thunk, name);
822 hdr = (IMAGE_IMPORT_DESCRIPTOR*)(pe->thunk->data + dll_ptr);
823 hdr->FirstThunk = thk_ptr + rva_base;
824 hdr->OriginalFirstThunk = ent_ptr + rva_base;
825 hdr->Name = v + rva_base;
827 for (k = 0, n = p->sym_count; k <= n; ++k) {
828 if (k < n) {
829 int iat_index = p->symbols[k]->iat_index;
830 int sym_index = p->symbols[k]->sym_index;
831 ElfW(Sym) *imp_sym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
832 ElfW(Sym) *org_sym = (ElfW(Sym) *)symtab_section->data + iat_index;
833 const char *name = (char*)pe->s1->dynsymtab_section->link->data + imp_sym->st_name;
834 int ordinal;
836 org_sym->st_value = thk_ptr;
837 org_sym->st_shndx = pe->thunk->sh_num;
839 if (dllref)
840 v = 0, ordinal = imp_sym->st_value; /* ordinal from pe_load_def */
841 else
842 ordinal = 0, v = imp_sym->st_value; /* address from tcc_add_symbol() */
844 #ifdef TCC_IS_NATIVE
845 if (pe->type == PE_RUN) {
846 if (dllref) {
847 if ( !dllref->handle )
848 dllref->handle = LoadLibrary(dllref->name);
849 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
851 if (!v)
852 tcc_error_noabort("can't build symbol '%s'", name);
853 } else
854 #endif
855 if (ordinal) {
856 v = ordinal | (ADDR3264)1 << (sizeof(ADDR3264)*8 - 1);
857 } else {
858 v = pe->thunk->data_offset + rva_base;
859 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
860 put_elf_str(pe->thunk, name);
863 } else {
864 v = 0; /* last entry is zero */
867 *(ADDR3264*)(pe->thunk->data+thk_ptr) =
868 *(ADDR3264*)(pe->thunk->data+ent_ptr) = v;
869 thk_ptr += sizeof (ADDR3264);
870 ent_ptr += sizeof (ADDR3264);
872 dll_ptr += sizeof(IMAGE_IMPORT_DESCRIPTOR);
876 /* ------------------------------------------------------------- */
878 struct pe_sort_sym
880 int index;
881 const char *name;
884 static int sym_cmp(const void *va, const void *vb)
886 const char *ca = (*(struct pe_sort_sym**)va)->name;
887 const char *cb = (*(struct pe_sort_sym**)vb)->name;
888 return strcmp(ca, cb);
891 static void pe_build_exports(struct pe_info *pe)
893 ElfW(Sym) *sym;
894 int sym_index, sym_end;
895 DWORD rva_base, func_o, name_o, ord_o, str_o;
896 IMAGE_EXPORT_DIRECTORY *hdr;
897 int sym_count, ord;
898 struct pe_sort_sym **sorted, *p;
900 FILE *op;
901 char buf[260];
902 const char *dllname;
903 const char *name;
905 rva_base = pe->thunk->sh_addr - pe->imagebase;
906 sym_count = 0, sorted = NULL, op = NULL;
908 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
909 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
910 sym = (ElfW(Sym)*)symtab_section->data + sym_index;
911 name = pe_export_name(pe->s1, sym);
912 if ((sym->st_other & ST_PE_EXPORT)
913 /* export only symbols from actually written sections */
914 && pe->s1->sections[sym->st_shndx]->sh_addr) {
915 p = tcc_malloc(sizeof *p);
916 p->index = sym_index;
917 p->name = name;
918 dynarray_add(&sorted, &sym_count, p);
920 #if 0
921 if (sym->st_other & ST_PE_EXPORT)
922 printf("export: %s\n", name);
923 if (sym->st_other & ST_PE_STDCALL)
924 printf("stdcall: %s\n", name);
925 #endif
928 if (0 == sym_count)
929 return;
931 qsort (sorted, sym_count, sizeof *sorted, sym_cmp);
933 pe_align_section(pe->thunk, 16);
934 dllname = tcc_basename(pe->filename);
936 pe->exp_offs = pe->thunk->data_offset;
937 func_o = pe->exp_offs + sizeof(IMAGE_EXPORT_DIRECTORY);
938 name_o = func_o + sym_count * sizeof (DWORD);
939 ord_o = name_o + sym_count * sizeof (DWORD);
940 str_o = ord_o + sym_count * sizeof(WORD);
942 hdr = section_ptr_add(pe->thunk, str_o - pe->exp_offs);
943 hdr->Characteristics = 0;
944 hdr->Base = 1;
945 hdr->NumberOfFunctions = sym_count;
946 hdr->NumberOfNames = sym_count;
947 hdr->AddressOfFunctions = func_o + rva_base;
948 hdr->AddressOfNames = name_o + rva_base;
949 hdr->AddressOfNameOrdinals = ord_o + rva_base;
950 hdr->Name = str_o + rva_base;
951 put_elf_str(pe->thunk, dllname);
953 #if 1
954 /* automatically write exports to <output-filename>.def */
955 pstrcpy(buf, sizeof buf, pe->filename);
956 strcpy(tcc_fileextension(buf), ".def");
957 op = fopen(buf, "w");
958 if (NULL == op) {
959 tcc_error_noabort("could not create '%s': %s", buf, strerror(errno));
960 } else {
961 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", dllname);
962 if (pe->s1->verbose)
963 printf("<- %s (%d symbol%s)\n", buf, sym_count, &"s"[sym_count < 2]);
965 #endif
967 for (ord = 0; ord < sym_count; ++ord)
969 p = sorted[ord], sym_index = p->index, name = p->name;
970 /* insert actual address later in pe_relocate_rva */
971 put_elf_reloc(symtab_section, pe->thunk,
972 func_o, R_XXX_RELATIVE, sym_index);
973 *(DWORD*)(pe->thunk->data + name_o)
974 = pe->thunk->data_offset + rva_base;
975 *(WORD*)(pe->thunk->data + ord_o)
976 = ord;
977 put_elf_str(pe->thunk, name);
978 func_o += sizeof (DWORD);
979 name_o += sizeof (DWORD);
980 ord_o += sizeof (WORD);
981 if (op)
982 fprintf(op, "%s\n", name);
984 pe->exp_size = pe->thunk->data_offset - pe->exp_offs;
985 dynarray_reset(&sorted, &sym_count);
986 if (op)
987 fclose(op);
990 /* ------------------------------------------------------------- */
991 static void pe_build_reloc (struct pe_info *pe)
993 DWORD offset, block_ptr, addr;
994 int count, i;
995 ElfW_Rel *rel, *rel_end;
996 Section *s = NULL, *sr;
998 offset = addr = block_ptr = count = i = 0;
999 rel = rel_end = NULL;
1001 for(;;) {
1002 if (rel < rel_end) {
1003 int type = ELFW(R_TYPE)(rel->r_info);
1004 addr = rel->r_offset + s->sh_addr;
1005 ++ rel;
1006 if (type != REL_TYPE_DIRECT)
1007 continue;
1008 if (count == 0) { /* new block */
1009 block_ptr = pe->reloc->data_offset;
1010 section_ptr_add(pe->reloc, sizeof(struct pe_reloc_header));
1011 offset = addr & 0xFFFFFFFF<<12;
1013 if ((addr -= offset) < (1<<12)) { /* one block spans 4k addresses */
1014 WORD *wp = section_ptr_add(pe->reloc, sizeof (WORD));
1015 *wp = addr | PE_IMAGE_REL<<12;
1016 ++count;
1017 continue;
1019 -- rel;
1021 } else if (i < pe->sec_count) {
1022 sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc;
1023 if (sr) {
1024 rel = (ElfW_Rel *)sr->data;
1025 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1027 continue;
1030 if (count) {
1031 /* store the last block and ready for a new one */
1032 struct pe_reloc_header *hdr;
1033 if (count & 1) /* align for DWORDS */
1034 section_ptr_add(pe->reloc, sizeof(WORD)), ++count;
1035 hdr = (struct pe_reloc_header *)(pe->reloc->data + block_ptr);
1036 hdr -> offset = offset - pe->imagebase;
1037 hdr -> size = count * sizeof(WORD) + sizeof(struct pe_reloc_header);
1038 count = 0;
1041 if (rel >= rel_end)
1042 break;
1046 /* ------------------------------------------------------------- */
1047 static int pe_section_class(Section *s)
1049 int type, flags;
1050 const char *name;
1052 type = s->sh_type;
1053 flags = s->sh_flags;
1054 name = s->name;
1055 if (flags & SHF_ALLOC) {
1056 if (type == SHT_PROGBITS) {
1057 if (flags & SHF_EXECINSTR)
1058 return sec_text;
1059 if (flags & SHF_WRITE)
1060 return sec_data;
1061 if (0 == strcmp(name, ".rsrc"))
1062 return sec_rsrc;
1063 if (0 == strcmp(name, ".iedat"))
1064 return sec_idata;
1065 if (0 == strcmp(name, ".pdata"))
1066 return sec_pdata;
1067 return sec_other;
1068 } else if (type == SHT_NOBITS) {
1069 if (flags & SHF_WRITE)
1070 return sec_bss;
1072 } else {
1073 if (0 == strcmp(name, ".reloc"))
1074 return sec_reloc;
1075 if (0 == strncmp(name, ".stab", 5)) /* .stab and .stabstr */
1076 return sec_stab;
1078 return -1;
1081 static int pe_assign_addresses (struct pe_info *pe)
1083 int i, k, o, c;
1084 DWORD addr;
1085 int *section_order;
1086 struct section_info *si;
1087 Section *s;
1089 if (PE_DLL == pe->type)
1090 pe->reloc = new_section(pe->s1, ".reloc", SHT_PROGBITS, 0);
1092 // pe->thunk = new_section(pe->s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);
1094 section_order = tcc_malloc(pe->s1->nb_sections * sizeof (int));
1095 for (o = k = 0 ; k < sec_last; ++k) {
1096 for (i = 1; i < pe->s1->nb_sections; ++i) {
1097 s = pe->s1->sections[i];
1098 if (k == pe_section_class(s)) {
1099 // printf("%s %d\n", s->name, k);
1100 s->sh_addr = pe->imagebase;
1101 section_order[o++] = i;
1106 pe->sec_info = tcc_mallocz(o * sizeof (struct section_info));
1107 addr = pe->imagebase + 1;
1109 for (i = 0; i < o; ++i)
1111 k = section_order[i];
1112 s = pe->s1->sections[k];
1113 c = pe_section_class(s);
1114 si = &pe->sec_info[pe->sec_count];
1116 #ifdef PE_MERGE_DATA
1117 if (c == sec_bss && pe->sec_count && si[-1].cls == sec_data) {
1118 /* append .bss to .data */
1119 s->sh_addr = addr = ((addr-1) | (s->sh_addralign-1)) + 1;
1120 addr += s->data_offset;
1121 si[-1].sh_size = addr - si[-1].sh_addr;
1122 continue;
1124 #endif
1125 if (c == sec_stab && 0 == pe->s1->do_debug)
1126 continue;
1128 strcpy(si->name, s->name);
1129 si->cls = c;
1130 si->ord = k;
1131 si->sh_addr = s->sh_addr = addr = pe_virtual_align(pe, addr);
1132 si->sh_flags = s->sh_flags;
1134 if (c == sec_data && NULL == pe->thunk)
1135 pe->thunk = s;
1137 if (s == pe->thunk) {
1138 pe_build_imports(pe);
1139 pe_build_exports(pe);
1142 if (c == sec_reloc)
1143 pe_build_reloc (pe);
1145 if (s->data_offset)
1147 if (s->sh_type != SHT_NOBITS) {
1148 si->data = s->data;
1149 si->data_size = s->data_offset;
1152 addr += s->data_offset;
1153 si->sh_size = s->data_offset;
1154 ++pe->sec_count;
1156 // printf("%08x %05x %s\n", si->sh_addr, si->sh_size, si->name);
1159 #if 0
1160 for (i = 1; i < pe->s1->nb_sections; ++i) {
1161 Section *s = pe->s1->sections[i];
1162 int type = s->sh_type;
1163 int flags = s->sh_flags;
1164 printf("section %-16s %-10s %5x %s,%s,%s\n",
1165 s->name,
1166 type == SHT_PROGBITS ? "progbits" :
1167 type == SHT_NOBITS ? "nobits" :
1168 type == SHT_SYMTAB ? "symtab" :
1169 type == SHT_STRTAB ? "strtab" :
1170 type == SHT_RELX ? "rel" : "???",
1171 s->data_offset,
1172 flags & SHF_ALLOC ? "alloc" : "",
1173 flags & SHF_WRITE ? "write" : "",
1174 flags & SHF_EXECINSTR ? "exec" : ""
1177 pe->s1->verbose = 2;
1178 #endif
1180 tcc_free(section_order);
1181 return 0;
1184 /* ------------------------------------------------------------- */
1185 static void pe_relocate_rva (struct pe_info *pe, Section *s)
1187 Section *sr = s->reloc;
1188 ElfW_Rel *rel, *rel_end;
1189 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1190 for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
1191 if (ELFW(R_TYPE)(rel->r_info) == R_XXX_RELATIVE) {
1192 int sym_index = ELFW(R_SYM)(rel->r_info);
1193 DWORD addr = s->sh_addr;
1194 if (sym_index) {
1195 ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
1196 addr = sym->st_value;
1198 // printf("reloc rva %08x %08x %s\n", (DWORD)rel->r_offset, addr, s->name);
1199 *(DWORD*)(s->data + rel->r_offset) += addr - pe->imagebase;
1204 /*----------------------------------------------------------------------------*/
1206 static int pe_isafunc(int sym_index)
1208 Section *sr = text_section->reloc;
1209 ElfW_Rel *rel, *rel_end;
1210 Elf32_Word info = ELF32_R_INFO(sym_index, R_386_PC32);
1211 if (!sr)
1212 return 0;
1213 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1214 for (rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++)
1215 if (rel->r_info == info)
1216 return 1;
1217 return 0;
1220 /*----------------------------------------------------------------------------*/
1221 static int pe_check_symbols(struct pe_info *pe)
1223 ElfW(Sym) *sym;
1224 int sym_index, sym_end;
1225 int ret = 0;
1227 pe_align_section(text_section, 8);
1229 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
1230 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
1232 sym = (ElfW(Sym) *)symtab_section->data + sym_index;
1233 if (sym->st_shndx == SHN_UNDEF) {
1235 const char *name = (char*)symtab_section->link->data + sym->st_name;
1236 unsigned type = ELFW(ST_TYPE)(sym->st_info);
1237 int imp_sym = pe_find_import(pe->s1, sym);
1238 struct import_symbol *is;
1240 if (imp_sym <= 0)
1241 goto not_found;
1243 if (type == STT_NOTYPE) {
1244 /* symbols from assembler have no type, find out which */
1245 if (pe_isafunc(sym_index))
1246 type = STT_FUNC;
1247 else
1248 type = STT_OBJECT;
1251 is = pe_add_import(pe, imp_sym);
1253 if (type == STT_FUNC) {
1254 unsigned long offset = is->thk_offset;
1255 if (offset) {
1256 /* got aliased symbol, like stricmp and _stricmp */
1258 } else {
1259 char buffer[100];
1260 WORD *p;
1262 offset = text_section->data_offset;
1263 /* add the 'jmp IAT[x]' instruction */
1264 #ifdef TCC_TARGET_ARM
1265 p = section_ptr_add(text_section, 8+4); // room for code and address
1266 (*(DWORD*)(p)) = 0xE59FC000; // arm code ldr ip, [pc] ; PC+8+0 = 0001xxxx
1267 (*(DWORD*)(p+2)) = 0xE59CF000; // arm code ldr pc, [ip]
1268 #else
1269 p = section_ptr_add(text_section, 8);
1270 *p = 0x25FF;
1271 #ifdef TCC_TARGET_X86_64
1272 *(DWORD*)(p+1) = (DWORD)-4;
1273 #endif
1274 #endif
1275 /* add a helper symbol, will be patched later in
1276 pe_build_imports */
1277 sprintf(buffer, "IAT.%s", name);
1278 is->iat_index = put_elf_sym(
1279 symtab_section, 0, sizeof(DWORD),
1280 ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
1281 0, SHN_UNDEF, buffer);
1282 #ifdef TCC_TARGET_ARM
1283 put_elf_reloc(symtab_section, text_section,
1284 offset + 8, R_XXX_THUNKFIX, is->iat_index); // offset to IAT position
1285 #else
1286 put_elf_reloc(symtab_section, text_section,
1287 offset + 2, R_XXX_THUNKFIX, is->iat_index);
1288 #endif
1289 is->thk_offset = offset;
1292 /* tcc_realloc might have altered sym's address */
1293 sym = (ElfW(Sym) *)symtab_section->data + sym_index;
1295 /* patch the original symbol */
1296 sym->st_value = offset;
1297 sym->st_shndx = text_section->sh_num;
1298 sym->st_other &= ~ST_PE_EXPORT; /* do not export */
1299 continue;
1302 if (type == STT_OBJECT) { /* data, ptr to that should be */
1303 if (0 == is->iat_index) {
1304 /* original symbol will be patched later in pe_build_imports */
1305 is->iat_index = sym_index;
1306 continue;
1310 not_found:
1311 if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
1312 /* STB_WEAK undefined symbols are accepted */
1313 continue;
1314 tcc_error_noabort("undefined symbol '%s'%s", name,
1315 imp_sym < 0 ? ", missing __declspec(dllimport)?":"");
1316 ret = -1;
1318 } else if (pe->s1->rdynamic
1319 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
1320 /* if -rdynamic option, then export all non local symbols */
1321 sym->st_other |= ST_PE_EXPORT;
1324 return ret;
1327 /*----------------------------------------------------------------------------*/
1328 #ifdef PE_PRINT_SECTIONS
1329 static void pe_print_section(FILE * f, Section * s)
1331 /* just if you're curious */
1332 BYTE *p, *e, b;
1333 int i, n, l, m;
1334 p = s->data;
1335 e = s->data + s->data_offset;
1336 l = e - p;
1338 fprintf(f, "section \"%s\"", s->name);
1339 if (s->link)
1340 fprintf(f, "\nlink \"%s\"", s->link->name);
1341 if (s->reloc)
1342 fprintf(f, "\nreloc \"%s\"", s->reloc->name);
1343 fprintf(f, "\nv_addr %08X", (unsigned)s->sh_addr);
1344 fprintf(f, "\ncontents %08X", (unsigned)l);
1345 fprintf(f, "\n\n");
1347 if (s->sh_type == SHT_NOBITS)
1348 return;
1350 if (0 == l)
1351 return;
1353 if (s->sh_type == SHT_SYMTAB)
1354 m = sizeof(ElfW(Sym));
1355 else if (s->sh_type == SHT_RELX)
1356 m = sizeof(ElfW_Rel);
1357 else
1358 m = 16;
1360 fprintf(f, "%-8s", "offset");
1361 for (i = 0; i < m; ++i)
1362 fprintf(f, " %02x", i);
1363 n = 56;
1365 if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_RELX) {
1366 const char *fields1[] = {
1367 "name",
1368 "value",
1369 "size",
1370 "bind",
1371 "type",
1372 "other",
1373 "shndx",
1374 NULL
1377 const char *fields2[] = {
1378 "offs",
1379 "type",
1380 "symb",
1381 NULL
1384 const char **p;
1386 if (s->sh_type == SHT_SYMTAB)
1387 p = fields1, n = 106;
1388 else
1389 p = fields2, n = 58;
1391 for (i = 0; p[i]; ++i)
1392 fprintf(f, "%6s", p[i]);
1393 fprintf(f, " symbol");
1396 fprintf(f, "\n");
1397 for (i = 0; i < n; ++i)
1398 fprintf(f, "-");
1399 fprintf(f, "\n");
1401 for (i = 0; i < l;)
1403 fprintf(f, "%08X", i);
1404 for (n = 0; n < m; ++n) {
1405 if (n + i < l)
1406 fprintf(f, " %02X", p[i + n]);
1407 else
1408 fprintf(f, " ");
1411 if (s->sh_type == SHT_SYMTAB) {
1412 ElfW(Sym) *sym = (ElfW(Sym) *) (p + i);
1413 const char *name = s->link->data + sym->st_name;
1414 fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",
1415 (unsigned)sym->st_name,
1416 (unsigned)sym->st_value,
1417 (unsigned)sym->st_size,
1418 (unsigned)ELFW(ST_BIND)(sym->st_info),
1419 (unsigned)ELFW(ST_TYPE)(sym->st_info),
1420 (unsigned)sym->st_other,
1421 (unsigned)sym->st_shndx,
1422 name);
1424 } else if (s->sh_type == SHT_RELX) {
1425 ElfW_Rel *rel = (ElfW_Rel *) (p + i);
1426 ElfW(Sym) *sym =
1427 (ElfW(Sym) *) s->link->data + ELFW(R_SYM)(rel->r_info);
1428 const char *name = s->link->link->data + sym->st_name;
1429 fprintf(f, " %04X %02X %04X \"%s\"",
1430 (unsigned)rel->r_offset,
1431 (unsigned)ELFW(R_TYPE)(rel->r_info),
1432 (unsigned)ELFW(R_SYM)(rel->r_info),
1433 name);
1434 } else {
1435 fprintf(f, " ");
1436 for (n = 0; n < m; ++n) {
1437 if (n + i < l) {
1438 b = p[i + n];
1439 if (b < 32 || b >= 127)
1440 b = '.';
1441 fprintf(f, "%c", b);
1445 i += m;
1446 fprintf(f, "\n");
1448 fprintf(f, "\n\n");
1451 static void pe_print_sections(TCCState *s1, const char *fname)
1453 Section *s;
1454 FILE *f;
1455 int i;
1456 f = fopen(fname, "w");
1457 for (i = 1; i < s1->nb_sections; ++i) {
1458 s = s1->sections[i];
1459 pe_print_section(f, s);
1461 pe_print_section(f, s1->dynsymtab_section);
1462 fclose(f);
1464 #endif
1466 /* ------------------------------------------------------------- */
1467 /* helper function for load/store to insert one more indirection */
1469 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1470 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2)
1472 int r2;
1473 if ((sv->r & (VT_VALMASK|VT_SYM)) != (VT_CONST|VT_SYM) || (sv->r2 != VT_CONST))
1474 return sv;
1475 if (!sv->sym->a.dllimport)
1476 return sv;
1477 // printf("import %04x %04x %04x %s\n", sv->type.t, sv->sym->type.t, sv->r, get_tok_str(sv->sym->v, NULL));
1478 memset(v2, 0, sizeof *v2);
1479 v2->type.t = VT_PTR;
1480 v2->r = VT_CONST | VT_SYM | VT_LVAL;
1481 v2->sym = sv->sym;
1483 r2 = get_reg(RC_INT);
1484 load(r2, v2);
1485 v2->r = r2;
1486 if ((uint32_t)sv->c.i) {
1487 vpushv(v2);
1488 vpushi(sv->c.i);
1489 gen_opi('+');
1490 *v2 = *vtop--;
1492 v2->type.t = sv->type.t;
1493 v2->r |= sv->r & VT_LVAL;
1494 return v2;
1496 #endif
1498 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)
1500 return set_elf_sym(
1501 s1->dynsymtab_section,
1502 value,
1503 dllindex, /* st_size */
1504 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE),
1506 value ? SHN_ABS : SHN_UNDEF,
1507 name
1511 static int add_dllref(TCCState *s1, const char *dllname)
1513 DLLReference *dllref;
1514 int i;
1515 for (i = 0; i < s1->nb_loaded_dlls; ++i)
1516 if (0 == strcmp(s1->loaded_dlls[i]->name, dllname))
1517 return i + 1;
1518 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));
1519 strcpy(dllref->name, dllname);
1520 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
1521 return s1->nb_loaded_dlls;
1524 /* ------------------------------------------------------------- */
1526 static int read_mem(int fd, unsigned offset, void *buffer, unsigned len)
1528 lseek(fd, offset, SEEK_SET);
1529 return len == read(fd, buffer, len);
1532 /* ------------------------------------------------------------- */
1534 PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp)
1536 int l, i, n, n0, ret;
1537 char *p;
1538 int fd;
1540 IMAGE_SECTION_HEADER ish;
1541 IMAGE_EXPORT_DIRECTORY ied;
1542 IMAGE_DOS_HEADER dh;
1543 IMAGE_FILE_HEADER ih;
1544 DWORD sig, ref, addr, ptr, namep;
1546 int pef_hdroffset, opt_hdroffset, sec_hdroffset;
1548 n = n0 = 0;
1549 p = NULL;
1550 ret = -1;
1552 fd = open(filename, O_RDONLY | O_BINARY);
1553 if (fd < 0)
1554 goto the_end_1;
1555 ret = 1;
1556 if (!read_mem(fd, 0, &dh, sizeof dh))
1557 goto the_end;
1558 if (!read_mem(fd, dh.e_lfanew, &sig, sizeof sig))
1559 goto the_end;
1560 if (sig != 0x00004550)
1561 goto the_end;
1562 pef_hdroffset = dh.e_lfanew + sizeof sig;
1563 if (!read_mem(fd, pef_hdroffset, &ih, sizeof ih))
1564 goto the_end;
1565 opt_hdroffset = pef_hdroffset + sizeof ih;
1566 if (ih.Machine == 0x014C) {
1567 IMAGE_OPTIONAL_HEADER32 oh;
1568 sec_hdroffset = opt_hdroffset + sizeof oh;
1569 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))
1570 goto the_end;
1571 if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
1572 goto the_end_0;
1573 addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
1574 } else if (ih.Machine == 0x8664) {
1575 IMAGE_OPTIONAL_HEADER64 oh;
1576 sec_hdroffset = opt_hdroffset + sizeof oh;
1577 if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh))
1578 goto the_end;
1579 if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
1580 goto the_end_0;
1581 addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
1582 } else
1583 goto the_end;
1585 //printf("addr: %08x\n", addr);
1586 for (i = 0; i < ih.NumberOfSections; ++i) {
1587 if (!read_mem(fd, sec_hdroffset + i * sizeof ish, &ish, sizeof ish))
1588 goto the_end;
1589 //printf("vaddr: %08x\n", ish.VirtualAddress);
1590 if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData)
1591 goto found;
1593 goto the_end_0;
1595 found:
1596 ref = ish.VirtualAddress - ish.PointerToRawData;
1597 if (!read_mem(fd, addr - ref, &ied, sizeof ied))
1598 goto the_end;
1600 namep = ied.AddressOfNames - ref;
1601 for (i = 0; i < ied.NumberOfNames; ++i) {
1602 if (!read_mem(fd, namep, &ptr, sizeof ptr))
1603 goto the_end;
1604 namep += sizeof ptr;
1605 for (l = 0;;) {
1606 if (n+1 >= n0)
1607 p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256);
1608 if (!read_mem(fd, ptr - ref + l++, p + n, 1)) {
1609 tcc_free(p), p = NULL;
1610 goto the_end;
1612 if (p[n++] == 0)
1613 break;
1616 if (p)
1617 p[n] = 0;
1618 the_end_0:
1619 ret = 0;
1620 the_end:
1621 close(fd);
1622 the_end_1:
1623 *pp = p;
1624 return ret;
1627 /* -------------------------------------------------------------
1628 * This is for compiled windows resources in 'coff' format
1629 * as generated by 'windres.exe -O coff ...'.
1632 static int pe_load_res(TCCState *s1, int fd)
1634 struct pe_rsrc_header hdr;
1635 Section *rsrc_section;
1636 int i, ret = -1;
1637 BYTE *ptr;
1638 unsigned offs;
1640 if (!read_mem(fd, 0, &hdr, sizeof hdr))
1641 goto quit;
1643 if (hdr.filehdr.Machine != IMAGE_FILE_MACHINE
1644 || hdr.filehdr.NumberOfSections != 1
1645 || strcmp((char*)hdr.sectionhdr.Name, ".rsrc") != 0)
1646 goto quit;
1648 rsrc_section = new_section(s1, ".rsrc", SHT_PROGBITS, SHF_ALLOC);
1649 ptr = section_ptr_add(rsrc_section, hdr.sectionhdr.SizeOfRawData);
1650 offs = hdr.sectionhdr.PointerToRawData;
1651 if (!read_mem(fd, offs, ptr, hdr.sectionhdr.SizeOfRawData))
1652 goto quit;
1653 offs = hdr.sectionhdr.PointerToRelocations;
1654 for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i)
1656 struct pe_rsrc_reloc rel;
1657 if (!read_mem(fd, offs, &rel, sizeof rel))
1658 goto quit;
1659 // printf("rsrc_reloc: %x %x %x\n", rel.offset, rel.size, rel.type);
1660 if (rel.type != RSRC_RELTYPE)
1661 goto quit;
1662 put_elf_reloc(symtab_section, rsrc_section,
1663 rel.offset, R_XXX_RELATIVE, 0);
1664 offs += sizeof rel;
1666 ret = 0;
1667 quit:
1668 return ret;
1671 /* ------------------------------------------------------------- */
1673 static char *trimfront(char *p)
1675 while (*p && (unsigned char)*p <= ' ')
1676 ++p;
1677 return p;
1680 static char *trimback(char *a, char *e)
1682 while (e > a && (unsigned char)e[-1] <= ' ')
1683 --e;
1684 *e = 0;;
1685 return a;
1688 /* ------------------------------------------------------------- */
1689 static int pe_load_def(TCCState *s1, int fd)
1691 int state = 0, ret = -1, dllindex = 0, ord;
1692 char line[400], dllname[80], *p, *x;
1693 FILE *fp;
1695 fp = fdopen(dup(fd), "rb");
1696 while (fgets(line, sizeof line, fp))
1698 p = trimfront(trimback(line, strchr(line, 0)));
1699 if (0 == *p || ';' == *p)
1700 continue;
1702 switch (state) {
1703 case 0:
1704 if (0 != strnicmp(p, "LIBRARY", 7))
1705 goto quit;
1706 pstrcpy(dllname, sizeof dllname, trimfront(p+7));
1707 ++state;
1708 continue;
1710 case 1:
1711 if (0 != stricmp(p, "EXPORTS"))
1712 goto quit;
1713 ++state;
1714 continue;
1716 case 2:
1717 dllindex = add_dllref(s1, dllname);
1718 ++state;
1719 /* fall through */
1720 default:
1721 /* get ordinal and will store in sym->st_value */
1722 ord = 0;
1723 x = strchr(p, ' ');
1724 if (x) {
1725 *x = 0, x = strrchr(x + 1, '@');
1726 if (x) {
1727 char *d;
1728 ord = (int)strtol(x + 1, &d, 10);
1729 if (*d)
1730 ord = 0;
1733 pe_putimport(s1, dllindex, p, ord);
1734 continue;
1737 ret = 0;
1738 quit:
1739 fclose(fp);
1740 return ret;
1743 /* ------------------------------------------------------------- */
1744 static int pe_load_dll(TCCState *s1, const char *filename)
1746 char *p, *q;
1747 int index, ret;
1749 ret = tcc_get_dllexports(filename, &p);
1750 if (ret) {
1751 return -1;
1752 } else if (p) {
1753 index = add_dllref(s1, tcc_basename(filename));
1754 for (q = p; *q; q += 1 + strlen(q))
1755 pe_putimport(s1, index, q, 0);
1756 tcc_free(p);
1758 return 0;
1761 /* ------------------------------------------------------------- */
1762 ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd)
1764 int ret = -1;
1765 char buf[10];
1766 if (0 == strcmp(tcc_fileextension(filename), ".def"))
1767 ret = pe_load_def(s1, fd);
1768 else if (pe_load_res(s1, fd) == 0)
1769 ret = 0;
1770 else if (read_mem(fd, 0, buf, 4) && 0 == memcmp(buf, "MZ\220", 4))
1771 ret = pe_load_dll(s1, filename);
1772 return ret;
1775 /* ------------------------------------------------------------- */
1776 #ifdef TCC_TARGET_X86_64
1777 static unsigned pe_add_uwwind_info(TCCState *s1)
1779 if (NULL == s1->uw_pdata) {
1780 s1->uw_pdata = find_section(tcc_state, ".pdata");
1781 s1->uw_pdata->sh_addralign = 4;
1782 s1->uw_sym = put_elf_sym(symtab_section, 0, 0, 0, 0, text_section->sh_num, NULL);
1785 if (0 == s1->uw_offs) {
1786 /* As our functions all have the same stackframe, we use one entry for all */
1787 static const unsigned char uw_info[] = {
1788 0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
1789 0x04, // UBYTE Size of prolog
1790 0x02, // UBYTE Count of unwind codes
1791 0x05, // UBYTE: 4 Frame Register (rbp), UBYTE: 4 Frame Register offset (scaled)
1792 // USHORT * n Unwind codes array
1793 // 0x0b, 0x01, 0xff, 0xff, // stack size
1794 0x04, 0x03, // set frame ptr (mov rsp -> rbp)
1795 0x01, 0x50 // push reg (rbp)
1798 Section *s = text_section;
1799 unsigned char *p;
1801 section_ptr_add(s, -s->data_offset & 3); /* align */
1802 s1->uw_offs = s->data_offset;
1803 p = section_ptr_add(s, sizeof uw_info);
1804 memcpy(p, uw_info, sizeof uw_info);
1807 return s1->uw_offs;
1810 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack)
1812 TCCState *s1 = tcc_state;
1813 Section *pd;
1814 unsigned o, n, d;
1815 struct /* _RUNTIME_FUNCTION */ {
1816 DWORD BeginAddress;
1817 DWORD EndAddress;
1818 DWORD UnwindData;
1819 } *p;
1821 d = pe_add_uwwind_info(s1);
1822 pd = s1->uw_pdata;
1823 o = pd->data_offset;
1824 p = section_ptr_add(pd, sizeof *p);
1826 /* record this function */
1827 p->BeginAddress = start;
1828 p->EndAddress = end;
1829 p->UnwindData = d;
1831 /* put relocations on it */
1832 for (n = o + sizeof *p; o < n; o += sizeof p->BeginAddress)
1833 put_elf_reloc(symtab_section, pd, o, R_X86_64_RELATIVE, s1->uw_sym);
1835 #endif
1836 /* ------------------------------------------------------------- */
1837 #ifdef TCC_TARGET_X86_64
1838 #define PE_STDSYM(n,s) n
1839 #else
1840 #define PE_STDSYM(n,s) "_" n s
1841 #endif
1843 static void pe_add_runtime(TCCState *s1, struct pe_info *pe)
1845 const char *start_symbol;
1846 int pe_type = 0;
1847 int unicode_entry = 0;
1849 if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16")))
1850 pe_type = PE_GUI;
1851 else
1852 if (find_elf_sym(symtab_section, PE_STDSYM("wWinMain","@16"))) {
1853 pe_type = PE_GUI;
1854 unicode_entry = PE_GUI;
1856 else
1857 if (TCC_OUTPUT_DLL == s1->output_type) {
1858 pe_type = PE_DLL;
1859 /* need this for 'tccelf.c:relocate_section()' */
1860 s1->output_type = TCC_OUTPUT_EXE;
1862 else {
1863 pe_type = PE_EXE;
1864 if (find_elf_sym(symtab_section, "wmain"))
1865 unicode_entry = PE_EXE;
1868 start_symbol =
1869 TCC_OUTPUT_MEMORY == s1->output_type
1870 ? PE_GUI == pe_type ? (unicode_entry ? "__runwwinmain" : "__runwinmain")
1871 : (unicode_entry ? "__runwmain" : "__runmain")
1872 : PE_DLL == pe_type ? PE_STDSYM("__dllstart","@12")
1873 : PE_GUI == pe_type ? (unicode_entry ? "__wwinstart": "__winstart")
1874 : (unicode_entry ? "__wstart" : "__start")
1877 if (!s1->leading_underscore || strchr(start_symbol, '@'))
1878 ++start_symbol;
1880 /* grab the startup code from libtcc1 */
1881 #ifdef TCC_IS_NATIVE
1882 if (TCC_OUTPUT_MEMORY != s1->output_type || s1->runtime_main)
1883 #endif
1884 set_elf_sym(symtab_section,
1885 0, 0,
1886 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1887 SHN_UNDEF, start_symbol);
1889 tcc_add_pragma_libs(s1);
1891 if (0 == s1->nostdlib) {
1892 static const char *libs[] = {
1893 TCC_LIBTCC1, "msvcrt", "kernel32", "", "user32", "gdi32", NULL
1895 const char **pp, *p;
1896 for (pp = libs; 0 != (p = *pp); ++pp) {
1897 if (0 == *p) {
1898 if (PE_DLL != pe_type && PE_GUI != pe_type)
1899 break;
1900 } else if (pp == libs && tcc_add_dll(s1, p, 0) >= 0) {
1901 continue;
1902 } else {
1903 tcc_add_library_err(s1, p);
1908 if (TCC_OUTPUT_MEMORY == s1->output_type)
1909 pe_type = PE_RUN;
1910 pe->type = pe_type;
1911 pe->start_symbol = start_symbol;
1914 static void pe_set_options(TCCState * s1, struct pe_info *pe)
1916 if (PE_DLL == pe->type) {
1917 /* XXX: check if is correct for arm-pe target */
1918 pe->imagebase = 0x10000000;
1919 } else {
1920 #if defined(TCC_TARGET_ARM)
1921 pe->imagebase = 0x00010000;
1922 #else
1923 pe->imagebase = 0x00400000;
1924 #endif
1927 #if defined(TCC_TARGET_ARM)
1928 /* we use "console" subsystem by default */
1929 pe->subsystem = 9;
1930 #else
1931 if (PE_DLL == pe->type || PE_GUI == pe->type)
1932 pe->subsystem = 2;
1933 else
1934 pe->subsystem = 3;
1935 #endif
1936 /* Allow override via -Wl,-subsystem=... option */
1937 if (s1->pe_subsystem != 0)
1938 pe->subsystem = s1->pe_subsystem;
1940 /* set default file/section alignment */
1941 if (pe->subsystem == 1) {
1942 pe->section_align = 0x20;
1943 pe->file_align = 0x20;
1944 } else {
1945 pe->section_align = 0x1000;
1946 pe->file_align = 0x200;
1949 if (s1->section_align != 0)
1950 pe->section_align = s1->section_align;
1951 if (s1->pe_file_align != 0)
1952 pe->file_align = s1->pe_file_align;
1954 if ((pe->subsystem >= 10) && (pe->subsystem <= 12))
1955 pe->imagebase = 0;
1957 if (s1->has_text_addr)
1958 pe->imagebase = s1->text_addr;
1961 ST_FUNC int pe_output_file(TCCState *s1, const char *filename)
1963 int ret;
1964 struct pe_info pe;
1965 int i;
1967 memset(&pe, 0, sizeof pe);
1968 pe.filename = filename;
1969 pe.s1 = s1;
1971 tcc_add_bcheck(s1);
1972 pe_add_runtime(s1, &pe);
1973 relocate_common_syms(); /* assign bss addresses */
1974 tcc_add_linker_symbols(s1);
1975 pe_set_options(s1, &pe);
1977 ret = pe_check_symbols(&pe);
1978 if (ret)
1980 else if (filename) {
1981 pe_assign_addresses(&pe);
1982 relocate_syms(s1, s1->symtab, 0);
1983 for (i = 1; i < s1->nb_sections; ++i) {
1984 Section *s = s1->sections[i];
1985 if (s->reloc) {
1986 relocate_section(s1, s);
1987 pe_relocate_rva(&pe, s);
1990 pe.start_addr = (DWORD)
1991 ((uintptr_t)tcc_get_symbol_err(s1, pe.start_symbol)
1992 - pe.imagebase);
1993 if (s1->nb_errors)
1994 ret = -1;
1995 else
1996 ret = pe_write(&pe);
1997 tcc_free(pe.sec_info);
1998 } else {
1999 #ifdef TCC_IS_NATIVE
2000 pe.thunk = data_section;
2001 pe_build_imports(&pe);
2002 s1->runtime_main = pe.start_symbol;
2003 #endif
2006 pe_free_imports(&pe);
2008 #ifdef PE_PRINT_SECTIONS
2009 pe_print_sections(s1, "tcc.log");
2010 #endif
2011 return ret;
2014 /* ------------------------------------------------------------- */