VLA code: minor fix
[tinycc.git] / tccpe.c
Commit [+]AuthorDateLineData
82bcbd02 grischka2013-02-04 16:08:06 +01001/*
5342b32e grischka2007-12-19 17:36:42 +00002 * TCCPE.C - PE file output for the Tiny C Compiler
3 *
4 * Copyright (c) 2005-2007 grischka
2c538d7a bellard2005-04-10 21:46:58 +00005 *
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.
10 *
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.
15 *
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
19 */
20
88a3ccab grischka2009-12-20 01:53:49 +010021#include "tcc.h"
2c538d7a bellard2005-04-10 21:46:58 +000022
5342b32e grischka2007-12-19 17:36:42 +000023#ifndef _WIN32
24#define stricmp strcasecmp
25#define strnicmp strncasecmp
26#endif
27
28#ifndef MAX_PATH
29#define MAX_PATH 260
30#endif
2c538d7a bellard2005-04-10 21:46:58 +000031
5342b32e grischka2007-12-19 17:36:42 +000032#define PE_MERGE_DATA
0bdbd49e Urs Janssen2013-02-17 00:48:51 +010033/* #define PE_PRINT_SECTIONS */
2c538d7a bellard2005-04-10 21:46:58 +000034
f366cb20 grischka2009-07-06 21:35:25 +020035#ifdef TCC_TARGET_X86_64
36# define ADDR3264 ULONGLONG
12f43953 grischka2014-08-01 10:51:28 +020037# define REL_TYPE_DIRECT R_X86_64_64
38# define R_XXX_THUNKFIX R_X86_64_PC32
39# define R_XXX_RELATIVE R_X86_64_RELATIVE
40# define IMAGE_FILE_MACHINE 0x8664
41# define RSRC_RELTYPE 3
42
43#elif defined TCC_TARGET_ARM
44# define ADDR3264 DWORD
45# define REL_TYPE_DIRECT R_ARM_ABS32
46# define R_XXX_THUNKFIX R_ARM_ABS32
47# define R_XXX_RELATIVE R_ARM_RELATIVE
48# define IMAGE_FILE_MACHINE 0x01C0
49# define RSRC_RELTYPE 7 /* ??? (not tested) */
50
51#elif defined TCC_TARGET_I386
f366cb20 grischka2009-07-06 21:35:25 +020052# define ADDR3264 DWORD
12f43953 grischka2014-08-01 10:51:28 +020053# 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 */
58
f366cb20 grischka2009-07-06 21:35:25 +020059#endif
60
1df662c1 grischka2009-07-18 22:07:25 +020061#ifdef _WIN32
62void dbg_printf (const char *fmt, ...)
63{
64 char buffer[4000];
65 va_list arg;
66 int x;
67 va_start(arg, fmt);
68 x = vsprintf (buffer, fmt, arg);
69 strcpy(buffer+x, "\n");
70 OutputDebugString(buffer);
71}
72#endif
73
5342b32e grischka2007-12-19 17:36:42 +000074/* ----------------------------------------------------------- */
75#ifndef IMAGE_NT_SIGNATURE
76/* ----------------------------------------------------------- */
77/* definitions below are from winnt.h */
78
79typedef unsigned char BYTE;
80typedef unsigned short WORD;
f332bb3b grischka2010-11-27 15:02:57 +010081typedef unsigned int DWORD;
f366cb20 grischka2009-07-06 21:35:25 +020082typedef unsigned long long ULONGLONG;
5342b32e grischka2007-12-19 17:36:42 +000083#pragma pack(push, 1)
84
85typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */
86 WORD e_magic; /* Magic number */
87 WORD e_cblp; /* Bytes on last page of file */
88 WORD e_cp; /* Pages in file */
89 WORD e_crlc; /* Relocations */
90 WORD e_cparhdr; /* Size of header in paragraphs */
91 WORD e_minalloc; /* Minimum extra paragraphs needed */
92 WORD e_maxalloc; /* Maximum extra paragraphs needed */
93 WORD e_ss; /* Initial (relative) SS value */
94 WORD e_sp; /* Initial SP value */
95 WORD e_csum; /* Checksum */
96 WORD e_ip; /* Initial IP value */
97 WORD e_cs; /* Initial (relative) CS value */
98 WORD e_lfarlc; /* File address of relocation table */
99 WORD e_ovno; /* Overlay number */
100 WORD e_res[4]; /* Reserved words */
101 WORD e_oemid; /* OEM identifier (for e_oeminfo) */
102 WORD e_oeminfo; /* OEM information; e_oemid specific */
103 WORD e_res2[10]; /* Reserved words */
104 DWORD e_lfanew; /* File address of new exe header */
2c538d7a bellard2005-04-10 21:46:58 +0000105} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
106
34140dd6 grischka2007-12-13 19:07:19 +0000107#define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
2c538d7a bellard2005-04-10 21:46:58 +0000108#define SIZE_OF_NT_SIGNATURE 4
109
110typedef struct _IMAGE_FILE_HEADER {
5342b32e grischka2007-12-19 17:36:42 +0000111 WORD Machine;
112 WORD NumberOfSections;
113 DWORD TimeDateStamp;
114 DWORD PointerToSymbolTable;
115 DWORD NumberOfSymbols;
116 WORD SizeOfOptionalHeader;
117 WORD Characteristics;
2c538d7a bellard2005-04-10 21:46:58 +0000118} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
119
120
121#define IMAGE_SIZEOF_FILE_HEADER 20
122
123typedef struct _IMAGE_DATA_DIRECTORY {
5342b32e grischka2007-12-19 17:36:42 +0000124 DWORD VirtualAddress;
125 DWORD Size;
2c538d7a bellard2005-04-10 21:46:58 +0000126} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
127
128
129typedef struct _IMAGE_OPTIONAL_HEADER {
130 /* Standard fields. */
5342b32e grischka2007-12-19 17:36:42 +0000131 WORD Magic;
132 BYTE MajorLinkerVersion;
133 BYTE MinorLinkerVersion;
134 DWORD SizeOfCode;
135 DWORD SizeOfInitializedData;
136 DWORD SizeOfUninitializedData;
137 DWORD AddressOfEntryPoint;
138 DWORD BaseOfCode;
f366cb20 grischka2009-07-06 21:35:25 +0200139#ifndef TCC_TARGET_X86_64
5342b32e grischka2007-12-19 17:36:42 +0000140 DWORD BaseOfData;
f366cb20 grischka2009-07-06 21:35:25 +0200141#endif
2c538d7a bellard2005-04-10 21:46:58 +0000142 /* NT additional fields. */
f366cb20 grischka2009-07-06 21:35:25 +0200143 ADDR3264 ImageBase;
5342b32e grischka2007-12-19 17:36:42 +0000144 DWORD SectionAlignment;
145 DWORD FileAlignment;
146 WORD MajorOperatingSystemVersion;
147 WORD MinorOperatingSystemVersion;
148 WORD MajorImageVersion;
149 WORD MinorImageVersion;
150 WORD MajorSubsystemVersion;
151 WORD MinorSubsystemVersion;
152 DWORD Win32VersionValue;
153 DWORD SizeOfImage;
154 DWORD SizeOfHeaders;
155 DWORD CheckSum;
156 WORD Subsystem;
157 WORD DllCharacteristics;
f366cb20 grischka2009-07-06 21:35:25 +0200158 ADDR3264 SizeOfStackReserve;
159 ADDR3264 SizeOfStackCommit;
160 ADDR3264 SizeOfHeapReserve;
161 ADDR3264 SizeOfHeapCommit;
5342b32e grischka2007-12-19 17:36:42 +0000162 DWORD LoaderFlags;
163 DWORD NumberOfRvaAndSizes;
2c538d7a bellard2005-04-10 21:46:58 +0000164 IMAGE_DATA_DIRECTORY DataDirectory[16];
f366cb20 grischka2009-07-06 21:35:25 +0200165} IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, IMAGE_OPTIONAL_HEADER;
2c538d7a bellard2005-04-10 21:46:58 +0000166
5342b32e grischka2007-12-19 17:36:42 +0000167#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
168#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
169#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */
170#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */
171#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */
172#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */
173#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */
2c538d7a bellard2005-04-10 21:46:58 +0000174/* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */
5342b32e grischka2007-12-19 17:36:42 +0000175#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */
176#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */
177#define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */
178#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */
179#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */
180#define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
181#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */
182#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */
2c538d7a bellard2005-04-10 21:46:58 +0000183
184/* Section header format. */
5342b32e grischka2007-12-19 17:36:42 +0000185#define IMAGE_SIZEOF_SHORT_NAME 8
2c538d7a bellard2005-04-10 21:46:58 +0000186
187typedef struct _IMAGE_SECTION_HEADER {
5342b32e grischka2007-12-19 17:36:42 +0000188 BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
2c538d7a bellard2005-04-10 21:46:58 +0000189 union {
5342b32e grischka2007-12-19 17:36:42 +0000190 DWORD PhysicalAddress;
191 DWORD VirtualSize;
2c538d7a bellard2005-04-10 21:46:58 +0000192 } Misc;
5342b32e grischka2007-12-19 17:36:42 +0000193 DWORD VirtualAddress;
194 DWORD SizeOfRawData;
195 DWORD PointerToRawData;
196 DWORD PointerToRelocations;
197 DWORD PointerToLinenumbers;
198 WORD NumberOfRelocations;
199 WORD NumberOfLinenumbers;
200 DWORD Characteristics;
2c538d7a bellard2005-04-10 21:46:58 +0000201} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
202
5342b32e grischka2007-12-19 17:36:42 +0000203#define IMAGE_SIZEOF_SECTION_HEADER 40
2c538d7a bellard2005-04-10 21:46:58 +0000204
719ba918 grischka2009-07-06 21:35:35 +0200205typedef struct _IMAGE_EXPORT_DIRECTORY {
206 DWORD Characteristics;
207 DWORD TimeDateStamp;
208 WORD MajorVersion;
209 WORD MinorVersion;
210 DWORD Name;
211 DWORD Base;
212 DWORD NumberOfFunctions;
213 DWORD NumberOfNames;
214 DWORD AddressOfFunctions;
215 DWORD AddressOfNames;
216 DWORD AddressOfNameOrdinals;
217} IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY;
218
219typedef struct _IMAGE_IMPORT_DESCRIPTOR {
220 union {
fc977d56 grischka2009-07-18 22:06:54 +0200221 DWORD Characteristics;
222 DWORD OriginalFirstThunk;
719ba918 grischka2009-07-06 21:35:35 +0200223 };
224 DWORD TimeDateStamp;
225 DWORD ForwarderChain;
226 DWORD Name;
227 DWORD FirstThunk;
228} IMAGE_IMPORT_DESCRIPTOR;
229
2c538d7a bellard2005-04-10 21:46:58 +0000230typedef struct _IMAGE_BASE_RELOCATION {
5342b32e grischka2007-12-19 17:36:42 +0000231 DWORD VirtualAddress;
232 DWORD SizeOfBlock;
2c538d7a bellard2005-04-10 21:46:58 +0000233// WORD TypeOffset[1];
234} IMAGE_BASE_RELOCATION;
235
5342b32e grischka2007-12-19 17:36:42 +0000236#define IMAGE_SIZEOF_BASE_RELOCATION 8
2c538d7a bellard2005-04-10 21:46:58 +0000237
5342b32e grischka2007-12-19 17:36:42 +0000238#define IMAGE_REL_BASED_ABSOLUTE 0
239#define IMAGE_REL_BASED_HIGH 1
240#define IMAGE_REL_BASED_LOW 2
241#define IMAGE_REL_BASED_HIGHLOW 3
242#define IMAGE_REL_BASED_HIGHADJ 4
243#define IMAGE_REL_BASED_MIPS_JMPADDR 5
244#define IMAGE_REL_BASED_SECTION 6
245#define IMAGE_REL_BASED_REL32 7
2c538d7a bellard2005-04-10 21:46:58 +0000246
5342b32e grischka2007-12-19 17:36:42 +0000247#pragma pack(pop)
2c538d7a bellard2005-04-10 21:46:58 +0000248
249/* ----------------------------------------------------------- */
5342b32e grischka2007-12-19 17:36:42 +0000250#endif /* ndef IMAGE_NT_SIGNATURE */
251/* ----------------------------------------------------------- */
5342b32e grischka2007-12-19 17:36:42 +0000252#pragma pack(push, 1)
2c538d7a bellard2005-04-10 21:46:58 +0000253
5342b32e grischka2007-12-19 17:36:42 +0000254struct pe_header
255{
256 IMAGE_DOS_HEADER doshdr;
257 BYTE dosstub[0x40];
258 DWORD nt_sig;
259 IMAGE_FILE_HEADER filehdr;
f366cb20 grischka2009-07-06 21:35:25 +0200260#ifdef TCC_TARGET_X86_64
261 IMAGE_OPTIONAL_HEADER64 opthdr;
262#else
263#ifdef _WIN64
264 IMAGE_OPTIONAL_HEADER32 opthdr;
265#else
5342b32e grischka2007-12-19 17:36:42 +0000266 IMAGE_OPTIONAL_HEADER opthdr;
f366cb20 grischka2009-07-06 21:35:25 +0200267#endif
268#endif
2c538d7a bellard2005-04-10 21:46:58 +0000269};
270
2c538d7a bellard2005-04-10 21:46:58 +0000271struct pe_reloc_header {
272 DWORD offset;
273 DWORD size;
274};
275
5342b32e grischka2007-12-19 17:36:42 +0000276struct pe_rsrc_header {
277 struct _IMAGE_FILE_HEADER filehdr;
278 struct _IMAGE_SECTION_HEADER sectionhdr;
279};
280
281struct pe_rsrc_reloc {
282 DWORD offset;
283 DWORD size;
284 WORD type;
285};
286
287#pragma pack(pop)
288
2c538d7a bellard2005-04-10 21:46:58 +0000289/* ------------------------------------------------------------- */
290/* internal temporary structures */
291
5342b32e grischka2007-12-19 17:36:42 +0000292/*
293#define IMAGE_SCN_CNT_CODE 0x00000020
294#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
295#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
296#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
297#define IMAGE_SCN_MEM_SHARED 0x10000000
298#define IMAGE_SCN_MEM_EXECUTE 0x20000000
299#define IMAGE_SCN_MEM_READ 0x40000000
300#define IMAGE_SCN_MEM_WRITE 0x80000000
301*/
2c538d7a bellard2005-04-10 21:46:58 +0000302
303enum {
304 sec_text = 0,
5342b32e grischka2007-12-19 17:36:42 +0000305 sec_data ,
306 sec_bss ,
307 sec_idata ,
50b040ef grischka2009-12-19 22:40:28 +0100308 sec_pdata ,
4a8c2229 grischka2009-02-16 15:13:29 +0100309 sec_other ,
5342b32e grischka2007-12-19 17:36:42 +0000310 sec_rsrc ,
311 sec_stab ,
312 sec_reloc ,
5342b32e grischka2007-12-19 17:36:42 +0000313 sec_last
2c538d7a bellard2005-04-10 21:46:58 +0000314};
315
88a3ccab grischka2009-12-20 01:53:49 +0100316static const DWORD pe_sec_flags[] = {
5342b32e grischka2007-12-19 17:36:42 +0000317 0x60000020, /* ".text" , */
318 0xC0000040, /* ".data" , */
319 0xC0000080, /* ".bss" , */
320 0x40000040, /* ".idata" , */
50b040ef grischka2009-12-19 22:40:28 +0100321 0x40000040, /* ".pdata" , */
4a8c2229 grischka2009-02-16 15:13:29 +0100322 0xE0000060, /* < other > , */
5342b32e grischka2007-12-19 17:36:42 +0000323 0x40000040, /* ".rsrc" , */
324 0x42000802, /* ".stab" , */
325 0x42000040, /* ".reloc" , */
2c538d7a bellard2005-04-10 21:46:58 +0000326};
327
328struct section_info {
5342b32e grischka2007-12-19 17:36:42 +0000329 int cls, ord;
330 char name[32];
2c538d7a bellard2005-04-10 21:46:58 +0000331 DWORD sh_addr;
332 DWORD sh_size;
5342b32e grischka2007-12-19 17:36:42 +0000333 DWORD sh_flags;
2c538d7a bellard2005-04-10 21:46:58 +0000334 unsigned char *data;
335 DWORD data_size;
5342b32e grischka2007-12-19 17:36:42 +0000336 IMAGE_SECTION_HEADER ish;
2c538d7a bellard2005-04-10 21:46:58 +0000337};
338
339struct import_symbol {
340 int sym_index;
5342b32e grischka2007-12-19 17:36:42 +0000341 int iat_index;
342 int thk_offset;
2c538d7a bellard2005-04-10 21:46:58 +0000343};
344
345struct pe_import_info {
346 int dll_index;
347 int sym_count;
348 struct import_symbol **symbols;
349};
350
351struct pe_info {
5342b32e grischka2007-12-19 17:36:42 +0000352 TCCState *s1;
353 Section *reloc;
354 Section *thunk;
2c538d7a bellard2005-04-10 21:46:58 +0000355 const char *filename;
5342b32e grischka2007-12-19 17:36:42 +0000356 int type;
2c538d7a bellard2005-04-10 21:46:58 +0000357 DWORD sizeofheaders;
a35b3059 grischka2012-03-05 20:15:56 +0100358 ADDR3264 imagebase;
2c538d7a bellard2005-04-10 21:46:58 +0000359 DWORD start_addr;
360 DWORD imp_offs;
361 DWORD imp_size;
362 DWORD iat_offs;
363 DWORD iat_size;
364 DWORD exp_offs;
365 DWORD exp_size;
2f73e42d
FF
Frederic Feret2009-08-27 10:12:13 +0200366 int subsystem;
367 DWORD section_align;
368 DWORD file_align;
5342b32e grischka2007-12-19 17:36:42 +0000369 struct section_info *sec_info;
2c538d7a bellard2005-04-10 21:46:58 +0000370 int sec_count;
371 struct pe_import_info **imp_info;
372 int imp_count;
2c538d7a bellard2005-04-10 21:46:58 +0000373};
374
2c538d7a bellard2005-04-10 21:46:58 +0000375#define PE_NUL 0
376#define PE_DLL 1
377#define PE_GUI 2
378#define PE_EXE 3
370cdeb6 grischka2009-07-06 21:34:22 +0200379#define PE_RUN 4
2c538d7a bellard2005-04-10 21:46:58 +0000380
5342b32e grischka2007-12-19 17:36:42 +0000381/* --------------------------------------------*/
382
a3ebdd0a grischka2011-02-13 17:41:05 +0100383static const char *pe_export_name(TCCState *s1, ElfW(Sym) *sym)
5342b32e grischka2007-12-19 17:36:42 +0000384{
a3ebdd0a grischka2011-02-13 17:41:05 +0100385 const char *name = symtab_section->link->data + sym->st_name;
2ac238fc grischka2014-04-17 17:01:28 +0200386 if (s1->leading_underscore && name[0] == '_' && !(sym->st_other & ST_PE_STDCALL))
a3ebdd0a grischka2011-02-13 17:41:05 +0100387 return name + 1;
388 return name;
5342b32e grischka2007-12-19 17:36:42 +0000389}
390
a3ebdd0a grischka2011-02-13 17:41:05 +0100391static int pe_find_import(TCCState * s1, ElfW(Sym) *sym)
5342b32e grischka2007-12-19 17:36:42 +0000392{
393 char buffer[200];
a3ebdd0a grischka2011-02-13 17:41:05 +0100394 const char *s, *p;
5280293d grischka2011-07-14 18:45:37 +0200395 int sym_index = 0, n = 0;
a3ebdd0a grischka2011-02-13 17:41:05 +0100396
5342b32e grischka2007-12-19 17:36:42 +0000397 do {
a3ebdd0a grischka2011-02-13 17:41:05 +0100398 s = pe_export_name(s1, sym);
399 if (n) {
400 /* second try: */
2ac238fc grischka2014-04-17 17:01:28 +0200401 if (sym->st_other & ST_PE_STDCALL) {
a3ebdd0a grischka2011-02-13 17:41:05 +0100402 /* try w/0 stdcall deco (windows API convention) */
403 p = strrchr(s, '@');
404 if (!p || s[0] != '_')
405 break;
406 strcpy(buffer, s+1)[p-s-1] = 0;
407 } else if (s[0] != '_') { /* try non-ansi function */
408 buffer[0] = '_', strcpy(buffer + 1, s);
409 } else if (0 == memcmp(s, "__imp__", 7)) { /* mingw 2.0 */
410 strcpy(buffer, s + 6);
411 } else if (0 == memcmp(s, "_imp___", 7)) { /* mingw 3.7 */
412 strcpy(buffer, s + 6);
413 } else {
414 break;
415 }
416 s = buffer;
417 }
5342b32e grischka2007-12-19 17:36:42 +0000418 sym_index = find_elf_sym(s1->dynsymtab_section, s);
370cdeb6 grischka2009-07-06 21:34:22 +0200419 // printf("find (%d) %d %s\n", n, sym_index, s);
5342b32e grischka2007-12-19 17:36:42 +0000420 } while (0 == sym_index && ++n < 2);
2c538d7a bellard2005-04-10 21:46:58 +0000421 return sym_index;
422}
423
2c538d7a bellard2005-04-10 21:46:58 +0000424/*----------------------------------------------------------------------------*/
2c538d7a bellard2005-04-10 21:46:58 +0000425
88a3ccab grischka2009-12-20 01:53:49 +0100426static int dynarray_assoc(void **pp, int n, int key)
2c538d7a bellard2005-04-10 21:46:58 +0000427{
428 int i;
429 for (i = 0; i < n; ++i, ++pp)
5342b32e grischka2007-12-19 17:36:42 +0000430 if (key == **(int **) pp)
431 return i;
2c538d7a bellard2005-04-10 21:46:58 +0000432 return -1;
433}
434
435#if 0
5342b32e grischka2007-12-19 17:36:42 +0000436ST_FN DWORD umin(DWORD a, DWORD b)
2c538d7a bellard2005-04-10 21:46:58 +0000437{
438 return a < b ? a : b;
439}
440#endif
441
88a3ccab grischka2009-12-20 01:53:49 +0100442static DWORD umax(DWORD a, DWORD b)
2c538d7a bellard2005-04-10 21:46:58 +0000443{
444 return a < b ? b : a;
445}
446
88a3ccab grischka2009-12-20 01:53:49 +0100447static DWORD pe_file_align(struct pe_info *pe, DWORD n)
2c538d7a bellard2005-04-10 21:46:58 +0000448{
2f73e42d Frederic Feret2009-08-27 10:12:13 +0200449 return (n + (pe->file_align - 1)) & ~(pe->file_align - 1);
2c538d7a bellard2005-04-10 21:46:58 +0000450}
451
88a3ccab grischka2009-12-20 01:53:49 +0100452static DWORD pe_virtual_align(struct pe_info *pe, DWORD n)
2c538d7a bellard2005-04-10 21:46:58 +0000453{
2f73e42d Frederic Feret2009-08-27 10:12:13 +0200454 return (n + (pe->section_align - 1)) & ~(pe->section_align - 1);
2c538d7a bellard2005-04-10 21:46:58 +0000455}
456
88a3ccab grischka2009-12-20 01:53:49 +0100457static void pe_align_section(Section *s, int a)
2c538d7a bellard2005-04-10 21:46:58 +0000458{
5342b32e grischka2007-12-19 17:36:42 +0000459 int i = s->data_offset & (a-1);
2c538d7a bellard2005-04-10 21:46:58 +0000460 if (i)
34140dd6 grischka2007-12-13 19:07:19 +0000461 section_ptr_add(s, a - i);
2c538d7a bellard2005-04-10 21:46:58 +0000462}
463
88a3ccab grischka2009-12-20 01:53:49 +0100464static void pe_set_datadir(struct pe_header *hdr, int dir, DWORD addr, DWORD size)
5342b32e grischka2007-12-19 17:36:42 +0000465{
c998985c grischka2009-07-18 22:07:42 +0200466 hdr->opthdr.DataDirectory[dir].VirtualAddress = addr;
467 hdr->opthdr.DataDirectory[dir].Size = size;
5342b32e grischka2007-12-19 17:36:42 +0000468}
2c538d7a bellard2005-04-10 21:46:58 +0000469
88a3ccab grischka2009-12-20 01:53:49 +0100470static int pe_fwrite(void *data, unsigned len, FILE *fp, DWORD *psum)
834b782a grischka2009-07-23 19:19:23 +0200471{
472 if (psum) {
473 DWORD sum = *psum;
474 WORD *p = data;
475 int i;
476 for (i = len; i > 0; i -= 2) {
477 sum += (i >= 2) ? *p++ : *(BYTE*)p;
478 sum = (sum + (sum >> 16)) & 0xFFFF;
479 }
480 *psum = sum;
481 }
482 return len == fwrite(data, 1, len, fp) ? 0 : -1;
483}
484
88a3ccab grischka2009-12-20 01:53:49 +0100485static void pe_fpad(FILE *fp, DWORD new_pos)
834b782a grischka2009-07-23 19:19:23 +0200486{
487 DWORD pos = ftell(fp);
488 while (++pos <= new_pos)
489 fputc(0, fp);
490}
491
2c538d7a bellard2005-04-10 21:46:58 +0000492/*----------------------------------------------------------------------------*/
88a3ccab grischka2009-12-20 01:53:49 +0100493static int pe_write(struct pe_info *pe)
2c538d7a bellard2005-04-10 21:46:58 +0000494{
c998985c grischka2009-07-18 22:07:42 +0200495 static const struct pe_header pe_template = {
496 {
497 /* IMAGE_DOS_HEADER doshdr */
498 0x5A4D, /*WORD e_magic; Magic number */
499 0x0090, /*WORD e_cblp; Bytes on last page of file */
500 0x0003, /*WORD e_cp; Pages in file */
501 0x0000, /*WORD e_crlc; Relocations */
502
503 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */
504 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */
505 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */
506 0x0000, /*WORD e_ss; Initial (relative) SS value */
507
508 0x00B8, /*WORD e_sp; Initial SP value */
509 0x0000, /*WORD e_csum; Checksum */
510 0x0000, /*WORD e_ip; Initial IP value */
511 0x0000, /*WORD e_cs; Initial (relative) CS value */
512 0x0040, /*WORD e_lfarlc; File address of relocation table */
513 0x0000, /*WORD e_ovno; Overlay number */
514 {0,0,0,0}, /*WORD e_res[4]; Reserved words */
515 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */
516 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */
517 {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */
518 0x00000080 /*DWORD e_lfanew; File address of new exe header */
519 },{
520 /* BYTE dosstub[0x40] */
521 /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */
522 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68,
523 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f,
524 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20,
525 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
526 },
527 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */
528 {
529 /* IMAGE_FILE_HEADER filehdr */
12f43953 grischka2014-08-01 10:51:28 +0200530 IMAGE_FILE_MACHINE, /*WORD Machine; */
c998985c grischka2009-07-18 22:07:42 +0200531 0x0003, /*WORD NumberOfSections; */
532 0x00000000, /*DWORD TimeDateStamp; */
533 0x00000000, /*DWORD PointerToSymbolTable; */
534 0x00000000, /*DWORD NumberOfSymbols; */
b7d7af4f Frederic Feret2009-08-27 10:25:28 +0200535#if defined(TCC_TARGET_X86_64)
c998985c grischka2009-07-18 22:07:42 +0200536 0x00F0, /*WORD SizeOfOptionalHeader; */
537 0x022F /*WORD Characteristics; */
538#define CHARACTERISTICS_DLL 0x222E
b7d7af4f Frederic Feret2009-08-27 10:25:28 +0200539#elif defined(TCC_TARGET_I386)
c998985c grischka2009-07-18 22:07:42 +0200540 0x00E0, /*WORD SizeOfOptionalHeader; */
541 0x030F /*WORD Characteristics; */
542#define CHARACTERISTICS_DLL 0x230E
b7d7af4f
FF
Frederic Feret2009-08-27 10:25:28 +0200543#elif defined(TCC_TARGET_ARM)
544 0x00E0, /*WORD SizeOfOptionalHeader; */
545 0x010F, /*WORD Characteristics; */
546#define CHARACTERISTICS_DLL 0x230F
c998985c grischka2009-07-18 22:07:42 +0200547#endif
548},{
549 /* IMAGE_OPTIONAL_HEADER opthdr */
550 /* Standard fields. */
551#ifdef TCC_TARGET_X86_64
552 0x020B, /*WORD Magic; */
553#else
554 0x010B, /*WORD Magic; */
555#endif
556 0x06, /*BYTE MajorLinkerVersion; */
557 0x00, /*BYTE MinorLinkerVersion; */
558 0x00000000, /*DWORD SizeOfCode; */
559 0x00000000, /*DWORD SizeOfInitializedData; */
560 0x00000000, /*DWORD SizeOfUninitializedData; */
561 0x00000000, /*DWORD AddressOfEntryPoint; */
562 0x00000000, /*DWORD BaseOfCode; */
563#ifndef TCC_TARGET_X86_64
564 0x00000000, /*DWORD BaseOfData; */
565#endif
566 /* NT additional fields. */
b7d7af4f
FF
Frederic Feret2009-08-27 10:25:28 +0200567#if defined(TCC_TARGET_ARM)
568 0x00100000, /*DWORD ImageBase; */
569#else
570 0x00400000, /*DWORD ImageBase; */
571#endif
c998985c grischka2009-07-18 22:07:42 +0200572 0x00001000, /*DWORD SectionAlignment; */
573 0x00000200, /*DWORD FileAlignment; */
574 0x0004, /*WORD MajorOperatingSystemVersion; */
575 0x0000, /*WORD MinorOperatingSystemVersion; */
576 0x0000, /*WORD MajorImageVersion; */
577 0x0000, /*WORD MinorImageVersion; */
578 0x0004, /*WORD MajorSubsystemVersion; */
579 0x0000, /*WORD MinorSubsystemVersion; */
580 0x00000000, /*DWORD Win32VersionValue; */
581 0x00000000, /*DWORD SizeOfImage; */
582 0x00000200, /*DWORD SizeOfHeaders; */
583 0x00000000, /*DWORD CheckSum; */
584 0x0002, /*WORD Subsystem; */
585 0x0000, /*WORD DllCharacteristics; */
586 0x00100000, /*DWORD SizeOfStackReserve; */
587 0x00001000, /*DWORD SizeOfStackCommit; */
588 0x00100000, /*DWORD SizeOfHeapReserve; */
589 0x00001000, /*DWORD SizeOfHeapCommit; */
590 0x00000000, /*DWORD LoaderFlags; */
591 0x00000010, /*DWORD NumberOfRvaAndSizes; */
592
593 /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */
594 {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
595 {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}
596 }};
597
834b782a grischka2009-07-23 19:19:23 +0200598 struct pe_header pe_header = pe_template;
599
2c538d7a bellard2005-04-10 21:46:58 +0000600 int i;
601 FILE *op;
834b782a grischka2009-07-23 19:19:23 +0200602 DWORD file_offset, sum;
603 struct section_info *si;
604 IMAGE_SECTION_HEADER *psh;
2c538d7a bellard2005-04-10 21:46:58 +0000605
606 op = fopen(pe->filename, "wb");
607 if (NULL == op) {
bf374a5f grischka2011-08-11 17:07:56 +0200608 tcc_error_noabort("could not write '%s': %s", pe->filename, strerror(errno));
834b782a grischka2009-07-23 19:19:23 +0200609 return -1;
2c538d7a bellard2005-04-10 21:46:58 +0000610 }
611
2f73e42d Frederic Feret2009-08-27 10:12:13 +0200612 pe->sizeofheaders = pe_file_align(pe,
c998985c grischka2009-07-18 22:07:42 +0200613 sizeof (struct pe_header)
5342b32e grischka2007-12-19 17:36:42 +0000614 + pe->sec_count * sizeof (IMAGE_SECTION_HEADER)
34140dd6 grischka2007-12-13 19:07:19 +0000615 );
2c538d7a bellard2005-04-10 21:46:58 +0000616
617 file_offset = pe->sizeofheaders;
2c538d7a bellard2005-04-10 21:46:58 +0000618
f9181416 grischka2009-05-11 18:45:44 +0200619 if (2 == pe->s1->verbose)
34140dd6 grischka2007-12-13 19:07:19 +0000620 printf("-------------------------------"
621 "\n virt file size section" "\n");
2c538d7a bellard2005-04-10 21:46:58 +0000622 for (i = 0; i < pe->sec_count; ++i) {
f332bb3b grischka2010-11-27 15:02:57 +0100623 DWORD addr, size;
834b782a grischka2009-07-23 19:19:23 +0200624 const char *sh_name;
625
626 si = pe->sec_info + i;
627 sh_name = si->name;
628 addr = si->sh_addr - pe->imagebase;
629 size = si->sh_size;
630 psh = &si->ish;
34140dd6 grischka2007-12-13 19:07:19 +0000631
f9181416 grischka2009-05-11 18:45:44 +0200632 if (2 == pe->s1->verbose)
f332bb3b grischka2010-11-27 15:02:57 +0100633 printf("%6x %6x %6x %s\n",
634 (unsigned)addr, (unsigned)file_offset, (unsigned)size, sh_name);
34140dd6 grischka2007-12-13 19:07:19 +0000635
5342b32e grischka2007-12-19 17:36:42 +0000636 switch (si->cls) {
637 case sec_text:
638 pe_header.opthdr.BaseOfCode = addr;
639 pe_header.opthdr.AddressOfEntryPoint = addr + pe->start_addr;
640 break;
641
642 case sec_data:
f366cb20 grischka2009-07-06 21:35:25 +0200643#ifndef TCC_TARGET_X86_64
5342b32e grischka2007-12-19 17:36:42 +0000644 pe_header.opthdr.BaseOfData = addr;
f366cb20 grischka2009-07-06 21:35:25 +0200645#endif
5342b32e grischka2007-12-19 17:36:42 +0000646 break;
647
648 case sec_bss:
649 break;
650
651 case sec_reloc:
c998985c grischka2009-07-18 22:07:42 +0200652 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size);
5342b32e grischka2007-12-19 17:36:42 +0000653 break;
654
655 case sec_rsrc:
c998985c grischka2009-07-18 22:07:42 +0200656 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size);
5342b32e grischka2007-12-19 17:36:42 +0000657 break;
658
50b040ef grischka2009-12-19 22:40:28 +0100659 case sec_pdata:
660 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXCEPTION, addr, size);
661 break;
662
5342b32e grischka2007-12-19 17:36:42 +0000663 case sec_stab:
664 break;
665 }
34140dd6 grischka2007-12-13 19:07:19 +0000666
5342b32e grischka2007-12-19 17:36:42 +0000667 if (pe->thunk == pe->s1->sections[si->ord]) {
34140dd6 grischka2007-12-13 19:07:19 +0000668 if (pe->imp_size) {
c998985c grischka2009-07-18 22:07:42 +0200669 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IMPORT,
5342b32e grischka2007-12-19 17:36:42 +0000670 pe->imp_offs + addr, pe->imp_size);
c998985c grischka2009-07-18 22:07:42 +0200671 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IAT,
5342b32e grischka2007-12-19 17:36:42 +0000672 pe->iat_offs + addr, pe->iat_size);
34140dd6 grischka2007-12-13 19:07:19 +0000673 }
674 if (pe->exp_size) {
c998985c grischka2009-07-18 22:07:42 +0200675 pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXPORT,
5342b32e grischka2007-12-19 17:36:42 +0000676 pe->exp_offs + addr, pe->exp_size);
34140dd6 grischka2007-12-13 19:07:19 +0000677 }
34140dd6 grischka2007-12-13 19:07:19 +0000678 }
679
807dc7c8 Roy2013-06-06 09:26:31 +0800680 strncpy((char*)psh->Name, sh_name, sizeof psh->Name);
34140dd6 grischka2007-12-13 19:07:19 +0000681
5342b32e grischka2007-12-19 17:36:42 +0000682 psh->Characteristics = pe_sec_flags[si->cls];
34140dd6 grischka2007-12-13 19:07:19 +0000683 psh->VirtualAddress = addr;
684 psh->Misc.VirtualSize = size;
5342b32e grischka2007-12-19 17:36:42 +0000685 pe_header.opthdr.SizeOfImage =
2f73e42d Frederic Feret2009-08-27 10:12:13 +0200686 umax(pe_virtual_align(pe, size + addr), pe_header.opthdr.SizeOfImage);
34140dd6 grischka2007-12-13 19:07:19 +0000687
688 if (si->data_size) {
834b782a grischka2009-07-23 19:19:23 +0200689 psh->PointerToRawData = file_offset;
2f73e42d Frederic Feret2009-08-27 10:12:13 +0200690 file_offset = pe_file_align(pe, file_offset + si->data_size);
834b782a grischka2009-07-23 19:19:23 +0200691 psh->SizeOfRawData = file_offset - psh->PointerToRawData;
34140dd6 grischka2007-12-13 19:07:19 +0000692 }
2c538d7a bellard2005-04-10 21:46:58 +0000693 }
694
834b782a grischka2009-07-23 19:19:23 +0200695 //pe_header.filehdr.TimeDateStamp = time(NULL);
5342b32e grischka2007-12-19 17:36:42 +0000696 pe_header.filehdr.NumberOfSections = pe->sec_count;
697 pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders;
698 pe_header.opthdr.ImageBase = pe->imagebase;
248dc675 grischka2013-02-04 17:58:37 +0100699 pe_header.opthdr.Subsystem = pe->subsystem;
45184e01 grischka2011-07-11 18:47:16 +0200700 if (pe->s1->pe_stack_size)
701 pe_header.opthdr.SizeOfStackReserve = pe->s1->pe_stack_size;
5342b32e grischka2007-12-19 17:36:42 +0000702 if (PE_DLL == pe->type)
f366cb20 grischka2009-07-06 21:35:25 +0200703 pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL;
2c538d7a bellard2005-04-10 21:46:58 +0000704
834b782a grischka2009-07-23 19:19:23 +0200705 sum = 0;
706 pe_fwrite(&pe_header, sizeof pe_header, op, &sum);
5342b32e grischka2007-12-19 17:36:42 +0000707 for (i = 0; i < pe->sec_count; ++i)
834b782a grischka2009-07-23 19:19:23 +0200708 pe_fwrite(&pe->sec_info[i].ish, sizeof(IMAGE_SECTION_HEADER), op, &sum);
709 pe_fpad(op, pe->sizeofheaders);
710 for (i = 0; i < pe->sec_count; ++i) {
711 si = pe->sec_info + i;
712 psh = &si->ish;
713 if (si->data_size) {
714 pe_fwrite(si->data, si->data_size, op, &sum);
715 file_offset = psh->PointerToRawData + psh->SizeOfRawData;
716 pe_fpad(op, file_offset);
717 }
718 }
719
720 pe_header.opthdr.CheckSum = sum + file_offset;
721 fseek(op, offsetof(struct pe_header, opthdr.CheckSum), SEEK_SET);
722 pe_fwrite(&pe_header.opthdr.CheckSum, sizeof pe_header.opthdr.CheckSum, op, NULL);
5342b32e grischka2007-12-19 17:36:42 +0000723 fclose (op);
2c538d7a bellard2005-04-10 21:46:58 +0000724
f9181416 grischka2009-05-11 18:45:44 +0200725 if (2 == pe->s1->verbose)
34140dd6 grischka2007-12-13 19:07:19 +0000726 printf("-------------------------------\n");
f9181416 grischka2009-05-11 18:45:44 +0200727 if (pe->s1->verbose)
f332bb3b grischka2010-11-27 15:02:57 +0100728 printf("<- %s (%u bytes)\n", pe->filename, (unsigned)file_offset);
2c538d7a bellard2005-04-10 21:46:58 +0000729
730 return 0;
731}
732
733/*----------------------------------------------------------------------------*/
5342b32e grischka2007-12-19 17:36:42 +0000734
88a3ccab grischka2009-12-20 01:53:49 +0100735static struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index)
2c538d7a bellard2005-04-10 21:46:58 +0000736{
737 int i;
738 int dll_index;
739 struct pe_import_info *p;
740 struct import_symbol *s;
2341ee51 grischka2010-01-14 20:55:51 +0100741 ElfW(Sym) *isym;
2c538d7a bellard2005-04-10 21:46:58 +0000742
2341ee51 grischka2010-01-14 20:55:51 +0100743 isym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
744 dll_index = isym->st_size;
5342b32e grischka2007-12-19 17:36:42 +0000745
746 i = dynarray_assoc ((void**)pe->imp_info, pe->imp_count, dll_index);
2c538d7a bellard2005-04-10 21:46:58 +0000747 if (-1 != i) {
34140dd6 grischka2007-12-13 19:07:19 +0000748 p = pe->imp_info[i];
749 goto found_dll;
2c538d7a bellard2005-04-10 21:46:58 +0000750 }
751 p = tcc_mallocz(sizeof *p);
752 p->dll_index = dll_index;
5342b32e grischka2007-12-19 17:36:42 +0000753 dynarray_add((void***)&pe->imp_info, &pe->imp_count, p);
2c538d7a bellard2005-04-10 21:46:58 +0000754
5342b32e grischka2007-12-19 17:36:42 +0000755found_dll:
756 i = dynarray_assoc ((void**)p->symbols, p->sym_count, sym_index);
2c538d7a bellard2005-04-10 21:46:58 +0000757 if (-1 != i)
5342b32e grischka2007-12-19 17:36:42 +0000758 return p->symbols[i];
759
2c538d7a bellard2005-04-10 21:46:58 +0000760 s = tcc_mallocz(sizeof *s);
5342b32e grischka2007-12-19 17:36:42 +0000761 dynarray_add((void***)&p->symbols, &p->sym_count, s);
2c538d7a bellard2005-04-10 21:46:58 +0000762 s->sym_index = sym_index;
5342b32e grischka2007-12-19 17:36:42 +0000763 return s;
2c538d7a bellard2005-04-10 21:46:58 +0000764}
765
766/*----------------------------------------------------------------------------*/
88a3ccab grischka2009-12-20 01:53:49 +0100767static void pe_build_imports(struct pe_info *pe)
2c538d7a bellard2005-04-10 21:46:58 +0000768{
769 int thk_ptr, ent_ptr, dll_ptr, sym_cnt, i;
5342b32e grischka2007-12-19 17:36:42 +0000770 DWORD rva_base = pe->thunk->sh_addr - pe->imagebase;
2c538d7a bellard2005-04-10 21:46:58 +0000771 int ndlls = pe->imp_count;
772
773 for (sym_cnt = i = 0; i < ndlls; ++i)
34140dd6 grischka2007-12-13 19:07:19 +0000774 sym_cnt += pe->imp_info[i]->sym_count;
2c538d7a bellard2005-04-10 21:46:58 +0000775
776 if (0 == sym_cnt)
34140dd6 grischka2007-12-13 19:07:19 +0000777 return;
2c538d7a bellard2005-04-10 21:46:58 +0000778
779 pe_align_section(pe->thunk, 16);
780
781 pe->imp_offs = dll_ptr = pe->thunk->data_offset;
719ba918 grischka2009-07-06 21:35:35 +0200782 pe->imp_size = (ndlls + 1) * sizeof(IMAGE_IMPORT_DESCRIPTOR);
2c538d7a bellard2005-04-10 21:46:58 +0000783 pe->iat_offs = dll_ptr + pe->imp_size;
f366cb20 grischka2009-07-06 21:35:25 +0200784 pe->iat_size = (sym_cnt + ndlls) * sizeof(ADDR3264);
5342b32e grischka2007-12-19 17:36:42 +0000785 section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size);
2c538d7a bellard2005-04-10 21:46:58 +0000786
787 thk_ptr = pe->iat_offs;
788 ent_ptr = pe->iat_offs + pe->iat_size;
5342b32e grischka2007-12-19 17:36:42 +0000789
2c538d7a bellard2005-04-10 21:46:58 +0000790 for (i = 0; i < pe->imp_count; ++i) {
719ba918 grischka2009-07-06 21:35:35 +0200791 IMAGE_IMPORT_DESCRIPTOR *hdr;
2341ee51 grischka2010-01-14 20:55:51 +0100792 int k, n, dllindex;
f366cb20 grischka2009-07-06 21:35:25 +0200793 ADDR3264 v;
34140dd6 grischka2007-12-13 19:07:19 +0000794 struct pe_import_info *p = pe->imp_info[i];
2341ee51 grischka2010-01-14 20:55:51 +0100795 const char *name;
796 DLLReference *dllref;
797
798 dllindex = p->dll_index;
799 if (dllindex)
800 name = (dllref = pe->s1->loaded_dlls[dllindex-1])->name;
801 else
802 name = "", dllref = NULL;
34140dd6 grischka2007-12-13 19:07:19 +0000803
804 /* put the dll name into the import header */
34140dd6 grischka2007-12-13 19:07:19 +0000805 v = put_elf_str(pe->thunk, name);
719ba918 grischka2009-07-06 21:35:35 +0200806 hdr = (IMAGE_IMPORT_DESCRIPTOR*)(pe->thunk->data + dll_ptr);
807 hdr->FirstThunk = thk_ptr + rva_base;
808 hdr->OriginalFirstThunk = ent_ptr + rva_base;
809 hdr->Name = v + rva_base;
34140dd6 grischka2007-12-13 19:07:19 +0000810
811 for (k = 0, n = p->sym_count; k <= n; ++k) {
812 if (k < n) {
370cdeb6 grischka2009-07-06 21:34:22 +0200813 int iat_index = p->symbols[k]->iat_index;
34140dd6 grischka2007-12-13 19:07:19 +0000814 int sym_index = p->symbols[k]->sym_index;
fe8def33 grischka2009-07-06 21:34:49 +0200815 ElfW(Sym) *imp_sym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
816 ElfW(Sym) *org_sym = (ElfW(Sym) *)symtab_section->data + iat_index;
5342b32e grischka2007-12-19 17:36:42 +0000817 const char *name = pe->s1->dynsymtab_section->link->data + imp_sym->st_name;
d443644d grischka2014-01-06 19:04:50 +0100818 int ordinal;
5342b32e grischka2007-12-19 17:36:42 +0000819
820 org_sym->st_value = thk_ptr;
821 org_sym->st_shndx = pe->thunk->sh_num;
d443644d grischka2014-01-06 19:04:50 +0100822
823 if (dllref)
824 v = 0, ordinal = imp_sym->st_value; /* ordinal from pe_load_def */
825 else
826 ordinal = 0, v = imp_sym->st_value; /* address from tcc_add_symbol() */
827
370cdeb6 grischka2009-07-06 21:34:22 +0200828#ifdef TCC_IS_NATIVE
829 if (pe->type == PE_RUN) {
2341ee51 grischka2010-01-14 20:55:51 +0100830 if (dllref) {
831 if ( !dllref->handle )
832 dllref->handle = LoadLibrary(dllref->name);
642b6d0f YX Hao2013-09-19 21:50:38 +0800833 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(LPCSTR)NULL+ordinal:name);
2341ee51 grischka2010-01-14 20:55:51 +0100834 }
370cdeb6 grischka2009-07-06 21:34:22 +0200835 if (!v)
642b6d0f YX Hao2013-09-19 21:50:38 +0800836 tcc_error_noabort("can't build symbol '%s'", name);
d443644d grischka2014-01-06 19:04:50 +0100837 } else
370cdeb6 grischka2009-07-06 21:34:22 +0200838#endif
d443644d grischka2014-01-06 19:04:50 +0100839 if (ordinal) {
840 v = ordinal | (ADDR3264)1 << (sizeof(ADDR3264)*8 - 1);
841 } else {
842 v = pe->thunk->data_offset + rva_base;
843 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
844 put_elf_str(pe->thunk, name);
845 }
846
34140dd6 grischka2007-12-13 19:07:19 +0000847 } else {
5342b32e grischka2007-12-19 17:36:42 +0000848 v = 0; /* last entry is zero */
34140dd6 grischka2007-12-13 19:07:19 +0000849 }
d443644d grischka2014-01-06 19:04:50 +0100850
f366cb20 grischka2009-07-06 21:35:25 +0200851 *(ADDR3264*)(pe->thunk->data+thk_ptr) =
d443644d grischka2014-01-06 19:04:50 +0100852 *(ADDR3264*)(pe->thunk->data+ent_ptr) = v;
f366cb20 grischka2009-07-06 21:35:25 +0200853 thk_ptr += sizeof (ADDR3264);
854 ent_ptr += sizeof (ADDR3264);
34140dd6 grischka2007-12-13 19:07:19 +0000855 }
719ba918 grischka2009-07-06 21:35:35 +0200856 dll_ptr += sizeof(IMAGE_IMPORT_DESCRIPTOR);
5342b32e grischka2007-12-19 17:36:42 +0000857 dynarray_reset(&p->symbols, &p->sym_count);
2c538d7a bellard2005-04-10 21:46:58 +0000858 }
5342b32e grischka2007-12-19 17:36:42 +0000859 dynarray_reset(&pe->imp_info, &pe->imp_count);
2c538d7a bellard2005-04-10 21:46:58 +0000860}
861
862/* ------------------------------------------------------------- */
5342b32e grischka2007-12-19 17:36:42 +0000863
dcfad3af grischka2009-07-06 21:34:37 +0200864struct pe_sort_sym
865{
866 int index;
867 const char *name;
868};
869
88a3ccab grischka2009-12-20 01:53:49 +0100870static int sym_cmp(const void *va, const void *vb)
fe9b1f60 bellard2005-04-17 13:15:54 +0000871{
dcfad3af grischka2009-07-06 21:34:37 +0200872 const char *ca = (*(struct pe_sort_sym**)va)->name;
873 const char *cb = (*(struct pe_sort_sym**)vb)->name;
fe9b1f60 bellard2005-04-17 13:15:54 +0000874 return strcmp(ca, cb);
875}
876
88a3ccab grischka2009-12-20 01:53:49 +0100877static void pe_build_exports(struct pe_info *pe)
2c538d7a bellard2005-04-10 21:46:58 +0000878{
fe8def33 grischka2009-07-06 21:34:49 +0200879 ElfW(Sym) *sym;
5342b32e grischka2007-12-19 17:36:42 +0000880 int sym_index, sym_end;
881 DWORD rva_base, func_o, name_o, ord_o, str_o;
719ba918 grischka2009-07-06 21:35:35 +0200882 IMAGE_EXPORT_DIRECTORY *hdr;
dcfad3af grischka2009-07-06 21:34:37 +0200883 int sym_count, ord;
884 struct pe_sort_sym **sorted, *p;
fe9b1f60 bellard2005-04-17 13:15:54 +0000885
5342b32e grischka2007-12-19 17:36:42 +0000886 FILE *op;
887 char buf[MAX_PATH];
888 const char *dllname;
889 const char *name;
2c538d7a bellard2005-04-10 21:46:58 +0000890
5342b32e grischka2007-12-19 17:36:42 +0000891 rva_base = pe->thunk->sh_addr - pe->imagebase;
dcfad3af grischka2009-07-06 21:34:37 +0200892 sym_count = 0, sorted = NULL, op = NULL;
5342b32e grischka2007-12-19 17:36:42 +0000893
fe8def33 grischka2009-07-06 21:34:49 +0200894 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
5342b32e grischka2007-12-19 17:36:42 +0000895 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
fe8def33 grischka2009-07-06 21:34:49 +0200896 sym = (ElfW(Sym)*)symtab_section->data + sym_index;
a3ebdd0a grischka2011-02-13 17:41:05 +0100897 name = pe_export_name(pe->s1, sym);
2ac238fc grischka2014-04-17 17:01:28 +0200898 if ((sym->st_other & ST_PE_EXPORT)
5342b32e grischka2007-12-19 17:36:42 +0000899 /* export only symbols from actually written sections */
900 && pe->s1->sections[sym->st_shndx]->sh_addr) {
dcfad3af grischka2009-07-06 21:34:37 +0200901 p = tcc_malloc(sizeof *p);
902 p->index = sym_index;
903 p->name = name;
904 dynarray_add((void***)&sorted, &sym_count, p);
5342b32e grischka2007-12-19 17:36:42 +0000905 }
5342b32e grischka2007-12-19 17:36:42 +0000906#if 0
2ac238fc grischka2014-04-17 17:01:28 +0200907 if (sym->st_other & ST_PE_EXPORT)
5342b32e grischka2007-12-19 17:36:42 +0000908 printf("export: %s\n", name);
2ac238fc grischka2014-04-17 17:01:28 +0200909 if (sym->st_other & ST_PE_STDCALL)
5342b32e grischka2007-12-19 17:36:42 +0000910 printf("stdcall: %s\n", name);
911#endif
fe9b1f60 bellard2005-04-17 13:15:54 +0000912 }
2c538d7a bellard2005-04-10 21:46:58 +0000913
914 if (0 == sym_count)
34140dd6 grischka2007-12-13 19:07:19 +0000915 return;
2c538d7a bellard2005-04-10 21:46:58 +0000916
dcfad3af grischka2009-07-06 21:34:37 +0200917 qsort (sorted, sym_count, sizeof *sorted, sym_cmp);
918
2c538d7a bellard2005-04-10 21:46:58 +0000919 pe_align_section(pe->thunk, 16);
5342b32e grischka2007-12-19 17:36:42 +0000920 dllname = tcc_basename(pe->filename);
2c538d7a bellard2005-04-10 21:46:58 +0000921
922 pe->exp_offs = pe->thunk->data_offset;
719ba918 grischka2009-07-06 21:35:35 +0200923 func_o = pe->exp_offs + sizeof(IMAGE_EXPORT_DIRECTORY);
5342b32e grischka2007-12-19 17:36:42 +0000924 name_o = func_o + sym_count * sizeof (DWORD);
925 ord_o = name_o + sym_count * sizeof (DWORD);
926 str_o = ord_o + sym_count * sizeof(WORD);
927
928 hdr = section_ptr_add(pe->thunk, str_o - pe->exp_offs);
929 hdr->Characteristics = 0;
930 hdr->Base = 1;
931 hdr->NumberOfFunctions = sym_count;
932 hdr->NumberOfNames = sym_count;
933 hdr->AddressOfFunctions = func_o + rva_base;
934 hdr->AddressOfNames = name_o + rva_base;
935 hdr->AddressOfNameOrdinals = ord_o + rva_base;
936 hdr->Name = str_o + rva_base;
937 put_elf_str(pe->thunk, dllname);
938
939#if 1
940 /* automatically write exports to <output-filename>.def */
2f6b8469 grischka2013-01-31 11:29:45 +0100941 pstrcpy(buf, sizeof buf, pe->filename);
5342b32e grischka2007-12-19 17:36:42 +0000942 strcpy(tcc_fileextension(buf), ".def");
943 op = fopen(buf, "w");
944 if (NULL == op) {
bf374a5f grischka2011-08-11 17:07:56 +0200945 tcc_error_noabort("could not create '%s': %s", buf, strerror(errno));
5342b32e grischka2007-12-19 17:36:42 +0000946 } else {
947 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", dllname);
f9181416 grischka2009-05-11 18:45:44 +0200948 if (pe->s1->verbose)
5342b32e grischka2007-12-19 17:36:42 +0000949 printf("<- %s (%d symbols)\n", buf, sym_count);
950 }
951#endif
952
dcfad3af grischka2009-07-06 21:34:37 +0200953 for (ord = 0; ord < sym_count; ++ord)
fe9b1f60 bellard2005-04-17 13:15:54 +0000954 {
dcfad3af grischka2009-07-06 21:34:37 +0200955 p = sorted[ord], sym_index = p->index, name = p->name;
5342b32e grischka2007-12-19 17:36:42 +0000956 /* insert actual address later in pe_relocate_rva */
957 put_elf_reloc(symtab_section, pe->thunk,
fe8def33 grischka2009-07-06 21:34:49 +0200958 func_o, R_XXX_RELATIVE, sym_index);
5342b32e grischka2007-12-19 17:36:42 +0000959 *(DWORD*)(pe->thunk->data + name_o)
960 = pe->thunk->data_offset + rva_base;
961 *(WORD*)(pe->thunk->data + ord_o)
962 = ord;
34140dd6 grischka2007-12-13 19:07:19 +0000963 put_elf_str(pe->thunk, name);
5342b32e grischka2007-12-19 17:36:42 +0000964 func_o += sizeof (DWORD);
965 name_o += sizeof (DWORD);
966 ord_o += sizeof (WORD);
5342b32e grischka2007-12-19 17:36:42 +0000967 if (op)
968 fprintf(op, "%s\n", name);
2c538d7a bellard2005-04-10 21:46:58 +0000969 }
970 pe->exp_size = pe->thunk->data_offset - pe->exp_offs;
dcfad3af grischka2009-07-06 21:34:37 +0200971 dynarray_reset(&sorted, &sym_count);
a5279b2c grischka2009-11-29 14:40:46 +0100972 if (op)
973 fclose(op);
2c538d7a bellard2005-04-10 21:46:58 +0000974}
975
976/* ------------------------------------------------------------- */
88a3ccab grischka2009-12-20 01:53:49 +0100977static void pe_build_reloc (struct pe_info *pe)
2c538d7a bellard2005-04-10 21:46:58 +0000978{
979 DWORD offset, block_ptr, addr;
980 int count, i;
fe8def33 grischka2009-07-06 21:34:49 +0200981 ElfW_Rel *rel, *rel_end;
2c538d7a bellard2005-04-10 21:46:58 +0000982 Section *s = NULL, *sr;
5342b32e grischka2007-12-19 17:36:42 +0000983
2c538d7a bellard2005-04-10 21:46:58 +0000984 offset = addr = block_ptr = count = i = 0;
985 rel = rel_end = NULL;
5342b32e grischka2007-12-19 17:36:42 +0000986
987 for(;;) {
34140dd6 grischka2007-12-13 19:07:19 +0000988 if (rel < rel_end) {
aa80e5b1 grischka2011-07-14 19:23:04 +0200989 int type = ELFW(R_TYPE)(rel->r_info);
34140dd6 grischka2007-12-13 19:07:19 +0000990 addr = rel->r_offset + s->sh_addr;
5342b32e grischka2007-12-19 17:36:42 +0000991 ++ rel;
fe8def33 grischka2009-07-06 21:34:49 +0200992 if (type != REL_TYPE_DIRECT)
34140dd6 grischka2007-12-13 19:07:19 +0000993 continue;
5342b32e grischka2007-12-19 17:36:42 +0000994 if (count == 0) { /* new block */
34140dd6 grischka2007-12-13 19:07:19 +0000995 block_ptr = pe->reloc->data_offset;
996 section_ptr_add(pe->reloc, sizeof(struct pe_reloc_header));
5342b32e grischka2007-12-19 17:36:42 +0000997 offset = addr & 0xFFFFFFFF<<12;
34140dd6 grischka2007-12-13 19:07:19 +0000998 }
5342b32e grischka2007-12-19 17:36:42 +0000999 if ((addr -= offset) < (1<<12)) { /* one block spans 4k addresses */
1000 WORD *wp = section_ptr_add(pe->reloc, sizeof (WORD));
1001 *wp = addr | IMAGE_REL_BASED_HIGHLOW<<12;
34140dd6 grischka2007-12-13 19:07:19 +00001002 ++count;
1003 continue;
1004 }
5342b32e grischka2007-12-19 17:36:42 +00001005 -- rel;
1006
1007 } else if (i < pe->sec_count) {
1008 sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc;
34140dd6 grischka2007-12-13 19:07:19 +00001009 if (sr) {
fe8def33 grischka2009-07-06 21:34:49 +02001010 rel = (ElfW_Rel *)sr->data;
1011 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
34140dd6 grischka2007-12-13 19:07:19 +00001012 }
1013 continue;
1014 }
1015
5342b32e grischka2007-12-19 17:36:42 +00001016 if (count) {
1017 /* store the last block and ready for a new one */
34140dd6 grischka2007-12-13 19:07:19 +00001018 struct pe_reloc_header *hdr;
5342b32e grischka2007-12-19 17:36:42 +00001019 if (count & 1) /* align for DWORDS */
1020 section_ptr_add(pe->reloc, sizeof(WORD)), ++count;
1021 hdr = (struct pe_reloc_header *)(pe->reloc->data + block_ptr);
1022 hdr -> offset = offset - pe->imagebase;
1023 hdr -> size = count * sizeof(WORD) + sizeof(struct pe_reloc_header);
34140dd6 grischka2007-12-13 19:07:19 +00001024 count = 0;
1025 }
5342b32e grischka2007-12-19 17:36:42 +00001026
34140dd6 grischka2007-12-13 19:07:19 +00001027 if (rel >= rel_end)
1028 break;
2c538d7a bellard2005-04-10 21:46:58 +00001029 }
1030}
1031
1032/* ------------------------------------------------------------- */
88a3ccab grischka2009-12-20 01:53:49 +01001033static int pe_section_class(Section *s)
2c538d7a bellard2005-04-10 21:46:58 +00001034{
5342b32e grischka2007-12-19 17:36:42 +00001035 int type, flags;
1036 const char *name;
1037
1038 type = s->sh_type;
1039 flags = s->sh_flags;
1040 name = s->name;
1041 if (flags & SHF_ALLOC) {
1042 if (type == SHT_PROGBITS) {
1043 if (flags & SHF_EXECINSTR)
1044 return sec_text;
1045 if (flags & SHF_WRITE)
1046 return sec_data;
1047 if (0 == strcmp(name, ".rsrc"))
1048 return sec_rsrc;
1049 if (0 == strcmp(name, ".iedat"))
1050 return sec_idata;
50b040ef grischka2009-12-19 22:40:28 +01001051 if (0 == strcmp(name, ".pdata"))
1052 return sec_pdata;
4a8c2229 grischka2009-02-16 15:13:29 +01001053 return sec_other;
5342b32e grischka2007-12-19 17:36:42 +00001054 } else if (type == SHT_NOBITS) {
1055 if (flags & SHF_WRITE)
1056 return sec_bss;
1057 }
1058 } else {
1059 if (0 == strcmp(name, ".reloc"))
1060 return sec_reloc;
1061 if (0 == strncmp(name, ".stab", 5)) /* .stab and .stabstr */
1062 return sec_stab;
1063 }
1064 return -1;
1065}
1066
88a3ccab grischka2009-12-20 01:53:49 +01001067static int pe_assign_addresses (struct pe_info *pe)
5342b32e grischka2007-12-19 17:36:42 +00001068{
1069 int i, k, o, c;
2c538d7a bellard2005-04-10 21:46:58 +00001070 DWORD addr;
5342b32e grischka2007-12-19 17:36:42 +00001071 int *section_order;
1072 struct section_info *si;
1073 Section *s;
1074
1075 // pe->thunk = new_section(pe->s1, ".iedat", SHT_PROGBITS, SHF_ALLOC);
1076
1077 section_order = tcc_malloc(pe->s1->nb_sections * sizeof (int));
1078 for (o = k = 0 ; k < sec_last; ++k) {
1079 for (i = 1; i < pe->s1->nb_sections; ++i) {
1080 s = pe->s1->sections[i];
1081 if (k == pe_section_class(s)) {
1082 // printf("%s %d\n", s->name, k);
1083 s->sh_addr = pe->imagebase;
1084 section_order[o++] = i;
1085 }
1086 }
1087 }
2c538d7a bellard2005-04-10 21:46:58 +00001088
5342b32e grischka2007-12-19 17:36:42 +00001089 pe->sec_info = tcc_mallocz(o * sizeof (struct section_info));
2c538d7a bellard2005-04-10 21:46:58 +00001090 addr = pe->imagebase + 1;
1091
5342b32e grischka2007-12-19 17:36:42 +00001092 for (i = 0; i < o; ++i)
1093 {
1094 k = section_order[i];
1095 s = pe->s1->sections[k];
1096 c = pe_section_class(s);
1097 si = &pe->sec_info[pe->sec_count];
2c538d7a bellard2005-04-10 21:46:58 +00001098
2c538d7a bellard2005-04-10 21:46:58 +00001099#ifdef PE_MERGE_DATA
5342b32e grischka2007-12-19 17:36:42 +00001100 if (c == sec_bss && pe->sec_count && si[-1].cls == sec_data) {
1101 /* append .bss to .data */
50b040ef grischka2009-12-19 22:40:28 +01001102 s->sh_addr = addr = ((addr-1) | (s->sh_addralign-1)) + 1;
5342b32e grischka2007-12-19 17:36:42 +00001103 addr += s->data_offset;
1104 si[-1].sh_size = addr - si[-1].sh_addr;
1105 continue;
1106 }
2c538d7a bellard2005-04-10 21:46:58 +00001107#endif
b7e75ccd grischka2012-12-22 18:06:17 +01001108 if (c == sec_stab && 0 == pe->s1->do_debug)
1109 continue;
1110
5342b32e grischka2007-12-19 17:36:42 +00001111 strcpy(si->name, s->name);
1112 si->cls = c;
1113 si->ord = k;
2f73e42d Frederic Feret2009-08-27 10:12:13 +02001114 si->sh_addr = s->sh_addr = addr = pe_virtual_align(pe, addr);
5342b32e grischka2007-12-19 17:36:42 +00001115 si->sh_flags = s->sh_flags;
1116
1117 if (c == sec_data && NULL == pe->thunk)
1118 pe->thunk = s;
1119
1120 if (s == pe->thunk) {
1121 pe_build_imports(pe);
1122 pe_build_exports(pe);
1123 }
1124
1125 if (c == sec_reloc)
1126 pe_build_reloc (pe);
1127
1128 if (s->data_offset)
1129 {
1130 if (s->sh_type != SHT_NOBITS) {
1131 si->data = s->data;
1132 si->data_size = s->data_offset;
34140dd6 grischka2007-12-13 19:07:19 +00001133 }
5342b32e grischka2007-12-19 17:36:42 +00001134
1135 addr += s->data_offset;
1136 si->sh_size = s->data_offset;
1137 ++pe->sec_count;
34140dd6 grischka2007-12-13 19:07:19 +00001138 }
5342b32e grischka2007-12-19 17:36:42 +00001139 // printf("%08x %05x %s\n", si->sh_addr, si->sh_size, si->name);
2c538d7a bellard2005-04-10 21:46:58 +00001140 }
5342b32e grischka2007-12-19 17:36:42 +00001141
1142#if 0
1143 for (i = 1; i < pe->s1->nb_sections; ++i) {
1144 Section *s = pe->s1->sections[i];
1145 int type = s->sh_type;
1146 int flags = s->sh_flags;
1147 printf("section %-16s %-10s %5x %s,%s,%s\n",
1148 s->name,
1149 type == SHT_PROGBITS ? "progbits" :
1150 type == SHT_NOBITS ? "nobits" :
1151 type == SHT_SYMTAB ? "symtab" :
1152 type == SHT_STRTAB ? "strtab" :
fe8def33 grischka2009-07-06 21:34:49 +02001153 type == SHT_RELX ? "rel" : "???",
5342b32e grischka2007-12-19 17:36:42 +00001154 s->data_offset,
1155 flags & SHF_ALLOC ? "alloc" : "",
1156 flags & SHF_WRITE ? "write" : "",
1157 flags & SHF_EXECINSTR ? "exec" : ""
1158 );
1159 }
f9181416 grischka2009-05-11 18:45:44 +02001160 pe->s1->verbose = 2;
5342b32e grischka2007-12-19 17:36:42 +00001161#endif
1162
1163 tcc_free(section_order);
2c538d7a bellard2005-04-10 21:46:58 +00001164 return 0;
1165}
1166
5342b32e grischka2007-12-19 17:36:42 +00001167/* ------------------------------------------------------------- */
88a3ccab grischka2009-12-20 01:53:49 +01001168static void pe_relocate_rva (struct pe_info *pe, Section *s)
5342b32e grischka2007-12-19 17:36:42 +00001169{
1170 Section *sr = s->reloc;
fe8def33 grischka2009-07-06 21:34:49 +02001171 ElfW_Rel *rel, *rel_end;
1172 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1173 for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
aa80e5b1 grischka2011-07-14 19:23:04 +02001174 if (ELFW(R_TYPE)(rel->r_info) == R_XXX_RELATIVE) {
1175 int sym_index = ELFW(R_SYM)(rel->r_info);
5342b32e grischka2007-12-19 17:36:42 +00001176 DWORD addr = s->sh_addr;
1177 if (sym_index) {
fe8def33 grischka2009-07-06 21:34:49 +02001178 ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
5342b32e grischka2007-12-19 17:36:42 +00001179 addr = sym->st_value;
1180 }
50b040ef grischka2009-12-19 22:40:28 +01001181 // printf("reloc rva %08x %08x %s\n", (DWORD)rel->r_offset, addr, s->name);
5342b32e grischka2007-12-19 17:36:42 +00001182 *(DWORD*)(s->data + rel->r_offset) += addr - pe->imagebase;
1183 }
fe8def33 grischka2009-07-06 21:34:49 +02001184 }
5342b32e grischka2007-12-19 17:36:42 +00001185}
1186
2c538d7a bellard2005-04-10 21:46:58 +00001187/*----------------------------------------------------------------------------*/
88a3ccab grischka2009-12-20 01:53:49 +01001188
1445fea5 grischka2009-12-19 19:14:01 +01001189static int pe_isafunc(int sym_index)
1190{
1191 Section *sr = text_section->reloc;
1192 ElfW_Rel *rel, *rel_end;
1193 Elf32_Word info = ELF32_R_INFO(sym_index, R_386_PC32);
1194 if (!sr)
1195 return 0;
1196 rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
1197 for (rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++)
1198 if (rel->r_info == info)
1199 return 1;
1200 return 0;
1201}
1202
1203/*----------------------------------------------------------------------------*/
88a3ccab grischka2009-12-20 01:53:49 +01001204static int pe_check_symbols(struct pe_info *pe)
2c538d7a bellard2005-04-10 21:46:58 +00001205{
fe8def33 grischka2009-07-06 21:34:49 +02001206 ElfW(Sym) *sym;
5342b32e grischka2007-12-19 17:36:42 +00001207 int sym_index, sym_end;
2c538d7a bellard2005-04-10 21:46:58 +00001208 int ret = 0;
1209
1210 pe_align_section(text_section, 8);
1211
fe8def33 grischka2009-07-06 21:34:49 +02001212 sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
5342b32e grischka2007-12-19 17:36:42 +00001213 for (sym_index = 1; sym_index < sym_end; ++sym_index) {
1214
fe8def33 grischka2009-07-06 21:34:49 +02001215 sym = (ElfW(Sym) *)symtab_section->data + sym_index;
34140dd6 grischka2007-12-13 19:07:19 +00001216 if (sym->st_shndx == SHN_UNDEF) {
5342b32e grischka2007-12-19 17:36:42 +00001217
1218 const char *name = symtab_section->link->data + sym->st_name;
aa80e5b1 grischka2011-07-14 19:23:04 +02001219 unsigned type = ELFW(ST_TYPE)(sym->st_info);
a3ebdd0a grischka2011-02-13 17:41:05 +01001220 int imp_sym = pe_find_import(pe->s1, sym);
5342b32e grischka2007-12-19 17:36:42 +00001221 struct import_symbol *is;
1222
1223 if (0 == imp_sym)
1224 goto not_found;
5342b32e grischka2007-12-19 17:36:42 +00001225
1445fea5 grischka2009-12-19 19:14:01 +01001226 if (type == STT_NOTYPE) {
1227 /* symbols from assembler have no type, find out which */
1228 if (pe_isafunc(sym_index))
1229 type = STT_FUNC;
1230 else
1231 type = STT_OBJECT;
1232 }
1233
2341ee51 grischka2010-01-14 20:55:51 +01001234 is = pe_add_import(pe, imp_sym);
1235
5342b32e grischka2007-12-19 17:36:42 +00001236 if (type == STT_FUNC) {
1237 unsigned long offset = is->thk_offset;
1238 if (offset) {
1239 /* got aliased symbol, like stricmp and _stricmp */
1240
1241 } else {
1242 char buffer[100];
f366cb20 grischka2009-07-06 21:35:25 +02001243 WORD *p;
5342b32e grischka2007-12-19 17:36:42 +00001244
1245 offset = text_section->data_offset;
1246 /* add the 'jmp IAT[x]' instruction */
2cb1fd6a
TVL
Timo VJ Lahde2010-04-19 15:50:22 +03001247#ifdef TCC_TARGET_ARM
1248 p = section_ptr_add(text_section, 8+4); // room for code and address
1249 (*(DWORD*)(p)) = 0xE59FC000; // arm code ldr ip, [pc] ; PC+8+0 = 0001xxxx
1250 (*(DWORD*)(p+2)) = 0xE59CF000; // arm code ldr pc, [ip]
1251#else
f366cb20 grischka2009-07-06 21:35:25 +02001252 p = section_ptr_add(text_section, 8);
1253 *p = 0x25FF;
1254#ifdef TCC_TARGET_X86_64
1255 *(DWORD*)(p+1) = (DWORD)-4;
1256#endif
2cb1fd6a Timo VJ Lahde2010-04-19 15:50:22 +03001257#endif
5342b32e grischka2007-12-19 17:36:42 +00001258 /* add a helper symbol, will be patched later in
1259 pe_build_imports */
1260 sprintf(buffer, "IAT.%s", name);
1261 is->iat_index = put_elf_sym(
1262 symtab_section, 0, sizeof(DWORD),
aa80e5b1 grischka2011-07-14 19:23:04 +02001263 ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT),
5342b32e grischka2007-12-19 17:36:42 +00001264 0, SHN_UNDEF, buffer);
2cb1fd6a
TVL
Timo VJ Lahde2010-04-19 15:50:22 +03001265#ifdef TCC_TARGET_ARM
1266 put_elf_reloc(symtab_section, text_section,
1267 offset + 8, R_XXX_THUNKFIX, is->iat_index); // offset to IAT position
1268#else
5342b32e grischka2007-12-19 17:36:42 +00001269 put_elf_reloc(symtab_section, text_section,
fe8def33 grischka2009-07-06 21:34:49 +02001270 offset + 2, R_XXX_THUNKFIX, is->iat_index);
2cb1fd6a Timo VJ Lahde2010-04-19 15:50:22 +03001271#endif
5342b32e grischka2007-12-19 17:36:42 +00001272 is->thk_offset = offset;
34140dd6 grischka2007-12-13 19:07:19 +00001273 }
5342b32e grischka2007-12-19 17:36:42 +00001274
1275 /* tcc_realloc might have altered sym's address */
fe8def33 grischka2009-07-06 21:34:49 +02001276 sym = (ElfW(Sym) *)symtab_section->data + sym_index;
5342b32e grischka2007-12-19 17:36:42 +00001277
1278 /* patch the original symbol */
1279 sym->st_value = offset;
1280 sym->st_shndx = text_section->sh_num;
2ac238fc grischka2014-04-17 17:01:28 +02001281 sym->st_other &= ~ST_PE_EXPORT; /* do not export */
5342b32e grischka2007-12-19 17:36:42 +00001282 continue;
34140dd6 grischka2007-12-13 19:07:19 +00001283 }
5342b32e grischka2007-12-19 17:36:42 +00001284
1285 if (type == STT_OBJECT) { /* data, ptr to that should be */
1286 if (0 == is->iat_index) {
1287 /* original symbol will be patched later in pe_build_imports */
1288 is->iat_index = sym_index;
1289 continue;
1290 }
1291 }
1292
1293 not_found:
bf374a5f grischka2011-08-11 17:07:56 +02001294 tcc_error_noabort("undefined symbol '%s'", name);
834b782a grischka2009-07-23 19:19:23 +02001295 ret = -1;
5342b32e grischka2007-12-19 17:36:42 +00001296
1297 } else if (pe->s1->rdynamic
aa80e5b1 grischka2011-07-14 19:23:04 +02001298 && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
34140dd6 grischka2007-12-13 19:07:19 +00001299 /* if -rdynamic option, then export all non local symbols */
2ac238fc grischka2014-04-17 17:01:28 +02001300 sym->st_other |= ST_PE_EXPORT;
34140dd6 grischka2007-12-13 19:07:19 +00001301 }
2c538d7a bellard2005-04-10 21:46:58 +00001302 }
1303 return ret;
1304}
1305
1306/*----------------------------------------------------------------------------*/
1307#ifdef PE_PRINT_SECTIONS
88a3ccab grischka2009-12-20 01:53:49 +01001308static void pe_print_section(FILE * f, Section * s)
5342b32e grischka2007-12-19 17:36:42 +00001309{
1310 /* just if you'r curious */
2c538d7a bellard2005-04-10 21:46:58 +00001311 BYTE *p, *e, b;
1312 int i, n, l, m;
1313 p = s->data;
1314 e = s->data + s->data_offset;
1315 l = e - p;
1316
1317 fprintf(f, "section \"%s\"", s->name);
1318 if (s->link)
34140dd6 grischka2007-12-13 19:07:19 +00001319 fprintf(f, "\nlink \"%s\"", s->link->name);
2c538d7a bellard2005-04-10 21:46:58 +00001320 if (s->reloc)
34140dd6 grischka2007-12-13 19:07:19 +00001321 fprintf(f, "\nreloc \"%s\"", s->reloc->name);
fe8def33 grischka2009-07-06 21:34:49 +02001322 fprintf(f, "\nv_addr %08X", (unsigned)s->sh_addr);
1323 fprintf(f, "\ncontents %08X", (unsigned)l);
2c538d7a bellard2005-04-10 21:46:58 +00001324 fprintf(f, "\n\n");
1325
1326 if (s->sh_type == SHT_NOBITS)
34140dd6 grischka2007-12-13 19:07:19 +00001327 return;
2c538d7a bellard2005-04-10 21:46:58 +00001328
5342b32e grischka2007-12-19 17:36:42 +00001329 if (0 == l)
1330 return;
1331
2c538d7a bellard2005-04-10 21:46:58 +00001332 if (s->sh_type == SHT_SYMTAB)
fe8def33 grischka2009-07-06 21:34:49 +02001333 m = sizeof(ElfW(Sym));
1334 else if (s->sh_type == SHT_RELX)
1335 m = sizeof(ElfW_Rel);
2c538d7a bellard2005-04-10 21:46:58 +00001336 else
34140dd6 grischka2007-12-13 19:07:19 +00001337 m = 16;
2c538d7a bellard2005-04-10 21:46:58 +00001338
5342b32e grischka2007-12-19 17:36:42 +00001339 fprintf(f, "%-8s", "offset");
1340 for (i = 0; i < m; ++i)
1341 fprintf(f, " %02x", i);
1342 n = 56;
1343
fe8def33 grischka2009-07-06 21:34:49 +02001344 if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_RELX) {
5342b32e grischka2007-12-19 17:36:42 +00001345 const char *fields1[] = {
1346 "name",
1347 "value",
1348 "size",
1349 "bind",
1350 "type",
1351 "other",
1352 "shndx",
1353 NULL
1354 };
1355
1356 const char *fields2[] = {
1357 "offs",
1358 "type",
1359 "symb",
1360 NULL
1361 };
1362
1363 const char **p;
1364
1365 if (s->sh_type == SHT_SYMTAB)
1366 p = fields1, n = 106;
1367 else
1368 p = fields2, n = 58;
1369
1370 for (i = 0; p[i]; ++i)
1371 fprintf(f, "%6s", p[i]);
1372 fprintf(f, " symbol");
1373 }
1374
1375 fprintf(f, "\n");
1376 for (i = 0; i < n; ++i)
1377 fprintf(f, "-");
1378 fprintf(f, "\n");
1379
1380 for (i = 0; i < l;)
1381 {
34140dd6 grischka2007-12-13 19:07:19 +00001382 fprintf(f, "%08X", i);
1383 for (n = 0; n < m; ++n) {
1384 if (n + i < l)
1385 fprintf(f, " %02X", p[i + n]);
1386 else
1387 fprintf(f, " ");
1388 }
1389
1390 if (s->sh_type == SHT_SYMTAB) {
fe8def33 grischka2009-07-06 21:34:49 +02001391 ElfW(Sym) *sym = (ElfW(Sym) *) (p + i);
34140dd6 grischka2007-12-13 19:07:19 +00001392 const char *name = s->link->data + sym->st_name;
5342b32e grischka2007-12-19 17:36:42 +00001393 fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"",
fe8def33 grischka2009-07-06 21:34:49 +02001394 (unsigned)sym->st_name,
1395 (unsigned)sym->st_value,
1396 (unsigned)sym->st_size,
aa80e5b1 grischka2011-07-14 19:23:04 +02001397 (unsigned)ELFW(ST_BIND)(sym->st_info),
1398 (unsigned)ELFW(ST_TYPE)(sym->st_info),
fe8def33 grischka2009-07-06 21:34:49 +02001399 (unsigned)sym->st_other,
1400 (unsigned)sym->st_shndx,
1401 name);
1402
1403 } else if (s->sh_type == SHT_RELX) {
1404 ElfW_Rel *rel = (ElfW_Rel *) (p + i);
1405 ElfW(Sym) *sym =
aa80e5b1 grischka2011-07-14 19:23:04 +02001406 (ElfW(Sym) *) s->link->data + ELFW(R_SYM)(rel->r_info);
34140dd6 grischka2007-12-13 19:07:19 +00001407 const char *name = s->link->link->data + sym->st_name;
5342b32e grischka2007-12-19 17:36:42 +00001408 fprintf(f, " %04X %02X %04X \"%s\"",
fe8def33 grischka2009-07-06 21:34:49 +02001409 (unsigned)rel->r_offset,
aa80e5b1 grischka2011-07-14 19:23:04 +02001410 (unsigned)ELFW(R_TYPE)(rel->r_info),
1411 (unsigned)ELFW(R_SYM)(rel->r_info),
fe8def33 grischka2009-07-06 21:34:49 +02001412 name);
34140dd6 grischka2007-12-13 19:07:19 +00001413 } else {
1414 fprintf(f, " ");
1415 for (n = 0; n < m; ++n) {
1416 if (n + i < l) {
1417 b = p[i + n];
1418 if (b < 32 || b >= 127)
1419 b = '.';
1420 fprintf(f, "%c", b);
1421 }
1422 }
1423 }
1424 i += m;
1425 fprintf(f, "\n");
2c538d7a bellard2005-04-10 21:46:58 +00001426 }
1427 fprintf(f, "\n\n");
1428}
5342b32e grischka2007-12-19 17:36:42 +00001429
88a3ccab grischka2009-12-20 01:53:49 +01001430static void pe_print_sections(TCCState *s1, const char *fname)
5342b32e grischka2007-12-19 17:36:42 +00001431{
1432 Section *s;
1433 FILE *f;
1434 int i;
f366cb20 grischka2009-07-06 21:35:25 +02001435 f = fopen(fname, "w");
5342b32e grischka2007-12-19 17:36:42 +00001436 for (i = 1; i < s1->nb_sections; ++i) {
1437 s = s1->sections[i];
1438 pe_print_section(f, s);
1439 }
1440 pe_print_section(f, s1->dynsymtab_section);
1441 fclose(f);
1442}
2c538d7a bellard2005-04-10 21:46:58 +00001443#endif
1444
1df662c1 grischka2009-07-18 22:07:25 +02001445/* ------------------------------------------------------------- */
2341ee51 grischka2010-01-14 20:55:51 +01001446/* helper function for load/store to insert one more indirection */
1447
1448ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2)
1449{
1450 Sym *sym;
1451 ElfW(Sym) *esym;
1452 int r2;
1453
1454 if ((sv->r & (VT_VALMASK|VT_SYM)) != (VT_CONST|VT_SYM) || (sv->r2 != VT_CONST))
1455 return sv;
1456 sym = sv->sym;
1457 if ((sym->type.t & (VT_EXTERN|VT_STATIC)) != VT_EXTERN)
1458 return sv;
1459 if (!sym->c)
1460 put_extern_sym(sym, NULL, 0, 0);
1461 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
2ac238fc grischka2014-04-17 17:01:28 +02001462 if (!(esym->st_other & ST_PE_IMPORT))
2341ee51 grischka2010-01-14 20:55:51 +01001463 return sv;
1464
1465 // printf("import %04x %04x %04x %s\n", sv->type.t, sym->type.t, sv->r, get_tok_str(sv->sym->v, NULL));
1466
1467 memset(v2, 0, sizeof *v2);
1468 v2->type.t = VT_PTR;
1469 v2->r = VT_CONST | VT_SYM | VT_LVAL;
1470 v2->sym = sv->sym;
1471
1472 r2 = get_reg(RC_INT);
1473 load(r2, v2);
1474 v2->r = r2;
1475
1476 if (sv->c.ui) {
1477 vpushv(v2);
1478 vpushi(sv->c.ui);
1479 gen_opi('+');
1480 *v2 = *vtop--;
1481 }
1482
1483 v2->type.t = sv->type.t;
1484 v2->r |= sv->r & VT_LVAL;
1485 return v2;
1486}
1487
82bcbd02 grischka2013-02-04 16:08:06 +01001488ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)
2341ee51 grischka2010-01-14 20:55:51 +01001489{
1490 return add_elf_sym(
1491 s1->dynsymtab_section,
82bcbd02 grischka2013-02-04 16:08:06 +01001492 value,
2341ee51 grischka2010-01-14 20:55:51 +01001493 dllindex, /* st_size */
aa80e5b1 grischka2011-07-14 19:23:04 +02001494 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE),
2341ee51 grischka2010-01-14 20:55:51 +01001495 0,
1496 value ? SHN_ABS : SHN_UNDEF,
1497 name
1498 );
1499}
1500
1501static int add_dllref(TCCState *s1, const char *dllname)
1502{
1503 DLLReference *dllref;
1504 int i;
1505 for (i = 0; i < s1->nb_loaded_dlls; ++i)
1506 if (0 == strcmp(s1->loaded_dlls[i]->name, dllname))
1507 return i + 1;
1508 dllref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));
1509 strcpy(dllref->name, dllname);
1510 dynarray_add((void ***) &s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
1511 return s1->nb_loaded_dlls;
1512}
1513
1514/* ------------------------------------------------------------- */
94bf4d2c grischka2009-12-19 22:08:52 +01001515
05108a3b grischka2013-02-12 19:13:28 +01001516static int read_mem(int fd, unsigned offset, void *buffer, unsigned len)
2c538d7a bellard2005-04-10 21:46:58 +00001517{
05108a3b grischka2013-02-12 19:13:28 +01001518 lseek(fd, offset, SEEK_SET);
1519 return len == read(fd, buffer, len);
5342b32e grischka2007-12-19 17:36:42 +00001520}
2c538d7a bellard2005-04-10 21:46:58 +00001521
1df662c1 grischka2009-07-18 22:07:25 +02001522/* -------------------------------------------------------------
1523 * This is for compiled windows resources in 'coff' format
1524 * as generated by 'windres.exe -O coff ...'.
1525 */
5342b32e grischka2007-12-19 17:36:42 +00001526
05108a3b grischka2013-02-12 19:13:28 +01001527static int pe_load_res(TCCState *s1, int fd)
5342b32e grischka2007-12-19 17:36:42 +00001528{
1529 struct pe_rsrc_header hdr;
1530 Section *rsrc_section;
1531 int i, ret = -1;
1532 BYTE *ptr;
1df662c1 grischka2009-07-18 22:07:25 +02001533 unsigned offs;
5342b32e grischka2007-12-19 17:36:42 +00001534
05108a3b grischka2013-02-12 19:13:28 +01001535 if (!read_mem(fd, 0, &hdr, sizeof hdr))
5342b32e grischka2007-12-19 17:36:42 +00001536 goto quit;
1df662c1 grischka2009-07-18 22:07:25 +02001537
12f43953 grischka2014-08-01 10:51:28 +02001538 if (hdr.filehdr.Machine != IMAGE_FILE_MACHINE
1df662c1 grischka2009-07-18 22:07:25 +02001539 || hdr.filehdr.NumberOfSections != 1
1540 || strcmp(hdr.sectionhdr.Name, ".rsrc") != 0)
5342b32e grischka2007-12-19 17:36:42 +00001541 goto quit;
1542
1543 rsrc_section = new_section(s1, ".rsrc", SHT_PROGBITS, SHF_ALLOC);
1544 ptr = section_ptr_add(rsrc_section, hdr.sectionhdr.SizeOfRawData);
1df662c1 grischka2009-07-18 22:07:25 +02001545 offs = hdr.sectionhdr.PointerToRawData;
05108a3b grischka2013-02-12 19:13:28 +01001546 if (!read_mem(fd, offs, ptr, hdr.sectionhdr.SizeOfRawData))
5342b32e grischka2007-12-19 17:36:42 +00001547 goto quit;
1df662c1 grischka2009-07-18 22:07:25 +02001548 offs = hdr.sectionhdr.PointerToRelocations;
5342b32e grischka2007-12-19 17:36:42 +00001549 for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i)
1550 {
1551 struct pe_rsrc_reloc rel;
05108a3b grischka2013-02-12 19:13:28 +01001552 if (!read_mem(fd, offs, &rel, sizeof rel))
5342b32e grischka2007-12-19 17:36:42 +00001553 goto quit;
1554 // printf("rsrc_reloc: %x %x %x\n", rel.offset, rel.size, rel.type);
12f43953 grischka2014-08-01 10:51:28 +02001555 if (rel.type != RSRC_RELTYPE)
5342b32e grischka2007-12-19 17:36:42 +00001556 goto quit;
1557 put_elf_reloc(symtab_section, rsrc_section,
fe8def33 grischka2009-07-06 21:34:49 +02001558 rel.offset, R_XXX_RELATIVE, 0);
1df662c1 grischka2009-07-18 22:07:25 +02001559 offs += sizeof rel;
2c538d7a bellard2005-04-10 21:46:58 +00001560 }
5342b32e grischka2007-12-19 17:36:42 +00001561 ret = 0;
1562quit:
2c538d7a bellard2005-04-10 21:46:58 +00001563 return ret;
1564}
1565
1566/* ------------------------------------------------------------- */
88a3ccab grischka2009-12-20 01:53:49 +01001567static char *trimfront(char *p)
2c538d7a bellard2005-04-10 21:46:58 +00001568{
5342b32e grischka2007-12-19 17:36:42 +00001569 while (*p && (unsigned char)*p <= ' ')
1570 ++p;
1571 return p;
1572}
2c538d7a bellard2005-04-10 21:46:58 +00001573
88a3ccab grischka2009-12-20 01:53:49 +01001574static char *trimback(char *a, char *e)
5342b32e grischka2007-12-19 17:36:42 +00001575{
1576 while (e > a && (unsigned char)e[-1] <= ' ')
1577 --e;
1578 *e = 0;;
1579 return a;
1580}
34140dd6 grischka2007-12-13 19:07:19 +00001581
2c538d7a bellard2005-04-10 21:46:58 +00001582/* ------------------------------------------------------------- */
05108a3b grischka2013-02-12 19:13:28 +01001583static int pe_load_def(TCCState *s1, int fd)
fe9b1f60 bellard2005-04-17 13:15:54 +00001584{
d443644d grischka2014-01-06 19:04:50 +01001585 int state = 0, ret = -1, dllindex = 0, ord;
1586 char line[400], dllname[80], *p, *x;
11214817 grischka2014-04-13 20:30:46 +02001587 FILE *fp;
5342b32e grischka2007-12-19 17:36:42 +00001588
11214817 grischka2014-04-13 20:30:46 +02001589 fp = fdopen(dup(fd), "rb");
1590 while (fgets(line, sizeof line, fp))
1591 {
1592 p = trimfront(trimback(line, strchr(line, 0)));
5342b32e grischka2007-12-19 17:36:42 +00001593 if (0 == *p || ';' == *p)
1594 continue;
11214817 grischka2014-04-13 20:30:46 +02001595
5342b32e grischka2007-12-19 17:36:42 +00001596 switch (state) {
1597 case 0:
1598 if (0 != strnicmp(p, "LIBRARY", 7))
1599 goto quit;
2f6b8469 grischka2013-01-31 11:29:45 +01001600 pstrcpy(dllname, sizeof dllname, trimfront(p+7));
5342b32e grischka2007-12-19 17:36:42 +00001601 ++state;
1602 continue;
1603
1604 case 1:
1605 if (0 != stricmp(p, "EXPORTS"))
1606 goto quit;
1607 ++state;
1608 continue;
1609
1610 case 2:
2341ee51 grischka2010-01-14 20:55:51 +01001611 dllindex = add_dllref(s1, dllname);
5342b32e grischka2007-12-19 17:36:42 +00001612 ++state;
d443644d grischka2014-01-06 19:04:50 +01001613 /* fall through */
5342b32e grischka2007-12-19 17:36:42 +00001614 default:
d443644d grischka2014-01-06 19:04:50 +01001615 /* get ordinal and will store in sym->st_value */
1616 ord = 0;
1617 x = strchr(p, ' ');
1618 if (x) {
1619 *x = 0, x = strrchr(x + 1, '@');
1620 if (x) {
1621 char *d;
1622 ord = (int)strtol(x + 1, &d, 10);
1623 if (*d)
1624 ord = 0;
1625 }
642b6d0f
YH
YX Hao2013-09-19 21:50:38 +08001626 }
1627 pe_putimport(s1, dllindex, p, ord);
5342b32e grischka2007-12-19 17:36:42 +00001628 continue;
1629 }
1630 }
1631 ret = 0;
1632quit:
11214817 grischka2014-04-13 20:30:46 +02001633 fclose(fp);
1df662c1 grischka2009-07-18 22:07:25 +02001634 return ret;
1635}
1636
1637/* ------------------------------------------------------------- */
1638#define TINY_IMPDEF_GET_EXPORT_NAMES_ONLY
1639#include "win32/tools/tiny_impdef.c"
1640
05108a3b grischka2013-02-12 19:13:28 +01001641static int pe_load_dll(TCCState *s1, const char *dllname, int fd)
1df662c1 grischka2009-07-18 22:07:25 +02001642{
1df662c1 grischka2009-07-18 22:07:25 +02001643 char *p, *q;
2341ee51 grischka2010-01-14 20:55:51 +01001644 int index;
05108a3b grischka2013-02-12 19:13:28 +01001645 p = get_export_names(fd);
2341ee51 grischka2010-01-14 20:55:51 +01001646 if (!p)
1647 return -1;
1648 index = add_dllref(s1, dllname);
1649 for (q = p; *q; q += 1 + strlen(q))
82bcbd02 grischka2013-02-04 16:08:06 +01001650 pe_putimport(s1, index, q, 0);
2341ee51 grischka2010-01-14 20:55:51 +01001651 tcc_free(p);
1652 return 0;
1df662c1 grischka2009-07-18 22:07:25 +02001653}
1654
1655/* ------------------------------------------------------------- */
8c56b0cf Thomas Preud'homme2013-01-14 17:34:07 +01001656ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd)
1df662c1 grischka2009-07-18 22:07:25 +02001657{
1df662c1 grischka2009-07-18 22:07:25 +02001658 int ret = -1;
1659 char buf[10];
05108a3b grischka2013-02-12 19:13:28 +01001660 if (0 == strcmp(tcc_fileextension(filename), ".def"))
1661 ret = pe_load_def(s1, fd);
1662 else if (pe_load_res(s1, fd) == 0)
1663 ret = 0;
1664 else if (read_mem(fd, 0, buf, sizeof buf) && 0 == strncmp(buf, "MZ", 2))
1665 ret = pe_load_dll(s1, tcc_basename(filename), fd);
5342b32e grischka2007-12-19 17:36:42 +00001666 return ret;
fe9b1f60 bellard2005-04-17 13:15:54 +00001667}
1668
1669/* ------------------------------------------------------------- */
f366cb20 grischka2009-07-06 21:35:25 +02001670#ifdef TCC_TARGET_X86_64
df4c0892 grischka2011-07-14 19:09:49 +02001671static unsigned pe_add_uwwind_info(TCCState *s1)
50b040ef grischka2009-12-19 22:40:28 +01001672{
df4c0892 grischka2011-07-14 19:09:49 +02001673 if (NULL == s1->uw_pdata) {
1674 s1->uw_pdata = find_section(tcc_state, ".pdata");
1675 s1->uw_pdata->sh_addralign = 4;
1676 s1->uw_sym = put_elf_sym(symtab_section, 0, 0, 0, 0, text_section->sh_num, NULL);
1677 }
50b040ef grischka2009-12-19 22:40:28 +01001678
df4c0892 grischka2011-07-14 19:09:49 +02001679 if (0 == s1->uw_offs) {
1680 /* As our functions all have the same stackframe, we use one entry for all */
1681 static const unsigned char uw_info[] = {
1682 0x01, // UBYTE: 3 Version , UBYTE: 5 Flags
1683 0x04, // UBYTE Size of prolog
1684 0x02, // UBYTE Count of unwind codes
1685 0x05, // UBYTE: 4 Frame Register (rbp), UBYTE: 4 Frame Register offset (scaled)
1686 // USHORT * n Unwind codes array
1687 // 0x0b, 0x01, 0xff, 0xff, // stack size
1688 0x04, 0x03, // set frame ptr (mov rsp -> rbp)
1689 0x01, 0x50 // push reg (rbp)
1690 };
50b040ef grischka2009-12-19 22:40:28 +01001691
df4c0892 grischka2011-07-14 19:09:49 +02001692 Section *s = text_section;
1693 unsigned char *p;
50b040ef grischka2009-12-19 22:40:28 +01001694
df4c0892 grischka2011-07-14 19:09:49 +02001695 section_ptr_add(s, -s->data_offset & 3); /* align */
1696 s1->uw_offs = s->data_offset;
1697 p = section_ptr_add(s, sizeof uw_info);
1698 memcpy(p, uw_info, sizeof uw_info);
50b040ef grischka2009-12-19 22:40:28 +01001699 }
1700
df4c0892 grischka2011-07-14 19:09:49 +02001701 return s1->uw_offs;
1702}
1703
1704ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack)
1705{
1706 TCCState *s1 = tcc_state;
1707 Section *pd;
1708 unsigned o, n, d;
1709 struct /* _RUNTIME_FUNCTION */ {
1710 DWORD BeginAddress;
1711 DWORD EndAddress;
1712 DWORD UnwindData;
1713 } *p;
1714
1715 d = pe_add_uwwind_info(s1);
1716 pd = s1->uw_pdata;
1717 o = pd->data_offset;
1718 p = section_ptr_add(pd, sizeof *p);
1719
50b040ef grischka2009-12-19 22:40:28 +01001720 /* record this function */
df4c0892 grischka2011-07-14 19:09:49 +02001721 p->BeginAddress = start;
1722 p->EndAddress = end;
1723 p->UnwindData = d;
1724
50b040ef grischka2009-12-19 22:40:28 +01001725 /* put relocations on it */
df4c0892 grischka2011-07-14 19:09:49 +02001726 for (n = o + sizeof *p; o < n; o += sizeof p->BeginAddress)
1727 put_elf_reloc(symtab_section, pd, o, R_X86_64_RELATIVE, s1->uw_sym);
50b040ef grischka2009-12-19 22:40:28 +01001728}
1729#endif
50b040ef grischka2009-12-19 22:40:28 +01001730/* ------------------------------------------------------------- */
1731#ifdef TCC_TARGET_X86_64
f366cb20 grischka2009-07-06 21:35:25 +02001732#define PE_STDSYM(n,s) n
1733#else
1734#define PE_STDSYM(n,s) "_" n s
1735#endif
5342b32e grischka2007-12-19 17:36:42 +00001736
05108a3b grischka2013-02-12 19:13:28 +01001737static void pe_add_runtime(TCCState *s1, struct pe_info *pe)
2c538d7a bellard2005-04-10 21:46:58 +00001738{
1739 const char *start_symbol;
5342b32e grischka2007-12-19 17:36:42 +00001740 int pe_type = 0;
2c538d7a bellard2005-04-10 21:46:58 +00001741
f366cb20 grischka2009-07-06 21:35:25 +02001742 if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16")))
34140dd6 grischka2007-12-13 19:07:19 +00001743 pe_type = PE_GUI;
fe9b1f60 bellard2005-04-17 13:15:54 +00001744 else
5342b32e grischka2007-12-19 17:36:42 +00001745 if (TCC_OUTPUT_DLL == s1->output_type) {
fe9b1f60 bellard2005-04-17 13:15:54 +00001746 pe_type = PE_DLL;
5342b32e grischka2007-12-19 17:36:42 +00001747 /* need this for 'tccelf.c:relocate_section()' */
fe9b1f60 bellard2005-04-17 13:15:54 +00001748 s1->output_type = TCC_OUTPUT_EXE;
1749 }
2341ee51 grischka2010-01-14 20:55:51 +01001750 else
1751 pe_type = PE_EXE;
2c538d7a bellard2005-04-10 21:46:58 +00001752
1753 start_symbol =
34140dd6 grischka2007-12-13 19:07:19 +00001754 TCC_OUTPUT_MEMORY == s1->output_type
13b99766 grischka2013-09-10 15:36:56 +02001755 ? PE_GUI == pe_type ? "__runwinmain" : "_main"
a3ebdd0a grischka2011-02-13 17:41:05 +01001756 : PE_DLL == pe_type ? PE_STDSYM("__dllstart","@12")
1757 : PE_GUI == pe_type ? "__winstart" : "__start"
5342b32e grischka2007-12-19 17:36:42 +00001758 ;
2c538d7a bellard2005-04-10 21:46:58 +00001759
73faaea2 grischka2013-08-28 22:55:05 +02001760 if (!s1->leading_underscore || strchr(start_symbol, '@'))
a3ebdd0a grischka2011-02-13 17:41:05 +01001761 ++start_symbol;
a3ebdd0a grischka2011-02-13 17:41:05 +01001762
2c538d7a bellard2005-04-10 21:46:58 +00001763 /* grab the startup code from libtcc1 */
13b99766 grischka2013-09-10 15:36:56 +02001764 if (TCC_OUTPUT_MEMORY != s1->output_type || PE_GUI == pe_type)
34140dd6 grischka2007-12-13 19:07:19 +00001765 add_elf_sym(symtab_section,
5342b32e grischka2007-12-19 17:36:42 +00001766 0, 0,
aa80e5b1 grischka2011-07-14 19:23:04 +02001767 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
5342b32e grischka2007-12-19 17:36:42 +00001768 SHN_UNDEF, start_symbol);
2c538d7a bellard2005-04-10 21:46:58 +00001769
1770 if (0 == s1->nostdlib) {
f366cb20 grischka2009-07-06 21:35:25 +02001771 static const char *libs[] = {
05108a3b grischka2013-02-12 19:13:28 +01001772 "libtcc1.a", "msvcrt", "kernel32", "", "user32", "gdi32", NULL
f366cb20 grischka2009-07-06 21:35:25 +02001773 };
1774 const char **pp, *p;
1775 for (pp = libs; 0 != (p = *pp); ++pp) {
1776 if (0 == *p) {
1777 if (PE_DLL != pe_type && PE_GUI != pe_type)
1778 break;
05108a3b grischka2013-02-12 19:13:28 +01001779 } else if (pp == libs ? tcc_add_dll(s1, p, 0) : tcc_add_library(s1, p)) {
bf374a5f grischka2011-08-11 17:07:56 +02001780 tcc_error_noabort("cannot find library: %s", p);
05108a3b grischka2013-02-12 19:13:28 +01001781 break;
1782 }
34140dd6 grischka2007-12-13 19:07:19 +00001783 }
2c538d7a bellard2005-04-10 21:46:58 +00001784 }
1785
73faaea2 grischka2013-08-28 22:55:05 +02001786 if (TCC_OUTPUT_MEMORY == s1->output_type) {
2341ee51 grischka2010-01-14 20:55:51 +01001787 pe_type = PE_RUN;
4ad186c5 grischka2014-01-06 19:07:08 +01001788#ifdef TCC_IS_NATIVE
73faaea2 grischka2013-08-28 22:55:05 +02001789 s1->runtime_main = start_symbol;
4ad186c5 grischka2014-01-06 19:07:08 +01001790#endif
13b99766 grischka2013-09-10 15:36:56 +02001791 } else {
32a49625 grischka2014-01-21 13:25:14 +01001792 pe->start_addr = (DWORD)(uintptr_t)tcc_get_symbol_err(s1, start_symbol);
5342b32e grischka2007-12-19 17:36:42 +00001793 }
f366cb20 grischka2009-07-06 21:35:25 +02001794
1795 pe->type = pe_type;
5342b32e grischka2007-12-19 17:36:42 +00001796}
1797
88a3ccab grischka2009-12-20 01:53:49 +01001798ST_FUNC int pe_output_file(TCCState * s1, const char *filename)
2c538d7a bellard2005-04-10 21:46:58 +00001799{
1800 int ret;
1801 struct pe_info pe;
1802 int i;
5342b32e grischka2007-12-19 17:36:42 +00001803
2c538d7a bellard2005-04-10 21:46:58 +00001804 memset(&pe, 0, sizeof pe);
1805 pe.filename = filename;
1806 pe.s1 = s1;
2c538d7a bellard2005-04-10 21:46:58 +00001807
7fa712e0 grischka2009-12-19 22:22:43 +01001808 tcc_add_bcheck(s1);
05108a3b grischka2013-02-12 19:13:28 +01001809 pe_add_runtime(s1, &pe);
5342b32e grischka2007-12-19 17:36:42 +00001810 relocate_common_syms(); /* assign bss adresses */
1811 tcc_add_linker_symbols(s1);
1812
2c538d7a bellard2005-04-10 21:46:58 +00001813 ret = pe_check_symbols(&pe);
370cdeb6 grischka2009-07-06 21:34:22 +02001814 if (ret)
7fa712e0 grischka2009-12-19 22:22:43 +01001815 ;
1816 else if (filename) {
5342b32e grischka2007-12-19 17:36:42 +00001817 if (PE_DLL == pe.type) {
1818 pe.reloc = new_section(pe.s1, ".reloc", SHT_PROGBITS, 0);
b7d7af4f Frederic Feret2009-08-27 10:25:28 +02001819 /* XXX: check if is correct for arm-pe target */
5342b32e grischka2007-12-19 17:36:42 +00001820 pe.imagebase = 0x10000000;
1821 } else {
b7d7af4f
FF
Frederic Feret2009-08-27 10:25:28 +02001822#if defined(TCC_TARGET_ARM)
1823 pe.imagebase = 0x00010000;
1824#else
5342b32e grischka2007-12-19 17:36:42 +00001825 pe.imagebase = 0x00400000;
b7d7af4f Frederic Feret2009-08-27 10:25:28 +02001826#endif
5342b32e grischka2007-12-19 17:36:42 +00001827 }
2f73e42d Frederic Feret2009-08-27 10:12:13 +02001828
b7d7af4f Frederic Feret2009-08-27 10:25:28 +02001829#if defined(TCC_TARGET_ARM)
248dc675 grischka2013-02-04 17:58:37 +01001830 /* we use "console" subsystem by default */
1831 pe.subsystem = 9;
2cb1fd6a Timo VJ Lahde2010-04-19 15:50:22 +03001832#else
248dc675 grischka2013-02-04 17:58:37 +01001833 if (PE_DLL == pe.type || PE_GUI == pe.type)
1834 pe.subsystem = 2;
1835 else
2cb1fd6a Timo VJ Lahde2010-04-19 15:50:22 +03001836 pe.subsystem = 3;
b7d7af4f Frederic Feret2009-08-27 10:25:28 +02001837#endif
248dc675 grischka2013-02-04 17:58:37 +01001838 /* Allow override via -Wl,-subsystem=... option */
1839 if (s1->pe_subsystem != 0)
1840 pe.subsystem = s1->pe_subsystem;
1841
2f73e42d
FF
Frederic Feret2009-08-27 10:12:13 +02001842 /* set default file/section alignment */
1843 if (pe.subsystem == 1) {
1844 pe.section_align = 0x20;
1845 pe.file_align = 0x20;
1846 } else {
1847 pe.section_align = 0x1000;
1848 pe.file_align = 0x200;
1849 }
1850
1851 if (s1->section_align != 0)
1852 pe.section_align = s1->section_align;
1853 if (s1->pe_file_align != 0)
1854 pe.file_align = s1->pe_file_align;
1855
1856 if ((pe.subsystem >= 10) && (pe.subsystem <= 12))
1857 pe.imagebase = 0;
1858
1859 if (s1->has_text_addr)
1860 pe.imagebase = s1->text_addr;
1861
34140dd6 grischka2007-12-13 19:07:19 +00001862 pe_assign_addresses(&pe);
1863 relocate_syms(s1, 0);
1864 for (i = 1; i < s1->nb_sections; ++i) {
1865 Section *s = s1->sections[i];
5342b32e grischka2007-12-19 17:36:42 +00001866 if (s->reloc) {
34140dd6 grischka2007-12-13 19:07:19 +00001867 relocate_section(s1, s);
5342b32e grischka2007-12-19 17:36:42 +00001868 pe_relocate_rva(&pe, s);
1869 }
34140dd6 grischka2007-12-13 19:07:19 +00001870 }
5342b32e grischka2007-12-19 17:36:42 +00001871 if (s1->nb_errors)
834b782a grischka2009-07-23 19:19:23 +02001872 ret = -1;
5342b32e grischka2007-12-19 17:36:42 +00001873 else
1874 ret = pe_write(&pe);
1875 tcc_free(pe.sec_info);
370cdeb6 grischka2009-07-06 21:34:22 +02001876 } else {
a35b3059 grischka2012-03-05 20:15:56 +01001877#ifdef TCC_IS_NATIVE
370cdeb6 grischka2009-07-06 21:34:22 +02001878 pe.thunk = data_section;
1879 pe_build_imports(&pe);
a35b3059 grischka2012-03-05 20:15:56 +01001880#endif
2c538d7a bellard2005-04-10 21:46:58 +00001881 }
5342b32e grischka2007-12-19 17:36:42 +00001882
2c538d7a bellard2005-04-10 21:46:58 +00001883#ifdef PE_PRINT_SECTIONS
5342b32e grischka2007-12-19 17:36:42 +00001884 pe_print_sections(s1, "tcc.log");
2c538d7a bellard2005-04-10 21:46:58 +00001885#endif
1886 return ret;
1887}
1888
5342b32e grischka2007-12-19 17:36:42 +00001889/* ------------------------------------------------------------- */