wbemprox: Always convert from BSTR.
[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_POWERPC 0x01f0
38 #define IMAGE_FILE_MACHINE_AMD64   0x8664
39 #define IMAGE_FILE_MACHINE_ARMNT   0x01C4
40 /* Wine extension */
41 #define IMAGE_FILE_MACHINE_SPARC   0x2000
42
43 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
44 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
45
46 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
47 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
48 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC  0x107
49
50 /* check if entry point needs a relay thunk */
51 static inline int needs_relay( const ORDDEF *odp )
52 {
53     /* skip nonexistent entry points */
54     if (!odp) return 0;
55     /* skip non-functions */
56     switch (odp->type)
57     {
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;
67     }
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;
73 }
74
75 static int is_float_arg( const ORDDEF *odp, int arg )
76 {
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);
79 }
80
81 /* check if dll will output relay thunks */
82 int has_relays( DLLSPEC *spec )
83 {
84     int i;
85
86     if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64 && target_cpu != CPU_ARM) return 0;
87
88     for (i = spec->base; i <= spec->limit; i++)
89     {
90         ORDDEF *odp = spec->ordinals[i];
91         if (needs_relay( odp )) return 1;
92     }
93     return 0;
94 }
95
96 /*******************************************************************
97  *         output_relay_debug
98  *
99  * Output entry points for relay debugging
100  */
101 static void output_relay_debug( DLLSPEC *spec )
102 {
103     int i, j;
104     unsigned int pos, args, flags;
105
106     /* first the table of entry point offsets */
107
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" );
111
112     for (i = spec->base; i <= spec->limit; i++)
113     {
114         ORDDEF *odp = spec->ordinals[i];
115
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" );
120     }
121
122     /* then the table of argument types */
123
124     output( "\t.align %d\n", get_alignment(4) );
125     output( ".L__wine_spec_relay_arg_types:\n" );
126
127     for (i = spec->base; i <= spec->limit; i++)
128     {
129         ORDDEF *odp = spec->ordinals[i];
130         unsigned int mask = 0;
131
132         if (needs_relay( odp ))
133         {
134             for (j = pos = 0; pos < 16 && j < odp->u.func.nb_args; j++)
135             {
136                 switch (odp->u.func.args[j])
137                 {
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;
144                 }
145             }
146         }
147         output( "\t.long 0x%08x\n", mask );
148     }
149
150     /* then the relay thunks */
151
152     output( "\t.text\n" );
153     output( "__wine_spec_relay_entry_points:\n" );
154     output( "\tnop\n" );  /* to avoid 0 offset */
155
156     for (i = spec->base; i <= spec->limit; i++)
157     {
158         ORDDEF *odp = spec->ordinals[i];
159
160         if (!needs_relay( odp )) continue;
161
162         output( "\t.align %d\n", get_alignment(4) );
163         output( ".L__wine_spec_relay_entry_point_%d:\n", i );
164         output_cfi( ".cfi_startproc" );
165
166         args = get_args_size(odp) / get_ptr_size();
167         flags = 0;
168
169         switch (target_cpu)
170         {
171         case CPU_x86:
172             if (odp->type == TYPE_THISCALL)  /* add the this pointer */
173             {
174                 output( "\tpopl %%eax\n" );
175                 output( "\tpushl %%ecx\n" );
176                 output( "\tpushl %%eax\n" );
177                 flags |= 2;
178             }
179             if (odp->flags & FLAG_REGISTER)
180                 output( "\tpushl %%eax\n" );
181             else
182                 output( "\tpushl %%esp\n" );
183             output_cfi( ".cfi_adjust_cfa_offset 4" );
184
185             if (odp->flags & FLAG_RET64) flags |= 1;
186             output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
187             output_cfi( ".cfi_adjust_cfa_offset 4" );
188
189             if (UsePIC)
190             {
191                 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
192                 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
193             }
194             else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
195             output( "\tpushl %%eax\n" );
196             output_cfi( ".cfi_adjust_cfa_offset 4" );
197
198             if (odp->flags & FLAG_REGISTER)
199             {
200                 output( "\tcall *8(%%eax)\n" );
201             }
202             else
203             {
204                 output( "\tcall *4(%%eax)\n" );
205                 output_cfi( ".cfi_adjust_cfa_offset -12" );
206                 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
207                     output( "\tret $%u\n", args * get_ptr_size() );
208                 else
209                     output( "\tret\n" );
210             }
211             break;
212
213         case CPU_ARM:
214             switch (args)
215             {
216             default: output( "\tpush {r0-r3}\n" ); break;
217             case 3:  output( "\tpush {r0-r2}\n" ); break;
218             case 2:  output( "\tpush {r0-r1}\n" ); break;
219             case 1:  output( "\tpush {r0}\n" ); break;
220             case 0:  break;
221             }
222             output( "\tpush {LR}\n" );
223             output( "\tmov r2, SP\n");
224             if (odp->flags & FLAG_RET64) flags |= 1;
225             output( "\tmov r1, #%u\n", (flags << 24) );
226             if (args) output( "\tadd r1, #%u\n", (args << 16) );
227             if ((i - spec->base) & 0xf000) output( "\tadd r1, #%u\n", (i - spec->base) & 0xf000 );
228             if ((i - spec->base) & 0x0f00) output( "\tadd r1, #%u\n", (i - spec->base) & 0x0f00 );
229             if ((i - spec->base) & 0x00f0) output( "\tadd r1, #%u\n", (i - spec->base) & 0x00f0 );
230             if ((i - spec->base) & 0x000f) output( "\tadd r1, #%u\n", (i - spec->base) & 0x000f );
231             output( "\tldr r0, [PC, #0]\n");
232             output( "\tmov PC, PC\n");
233             output( "\t.long .L__wine_spec_relay_descr\n" );
234             output( "\tldr r3, [r0, #4]\n");
235             output( "\tblx r3\n");
236             output( "\tpop {r3}\n" );
237             if (args) output( "\tadd SP, SP, #%u\n", min(args*4, 16) );
238             output( "\tbx r3\n");
239             break;
240
241         case CPU_x86_64:
242             output( "\tsubq $40,%%rsp\n" );
243             output_cfi( ".cfi_adjust_cfa_offset 40" );
244             switch (args)
245             {
246             default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
247             /* fall through */
248             case 3:  output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
249             /* fall through */
250             case 2:  output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
251             /* fall through */
252             case 1:  output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
253             /* fall through */
254             case 0:  break;
255             }
256             output( "\tleaq 40(%%rsp),%%r8\n" );
257             output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
258             output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
259             output( "\tcallq *8(%%rcx)\n" );
260             output( "\taddq $40,%%rsp\n" );
261             output_cfi( ".cfi_adjust_cfa_offset -40" );
262             output( "\tret\n" );
263             break;
264
265         default:
266             assert(0);
267         }
268         output_cfi( ".cfi_endproc" );
269     }
270 }
271
272 /*******************************************************************
273  *         output_exports
274  *
275  * Output the export table for a Win32 module.
276  */
277 void output_exports( DLLSPEC *spec )
278 {
279     int i, fwd_size = 0;
280     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
281
282     if (!nr_exports) return;
283
284     output( "\n/* export table */\n\n" );
285     output( "\t.data\n" );
286     output( "\t.align %d\n", get_alignment(4) );
287     output( ".L__wine_spec_exports:\n" );
288
289     /* export directory header */
290
291     output( "\t.long 0\n" );                       /* Characteristics */
292     output( "\t.long 0\n" );                       /* TimeDateStamp */
293     output( "\t.long 0\n" );                       /* MajorVersion/MinorVersion */
294     output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
295     output( "\t.long %u\n", spec->base );          /* Base */
296     output( "\t.long %u\n", nr_exports );          /* NumberOfFunctions */
297     output( "\t.long %u\n", spec->nb_names );      /* NumberOfNames */
298     output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
299     if (spec->nb_names)
300     {
301         output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
302         output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" );  /* AddressOfNameOrdinals */
303     }
304     else
305     {
306         output( "\t.long 0\n" );  /* AddressOfNames */
307         output( "\t.long 0\n" );  /* AddressOfNameOrdinals */
308     }
309
310     /* output the function pointers */
311
312     output( "\n.L__wine_spec_exports_funcs:\n" );
313     for (i = spec->base; i <= spec->limit; i++)
314     {
315         ORDDEF *odp = spec->ordinals[i];
316         if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
317         else switch(odp->type)
318         {
319         case TYPE_EXTERN:
320         case TYPE_STDCALL:
321         case TYPE_VARARGS:
322         case TYPE_CDECL:
323         case TYPE_THISCALL:
324             if (odp->flags & FLAG_FORWARD)
325             {
326                 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
327                 fwd_size += strlen(odp->link_name) + 1;
328             }
329             else if (odp->flags & FLAG_EXT_LINK)
330             {
331                 output( "\t%s %s_%s\n",
332                          get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
333             }
334             else
335             {
336                 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
337             }
338             break;
339         case TYPE_STUB:
340             output( "\t%s %s\n", get_asm_ptr_keyword(),
341                      asm_name( get_stub_name( odp, spec )) );
342             break;
343         default:
344             assert(0);
345         }
346     }
347
348     if (spec->nb_names)
349     {
350         /* output the function name pointers */
351
352         int namepos = strlen(spec->file_name) + 1;
353
354         output( "\n.L__wine_spec_exp_name_ptrs:\n" );
355         for (i = 0; i < spec->nb_names; i++)
356         {
357             output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
358             namepos += strlen(spec->names[i]->name) + 1;
359         }
360
361         /* output the function ordinals */
362
363         output( "\n.L__wine_spec_exp_ordinals:\n" );
364         for (i = 0; i < spec->nb_names; i++)
365         {
366             output( "\t%s %d\n",
367                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
368         }
369         if (spec->nb_names % 2)
370         {
371             output( "\t%s 0\n", get_asm_short_keyword() );
372         }
373     }
374
375     /* output the export name strings */
376
377     output( "\n.L__wine_spec_exp_names:\n" );
378     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
379     for (i = 0; i < spec->nb_names; i++)
380         output( "\t%s \"%s\"\n",
381                  get_asm_string_keyword(), spec->names[i]->name );
382
383     /* output forward strings */
384
385     if (fwd_size)
386     {
387         output( "\n.L__wine_spec_forwards:\n" );
388         for (i = spec->base; i <= spec->limit; i++)
389         {
390             ORDDEF *odp = spec->ordinals[i];
391             if (odp && (odp->flags & FLAG_FORWARD))
392                 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
393         }
394     }
395     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
396     output( ".L__wine_spec_exports_end:\n" );
397
398     /* output relays */
399
400     if (!has_relays( spec ))
401     {
402         output( "\t%s 0\n", get_asm_ptr_keyword() );
403         return;
404     }
405
406     output( ".L__wine_spec_relay_descr:\n" );
407     output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() );  /* magic */
408     output( "\t%s 0,0\n", get_asm_ptr_keyword() );         /* relay funcs */
409     output( "\t%s 0\n", get_asm_ptr_keyword() );           /* private data */
410     output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
411     output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
412     output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
413
414     output_relay_debug( spec );
415 }
416
417
418 /*******************************************************************
419  *         output_asm_constructor
420  *
421  * Output code for calling a dll constructor.
422  */
423 static void output_asm_constructor( const char *constructor )
424 {
425     if (target_platform == PLATFORM_APPLE)
426     {
427         /* Mach-O doesn't have an init section */
428         output( "\n\t.mod_init_func\n" );
429         output( "\t.align %d\n", get_alignment(4) );
430         output( "\t.long %s\n", asm_name(constructor) );
431     }
432     else
433     {
434         switch(target_cpu)
435         {
436         case CPU_x86:
437         case CPU_x86_64:
438             output( "\n\t.section \".init\",\"ax\"\n" );
439             output( "\tcall %s\n", asm_name(constructor) );
440             break;
441         case CPU_SPARC:
442             output( "\n\t.section \".init\",\"ax\"\n" );
443             output( "\tcall %s\n", asm_name(constructor) );
444             output( "\tnop\n" );
445             break;
446         case CPU_ARM:
447             output( "\n\t.section \".text\",\"ax\"\n" );
448             output( "\tblx %s\n", asm_name(constructor) );
449             break;
450         case CPU_POWERPC:
451             output( "\n\t.section \".init\",\"ax\"\n" );
452             output( "\tbl %s\n", asm_name(constructor) );
453             break;
454         }
455     }
456 }
457
458
459 /*******************************************************************
460  *         output_module
461  *
462  * Output the module data.
463  */
464 void output_module( DLLSPEC *spec )
465 {
466     int machine = 0;
467     unsigned int page_size = get_page_size();
468
469     /* Reserve some space for the PE header */
470
471     switch (target_platform)
472     {
473     case PLATFORM_APPLE:
474         output( "\t.text\n" );
475         output( "\t.align %d\n", get_alignment(page_size) );
476         output( "__wine_spec_pe_header:\n" );
477         output( "\t.space 65536\n" );
478         break;
479     case PLATFORM_SOLARIS:
480         output( "\n\t.section \".text\",\"ax\"\n" );
481         output( "__wine_spec_pe_header:\n" );
482         output( "\t.skip %u\n", 65536 + page_size );
483         break;
484     default:
485         switch(target_cpu)
486         {
487         case CPU_x86:
488         case CPU_x86_64:
489         case CPU_SPARC:
490             output( "\n\t.section \".init\",\"ax\"\n" );
491             output( "\tjmp 1f\n" );
492             break;
493         case CPU_ARM:
494             output( "\n\t.section \".text\",\"ax\"\n" );
495             output( "\tb 1f\n" );
496             break;
497         case CPU_POWERPC:
498             output( "\n\t.section \".init\",\"ax\"\n" );
499             output( "\tb 1f\n" );
500             break;
501         }
502         output( "__wine_spec_pe_header:\n" );
503         output( "\t.skip %u\n", 65536 + page_size );
504         output( "1:\n" );
505         break;
506     }
507
508     /* Output the NT header */
509
510     output( "\n\t.data\n" );
511     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
512     output( "%s\n", asm_globl("__wine_spec_nt_header") );
513     output( ".L__wine_spec_rva_base:\n" );
514
515     output( "\t.long 0x4550\n" );         /* Signature */
516     switch(target_cpu)
517     {
518     case CPU_x86:     machine = IMAGE_FILE_MACHINE_I386; break;
519     case CPU_x86_64:  machine = IMAGE_FILE_MACHINE_AMD64; break;
520     case CPU_ARM:     machine = IMAGE_FILE_MACHINE_ARMNT; break;
521     case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
522     case CPU_SPARC:   machine = IMAGE_FILE_MACHINE_SPARC; break;
523     }
524     output( "\t%s 0x%04x\n",              /* Machine */
525              get_asm_short_keyword(), machine );
526     output( "\t%s 0\n",                   /* NumberOfSections */
527              get_asm_short_keyword() );
528     output( "\t.long 0\n" );              /* TimeDateStamp */
529     output( "\t.long 0\n" );              /* PointerToSymbolTable */
530     output( "\t.long 0\n" );              /* NumberOfSymbols */
531     output( "\t%s %d\n",                  /* SizeOfOptionalHeader */
532              get_asm_short_keyword(),
533              get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
534     output( "\t%s 0x%04x\n",              /* Characteristics */
535              get_asm_short_keyword(), spec->characteristics );
536     output( "\t%s 0x%04x\n",              /* Magic */
537              get_asm_short_keyword(),
538              get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
539     output( "\t.byte 0\n" );              /* MajorLinkerVersion */
540     output( "\t.byte 0\n" );              /* MinorLinkerVersion */
541     output( "\t.long 0\n" );              /* SizeOfCode */
542     output( "\t.long 0\n" );              /* SizeOfInitializedData */
543     output( "\t.long 0\n" );              /* SizeOfUninitializedData */
544     /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
545     output( "\t%s %s\n",                  /* AddressOfEntryPoint */
546             get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
547     if (get_ptr_size() == 4)
548     {
549         output( "\t.long 0\n" );          /* BaseOfCode */
550         output( "\t.long 0\n" );          /* BaseOfData */
551     }
552     output( "\t%s __wine_spec_pe_header\n",         /* ImageBase */
553              get_asm_ptr_keyword() );
554     output( "\t.long %u\n", page_size );  /* SectionAlignment */
555     output( "\t.long %u\n", page_size );  /* FileAlignment */
556     output( "\t%s 1,0\n",                 /* Major/MinorOperatingSystemVersion */
557              get_asm_short_keyword() );
558     output( "\t%s 0,0\n",                 /* Major/MinorImageVersion */
559              get_asm_short_keyword() );
560     output( "\t%s %u,%u\n",               /* Major/MinorSubsystemVersion */
561              get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
562     output( "\t.long 0\n" );                          /* Win32VersionValue */
563     output( "\t.long %s-.L__wine_spec_rva_base\n",    /* SizeOfImage */
564              asm_name("_end") );
565     output( "\t.long %u\n", page_size );  /* SizeOfHeaders */
566     output( "\t.long 0\n" );              /* CheckSum */
567     output( "\t%s 0x%04x\n",              /* Subsystem */
568              get_asm_short_keyword(), spec->subsystem );
569     output( "\t%s 0x%04x\n",              /* DllCharacteristics */
570             get_asm_short_keyword(), spec->dll_characteristics );
571     output( "\t%s %u,%u\n",               /* SizeOfStackReserve/Commit */
572              get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
573     output( "\t%s %u,%u\n",               /* SizeOfHeapReserve/Commit */
574              get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
575     output( "\t.long 0\n" );              /* LoaderFlags */
576     output( "\t.long 16\n" );             /* NumberOfRvaAndSizes */
577
578     if (spec->base <= spec->limit)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
579         output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
580                  ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
581     else
582         output( "\t.long 0,0\n" );
583
584     if (has_imports())   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
585         output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
586                  ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
587     else
588         output( "\t.long 0,0\n" );
589
590     if (spec->nb_resources)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
591         output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
592                  ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
593     else
594         output( "\t.long 0,0\n" );
595
596     output( "\t.long 0,0\n" );  /* DataDirectory[3] */
597     output( "\t.long 0,0\n" );  /* DataDirectory[4] */
598     output( "\t.long 0,0\n" );  /* DataDirectory[5] */
599     output( "\t.long 0,0\n" );  /* DataDirectory[6] */
600     output( "\t.long 0,0\n" );  /* DataDirectory[7] */
601     output( "\t.long 0,0\n" );  /* DataDirectory[8] */
602     output( "\t.long 0,0\n" );  /* DataDirectory[9] */
603     output( "\t.long 0,0\n" );  /* DataDirectory[10] */
604     output( "\t.long 0,0\n" );  /* DataDirectory[11] */
605     output( "\t.long 0,0\n" );  /* DataDirectory[12] */
606     output( "\t.long 0,0\n" );  /* DataDirectory[13] */
607     output( "\t.long 0,0\n" );  /* DataDirectory[14] */
608     output( "\t.long 0,0\n" );  /* DataDirectory[15] */
609
610     output( "\n\t%s\n", get_asm_string_section() );
611     output( "%s\n", asm_globl("__wine_spec_file_name") );
612     output( ".L__wine_spec_file_name:\n" );
613     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
614     if (target_platform == PLATFORM_APPLE)
615         output( "\t.lcomm %s,4\n", asm_name("_end") );
616
617     output_asm_constructor( "__wine_spec_init_ctor" );
618 }
619
620
621 /*******************************************************************
622  *         BuildSpec32File
623  *
624  * Build a Win32 C file from a spec file.
625  */
626 void BuildSpec32File( DLLSPEC *spec )
627 {
628     resolve_imports( spec );
629     output_standard_file_header();
630     output_module( spec );
631     output_stubs( spec );
632     output_exports( spec );
633     output_imports( spec );
634     if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
635     output_resources( spec );
636     output_gnu_stack_note();
637 }
638
639
640 /*******************************************************************
641  *         output_fake_module
642  *
643  * Build a fake binary module from a spec file.
644  */
645 void output_fake_module( DLLSPEC *spec )
646 {
647     static const unsigned char dll_code_section[] = { 0x31, 0xc0,          /* xor %eax,%eax */
648                                                       0xc2, 0x0c, 0x00 };  /* ret $12 */
649
650     static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00,  /* movl $1,%eax */
651                                                       0xc2, 0x04, 0x00 };            /* ret $4 */
652
653     static const char fakedll_signature[] = "Wine placeholder DLL";
654     const unsigned int page_size = get_page_size();
655     const unsigned int section_align = page_size;
656     const unsigned int file_align = 0x200;
657     const unsigned int reloc_size = 8;
658     const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
659     const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
660     const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
661                                     sizeof(dll_code_section) : sizeof(exe_code_section);
662     unsigned char *resources;
663     unsigned int resources_size;
664     unsigned int image_size = 3 * section_align;
665
666     resolve_imports( spec );
667     output_bin_resources( spec, 3 * section_align );
668     resources = output_buffer;
669     resources_size = output_buffer_pos;
670     if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
671
672     init_output_buffer();
673
674     put_word( 0x5a4d );       /* e_magic */
675     put_word( 0x40 );         /* e_cblp */
676     put_word( 0x01 );         /* e_cp */
677     put_word( 0 );            /* e_crlc */
678     put_word( lfanew / 16 );  /* e_cparhdr */
679     put_word( 0x0000 );       /* e_minalloc */
680     put_word( 0xffff );       /* e_maxalloc */
681     put_word( 0x0000 );       /* e_ss */
682     put_word( 0x00b8 );       /* e_sp */
683     put_word( 0 );            /* e_csum */
684     put_word( 0 );            /* e_ip */
685     put_word( 0 );            /* e_cs */
686     put_word( lfanew );       /* e_lfarlc */
687     put_word( 0 );            /* e_ovno */
688     put_dword( 0 );           /* e_res */
689     put_dword( 0 );
690     put_word( 0 );            /* e_oemid */
691     put_word( 0 );            /* e_oeminfo */
692     put_dword( 0 );           /* e_res2 */
693     put_dword( 0 );
694     put_dword( 0 );
695     put_dword( 0 );
696     put_dword( 0 );
697     put_dword( lfanew );
698
699     put_data( fakedll_signature, sizeof(fakedll_signature) );
700     align_output( 16 );
701
702     put_dword( 0x4550 );                             /* Signature */
703     switch(target_cpu)
704     {
705     case CPU_x86:     put_word( IMAGE_FILE_MACHINE_I386 ); break;
706     case CPU_x86_64:  put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
707     case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
708     case CPU_SPARC:   put_word( IMAGE_FILE_MACHINE_SPARC ); break;
709     case CPU_ARM:     put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
710     }
711     put_word( nb_sections );                         /* NumberOfSections */
712     put_dword( 0 );                                  /* TimeDateStamp */
713     put_dword( 0 );                                  /* PointerToSymbolTable */
714     put_dword( 0 );                                  /* NumberOfSymbols */
715     put_word( get_ptr_size() == 8 ?
716               IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
717               IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );   /* SizeOfOptionalHeader */
718     put_word( spec->characteristics );               /* Characteristics */
719     put_word( get_ptr_size() == 8 ?
720               IMAGE_NT_OPTIONAL_HDR64_MAGIC :
721               IMAGE_NT_OPTIONAL_HDR32_MAGIC );       /* Magic */
722     put_byte(  0 );                                  /* MajorLinkerVersion */
723     put_byte(  0 );                                  /* MinorLinkerVersion */
724     put_dword( text_size );                          /* SizeOfCode */
725     put_dword( 0 );                                  /* SizeOfInitializedData */
726     put_dword( 0 );                                  /* SizeOfUninitializedData */
727     put_dword( section_align );                      /* AddressOfEntryPoint */
728     put_dword( section_align );                      /* BaseOfCode */
729     if (get_ptr_size() == 4) put_dword( 0 );         /* BaseOfData */
730     put_pword( 0x10000000 );                         /* ImageBase */
731     put_dword( section_align );                      /* SectionAlignment */
732     put_dword( file_align );                         /* FileAlignment */
733     put_word( 1 );                                   /* MajorOperatingSystemVersion */
734     put_word( 0 );                                   /* MinorOperatingSystemVersion */
735     put_word( 0 );                                   /* MajorImageVersion */
736     put_word( 0 );                                   /* MinorImageVersion */
737     put_word( spec->subsystem_major );               /* MajorSubsystemVersion */
738     put_word( spec->subsystem_minor );               /* MinorSubsystemVersion */
739     put_dword( 0 );                                  /* Win32VersionValue */
740     put_dword( image_size );                         /* SizeOfImage */
741     put_dword( file_align );                         /* SizeOfHeaders */
742     put_dword( 0 );                                  /* CheckSum */
743     put_word( spec->subsystem );                     /* Subsystem */
744     put_word( spec->dll_characteristics );           /* DllCharacteristics */
745     put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
746     put_pword( page_size );                          /* SizeOfStackCommit */
747     put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 );   /* SizeOfHeapReserve */
748     put_pword( page_size );                          /* SizeOfHeapCommit */
749     put_dword( 0 );                                  /* LoaderFlags */
750     put_dword( 16 );                                 /* NumberOfRvaAndSizes */
751
752     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
753     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
754     if (resources_size)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
755     {
756         put_dword( 3 * section_align );
757         put_dword( resources_size );
758     }
759     else
760     {
761         put_dword( 0 );
762         put_dword( 0 );
763     }
764
765     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
766     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
767     put_dword( 2 * section_align );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
768     put_dword( reloc_size );
769     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
770     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
771     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
772     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
773     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
774     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
775     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
776     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
777     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
778     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[15] */
779
780     /* .text section */
781     put_data( ".text\0\0", 8 );    /* Name */
782     put_dword( section_align );    /* VirtualSize */
783     put_dword( section_align );    /* VirtualAddress */
784     put_dword( text_size );        /* SizeOfRawData */
785     put_dword( file_align );       /* PointerToRawData */
786     put_dword( 0 );                /* PointerToRelocations */
787     put_dword( 0 );                /* PointerToLinenumbers */
788     put_word( 0 );                 /* NumberOfRelocations */
789     put_word( 0 );                 /* NumberOfLinenumbers */
790     put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics  */
791
792     /* .reloc section */
793     put_data( ".reloc\0", 8 );     /* Name */
794     put_dword( section_align );    /* VirtualSize */
795     put_dword( 2 * section_align );/* VirtualAddress */
796     put_dword( reloc_size );       /* SizeOfRawData */
797     put_dword( 2 * file_align );   /* PointerToRawData */
798     put_dword( 0 );                /* PointerToRelocations */
799     put_dword( 0 );                /* PointerToLinenumbers */
800     put_word( 0 );                 /* NumberOfRelocations */
801     put_word( 0 );                 /* NumberOfLinenumbers */
802     put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
803
804     /* .rsrc section */
805     if (resources_size)
806     {
807         put_data( ".rsrc\0\0", 8 );    /* Name */
808         put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
809         put_dword( 3 * section_align );/* VirtualAddress */
810         put_dword( resources_size );   /* SizeOfRawData */
811         put_dword( 3 * file_align );   /* PointerToRawData */
812         put_dword( 0 );                /* PointerToRelocations */
813         put_dword( 0 );                /* PointerToLinenumbers */
814         put_word( 0 );                 /* NumberOfRelocations */
815         put_word( 0 );                 /* NumberOfLinenumbers */
816         put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
817     }
818
819     /* .text contents */
820     align_output( file_align );
821     if (spec->characteristics & IMAGE_FILE_DLL)
822         put_data( dll_code_section, sizeof(dll_code_section) );
823     else
824         put_data( exe_code_section, sizeof(exe_code_section) );
825
826     /* .reloc contents */
827     align_output( file_align );
828     put_dword( 0 );   /* VirtualAddress */
829     put_dword( 0 );   /* SizeOfBlock */
830
831     /* .rsrc contents */
832     if (resources_size)
833     {
834         align_output( file_align );
835         put_data( resources, resources_size );
836     }
837     flush_output_buffer();
838 }
839
840
841 /*******************************************************************
842  *         output_def_file
843  *
844  * Build a Win32 def file from a spec file.
845  */
846 void output_def_file( DLLSPEC *spec, int include_private )
847 {
848     DLLSPEC *spec32 = NULL;
849     const char *name;
850     int i, total;
851
852     if (spec->type == SPEC_WIN16)
853     {
854         spec32 = alloc_dll_spec();
855         add_16bit_exports( spec32, spec );
856         spec = spec32;
857     }
858
859     if (spec_file_name)
860         output( "; File generated automatically from %s; do not edit!\n\n",
861                  spec_file_name );
862     else
863         output( "; File generated automatically; do not edit!\n\n" );
864
865     output( "LIBRARY %s\n\n", spec->file_name);
866     output( "EXPORTS\n");
867
868     /* Output the exports and relay entry points */
869
870     for (i = total = 0; i < spec->nb_entry_points; i++)
871     {
872         const ORDDEF *odp = &spec->entry_points[i];
873         int is_data = 0;
874
875         if (!odp) continue;
876
877         if (odp->name) name = odp->name;
878         else if (odp->export_name) name = odp->export_name;
879         else continue;
880
881         if (!(odp->flags & FLAG_PRIVATE)) total++;
882         else if (!include_private) continue;
883
884         if (odp->type == TYPE_STUB) continue;
885
886         output( "  %s", name );
887
888         switch(odp->type)
889         {
890         case TYPE_EXTERN:
891             is_data = 1;
892             /* fall through */
893         case TYPE_VARARGS:
894         case TYPE_CDECL:
895         case TYPE_THISCALL:
896             /* try to reduce output */
897             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
898                 output( "=%s", odp->link_name );
899             break;
900         case TYPE_STDCALL:
901         {
902             int at_param = get_args_size( odp );
903             if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
904             if  (odp->flags & FLAG_FORWARD)
905             {
906                 output( "=%s", odp->link_name );
907             }
908             else if (strcmp(name, odp->link_name)) /* try to reduce output */
909             {
910                 output( "=%s", odp->link_name );
911                 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
912             }
913             break;
914         }
915         default:
916             assert(0);
917         }
918         output( " @%d", odp->ordinal );
919         if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
920         if (is_data) output( " DATA" );
921         if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
922         output( "\n" );
923     }
924     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
925     if (spec32) free_dll_spec( spec32 );
926 }