ddraw: Separate IDirectDrawSurface2 reference count.
[wine.git] / tools / winebuild / spec32.c
bloba5b4118e06bd906b7c8e1939e37645533e1257db
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_ARM 0x01C0
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 );
165 args = get_args_size(odp) / get_ptr_size();
166 flags = 0;
168 switch (target_cpu)
170 case CPU_x86:
171 if (odp->type == TYPE_THISCALL) /* add the this pointer */
173 output( "\tpopl %%eax\n" );
174 output( "\tpushl %%ecx\n" );
175 output( "\tpushl %%eax\n" );
176 flags |= 2;
178 if (odp->flags & FLAG_REGISTER)
179 output( "\tpushl %%eax\n" );
180 else
181 output( "\tpushl %%esp\n" );
183 if (odp->flags & FLAG_RET64) flags |= 1;
184 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
186 if (UsePIC)
188 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
189 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
191 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
192 output( "\tpushl %%eax\n" );
194 if (odp->flags & FLAG_REGISTER)
196 output( "\tcall *8(%%eax)\n" );
198 else
200 output( "\tcall *4(%%eax)\n" );
201 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
202 output( "\tret $%u\n", args * get_ptr_size() );
203 else
204 output( "\tret\n" );
206 break;
208 case CPU_x86_64:
209 output( "\t.cfi_startproc\n" );
210 output( "\tsubq $40,%%rsp\n" );
211 output( "\t.cfi_adjust_cfa_offset 40\n" );
212 switch (args)
214 default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
215 /* fall through */
216 case 3: output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
217 /* fall through */
218 case 2: output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
219 /* fall through */
220 case 1: output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
221 /* fall through */
222 case 0: break;
224 output( "\tleaq 40(%%rsp),%%r8\n" );
225 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
226 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
227 output( "\tcallq *8(%%rcx)\n" );
228 output( "\taddq $40,%%rsp\n" );
229 output( "\t.cfi_adjust_cfa_offset -40\n" );
230 output( "\tret\n" );
231 output( "\t.cfi_endproc\n" );
232 break;
234 default:
235 assert(0);
240 /*******************************************************************
241 * output_exports
243 * Output the export table for a Win32 module.
245 void output_exports( DLLSPEC *spec )
247 int i, fwd_size = 0;
248 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
250 if (!nr_exports) return;
252 output( "\n/* export table */\n\n" );
253 output( "\t.data\n" );
254 output( "\t.align %d\n", get_alignment(4) );
255 output( ".L__wine_spec_exports:\n" );
257 /* export directory header */
259 output( "\t.long 0\n" ); /* Characteristics */
260 output( "\t.long 0\n" ); /* TimeDateStamp */
261 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
262 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
263 output( "\t.long %u\n", spec->base ); /* Base */
264 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
265 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
266 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
267 if (spec->nb_names)
269 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
270 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
272 else
274 output( "\t.long 0\n" ); /* AddressOfNames */
275 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
278 /* output the function pointers */
280 output( "\n.L__wine_spec_exports_funcs:\n" );
281 for (i = spec->base; i <= spec->limit; i++)
283 ORDDEF *odp = spec->ordinals[i];
284 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
285 else switch(odp->type)
287 case TYPE_EXTERN:
288 case TYPE_STDCALL:
289 case TYPE_VARARGS:
290 case TYPE_CDECL:
291 case TYPE_THISCALL:
292 if (odp->flags & FLAG_FORWARD)
294 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
295 fwd_size += strlen(odp->link_name) + 1;
297 else if (odp->flags & FLAG_EXT_LINK)
299 output( "\t%s %s_%s\n",
300 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
302 else
304 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
306 break;
307 case TYPE_STUB:
308 output( "\t%s %s\n", get_asm_ptr_keyword(),
309 asm_name( get_stub_name( odp, spec )) );
310 break;
311 default:
312 assert(0);
316 if (spec->nb_names)
318 /* output the function name pointers */
320 int namepos = strlen(spec->file_name) + 1;
322 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
323 for (i = 0; i < spec->nb_names; i++)
325 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
326 namepos += strlen(spec->names[i]->name) + 1;
329 /* output the function ordinals */
331 output( "\n.L__wine_spec_exp_ordinals:\n" );
332 for (i = 0; i < spec->nb_names; i++)
334 output( "\t%s %d\n",
335 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
337 if (spec->nb_names % 2)
339 output( "\t%s 0\n", get_asm_short_keyword() );
343 /* output the export name strings */
345 output( "\n.L__wine_spec_exp_names:\n" );
346 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
347 for (i = 0; i < spec->nb_names; i++)
348 output( "\t%s \"%s\"\n",
349 get_asm_string_keyword(), spec->names[i]->name );
351 /* output forward strings */
353 if (fwd_size)
355 output( "\n.L__wine_spec_forwards:\n" );
356 for (i = spec->base; i <= spec->limit; i++)
358 ORDDEF *odp = spec->ordinals[i];
359 if (odp && (odp->flags & FLAG_FORWARD))
360 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
363 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
364 output( ".L__wine_spec_exports_end:\n" );
366 /* output relays */
368 if (!has_relays( spec ))
370 output( "\t%s 0\n", get_asm_ptr_keyword() );
371 return;
374 output( ".L__wine_spec_relay_descr:\n" );
375 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
376 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
377 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
378 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
379 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
380 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
382 output_relay_debug( spec );
386 /*******************************************************************
387 * output_asm_constructor
389 * Output code for calling a dll constructor.
391 static void output_asm_constructor( const char *constructor )
393 if (target_platform == PLATFORM_APPLE)
395 /* Mach-O doesn't have an init section */
396 output( "\n\t.mod_init_func\n" );
397 output( "\t.align %d\n", get_alignment(4) );
398 output( "\t.long %s\n", asm_name(constructor) );
400 else
402 output( "\n\t.section \".init\",\"ax\"\n" );
403 switch(target_cpu)
405 case CPU_x86:
406 case CPU_x86_64:
407 output( "\tcall %s\n", asm_name(constructor) );
408 break;
409 case CPU_SPARC:
410 output( "\tcall %s\n", asm_name(constructor) );
411 output( "\tnop\n" );
412 break;
413 case CPU_ARM:
414 case CPU_POWERPC:
415 output( "\tbl %s\n", asm_name(constructor) );
416 break;
422 /*******************************************************************
423 * output_module
425 * Output the module data.
427 void output_module( DLLSPEC *spec )
429 int machine = 0;
430 unsigned int page_size = get_page_size();
432 /* Reserve some space for the PE header */
434 switch (target_platform)
436 case PLATFORM_APPLE:
437 output( "\t.text\n" );
438 output( "\t.align %d\n", get_alignment(page_size) );
439 output( "__wine_spec_pe_header:\n" );
440 output( "\t.space 65536\n" );
441 break;
442 case PLATFORM_SOLARIS:
443 output( "\n\t.section \".text\",\"ax\"\n" );
444 output( "__wine_spec_pe_header:\n" );
445 output( "\t.skip %u\n", 65536 + page_size );
446 break;
447 default:
448 output( "\n\t.section \".init\",\"ax\"\n" );
449 switch(target_cpu)
451 case CPU_x86:
452 case CPU_x86_64:
453 case CPU_SPARC:
454 output( "\tjmp 1f\n" );
455 break;
456 case CPU_ARM:
457 case CPU_POWERPC:
458 output( "\tb 1f\n" );
459 break;
461 output( "__wine_spec_pe_header:\n" );
462 output( "\t.skip %u\n", 65536 + page_size );
463 output( "1:\n" );
464 break;
467 /* Output the NT header */
469 output( "\n\t.data\n" );
470 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
471 output( "%s\n", asm_globl("__wine_spec_nt_header") );
472 output( ".L__wine_spec_rva_base:\n" );
474 output( "\t.long 0x4550\n" ); /* Signature */
475 switch(target_cpu)
477 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
478 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
479 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARM; break;
480 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
481 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_SPARC; break;
483 output( "\t%s 0x%04x\n", /* Machine */
484 get_asm_short_keyword(), machine );
485 output( "\t%s 0\n", /* NumberOfSections */
486 get_asm_short_keyword() );
487 output( "\t.long 0\n" ); /* TimeDateStamp */
488 output( "\t.long 0\n" ); /* PointerToSymbolTable */
489 output( "\t.long 0\n" ); /* NumberOfSymbols */
490 output( "\t%s %d\n", /* SizeOfOptionalHeader */
491 get_asm_short_keyword(),
492 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
493 output( "\t%s 0x%04x\n", /* Characteristics */
494 get_asm_short_keyword(), spec->characteristics );
495 output( "\t%s 0x%04x\n", /* Magic */
496 get_asm_short_keyword(),
497 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
498 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
499 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
500 output( "\t.long 0\n" ); /* SizeOfCode */
501 output( "\t.long 0\n" ); /* SizeOfInitializedData */
502 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
503 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
504 output( "\t%s %s\n", /* AddressOfEntryPoint */
505 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
506 if (get_ptr_size() == 4)
508 output( "\t.long 0\n" ); /* BaseOfCode */
509 output( "\t.long 0\n" ); /* BaseOfData */
511 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
512 get_asm_ptr_keyword() );
513 output( "\t.long %u\n", page_size ); /* SectionAlignment */
514 output( "\t.long %u\n", page_size ); /* FileAlignment */
515 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
516 get_asm_short_keyword() );
517 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
518 get_asm_short_keyword() );
519 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
520 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
521 output( "\t.long 0\n" ); /* Win32VersionValue */
522 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
523 asm_name("_end") );
524 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
525 output( "\t.long 0\n" ); /* CheckSum */
526 output( "\t%s 0x%04x\n", /* Subsystem */
527 get_asm_short_keyword(), spec->subsystem );
528 output( "\t%s 0x%04x\n", /* DllCharacteristics */
529 get_asm_short_keyword(), spec->dll_characteristics );
530 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
531 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
532 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
533 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
534 output( "\t.long 0\n" ); /* LoaderFlags */
535 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
537 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
538 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
539 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
540 else
541 output( "\t.long 0,0\n" );
543 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
544 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
545 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
546 else
547 output( "\t.long 0,0\n" );
549 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
550 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
551 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
552 else
553 output( "\t.long 0,0\n" );
555 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
556 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
557 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
558 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
559 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
560 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
561 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
562 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
563 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
564 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
565 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
566 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
567 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
569 output( "\n\t%s\n", get_asm_string_section() );
570 output( "%s\n", asm_globl("__wine_spec_file_name") );
571 output( ".L__wine_spec_file_name:\n" );
572 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
573 if (target_platform == PLATFORM_APPLE)
574 output( "\t.lcomm %s,4\n", asm_name("_end") );
576 output_asm_constructor( "__wine_spec_init_ctor" );
580 /*******************************************************************
581 * BuildSpec32File
583 * Build a Win32 C file from a spec file.
585 void BuildSpec32File( DLLSPEC *spec )
587 resolve_imports( spec );
588 output_standard_file_header();
589 output_module( spec );
590 output_stubs( spec );
591 output_exports( spec );
592 output_imports( spec );
593 if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
594 output_resources( spec );
595 output_gnu_stack_note();
599 /*******************************************************************
600 * output_fake_module
602 * Build a fake binary module from a spec file.
604 void output_fake_module( DLLSPEC *spec )
606 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
607 0xc2, 0x0c, 0x00 }; /* ret $12 */
609 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
610 0xc2, 0x04, 0x00 }; /* ret $4 */
612 static const char fakedll_signature[] = "Wine placeholder DLL";
613 const unsigned int page_size = get_page_size();
614 const unsigned int section_align = page_size;
615 const unsigned int file_align = 0x200;
616 const unsigned int reloc_size = 8;
617 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
618 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
619 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
620 sizeof(dll_code_section) : sizeof(exe_code_section);
621 unsigned char *resources;
622 unsigned int resources_size;
623 unsigned int image_size = 3 * section_align;
625 resolve_imports( spec );
626 output_bin_resources( spec, 3 * section_align );
627 resources = output_buffer;
628 resources_size = output_buffer_pos;
629 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
631 init_output_buffer();
633 put_word( 0x5a4d ); /* e_magic */
634 put_word( 0x40 ); /* e_cblp */
635 put_word( 0x01 ); /* e_cp */
636 put_word( 0 ); /* e_crlc */
637 put_word( lfanew / 16 ); /* e_cparhdr */
638 put_word( 0x0000 ); /* e_minalloc */
639 put_word( 0xffff ); /* e_maxalloc */
640 put_word( 0x0000 ); /* e_ss */
641 put_word( 0x00b8 ); /* e_sp */
642 put_word( 0 ); /* e_csum */
643 put_word( 0 ); /* e_ip */
644 put_word( 0 ); /* e_cs */
645 put_word( lfanew ); /* e_lfarlc */
646 put_word( 0 ); /* e_ovno */
647 put_dword( 0 ); /* e_res */
648 put_dword( 0 );
649 put_word( 0 ); /* e_oemid */
650 put_word( 0 ); /* e_oeminfo */
651 put_dword( 0 ); /* e_res2 */
652 put_dword( 0 );
653 put_dword( 0 );
654 put_dword( 0 );
655 put_dword( 0 );
656 put_dword( lfanew );
658 put_data( fakedll_signature, sizeof(fakedll_signature) );
659 align_output( 16 );
661 put_dword( 0x4550 ); /* Signature */
662 switch(target_cpu)
664 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
665 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
666 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
667 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_SPARC ); break;
668 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARM ); break;
670 put_word( nb_sections ); /* NumberOfSections */
671 put_dword( 0 ); /* TimeDateStamp */
672 put_dword( 0 ); /* PointerToSymbolTable */
673 put_dword( 0 ); /* NumberOfSymbols */
674 put_word( get_ptr_size() == 8 ?
675 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
676 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
677 put_word( spec->characteristics ); /* Characteristics */
678 put_word( get_ptr_size() == 8 ?
679 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
680 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
681 put_byte( 0 ); /* MajorLinkerVersion */
682 put_byte( 0 ); /* MinorLinkerVersion */
683 put_dword( text_size ); /* SizeOfCode */
684 put_dword( 0 ); /* SizeOfInitializedData */
685 put_dword( 0 ); /* SizeOfUninitializedData */
686 put_dword( section_align ); /* AddressOfEntryPoint */
687 put_dword( section_align ); /* BaseOfCode */
688 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
689 put_pword( 0x10000000 ); /* ImageBase */
690 put_dword( section_align ); /* SectionAlignment */
691 put_dword( file_align ); /* FileAlignment */
692 put_word( 1 ); /* MajorOperatingSystemVersion */
693 put_word( 0 ); /* MinorOperatingSystemVersion */
694 put_word( 0 ); /* MajorImageVersion */
695 put_word( 0 ); /* MinorImageVersion */
696 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
697 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
698 put_dword( 0 ); /* Win32VersionValue */
699 put_dword( image_size ); /* SizeOfImage */
700 put_dword( file_align ); /* SizeOfHeaders */
701 put_dword( 0 ); /* CheckSum */
702 put_word( spec->subsystem ); /* Subsystem */
703 put_word( spec->dll_characteristics ); /* DllCharacteristics */
704 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
705 put_pword( page_size ); /* SizeOfStackCommit */
706 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
707 put_pword( page_size ); /* SizeOfHeapCommit */
708 put_dword( 0 ); /* LoaderFlags */
709 put_dword( 16 ); /* NumberOfRvaAndSizes */
711 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
712 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
713 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
715 put_dword( 3 * section_align );
716 put_dword( resources_size );
718 else
720 put_dword( 0 );
721 put_dword( 0 );
724 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
725 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
726 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
727 put_dword( reloc_size );
728 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
729 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
730 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
731 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
732 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
733 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
734 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
735 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
736 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
737 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
739 /* .text section */
740 put_data( ".text\0\0", 8 ); /* Name */
741 put_dword( section_align ); /* VirtualSize */
742 put_dword( section_align ); /* VirtualAddress */
743 put_dword( text_size ); /* SizeOfRawData */
744 put_dword( 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( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
751 /* .reloc section */
752 put_data( ".reloc\0", 8 ); /* Name */
753 put_dword( section_align ); /* VirtualSize */
754 put_dword( 2 * section_align );/* VirtualAddress */
755 put_dword( reloc_size ); /* SizeOfRawData */
756 put_dword( 2 * file_align ); /* PointerToRawData */
757 put_dword( 0 ); /* PointerToRelocations */
758 put_dword( 0 ); /* PointerToLinenumbers */
759 put_word( 0 ); /* NumberOfRelocations */
760 put_word( 0 ); /* NumberOfLinenumbers */
761 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
763 /* .rsrc section */
764 if (resources_size)
766 put_data( ".rsrc\0\0", 8 ); /* Name */
767 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
768 put_dword( 3 * section_align );/* VirtualAddress */
769 put_dword( resources_size ); /* SizeOfRawData */
770 put_dword( 3 * file_align ); /* PointerToRawData */
771 put_dword( 0 ); /* PointerToRelocations */
772 put_dword( 0 ); /* PointerToLinenumbers */
773 put_word( 0 ); /* NumberOfRelocations */
774 put_word( 0 ); /* NumberOfLinenumbers */
775 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
778 /* .text contents */
779 align_output( file_align );
780 if (spec->characteristics & IMAGE_FILE_DLL)
781 put_data( dll_code_section, sizeof(dll_code_section) );
782 else
783 put_data( exe_code_section, sizeof(exe_code_section) );
785 /* .reloc contents */
786 align_output( file_align );
787 put_dword( 0 ); /* VirtualAddress */
788 put_dword( 0 ); /* SizeOfBlock */
790 /* .rsrc contents */
791 if (resources_size)
793 align_output( file_align );
794 put_data( resources, resources_size );
796 flush_output_buffer();
800 /*******************************************************************
801 * output_def_file
803 * Build a Win32 def file from a spec file.
805 void output_def_file( DLLSPEC *spec, int include_private )
807 DLLSPEC *spec32 = NULL;
808 const char *name;
809 int i, total;
811 if (spec->type == SPEC_WIN16)
813 spec32 = alloc_dll_spec();
814 add_16bit_exports( spec32, spec );
815 spec = spec32;
818 if (spec_file_name)
819 output( "; File generated automatically from %s; do not edit!\n\n",
820 spec_file_name );
821 else
822 output( "; File generated automatically; do not edit!\n\n" );
824 output( "LIBRARY %s\n\n", spec->file_name);
825 output( "EXPORTS\n");
827 /* Output the exports and relay entry points */
829 for (i = total = 0; i < spec->nb_entry_points; i++)
831 const ORDDEF *odp = &spec->entry_points[i];
832 int is_data = 0;
834 if (!odp) continue;
836 if (odp->name) name = odp->name;
837 else if (odp->export_name) name = odp->export_name;
838 else continue;
840 if (!(odp->flags & FLAG_PRIVATE)) total++;
841 else if (!include_private) continue;
843 if (odp->type == TYPE_STUB) continue;
845 output( " %s", name );
847 switch(odp->type)
849 case TYPE_EXTERN:
850 is_data = 1;
851 /* fall through */
852 case TYPE_VARARGS:
853 case TYPE_CDECL:
854 case TYPE_THISCALL:
855 /* try to reduce output */
856 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
857 output( "=%s", odp->link_name );
858 break;
859 case TYPE_STDCALL:
861 int at_param = get_args_size( odp );
862 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
863 if (odp->flags & FLAG_FORWARD)
865 output( "=%s", odp->link_name );
867 else if (strcmp(name, odp->link_name)) /* try to reduce output */
869 output( "=%s", odp->link_name );
870 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
872 break;
874 default:
875 assert(0);
877 output( " @%d", odp->ordinal );
878 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
879 if (is_data) output( " DATA" );
880 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
881 output( "\n" );
883 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
884 if (spec32) free_dll_spec( spec32 );