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
24 #define ST_DATA static
28 #define stricmp strcasecmp
29 #define strnicmp strncasecmp
37 // #define PE_PRINT_SECTIONS
39 #if defined _WIN32 && (defined _WIN64) == (defined TCC_TARGET_X86_64)
43 /* ----------------------------------------------------------- */
44 #ifndef IMAGE_NT_SIGNATURE
45 /* ----------------------------------------------------------- */
46 /* definitions below are from winnt.h */
48 typedef unsigned char BYTE
;
49 typedef unsigned short WORD
;
50 typedef unsigned long DWORD
;
53 typedef struct _IMAGE_DOS_HEADER
{ /* DOS .EXE header */
54 WORD e_magic
; /* Magic number */
55 WORD e_cblp
; /* Bytes on last page of file */
56 WORD e_cp
; /* Pages in file */
57 WORD e_crlc
; /* Relocations */
58 WORD e_cparhdr
; /* Size of header in paragraphs */
59 WORD e_minalloc
; /* Minimum extra paragraphs needed */
60 WORD e_maxalloc
; /* Maximum extra paragraphs needed */
61 WORD e_ss
; /* Initial (relative) SS value */
62 WORD e_sp
; /* Initial SP value */
63 WORD e_csum
; /* Checksum */
64 WORD e_ip
; /* Initial IP value */
65 WORD e_cs
; /* Initial (relative) CS value */
66 WORD e_lfarlc
; /* File address of relocation table */
67 WORD e_ovno
; /* Overlay number */
68 WORD e_res
[4]; /* Reserved words */
69 WORD e_oemid
; /* OEM identifier (for e_oeminfo) */
70 WORD e_oeminfo
; /* OEM information; e_oemid specific */
71 WORD e_res2
[10]; /* Reserved words */
72 DWORD e_lfanew
; /* File address of new exe header */
73 } IMAGE_DOS_HEADER
, *PIMAGE_DOS_HEADER
;
75 #define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
76 #define SIZE_OF_NT_SIGNATURE 4
78 typedef struct _IMAGE_FILE_HEADER
{
80 WORD NumberOfSections
;
82 DWORD PointerToSymbolTable
;
83 DWORD NumberOfSymbols
;
84 WORD SizeOfOptionalHeader
;
86 } IMAGE_FILE_HEADER
, *PIMAGE_FILE_HEADER
;
89 #define IMAGE_SIZEOF_FILE_HEADER 20
91 typedef struct _IMAGE_DATA_DIRECTORY
{
94 } IMAGE_DATA_DIRECTORY
, *PIMAGE_DATA_DIRECTORY
;
97 typedef struct _IMAGE_OPTIONAL_HEADER
{
98 /* Standard fields. */
100 BYTE MajorLinkerVersion
;
101 BYTE MinorLinkerVersion
;
103 DWORD SizeOfInitializedData
;
104 DWORD SizeOfUninitializedData
;
105 DWORD AddressOfEntryPoint
;
109 /* NT additional fields. */
111 DWORD SectionAlignment
;
113 WORD MajorOperatingSystemVersion
;
114 WORD MinorOperatingSystemVersion
;
115 WORD MajorImageVersion
;
116 WORD MinorImageVersion
;
117 WORD MajorSubsystemVersion
;
118 WORD MinorSubsystemVersion
;
119 DWORD Win32VersionValue
;
124 WORD DllCharacteristics
;
125 DWORD SizeOfStackReserve
;
126 DWORD SizeOfStackCommit
;
127 DWORD SizeOfHeapReserve
;
128 DWORD SizeOfHeapCommit
;
130 DWORD NumberOfRvaAndSizes
;
131 IMAGE_DATA_DIRECTORY DataDirectory
[16];
133 } IMAGE_OPTIONAL_HEADER
, *PIMAGE_OPTIONAL_HEADER
;
136 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
137 #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
138 #define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */
139 #define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */
140 #define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */
141 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */
142 #define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */
143 /* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */
144 #define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */
145 #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */
146 #define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */
147 #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */
148 #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */
149 #define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
150 #define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */
151 #define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */
153 /* Section header format. */
154 #define IMAGE_SIZEOF_SHORT_NAME 8
156 typedef struct _IMAGE_SECTION_HEADER
{
157 BYTE Name
[IMAGE_SIZEOF_SHORT_NAME
];
159 DWORD PhysicalAddress
;
162 DWORD VirtualAddress
;
164 DWORD PointerToRawData
;
165 DWORD PointerToRelocations
;
166 DWORD PointerToLinenumbers
;
167 WORD NumberOfRelocations
;
168 WORD NumberOfLinenumbers
;
169 DWORD Characteristics
;
170 } IMAGE_SECTION_HEADER
, *PIMAGE_SECTION_HEADER
;
172 #define IMAGE_SIZEOF_SECTION_HEADER 40
174 typedef struct _IMAGE_BASE_RELOCATION
{
175 DWORD VirtualAddress
;
177 // WORD TypeOffset[1];
178 } IMAGE_BASE_RELOCATION
;
180 #define IMAGE_SIZEOF_BASE_RELOCATION 8
182 #define IMAGE_REL_BASED_ABSOLUTE 0
183 #define IMAGE_REL_BASED_HIGH 1
184 #define IMAGE_REL_BASED_LOW 2
185 #define IMAGE_REL_BASED_HIGHLOW 3
186 #define IMAGE_REL_BASED_HIGHADJ 4
187 #define IMAGE_REL_BASED_MIPS_JMPADDR 5
188 #define IMAGE_REL_BASED_SECTION 6
189 #define IMAGE_REL_BASED_REL32 7
193 /* ----------------------------------------------------------- */
194 #endif /* ndef IMAGE_NT_SIGNATURE */
195 /* ----------------------------------------------------------- */
197 #pragma pack(push, 1)
201 IMAGE_DOS_HEADER doshdr
;
204 IMAGE_FILE_HEADER filehdr
;
205 IMAGE_OPTIONAL_HEADER opthdr
;
208 struct pe_import_header
{
212 DWORD lib_name_offset
;
216 struct pe_export_header
{
217 DWORD Characteristics
;
222 DWORD NumberOfFunctions
;
224 DWORD AddressOfFunctions
;
225 DWORD AddressOfNames
;
226 DWORD AddressOfNameOrdinals
;
229 struct pe_reloc_header
{
234 struct pe_rsrc_header
{
235 struct _IMAGE_FILE_HEADER filehdr
;
236 struct _IMAGE_SECTION_HEADER sectionhdr
;
239 struct pe_rsrc_reloc
{
247 /* ----------------------------------------------------------- */
248 ST_DATA
struct pe_header pe_header
= {
250 /* IMAGE_DOS_HEADER doshdr */
251 0x5A4D, /*WORD e_magic; Magic number */
252 0x0090, /*WORD e_cblp; Bytes on last page of file */
253 0x0003, /*WORD e_cp; Pages in file */
254 0x0000, /*WORD e_crlc; Relocations */
256 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */
257 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */
258 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */
259 0x0000, /*WORD e_ss; Initial (relative) SS value */
261 0x00B8, /*WORD e_sp; Initial SP value */
262 0x0000, /*WORD e_csum; Checksum */
263 0x0000, /*WORD e_ip; Initial IP value */
264 0x0000, /*WORD e_cs; Initial (relative) CS value */
265 0x0040, /*WORD e_lfarlc; File address of relocation table */
266 0x0000, /*WORD e_ovno; Overlay number */
267 {0,0,0,0}, /*WORD e_res[4]; Reserved words */
268 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */
269 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */
270 {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */
271 0x00000080 /*DWORD e_lfanew; File address of new exe header */
273 /* BYTE dosstub[0x40] */
274 /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */
275 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68,
276 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f,
277 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20,
278 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
280 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */
282 /* IMAGE_FILE_HEADER filehdr */
283 0x014C, /*WORD Machine; */
284 0x0003, /*WORD NumberOfSections; */
285 0x00000000, /*DWORD TimeDateStamp; */
286 0x00000000, /*DWORD PointerToSymbolTable; */
287 0x00000000, /*DWORD NumberOfSymbols; */
288 0x00E0, /*WORD SizeOfOptionalHeader; */
289 0x030F /*WORD Characteristics; */
291 /* IMAGE_OPTIONAL_HEADER opthdr */
292 /* Standard fields. */
293 0x010B, /*WORD Magic; */
294 0x06, /*BYTE MajorLinkerVersion; */
295 0x00, /*BYTE MinorLinkerVersion; */
296 0x00000000, /*DWORD SizeOfCode; */
297 0x00000000, /*DWORD SizeOfInitializedData; */
298 0x00000000, /*DWORD SizeOfUninitializedData; */
299 0x00000000, /*DWORD AddressOfEntryPoint; */
300 0x00000000, /*DWORD BaseOfCode; */
301 0x00000000, /*DWORD BaseOfData; */
303 /* NT additional fields. */
304 0x00400000, /*DWORD ImageBase; */
305 0x00001000, /*DWORD SectionAlignment; */
306 0x00000200, /*DWORD FileAlignment; */
307 0x0004, /*WORD MajorOperatingSystemVersion; */
308 0x0000, /*WORD MinorOperatingSystemVersion; */
309 0x0000, /*WORD MajorImageVersion; */
310 0x0000, /*WORD MinorImageVersion; */
311 0x0004, /*WORD MajorSubsystemVersion; */
312 0x0000, /*WORD MinorSubsystemVersion; */
313 0x00000000, /*DWORD Win32VersionValue; */
314 0x00000000, /*DWORD SizeOfImage; */
315 0x00000200, /*DWORD SizeOfHeaders; */
316 0x00000000, /*DWORD CheckSum; */
317 0x0002, /*WORD Subsystem; */
318 0x0000, /*WORD DllCharacteristics; */
319 0x00100000, /*DWORD SizeOfStackReserve; */
320 0x00001000, /*DWORD SizeOfStackCommit; */
321 0x00100000, /*DWORD SizeOfHeapReserve; */
322 0x00001000, /*DWORD SizeOfHeapCommit; */
323 0x00000000, /*DWORD LoaderFlags; */
324 0x00000010, /*DWORD NumberOfRvaAndSizes; */
326 /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */
327 {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
328 {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}
331 /* ------------------------------------------------------------- */
332 /* internal temporary structures */
335 #define IMAGE_SCN_CNT_CODE 0x00000020
336 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
337 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
338 #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
339 #define IMAGE_SCN_MEM_SHARED 0x10000000
340 #define IMAGE_SCN_MEM_EXECUTE 0x20000000
341 #define IMAGE_SCN_MEM_READ 0x40000000
342 #define IMAGE_SCN_MEM_WRITE 0x80000000
357 ST_DATA DWORD pe_sec_flags
[] = {
358 0x60000020, /* ".text" , */
359 0xC0000040, /* ".data" , */
360 0xC0000080, /* ".bss" , */
361 0x40000040, /* ".idata" , */
362 0xE0000060, /* < other > , */
363 0x40000040, /* ".rsrc" , */
364 0x42000802, /* ".stab" , */
365 0x42000040, /* ".reloc" , */
368 struct section_info
{
376 IMAGE_SECTION_HEADER ish
;
379 struct import_symbol
{
385 struct pe_import_info
{
388 struct import_symbol
**symbols
;
395 const char *filename
;
406 struct section_info
*sec_info
;
408 struct pe_import_info
**imp_info
;
412 /* ------------------------------------------------------------- */
420 void error_noabort(const char *, ...);
423 void dbg_printf (const char *fmt
, ...)
429 x
= vsprintf (buffer
, fmt
, arg
);
430 strcpy(buffer
+x
, "\n");
431 OutputDebugString(buffer
);
435 /* --------------------------------------------*/
437 ST_FN
const char* get_alt_symbol(char *buffer
, const char *symbol
)
440 p
= strrchr(symbol
, '@');
441 if (p
&& isnum(p
[1]) && symbol
[0] == '_') { /* stdcall decor */
442 strcpy(buffer
, symbol
+1)[p
-symbol
-1] = 0;
443 } else if (symbol
[0] != '_') { /* try non-ansi function */
444 buffer
[0] = '_', strcpy(buffer
+ 1, symbol
);
445 } else if (0 == memcmp(symbol
, "__imp__", 7)) { /* mingw 2.0 */
446 strcpy(buffer
, symbol
+ 6);
447 } else if (0 == memcmp(symbol
, "_imp___", 7)) { /* mingw 3.7 */
448 strcpy(buffer
, symbol
+ 6);
455 ST_FN
int pe_find_import(TCCState
* s1
, const char *symbol
)
459 int sym_index
, n
= 0;
461 s
= n
? get_alt_symbol(buffer
, symbol
) : symbol
;
462 sym_index
= find_elf_sym(s1
->dynsymtab_section
, s
);
463 // printf("find (%d) %d %s\n", n, sym_index, s);
464 } while (0 == sym_index
&& ++n
< 2);
468 /*----------------------------------------------------------------------------*/
470 ST_FN
int dynarray_assoc(void **pp
, int n
, int key
)
473 for (i
= 0; i
< n
; ++i
, ++pp
)
474 if (key
== **(int **) pp
)
480 ST_FN DWORD
umin(DWORD a
, DWORD b
)
482 return a
< b
? a
: b
;
486 ST_FN DWORD
umax(DWORD a
, DWORD b
)
488 return a
< b
? b
: a
;
491 ST_FN
void pe_fpad(FILE *fp
, DWORD new_pos
)
493 DWORD pos
= ftell(fp
);
494 while (++pos
<= new_pos
)
498 ST_FN DWORD
pe_file_align(DWORD n
)
500 return (n
+ (0x200 - 1)) & ~(0x200 - 1);
503 ST_FN DWORD
pe_virtual_align(DWORD n
)
505 return (n
+ (0x1000 - 1)) & ~(0x1000 - 1);
508 ST_FN
void pe_align_section(Section
*s
, int a
)
510 int i
= s
->data_offset
& (a
-1);
512 section_ptr_add(s
, a
- i
);
515 ST_FN
void pe_set_datadir(int dir
, DWORD addr
, DWORD size
)
517 pe_header
.opthdr
.DataDirectory
[dir
].VirtualAddress
= addr
;
518 pe_header
.opthdr
.DataDirectory
[dir
].Size
= size
;
521 /*----------------------------------------------------------------------------*/
522 ST_FN
int pe_write(struct pe_info
*pe
)
526 DWORD file_offset
, r
;
528 op
= fopen(pe
->filename
, "wb");
530 error_noabort("could not write '%s': %s", pe
->filename
, strerror(errno
));
534 pe
->sizeofheaders
= pe_file_align(
536 + pe
->sec_count
* sizeof (IMAGE_SECTION_HEADER
)
539 file_offset
= pe
->sizeofheaders
;
540 pe_fpad(op
, file_offset
);
542 if (2 == pe
->s1
->verbose
)
543 printf("-------------------------------"
544 "\n virt file size section" "\n");
546 for (i
= 0; i
< pe
->sec_count
; ++i
) {
547 struct section_info
*si
= pe
->sec_info
+ i
;
548 const char *sh_name
= si
->name
;
549 unsigned long addr
= si
->sh_addr
- pe
->imagebase
;
550 unsigned long size
= si
->sh_size
;
551 IMAGE_SECTION_HEADER
*psh
= &si
->ish
;
553 if (2 == pe
->s1
->verbose
)
554 printf("%6lx %6lx %6lx %s\n",
555 addr
, file_offset
, size
, sh_name
);
559 pe_header
.opthdr
.BaseOfCode
= addr
;
560 pe_header
.opthdr
.AddressOfEntryPoint
= addr
+ pe
->start_addr
;
564 pe_header
.opthdr
.BaseOfData
= addr
;
571 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_BASERELOC
, addr
, size
);
575 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_RESOURCE
, addr
, size
);
582 if (pe
->thunk
== pe
->s1
->sections
[si
->ord
]) {
584 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IMPORT
,
585 pe
->imp_offs
+ addr
, pe
->imp_size
);
586 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IAT
,
587 pe
->iat_offs
+ addr
, pe
->iat_size
);
590 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_EXPORT
,
591 pe
->exp_offs
+ addr
, pe
->exp_size
);
595 strcpy((char*)psh
->Name
, sh_name
);
597 psh
->Characteristics
= pe_sec_flags
[si
->cls
];
598 psh
->VirtualAddress
= addr
;
599 psh
->Misc
.VirtualSize
= size
;
600 pe_header
.opthdr
.SizeOfImage
=
601 umax(pe_virtual_align(size
+ addr
), pe_header
.opthdr
.SizeOfImage
);
604 psh
->PointerToRawData
= r
= file_offset
;
605 fwrite(si
->data
, 1, si
->data_size
, op
);
606 file_offset
= pe_file_align(file_offset
+ si
->data_size
);
607 psh
->SizeOfRawData
= file_offset
- r
;
608 pe_fpad(op
, file_offset
);
612 // pe_header.filehdr.TimeDateStamp = time(NULL);
613 pe_header
.filehdr
.NumberOfSections
= pe
->sec_count
;
614 pe_header
.opthdr
.SizeOfHeaders
= pe
->sizeofheaders
;
615 pe_header
.opthdr
.ImageBase
= pe
->imagebase
;
616 if (PE_DLL
== pe
->type
)
617 pe_header
.filehdr
.Characteristics
= 0x230E;
618 else if (PE_GUI
!= pe
->type
)
619 pe_header
.opthdr
.Subsystem
= 3;
621 fseek(op
, SEEK_SET
, 0);
622 fwrite(&pe_header
, 1, sizeof pe_header
, op
);
623 for (i
= 0; i
< pe
->sec_count
; ++i
)
624 fwrite(&pe
->sec_info
[i
].ish
, 1, sizeof(IMAGE_SECTION_HEADER
), op
);
627 if (2 == pe
->s1
->verbose
)
628 printf("-------------------------------\n");
630 printf("<- %s (%lu bytes)\n", pe
->filename
, file_offset
);
635 /*----------------------------------------------------------------------------*/
637 #ifdef TCC_TARGET_X86_64
638 #define ELFW_R_(type) ELF64_R_##type
639 #define ElfW_Rel ElfW(Rela)
641 #define REL_TYPE_DIRECT R_X86_64_64
642 #define R_XXX_THUNKFIX R_X86_64_PC32
643 #define R_XXX_RELATIVE R_X86_64_RELATIVE
645 #define ELFW_ST_BIND ELF64_ST_BIND
646 #define ELFW_ST_TYPE ELF64_ST_TYPE
647 #define ELFW_ST_INFO ELF64_ST_INFO
649 #define ELFW_R_(type) ELF32_R_##type
650 #define ElfW_Rel ElfW(Rel)
652 #define REL_TYPE_DIRECT R_386_32
653 #define R_XXX_THUNKFIX R_386_32
654 #define R_XXX_RELATIVE R_386_RELATIVE
656 #define ELFW_ST_BIND ELF32_ST_BIND
657 #define ELFW_ST_TYPE ELF32_ST_TYPE
658 #define ELFW_ST_INFO ELF32_ST_INFO
660 /*----------------------------------------------------------------------------*/
662 ST_FN
struct import_symbol
*pe_add_import(struct pe_info
*pe
, int sym_index
)
666 struct pe_import_info
*p
;
667 struct import_symbol
*s
;
669 dll_index
= ((ElfW(Sym
) *)pe
->s1
->dynsymtab_section
->data
+ sym_index
)->st_value
;
673 i
= dynarray_assoc ((void**)pe
->imp_info
, pe
->imp_count
, dll_index
);
678 p
= tcc_mallocz(sizeof *p
);
679 p
->dll_index
= dll_index
;
680 dynarray_add((void***)&pe
->imp_info
, &pe
->imp_count
, p
);
683 i
= dynarray_assoc ((void**)p
->symbols
, p
->sym_count
, sym_index
);
685 return p
->symbols
[i
];
687 s
= tcc_mallocz(sizeof *s
);
688 dynarray_add((void***)&p
->symbols
, &p
->sym_count
, s
);
689 s
->sym_index
= sym_index
;
693 /*----------------------------------------------------------------------------*/
694 ST_FN
void pe_build_imports(struct pe_info
*pe
)
696 int thk_ptr
, ent_ptr
, dll_ptr
, sym_cnt
, i
;
697 DWORD rva_base
= pe
->thunk
->sh_addr
- pe
->imagebase
;
698 int ndlls
= pe
->imp_count
;
700 for (sym_cnt
= i
= 0; i
< ndlls
; ++i
)
701 sym_cnt
+= pe
->imp_info
[i
]->sym_count
;
706 pe_align_section(pe
->thunk
, 16);
708 pe
->imp_offs
= dll_ptr
= pe
->thunk
->data_offset
;
709 pe
->imp_size
= (ndlls
+ 1) * sizeof(struct pe_import_header
);
710 pe
->iat_offs
= dll_ptr
+ pe
->imp_size
;
711 pe
->iat_size
= (sym_cnt
+ ndlls
) * sizeof(DWORD
);
712 section_ptr_add(pe
->thunk
, pe
->imp_size
+ 2*pe
->iat_size
);
714 thk_ptr
= pe
->iat_offs
;
715 ent_ptr
= pe
->iat_offs
+ pe
->iat_size
;
717 for (i
= 0; i
< pe
->imp_count
; ++i
) {
718 struct pe_import_header
*hdr
;
721 struct pe_import_info
*p
= pe
->imp_info
[i
];
722 const char *name
= pe
->s1
->loaded_dlls
[p
->dll_index
-1]->name
;
724 /* put the dll name into the import header */
725 v
= put_elf_str(pe
->thunk
, name
);
727 hdr
= (struct pe_import_header
*)(pe
->thunk
->data
+ dll_ptr
);
728 hdr
->first_thunk
= thk_ptr
+ rva_base
;
729 hdr
->first_entry
= ent_ptr
+ rva_base
;
730 hdr
->lib_name_offset
= v
+ rva_base
;
732 for (k
= 0, n
= p
->sym_count
; k
<= n
; ++k
) {
734 int iat_index
= p
->symbols
[k
]->iat_index
;
735 int sym_index
= p
->symbols
[k
]->sym_index
;
736 ElfW(Sym
) *imp_sym
= (ElfW(Sym
) *)pe
->s1
->dynsymtab_section
->data
+ sym_index
;
737 ElfW(Sym
) *org_sym
= (ElfW(Sym
) *)symtab_section
->data
+ iat_index
;
738 const char *name
= pe
->s1
->dynsymtab_section
->link
->data
+ imp_sym
->st_name
;
740 org_sym
->st_value
= thk_ptr
;
741 org_sym
->st_shndx
= pe
->thunk
->sh_num
;
742 v
= pe
->thunk
->data_offset
+ rva_base
;
743 section_ptr_add(pe
->thunk
, sizeof(WORD
)); /* hint, not used */
744 put_elf_str(pe
->thunk
, name
);
746 if (pe
->type
== PE_RUN
) {
747 DLLReference
*dllref
= pe
->s1
->loaded_dlls
[imp_sym
->st_value
-1];
748 if ( !dllref
->handle
)
749 dllref
->handle
= LoadLibrary(dllref
->name
);
750 v
= (DWORD
)GetProcAddress(dllref
->handle
, name
);
752 error_noabort("undefined symbol '%s'", name
);
756 v
= 0; /* last entry is zero */
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
);
763 dll_ptr
+= sizeof(struct pe_import_header
);
764 dynarray_reset(&p
->symbols
, &p
->sym_count
);
766 dynarray_reset(&pe
->imp_info
, &pe
->imp_count
);
769 /* ------------------------------------------------------------- */
771 For now only functions are exported. Export of data
772 would work, but import requires compiler support to
773 do an additional indirection.
776 __declspec(dllimport) extern int something;
778 needs to be translated to:
789 ST_FN
int sym_cmp(const void *va
, const void *vb
)
791 const char *ca
= (*(struct pe_sort_sym
**)va
)->name
;
792 const char *cb
= (*(struct pe_sort_sym
**)vb
)->name
;
793 return strcmp(ca
, cb
);
796 ST_FN
void pe_build_exports(struct pe_info
*pe
)
799 int sym_index
, sym_end
;
800 DWORD rva_base
, func_o
, name_o
, ord_o
, str_o
;
801 struct pe_export_header
*hdr
;
803 struct pe_sort_sym
**sorted
, *p
;
810 rva_base
= pe
->thunk
->sh_addr
- pe
->imagebase
;
811 sym_count
= 0, sorted
= NULL
, op
= NULL
;
813 sym_end
= symtab_section
->data_offset
/ sizeof(ElfW(Sym
));
814 for (sym_index
= 1; sym_index
< sym_end
; ++sym_index
) {
815 sym
= (ElfW(Sym
)*)symtab_section
->data
+ sym_index
;
816 name
= symtab_section
->link
->data
+ sym
->st_name
;
817 if ((sym
->st_other
& 1)
818 /* export only symbols from actually written sections */
819 && pe
->s1
->sections
[sym
->st_shndx
]->sh_addr
) {
820 p
= tcc_malloc(sizeof *p
);
821 p
->index
= sym_index
;
823 dynarray_add((void***)&sorted
, &sym_count
, p
);
826 if (sym
->st_other
& 1)
827 printf("export: %s\n", name
);
828 if (sym
->st_other
& 2)
829 printf("stdcall: %s\n", name
);
836 qsort (sorted
, sym_count
, sizeof *sorted
, sym_cmp
);
838 pe_align_section(pe
->thunk
, 16);
839 dllname
= tcc_basename(pe
->filename
);
841 pe
->exp_offs
= pe
->thunk
->data_offset
;
842 func_o
= pe
->exp_offs
+ sizeof(struct pe_export_header
);
843 name_o
= func_o
+ sym_count
* sizeof (DWORD
);
844 ord_o
= name_o
+ sym_count
* sizeof (DWORD
);
845 str_o
= ord_o
+ sym_count
* sizeof(WORD
);
847 hdr
= section_ptr_add(pe
->thunk
, str_o
- pe
->exp_offs
);
848 hdr
->Characteristics
= 0;
850 hdr
->NumberOfFunctions
= sym_count
;
851 hdr
->NumberOfNames
= sym_count
;
852 hdr
->AddressOfFunctions
= func_o
+ rva_base
;
853 hdr
->AddressOfNames
= name_o
+ rva_base
;
854 hdr
->AddressOfNameOrdinals
= ord_o
+ rva_base
;
855 hdr
->Name
= str_o
+ rva_base
;
856 put_elf_str(pe
->thunk
, dllname
);
859 /* automatically write exports to <output-filename>.def */
860 strcpy(buf
, pe
->filename
);
861 strcpy(tcc_fileextension(buf
), ".def");
862 op
= fopen(buf
, "w");
864 error_noabort("could not create '%s': %s", buf
, strerror(errno
));
866 fprintf(op
, "LIBRARY %s\n\nEXPORTS\n", dllname
);
868 printf("<- %s (%d symbols)\n", buf
, sym_count
);
872 for (ord
= 0; ord
< sym_count
; ++ord
)
874 p
= sorted
[ord
], sym_index
= p
->index
, name
= p
->name
;
875 /* insert actual address later in pe_relocate_rva */
876 put_elf_reloc(symtab_section
, pe
->thunk
,
877 func_o
, R_XXX_RELATIVE
, sym_index
);
878 *(DWORD
*)(pe
->thunk
->data
+ name_o
)
879 = pe
->thunk
->data_offset
+ rva_base
;
880 *(WORD
*)(pe
->thunk
->data
+ ord_o
)
882 put_elf_str(pe
->thunk
, name
);
883 func_o
+= sizeof (DWORD
);
884 name_o
+= sizeof (DWORD
);
885 ord_o
+= sizeof (WORD
);
887 fprintf(op
, "%s\n", name
);
889 pe
->exp_size
= pe
->thunk
->data_offset
- pe
->exp_offs
;
890 dynarray_reset(&sorted
, &sym_count
);
893 /* ------------------------------------------------------------- */
894 ST_FN
void pe_build_reloc (struct pe_info
*pe
)
896 DWORD offset
, block_ptr
, addr
;
898 ElfW_Rel
*rel
, *rel_end
;
899 Section
*s
= NULL
, *sr
;
901 offset
= addr
= block_ptr
= count
= i
= 0;
902 rel
= rel_end
= NULL
;
906 int type
= ELFW_R_(TYPE
)(rel
->r_info
);
907 addr
= rel
->r_offset
+ s
->sh_addr
;
909 if (type
!= REL_TYPE_DIRECT
)
911 if (count
== 0) { /* new block */
912 block_ptr
= pe
->reloc
->data_offset
;
913 section_ptr_add(pe
->reloc
, sizeof(struct pe_reloc_header
));
914 offset
= addr
& 0xFFFFFFFF<<12;
916 if ((addr
-= offset
) < (1<<12)) { /* one block spans 4k addresses */
917 WORD
*wp
= section_ptr_add(pe
->reloc
, sizeof (WORD
));
918 *wp
= addr
| IMAGE_REL_BASED_HIGHLOW
<<12;
924 } else if (i
< pe
->sec_count
) {
925 sr
= (s
= pe
->s1
->sections
[pe
->sec_info
[i
++].ord
])->reloc
;
927 rel
= (ElfW_Rel
*)sr
->data
;
928 rel_end
= (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
);
934 /* store the last block and ready for a new one */
935 struct pe_reloc_header
*hdr
;
936 if (count
& 1) /* align for DWORDS */
937 section_ptr_add(pe
->reloc
, sizeof(WORD
)), ++count
;
938 hdr
= (struct pe_reloc_header
*)(pe
->reloc
->data
+ block_ptr
);
939 hdr
-> offset
= offset
- pe
->imagebase
;
940 hdr
-> size
= count
* sizeof(WORD
) + sizeof(struct pe_reloc_header
);
949 /* ------------------------------------------------------------- */
950 ST_FN
int pe_section_class(Section
*s
)
958 if (flags
& SHF_ALLOC
) {
959 if (type
== SHT_PROGBITS
) {
960 if (flags
& SHF_EXECINSTR
)
962 if (flags
& SHF_WRITE
)
964 if (0 == strcmp(name
, ".rsrc"))
966 if (0 == strcmp(name
, ".iedat"))
969 } else if (type
== SHT_NOBITS
) {
970 if (flags
& SHF_WRITE
)
974 if (0 == strcmp(name
, ".reloc"))
976 if (0 == strncmp(name
, ".stab", 5)) /* .stab and .stabstr */
982 ST_FN
int pe_assign_addresses (struct pe_info
*pe
)
987 struct section_info
*si
;
990 // pe->thunk = new_section(pe->s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);
992 section_order
= tcc_malloc(pe
->s1
->nb_sections
* sizeof (int));
993 for (o
= k
= 0 ; k
< sec_last
; ++k
) {
994 for (i
= 1; i
< pe
->s1
->nb_sections
; ++i
) {
995 s
= pe
->s1
->sections
[i
];
996 if (k
== pe_section_class(s
)) {
997 // printf("%s %d\n", s->name, k);
998 s
->sh_addr
= pe
->imagebase
;
999 section_order
[o
++] = i
;
1004 pe
->sec_info
= tcc_mallocz(o
* sizeof (struct section_info
));
1005 addr
= pe
->imagebase
+ 1;
1007 for (i
= 0; i
< o
; ++i
)
1009 k
= section_order
[i
];
1010 s
= pe
->s1
->sections
[k
];
1011 c
= pe_section_class(s
);
1012 si
= &pe
->sec_info
[pe
->sec_count
];
1014 #ifdef PE_MERGE_DATA
1015 if (c
== sec_bss
&& pe
->sec_count
&& si
[-1].cls
== sec_data
) {
1016 /* append .bss to .data */
1017 s
->sh_addr
= addr
= ((addr
-1) | 15) + 1;
1018 addr
+= s
->data_offset
;
1019 si
[-1].sh_size
= addr
- si
[-1].sh_addr
;
1023 strcpy(si
->name
, s
->name
);
1026 si
->sh_addr
= s
->sh_addr
= addr
= pe_virtual_align(addr
);
1027 si
->sh_flags
= s
->sh_flags
;
1029 if (c
== sec_data
&& NULL
== pe
->thunk
)
1032 if (s
== pe
->thunk
) {
1033 pe_build_imports(pe
);
1034 pe_build_exports(pe
);
1038 pe_build_reloc (pe
);
1042 if (s
->sh_type
!= SHT_NOBITS
) {
1044 si
->data_size
= s
->data_offset
;
1047 addr
+= s
->data_offset
;
1048 si
->sh_size
= s
->data_offset
;
1051 // printf("%08x %05x %s\n", si->sh_addr, si->sh_size, si->name);
1055 for (i
= 1; i
< pe
->s1
->nb_sections
; ++i
) {
1056 Section
*s
= pe
->s1
->sections
[i
];
1057 int type
= s
->sh_type
;
1058 int flags
= s
->sh_flags
;
1059 printf("section %-16s %-10s %5x %s,%s,%s\n",
1061 type
== SHT_PROGBITS
? "progbits" :
1062 type
== SHT_NOBITS
? "nobits" :
1063 type
== SHT_SYMTAB
? "symtab" :
1064 type
== SHT_STRTAB
? "strtab" :
1065 type
== SHT_RELX
? "rel" : "???",
1067 flags
& SHF_ALLOC
? "alloc" : "",
1068 flags
& SHF_WRITE
? "write" : "",
1069 flags
& SHF_EXECINSTR
? "exec" : ""
1072 pe
->s1
->verbose
= 2;
1075 tcc_free(section_order
);
1079 /* ------------------------------------------------------------- */
1080 ST_FN
void pe_relocate_rva (struct pe_info
*pe
, Section
*s
)
1082 Section
*sr
= s
->reloc
;
1083 ElfW_Rel
*rel
, *rel_end
;
1084 rel_end
= (ElfW_Rel
*)(sr
->data
+ sr
->data_offset
);
1085 for(rel
= (ElfW_Rel
*)sr
->data
; rel
< rel_end
; rel
++) {
1086 if (ELFW_R_(TYPE
)(rel
->r_info
) == R_XXX_RELATIVE
) {
1087 int sym_index
= ELFW_R_(SYM
)(rel
->r_info
);
1088 DWORD addr
= s
->sh_addr
;
1090 ElfW(Sym
) *sym
= (ElfW(Sym
) *)symtab_section
->data
+ sym_index
;
1091 addr
= sym
->st_value
;
1093 // printf("reloc rva %08x %08x\n", (DWORD)rel->r_offset, addr);
1094 *(DWORD
*)(s
->data
+ rel
->r_offset
) += addr
- pe
->imagebase
;
1099 /*----------------------------------------------------------------------------*/
1100 ST_FN
int pe_check_symbols(struct pe_info
*pe
)
1103 int sym_index
, sym_end
;
1106 pe_align_section(text_section
, 8);
1108 sym_end
= symtab_section
->data_offset
/ sizeof(ElfW(Sym
));
1109 for (sym_index
= 1; sym_index
< sym_end
; ++sym_index
) {
1111 sym
= (ElfW(Sym
) *)symtab_section
->data
+ sym_index
;
1112 if (sym
->st_shndx
== SHN_UNDEF
) {
1114 const char *name
= symtab_section
->link
->data
+ sym
->st_name
;
1115 unsigned type
= ELFW_ST_TYPE(sym
->st_info
);
1116 int imp_sym
= pe_find_import(pe
->s1
, name
);
1117 struct import_symbol
*is
;
1121 is
= pe_add_import(pe
, imp_sym
);
1125 if (type
== STT_FUNC
) {
1126 unsigned long offset
= is
->thk_offset
;
1128 /* got aliased symbol, like stricmp and _stricmp */
1133 offset
= text_section
->data_offset
;
1134 /* add the 'jmp IAT[x]' instruction */
1135 *(WORD
*)section_ptr_add(text_section
, 8) = 0x25FF;
1137 /* add a helper symbol, will be patched later in
1139 sprintf(buffer
, "IAT.%s", name
);
1140 is
->iat_index
= put_elf_sym(
1141 symtab_section
, 0, sizeof(DWORD
),
1142 ELFW_ST_INFO(STB_GLOBAL
, STT_OBJECT
),
1143 0, SHN_UNDEF
, buffer
);
1144 put_elf_reloc(symtab_section
, text_section
,
1145 offset
+ 2, R_XXX_THUNKFIX
, is
->iat_index
);
1146 is
->thk_offset
= offset
;
1149 /* tcc_realloc might have altered sym's address */
1150 sym
= (ElfW(Sym
) *)symtab_section
->data
+ sym_index
;
1152 /* patch the original symbol */
1153 sym
->st_value
= offset
;
1154 sym
->st_shndx
= text_section
->sh_num
;
1155 sym
->st_other
&= ~1; /* do not export */
1159 if (type
== STT_OBJECT
) { /* data, ptr to that should be */
1160 if (0 == is
->iat_index
) {
1161 /* original symbol will be patched later in pe_build_imports */
1162 is
->iat_index
= sym_index
;
1168 error_noabort("undefined symbol '%s'", name
);
1171 } else if (pe
->s1
->rdynamic
1172 && ELFW_ST_BIND(sym
->st_info
) != STB_LOCAL
) {
1173 /* if -rdynamic option, then export all non local symbols */
1180 /*----------------------------------------------------------------------------*/
1181 #ifdef PE_PRINT_SECTIONS
1182 ST_FN
void pe_print_section(FILE * f
, Section
* s
)
1184 /* just if you'r curious */
1188 e
= s
->data
+ s
->data_offset
;
1191 fprintf(f
, "section \"%s\"", s
->name
);
1193 fprintf(f
, "\nlink \"%s\"", s
->link
->name
);
1195 fprintf(f
, "\nreloc \"%s\"", s
->reloc
->name
);
1196 fprintf(f
, "\nv_addr %08X", (unsigned)s
->sh_addr
);
1197 fprintf(f
, "\ncontents %08X", (unsigned)l
);
1200 if (s
->sh_type
== SHT_NOBITS
)
1206 if (s
->sh_type
== SHT_SYMTAB
)
1207 m
= sizeof(ElfW(Sym
));
1208 else if (s
->sh_type
== SHT_RELX
)
1209 m
= sizeof(ElfW_Rel
);
1213 fprintf(f
, "%-8s", "offset");
1214 for (i
= 0; i
< m
; ++i
)
1215 fprintf(f
, " %02x", i
);
1218 if (s
->sh_type
== SHT_SYMTAB
|| s
->sh_type
== SHT_RELX
) {
1219 const char *fields1
[] = {
1230 const char *fields2
[] = {
1239 if (s
->sh_type
== SHT_SYMTAB
)
1240 p
= fields1
, n
= 106;
1242 p
= fields2
, n
= 58;
1244 for (i
= 0; p
[i
]; ++i
)
1245 fprintf(f
, "%6s", p
[i
]);
1246 fprintf(f
, " symbol");
1250 for (i
= 0; i
< n
; ++i
)
1256 fprintf(f
, "%08X", i
);
1257 for (n
= 0; n
< m
; ++n
) {
1259 fprintf(f
, " %02X", p
[i
+ n
]);
1264 if (s
->sh_type
== SHT_SYMTAB
) {
1265 ElfW(Sym
) *sym
= (ElfW(Sym
) *) (p
+ i
);
1266 const char *name
= s
->link
->data
+ sym
->st_name
;
1267 fprintf(f
, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",
1268 (unsigned)sym
->st_name
,
1269 (unsigned)sym
->st_value
,
1270 (unsigned)sym
->st_size
,
1271 (unsigned)ELFW_ST_BIND(sym
->st_info
),
1272 (unsigned)ELFW_ST_TYPE(sym
->st_info
),
1273 (unsigned)sym
->st_other
,
1274 (unsigned)sym
->st_shndx
,
1277 } else if (s
->sh_type
== SHT_RELX
) {
1278 ElfW_Rel
*rel
= (ElfW_Rel
*) (p
+ i
);
1280 (ElfW(Sym
) *) s
->link
->data
+ ELFW_R_(SYM
)(rel
->r_info
);
1281 const char *name
= s
->link
->link
->data
+ sym
->st_name
;
1282 fprintf(f
, " %04X %02X %04X \"%s\"",
1283 (unsigned)rel
->r_offset
,
1284 (unsigned)ELFW_R_(TYPE
)(rel
->r_info
),
1285 (unsigned)ELFW_R_(SYM
)(rel
->r_info
),
1289 for (n
= 0; n
< m
; ++n
) {
1292 if (b
< 32 || b
>= 127)
1294 fprintf(f
, "%c", b
);
1304 ST_FN
void pe_print_sections(TCCState
*s1
, const char *fname
)
1309 f
= fopen(fname
, "wt");
1310 for (i
= 1; i
< s1
->nb_sections
; ++i
) {
1311 s
= s1
->sections
[i
];
1312 pe_print_section(f
, s
);
1314 pe_print_section(f
, s1
->dynsymtab_section
);
1319 /* -------------------------------------------------------------
1320 * This is for compiled windows resources in 'coff' format
1321 * as generated by 'windres.exe -O coff ...'.
1324 PUB_FN
int pe_test_res_file(void *v
, int size
)
1326 struct pe_rsrc_header
*p
= (struct pe_rsrc_header
*)v
;
1328 size
>= IMAGE_SIZEOF_FILE_HEADER
+ IMAGE_SIZEOF_SHORT_NAME
/* = 28 */
1329 && p
->filehdr
.Machine
== 0x014C
1330 && 1 == p
->filehdr
.NumberOfSections
1331 && 0 == strcmp(p
->sectionhdr
.Name
, ".rsrc")
1335 ST_FN
int read_n(int fd
, void *ptr
, unsigned size
)
1337 return size
== read(fd
, ptr
, size
);
1340 PUB_FN
int pe_load_res_file(TCCState
*s1
, int fd
)
1342 struct pe_rsrc_header hdr
;
1343 Section
*rsrc_section
;
1347 lseek (fd
, 0, SEEK_SET
);
1348 if (!read_n(fd
, &hdr
, sizeof hdr
))
1350 if (!pe_test_res_file(&hdr
, sizeof hdr
))
1353 rsrc_section
= new_section(s1
, ".rsrc", SHT_PROGBITS
, SHF_ALLOC
);
1354 ptr
= section_ptr_add(rsrc_section
, hdr
.sectionhdr
.SizeOfRawData
);
1355 lseek (fd
, hdr
.sectionhdr
.PointerToRawData
, SEEK_SET
);
1356 if (!read_n(fd
, ptr
, hdr
.sectionhdr
.SizeOfRawData
))
1359 lseek (fd
, hdr
.sectionhdr
.PointerToRelocations
, SEEK_SET
);
1360 for (i
= 0; i
< hdr
.sectionhdr
.NumberOfRelocations
; ++i
)
1362 struct pe_rsrc_reloc rel
;
1363 if (!read_n(fd
, &rel
, sizeof rel
))
1365 // printf("rsrc_reloc: %x %x %x\n", rel.offset, rel.size, rel.type);
1366 if (rel
.type
!= 7) /* DIR32NB */
1368 put_elf_reloc(symtab_section
, rsrc_section
,
1369 rel
.offset
, R_XXX_RELATIVE
, 0);
1374 error_noabort("unrecognized resource file format");
1378 /* ------------------------------------------------------------- */
1379 ST_FN
char *trimfront(char *p
)
1381 while (*p
&& (unsigned char)*p
<= ' ')
1386 ST_FN
char *trimback(char *a
, char *e
)
1388 while (e
> a
&& (unsigned char)e
[-1] <= ' ')
1394 ST_FN
char *get_line(char *line
, int size
, FILE *fp
)
1396 if (NULL
== fgets(line
, size
, fp
))
1398 trimback(line
, strchr(line
, 0));
1399 return trimfront(line
);
1402 /* ------------------------------------------------------------- */
1403 PUB_FN
int pe_load_def_file(TCCState
*s1
, int fd
)
1405 DLLReference
*dllref
;
1406 int state
= 0, ret
= -1;
1407 char line
[400], dllname
[80], *p
;
1408 FILE *fp
= fdopen(dup(fd
), "rb");
1414 p
= get_line(line
, sizeof line
, fp
);
1417 if (0 == *p
|| ';' == *p
)
1421 if (0 != strnicmp(p
, "LIBRARY", 7))
1423 strcpy(dllname
, trimfront(p
+7));
1428 if (0 != stricmp(p
, "EXPORTS"))
1434 dllref
= tcc_mallocz(sizeof(DLLReference
) + strlen(dllname
));
1435 strcpy(dllref
->name
, dllname
);
1436 dynarray_add((void ***) &s1
->loaded_dlls
, &s1
->nb_loaded_dlls
, dllref
);
1440 add_elf_sym(s1
->dynsymtab_section
,
1441 s1
->nb_loaded_dlls
, 0,
1442 ELFW_ST_INFO(STB_GLOBAL
, STT_FUNC
), 0,
1443 text_section
->sh_num
, p
);
1452 error_noabort("unrecognized export definition file format");
1456 /* ------------------------------------------------------------- */
1458 ST_FN
void pe_add_runtime_ex(TCCState
*s1
, struct pe_info
*pe
)
1460 const char *start_symbol
;
1461 unsigned long addr
= 0;
1464 if (find_elf_sym(symtab_section
, "_WinMain@16"))
1467 if (TCC_OUTPUT_DLL
== s1
->output_type
) {
1469 /* need this for 'tccelf.c:relocate_section()' */
1470 s1
->output_type
= TCC_OUTPUT_EXE
;
1474 TCC_OUTPUT_MEMORY
== s1
->output_type
1475 ? PE_GUI
== pe_type
? "_runwinmain" : NULL
1476 : PE_DLL
== pe_type
? "__dllstart@12"
1477 : PE_GUI
== pe_type
? "_winstart" : "_start"
1480 /* grab the startup code from libtcc1 */
1482 add_elf_sym(symtab_section
,
1484 ELFW_ST_INFO(STB_GLOBAL
, STT_NOTYPE
), 0,
1485 SHN_UNDEF
, start_symbol
);
1487 if (0 == s1
->nostdlib
) {
1488 tcc_add_library(s1
, "tcc1");
1490 tcc_add_library(s1
, "cygwin1");
1492 tcc_add_library(s1
, "msvcrt");
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");
1503 addr
= (unsigned long)tcc_get_symbol_err(s1
, start_symbol
);
1504 if (s1
->output_type
== TCC_OUTPUT_MEMORY
&& addr
)
1505 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1506 add_elf_sym(symtab_section
,
1508 ELFW_ST_INFO(STB_GLOBAL
, STT_NOTYPE
), 0,
1509 text_section
->sh_num
, "main");
1511 pe
->start_addr
= addr
;
1514 PUB_FN
int pe_output_file(TCCState
* s1
, const char *filename
)
1520 memset(&pe
, 0, sizeof pe
);
1521 pe
.filename
= filename
;
1524 pe_add_runtime_ex(s1
, &pe
);
1525 relocate_common_syms(); /* assign bss adresses */
1526 tcc_add_linker_symbols(s1
);
1528 ret
= pe_check_symbols(&pe
);
1533 if (PE_DLL
== pe
.type
) {
1534 pe
.reloc
= new_section(pe
.s1
, ".reloc", SHT_PROGBITS
, 0);
1535 pe
.imagebase
= 0x10000000;
1537 pe
.imagebase
= 0x00400000;
1539 pe_assign_addresses(&pe
);
1540 relocate_syms(s1
, 0);
1541 for (i
= 1; i
< s1
->nb_sections
; ++i
) {
1542 Section
*s
= s1
->sections
[i
];
1544 relocate_section(s1
, s
);
1545 pe_relocate_rva(&pe
, s
);
1551 ret
= pe_write(&pe
);
1552 tcc_free(pe
.sec_info
);
1554 #ifndef TCC_IS_NATIVE
1555 error_noabort("-run supported only on native platform");
1558 pe
.thunk
= data_section
;
1559 pe_build_imports(&pe
);
1562 #ifdef PE_PRINT_SECTIONS
1563 pe_print_sections(s1
, "tcc.log");
1568 /* ------------------------------------------------------------- */
1569 #endif /* def TCC_TARGET_PE */
1570 /* ------------------------------------------------------------- */