shlwapi: Implement SHRegEnumUSValue.
[wine] / tools / winebuild / spec32.c
1 /*
2  * 32-bit spec files
3  *
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
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "build.h"
34
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
41
42 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
43 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
44
45 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
46 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
47 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC  0x107
48
49 /* check if entry point needs a relay thunk */
50 static inline int needs_relay( const ORDDEF *odp )
51 {
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;
59 }
60
61 /* check if dll will output relay thunks */
62 int has_relays( DLLSPEC *spec )
63 {
64     int i;
65
66     if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
67
68     for (i = spec->base; i <= spec->limit; i++)
69     {
70         ORDDEF *odp = spec->ordinals[i];
71         if (needs_relay( odp )) return 1;
72     }
73     return 0;
74 }
75
76 /*******************************************************************
77  *         output_relay_debug
78  *
79  * Output entry points for relay debugging
80  */
81 static void output_relay_debug( DLLSPEC *spec )
82 {
83     int i;
84     unsigned int j, args, flags;
85
86     /* first the table of entry point offsets */
87
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" );
91
92     for (i = spec->base; i <= spec->limit; i++)
93     {
94         ORDDEF *odp = spec->ordinals[i];
95
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" );
100     }
101
102     /* then the table of argument types */
103
104     output( "\t.align %d\n", get_alignment(4) );
105     output( ".L__wine_spec_relay_arg_types:\n" );
106
107     for (i = spec->base; i <= spec->limit; i++)
108     {
109         ORDDEF *odp = spec->ordinals[i];
110         unsigned int mask = 0;
111
112         if (needs_relay( odp ))
113         {
114             for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
115             {
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);
118             }
119         }
120         output( "\t.long 0x%08x\n", mask );
121     }
122
123     /* then the relay thunks */
124
125     output( "\t.text\n" );
126     output( "__wine_spec_relay_entry_points:\n" );
127     output( "\tnop\n" );  /* to avoid 0 offset */
128
129     for (i = spec->base; i <= spec->limit; i++)
130     {
131         ORDDEF *odp = spec->ordinals[i];
132
133         if (!needs_relay( odp )) continue;
134
135         output( "\t.align %d\n", get_alignment(4) );
136         output( ".L__wine_spec_relay_entry_point_%d:\n", i );
137
138         args = strlen(odp->u.func.arg_types);
139         flags = 0;
140
141         switch (target_cpu)
142         {
143         case CPU_x86:
144             if (odp->flags & FLAG_REGISTER)
145                 output( "\tpushl %%eax\n" );
146             else
147                 output( "\tpushl %%esp\n" );
148
149             if (odp->flags & FLAG_RET64) flags |= 1;
150             output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
151
152             if (UsePIC)
153             {
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" );
156             }
157             else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
158             output( "\tpushl %%eax\n" );
159
160             if (odp->flags & FLAG_REGISTER)
161             {
162                 output( "\tcall *8(%%eax)\n" );
163             }
164             else
165             {
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" );
171             }
172             break;
173
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;
191
192         default:
193             assert(0);
194         }
195     }
196 }
197
198 /*******************************************************************
199  *         output_exports
200  *
201  * Output the export table for a Win32 module.
202  */
203 void output_exports( DLLSPEC *spec )
204 {
205     int i, fwd_size = 0;
206     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
207
208     if (!nr_exports) return;
209
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" );
214
215     /* export directory header */
216
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)
226     {
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 */
229     }
230     else
231     {
232         output( "\t.long 0\n" );  /* AddressOfNames */
233         output( "\t.long 0\n" );  /* AddressOfNameOrdinals */
234     }
235
236     /* output the function pointers */
237
238     output( "\n.L__wine_spec_exports_funcs:\n" );
239     for (i = spec->base; i <= spec->limit; i++)
240     {
241         ORDDEF *odp = spec->ordinals[i];
242         if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
243         else switch(odp->type)
244         {
245         case TYPE_EXTERN:
246         case TYPE_STDCALL:
247         case TYPE_VARARGS:
248         case TYPE_CDECL:
249             if (odp->flags & FLAG_FORWARD)
250             {
251                 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
252                 fwd_size += strlen(odp->link_name) + 1;
253             }
254             else if (odp->flags & FLAG_EXT_LINK)
255             {
256                 output( "\t%s %s_%s\n",
257                          get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
258             }
259             else
260             {
261                 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
262             }
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);
270         }
271     }
272
273     if (spec->nb_names)
274     {
275         /* output the function name pointers */
276
277         int namepos = strlen(spec->file_name) + 1;
278
279         output( "\n.L__wine_spec_exp_name_ptrs:\n" );
280         for (i = 0; i < spec->nb_names; i++)
281         {
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;
284         }
285
286         /* output the function ordinals */
287
288         output( "\n.L__wine_spec_exp_ordinals:\n" );
289         for (i = 0; i < spec->nb_names; i++)
290         {
291             output( "\t%s %d\n",
292                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
293         }
294         if (spec->nb_names % 2)
295         {
296             output( "\t%s 0\n", get_asm_short_keyword() );
297         }
298     }
299
300     /* output the export name strings */
301
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 );
307
308     /* output forward strings */
309
310     if (fwd_size)
311     {
312         output( "\n.L__wine_spec_forwards:\n" );
313         for (i = spec->base; i <= spec->limit; i++)
314         {
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 );
318         }
319     }
320     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
321     output( ".L__wine_spec_exports_end:\n" );
322
323     /* output relays */
324
325     if (!has_relays( spec ))
326     {
327         output( "\t%s 0\n", get_asm_ptr_keyword() );
328         return;
329     }
330
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() );
338
339     output_relay_debug( spec );
340 }
341
342
343 /*******************************************************************
344  *         output_asm_constructor
345  *
346  * Output code for calling a dll constructor.
347  */
348 static void output_asm_constructor( const char *constructor )
349 {
350     if (target_platform == PLATFORM_APPLE)
351     {
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) );
356     }
357     else
358     {
359         output( "\n\t.section \".init\",\"ax\"\n" );
360         switch(target_cpu)
361         {
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;
379         }
380     }
381 }
382
383
384 /*******************************************************************
385  *         output_module
386  *
387  * Output the module data.
388  */
389 void output_module( DLLSPEC *spec )
390 {
391     int machine = 0;
392     unsigned int page_size = get_page_size();
393
394     /* Reserve some space for the PE header */
395
396     switch (target_platform)
397     {
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)
412         {
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;
423         }
424         output( "__wine_spec_pe_header:\n" );
425         output( "\t.skip %u\n", 65536 + page_size );
426         output( "1:\n" );
427         break;
428     }
429
430     /* Output the NT header */
431
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" );
436
437     output( "\t.long 0x4550\n" );         /* Signature */
438     switch(target_cpu)
439     {
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;
446     }
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)
471     {
472         output( "\t.long 0\n" );          /* BaseOfCode */
473         output( "\t.long 0\n" );          /* BaseOfData */
474     }
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 */
500
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" );
506
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" );
512
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" );
518
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] */
532
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") );
539
540     output_asm_constructor( "__wine_spec_init_ctor" );
541 }
542
543
544 /*******************************************************************
545  *         BuildSpec32File
546  *
547  * Build a Win32 C file from a spec file.
548  */
549 void BuildSpec32File( DLLSPEC *spec )
550 {
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     if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
558     output_resources( spec );
559     output_gnu_stack_note();
560 }
561
562
563 /*******************************************************************
564  *         output_fake_module
565  *
566  * Build a fake binary module from a spec file.
567  */
568 void output_fake_module( DLLSPEC *spec )
569 {
570     static const unsigned char dll_code_section[] = { 0x31, 0xc0,          /* xor %eax,%eax */
571                                                       0xc2, 0x0c, 0x00 };  /* ret $12 */
572
573     static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00,  /* movl $1,%eax */
574                                                       0xc2, 0x04, 0x00 };            /* ret $4 */
575
576     static const char fakedll_signature[] = "Wine placeholder DLL";
577     const unsigned int page_size = get_page_size();
578     const unsigned int section_align = page_size;
579     const unsigned int file_align = 0x200;
580     const unsigned int reloc_size = 8;
581     const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
582     const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
583     const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
584                                     sizeof(dll_code_section) : sizeof(exe_code_section);
585     unsigned char *resources;
586     unsigned int resources_size;
587     unsigned int image_size = 3 * section_align;
588
589     resolve_imports( spec );
590     output_bin_resources( spec, 3 * section_align );
591     resources = output_buffer;
592     resources_size = output_buffer_pos;
593     if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
594
595     init_output_buffer();
596
597     put_word( 0x5a4d );       /* e_magic */
598     put_word( 0x40 );         /* e_cblp */
599     put_word( 0x01 );         /* e_cp */
600     put_word( 0 );            /* e_crlc */
601     put_word( lfanew / 16 );  /* e_cparhdr */
602     put_word( 0x0000 );       /* e_minalloc */
603     put_word( 0xffff );       /* e_maxalloc */
604     put_word( 0x0000 );       /* e_ss */
605     put_word( 0x00b8 );       /* e_sp */
606     put_word( 0 );            /* e_csum */
607     put_word( 0 );            /* e_ip */
608     put_word( 0 );            /* e_cs */
609     put_word( lfanew );       /* e_lfarlc */
610     put_word( 0 );            /* e_ovno */
611     put_dword( 0 );           /* e_res */
612     put_dword( 0 );
613     put_word( 0 );            /* e_oemid */
614     put_word( 0 );            /* e_oeminfo */
615     put_dword( 0 );           /* e_res2 */
616     put_dword( 0 );
617     put_dword( 0 );
618     put_dword( 0 );
619     put_dword( 0 );
620     put_dword( lfanew );
621
622     put_data( fakedll_signature, sizeof(fakedll_signature) );
623     align_output( 16 );
624
625     put_dword( 0x4550 );                             /* Signature */
626     switch(target_cpu)
627     {
628     case CPU_x86:     put_word( IMAGE_FILE_MACHINE_I386 ); break;
629     case CPU_x86_64:  put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
630     case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
631     case CPU_ALPHA:   put_word( IMAGE_FILE_MACHINE_ALPHA ); break;
632     case CPU_SPARC:   put_word( IMAGE_FILE_MACHINE_UNKNOWN ); break;
633     case CPU_ARM:     put_word( IMAGE_FILE_MACHINE_ARM ); break;
634     }
635     put_word( nb_sections );                         /* NumberOfSections */
636     put_dword( 0 );                                  /* TimeDateStamp */
637     put_dword( 0 );                                  /* PointerToSymbolTable */
638     put_dword( 0 );                                  /* NumberOfSymbols */
639     put_word( get_ptr_size() == 8 ?
640               IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
641               IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );   /* SizeOfOptionalHeader */
642     put_word( spec->characteristics );               /* Characteristics */
643     put_word( get_ptr_size() == 8 ?
644               IMAGE_NT_OPTIONAL_HDR64_MAGIC :
645               IMAGE_NT_OPTIONAL_HDR32_MAGIC );       /* Magic */
646     put_byte(  0 );                                  /* MajorLinkerVersion */
647     put_byte(  0 );                                  /* MinorLinkerVersion */
648     put_dword( text_size );                          /* SizeOfCode */
649     put_dword( 0 );                                  /* SizeOfInitializedData */
650     put_dword( 0 );                                  /* SizeOfUninitializedData */
651     put_dword( section_align );                      /* AddressOfEntryPoint */
652     put_dword( section_align );                      /* BaseOfCode */
653     if (get_ptr_size() == 4) put_dword( 0 );         /* BaseOfData */
654     put_pword( 0x10000000 );                         /* ImageBase */
655     put_dword( section_align );                      /* SectionAlignment */
656     put_dword( file_align );                         /* FileAlignment */
657     put_word( 1 );                                   /* MajorOperatingSystemVersion */
658     put_word( 0 );                                   /* MinorOperatingSystemVersion */
659     put_word( 0 );                                   /* MajorImageVersion */
660     put_word( 0 );                                   /* MinorImageVersion */
661     put_word( spec->subsystem_major );               /* MajorSubsystemVersion */
662     put_word( spec->subsystem_minor );               /* MinorSubsystemVersion */
663     put_dword( 0 );                                  /* Win32VersionValue */
664     put_dword( image_size );                         /* SizeOfImage */
665     put_dword( file_align );                         /* SizeOfHeaders */
666     put_dword( 0 );                                  /* CheckSum */
667     put_word( spec->subsystem );                     /* Subsystem */
668     put_word( spec->dll_characteristics );           /* DllCharacteristics */
669     put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
670     put_pword( page_size );                          /* SizeOfStackCommit */
671     put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 );   /* SizeOfHeapReserve */
672     put_pword( page_size );                          /* SizeOfHeapCommit */
673     put_dword( 0 );                                  /* LoaderFlags */
674     put_dword( 16 );                                 /* NumberOfRvaAndSizes */
675
676     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
677     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
678     if (resources_size)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
679     {
680         put_dword( 3 * section_align );
681         put_dword( resources_size );
682     }
683     else
684     {
685         put_dword( 0 );
686         put_dword( 0 );
687     }
688
689     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
690     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
691     put_dword( 2 * section_align );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
692     put_dword( reloc_size );
693     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
694     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
695     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
696     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
697     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
698     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
699     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
700     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
701     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
702     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[15] */
703
704     /* .text section */
705     put_data( ".text\0\0", 8 );    /* Name */
706     put_dword( section_align );    /* VirtualSize */
707     put_dword( section_align );    /* VirtualAddress */
708     put_dword( text_size );        /* SizeOfRawData */
709     put_dword( file_align );       /* PointerToRawData */
710     put_dword( 0 );                /* PointerToRelocations */
711     put_dword( 0 );                /* PointerToLinenumbers */
712     put_word( 0 );                 /* NumberOfRelocations */
713     put_word( 0 );                 /* NumberOfLinenumbers */
714     put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics  */
715
716     /* .reloc section */
717     put_data( ".reloc\0", 8 );     /* Name */
718     put_dword( section_align );    /* VirtualSize */
719     put_dword( 2 * section_align );/* VirtualAddress */
720     put_dword( reloc_size );       /* SizeOfRawData */
721     put_dword( 2 * file_align );   /* PointerToRawData */
722     put_dword( 0 );                /* PointerToRelocations */
723     put_dword( 0 );                /* PointerToLinenumbers */
724     put_word( 0 );                 /* NumberOfRelocations */
725     put_word( 0 );                 /* NumberOfLinenumbers */
726     put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
727
728     /* .rsrc section */
729     if (resources_size)
730     {
731         put_data( ".rsrc\0\0", 8 );    /* Name */
732         put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
733         put_dword( 3 * section_align );/* VirtualAddress */
734         put_dword( resources_size );   /* SizeOfRawData */
735         put_dword( 3 * file_align );   /* PointerToRawData */
736         put_dword( 0 );                /* PointerToRelocations */
737         put_dword( 0 );                /* PointerToLinenumbers */
738         put_word( 0 );                 /* NumberOfRelocations */
739         put_word( 0 );                 /* NumberOfLinenumbers */
740         put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
741     }
742
743     /* .text contents */
744     align_output( file_align );
745     if (spec->characteristics & IMAGE_FILE_DLL)
746         put_data( dll_code_section, sizeof(dll_code_section) );
747     else
748         put_data( exe_code_section, sizeof(exe_code_section) );
749
750     /* .reloc contents */
751     align_output( file_align );
752     put_dword( 0 );   /* VirtualAddress */
753     put_dword( 0 );   /* SizeOfBlock */
754
755     /* .rsrc contents */
756     if (resources_size)
757     {
758         align_output( file_align );
759         put_data( resources, resources_size );
760     }
761     flush_output_buffer();
762 }
763
764
765 /*******************************************************************
766  *         output_def_file
767  *
768  * Build a Win32 def file from a spec file.
769  */
770 void output_def_file( DLLSPEC *spec, int include_private )
771 {
772     DLLSPEC *spec32 = NULL;
773     const char *name;
774     int i, total;
775
776     if (spec->type == SPEC_WIN16)
777     {
778         spec32 = alloc_dll_spec();
779         add_16bit_exports( spec32, spec );
780         spec = spec32;
781     }
782
783     if (spec_file_name)
784         output( "; File generated automatically from %s; do not edit!\n\n",
785                  spec_file_name );
786     else
787         output( "; File generated automatically; do not edit!\n\n" );
788
789     output( "LIBRARY %s\n\n", spec->file_name);
790     output( "EXPORTS\n");
791
792     /* Output the exports and relay entry points */
793
794     for (i = total = 0; i < spec->nb_entry_points; i++)
795     {
796         const ORDDEF *odp = &spec->entry_points[i];
797         int is_data = 0;
798
799         if (!odp) continue;
800
801         if (odp->name) name = odp->name;
802         else if (odp->export_name) name = odp->export_name;
803         else continue;
804
805         if (!(odp->flags & FLAG_PRIVATE)) total++;
806         else if (!include_private) continue;
807
808         if (odp->type == TYPE_STUB) continue;
809
810         output( "  %s", name );
811
812         switch(odp->type)
813         {
814         case TYPE_EXTERN:
815             is_data = 1;
816             /* fall through */
817         case TYPE_VARARGS:
818         case TYPE_CDECL:
819             /* try to reduce output */
820             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
821                 output( "=%s", odp->link_name );
822             break;
823         case TYPE_STDCALL:
824         {
825             int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
826             if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
827             if  (odp->flags & FLAG_FORWARD)
828             {
829                 output( "=%s", odp->link_name );
830             }
831             else if (strcmp(name, odp->link_name)) /* try to reduce output */
832             {
833                 output( "=%s", odp->link_name );
834                 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
835             }
836             break;
837         }
838         default:
839             assert(0);
840         }
841         output( " @%d", odp->ordinal );
842         if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
843         if (is_data) output( " DATA" );
844         if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
845         output( "\n" );
846     }
847     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
848     if (spec32) free_dll_spec( spec32 );
849 }