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