Output the resource data in assembly too.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "windef.h"
34 #include "winbase.h"
35 #include "wine/exception.h"
36 #include "build.h"
37
38
39 static int string_compare( const void *ptr1, const void *ptr2 )
40 {
41     const char * const *str1 = ptr1;
42     const char * const *str2 = ptr2;
43     return strcmp( *str1, *str2 );
44 }
45
46
47 /*******************************************************************
48  *         make_internal_name
49  *
50  * Generate an internal name for an entry point. Used for stubs etc.
51  */
52 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
53 {
54     static char buffer[256];
55     if (odp->name || odp->export_name)
56     {
57         char *p;
58         sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->file_name,
59                  odp->name ? odp->name : odp->export_name );
60         /* make sure name is a legal C identifier */
61         for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
62         if (!*p) return buffer;
63     }
64     sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
65     return buffer;
66 }
67
68
69 /*******************************************************************
70  *         output_debug
71  *
72  * Output the debug channels.
73  */
74 static int output_debug( FILE *outfile )
75 {
76     int i;
77
78     if (!nb_debug_channels) return 0;
79     qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
80
81     for (i = 0; i < nb_debug_channels; i++)
82         fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
83                  debug_channels[i], debug_channels[i] );
84
85     fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
86     for (i = 0; i < nb_debug_channels; i++)
87     {
88         fprintf( outfile, "    __wine_dbch_%s", debug_channels[i] );
89         if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
90     }
91     fprintf( outfile, "\n};\n\n" );
92     fprintf( outfile, "static void *debug_registration;\n\n" );
93
94     return nb_debug_channels;
95 }
96
97
98 /*******************************************************************
99  *         output_exports
100  *
101  * Output the export table for a Win32 module.
102  */
103 static void output_exports( FILE *outfile, DLLSPEC *spec )
104 {
105     int i, fwd_size = 0;
106     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
107
108     if (!nr_exports) return;
109
110     fprintf( outfile, "/* export table */\n" );
111     fprintf( outfile, "asm(\".data\\n\"\n" );
112     fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
113     fprintf( outfile, "    \".L__wine_spec_exports:\\n\"\n" );
114
115     /* export directory header */
116
117     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* Characteristics */
118     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* TimeDateStamp */
119     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* MajorVersion/MinorVersion */
120     fprintf( outfile, "    \"\\t.long .L__wine_spec_exp_names\\n\"\n" ); /* Name */
121     fprintf( outfile, "    \"\\t.long %d\\n\"\n", spec->base );        /* Base */
122     fprintf( outfile, "    \"\\t.long %d\\n\"\n", nr_exports );        /* NumberOfFunctions */
123     fprintf( outfile, "    \"\\t.long %d\\n\"\n", spec->nb_names );    /* NumberOfNames */
124     fprintf( outfile, "    \"\\t.long .L__wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
125     if (spec->nb_names)
126     {
127         fprintf( outfile, "    \"\\t.long .L__wine_spec_exp_name_ptrs\\n\"\n" );     /* AddressOfNames */
128         fprintf( outfile, "    \"\\t.long .L__wine_spec_exp_ordinals\\n\"\n" );  /* AddressOfNameOrdinals */
129     }
130     else
131     {
132         fprintf( outfile, "    \"\\t.long 0\\n\"\n" );  /* AddressOfNames */
133         fprintf( outfile, "    \"\\t.long 0\\n\"\n" );  /* AddressOfNameOrdinals */
134     }
135
136     /* output the function pointers */
137
138     fprintf( outfile, "    \".L__wine_spec_exports_funcs:\\n\"\n" );
139     for (i = spec->base; i <= spec->limit; i++)
140     {
141         ORDDEF *odp = spec->ordinals[i];
142         if (!odp) fprintf( outfile, "    \"\\t.long 0\\n\"\n" );
143         else switch(odp->type)
144         {
145         case TYPE_EXTERN:
146         case TYPE_STDCALL:
147         case TYPE_VARARGS:
148         case TYPE_CDECL:
149             if (!(odp->flags & FLAG_FORWARD))
150             {
151                 fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(odp->link_name) );
152             }
153             else
154             {
155                 fprintf( outfile, "    \"\\t.long .L__wine_spec_forwards+%d\\n\"\n", fwd_size );
156                 fwd_size += strlen(odp->link_name) + 1;
157             }
158             break;
159         case TYPE_STUB:
160             fprintf( outfile, "    \"\\t.long %s\\n\"\n",
161                      asm_name( make_internal_name( odp, spec, "stub" )) );
162             break;
163         default:
164             assert(0);
165         }
166     }
167
168     if (spec->nb_names)
169     {
170         /* output the function name pointers */
171
172         int namepos = strlen(spec->file_name) + 1;
173
174         fprintf( outfile, "    \".L__wine_spec_exp_name_ptrs:\\n\"\n" );
175         for (i = 0; i < spec->nb_names; i++)
176         {
177             fprintf( outfile, "    \"\\t.long .L__wine_spec_exp_names+%d\\n\"\n", namepos );
178             namepos += strlen(spec->names[i]->name) + 1;
179         }
180
181         /* output the function ordinals */
182
183         fprintf( outfile, "    \".L__wine_spec_exp_ordinals:\\n\"\n" );
184         for (i = 0; i < spec->nb_names; i++)
185         {
186             fprintf( outfile, "    \"\\t%s %d\\n\"\n",
187                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
188         }
189         if (spec->nb_names % 2)
190         {
191             fprintf( outfile, "    \"\\t%s 0\\n\"\n", get_asm_short_keyword() );
192         }
193     }
194
195     /* output the export name strings */
196
197     fprintf( outfile, "    \".L__wine_spec_exp_names:\\n\"\n" );
198     fprintf( outfile, "    \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), spec->file_name );
199     for (i = 0; i < spec->nb_names; i++)
200         fprintf( outfile, "    \"\\t%s \\\"%s\\\"\\n\"\n",
201                  get_asm_string_keyword(), spec->names[i]->name );
202
203     /* output forward strings */
204
205     if (fwd_size)
206     {
207         fprintf( outfile, "    \".L__wine_spec_forwards:\\n\"\n" );
208         for (i = spec->base; i <= spec->limit; i++)
209         {
210             ORDDEF *odp = spec->ordinals[i];
211             if (odp && (odp->flags & FLAG_FORWARD))
212                 fprintf( outfile, "    \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), odp->link_name );
213         }
214     }
215     fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
216     fprintf( outfile, "    \".L__wine_spec_exports_end:\\n\"\n" );
217
218     /* output relays */
219
220     /* we only support relay debugging on i386 */
221     if (target_cpu == CPU_x86)
222     {
223         for (i = spec->base; i <= spec->limit; i++)
224         {
225             ORDDEF *odp = spec->ordinals[i];
226             unsigned int j, args, mask = 0;
227
228             /* skip nonexistent entry points */
229             if (!odp) goto ignore;
230             /* skip non-functions */
231             if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
232             /* skip norelay and forward entry points */
233             if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
234
235             for (j = 0; odp->u.func.arg_types[j]; j++)
236             {
237                 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
238                 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
239             }
240             if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
241
242             args = strlen(odp->u.func.arg_types) * get_ptr_size();
243
244             switch(odp->type)
245             {
246             case TYPE_STDCALL:
247                 fprintf( outfile, "    \"\\tjmp %s\\n\"\n", asm_name(odp->link_name) );
248                 fprintf( outfile, "    \"\\tret $%d\\n\"\n", args );
249                 fprintf( outfile, "    \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp->link_name), mask );
250                 break;
251             case TYPE_CDECL:
252                 fprintf( outfile, "    \"\\tjmp %s\\n\"\n", asm_name(odp->link_name) );
253                 fprintf( outfile, "    \"\\tret\\n\"\n" );
254                 fprintf( outfile, "    \"\\t%s %d\\n\"\n", get_asm_short_keyword(), args );
255                 fprintf( outfile, "    \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp->link_name), mask );
256                 break;
257             default:
258                 assert(0);
259             }
260             continue;
261
262         ignore:
263             fprintf( outfile, "    \"\\t.long 0,0,0,0\\n\"\n" );
264         }
265     }
266     else fprintf( outfile, "    \"\\t.long 0\\n\"\n" );
267
268     fprintf( outfile, ");\n" );
269 }
270
271
272 /*******************************************************************
273  *         output_stubs
274  *
275  * Output the functions for stub entry points
276 */
277 static void output_stubs( FILE *outfile, DLLSPEC *spec )
278 {
279     const char *name, *exp_name;
280     int i, pos;
281
282     if (!has_stubs( spec )) return;
283
284     fprintf( outfile, "asm(\".text\\n\"\n" );
285
286     for (i = pos = 0; i < spec->nb_entry_points; i++)
287     {
288         ORDDEF *odp = &spec->entry_points[i];
289         if (odp->type != TYPE_STUB) continue;
290
291         name = make_internal_name( odp, spec, "stub" );
292         exp_name = odp->name ? odp->name : odp->export_name;
293         fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
294         fprintf( outfile, "    \"\\t%s\\n\"\n", func_declaration(name) );
295         fprintf( outfile, "    \"%s:\\n\"\n", asm_name(name) );
296         fprintf( outfile, "    \"\\tcall .L__wine_stub_getpc_%d\\n\"\n", i );
297         fprintf( outfile, "    \".L__wine_stub_getpc_%d:\\n\"\n", i );
298         fprintf( outfile, "    \"\\tpopl %%eax\\n\"\n" );
299         if (exp_name)
300         {
301             fprintf( outfile, "    \"\\tleal .L__wine_stub_strings+%d-.L__wine_stub_getpc_%d(%%eax),%%ecx\\n\"\n",
302                      pos, i );
303             fprintf( outfile, "    \"\\tpushl %%ecx\\n\"\n" );
304             pos += strlen(exp_name) + 1;
305         }
306         else
307             fprintf( outfile, "    \"\\tpushl $%d\\n\"\n", odp->ordinal );
308         fprintf( outfile, "    \"\\tleal %s-.L__wine_stub_getpc_%d(%%eax),%%ecx\\n\"\n",
309                  asm_name("__wine_spec_file_name"), i );
310         fprintf( outfile, "    \"\\tpushl %%ecx\\n\"\n" );
311         fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name("__wine_spec_unimplemented_stub") );
312         fprintf( outfile, "    \"\\t%s\\n\"\n", func_size(name) );
313     }
314
315     if (pos)
316     {
317         fprintf( outfile, "    \"\\t%s\\n\"\n", get_asm_string_section() );
318         fprintf( outfile, "    \".L__wine_stub_strings:\\n\"\n" );
319         for (i = 0; i < spec->nb_entry_points; i++)
320         {
321             ORDDEF *odp = &spec->entry_points[i];
322             if (odp->type != TYPE_STUB) continue;
323             exp_name = odp->name ? odp->name : odp->export_name;
324             if (exp_name)
325                 fprintf( outfile, "    \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), exp_name );
326         }
327     }
328
329     fprintf( outfile, ");\n" );
330 }
331
332
333 /*******************************************************************
334  *         output_dll_init
335  *
336  * Output code for calling a dll constructor and destructor.
337  */
338 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
339 {
340     if (target_platform == PLATFORM_APPLE)
341     {
342         /* Mach-O doesn't have an init section */
343         if (constructor)
344         {
345             fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
346             fprintf( outfile, "    \"\\t.align 2\\n\"\n" );
347             fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(constructor) );
348             fprintf( outfile, "    \"\\t.text\\n\");\n" );
349         }
350         if (destructor)
351         {
352             fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
353             fprintf( outfile, "    \"\\t.align 2\\n\"\n" );
354             fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(destructor) );
355             fprintf( outfile, "    \"\\t.text\\n\");\n" );
356         }
357     }
358     else switch(target_cpu)
359     {
360     case CPU_x86:
361     case CPU_x86_64:
362         if (constructor)
363         {
364             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
365             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(constructor) );
366             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
367         }
368         if (destructor)
369         {
370             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
371             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(destructor) );
372             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
373         }
374         break;
375     case CPU_SPARC:
376         if (constructor)
377         {
378             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
379             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(constructor) );
380             fprintf( outfile, "    \"\\tnop\\n\"\n" );
381             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
382         }
383         if (destructor)
384         {
385             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
386             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(destructor) );
387             fprintf( outfile, "    \"\\tnop\\n\"\n" );
388             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
389         }
390         break;
391     case CPU_ALPHA:
392         if (constructor)
393         {
394             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
395             fprintf( outfile, "    \"\\tjsr $26,%s\\n\"\n", asm_name(constructor) );
396             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
397         }
398         if (destructor)
399         {
400             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
401             fprintf( outfile, "    \"\\tjsr $26,%s\\n\"\n", asm_name(destructor) );
402             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
403         }
404         break;
405     case CPU_POWERPC:
406         if (constructor)
407         {
408             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
409             fprintf( outfile, "    \"\\tbl %s\\n\"\n", asm_name(constructor) );
410             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
411         }
412         if (destructor)
413         {
414             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
415             fprintf( outfile, "    \"\\tbl %s\\n\"\n", asm_name(destructor) );
416             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
417         }
418         break;
419     }
420 }
421
422
423 /*******************************************************************
424  *         BuildSpec32File
425  *
426  * Build a Win32 C file from a spec file.
427  */
428 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
429 {
430     int machine = 0;
431     unsigned int page_size = get_page_size();
432
433     resolve_imports( spec );
434     output_standard_file_header( outfile );
435
436     /* Reserve some space for the PE header */
437
438     fprintf( outfile, "#ifndef __GNUC__\n" );
439     fprintf( outfile, "static void __asm__dummy(void) {\n" );
440     fprintf( outfile, "#endif\n" );
441
442     fprintf( outfile, "asm(\".text\\n\\t\"\n" );
443     fprintf( outfile, "    \".align %d\\n\"\n", get_alignment(page_size) );
444     fprintf( outfile, "    \"__wine_spec_pe_header:\\t\"\n" );
445     if (target_platform == PLATFORM_APPLE)
446         fprintf( outfile, "    \".space 65536\\n\\t\"\n" );
447     else
448         fprintf( outfile, "    \".skip 65536\\n\\t\"\n" );
449
450     /* Output the NT header */
451
452     fprintf( outfile, "    \"\\t.data\\n\\t\"\n" );
453     fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(get_ptr_size()) );
454     fprintf( outfile, "    \"\\t.globl %s\\n\"\n", asm_name("__wine_spec_nt_header") );
455     fprintf( outfile, "    \"%s:\\n\"\n", asm_name("__wine_spec_nt_header"));
456
457     fprintf( outfile, "    \"\\t.long 0x%04x\\n\"\n", IMAGE_NT_SIGNATURE );    /* Signature */
458     switch(target_cpu)
459     {
460     case CPU_x86:     machine = IMAGE_FILE_MACHINE_I386; break;
461     case CPU_x86_64:  machine = IMAGE_FILE_MACHINE_AMD64; break;
462     case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
463     case CPU_ALPHA:   machine = IMAGE_FILE_MACHINE_ALPHA; break;
464     case CPU_SPARC:   machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
465     }
466     fprintf( outfile, "    \"\\t%s 0x%04x\\n\"\n",              /* Machine */
467              get_asm_short_keyword(), machine );
468     fprintf( outfile, "    \"\\t%s 0\\n\"\n",                   /* NumberOfSections */
469              get_asm_short_keyword() );
470     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* TimeDateStamp */
471     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* PointerToSymbolTable */
472     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* NumberOfSymbols */
473     fprintf( outfile, "    \"\\t%s %d\\n\"\n",                  /* SizeOfOptionalHeader */
474              get_asm_short_keyword(),
475              get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
476     fprintf( outfile, "    \"\\t%s 0x%04x\\n\"\n",              /* Characteristics */
477              get_asm_short_keyword(), spec->characteristics );
478     fprintf( outfile, "    \"\\t%s 0x%04x\\n\"\n",              /* Magic */
479              get_asm_short_keyword(),
480              get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
481     fprintf( outfile, "    \"\\t.byte 0\\n\"\n" );              /* MajorLinkerVersion */
482     fprintf( outfile, "    \"\\t.byte 0\\n\"\n" );              /* MinorLinkerVersion */
483     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* SizeOfCode */
484     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* SizeOfInitializedData */
485     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* SizeOfUninitializedData */
486     fprintf( outfile, "    \"\\t.long %s\\n\"\n",               /* AddressOfEntryPoint */
487              asm_name(spec->init_func) );
488     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* BaseOfCode */
489     if (get_ptr_size() == 4)
490         fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name("__wine_spec_nt_header") ); /* BaseOfData */
491     fprintf( outfile, "    \"\\t%s __wine_spec_pe_header\\n\"\n",         /* ImageBase */
492              get_asm_ptr_keyword() );
493     fprintf( outfile, "    \"\\t.long %u\\n\"\n", page_size );  /* SectionAlignment */
494     fprintf( outfile, "    \"\\t.long %u\\n\"\n", page_size );  /* FileAlignment */
495     fprintf( outfile, "    \"\\t%s 1,0\\n\"\n",                 /* Major/MinorOperatingSystemVersion */
496              get_asm_short_keyword() );
497     fprintf( outfile, "    \"\\t%s 0,0\\n\"\n",                 /* Major/MinorImageVersion */
498              get_asm_short_keyword() );
499     fprintf( outfile, "    \"\\t%s %u,%u\\n\"\n",               /* Major/MinorSubsystemVersion */
500              get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
501     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* Win32VersionValue */
502     fprintf( outfile, "    \"\\t.long %s\\n\"\n",               /* SizeOfImage */
503              asm_name("_end") );
504     fprintf( outfile, "    \"\\t.long %u\\n\"\n", page_size );  /* SizeOfHeaders */
505     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* CheckSum */
506     fprintf( outfile, "    \"\\t%s 0x%04x\\n\"\n",              /* Subsystem */
507              get_asm_short_keyword(), spec->subsystem );
508     fprintf( outfile, "    \"\\t%s 0\\n\"\n",                   /* DllCharacteristics */
509              get_asm_short_keyword() );
510     fprintf( outfile, "    \"\\t%s %u,%u\\n\"\n",               /* SizeOfStackReserve/Commit */
511              get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
512     fprintf( outfile, "    \"\\t%s %u,%u\\n\"\n",               /* SizeOfHeapReserve/Commit */
513              get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
514     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );              /* LoaderFlags */
515     fprintf( outfile, "    \"\\t.long 16\\n\"\n" );             /* NumberOfRvaAndSizes */
516
517     if (spec->base <= spec->limit)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
518         fprintf( outfile, "    \"\\t.long .L__wine_spec_exports, .L__wine_spec_exports_end-.L__wine_spec_exports\\n\"\n" );
519     else
520         fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );
521
522     if (has_imports())   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
523         fprintf( outfile, "    \"\\t.long .L__wine_spec_imports, .L__wine_spec_imports_end-.L__wine_spec_imports\\n\"\n" );
524     else
525         fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );
526
527     if (spec->nb_resources)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
528         fprintf( outfile, "    \"\\t.long .L__wine_spec_resources, .L__wine_spec_resources_end-.L__wine_spec_resources\\n\"\n" );
529     else
530         fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );
531
532     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[3] */
533     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[4] */
534     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[5] */
535     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[6] */
536     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[7] */
537     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[8] */
538     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[9] */
539     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[10] */
540     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[11] */
541     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[12] */
542     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[13] */
543     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[14] */
544     fprintf( outfile, "    \"\\t.long 0,0\\n\"\n" );  /* DataDirectory[15] */
545     fprintf( outfile, ");\n" );
546
547     fprintf( outfile, "asm(\"%s\\n\"\n", get_asm_string_section() );
548     fprintf( outfile, "    \"\\t.globl %s\\n\"\n", asm_name("__wine_spec_file_name") );
549     fprintf( outfile, "    \"%s:\\n\"\n", asm_name("__wine_spec_file_name"));
550     fprintf( outfile, "    \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), spec->file_name );
551     if (target_platform == PLATFORM_APPLE)
552         fprintf( outfile, "    \"\\t.comm %s,4\\n\"\n", asm_name("_end") );
553
554     fprintf( outfile, ");\n" );
555
556     output_stubs( outfile, spec );
557     output_exports( outfile, spec );
558     output_imports( outfile, spec );
559     output_resources( outfile, spec );
560     output_dll_init( outfile, "__wine_spec_init_ctor", NULL );
561
562     fprintf( outfile, "#ifndef __GNUC__\n" );
563     fprintf( outfile, "}\n" );
564     fprintf( outfile, "#endif\n" );
565 }
566
567
568 /*******************************************************************
569  *         BuildDef32File
570  *
571  * Build a Win32 def file from a spec file.
572  */
573 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
574 {
575     const char *name;
576     int i, total;
577
578     if (spec_file_name)
579         fprintf( outfile, "; File generated automatically from %s; do not edit!\n\n",
580                  spec_file_name );
581     else
582         fprintf( outfile, "; File generated automatically; do not edit!\n\n" );
583
584     fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
585
586     fprintf(outfile, "EXPORTS\n");
587
588     /* Output the exports and relay entry points */
589
590     for (i = total = 0; i < spec->nb_entry_points; i++)
591     {
592         const ORDDEF *odp = &spec->entry_points[i];
593         int is_data = 0;
594
595         if (!odp) continue;
596
597         if (odp->name) name = odp->name;
598         else if (odp->export_name) name = odp->export_name;
599         else continue;
600
601         if (!(odp->flags & FLAG_PRIVATE)) total++;
602
603         if (odp->type == TYPE_STUB) continue;
604
605         fprintf(outfile, "  %s", name);
606
607         switch(odp->type)
608         {
609         case TYPE_EXTERN:
610             is_data = 1;
611             /* fall through */
612         case TYPE_VARARGS:
613         case TYPE_CDECL:
614             /* try to reduce output */
615             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
616                 fprintf(outfile, "=%s", odp->link_name);
617             break;
618         case TYPE_STDCALL:
619         {
620             int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
621             if (!kill_at) fprintf(outfile, "@%d", at_param);
622             if  (odp->flags & FLAG_FORWARD)
623             {
624                 fprintf(outfile, "=%s", odp->link_name);
625             }
626             else if (strcmp(name, odp->link_name)) /* try to reduce output */
627             {
628                 fprintf(outfile, "=%s", odp->link_name);
629                 if (!kill_at) fprintf(outfile, "@%d", at_param);
630             }
631             break;
632         }
633         default:
634             assert(0);
635         }
636         fprintf( outfile, " @%d", odp->ordinal );
637         if (!odp->name) fprintf( outfile, " NONAME" );
638         if (is_data) fprintf( outfile, " DATA" );
639         if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
640         fprintf( outfile, "\n" );
641     }
642     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
643 }
644
645
646 /*******************************************************************
647  *         BuildDebugFile
648  *
649  * Build the debugging channels source file.
650  */
651 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
652 {
653     int nr_debug;
654     char *prefix, *p, *constructor, *destructor;
655
656     while (*argv)
657     {
658         if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
659     }
660
661     output_standard_file_header( outfile );
662     nr_debug = output_debug( outfile );
663     if (!nr_debug)
664     {
665         fprintf( outfile, "/* no debug channels found for this module */\n" );
666         return;
667     }
668
669     if (output_file_name)
670     {
671         if ((p = strrchr( output_file_name, '/' ))) p++;
672         prefix = xstrdup( p ? p : output_file_name );
673         if ((p = strchr( prefix, '.' ))) *p = 0;
674         strcpy( p, make_c_identifier(p) );
675     }
676     else prefix = xstrdup( "_" );
677
678     /* Output the DLL constructor */
679
680     constructor = xmalloc( strlen(prefix) + 17 );
681     destructor = xmalloc( strlen(prefix) + 17 );
682     sprintf( constructor, "__wine_dbg_%s_init", prefix );
683     sprintf( destructor, "__wine_dbg_%s_fini", prefix );
684     fprintf( outfile,
685              "#ifdef __GNUC__\n"
686              "void %s(void) __attribute__((constructor));\n"
687              "void %s(void) __attribute__((destructor));\n"
688              "#else\n"
689              "static void __asm__dummy_dll_init(void) {\n",
690              constructor, destructor );
691     output_dll_init( outfile, constructor, destructor );
692     fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
693
694     fprintf( outfile,
695              "void %s(void)\n"
696              "{\n"
697              "    extern void *__wine_dbg_register( char * const *, int );\n"
698              "    if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
699              "}\n\n", constructor, nr_debug );
700     fprintf( outfile,
701              "void %s(void)\n"
702              "{\n"
703              "    extern void __wine_dbg_unregister( void* );\n"
704              "    __wine_dbg_unregister( debug_registration );\n"
705              "}\n", destructor );
706
707     free( constructor );
708     free( destructor );
709     free( prefix );
710 }