winebuild: Fix ARM support.
[wine/multimedia.git] / tools / winebuild / spec32.c
blob29436f1574245096d978841f0f19efa16f4c02c0
1 /*
2 * 32-bit spec files
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
33 #include "build.h"
35 #define IMAGE_FILE_MACHINE_UNKNOWN 0
36 #define IMAGE_FILE_MACHINE_I386 0x014c
37 #define IMAGE_FILE_MACHINE_ALPHA 0x0184
38 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
39 #define IMAGE_FILE_MACHINE_AMD64 0x8664
40 #define IMAGE_FILE_MACHINE_ARM 0x01C0
42 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
43 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
45 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
46 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
47 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
49 /* check if entry point needs a relay thunk */
50 static inline int needs_relay( const ORDDEF *odp )
52 /* skip nonexistent entry points */
53 if (!odp) return 0;
54 /* skip non-functions */
55 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL && odp->type != TYPE_THISCALL) return 0;
56 /* skip norelay and forward entry points */
57 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
58 return 1;
61 static int is_float_arg( const ORDDEF *odp, unsigned int arg )
63 if (arg >= odp->u.func.nb_args) return 0;
64 return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
67 /* check if dll will output relay thunks */
68 int has_relays( DLLSPEC *spec )
70 int i;
72 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
74 for (i = spec->base; i <= spec->limit; i++)
76 ORDDEF *odp = spec->ordinals[i];
77 if (needs_relay( odp )) return 1;
79 return 0;
82 /*******************************************************************
83 * output_relay_debug
85 * Output entry points for relay debugging
87 static void output_relay_debug( DLLSPEC *spec )
89 int i;
90 unsigned int j, pos, args, flags;
92 /* first the table of entry point offsets */
94 output( "\t%s\n", get_asm_rodata_section() );
95 output( "\t.align %d\n", get_alignment(4) );
96 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
98 for (i = spec->base; i <= spec->limit; i++)
100 ORDDEF *odp = spec->ordinals[i];
102 if (needs_relay( odp ))
103 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
104 else
105 output( "\t.long 0\n" );
108 /* then the table of argument types */
110 output( "\t.align %d\n", get_alignment(4) );
111 output( ".L__wine_spec_relay_arg_types:\n" );
113 for (i = spec->base; i <= spec->limit; i++)
115 ORDDEF *odp = spec->ordinals[i];
116 unsigned int mask = 0;
118 if (needs_relay( odp ))
120 for (j = pos = 0; pos < 16 && j < odp->u.func.nb_args; j++)
122 switch (odp->u.func.args[j])
124 case ARG_STR: mask |= 1 << (2 * pos++); break;
125 case ARG_WSTR: mask |= 2 << (2 * pos++); break;
126 case ARG_INT64:
127 case ARG_DOUBLE: pos += 8 / get_ptr_size(); break;
128 case ARG_INT128: pos += (target_cpu == CPU_x86) ? 4 : 1; break;
129 default: pos++; break;
133 output( "\t.long 0x%08x\n", mask );
136 /* then the relay thunks */
138 output( "\t.text\n" );
139 output( "__wine_spec_relay_entry_points:\n" );
140 output( "\tnop\n" ); /* to avoid 0 offset */
142 for (i = spec->base; i <= spec->limit; i++)
144 ORDDEF *odp = spec->ordinals[i];
146 if (!needs_relay( odp )) continue;
148 output( "\t.align %d\n", get_alignment(4) );
149 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
151 args = get_args_size(odp) / get_ptr_size();
152 flags = 0;
154 switch (target_cpu)
156 case CPU_x86:
157 if (odp->type == TYPE_THISCALL) /* add the this pointer */
159 output( "\tpopl %%eax\n" );
160 output( "\tpushl %%ecx\n" );
161 output( "\tpushl %%eax\n" );
162 flags |= 2;
164 if (odp->flags & FLAG_REGISTER)
165 output( "\tpushl %%eax\n" );
166 else
167 output( "\tpushl %%esp\n" );
169 if (odp->flags & FLAG_RET64) flags |= 1;
170 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
172 if (UsePIC)
174 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
175 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
177 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
178 output( "\tpushl %%eax\n" );
180 if (odp->flags & FLAG_REGISTER)
182 output( "\tcall *8(%%eax)\n" );
184 else
186 output( "\tcall *4(%%eax)\n" );
187 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
188 output( "\tret $%u\n", args * get_ptr_size() );
189 else
190 output( "\tret\n" );
192 break;
194 case CPU_x86_64:
195 output( "\t.cfi_startproc\n" );
196 output( "\tsubq $40,%%rsp\n" );
197 output( "\t.cfi_adjust_cfa_offset 40\n" );
198 switch (args)
200 default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
201 case 3: output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
202 case 2: output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
203 case 1: output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
204 case 0: break;
206 output( "\tleaq 40(%%rsp),%%r8\n" );
207 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
208 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
209 output( "\tcallq *%u(%%rcx)\n", (odp->flags & FLAG_REGISTER) ? 16 : 8 );
210 output( "\taddq $40,%%rsp\n" );
211 output( "\t.cfi_adjust_cfa_offset -40\n" );
212 output( "\tret\n" );
213 output( "\t.cfi_endproc\n" );
214 break;
216 default:
217 assert(0);
222 /*******************************************************************
223 * output_exports
225 * Output the export table for a Win32 module.
227 void output_exports( DLLSPEC *spec )
229 int i, fwd_size = 0;
230 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
232 if (!nr_exports) return;
234 output( "\n/* export table */\n\n" );
235 output( "\t.data\n" );
236 output( "\t.align %d\n", get_alignment(4) );
237 output( ".L__wine_spec_exports:\n" );
239 /* export directory header */
241 output( "\t.long 0\n" ); /* Characteristics */
242 output( "\t.long 0\n" ); /* TimeDateStamp */
243 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
244 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
245 output( "\t.long %u\n", spec->base ); /* Base */
246 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
247 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
248 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
249 if (spec->nb_names)
251 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
252 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
254 else
256 output( "\t.long 0\n" ); /* AddressOfNames */
257 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
260 /* output the function pointers */
262 output( "\n.L__wine_spec_exports_funcs:\n" );
263 for (i = spec->base; i <= spec->limit; i++)
265 ORDDEF *odp = spec->ordinals[i];
266 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
267 else switch(odp->type)
269 case TYPE_EXTERN:
270 case TYPE_STDCALL:
271 case TYPE_VARARGS:
272 case TYPE_CDECL:
273 case TYPE_THISCALL:
274 if (odp->flags & FLAG_FORWARD)
276 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
277 fwd_size += strlen(odp->link_name) + 1;
279 else if (odp->flags & FLAG_EXT_LINK)
281 output( "\t%s %s_%s\n",
282 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
284 else
286 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
288 break;
289 case TYPE_STUB:
290 output( "\t%s %s\n", get_asm_ptr_keyword(),
291 asm_name( get_stub_name( odp, spec )) );
292 break;
293 default:
294 assert(0);
298 if (spec->nb_names)
300 /* output the function name pointers */
302 int namepos = strlen(spec->file_name) + 1;
304 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
305 for (i = 0; i < spec->nb_names; i++)
307 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
308 namepos += strlen(spec->names[i]->name) + 1;
311 /* output the function ordinals */
313 output( "\n.L__wine_spec_exp_ordinals:\n" );
314 for (i = 0; i < spec->nb_names; i++)
316 output( "\t%s %d\n",
317 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
319 if (spec->nb_names % 2)
321 output( "\t%s 0\n", get_asm_short_keyword() );
325 /* output the export name strings */
327 output( "\n.L__wine_spec_exp_names:\n" );
328 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
329 for (i = 0; i < spec->nb_names; i++)
330 output( "\t%s \"%s\"\n",
331 get_asm_string_keyword(), spec->names[i]->name );
333 /* output forward strings */
335 if (fwd_size)
337 output( "\n.L__wine_spec_forwards:\n" );
338 for (i = spec->base; i <= spec->limit; i++)
340 ORDDEF *odp = spec->ordinals[i];
341 if (odp && (odp->flags & FLAG_FORWARD))
342 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
345 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
346 output( ".L__wine_spec_exports_end:\n" );
348 /* output relays */
350 if (!has_relays( spec ))
352 output( "\t%s 0\n", get_asm_ptr_keyword() );
353 return;
356 output( ".L__wine_spec_relay_descr:\n" );
357 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
358 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
359 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
360 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
361 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
362 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
364 output_relay_debug( spec );
368 /*******************************************************************
369 * output_asm_constructor
371 * Output code for calling a dll constructor.
373 static void output_asm_constructor( const char *constructor )
375 if (target_platform == PLATFORM_APPLE)
377 /* Mach-O doesn't have an init section */
378 output( "\n\t.mod_init_func\n" );
379 output( "\t.align %d\n", get_alignment(4) );
380 output( "\t.long %s\n", asm_name(constructor) );
382 else
384 output( "\n\t.section \".init\",\"ax\"\n" );
385 switch(target_cpu)
387 case CPU_x86:
388 case CPU_x86_64:
389 output( "\tcall %s\n", asm_name(constructor) );
390 break;
391 case CPU_SPARC:
392 output( "\tcall %s\n", asm_name(constructor) );
393 output( "\tnop\n" );
394 break;
395 case CPU_ALPHA:
396 output( "\tjsr $26,%s\n", asm_name(constructor) );
397 break;
398 case CPU_ARM:
399 case CPU_POWERPC:
400 output( "\tbl %s\n", asm_name(constructor) );
401 break;
407 /*******************************************************************
408 * output_module
410 * Output the module data.
412 void output_module( DLLSPEC *spec )
414 int machine = 0;
415 unsigned int page_size = get_page_size();
417 /* Reserve some space for the PE header */
419 switch (target_platform)
421 case PLATFORM_APPLE:
422 output( "\t.text\n" );
423 output( "\t.align %d\n", get_alignment(page_size) );
424 output( "__wine_spec_pe_header:\n" );
425 output( "\t.space 65536\n" );
426 break;
427 case PLATFORM_SOLARIS:
428 output( "\n\t.section \".text\",\"ax\"\n" );
429 output( "__wine_spec_pe_header:\n" );
430 output( "\t.skip %u\n", 65536 + page_size );
431 break;
432 default:
433 output( "\n\t.section \".init\",\"ax\"\n" );
434 switch(target_cpu)
436 case CPU_x86:
437 case CPU_x86_64:
438 case CPU_ALPHA:
439 case CPU_SPARC:
440 output( "\tjmp 1f\n" );
441 break;
442 case CPU_ARM:
443 case CPU_POWERPC:
444 output( "\tb 1f\n" );
445 break;
447 output( "__wine_spec_pe_header:\n" );
448 output( "\t.skip %u\n", 65536 + page_size );
449 output( "1:\n" );
450 break;
453 /* Output the NT header */
455 output( "\n\t.data\n" );
456 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
457 output( "%s\n", asm_globl("__wine_spec_nt_header") );
458 output( ".L__wine_spec_rva_base:\n" );
460 output( "\t.long 0x4550\n" ); /* Signature */
461 switch(target_cpu)
463 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
464 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
465 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARM; break;
466 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
467 case CPU_ALPHA: machine = IMAGE_FILE_MACHINE_ALPHA; break;
468 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
470 output( "\t%s 0x%04x\n", /* Machine */
471 get_asm_short_keyword(), machine );
472 output( "\t%s 0\n", /* NumberOfSections */
473 get_asm_short_keyword() );
474 output( "\t.long 0\n" ); /* TimeDateStamp */
475 output( "\t.long 0\n" ); /* PointerToSymbolTable */
476 output( "\t.long 0\n" ); /* NumberOfSymbols */
477 output( "\t%s %d\n", /* SizeOfOptionalHeader */
478 get_asm_short_keyword(),
479 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
480 output( "\t%s 0x%04x\n", /* Characteristics */
481 get_asm_short_keyword(), spec->characteristics );
482 output( "\t%s 0x%04x\n", /* Magic */
483 get_asm_short_keyword(),
484 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
485 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
486 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
487 output( "\t.long 0\n" ); /* SizeOfCode */
488 output( "\t.long 0\n" ); /* SizeOfInitializedData */
489 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
490 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
491 output( "\t%s %s\n", /* AddressOfEntryPoint */
492 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
493 if (get_ptr_size() == 4)
495 output( "\t.long 0\n" ); /* BaseOfCode */
496 output( "\t.long 0\n" ); /* BaseOfData */
498 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
499 get_asm_ptr_keyword() );
500 output( "\t.long %u\n", page_size ); /* SectionAlignment */
501 output( "\t.long %u\n", page_size ); /* FileAlignment */
502 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
503 get_asm_short_keyword() );
504 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
505 get_asm_short_keyword() );
506 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
507 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
508 output( "\t.long 0\n" ); /* Win32VersionValue */
509 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
510 asm_name("_end") );
511 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
512 output( "\t.long 0\n" ); /* CheckSum */
513 output( "\t%s 0x%04x\n", /* Subsystem */
514 get_asm_short_keyword(), spec->subsystem );
515 output( "\t%s 0x%04x\n", /* DllCharacteristics */
516 get_asm_short_keyword(), spec->dll_characteristics );
517 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
518 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
519 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
520 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
521 output( "\t.long 0\n" ); /* LoaderFlags */
522 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
524 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
525 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
526 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
527 else
528 output( "\t.long 0,0\n" );
530 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
531 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
532 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
533 else
534 output( "\t.long 0,0\n" );
536 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
537 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
538 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
539 else
540 output( "\t.long 0,0\n" );
542 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
543 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
544 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
545 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
546 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
547 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
548 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
549 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
550 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
551 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
552 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
553 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
554 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
556 output( "\n\t%s\n", get_asm_string_section() );
557 output( "%s\n", asm_globl("__wine_spec_file_name") );
558 output( ".L__wine_spec_file_name:\n" );
559 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
560 if (target_platform == PLATFORM_APPLE)
561 output( "\t.lcomm %s,4\n", asm_name("_end") );
563 output_asm_constructor( "__wine_spec_init_ctor" );
567 /*******************************************************************
568 * BuildSpec32File
570 * Build a Win32 C file from a spec file.
572 void BuildSpec32File( DLLSPEC *spec )
574 resolve_imports( spec );
575 output_standard_file_header();
576 output_module( spec );
577 output_stubs( spec );
578 output_exports( spec );
579 output_imports( spec );
580 if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
581 output_resources( spec );
582 output_gnu_stack_note();
586 /*******************************************************************
587 * output_fake_module
589 * Build a fake binary module from a spec file.
591 void output_fake_module( DLLSPEC *spec )
593 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
594 0xc2, 0x0c, 0x00 }; /* ret $12 */
596 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
597 0xc2, 0x04, 0x00 }; /* ret $4 */
599 static const char fakedll_signature[] = "Wine placeholder DLL";
600 const unsigned int page_size = get_page_size();
601 const unsigned int section_align = page_size;
602 const unsigned int file_align = 0x200;
603 const unsigned int reloc_size = 8;
604 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
605 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
606 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
607 sizeof(dll_code_section) : sizeof(exe_code_section);
608 unsigned char *resources;
609 unsigned int resources_size;
610 unsigned int image_size = 3 * section_align;
612 resolve_imports( spec );
613 output_bin_resources( spec, 3 * section_align );
614 resources = output_buffer;
615 resources_size = output_buffer_pos;
616 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
618 init_output_buffer();
620 put_word( 0x5a4d ); /* e_magic */
621 put_word( 0x40 ); /* e_cblp */
622 put_word( 0x01 ); /* e_cp */
623 put_word( 0 ); /* e_crlc */
624 put_word( lfanew / 16 ); /* e_cparhdr */
625 put_word( 0x0000 ); /* e_minalloc */
626 put_word( 0xffff ); /* e_maxalloc */
627 put_word( 0x0000 ); /* e_ss */
628 put_word( 0x00b8 ); /* e_sp */
629 put_word( 0 ); /* e_csum */
630 put_word( 0 ); /* e_ip */
631 put_word( 0 ); /* e_cs */
632 put_word( lfanew ); /* e_lfarlc */
633 put_word( 0 ); /* e_ovno */
634 put_dword( 0 ); /* e_res */
635 put_dword( 0 );
636 put_word( 0 ); /* e_oemid */
637 put_word( 0 ); /* e_oeminfo */
638 put_dword( 0 ); /* e_res2 */
639 put_dword( 0 );
640 put_dword( 0 );
641 put_dword( 0 );
642 put_dword( 0 );
643 put_dword( lfanew );
645 put_data( fakedll_signature, sizeof(fakedll_signature) );
646 align_output( 16 );
648 put_dword( 0x4550 ); /* Signature */
649 switch(target_cpu)
651 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
652 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
653 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
654 case CPU_ALPHA: put_word( IMAGE_FILE_MACHINE_ALPHA ); break;
655 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_UNKNOWN ); break;
656 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARM ); break;
658 put_word( nb_sections ); /* NumberOfSections */
659 put_dword( 0 ); /* TimeDateStamp */
660 put_dword( 0 ); /* PointerToSymbolTable */
661 put_dword( 0 ); /* NumberOfSymbols */
662 put_word( get_ptr_size() == 8 ?
663 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
664 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
665 put_word( spec->characteristics ); /* Characteristics */
666 put_word( get_ptr_size() == 8 ?
667 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
668 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
669 put_byte( 0 ); /* MajorLinkerVersion */
670 put_byte( 0 ); /* MinorLinkerVersion */
671 put_dword( text_size ); /* SizeOfCode */
672 put_dword( 0 ); /* SizeOfInitializedData */
673 put_dword( 0 ); /* SizeOfUninitializedData */
674 put_dword( section_align ); /* AddressOfEntryPoint */
675 put_dword( section_align ); /* BaseOfCode */
676 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
677 put_pword( 0x10000000 ); /* ImageBase */
678 put_dword( section_align ); /* SectionAlignment */
679 put_dword( file_align ); /* FileAlignment */
680 put_word( 1 ); /* MajorOperatingSystemVersion */
681 put_word( 0 ); /* MinorOperatingSystemVersion */
682 put_word( 0 ); /* MajorImageVersion */
683 put_word( 0 ); /* MinorImageVersion */
684 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
685 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
686 put_dword( 0 ); /* Win32VersionValue */
687 put_dword( image_size ); /* SizeOfImage */
688 put_dword( file_align ); /* SizeOfHeaders */
689 put_dword( 0 ); /* CheckSum */
690 put_word( spec->subsystem ); /* Subsystem */
691 put_word( spec->dll_characteristics ); /* DllCharacteristics */
692 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
693 put_pword( page_size ); /* SizeOfStackCommit */
694 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
695 put_pword( page_size ); /* SizeOfHeapCommit */
696 put_dword( 0 ); /* LoaderFlags */
697 put_dword( 16 ); /* NumberOfRvaAndSizes */
699 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
700 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
701 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
703 put_dword( 3 * section_align );
704 put_dword( resources_size );
706 else
708 put_dword( 0 );
709 put_dword( 0 );
712 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
713 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
714 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
715 put_dword( reloc_size );
716 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
717 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
718 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
719 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
720 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
721 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
722 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
723 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
724 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
725 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
727 /* .text section */
728 put_data( ".text\0\0", 8 ); /* Name */
729 put_dword( section_align ); /* VirtualSize */
730 put_dword( section_align ); /* VirtualAddress */
731 put_dword( text_size ); /* SizeOfRawData */
732 put_dword( file_align ); /* PointerToRawData */
733 put_dword( 0 ); /* PointerToRelocations */
734 put_dword( 0 ); /* PointerToLinenumbers */
735 put_word( 0 ); /* NumberOfRelocations */
736 put_word( 0 ); /* NumberOfLinenumbers */
737 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
739 /* .reloc section */
740 put_data( ".reloc\0", 8 ); /* Name */
741 put_dword( section_align ); /* VirtualSize */
742 put_dword( 2 * section_align );/* VirtualAddress */
743 put_dword( reloc_size ); /* SizeOfRawData */
744 put_dword( 2 * file_align ); /* PointerToRawData */
745 put_dword( 0 ); /* PointerToRelocations */
746 put_dword( 0 ); /* PointerToLinenumbers */
747 put_word( 0 ); /* NumberOfRelocations */
748 put_word( 0 ); /* NumberOfLinenumbers */
749 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
751 /* .rsrc section */
752 if (resources_size)
754 put_data( ".rsrc\0\0", 8 ); /* Name */
755 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
756 put_dword( 3 * section_align );/* VirtualAddress */
757 put_dword( resources_size ); /* SizeOfRawData */
758 put_dword( 3 * file_align ); /* PointerToRawData */
759 put_dword( 0 ); /* PointerToRelocations */
760 put_dword( 0 ); /* PointerToLinenumbers */
761 put_word( 0 ); /* NumberOfRelocations */
762 put_word( 0 ); /* NumberOfLinenumbers */
763 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
766 /* .text contents */
767 align_output( file_align );
768 if (spec->characteristics & IMAGE_FILE_DLL)
769 put_data( dll_code_section, sizeof(dll_code_section) );
770 else
771 put_data( exe_code_section, sizeof(exe_code_section) );
773 /* .reloc contents */
774 align_output( file_align );
775 put_dword( 0 ); /* VirtualAddress */
776 put_dword( 0 ); /* SizeOfBlock */
778 /* .rsrc contents */
779 if (resources_size)
781 align_output( file_align );
782 put_data( resources, resources_size );
784 flush_output_buffer();
788 /*******************************************************************
789 * output_def_file
791 * Build a Win32 def file from a spec file.
793 void output_def_file( DLLSPEC *spec, int include_private )
795 DLLSPEC *spec32 = NULL;
796 const char *name;
797 int i, total;
799 if (spec->type == SPEC_WIN16)
801 spec32 = alloc_dll_spec();
802 add_16bit_exports( spec32, spec );
803 spec = spec32;
806 if (spec_file_name)
807 output( "; File generated automatically from %s; do not edit!\n\n",
808 spec_file_name );
809 else
810 output( "; File generated automatically; do not edit!\n\n" );
812 output( "LIBRARY %s\n\n", spec->file_name);
813 output( "EXPORTS\n");
815 /* Output the exports and relay entry points */
817 for (i = total = 0; i < spec->nb_entry_points; i++)
819 const ORDDEF *odp = &spec->entry_points[i];
820 int is_data = 0;
822 if (!odp) continue;
824 if (odp->name) name = odp->name;
825 else if (odp->export_name) name = odp->export_name;
826 else continue;
828 if (!(odp->flags & FLAG_PRIVATE)) total++;
829 else if (!include_private) continue;
831 if (odp->type == TYPE_STUB) continue;
833 output( " %s", name );
835 switch(odp->type)
837 case TYPE_EXTERN:
838 is_data = 1;
839 /* fall through */
840 case TYPE_VARARGS:
841 case TYPE_CDECL:
842 case TYPE_THISCALL:
843 /* try to reduce output */
844 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
845 output( "=%s", odp->link_name );
846 break;
847 case TYPE_STDCALL:
849 int at_param = get_args_size( odp );
850 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
851 if (odp->flags & FLAG_FORWARD)
853 output( "=%s", odp->link_name );
855 else if (strcmp(name, odp->link_name)) /* try to reduce output */
857 output( "=%s", odp->link_name );
858 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
860 break;
862 default:
863 assert(0);
865 output( " @%d", odp->ordinal );
866 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
867 if (is_data) output( " DATA" );
868 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
869 output( "\n" );
871 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
872 if (spec32) free_dll_spec( spec32 );