_Bool type fix
[tinycc.git] / tccpe.c
blob611019e52ade4f57c3ede19ed247dcdcda69361f
1 /*
2 * TCCPE.C - PE file output for the TinyC Compiler
3 *
4 * Copyright (c) 2005 grischka
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 typedef unsigned char BYTE;
22 typedef unsigned short WORD;
23 typedef unsigned long DWORD;
24 #define ST static
26 /* XXX: move that to TCC ? */
27 int verbose = 0;
29 /* definitions below are from winnt.h */
31 typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */
32 WORD e_magic; /* Magic number */
33 WORD e_cblp; /* Bytes on last page of file */
34 WORD e_cp; /* Pages in file */
35 WORD e_crlc; /* Relocations */
36 WORD e_cparhdr; /* Size of header in paragraphs */
37 WORD e_minalloc; /* Minimum extra paragraphs needed */
38 WORD e_maxalloc; /* Maximum extra paragraphs needed */
39 WORD e_ss; /* Initial (relative) SS value */
40 WORD e_sp; /* Initial SP value */
41 WORD e_csum; /* Checksum */
42 WORD e_ip; /* Initial IP value */
43 WORD e_cs; /* Initial (relative) CS value */
44 WORD e_lfarlc; /* File address of relocation table */
45 WORD e_ovno; /* Overlay number */
46 WORD e_res[4]; /* Reserved words */
47 WORD e_oemid; /* OEM identifier (for e_oeminfo) */
48 WORD e_oeminfo; /* OEM information; e_oemid specific */
49 WORD e_res2[10]; /* Reserved words */
50 DWORD e_lfanew; /* File address of new exe header */
51 BYTE e_code[0x40];
53 } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
55 #define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
56 #define SIZE_OF_NT_SIGNATURE 4
58 typedef struct _IMAGE_FILE_HEADER {
59 WORD Machine;
60 WORD NumberOfSections;
61 DWORD TimeDateStamp;
62 DWORD PointerToSymbolTable;
63 DWORD NumberOfSymbols;
64 WORD SizeOfOptionalHeader;
65 WORD Characteristics;
66 } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
69 #define IMAGE_SIZEOF_FILE_HEADER 20
71 typedef struct _IMAGE_DATA_DIRECTORY {
72 DWORD VirtualAddress;
73 DWORD Size;
74 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
77 typedef struct _IMAGE_OPTIONAL_HEADER {
78 /* Standard fields. */
79 WORD Magic;
80 BYTE MajorLinkerVersion;
81 BYTE MinorLinkerVersion;
82 DWORD SizeOfCode;
83 DWORD SizeOfInitializedData;
84 DWORD SizeOfUninitializedData;
85 DWORD AddressOfEntryPoint;
86 DWORD BaseOfCode;
87 DWORD BaseOfData;
89 /* NT additional fields. */
90 DWORD ImageBase;
91 DWORD SectionAlignment;
92 DWORD FileAlignment;
93 WORD MajorOperatingSystemVersion;
94 WORD MinorOperatingSystemVersion;
95 WORD MajorImageVersion;
96 WORD MinorImageVersion;
97 WORD MajorSubsystemVersion;
98 WORD MinorSubsystemVersion;
99 DWORD Win32VersionValue;
100 DWORD SizeOfImage;
101 DWORD SizeOfHeaders;
102 DWORD CheckSum;
103 WORD Subsystem;
104 WORD DllCharacteristics;
105 DWORD SizeOfStackReserve;
106 DWORD SizeOfStackCommit;
107 DWORD SizeOfHeapReserve;
108 DWORD SizeOfHeapCommit;
109 DWORD LoaderFlags;
110 DWORD NumberOfRvaAndSizes;
111 IMAGE_DATA_DIRECTORY DataDirectory[16];
113 } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
116 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
117 #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
118 #define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */
119 #define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */
120 #define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */
121 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */
122 #define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */
123 /* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */
124 #define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */
125 #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */
126 #define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */
127 #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */
128 #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */
129 #define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
130 #define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */
131 #define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */
133 /* Section header format. */
134 #define IMAGE_SIZEOF_SHORT_NAME 8
136 typedef struct _IMAGE_SECTION_HEADER {
137 BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
138 union {
139 DWORD PhysicalAddress;
140 DWORD VirtualSize;
141 } Misc;
142 DWORD VirtualAddress;
143 DWORD SizeOfRawData;
144 DWORD PointerToRawData;
145 DWORD PointerToRelocations;
146 DWORD PointerToLinenumbers;
147 WORD NumberOfRelocations;
148 WORD NumberOfLinenumbers;
149 DWORD Characteristics;
150 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
152 #define IMAGE_SIZEOF_SECTION_HEADER 40
154 /* ----------------------------------------------------------- */
155 typedef struct _IMAGE_BASE_RELOCATION {
156 DWORD VirtualAddress;
157 DWORD SizeOfBlock;
158 // WORD TypeOffset[1];
159 } IMAGE_BASE_RELOCATION;
161 #define IMAGE_SIZEOF_BASE_RELOCATION 8
163 #define IMAGE_REL_BASED_ABSOLUTE 0
164 #define IMAGE_REL_BASED_HIGH 1
165 #define IMAGE_REL_BASED_LOW 2
166 #define IMAGE_REL_BASED_HIGHLOW 3
167 #define IMAGE_REL_BASED_HIGHADJ 4
168 #define IMAGE_REL_BASED_MIPS_JMPADDR 5
169 #define IMAGE_REL_BASED_SECTION 6
170 #define IMAGE_REL_BASED_REL32 7
172 /* ----------------------------------------------------------- */
174 /* ----------------------------------------------------------- */
175 IMAGE_DOS_HEADER pe_dos_hdr = {
176 0x5A4D, /*WORD e_magic; Magic number */
177 0x0090, /*WORD e_cblp; Bytes on last page of file */
178 0x0003, /*WORD e_cp; Pages in file */
179 0x0000, /*WORD e_crlc; Relocations */
181 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */
182 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */
183 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */
184 0x0000, /*WORD e_ss; Initial (relative) SS value */
186 0x00B8, /*WORD e_sp; Initial SP value */
187 0x0000, /*WORD e_csum; Checksum */
188 0x0000, /*WORD e_ip; Initial IP value */
189 0x0000, /*WORD e_cs; Initial (relative) CS value */
190 0x0040, /*WORD e_lfarlc; File address of relocation table */
191 0x0000, /*WORD e_ovno; Overlay number */
192 {0, 0, 0, 0}, /*WORD e_res[4]; Reserved words */
193 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */
194 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */
195 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /*WORD e_res2[10]; Reserved words */
196 0x00000080, /*DWORD e_lfanew; File address of new exe header */
197 { /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */
198 /*0040 */ 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8,
199 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68,
200 /*0050 */ 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d,
201 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f,
202 /*0060 */ 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69,
203 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
204 /*0070 */ 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a, 0x24, 0x00,
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 /*0080 */
210 DWORD pe_magic = IMAGE_NT_SIGNATURE;
212 IMAGE_FILE_HEADER pe_file_hdr = {
213 0x014C, /*WORD Machine; */
214 0x0003, /*WORD NumberOfSections; */
215 0x00000000, /*DWORD TimeDateStamp; */
216 0x00000000, /*DWORD PointerToSymbolTable; */
217 0x00000000, /*DWORD NumberOfSymbols; */
218 0x00E0, /*WORD SizeOfOptionalHeader; */
219 0x030F /*WORD Characteristics; */
222 IMAGE_OPTIONAL_HEADER32 pe_opt_hdr = {
223 /* Standard fields. */
224 0x010B, /*WORD Magic; */
225 0x06, /*BYTE MajorLinkerVersion; */
226 0x00, /*BYTE MinorLinkerVersion; */
227 0x00000000, /*DWORD SizeOfCode; */
228 0x00000000, /*DWORD SizeOfInitializedData; */
229 0x00000000, /*DWORD SizeOfUninitializedData; */
230 0x00000000, /*DWORD AddressOfEntryPoint; */
231 0x00000000, /*DWORD BaseOfCode; */
232 0x00000000, /*DWORD BaseOfData; */
234 /* NT additional fields. */
235 0x00400000, /*DWORD ImageBase; */
236 0x00001000, /*DWORD SectionAlignment; */
237 0x00000200, /*DWORD FileAlignment; */
238 0x0004, /*WORD MajorOperatingSystemVersion; */
239 0x0000, /*WORD MinorOperatingSystemVersion; */
240 0x0000, /*WORD MajorImageVersion; */
241 0x0000, /*WORD MinorImageVersion; */
242 0x0004, /*WORD MajorSubsystemVersion; */
243 0x0000, /*WORD MinorSubsystemVersion; */
244 0x00000000, /*DWORD Win32VersionValue; */
245 0x00000000, /*DWORD SizeOfImage; */
246 0x00000200, /*DWORD SizeOfHeaders; */
247 0x00000000, /*DWORD CheckSum; */
248 0x0002, /*WORD Subsystem; */
249 0x0000, /*WORD DllCharacteristics; */
250 0x00100000, /*DWORD SizeOfStackReserve; */
251 0x00001000, /*DWORD SizeOfStackCommit; */
252 0x00100000, /*DWORD SizeOfHeapReserve; */
253 0x00001000, /*DWORD SizeOfHeapCommit; */
254 0x00000000, /*DWORD LoaderFlags; */
255 0x00000010, /*DWORD NumberOfRvaAndSizes; */
257 /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */
258 {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
259 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}
262 /*----------------------------------------------------------------------------*/
264 /*----------------------------------------------------------------------------*/
266 struct pe_import_header {
267 DWORD first_entry;
268 DWORD time_date;
269 DWORD forwarder;
270 DWORD lib_name_offset;
271 DWORD first_thunk;
274 struct pe_export_header {
275 DWORD Characteristics;
276 DWORD TimeDateStamp;
277 DWORD Version;
278 DWORD Name;
279 DWORD Base;
280 DWORD NumberOfFunctions;
281 DWORD NumberOfNames;
282 DWORD AddressOfFunctions;
283 DWORD AddressOfNames;
284 DWORD AddressOfNameOrdinals;
287 struct pe_reloc_header {
288 DWORD offset;
289 DWORD size;
292 /* ------------------------------------------------------------- */
293 /* internal temporary structures */
295 ST const char *pe_sec_names[] = {
296 ".text",
297 ".data",
298 ".bss",
299 ".rsrc",
300 ".reloc",
301 ".stab",
302 ".stabstr"
305 enum {
306 sec_text = 0,
307 sec_data,
308 sec_bss,
309 sec_rsrc,
310 sec_reloc,
311 sec_stab,
312 sec_stabstr,
313 pe_sec_number
316 ST DWORD pe_flags[] = {
317 0x60000020, /* ".text", */
318 0xC0000040, /* ".data", */
319 0xC0000080, /* ".bss", */
320 0x40000040, /* ".rsrc", */
321 0x42000040, /* ".reloc", */
322 0x42000802, /* ".stab", */
323 0x42000802 /* ".stabstr", */
326 struct section_info {
327 struct section_info *next;
328 int id;
329 DWORD sh_addr;
330 DWORD sh_size;
331 unsigned char *data;
332 DWORD data_size;
335 struct import_symbol {
336 int sym_index;
337 int offset;
340 struct pe_import_info {
341 int dll_index;
342 int sym_count;
343 struct import_symbol **symbols;
346 struct pe_info {
347 const char *filename;
348 DWORD sizeofheaders;
349 DWORD imagebase;
350 DWORD start_addr;
351 DWORD imp_offs;
352 DWORD imp_size;
353 DWORD iat_offs;
354 DWORD iat_size;
355 DWORD exp_offs;
356 DWORD exp_size;
357 struct section_info sh_info[pe_sec_number];
358 int sec_count;
359 struct pe_import_info **imp_info;
360 int imp_count;
361 Section *reloc;
362 Section *thunk;
363 TCCState *s1;
366 /* ------------------------------------------------------------- */
367 #define PE_MERGE_DATA
368 // #define PE_PRINT_SECTIONS
370 #ifndef MAX_PATH
371 #define MAX_PATH 260
372 #endif
374 void error_noabort(const char *, ...);
376 ST char pe_type;
378 #define PE_NUL 0
379 #define PE_DLL 1
380 #define PE_GUI 2
381 #define PE_EXE 3
383 ST int pe_find_import(TCCState * s1, const char *symbol, char *ret)
385 int sym_index = find_elf_sym(s1->dynsymtab_section, symbol);
386 if (0 == sym_index) {
387 /* Hm, maybe it's '_symbol' instead of 'symbol' or '__imp__symbol' */
388 char buffer[100];
389 if (0 == memcmp(symbol, "__imp__", 7))
390 symbol += 6;
391 else
392 buffer[0] = '_', strcpy(buffer + 1, symbol), symbol = buffer;
393 sym_index = find_elf_sym(s1->dynsymtab_section, symbol);
395 if (ret)
396 strcpy(ret, symbol);
397 return sym_index;
400 #ifdef WIN32
401 ST void **pe_imp;
402 ST int nb_pe_imp;
404 void *resolve_sym(struct TCCState *s1, const char *symbol, int type)
406 char buffer[100], *p = buffer;
407 void *a = NULL;
408 int sym_index = pe_find_import(s1, symbol, p);
409 int dll_index;
410 const char *dll_name;
411 void *hm;
413 if (sym_index) {
414 dll_index = ((Elf32_Sym *) s1->dynsymtab_section->data)[sym_index].
415 st_other;
416 dll_name = s1->loaded_dlls[dll_index]->name;
417 hm = GetModuleHandleA(dll_name);
418 if (NULL == hm)
419 hm = LoadLibraryA(dll_name);
420 if (hm) {
421 a = GetProcAddress(hm, buffer);
422 if (a && STT_OBJECT == type) {
423 // need to return a pointer to the address for data objects
424 dynarray_add(&pe_imp, &nb_pe_imp, a);
425 a = &pe_imp[nb_pe_imp - 1];
429 return a;
431 #endif
433 #define for_sym_in_symtab(sym) \
434 for (sym = (Elf32_Sym *)symtab_section->data + 1; \
435 sym < (Elf32_Sym *)(symtab_section->data + \
436 symtab_section->data_offset); \
437 ++sym)
439 #define pe_set_datadir(dir,addr,size) \
440 pe_opt_hdr.DataDirectory[dir].VirtualAddress = addr, \
441 pe_opt_hdr.DataDirectory[dir].Size = size
443 /*----------------------------------------------------------------------------*/
444 ST void dynarray_reset(void ***pp, int *n)
446 int i;
447 for (i = 0; i < *n; ++i)
448 tcc_free((*pp)[i]);
449 tcc_free(*pp);
450 *pp = NULL;
451 *n = 0;
454 ST int dynarray_assoc(void **pp, int n, int key)
456 int i;
457 for (i = 0; i < n; ++i, ++pp)
458 if (key == **(int **) pp)
459 return i;
460 return -1;
463 #if 0
464 ST DWORD umin(DWORD a, DWORD b)
466 return a < b ? a : b;
468 #endif
470 ST DWORD umax(DWORD a, DWORD b)
472 return a < b ? b : a;
475 ST void pe_fpad(FILE * fp, DWORD new_pos)
477 DWORD pos = ftell(fp);
478 while (++pos <= new_pos)
479 fputc(0, fp);
482 ST DWORD pe_file_align(DWORD n)
484 return (n + (0x200 - 1)) & ~(0x200 - 1);
487 ST DWORD pe_virtual_align(DWORD n)
489 return (n + (0x1000 - 1)) & ~(0x1000 - 1);
492 ST void pe_align_section(Section * s, int a)
494 int i = s->data_offset & (a - 1);
495 if (i)
496 section_ptr_add(s, a - i);
500 /*----------------------------------------------------------------------------*/
501 ST int pe_write_pe(struct pe_info *pe)
503 int i;
504 FILE *op;
505 DWORD file_offset;
506 IMAGE_SECTION_HEADER ish[pe_sec_number], *psh;
507 int sec_index = 0;
509 op = fopen(pe->filename, "wb");
510 if (NULL == op) {
511 error_noabort("could not create file: %s", pe->filename);
512 return 1;
515 memset(&ish, 0, sizeof ish);
517 pe->sizeofheaders = pe_file_align(sizeof pe_dos_hdr
518 + sizeof pe_magic
519 + sizeof pe_file_hdr
520 + sizeof pe_opt_hdr
522 pe->sec_count *
523 sizeof(IMAGE_SECTION_HEADER)
526 file_offset = pe->sizeofheaders;
527 pe_fpad(op, file_offset);
529 if (2 == verbose)
530 printf("-------------------------------"
531 "\n virt file size section" "\n");
533 for (i = 0; i < pe->sec_count; ++i) {
534 struct section_info *si = pe->sh_info + i;
535 const char *sh_name = pe_sec_names[si->id];
536 unsigned long addr = si->sh_addr - pe->imagebase;
537 unsigned long size = si->sh_size;
539 if (2 == verbose)
540 printf("%6lx %6lx %6lx %s\n",
541 addr, file_offset, size, sh_name);
543 switch (si->id) {
544 case sec_text:
545 pe_opt_hdr.BaseOfCode = addr;
546 pe_opt_hdr.AddressOfEntryPoint = addr + pe->start_addr;
547 break;
549 case sec_data:
550 pe_opt_hdr.BaseOfData = addr;
551 if (pe->imp_size) {
552 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IMPORT,
553 pe->imp_offs + addr, pe->imp_size);
554 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_IAT,
555 pe->iat_offs + addr, pe->iat_size);
557 if (pe->exp_size) {
558 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_EXPORT,
559 pe->exp_offs + addr, pe->exp_size);
561 break;
563 case sec_bss:
564 break;
566 case sec_reloc:
567 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size);
568 break;
570 case sec_rsrc:
571 pe_set_datadir(IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size);
572 break;
574 case sec_stab:
575 break;
577 case sec_stabstr:
578 break;
581 psh = &ish[sec_index++];
582 strcpy((char *) psh->Name, sh_name);
584 psh->Characteristics = pe_flags[si->id];
585 psh->VirtualAddress = addr;
586 psh->Misc.VirtualSize = size;
587 pe_opt_hdr.SizeOfImage =
588 umax(psh->VirtualAddress + psh->Misc.VirtualSize,
589 pe_opt_hdr.SizeOfImage);
591 if (si->data_size) {
592 psh->PointerToRawData = file_offset;
593 fwrite(si->data, 1, si->data_size, op);
594 file_offset = pe_file_align(file_offset + si->data_size);
595 psh->SizeOfRawData = file_offset - psh->PointerToRawData;
596 pe_fpad(op, file_offset);
600 /*----------------------------------------------------- */
602 pe_file_hdr.NumberOfSections = sec_index;
603 pe_opt_hdr.SizeOfHeaders = pe->sizeofheaders;
604 pe_opt_hdr.ImageBase = pe->imagebase;
605 if (PE_DLL == pe_type)
606 pe_file_hdr.Characteristics = 0x230E;
607 else if (PE_GUI != pe_type)
608 pe_opt_hdr.Subsystem = 3;
610 fseek(op, SEEK_SET, 0);
611 fwrite(&pe_dos_hdr, 1, sizeof pe_dos_hdr, op);
612 fwrite(&pe_magic, 1, sizeof pe_magic, op);
613 fwrite(&pe_file_hdr, 1, sizeof pe_file_hdr, op);
614 fwrite(&pe_opt_hdr, 1, sizeof pe_opt_hdr, op);
615 for (i = 0; i < sec_index; ++i)
616 fwrite(&ish[i], 1, sizeof(IMAGE_SECTION_HEADER), op);
617 fclose(op);
619 if (2 == verbose)
620 printf("-------------------------------\n");
621 if (verbose)
622 printf("<-- %s (%lu bytes)\n", pe->filename, file_offset);
624 return 0;
627 /*----------------------------------------------------------------------------*/
628 ST int pe_add_import(struct pe_info *pe, int sym_index, DWORD offset)
630 int i;
631 int dll_index;
632 struct pe_import_info *p;
633 struct import_symbol *s;
635 dll_index =
636 ((Elf32_Sym *) pe->s1->dynsymtab_section->data)[sym_index].
637 st_other;
638 i = dynarray_assoc((void **) pe->imp_info, pe->imp_count, dll_index);
639 if (-1 != i) {
640 p = pe->imp_info[i];
641 goto found_dll;
643 p = tcc_mallocz(sizeof *p);
644 p->dll_index = dll_index;
645 dynarray_add((void ***) &pe->imp_info, &pe->imp_count, p);
647 found_dll:
648 i = dynarray_assoc((void **) p->symbols, p->sym_count, sym_index);
649 if (-1 != i)
650 goto found_sym;
651 s = tcc_mallocz(sizeof *s);
652 s->sym_index = sym_index;
653 s->offset = offset;
654 dynarray_add((void ***) &p->symbols, &p->sym_count, s);
656 found_sym:
657 return 1;
660 /*----------------------------------------------------------------------------*/
661 ST void pe_build_imports(struct pe_info *pe)
663 int thk_ptr, ent_ptr, dll_ptr, sym_cnt, i;
664 DWORD voffset = pe->thunk->sh_addr - pe->imagebase;
665 int ndlls = pe->imp_count;
667 for (sym_cnt = i = 0; i < ndlls; ++i)
668 sym_cnt += pe->imp_info[i]->sym_count;
670 if (0 == sym_cnt)
671 return;
673 pe_align_section(pe->thunk, 16);
675 pe->imp_offs = dll_ptr = pe->thunk->data_offset;
676 pe->imp_size = (ndlls + 1) * sizeof(struct pe_import_header);
677 pe->iat_offs = dll_ptr + pe->imp_size;
678 pe->iat_size = (sym_cnt + ndlls) * sizeof(DWORD);
679 section_ptr_add(pe->thunk, pe->imp_size + 2 * pe->iat_size);
681 thk_ptr = pe->iat_offs;
682 ent_ptr = pe->iat_offs + pe->iat_size;
683 for (i = 0; i < pe->imp_count; ++i) {
684 struct pe_import_header *hdr;
685 int k, n, v;
686 struct pe_import_info *p = pe->imp_info[i];
687 const char *name = pe->s1->loaded_dlls[p->dll_index]->name;
689 /* put the dll name into the import header */
690 if (0 == strncmp(name, "lib", 3))
691 name += 3;
692 v = put_elf_str(pe->thunk, name);
694 hdr = (struct pe_import_header *) (pe->thunk->data + dll_ptr);
695 hdr->first_thunk = thk_ptr + voffset;
696 hdr->first_entry = ent_ptr + voffset;
697 hdr->lib_name_offset = v + voffset;
699 for (k = 0, n = p->sym_count; k <= n; ++k) {
700 if (k < n) {
701 DWORD offset = p->symbols[k]->offset;
702 int sym_index = p->symbols[k]->sym_index;
703 Elf32_Sym *sym =
704 (Elf32_Sym *) pe->s1->dynsymtab_section->data +
705 sym_index;
706 const char *name =
707 pe->s1->dynsymtab_section->link->data + sym->st_name;
709 if (offset & 0x80000000) { /* ref to data */
710 Elf32_Sym *sym =
711 &((Elf32_Sym *) symtab_section->
712 data)[offset & 0x7FFFFFFF];
713 sym->st_value = thk_ptr;
714 sym->st_shndx = pe->thunk->sh_num;
715 } else { /* ref to function */
717 char buffer[100];
718 sprintf(buffer, "IAT.%s", name);
719 sym_index =
720 put_elf_sym(symtab_section, thk_ptr, sizeof(DWORD),
721 ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),
722 0, pe->thunk->sh_num, buffer);
724 put_elf_reloc(symtab_section, text_section, offset, R_386_32, /*R_JMP_SLOT, */
725 sym_index);
727 v = pe->thunk->data_offset + voffset;
728 section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */
729 put_elf_str(pe->thunk, name);
730 } else {
731 v = 0; // last entry is zero
733 *(DWORD *) (pe->thunk->data + thk_ptr) =
734 *(DWORD *) (pe->thunk->data + ent_ptr) = v;
735 thk_ptr += sizeof(DWORD);
736 ent_ptr += sizeof(DWORD);
738 dll_ptr += sizeof(struct pe_import_header);
739 dynarray_reset((void ***) &p->symbols, &p->sym_count);
741 dynarray_reset((void ***) &pe->imp_info, &pe->imp_count);
744 /* ------------------------------------------------------------- */
745 ST void pe_build_exports(struct pe_info *pe)
747 Elf32_Sym *sym;
748 DWORD func_offset;
749 DWORD voffset = pe->thunk->sh_addr - pe->imagebase;
750 struct pe_export_header *hdr;
751 int sym_count = 0, ordinal = 0;
753 // for simplicity only functions are exported
754 for_sym_in_symtab(sym)
755 if (sym->st_shndx != text_section->sh_num)
756 sym->st_other &= ~1;
757 else if (sym->st_other & 1)
758 ++sym_count;
760 if (0 == sym_count)
761 return;
763 pe_align_section(pe->thunk, 16);
765 pe->exp_offs = pe->thunk->data_offset;
766 hdr = section_ptr_add(pe->thunk,
767 sizeof(struct pe_export_header) +
768 sym_count * (2 * sizeof(DWORD) + sizeof(WORD)));
770 func_offset = pe->exp_offs + sizeof(struct pe_export_header);
772 hdr->Characteristics = 0;
773 hdr->Base = 1;
774 hdr->NumberOfFunctions = sym_count;
775 hdr->NumberOfNames = sym_count;
776 hdr->AddressOfFunctions = func_offset + voffset;
777 hdr->AddressOfNames =
778 hdr->AddressOfFunctions + sym_count * sizeof(DWORD);
779 hdr->AddressOfNameOrdinals =
780 hdr->AddressOfNames + sym_count * sizeof(DWORD);
781 hdr->Name = pe->thunk->data_offset + voffset;
782 put_elf_str(pe->thunk, tcc_basename(pe->filename));
784 for_sym_in_symtab(sym)
785 if (sym->st_other & 1) {
786 char *name = symtab_section->link->data + sym->st_name;
787 DWORD *p = (DWORD *) (pe->thunk->data + func_offset);
788 DWORD *pfunc = p + ordinal;
789 DWORD *pname = p + sym_count + ordinal;
790 WORD *pord = (WORD *) (p + 2 * sym_count) + ordinal;
791 *pfunc =
792 sym->st_value + pe->s1->sections[sym->st_shndx]->sh_addr -
793 pe->imagebase;
794 *pname = pe->thunk->data_offset + voffset;
795 *pord = ordinal;
796 put_elf_str(pe->thunk, name);
797 /* printf("export: %s\n", name); */
798 ++ordinal;
800 pe->exp_size = pe->thunk->data_offset - pe->exp_offs;
803 /* ------------------------------------------------------------- */
804 ST void pe_build_reloc(struct pe_info *pe, int *section_order,
805 int section_count)
807 DWORD offset, block_ptr, addr;
808 int count, i;
809 Elf32_Rel *rel, *rel_end;
810 Section *s = NULL, *sr;
811 offset = addr = block_ptr = count = i = 0;
812 rel = rel_end = NULL;
813 for (;;) {
814 if (rel < rel_end) {
815 int type = ELF32_R_TYPE(rel->r_info);
816 addr = rel->r_offset + s->sh_addr;
817 ++rel;
818 if (type != R_386_32)
819 continue;
820 if (count == 0) { /* new block */
821 block_ptr = pe->reloc->data_offset;
822 section_ptr_add(pe->reloc, sizeof(struct pe_reloc_header));
823 offset = addr & 0xFFFFFFFF << 12;
825 if ((addr -= offset) < (1 << 12)) { /* one block spans 4k addresses */
826 WORD *wp = section_ptr_add(pe->reloc, sizeof(WORD));
827 *wp = addr | IMAGE_REL_BASED_HIGHLOW << 12;
828 ++count;
829 continue;
831 --rel;
832 } else if (i < section_count) {
833 sr = (s = pe->s1->sections[section_order[i++]])->reloc;
834 if (sr) {
835 rel = (Elf32_Rel *) sr->data;
836 rel_end = (Elf32_Rel *) (sr->data + sr->data_offset);
838 continue;
841 if (count) { /* store the last block and ready for a new one */
842 struct pe_reloc_header *hdr;
843 if (count & 1)
844 section_ptr_add(pe->reloc, 2), ++count;
845 hdr = (struct pe_reloc_header *) (pe->reloc->data + block_ptr);
846 hdr->offset = offset - pe->imagebase;
847 hdr->size =
848 count * sizeof(WORD) + sizeof(struct pe_reloc_header);
849 count = 0;
851 if (rel >= rel_end)
852 break;
856 /* ------------------------------------------------------------- */
857 ST int pe_assign_addresses(struct pe_info *pe)
859 int i, k, n;
860 DWORD addr;
861 int section_order[pe_sec_number];
862 struct section_info *si_data = NULL;
864 pe->imagebase = PE_DLL == pe_type ? 0x10000000 : 0x00400000;
865 addr = pe->imagebase + 1;
867 if (PE_DLL == pe_type)
868 pe->reloc = new_section(pe->s1, ".reloc", SHT_DYNAMIC, SHF_ALLOC);
870 for (n = k = 0; n < pe_sec_number; ++n) {
871 for (i = 1; i < pe->s1->nb_sections; ++i) {
872 Section *s = pe->s1->sections[i];
873 if (0 == strcmp(s->name, pe_sec_names[n])) {
874 struct section_info *si = &pe->sh_info[pe->sec_count];
875 #ifdef PE_MERGE_DATA
876 if (n == sec_bss && si_data) {
877 /* append .bss to .data */
878 s->sh_addr = addr = ((addr - 1) | 15) + 1;
879 addr += s->data_offset;
880 si_data->sh_size = addr - si_data->sh_addr;
881 } else
882 #endif
884 si->sh_addr = s->sh_addr = addr =
885 pe_virtual_align(addr);
886 si->id = n;
888 if (n == sec_data) {
889 pe->thunk = s;
890 si_data = si;
891 pe_build_imports(pe);
892 pe_build_exports(pe);
893 } else if (n == sec_reloc) {
894 pe_build_reloc(pe, section_order, k);
897 if (s->data_offset) {
898 if (n != sec_bss) {
899 si->data = s->data;
900 si->data_size = s->data_offset;
903 addr += s->data_offset;
904 si->sh_size = s->data_offset;
905 ++pe->sec_count;
907 //printf("Section %08X %04X %s\n", si->sh_addr, si->data_size, s->name);
909 section_order[k] = i, ++k;
913 return 0;
916 /*----------------------------------------------------------------------------*/
917 ST int pe_check_symbols(struct pe_info *pe)
919 Elf32_Sym *sym;
920 int ret = 0;
922 pe_align_section(text_section, 8);
924 for_sym_in_symtab(sym) {
925 if (sym->st_shndx == SHN_UNDEF) {
926 const char *symbol = symtab_section->link->data + sym->st_name;
927 unsigned type = ELF32_ST_TYPE(sym->st_info);
928 int sym_index = pe_find_import(pe->s1, symbol, NULL);
929 if (sym_index) {
930 if (type == STT_FUNC) {
931 unsigned long offset = text_section->data_offset;
932 if (pe_add_import(pe, sym_index, offset + 2)) {
933 /* add the 'jmp IAT[x]' instruction */
934 *(WORD *) section_ptr_add(text_section, 8) =
935 0x25FF;
936 /* patch the symbol */
937 sym->st_shndx = text_section->sh_num;
938 sym->st_value = offset;
939 continue;
941 } else if (type == STT_OBJECT) { /* data, ptr to that should be */
942 if (pe_add_import(pe, sym_index,
943 (sym -
944 (Elf32_Sym *) symtab_section->data) |
945 0x80000000))
946 continue;
949 error_noabort("undefined symbol '%s'", symbol);
950 ret = 1;
951 } else
952 if (pe->s1->rdynamic
953 && ELF32_ST_BIND(sym->st_info) != STB_LOCAL) {
954 /* if -rdynamic option, then export all non local symbols */
955 sym->st_other |= 1;
958 return ret;
961 /*----------------------------------------------------------------------------*/
962 #ifdef PE_PRINT_SECTIONS
963 ST void pe_print_section(FILE * f, Section * s)
964 { /* just if you'r curious */
965 BYTE *p, *e, b;
966 int i, n, l, m;
967 p = s->data;
968 e = s->data + s->data_offset;
969 l = e - p;
971 fprintf(f, "section \"%s\"", s->name);
972 if (s->link)
973 fprintf(f, "\nlink \"%s\"", s->link->name);
974 if (s->reloc)
975 fprintf(f, "\nreloc \"%s\"", s->reloc->name);
976 fprintf(f, "\nv_addr %08X", s->sh_addr);
977 fprintf(f, "\ncontents %08X", l);
978 fprintf(f, "\n\n");
980 if (s->sh_type == SHT_NOBITS)
981 return;
983 if (s->sh_type == SHT_SYMTAB)
984 m = sizeof(Elf32_Sym);
985 if (s->sh_type == SHT_REL)
986 m = sizeof(Elf32_Rel);
987 else
988 m = 16;
990 for (i = 0; i < l;) {
991 fprintf(f, "%08X", i);
992 for (n = 0; n < m; ++n) {
993 if (n + i < l)
994 fprintf(f, " %02X", p[i + n]);
995 else
996 fprintf(f, " ");
999 if (s->sh_type == SHT_SYMTAB) {
1000 Elf32_Sym *sym = (Elf32_Sym *) (p + i);
1001 const char *name = s->link->data + sym->st_name;
1002 fprintf(f,
1003 " name:%04X"
1004 " value:%04X"
1005 " size:%04X"
1006 " bind:%02X"
1007 " type:%02X"
1008 " other:%02X"
1009 " shndx:%04X"
1010 " \"%s\"",
1011 sym->st_name,
1012 sym->st_value,
1013 sym->st_size,
1014 ELF32_ST_BIND(sym->st_info),
1015 ELF32_ST_TYPE(sym->st_info),
1016 sym->st_other, sym->st_shndx, name);
1017 } else if (s->sh_type == SHT_REL) {
1018 Elf32_Rel *rel = (Elf32_Rel *) (p + i);
1019 Elf32_Sym *sym =
1020 (Elf32_Sym *) s->link->data + ELF32_R_SYM(rel->r_info);
1021 const char *name = s->link->link->data + sym->st_name;
1022 fprintf(f,
1023 " offset:%04X"
1024 " type:%02X"
1025 " symbol:%04X"
1026 " \"%s\"",
1027 rel->r_offset,
1028 ELF32_R_TYPE(rel->r_info),
1029 ELF32_R_SYM(rel->r_info), name);
1030 } else {
1031 fprintf(f, " ");
1032 for (n = 0; n < m; ++n) {
1033 if (n + i < l) {
1034 b = p[i + n];
1035 if (b < 32 || b >= 127)
1036 b = '.';
1037 fprintf(f, "%c", b);
1041 i += m;
1042 fprintf(f, "\n");
1044 fprintf(f, "\n\n");
1046 #endif
1048 static int pe_test_cmd(const char **pp, const char *cmd)
1050 const char *p;
1051 char *q, buf[16];
1052 int ret;
1054 p = *pp;
1055 q = buf;
1056 while (*p != '\0' && !is_space(*p)) {
1057 if ((q - buf) < sizeof(buf) - 1)
1058 *q++ = toup(*p);
1059 p++;
1061 *q = '\0';
1062 ret = !strcmp(buf, cmd);
1063 *pp = p;
1064 return ret;
1067 /* ------------------------------------------------------------- */
1068 int pe_load_def_file(TCCState * s1, FILE * fp)
1070 DLLReference *dllref;
1071 int f = 0, sym_index;
1072 char *p, line[120], dllname[40];
1073 while (fgets(line, sizeof line, fp)) {
1074 p = strchr(line, 0);
1075 while (p > line && p[-1] <= ' ')
1076 --p;
1077 *p = 0;
1078 p = line;
1079 while (*p && *p <= ' ')
1080 ++p;
1082 if (*p && ';' != *p)
1083 switch (f) {
1084 case 0:
1085 if (!pe_test_cmd((const char **)&p, "LIBRARY"))
1086 return -1;
1087 while (is_space(*p))
1088 p++;
1089 pstrcpy(dllname, sizeof(dllname), p);
1090 ++f;
1091 continue;
1093 case 1:
1094 if (!pe_test_cmd((const char **)&p, "EXPORTS"))
1095 return -1;
1096 ++f;
1097 continue;
1099 case 2:
1100 dllref =
1101 tcc_malloc(sizeof(DLLReference) + strlen(dllname));
1102 strcpy(dllref->name, dllname);
1103 dllref->level = 0;
1104 dynarray_add((void ***) &s1->loaded_dlls,
1105 &s1->nb_loaded_dlls, dllref);
1106 ++f;
1108 default:
1109 /* tccpe needs to know from what dll it should import
1110 the sym */
1111 sym_index = add_elf_sym(s1->dynsymtab_section,
1112 0, 0, ELF32_ST_INFO(STB_GLOBAL,
1113 STT_FUNC),
1114 s1->nb_loaded_dlls - 1,
1115 text_section->sh_num, p);
1116 continue;
1119 return 0;
1122 /* ------------------------------------------------------------- */
1123 unsigned long pe_add_runtime(TCCState * s1)
1125 const char *start_symbol;
1126 unsigned long addr;
1128 if (find_elf_sym(symtab_section, "WinMain"))
1129 pe_type = PE_GUI;
1131 start_symbol =
1132 TCC_OUTPUT_MEMORY == s1->output_type
1133 ? PE_GUI == pe_type ? "_runwinmain" : NULL
1134 : PE_DLL == pe_type ? "_dllstart"
1135 : PE_GUI == pe_type ? "_winstart" : "_start";
1137 /* grab the startup code from libtcc1 */
1138 if (start_symbol)
1139 add_elf_sym(symtab_section,
1140 0, 0,
1141 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1142 SHN_UNDEF, start_symbol);
1144 if (0 == s1->nostdlib) {
1145 tcc_add_library(s1, "tcc1");
1146 tcc_add_library(s1, "msvcrt");
1147 if (PE_DLL == pe_type || PE_GUI == pe_type) {
1148 tcc_add_library(s1, "kernel32");
1149 tcc_add_library(s1, "user32");
1150 tcc_add_library(s1, "gdi32");
1154 addr = start_symbol ?
1155 (unsigned long) tcc_get_symbol_err(s1, start_symbol) : 0;
1157 if (s1->output_type == TCC_OUTPUT_MEMORY && addr) {
1158 /* for -run GUI's, put '_runwinmain' instead of 'main' */
1159 add_elf_sym(symtab_section,
1160 addr, 0,
1161 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
1162 text_section->sh_num, "main");
1164 /* FreeConsole(); */
1166 return addr;
1169 int tcc_output_pe(TCCState * s1, const char *filename)
1171 int ret;
1172 struct pe_info pe;
1173 int i;
1174 memset(&pe, 0, sizeof pe);
1175 pe.filename = filename;
1176 pe.s1 = s1;
1177 pe.start_addr = pe_add_runtime(s1);
1179 relocate_common_syms(); /* assign bss adresses */
1180 ret = pe_check_symbols(&pe);
1181 if (0 == ret) {
1182 pe_assign_addresses(&pe);
1183 relocate_syms(s1, 0);
1184 for (i = 1; i < s1->nb_sections; ++i) {
1185 Section *s = s1->sections[i];
1186 if (s->reloc)
1187 relocate_section(s1, s);
1189 ret = pe_write_pe(&pe);
1191 #ifdef PE_PRINT_SECTIONS
1193 Section *s;
1194 FILE *f;
1195 f = fopen("tccpe.log", "wb");
1196 for (i = 1; i < s1->nb_sections; ++i) {
1197 s = s1->sections[i];
1198 pe_print_section(f, s);
1200 pe_print_section(f, s1->dynsymtab_section);
1201 fclose(f);
1203 #endif
1204 return ret;
1207 /*----------------------------------------------------------------------------*/