ntdll/tests: Add tests for opening files with a root directory handle.
[wine/multimedia.git] / tools / winebuild / spec32.c
blobc2b11af847471371bcc3be363ab1e38ffabfbe76
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)) return 0;
56 /* skip norelay and forward entry points */
57 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
58 return 1;
61 /* check if dll will output relay thunks */
62 int has_relays( DLLSPEC *spec )
64 int i;
66 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
68 for (i = spec->base; i <= spec->limit; i++)
70 ORDDEF *odp = spec->ordinals[i];
71 if (needs_relay( odp )) return 1;
73 return 0;
76 /*******************************************************************
77 * output_relay_debug
79 * Output entry points for relay debugging
81 static void output_relay_debug( DLLSPEC *spec )
83 int i;
84 unsigned int j, args, flags;
86 /* first the table of entry point offsets */
88 output( "\t%s\n", get_asm_rodata_section() );
89 output( "\t.align %d\n", get_alignment(4) );
90 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
92 for (i = spec->base; i <= spec->limit; i++)
94 ORDDEF *odp = spec->ordinals[i];
96 if (needs_relay( odp ))
97 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
98 else
99 output( "\t.long 0\n" );
102 /* then the table of argument types */
104 output( "\t.align %d\n", get_alignment(4) );
105 output( ".L__wine_spec_relay_arg_types:\n" );
107 for (i = spec->base; i <= spec->limit; i++)
109 ORDDEF *odp = spec->ordinals[i];
110 unsigned int mask = 0;
112 if (needs_relay( odp ))
114 for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
116 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
117 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
120 output( "\t.long 0x%08x\n", mask );
123 /* then the relay thunks */
125 output( "\t.text\n" );
126 output( "__wine_spec_relay_entry_points:\n" );
127 output( "\tnop\n" ); /* to avoid 0 offset */
129 for (i = spec->base; i <= spec->limit; i++)
131 ORDDEF *odp = spec->ordinals[i];
133 if (!needs_relay( odp )) continue;
135 output( "\t.align %d\n", get_alignment(4) );
136 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
138 args = strlen(odp->u.func.arg_types);
139 flags = 0;
141 switch (target_cpu)
143 case CPU_x86:
144 if (odp->flags & FLAG_REGISTER)
145 output( "\tpushl %%eax\n" );
146 else
147 output( "\tpushl %%esp\n" );
149 if (odp->flags & FLAG_RET64) flags |= 1;
150 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
152 if (UsePIC)
154 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
155 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
157 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
158 output( "\tpushl %%eax\n" );
160 if (odp->flags & FLAG_REGISTER)
162 output( "\tcall *8(%%eax)\n" );
164 else
166 output( "\tcall *4(%%eax)\n" );
167 if (odp->type == TYPE_STDCALL)
168 output( "\tret $%u\n", args * get_ptr_size() );
169 else
170 output( "\tret\n" );
172 break;
174 case CPU_x86_64:
175 output( "\t.cfi_startproc\n" );
176 output( "\tsubq $40,%%rsp\n" );
177 output( "\t.cfi_adjust_cfa_offset 40\n" );
178 output( "\tmovq %%rcx,48(%%rsp)\n" );
179 output( "\tmovq %%rdx,56(%%rsp)\n" );
180 output( "\tmovq %%r8,64(%%rsp)\n" );
181 output( "\tmovq %%r9,72(%%rsp)\n" );
182 output( "\tleaq 40(%%rsp),%%r8\n" );
183 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
184 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
185 output( "\tcallq *%u(%%rcx)\n", (odp->flags & FLAG_REGISTER) ? 16 : 8 );
186 output( "\taddq $40,%%rsp\n" );
187 output( "\t.cfi_adjust_cfa_offset -40\n" );
188 output( "\tret\n" );
189 output( "\t.cfi_endproc\n" );
190 break;
192 default:
193 assert(0);
198 /*******************************************************************
199 * output_exports
201 * Output the export table for a Win32 module.
203 void output_exports( DLLSPEC *spec )
205 int i, fwd_size = 0;
206 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
208 if (!nr_exports) return;
210 output( "\n/* export table */\n\n" );
211 output( "\t.data\n" );
212 output( "\t.align %d\n", get_alignment(4) );
213 output( ".L__wine_spec_exports:\n" );
215 /* export directory header */
217 output( "\t.long 0\n" ); /* Characteristics */
218 output( "\t.long 0\n" ); /* TimeDateStamp */
219 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
220 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
221 output( "\t.long %u\n", spec->base ); /* Base */
222 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
223 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
224 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
225 if (spec->nb_names)
227 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
228 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
230 else
232 output( "\t.long 0\n" ); /* AddressOfNames */
233 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
236 /* output the function pointers */
238 output( "\n.L__wine_spec_exports_funcs:\n" );
239 for (i = spec->base; i <= spec->limit; i++)
241 ORDDEF *odp = spec->ordinals[i];
242 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
243 else switch(odp->type)
245 case TYPE_EXTERN:
246 case TYPE_STDCALL:
247 case TYPE_VARARGS:
248 case TYPE_CDECL:
249 if (odp->flags & FLAG_FORWARD)
251 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
252 fwd_size += strlen(odp->link_name) + 1;
254 else if (odp->flags & FLAG_EXT_LINK)
256 output( "\t%s %s_%s\n",
257 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
259 else
261 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
263 break;
264 case TYPE_STUB:
265 output( "\t%s %s\n", get_asm_ptr_keyword(),
266 asm_name( get_stub_name( odp, spec )) );
267 break;
268 default:
269 assert(0);
273 if (spec->nb_names)
275 /* output the function name pointers */
277 int namepos = strlen(spec->file_name) + 1;
279 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
280 for (i = 0; i < spec->nb_names; i++)
282 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
283 namepos += strlen(spec->names[i]->name) + 1;
286 /* output the function ordinals */
288 output( "\n.L__wine_spec_exp_ordinals:\n" );
289 for (i = 0; i < spec->nb_names; i++)
291 output( "\t%s %d\n",
292 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
294 if (spec->nb_names % 2)
296 output( "\t%s 0\n", get_asm_short_keyword() );
300 /* output the export name strings */
302 output( "\n.L__wine_spec_exp_names:\n" );
303 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
304 for (i = 0; i < spec->nb_names; i++)
305 output( "\t%s \"%s\"\n",
306 get_asm_string_keyword(), spec->names[i]->name );
308 /* output forward strings */
310 if (fwd_size)
312 output( "\n.L__wine_spec_forwards:\n" );
313 for (i = spec->base; i <= spec->limit; i++)
315 ORDDEF *odp = spec->ordinals[i];
316 if (odp && (odp->flags & FLAG_FORWARD))
317 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
320 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
321 output( ".L__wine_spec_exports_end:\n" );
323 /* output relays */
325 if (!has_relays( spec ))
327 output( "\t%s 0\n", get_asm_ptr_keyword() );
328 return;
331 output( ".L__wine_spec_relay_descr:\n" );
332 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
333 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
334 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
335 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
336 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
337 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
339 output_relay_debug( spec );
343 /*******************************************************************
344 * output_asm_constructor
346 * Output code for calling a dll constructor.
348 static void output_asm_constructor( const char *constructor )
350 if (target_platform == PLATFORM_APPLE)
352 /* Mach-O doesn't have an init section */
353 output( "\n\t.mod_init_func\n" );
354 output( "\t.align %d\n", get_alignment(4) );
355 output( "\t.long %s\n", asm_name(constructor) );
357 else
359 output( "\n\t.section \".init\",\"ax\"\n" );
360 switch(target_cpu)
362 case CPU_x86:
363 case CPU_x86_64:
364 output( "\tcall %s\n", asm_name(constructor) );
365 break;
366 case CPU_SPARC:
367 output( "\tcall %s\n", asm_name(constructor) );
368 output( "\tnop\n" );
369 break;
370 case CPU_ALPHA:
371 output( "\tjsr $26,%s\n", asm_name(constructor) );
372 break;
373 case CPU_ARM:
374 output( "\tblx %s\n", asm_name(constructor) );
375 break;
376 case CPU_POWERPC:
377 output( "\tbl %s\n", asm_name(constructor) );
378 break;
384 /*******************************************************************
385 * output_module
387 * Output the module data.
389 void output_module( DLLSPEC *spec )
391 int machine = 0;
392 unsigned int page_size = get_page_size();
394 /* Reserve some space for the PE header */
396 switch (target_platform)
398 case PLATFORM_APPLE:
399 output( "\t.text\n" );
400 output( "\t.align %d\n", get_alignment(page_size) );
401 output( "__wine_spec_pe_header:\n" );
402 output( "\t.space 65536\n" );
403 break;
404 case PLATFORM_SOLARIS:
405 output( "\n\t.section \".text\",\"ax\"\n" );
406 output( "__wine_spec_pe_header:\n" );
407 output( "\t.skip %u\n", 65536 + page_size );
408 break;
409 default:
410 output( "\n\t.section \".init\",\"ax\"\n" );
411 switch(target_cpu)
413 case CPU_x86:
414 case CPU_x86_64:
415 case CPU_ALPHA:
416 case CPU_SPARC:
417 output( "\tjmp 1f\n" );
418 break;
419 case CPU_ARM:
420 case CPU_POWERPC:
421 output( "\tb 1f\n" );
422 break;
424 output( "__wine_spec_pe_header:\n" );
425 output( "\t.skip %u\n", 65536 + page_size );
426 output( "1:\n" );
427 break;
430 /* Output the NT header */
432 output( "\n\t.data\n" );
433 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
434 output( "%s\n", asm_globl("__wine_spec_nt_header") );
435 output( ".L__wine_spec_rva_base:\n" );
437 output( "\t.long 0x4550\n" ); /* Signature */
438 switch(target_cpu)
440 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
441 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
442 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARM; break;
443 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
444 case CPU_ALPHA: machine = IMAGE_FILE_MACHINE_ALPHA; break;
445 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
447 output( "\t%s 0x%04x\n", /* Machine */
448 get_asm_short_keyword(), machine );
449 output( "\t%s 0\n", /* NumberOfSections */
450 get_asm_short_keyword() );
451 output( "\t.long 0\n" ); /* TimeDateStamp */
452 output( "\t.long 0\n" ); /* PointerToSymbolTable */
453 output( "\t.long 0\n" ); /* NumberOfSymbols */
454 output( "\t%s %d\n", /* SizeOfOptionalHeader */
455 get_asm_short_keyword(),
456 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
457 output( "\t%s 0x%04x\n", /* Characteristics */
458 get_asm_short_keyword(), spec->characteristics );
459 output( "\t%s 0x%04x\n", /* Magic */
460 get_asm_short_keyword(),
461 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
462 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
463 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
464 output( "\t.long 0\n" ); /* SizeOfCode */
465 output( "\t.long 0\n" ); /* SizeOfInitializedData */
466 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
467 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
468 output( "\t%s %s\n", /* AddressOfEntryPoint */
469 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
470 if (get_ptr_size() == 4)
472 output( "\t.long 0\n" ); /* BaseOfCode */
473 output( "\t.long 0\n" ); /* BaseOfData */
475 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
476 get_asm_ptr_keyword() );
477 output( "\t.long %u\n", page_size ); /* SectionAlignment */
478 output( "\t.long %u\n", page_size ); /* FileAlignment */
479 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
480 get_asm_short_keyword() );
481 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
482 get_asm_short_keyword() );
483 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
484 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
485 output( "\t.long 0\n" ); /* Win32VersionValue */
486 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
487 asm_name("_end") );
488 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
489 output( "\t.long 0\n" ); /* CheckSum */
490 output( "\t%s 0x%04x\n", /* Subsystem */
491 get_asm_short_keyword(), spec->subsystem );
492 output( "\t%s 0x%04x\n", /* DllCharacteristics */
493 get_asm_short_keyword(), spec->dll_characteristics );
494 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
495 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
496 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
497 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
498 output( "\t.long 0\n" ); /* LoaderFlags */
499 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
501 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
502 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
503 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
504 else
505 output( "\t.long 0,0\n" );
507 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
508 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
509 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
510 else
511 output( "\t.long 0,0\n" );
513 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
514 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
515 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
516 else
517 output( "\t.long 0,0\n" );
519 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
520 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
521 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
522 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
523 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
524 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
525 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
526 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
527 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
528 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
529 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
530 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
531 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
533 output( "\n\t%s\n", get_asm_string_section() );
534 output( "%s\n", asm_globl("__wine_spec_file_name") );
535 output( ".L__wine_spec_file_name:\n" );
536 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
537 if (target_platform == PLATFORM_APPLE)
538 output( "\t.lcomm %s,4\n", asm_name("_end") );
540 output_asm_constructor( "__wine_spec_init_ctor" );
544 /*******************************************************************
545 * BuildSpec32File
547 * Build a Win32 C file from a spec file.
549 void BuildSpec32File( DLLSPEC *spec )
551 resolve_imports( spec );
552 output_standard_file_header();
553 output_module( spec );
554 output_stubs( spec );
555 output_exports( spec );
556 output_imports( spec );
557 output_resources( spec );
558 output_gnu_stack_note();
562 /*******************************************************************
563 * output_fake_module
565 * Build a fake binary module from a spec file.
567 void output_fake_module( DLLSPEC *spec )
569 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
570 0xc2, 0x0c, 0x00 }; /* ret $12 */
572 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
573 0xc2, 0x04, 0x00 }; /* ret $4 */
575 static const char fakedll_signature[] = "Wine placeholder DLL";
576 const unsigned int page_size = get_page_size();
577 const unsigned int section_align = page_size;
578 const unsigned int file_align = 0x200;
579 const unsigned int reloc_size = 8;
580 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
581 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
582 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
583 sizeof(dll_code_section) : sizeof(exe_code_section);
584 unsigned char *resources;
585 unsigned int resources_size;
586 unsigned int image_size = 3 * section_align;
588 resolve_imports( spec );
589 output_bin_resources( spec, 3 * section_align );
590 resources = output_buffer;
591 resources_size = output_buffer_pos;
592 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
594 init_output_buffer();
596 put_word( 0x5a4d ); /* e_magic */
597 put_word( 0x40 ); /* e_cblp */
598 put_word( 0x01 ); /* e_cp */
599 put_word( 0 ); /* e_crlc */
600 put_word( lfanew / 16 ); /* e_cparhdr */
601 put_word( 0x0000 ); /* e_minalloc */
602 put_word( 0xffff ); /* e_maxalloc */
603 put_word( 0x0000 ); /* e_ss */
604 put_word( 0x00b8 ); /* e_sp */
605 put_word( 0 ); /* e_csum */
606 put_word( 0 ); /* e_ip */
607 put_word( 0 ); /* e_cs */
608 put_word( lfanew ); /* e_lfarlc */
609 put_word( 0 ); /* e_ovno */
610 put_dword( 0 ); /* e_res */
611 put_dword( 0 );
612 put_word( 0 ); /* e_oemid */
613 put_word( 0 ); /* e_oeminfo */
614 put_dword( 0 ); /* e_res2 */
615 put_dword( 0 );
616 put_dword( 0 );
617 put_dword( 0 );
618 put_dword( 0 );
619 put_dword( lfanew );
621 put_data( fakedll_signature, sizeof(fakedll_signature) );
622 align_output( 16 );
624 put_dword( 0x4550 ); /* Signature */
625 switch(target_cpu)
627 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
628 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
629 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
630 case CPU_ALPHA: put_word( IMAGE_FILE_MACHINE_ALPHA ); break;
631 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_UNKNOWN ); break;
632 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARM ); break;
634 put_word( nb_sections ); /* NumberOfSections */
635 put_dword( 0 ); /* TimeDateStamp */
636 put_dword( 0 ); /* PointerToSymbolTable */
637 put_dword( 0 ); /* NumberOfSymbols */
638 put_word( get_ptr_size() == 8 ?
639 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
640 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
641 put_word( spec->characteristics ); /* Characteristics */
642 put_word( get_ptr_size() == 8 ?
643 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
644 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
645 put_byte( 0 ); /* MajorLinkerVersion */
646 put_byte( 0 ); /* MinorLinkerVersion */
647 put_dword( text_size ); /* SizeOfCode */
648 put_dword( 0 ); /* SizeOfInitializedData */
649 put_dword( 0 ); /* SizeOfUninitializedData */
650 put_dword( section_align ); /* AddressOfEntryPoint */
651 put_dword( section_align ); /* BaseOfCode */
652 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
653 put_pword( 0x10000000 ); /* ImageBase */
654 put_dword( section_align ); /* SectionAlignment */
655 put_dword( file_align ); /* FileAlignment */
656 put_word( 1 ); /* MajorOperatingSystemVersion */
657 put_word( 0 ); /* MinorOperatingSystemVersion */
658 put_word( 0 ); /* MajorImageVersion */
659 put_word( 0 ); /* MinorImageVersion */
660 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
661 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
662 put_dword( 0 ); /* Win32VersionValue */
663 put_dword( image_size ); /* SizeOfImage */
664 put_dword( file_align ); /* SizeOfHeaders */
665 put_dword( 0 ); /* CheckSum */
666 put_word( spec->subsystem ); /* Subsystem */
667 put_word( spec->dll_characteristics ); /* DllCharacteristics */
668 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
669 put_pword( page_size ); /* SizeOfStackCommit */
670 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
671 put_pword( page_size ); /* SizeOfHeapCommit */
672 put_dword( 0 ); /* LoaderFlags */
673 put_dword( 16 ); /* NumberOfRvaAndSizes */
675 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
676 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
677 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
679 put_dword( 3 * section_align );
680 put_dword( resources_size );
682 else
684 put_dword( 0 );
685 put_dword( 0 );
688 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
689 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
690 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
691 put_dword( reloc_size );
692 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
693 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
694 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
695 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
696 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
697 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
698 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
699 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
700 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
701 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
703 /* .text section */
704 put_data( ".text\0\0", 8 ); /* Name */
705 put_dword( section_align ); /* VirtualSize */
706 put_dword( section_align ); /* VirtualAddress */
707 put_dword( text_size ); /* SizeOfRawData */
708 put_dword( file_align ); /* PointerToRawData */
709 put_dword( 0 ); /* PointerToRelocations */
710 put_dword( 0 ); /* PointerToLinenumbers */
711 put_word( 0 ); /* NumberOfRelocations */
712 put_word( 0 ); /* NumberOfLinenumbers */
713 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
715 /* .reloc section */
716 put_data( ".reloc\0", 8 ); /* Name */
717 put_dword( section_align ); /* VirtualSize */
718 put_dword( 2 * section_align );/* VirtualAddress */
719 put_dword( reloc_size ); /* SizeOfRawData */
720 put_dword( 2 * file_align ); /* PointerToRawData */
721 put_dword( 0 ); /* PointerToRelocations */
722 put_dword( 0 ); /* PointerToLinenumbers */
723 put_word( 0 ); /* NumberOfRelocations */
724 put_word( 0 ); /* NumberOfLinenumbers */
725 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
727 /* .rsrc section */
728 if (resources_size)
730 put_data( ".rsrc\0\0", 8 ); /* Name */
731 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
732 put_dword( 3 * section_align );/* VirtualAddress */
733 put_dword( resources_size ); /* SizeOfRawData */
734 put_dword( 3 * file_align ); /* PointerToRawData */
735 put_dword( 0 ); /* PointerToRelocations */
736 put_dword( 0 ); /* PointerToLinenumbers */
737 put_word( 0 ); /* NumberOfRelocations */
738 put_word( 0 ); /* NumberOfLinenumbers */
739 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
742 /* .text contents */
743 align_output( file_align );
744 if (spec->characteristics & IMAGE_FILE_DLL)
745 put_data( dll_code_section, sizeof(dll_code_section) );
746 else
747 put_data( exe_code_section, sizeof(exe_code_section) );
749 /* .reloc contents */
750 align_output( file_align );
751 put_dword( 0 ); /* VirtualAddress */
752 put_dword( 0 ); /* SizeOfBlock */
754 /* .rsrc contents */
755 if (resources_size)
757 align_output( file_align );
758 put_data( resources, resources_size );
760 flush_output_buffer();
764 /*******************************************************************
765 * BuildDef32File
767 * Build a Win32 def file from a spec file.
769 void BuildDef32File( DLLSPEC *spec )
771 const char *name;
772 int i, total;
774 if (spec_file_name)
775 output( "; File generated automatically from %s; do not edit!\n\n",
776 spec_file_name );
777 else
778 output( "; File generated automatically; do not edit!\n\n" );
780 output( "LIBRARY %s\n\n", spec->file_name);
781 output( "EXPORTS\n");
783 /* Output the exports and relay entry points */
785 for (i = total = 0; i < spec->nb_entry_points; i++)
787 const ORDDEF *odp = &spec->entry_points[i];
788 int is_data = 0;
790 if (!odp) continue;
792 if (odp->name) name = odp->name;
793 else if (odp->export_name) name = odp->export_name;
794 else continue;
796 if (!(odp->flags & FLAG_PRIVATE)) total++;
798 if (odp->type == TYPE_STUB) continue;
800 output( " %s", name );
802 switch(odp->type)
804 case TYPE_EXTERN:
805 is_data = 1;
806 /* fall through */
807 case TYPE_VARARGS:
808 case TYPE_CDECL:
809 /* try to reduce output */
810 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
811 output( "=%s", odp->link_name );
812 break;
813 case TYPE_STDCALL:
815 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
816 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
817 if (odp->flags & FLAG_FORWARD)
819 output( "=%s", odp->link_name );
821 else if (strcmp(name, odp->link_name)) /* try to reduce output */
823 output( "=%s", odp->link_name );
824 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
826 break;
828 default:
829 assert(0);
831 output( " @%d", odp->ordinal );
832 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
833 if (is_data) output( " DATA" );
834 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
835 output( "\n" );
837 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );