Added a common function to declare global symbols, and make them
[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, "\n/* export table */\n\n" );
111     fprintf( outfile, "\t.data\n" );
112     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
113     fprintf( outfile, ".L__wine_spec_exports:\n" );
114
115     /* export directory header */
116
117     fprintf( outfile, "\t.long 0\n" );                       /* Characteristics */
118     fprintf( outfile, "\t.long 0\n" );                       /* TimeDateStamp */
119     fprintf( outfile, "\t.long 0\n" );                       /* MajorVersion/MinorVersion */
120     fprintf( outfile, "\t.long .L__wine_spec_exp_names\n" ); /* Name */
121     fprintf( outfile, "\t.long %d\n", spec->base );          /* Base */
122     fprintf( outfile, "\t.long %d\n", nr_exports );          /* NumberOfFunctions */
123     fprintf( outfile, "\t.long %d\n", spec->nb_names );      /* NumberOfNames */
124     fprintf( outfile, "\t.long .L__wine_spec_exports_funcs\n" ); /* AddressOfFunctions */
125     if (spec->nb_names)
126     {
127         fprintf( outfile, "\t.long .L__wine_spec_exp_name_ptrs\n" ); /* AddressOfNames */
128         fprintf( outfile, "\t.long .L__wine_spec_exp_ordinals\n" );  /* AddressOfNameOrdinals */
129     }
130     else
131     {
132         fprintf( outfile, "\t.long 0\n" );  /* AddressOfNames */
133         fprintf( outfile, "\t.long 0\n" );  /* AddressOfNameOrdinals */
134     }
135
136     /* output the function pointers */
137
138     fprintf( outfile, "\n.L__wine_spec_exports_funcs:\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" );
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", asm_name(odp->link_name) );
152             }
153             else
154             {
155                 fprintf( outfile, "\t.long .L__wine_spec_forwards+%d\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",
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, "\n.L__wine_spec_exp_name_ptrs:\n" );
175         for (i = 0; i < spec->nb_names; i++)
176         {
177             fprintf( outfile, "\t.long .L__wine_spec_exp_names+%d\n", namepos );
178             namepos += strlen(spec->names[i]->name) + 1;
179         }
180
181         /* output the function ordinals */
182
183         fprintf( outfile, "\n.L__wine_spec_exp_ordinals:\n" );
184         for (i = 0; i < spec->nb_names; i++)
185         {
186             fprintf( outfile, "\t%s %d\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", get_asm_short_keyword() );
192         }
193     }
194
195     /* output the export name strings */
196
197     fprintf( outfile, "\n.L__wine_spec_exp_names:\n" );
198     fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
199     for (i = 0; i < spec->nb_names; i++)
200         fprintf( outfile, "\t%s \"%s\"\n",
201                  get_asm_string_keyword(), spec->names[i]->name );
202
203     /* output forward strings */
204
205     if (fwd_size)
206     {
207         fprintf( outfile, "\n.L__wine_spec_forwards:\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", get_asm_string_keyword(), odp->link_name );
213         }
214     }
215     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
216     fprintf( outfile, ".L__wine_spec_exports_end:\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", asm_name(odp->link_name) );
248                 fprintf( outfile, "\tret $%d\n", args );
249                 fprintf( outfile, "\t.long %s,0x%08x\n", asm_name(odp->link_name), mask );
250                 break;
251             case TYPE_CDECL:
252                 fprintf( outfile, "\tjmp %s\n", asm_name(odp->link_name) );
253                 fprintf( outfile, "\tret\n" );
254                 fprintf( outfile, "\t%s %d\n", get_asm_short_keyword(), args );
255                 fprintf( outfile, "\t.long %s,0x%08x\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" );
264         }
265     }
266     else fprintf( outfile, "\t.long 0\n" );
267 }
268
269
270 /*******************************************************************
271  *         output_stubs
272  *
273  * Output the functions for stub entry points
274 */
275 static void output_stubs( FILE *outfile, DLLSPEC *spec )
276 {
277     const char *name, *exp_name;
278     int i, pos;
279
280     if (!has_stubs( spec )) return;
281
282     fprintf( outfile, "\n/* stub functions */\n\n" );
283     fprintf( outfile, "\t.text\n" );
284
285     for (i = pos = 0; i < spec->nb_entry_points; i++)
286     {
287         ORDDEF *odp = &spec->entry_points[i];
288         if (odp->type != TYPE_STUB) continue;
289
290         name = make_internal_name( odp, spec, "stub" );
291         exp_name = odp->name ? odp->name : odp->export_name;
292         fprintf( outfile, "\t.align %d\n", get_alignment(4) );
293         fprintf( outfile, "\t%s\n", func_declaration(name) );
294         fprintf( outfile, "%s:\n", asm_name(name) );
295         fprintf( outfile, "\tcall .L__wine_stub_getpc_%d\n", i );
296         fprintf( outfile, ".L__wine_stub_getpc_%d:\n", i );
297         fprintf( outfile, "\tpopl %%eax\n" );
298         if (exp_name)
299         {
300             fprintf( outfile, "\tleal .L__wine_stub_strings+%d-.L__wine_stub_getpc_%d(%%eax),%%ecx\n",
301                      pos, i );
302             fprintf( outfile, "\tpushl %%ecx\n" );
303             pos += strlen(exp_name) + 1;
304         }
305         else
306             fprintf( outfile, "\tpushl $%d\n", odp->ordinal );
307         fprintf( outfile, "\tleal %s-.L__wine_stub_getpc_%d(%%eax),%%ecx\n",
308                  asm_name("__wine_spec_file_name"), i );
309         fprintf( outfile, "\tpushl %%ecx\n" );
310         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
311         fprintf( outfile, "\t%s\n", func_size(name) );
312     }
313
314     if (pos)
315     {
316         fprintf( outfile, "\t%s\n", get_asm_string_section() );
317         fprintf( outfile, ".L__wine_stub_strings:\n" );
318         for (i = 0; i < spec->nb_entry_points; i++)
319         {
320             ORDDEF *odp = &spec->entry_points[i];
321             if (odp->type != TYPE_STUB) continue;
322             exp_name = odp->name ? odp->name : odp->export_name;
323             if (exp_name)
324                 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
325         }
326     }
327 }
328
329
330 /*******************************************************************
331  *         output_dll_init
332  *
333  * Output code for calling a dll constructor and destructor.
334  */
335 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
336 {
337     if (target_platform == PLATFORM_APPLE)
338     {
339         /* Mach-O doesn't have an init section */
340         if (constructor)
341         {
342             fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
343             fprintf( outfile, "    \"\\t.align 2\\n\"\n" );
344             fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(constructor) );
345             fprintf( outfile, "    \"\\t.text\\n\");\n" );
346         }
347         if (destructor)
348         {
349             fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
350             fprintf( outfile, "    \"\\t.align 2\\n\"\n" );
351             fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(destructor) );
352             fprintf( outfile, "    \"\\t.text\\n\");\n" );
353         }
354     }
355     else switch(target_cpu)
356     {
357     case CPU_x86:
358     case CPU_x86_64:
359         if (constructor)
360         {
361             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
362             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(constructor) );
363             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
364         }
365         if (destructor)
366         {
367             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
368             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(destructor) );
369             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
370         }
371         break;
372     case CPU_SPARC:
373         if (constructor)
374         {
375             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
376             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(constructor) );
377             fprintf( outfile, "    \"\\tnop\\n\"\n" );
378             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
379         }
380         if (destructor)
381         {
382             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
383             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(destructor) );
384             fprintf( outfile, "    \"\\tnop\\n\"\n" );
385             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
386         }
387         break;
388     case CPU_ALPHA:
389         if (constructor)
390         {
391             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
392             fprintf( outfile, "    \"\\tjsr $26,%s\\n\"\n", asm_name(constructor) );
393             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
394         }
395         if (destructor)
396         {
397             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
398             fprintf( outfile, "    \"\\tjsr $26,%s\\n\"\n", asm_name(destructor) );
399             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
400         }
401         break;
402     case CPU_POWERPC:
403         if (constructor)
404         {
405             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
406             fprintf( outfile, "    \"\\tbl %s\\n\"\n", asm_name(constructor) );
407             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
408         }
409         if (destructor)
410         {
411             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
412             fprintf( outfile, "    \"\\tbl %s\\n\"\n", asm_name(destructor) );
413             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
414         }
415         break;
416     }
417 }
418
419
420 /*******************************************************************
421  *         output_asm_constructor
422  *
423  * Output code for calling a dll constructor.
424  */
425 static void output_asm_constructor( FILE *outfile, const char *constructor )
426 {
427     if (target_platform == PLATFORM_APPLE)
428     {
429         /* Mach-O doesn't have an init section */
430         fprintf( outfile, "\n\t.mod_init_func\n" );
431         fprintf( outfile, "\t.align %d\n", get_alignment(4) );
432         fprintf( outfile, "\t.long %s\n", asm_name(constructor) );
433     }
434     else
435     {
436         fprintf( outfile, "\n\t.section \".init\",\"ax\"\n" );
437         switch(target_cpu)
438         {
439         case CPU_x86:
440         case CPU_x86_64:
441             fprintf( outfile, "\tcall %s\n", asm_name(constructor) );
442             break;
443         case CPU_SPARC:
444             fprintf( outfile, "\tcall %s\n", asm_name(constructor) );
445             fprintf( outfile, "\tnop\n" );
446             break;
447         case CPU_ALPHA:
448             fprintf( outfile, "\tjsr $26,%s\n", asm_name(constructor) );
449             break;
450         case CPU_POWERPC:
451             fprintf( outfile, "\tbl %s\n", asm_name(constructor) );
452             break;
453         }
454     }
455 }
456
457
458 /*******************************************************************
459  *         BuildSpec32File
460  *
461  * Build a Win32 C file from a spec file.
462  */
463 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
464 {
465     int machine = 0;
466     unsigned int page_size = get_page_size();
467
468     resolve_imports( spec );
469     output_standard_file_header( outfile );
470
471     /* Reserve some space for the PE header */
472
473     fprintf( outfile, "\t.text\n" );
474     fprintf( outfile, "\t.align %d\n", get_alignment(page_size) );
475     fprintf( outfile, "__wine_spec_pe_header:\n" );
476     if (target_platform == PLATFORM_APPLE)
477         fprintf( outfile, "\t.space 65536\n" );
478     else
479         fprintf( outfile, "\t.skip 65536\n" );
480
481     /* Output the NT header */
482
483     fprintf( outfile, "\n\t.data\n" );
484     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
485     fprintf( outfile, "%s\n", asm_globl("__wine_spec_nt_header") );
486
487     fprintf( outfile, "\t.long 0x%04x\n", IMAGE_NT_SIGNATURE );    /* Signature */
488     switch(target_cpu)
489     {
490     case CPU_x86:     machine = IMAGE_FILE_MACHINE_I386; break;
491     case CPU_x86_64:  machine = IMAGE_FILE_MACHINE_AMD64; break;
492     case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
493     case CPU_ALPHA:   machine = IMAGE_FILE_MACHINE_ALPHA; break;
494     case CPU_SPARC:   machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
495     }
496     fprintf( outfile, "\t%s 0x%04x\n",              /* Machine */
497              get_asm_short_keyword(), machine );
498     fprintf( outfile, "\t%s 0\n",                   /* NumberOfSections */
499              get_asm_short_keyword() );
500     fprintf( outfile, "\t.long 0\n" );              /* TimeDateStamp */
501     fprintf( outfile, "\t.long 0\n" );              /* PointerToSymbolTable */
502     fprintf( outfile, "\t.long 0\n" );              /* NumberOfSymbols */
503     fprintf( outfile, "\t%s %d\n",                  /* SizeOfOptionalHeader */
504              get_asm_short_keyword(),
505              get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
506     fprintf( outfile, "\t%s 0x%04x\n",              /* Characteristics */
507              get_asm_short_keyword(), spec->characteristics );
508     fprintf( outfile, "\t%s 0x%04x\n",              /* Magic */
509              get_asm_short_keyword(),
510              get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
511     fprintf( outfile, "\t.byte 0\n" );              /* MajorLinkerVersion */
512     fprintf( outfile, "\t.byte 0\n" );              /* MinorLinkerVersion */
513     fprintf( outfile, "\t.long 0\n" );              /* SizeOfCode */
514     fprintf( outfile, "\t.long 0\n" );              /* SizeOfInitializedData */
515     fprintf( outfile, "\t.long 0\n" );              /* SizeOfUninitializedData */
516     fprintf( outfile, "\t.long %s\n",               /* AddressOfEntryPoint */
517              asm_name(spec->init_func) );
518     fprintf( outfile, "\t.long 0\n" );              /* BaseOfCode */
519     if (get_ptr_size() == 4)
520         fprintf( outfile, "\t.long %s\n", asm_name("__wine_spec_nt_header") ); /* BaseOfData */
521     fprintf( outfile, "\t%s __wine_spec_pe_header\n",         /* ImageBase */
522              get_asm_ptr_keyword() );
523     fprintf( outfile, "\t.long %u\n", page_size );  /* SectionAlignment */
524     fprintf( outfile, "\t.long %u\n", page_size );  /* FileAlignment */
525     fprintf( outfile, "\t%s 1,0\n",                 /* Major/MinorOperatingSystemVersion */
526              get_asm_short_keyword() );
527     fprintf( outfile, "\t%s 0,0\n",                 /* Major/MinorImageVersion */
528              get_asm_short_keyword() );
529     fprintf( outfile, "\t%s %u,%u\n",               /* Major/MinorSubsystemVersion */
530              get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
531     fprintf( outfile, "\t.long 0\n" );              /* Win32VersionValue */
532     fprintf( outfile, "\t.long %s\n",               /* SizeOfImage */
533              asm_name("_end") );
534     fprintf( outfile, "\t.long %u\n", page_size );  /* SizeOfHeaders */
535     fprintf( outfile, "\t.long 0\n" );              /* CheckSum */
536     fprintf( outfile, "\t%s 0x%04x\n",              /* Subsystem */
537              get_asm_short_keyword(), spec->subsystem );
538     fprintf( outfile, "\t%s 0\n",                   /* DllCharacteristics */
539              get_asm_short_keyword() );
540     fprintf( outfile, "\t%s %u,%u\n",               /* SizeOfStackReserve/Commit */
541              get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
542     fprintf( outfile, "\t%s %u,%u\n",               /* SizeOfHeapReserve/Commit */
543              get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
544     fprintf( outfile, "\t.long 0\n" );              /* LoaderFlags */
545     fprintf( outfile, "\t.long 16\n" );             /* NumberOfRvaAndSizes */
546
547     if (spec->base <= spec->limit)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
548         fprintf( outfile, "\t.long .L__wine_spec_exports, .L__wine_spec_exports_end-.L__wine_spec_exports\n" );
549     else
550         fprintf( outfile, "\t.long 0,0\n" );
551
552     if (has_imports())   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
553         fprintf( outfile, "\t.long .L__wine_spec_imports, .L__wine_spec_imports_end-.L__wine_spec_imports\n" );
554     else
555         fprintf( outfile, "\t.long 0,0\n" );
556
557     if (spec->nb_resources)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
558         fprintf( outfile, "\t.long .L__wine_spec_resources, .L__wine_spec_resources_end-.L__wine_spec_resources\n" );
559     else
560         fprintf( outfile, "\t.long 0,0\n" );
561
562     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[3] */
563     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[4] */
564     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[5] */
565     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[6] */
566     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[7] */
567     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[8] */
568     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[9] */
569     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[10] */
570     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[11] */
571     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[12] */
572     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[13] */
573     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[14] */
574     fprintf( outfile, "\t.long 0,0\n" );  /* DataDirectory[15] */
575
576     fprintf( outfile, "\n\t%s\n", get_asm_string_section() );
577     fprintf( outfile, "%s\n", asm_globl("__wine_spec_file_name") );
578     fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
579     if (target_platform == PLATFORM_APPLE)
580         fprintf( outfile, "\t.comm %s,4\n", asm_name("_end") );
581
582     output_stubs( outfile, spec );
583     output_exports( outfile, spec );
584     output_imports( outfile, spec );
585     output_resources( outfile, spec );
586     output_asm_constructor( outfile, "__wine_spec_init_ctor" );
587 }
588
589
590 /*******************************************************************
591  *         BuildDef32File
592  *
593  * Build a Win32 def file from a spec file.
594  */
595 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
596 {
597     const char *name;
598     int i, total;
599
600     if (spec_file_name)
601         fprintf( outfile, "; File generated automatically from %s; do not edit!\n\n",
602                  spec_file_name );
603     else
604         fprintf( outfile, "; File generated automatically; do not edit!\n\n" );
605
606     fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
607
608     fprintf(outfile, "EXPORTS\n");
609
610     /* Output the exports and relay entry points */
611
612     for (i = total = 0; i < spec->nb_entry_points; i++)
613     {
614         const ORDDEF *odp = &spec->entry_points[i];
615         int is_data = 0;
616
617         if (!odp) continue;
618
619         if (odp->name) name = odp->name;
620         else if (odp->export_name) name = odp->export_name;
621         else continue;
622
623         if (!(odp->flags & FLAG_PRIVATE)) total++;
624
625         if (odp->type == TYPE_STUB) continue;
626
627         fprintf(outfile, "  %s", name);
628
629         switch(odp->type)
630         {
631         case TYPE_EXTERN:
632             is_data = 1;
633             /* fall through */
634         case TYPE_VARARGS:
635         case TYPE_CDECL:
636             /* try to reduce output */
637             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
638                 fprintf(outfile, "=%s", odp->link_name);
639             break;
640         case TYPE_STDCALL:
641         {
642             int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
643             if (!kill_at) fprintf(outfile, "@%d", at_param);
644             if  (odp->flags & FLAG_FORWARD)
645             {
646                 fprintf(outfile, "=%s", odp->link_name);
647             }
648             else if (strcmp(name, odp->link_name)) /* try to reduce output */
649             {
650                 fprintf(outfile, "=%s", odp->link_name);
651                 if (!kill_at) fprintf(outfile, "@%d", at_param);
652             }
653             break;
654         }
655         default:
656             assert(0);
657         }
658         fprintf( outfile, " @%d", odp->ordinal );
659         if (!odp->name) fprintf( outfile, " NONAME" );
660         if (is_data) fprintf( outfile, " DATA" );
661         if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
662         fprintf( outfile, "\n" );
663     }
664     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
665 }
666
667
668 /*******************************************************************
669  *         BuildDebugFile
670  *
671  * Build the debugging channels source file.
672  */
673 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
674 {
675     int nr_debug;
676     char *prefix, *p, *constructor, *destructor;
677
678     while (*argv)
679     {
680         if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
681     }
682
683     output_standard_file_header( outfile );
684     nr_debug = output_debug( outfile );
685     if (!nr_debug)
686     {
687         fprintf( outfile, "/* no debug channels found for this module */\n" );
688         return;
689     }
690
691     if (output_file_name)
692     {
693         if ((p = strrchr( output_file_name, '/' ))) p++;
694         prefix = xstrdup( p ? p : output_file_name );
695         if ((p = strchr( prefix, '.' ))) *p = 0;
696         strcpy( p, make_c_identifier(p) );
697     }
698     else prefix = xstrdup( "_" );
699
700     /* Output the DLL constructor */
701
702     constructor = xmalloc( strlen(prefix) + 17 );
703     destructor = xmalloc( strlen(prefix) + 17 );
704     sprintf( constructor, "__wine_dbg_%s_init", prefix );
705     sprintf( destructor, "__wine_dbg_%s_fini", prefix );
706     fprintf( outfile,
707              "#ifdef __GNUC__\n"
708              "void %s(void) __attribute__((constructor));\n"
709              "void %s(void) __attribute__((destructor));\n"
710              "#else\n"
711              "static void __asm__dummy_dll_init(void) {\n",
712              constructor, destructor );
713     output_dll_init( outfile, constructor, destructor );
714     fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
715
716     fprintf( outfile,
717              "void %s(void)\n"
718              "{\n"
719              "    extern void *__wine_dbg_register( char * const *, int );\n"
720              "    if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
721              "}\n\n", constructor, nr_debug );
722     fprintf( outfile,
723              "void %s(void)\n"
724              "{\n"
725              "    extern void __wine_dbg_unregister( void* );\n"
726              "    __wine_dbg_unregister( debug_registration );\n"
727              "}\n", destructor );
728
729     free( constructor );
730     free( destructor );
731     free( prefix );
732 }