d3d9: Pass a wined3d_swapchain_desc structure to swapchain_init().
[wine/multimedia.git] / tools / winebuild / spec32.c
blob3dff304a5d2351d1245c55cbc8900c80e3a40467
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_POWERPC 0x01f0
38 #define IMAGE_FILE_MACHINE_AMD64 0x8664
39 #define IMAGE_FILE_MACHINE_ARMV7 0x01C4
40 /* Wine extension */
41 #define IMAGE_FILE_MACHINE_SPARC 0x2000
43 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
44 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
46 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
47 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
48 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
50 /* check if entry point needs a relay thunk */
51 static inline int needs_relay( const ORDDEF *odp )
53 /* skip nonexistent entry points */
54 if (!odp) return 0;
55 /* skip non-functions */
56 switch (odp->type)
58 case TYPE_STDCALL:
59 case TYPE_CDECL:
60 case TYPE_THISCALL:
61 break;
62 case TYPE_STUB:
63 if (odp->u.func.nb_args != -1) break;
64 /* fall through */
65 default:
66 return 0;
68 /* skip norelay and forward entry points */
69 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
70 /* skip register entry points on x86_64 */
71 if (target_cpu == CPU_x86_64 && (odp->flags & FLAG_REGISTER)) return 0;
72 return 1;
75 static int is_float_arg( const ORDDEF *odp, int arg )
77 if (arg >= odp->u.func.nb_args) return 0;
78 return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
81 /* check if dll will output relay thunks */
82 int has_relays( DLLSPEC *spec )
84 int i;
86 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
88 for (i = spec->base; i <= spec->limit; i++)
90 ORDDEF *odp = spec->ordinals[i];
91 if (needs_relay( odp )) return 1;
93 return 0;
96 /*******************************************************************
97 * output_relay_debug
99 * Output entry points for relay debugging
101 static void output_relay_debug( DLLSPEC *spec )
103 int i, j;
104 unsigned int pos, args, flags;
106 /* first the table of entry point offsets */
108 output( "\t%s\n", get_asm_rodata_section() );
109 output( "\t.align %d\n", get_alignment(4) );
110 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
112 for (i = spec->base; i <= spec->limit; i++)
114 ORDDEF *odp = spec->ordinals[i];
116 if (needs_relay( odp ))
117 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
118 else
119 output( "\t.long 0\n" );
122 /* then the table of argument types */
124 output( "\t.align %d\n", get_alignment(4) );
125 output( ".L__wine_spec_relay_arg_types:\n" );
127 for (i = spec->base; i <= spec->limit; i++)
129 ORDDEF *odp = spec->ordinals[i];
130 unsigned int mask = 0;
132 if (needs_relay( odp ))
134 for (j = pos = 0; pos < 16 && j < odp->u.func.nb_args; j++)
136 switch (odp->u.func.args[j])
138 case ARG_STR: mask |= 1 << (2 * pos++); break;
139 case ARG_WSTR: mask |= 2 << (2 * pos++); break;
140 case ARG_INT64:
141 case ARG_DOUBLE: pos += 8 / get_ptr_size(); break;
142 case ARG_INT128: pos += (target_cpu == CPU_x86) ? 4 : 1; break;
143 default: pos++; break;
147 output( "\t.long 0x%08x\n", mask );
150 /* then the relay thunks */
152 output( "\t.text\n" );
153 output( "__wine_spec_relay_entry_points:\n" );
154 output( "\tnop\n" ); /* to avoid 0 offset */
156 for (i = spec->base; i <= spec->limit; i++)
158 ORDDEF *odp = spec->ordinals[i];
160 if (!needs_relay( odp )) continue;
162 output( "\t.align %d\n", get_alignment(4) );
163 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
164 output_cfi( ".cfi_startproc" );
166 args = get_args_size(odp) / get_ptr_size();
167 flags = 0;
169 switch (target_cpu)
171 case CPU_x86:
172 if (odp->type == TYPE_THISCALL) /* add the this pointer */
174 output( "\tpopl %%eax\n" );
175 output( "\tpushl %%ecx\n" );
176 output( "\tpushl %%eax\n" );
177 flags |= 2;
179 if (odp->flags & FLAG_REGISTER)
180 output( "\tpushl %%eax\n" );
181 else
182 output( "\tpushl %%esp\n" );
183 output_cfi( ".cfi_adjust_cfa_offset 4" );
185 if (odp->flags & FLAG_RET64) flags |= 1;
186 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
187 output_cfi( ".cfi_adjust_cfa_offset 4" );
189 if (UsePIC)
191 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
192 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
194 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
195 output( "\tpushl %%eax\n" );
196 output_cfi( ".cfi_adjust_cfa_offset 4" );
198 if (odp->flags & FLAG_REGISTER)
200 output( "\tcall *8(%%eax)\n" );
202 else
204 output( "\tcall *4(%%eax)\n" );
205 output_cfi( ".cfi_adjust_cfa_offset -12" );
206 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
207 output( "\tret $%u\n", args * get_ptr_size() );
208 else
209 output( "\tret\n" );
211 break;
213 case CPU_x86_64:
214 output( "\tsubq $40,%%rsp\n" );
215 output_cfi( ".cfi_adjust_cfa_offset 40" );
216 switch (args)
218 default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
219 /* fall through */
220 case 3: output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
221 /* fall through */
222 case 2: output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
223 /* fall through */
224 case 1: output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
225 /* fall through */
226 case 0: break;
228 output( "\tleaq 40(%%rsp),%%r8\n" );
229 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
230 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
231 output( "\tcallq *8(%%rcx)\n" );
232 output( "\taddq $40,%%rsp\n" );
233 output_cfi( ".cfi_adjust_cfa_offset -40" );
234 output( "\tret\n" );
235 break;
237 default:
238 assert(0);
240 output_cfi( ".cfi_endproc" );
244 /*******************************************************************
245 * output_exports
247 * Output the export table for a Win32 module.
249 void output_exports( DLLSPEC *spec )
251 int i, fwd_size = 0;
252 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
254 if (!nr_exports) return;
256 output( "\n/* export table */\n\n" );
257 output( "\t.data\n" );
258 output( "\t.align %d\n", get_alignment(4) );
259 output( ".L__wine_spec_exports:\n" );
261 /* export directory header */
263 output( "\t.long 0\n" ); /* Characteristics */
264 output( "\t.long 0\n" ); /* TimeDateStamp */
265 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
266 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
267 output( "\t.long %u\n", spec->base ); /* Base */
268 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
269 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
270 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
271 if (spec->nb_names)
273 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
274 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
276 else
278 output( "\t.long 0\n" ); /* AddressOfNames */
279 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
282 /* output the function pointers */
284 output( "\n.L__wine_spec_exports_funcs:\n" );
285 for (i = spec->base; i <= spec->limit; i++)
287 ORDDEF *odp = spec->ordinals[i];
288 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
289 else switch(odp->type)
291 case TYPE_EXTERN:
292 case TYPE_STDCALL:
293 case TYPE_VARARGS:
294 case TYPE_CDECL:
295 case TYPE_THISCALL:
296 if (odp->flags & FLAG_FORWARD)
298 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
299 fwd_size += strlen(odp->link_name) + 1;
301 else if (odp->flags & FLAG_EXT_LINK)
303 output( "\t%s %s_%s\n",
304 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
306 else
308 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
310 break;
311 case TYPE_STUB:
312 output( "\t%s %s\n", get_asm_ptr_keyword(),
313 asm_name( get_stub_name( odp, spec )) );
314 break;
315 default:
316 assert(0);
320 if (spec->nb_names)
322 /* output the function name pointers */
324 int namepos = strlen(spec->file_name) + 1;
326 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
327 for (i = 0; i < spec->nb_names; i++)
329 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
330 namepos += strlen(spec->names[i]->name) + 1;
333 /* output the function ordinals */
335 output( "\n.L__wine_spec_exp_ordinals:\n" );
336 for (i = 0; i < spec->nb_names; i++)
338 output( "\t%s %d\n",
339 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
341 if (spec->nb_names % 2)
343 output( "\t%s 0\n", get_asm_short_keyword() );
347 /* output the export name strings */
349 output( "\n.L__wine_spec_exp_names:\n" );
350 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
351 for (i = 0; i < spec->nb_names; i++)
352 output( "\t%s \"%s\"\n",
353 get_asm_string_keyword(), spec->names[i]->name );
355 /* output forward strings */
357 if (fwd_size)
359 output( "\n.L__wine_spec_forwards:\n" );
360 for (i = spec->base; i <= spec->limit; i++)
362 ORDDEF *odp = spec->ordinals[i];
363 if (odp && (odp->flags & FLAG_FORWARD))
364 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
367 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
368 output( ".L__wine_spec_exports_end:\n" );
370 /* output relays */
372 if (!has_relays( spec ))
374 output( "\t%s 0\n", get_asm_ptr_keyword() );
375 return;
378 output( ".L__wine_spec_relay_descr:\n" );
379 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
380 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
381 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
382 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
383 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
384 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
386 output_relay_debug( spec );
390 /*******************************************************************
391 * output_asm_constructor
393 * Output code for calling a dll constructor.
395 static void output_asm_constructor( const char *constructor )
397 if (target_platform == PLATFORM_APPLE)
399 /* Mach-O doesn't have an init section */
400 output( "\n\t.mod_init_func\n" );
401 output( "\t.align %d\n", get_alignment(4) );
402 output( "\t.long %s\n", asm_name(constructor) );
404 else
406 switch(target_cpu)
408 case CPU_x86:
409 case CPU_x86_64:
410 output( "\n\t.section \".init\",\"ax\"\n" );
411 output( "\tcall %s\n", asm_name(constructor) );
412 break;
413 case CPU_SPARC:
414 output( "\n\t.section \".init\",\"ax\"\n" );
415 output( "\tcall %s\n", asm_name(constructor) );
416 output( "\tnop\n" );
417 break;
418 case CPU_ARM:
419 output( "\n\t.section \".text\",\"ax\"\n" );
420 output( "\tblx %s\n", asm_name(constructor) );
421 break;
422 case CPU_POWERPC:
423 output( "\n\t.section \".init\",\"ax\"\n" );
424 output( "\tbl %s\n", asm_name(constructor) );
425 break;
431 /*******************************************************************
432 * output_module
434 * Output the module data.
436 void output_module( DLLSPEC *spec )
438 int machine = 0;
439 unsigned int page_size = get_page_size();
441 /* Reserve some space for the PE header */
443 switch (target_platform)
445 case PLATFORM_APPLE:
446 output( "\t.text\n" );
447 output( "\t.align %d\n", get_alignment(page_size) );
448 output( "__wine_spec_pe_header:\n" );
449 output( "\t.space 65536\n" );
450 break;
451 case PLATFORM_SOLARIS:
452 output( "\n\t.section \".text\",\"ax\"\n" );
453 output( "__wine_spec_pe_header:\n" );
454 output( "\t.skip %u\n", 65536 + page_size );
455 break;
456 default:
457 switch(target_cpu)
459 case CPU_x86:
460 case CPU_x86_64:
461 case CPU_SPARC:
462 output( "\n\t.section \".init\",\"ax\"\n" );
463 output( "\tjmp 1f\n" );
464 break;
465 case CPU_ARM:
466 output( "\n\t.section \".text\",\"ax\"\n" );
467 output( "\tb 1f\n" );
468 break;
469 case CPU_POWERPC:
470 output( "\n\t.section \".init\",\"ax\"\n" );
471 output( "\tb 1f\n" );
472 break;
474 output( "__wine_spec_pe_header:\n" );
475 output( "\t.skip %u\n", 65536 + page_size );
476 output( "1:\n" );
477 break;
480 /* Output the NT header */
482 output( "\n\t.data\n" );
483 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
484 output( "%s\n", asm_globl("__wine_spec_nt_header") );
485 output( ".L__wine_spec_rva_base:\n" );
487 output( "\t.long 0x4550\n" ); /* Signature */
488 switch(target_cpu)
490 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
491 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
492 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMV7; break;
493 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
494 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_SPARC; break;
496 output( "\t%s 0x%04x\n", /* Machine */
497 get_asm_short_keyword(), machine );
498 output( "\t%s 0\n", /* NumberOfSections */
499 get_asm_short_keyword() );
500 output( "\t.long 0\n" ); /* TimeDateStamp */
501 output( "\t.long 0\n" ); /* PointerToSymbolTable */
502 output( "\t.long 0\n" ); /* NumberOfSymbols */
503 output( "\t%s %d\n", /* SizeOfOptionalHeader */
504 get_asm_short_keyword(),
505 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
506 output( "\t%s 0x%04x\n", /* Characteristics */
507 get_asm_short_keyword(), spec->characteristics );
508 output( "\t%s 0x%04x\n", /* Magic */
509 get_asm_short_keyword(),
510 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
511 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
512 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
513 output( "\t.long 0\n" ); /* SizeOfCode */
514 output( "\t.long 0\n" ); /* SizeOfInitializedData */
515 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
516 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
517 output( "\t%s %s\n", /* AddressOfEntryPoint */
518 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
519 if (get_ptr_size() == 4)
521 output( "\t.long 0\n" ); /* BaseOfCode */
522 output( "\t.long 0\n" ); /* BaseOfData */
524 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
525 get_asm_ptr_keyword() );
526 output( "\t.long %u\n", page_size ); /* SectionAlignment */
527 output( "\t.long %u\n", page_size ); /* FileAlignment */
528 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
529 get_asm_short_keyword() );
530 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
531 get_asm_short_keyword() );
532 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
533 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
534 output( "\t.long 0\n" ); /* Win32VersionValue */
535 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
536 asm_name("_end") );
537 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
538 output( "\t.long 0\n" ); /* CheckSum */
539 output( "\t%s 0x%04x\n", /* Subsystem */
540 get_asm_short_keyword(), spec->subsystem );
541 output( "\t%s 0x%04x\n", /* DllCharacteristics */
542 get_asm_short_keyword(), spec->dll_characteristics );
543 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
544 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
545 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
546 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
547 output( "\t.long 0\n" ); /* LoaderFlags */
548 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
550 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
551 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
552 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
553 else
554 output( "\t.long 0,0\n" );
556 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
557 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
558 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
559 else
560 output( "\t.long 0,0\n" );
562 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
563 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
564 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
565 else
566 output( "\t.long 0,0\n" );
568 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
569 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
570 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
571 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
572 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
573 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
574 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
575 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
576 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
577 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
578 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
579 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
580 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
582 output( "\n\t%s\n", get_asm_string_section() );
583 output( "%s\n", asm_globl("__wine_spec_file_name") );
584 output( ".L__wine_spec_file_name:\n" );
585 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
586 if (target_platform == PLATFORM_APPLE)
587 output( "\t.lcomm %s,4\n", asm_name("_end") );
589 output_asm_constructor( "__wine_spec_init_ctor" );
593 /*******************************************************************
594 * BuildSpec32File
596 * Build a Win32 C file from a spec file.
598 void BuildSpec32File( DLLSPEC *spec )
600 resolve_imports( spec );
601 output_standard_file_header();
602 output_module( spec );
603 output_stubs( spec );
604 output_exports( spec );
605 output_imports( spec );
606 if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
607 output_resources( spec );
608 output_gnu_stack_note();
612 /*******************************************************************
613 * output_fake_module
615 * Build a fake binary module from a spec file.
617 void output_fake_module( DLLSPEC *spec )
619 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
620 0xc2, 0x0c, 0x00 }; /* ret $12 */
622 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
623 0xc2, 0x04, 0x00 }; /* ret $4 */
625 static const char fakedll_signature[] = "Wine placeholder DLL";
626 const unsigned int page_size = get_page_size();
627 const unsigned int section_align = page_size;
628 const unsigned int file_align = 0x200;
629 const unsigned int reloc_size = 8;
630 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
631 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
632 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
633 sizeof(dll_code_section) : sizeof(exe_code_section);
634 unsigned char *resources;
635 unsigned int resources_size;
636 unsigned int image_size = 3 * section_align;
638 resolve_imports( spec );
639 output_bin_resources( spec, 3 * section_align );
640 resources = output_buffer;
641 resources_size = output_buffer_pos;
642 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
644 init_output_buffer();
646 put_word( 0x5a4d ); /* e_magic */
647 put_word( 0x40 ); /* e_cblp */
648 put_word( 0x01 ); /* e_cp */
649 put_word( 0 ); /* e_crlc */
650 put_word( lfanew / 16 ); /* e_cparhdr */
651 put_word( 0x0000 ); /* e_minalloc */
652 put_word( 0xffff ); /* e_maxalloc */
653 put_word( 0x0000 ); /* e_ss */
654 put_word( 0x00b8 ); /* e_sp */
655 put_word( 0 ); /* e_csum */
656 put_word( 0 ); /* e_ip */
657 put_word( 0 ); /* e_cs */
658 put_word( lfanew ); /* e_lfarlc */
659 put_word( 0 ); /* e_ovno */
660 put_dword( 0 ); /* e_res */
661 put_dword( 0 );
662 put_word( 0 ); /* e_oemid */
663 put_word( 0 ); /* e_oeminfo */
664 put_dword( 0 ); /* e_res2 */
665 put_dword( 0 );
666 put_dword( 0 );
667 put_dword( 0 );
668 put_dword( 0 );
669 put_dword( lfanew );
671 put_data( fakedll_signature, sizeof(fakedll_signature) );
672 align_output( 16 );
674 put_dword( 0x4550 ); /* Signature */
675 switch(target_cpu)
677 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
678 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
679 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
680 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_SPARC ); break;
681 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMV7 ); break;
683 put_word( nb_sections ); /* NumberOfSections */
684 put_dword( 0 ); /* TimeDateStamp */
685 put_dword( 0 ); /* PointerToSymbolTable */
686 put_dword( 0 ); /* NumberOfSymbols */
687 put_word( get_ptr_size() == 8 ?
688 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
689 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
690 put_word( spec->characteristics ); /* Characteristics */
691 put_word( get_ptr_size() == 8 ?
692 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
693 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
694 put_byte( 0 ); /* MajorLinkerVersion */
695 put_byte( 0 ); /* MinorLinkerVersion */
696 put_dword( text_size ); /* SizeOfCode */
697 put_dword( 0 ); /* SizeOfInitializedData */
698 put_dword( 0 ); /* SizeOfUninitializedData */
699 put_dword( section_align ); /* AddressOfEntryPoint */
700 put_dword( section_align ); /* BaseOfCode */
701 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
702 put_pword( 0x10000000 ); /* ImageBase */
703 put_dword( section_align ); /* SectionAlignment */
704 put_dword( file_align ); /* FileAlignment */
705 put_word( 1 ); /* MajorOperatingSystemVersion */
706 put_word( 0 ); /* MinorOperatingSystemVersion */
707 put_word( 0 ); /* MajorImageVersion */
708 put_word( 0 ); /* MinorImageVersion */
709 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
710 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
711 put_dword( 0 ); /* Win32VersionValue */
712 put_dword( image_size ); /* SizeOfImage */
713 put_dword( file_align ); /* SizeOfHeaders */
714 put_dword( 0 ); /* CheckSum */
715 put_word( spec->subsystem ); /* Subsystem */
716 put_word( spec->dll_characteristics ); /* DllCharacteristics */
717 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
718 put_pword( page_size ); /* SizeOfStackCommit */
719 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
720 put_pword( page_size ); /* SizeOfHeapCommit */
721 put_dword( 0 ); /* LoaderFlags */
722 put_dword( 16 ); /* NumberOfRvaAndSizes */
724 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
725 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
726 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
728 put_dword( 3 * section_align );
729 put_dword( resources_size );
731 else
733 put_dword( 0 );
734 put_dword( 0 );
737 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
738 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
739 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
740 put_dword( reloc_size );
741 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
742 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
743 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
744 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
745 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
746 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
747 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
748 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
749 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
750 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
752 /* .text section */
753 put_data( ".text\0\0", 8 ); /* Name */
754 put_dword( section_align ); /* VirtualSize */
755 put_dword( section_align ); /* VirtualAddress */
756 put_dword( text_size ); /* SizeOfRawData */
757 put_dword( file_align ); /* PointerToRawData */
758 put_dword( 0 ); /* PointerToRelocations */
759 put_dword( 0 ); /* PointerToLinenumbers */
760 put_word( 0 ); /* NumberOfRelocations */
761 put_word( 0 ); /* NumberOfLinenumbers */
762 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
764 /* .reloc section */
765 put_data( ".reloc\0", 8 ); /* Name */
766 put_dword( section_align ); /* VirtualSize */
767 put_dword( 2 * section_align );/* VirtualAddress */
768 put_dword( reloc_size ); /* SizeOfRawData */
769 put_dword( 2 * file_align ); /* PointerToRawData */
770 put_dword( 0 ); /* PointerToRelocations */
771 put_dword( 0 ); /* PointerToLinenumbers */
772 put_word( 0 ); /* NumberOfRelocations */
773 put_word( 0 ); /* NumberOfLinenumbers */
774 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
776 /* .rsrc section */
777 if (resources_size)
779 put_data( ".rsrc\0\0", 8 ); /* Name */
780 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
781 put_dword( 3 * section_align );/* VirtualAddress */
782 put_dword( resources_size ); /* SizeOfRawData */
783 put_dword( 3 * file_align ); /* PointerToRawData */
784 put_dword( 0 ); /* PointerToRelocations */
785 put_dword( 0 ); /* PointerToLinenumbers */
786 put_word( 0 ); /* NumberOfRelocations */
787 put_word( 0 ); /* NumberOfLinenumbers */
788 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
791 /* .text contents */
792 align_output( file_align );
793 if (spec->characteristics & IMAGE_FILE_DLL)
794 put_data( dll_code_section, sizeof(dll_code_section) );
795 else
796 put_data( exe_code_section, sizeof(exe_code_section) );
798 /* .reloc contents */
799 align_output( file_align );
800 put_dword( 0 ); /* VirtualAddress */
801 put_dword( 0 ); /* SizeOfBlock */
803 /* .rsrc contents */
804 if (resources_size)
806 align_output( file_align );
807 put_data( resources, resources_size );
809 flush_output_buffer();
813 /*******************************************************************
814 * output_def_file
816 * Build a Win32 def file from a spec file.
818 void output_def_file( DLLSPEC *spec, int include_private )
820 DLLSPEC *spec32 = NULL;
821 const char *name;
822 int i, total;
824 if (spec->type == SPEC_WIN16)
826 spec32 = alloc_dll_spec();
827 add_16bit_exports( spec32, spec );
828 spec = spec32;
831 if (spec_file_name)
832 output( "; File generated automatically from %s; do not edit!\n\n",
833 spec_file_name );
834 else
835 output( "; File generated automatically; do not edit!\n\n" );
837 output( "LIBRARY %s\n\n", spec->file_name);
838 output( "EXPORTS\n");
840 /* Output the exports and relay entry points */
842 for (i = total = 0; i < spec->nb_entry_points; i++)
844 const ORDDEF *odp = &spec->entry_points[i];
845 int is_data = 0;
847 if (!odp) continue;
849 if (odp->name) name = odp->name;
850 else if (odp->export_name) name = odp->export_name;
851 else continue;
853 if (!(odp->flags & FLAG_PRIVATE)) total++;
854 else if (!include_private) continue;
856 if (odp->type == TYPE_STUB) continue;
858 output( " %s", name );
860 switch(odp->type)
862 case TYPE_EXTERN:
863 is_data = 1;
864 /* fall through */
865 case TYPE_VARARGS:
866 case TYPE_CDECL:
867 case TYPE_THISCALL:
868 /* try to reduce output */
869 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
870 output( "=%s", odp->link_name );
871 break;
872 case TYPE_STDCALL:
874 int at_param = get_args_size( odp );
875 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
876 if (odp->flags & FLAG_FORWARD)
878 output( "=%s", odp->link_name );
880 else if (strcmp(name, odp->link_name)) /* try to reduce output */
882 output( "=%s", odp->link_name );
883 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
885 break;
887 default:
888 assert(0);
890 output( " @%d", odp->ordinal );
891 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
892 if (is_data) output( " DATA" );
893 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
894 output( "\n" );
896 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
897 if (spec32) free_dll_spec( spec32 );