Fix blank.htm resources.
[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  *         declare_weak_function
70  *
71  * Output a prototype for a weak function.
72  */
73 static void declare_weak_function( FILE *outfile, const char *ret_type, const char *name, const char *params)
74 {
75     fprintf( outfile, "#ifdef __GNUC__\n" );
76     if (target_platform == PLATFORM_APPLE)
77     {
78         fprintf( outfile, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type, name, params );
79         fprintf( outfile, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type, name, params, name );
80         fprintf( outfile, "#define %s __wine_spec_weak_%s\n", name, name );
81         fprintf( outfile, "asm(\".weak_reference %s\");\n", asm_name(name) );
82     }
83     else fprintf( outfile, "extern %s %s(%s) __attribute__((weak));\n", ret_type, name, params );
84
85     fprintf( outfile, "#else\n" );
86     fprintf( outfile, "extern %s %s(%s);\n", ret_type, name, params );
87     fprintf( outfile, "static void __asm__dummy_%s(void)", name );
88     fprintf( outfile, " { asm(\".weak %s\"); }\n", asm_name(name) );
89     fprintf( outfile, "#endif\n\n" );
90 }
91
92
93 /*******************************************************************
94  *         output_debug
95  *
96  * Output the debug channels.
97  */
98 static int output_debug( FILE *outfile )
99 {
100     int i;
101
102     if (!nb_debug_channels) return 0;
103     qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
104
105     for (i = 0; i < nb_debug_channels; i++)
106         fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
107                  debug_channels[i], debug_channels[i] );
108
109     fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
110     for (i = 0; i < nb_debug_channels; i++)
111     {
112         fprintf( outfile, "    __wine_dbch_%s", debug_channels[i] );
113         if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
114     }
115     fprintf( outfile, "\n};\n\n" );
116     fprintf( outfile, "static void *debug_registration;\n\n" );
117
118     return nb_debug_channels;
119 }
120
121
122 /*******************************************************************
123  *         get_exports_size
124  *
125  * Compute the size of the export table.
126  */
127 static int get_exports_size( DLLSPEC *spec )
128 {
129     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
130     int i, fwd_size = 0, total_size;
131
132     if (!nr_exports) return 0;
133
134     /* export directory header */
135     total_size = 10 * sizeof(int);
136
137     /* function pointers */
138     total_size += nr_exports * sizeof(int);
139
140     /* function name pointers */
141     total_size += spec->nb_names * sizeof(int);
142
143     /* function ordinals */
144     total_size += spec->nb_names * sizeof(short);
145     if (spec->nb_names % 2) total_size += sizeof(short);
146
147     /* forward strings */
148     for (i = spec->base; i <= spec->limit; i++)
149     {
150         ORDDEF *odp = spec->ordinals[i];
151         if (odp && odp->flags & FLAG_FORWARD) fwd_size += strlen(odp->link_name) + 1;
152     }
153     total_size += (fwd_size + 3) & ~3;
154
155     return total_size;
156 }
157
158
159 /*******************************************************************
160  *         output_export_names
161  *
162  * Output all the exported names for a Win32 module.
163  */
164 static void output_export_names( FILE *outfile, DLLSPEC *spec )
165 {
166     int i, nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
167
168     if (!nr_exports) return;
169
170     fprintf( outfile, "\nconst char __wine_spec_exp_names[] =" );
171     fprintf( outfile, "\n    \"%s\\0\"", spec->file_name );
172     for (i = 0; i < spec->nb_names; i++)
173         fprintf( outfile, "\n    \"%s\\0\"", spec->names[i]->name );
174     fprintf( outfile, ";\n" );
175 }
176
177
178 /*******************************************************************
179  *         output_exports
180  *
181  * Output the export table for a Win32 module.
182  */
183 static void output_exports( FILE *outfile, DLLSPEC *spec )
184 {
185     int i, fwd_size = 0;
186     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
187
188     if (!nr_exports) return;
189
190     fprintf( outfile, "/* export table */\n" );
191     fprintf( outfile, "asm(\".data\\n\"\n" );
192     fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
193     fprintf( outfile, "    \"%s:\\n\"\n", asm_name("__wine_spec_exports") );
194
195     /* export directory header */
196
197     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* Characteristics */
198     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* TimeDateStamp */
199     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* MajorVersion/MinorVersion */
200     fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name("__wine_spec_exp_names") ); /* Name */
201     fprintf( outfile, "    \"\\t.long %d\\n\"\n", spec->base );        /* Base */
202     fprintf( outfile, "    \"\\t.long %d\\n\"\n", nr_exports );        /* NumberOfFunctions */
203     fprintf( outfile, "    \"\\t.long %d\\n\"\n", spec->nb_names );    /* NumberOfNames */
204     fprintf( outfile, "    \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
205     if (spec->nb_names)
206     {
207         fprintf( outfile, "    \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" );     /* AddressOfNames */
208         fprintf( outfile, "    \"\\t.long __wine_spec_exp_ordinals\\n\"\n" );  /* AddressOfNameOrdinals */
209     }
210     else
211     {
212         fprintf( outfile, "    \"\\t.long 0\\n\"\n" );  /* AddressOfNames */
213         fprintf( outfile, "    \"\\t.long 0\\n\"\n" );  /* AddressOfNameOrdinals */
214     }
215
216     /* output the function pointers */
217
218     fprintf( outfile, "    \"__wine_spec_exports_funcs:\\n\"\n" );
219     for (i = spec->base; i <= spec->limit; i++)
220     {
221         ORDDEF *odp = spec->ordinals[i];
222         if (!odp) fprintf( outfile, "    \"\\t.long 0\\n\"\n" );
223         else switch(odp->type)
224         {
225         case TYPE_EXTERN:
226         case TYPE_STDCALL:
227         case TYPE_VARARGS:
228         case TYPE_CDECL:
229             if (!(odp->flags & FLAG_FORWARD))
230             {
231                 fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(odp->link_name) );
232             }
233             else
234             {
235                 fprintf( outfile, "    \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
236                 fwd_size += strlen(odp->link_name) + 1;
237             }
238             break;
239         case TYPE_STUB:
240             fprintf( outfile, "    \"\\t.long %s\\n\"\n",
241                      asm_name( make_internal_name( odp, spec, "stub" )) );
242             break;
243         default:
244             assert(0);
245         }
246     }
247
248     if (spec->nb_names)
249     {
250         /* output the function name pointers */
251
252         int namepos = strlen(spec->file_name) + 1;
253
254         fprintf( outfile, "    \"__wine_spec_exp_name_ptrs:\\n\"\n" );
255         for (i = 0; i < spec->nb_names; i++)
256         {
257             fprintf( outfile, "    \"\\t.long %s+%d\\n\"\n", asm_name("__wine_spec_exp_names"), namepos );
258             namepos += strlen(spec->names[i]->name) + 1;
259         }
260     }
261
262     if (spec->nb_names)
263     {
264         /* output the function ordinals */
265
266         fprintf( outfile, "    \"__wine_spec_exp_ordinals:\\n\"\n" );
267         for (i = 0; i < spec->nb_names; i++)
268         {
269             fprintf( outfile, "    \"\\t%s %d\\n\"\n",
270                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
271         }
272         if (spec->nb_names % 2)
273         {
274             fprintf( outfile, "    \"\\t%s 0\\n\"\n", get_asm_short_keyword() );
275         }
276     }
277
278     /* output forward strings */
279
280     if (fwd_size)
281     {
282         fprintf( outfile, "    \"__wine_spec_forwards:\\n\"\n" );
283         for (i = spec->base; i <= spec->limit; i++)
284         {
285             ORDDEF *odp = spec->ordinals[i];
286             if (odp && (odp->flags & FLAG_FORWARD))
287                 fprintf( outfile, "    \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), odp->link_name );
288         }
289         fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
290     }
291
292     /* output relays */
293
294     if (debugging)
295     {
296         for (i = spec->base; i <= spec->limit; i++)
297         {
298             ORDDEF *odp = spec->ordinals[i];
299             unsigned int j, args, mask = 0;
300
301             /* skip nonexistent entry points */
302             if (!odp) goto ignore;
303             /* skip non-functions */
304             if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
305             /* skip norelay and forward entry points */
306             if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
307
308             for (j = 0; odp->u.func.arg_types[j]; j++)
309             {
310                 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
311                 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
312             }
313             if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
314
315             args = strlen(odp->u.func.arg_types) * sizeof(int);
316
317             switch(odp->type)
318             {
319             case TYPE_STDCALL:
320                 fprintf( outfile, "    \"\\tjmp %s\\n\"\n", asm_name(odp->link_name) );
321                 fprintf( outfile, "    \"\\tret $%d\\n\"\n", args );
322                 fprintf( outfile, "    \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp->link_name), mask );
323                 break;
324             case TYPE_CDECL:
325                 fprintf( outfile, "    \"\\tjmp %s\\n\"\n", asm_name(odp->link_name) );
326                 fprintf( outfile, "    \"\\tret\\n\"\n" );
327                 fprintf( outfile, "    \"\\t%s %d\\n\"\n", get_asm_short_keyword(), args );
328                 fprintf( outfile, "    \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp->link_name), mask );
329                 break;
330             default:
331                 assert(0);
332             }
333             continue;
334
335         ignore:
336             fprintf( outfile, "    \"\\t.long 0,0,0,0\\n\"\n" );
337         }
338     }
339     fprintf( outfile, ");\n" );
340 }
341
342
343 /*******************************************************************
344  *         output_stub_funcs
345  *
346  * Output the functions for stub entry points
347 */
348 static void output_stub_funcs( FILE *outfile, DLLSPEC *spec )
349 {
350     int i;
351
352     for (i = 0; i < spec->nb_entry_points; i++)
353     {
354         ORDDEF *odp = &spec->entry_points[i];
355         if (odp->type != TYPE_STUB) continue;
356         fprintf( outfile, "#ifdef __GNUC__\n" );
357         fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
358         fprintf( outfile, "#endif\n\n" );
359         fprintf( outfile, "struct exc_record {\n" );
360         fprintf( outfile, "  unsigned int code, flags;\n" );
361         fprintf( outfile, "  void *rec, *addr;\n" );
362         fprintf( outfile, "  unsigned int params;\n" );
363         fprintf( outfile, "  const void *info[15];\n" );
364         fprintf( outfile, "};\n\n" );
365         fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
366         fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
367         fprintf( outfile, "  struct exc_record rec;\n" );
368         fprintf( outfile, "  rec.code    = 0x%08x;\n", EXCEPTION_WINE_STUB );
369         fprintf( outfile, "  rec.flags   = %d;\n", EH_NONCONTINUABLE );
370         fprintf( outfile, "  rec.rec     = 0;\n" );
371         fprintf( outfile, "  rec.params  = 2;\n" );
372         fprintf( outfile, "  rec.info[0] = \"%s\";\n", spec->file_name );
373         fprintf( outfile, "  rec.info[1] = func;\n" );
374         fprintf( outfile, "#ifdef __GNUC__\n" );
375         fprintf( outfile, "  rec.addr = __builtin_return_address(1);\n" );
376         fprintf( outfile, "#else\n" );
377         fprintf( outfile, "  rec.addr = 0;\n" );
378         fprintf( outfile, "#endif\n" );
379         fprintf( outfile, "  for (;;) RtlRaiseException( &rec );\n}\n\n" );
380         break;
381     }
382
383     for (i = 0; i < spec->nb_entry_points; i++)
384     {
385         const ORDDEF *odp = &spec->entry_points[i];
386         if (odp->type != TYPE_STUB) continue;
387         fprintf( outfile, "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
388         if (odp->name)
389             fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
390         else if (odp->export_name)
391             fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
392         else
393             fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
394     }
395 }
396
397
398 /*******************************************************************
399  *         output_dll_init
400  *
401  * Output code for calling a dll constructor and destructor.
402  */
403 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
404 {
405     if (target_platform == PLATFORM_APPLE)
406     {
407         /* Mach-O doesn't have an init section */
408         if (constructor)
409         {
410             fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
411             fprintf( outfile, "    \"\\t.align 2\\n\"\n" );
412             fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(constructor) );
413             fprintf( outfile, "    \"\\t.text\\n\");\n" );
414         }
415         if (destructor)
416         {
417             fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
418             fprintf( outfile, "    \"\\t.align 2\\n\"\n" );
419             fprintf( outfile, "    \"\\t.long %s\\n\"\n", asm_name(destructor) );
420             fprintf( outfile, "    \"\\t.text\\n\");\n" );
421         }
422     }
423     else switch(target_cpu)
424     {
425     case CPU_x86:
426         if (constructor)
427         {
428             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
429             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(constructor) );
430             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
431         }
432         if (destructor)
433         {
434             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
435             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(destructor) );
436             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
437         }
438         break;
439     case CPU_SPARC:
440         if (constructor)
441         {
442             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
443             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(constructor) );
444             fprintf( outfile, "    \"\\tnop\\n\"\n" );
445             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
446         }
447         if (destructor)
448         {
449             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
450             fprintf( outfile, "    \"\\tcall %s\\n\"\n", asm_name(destructor) );
451             fprintf( outfile, "    \"\\tnop\\n\"\n" );
452             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
453         }
454         break;
455     case CPU_ALPHA:
456         if (constructor)
457         {
458             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
459             fprintf( outfile, "    \"\\tjsr $26,%s\\n\"\n", asm_name(constructor) );
460             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
461         }
462         if (destructor)
463         {
464             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
465             fprintf( outfile, "    \"\\tjsr $26,%s\\n\"\n", asm_name(destructor) );
466             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
467         }
468         break;
469     case CPU_POWERPC:
470         if (constructor)
471         {
472             fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
473             fprintf( outfile, "    \"\\tbl %s\\n\"\n", asm_name(constructor) );
474             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
475         }
476         if (destructor)
477         {
478             fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
479             fprintf( outfile, "    \"\\tbl %s\\n\"\n", asm_name(destructor) );
480             fprintf( outfile, "    \"\\t.section\\t\\\".text\\\"\\n\");\n" );
481         }
482         break;
483     }
484 }
485
486
487 /*******************************************************************
488  *         BuildSpec32File
489  *
490  * Build a Win32 C file from a spec file.
491  */
492 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
493 {
494     int exports_size = 0;
495     int nr_exports, nr_imports, nr_delayed;
496     unsigned int page_size = get_page_size();
497     const char *init_func = spec->init_func;
498
499     nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
500     resolve_imports( spec );
501     exports_size = get_exports_size( spec );
502     output_standard_file_header( outfile );
503
504     /* Reserve some space for the PE header */
505
506     fprintf( outfile, "extern char __wine_spec_pe_header[];\n" );
507     fprintf( outfile, "#ifndef __GNUC__\n" );
508     fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
509     fprintf( outfile, "#endif\n" );
510     fprintf( outfile, "asm(\".text\\n\\t\"\n" );
511     fprintf( outfile, "    \".align %d\\n\"\n", get_alignment(page_size) );
512     fprintf( outfile, "    \"%s:\\t\"\n", asm_name("__wine_spec_pe_header") );
513     if (target_platform == PLATFORM_APPLE)
514         fprintf( outfile, "    \".space 65536\\n\\t\"\n" );
515     else
516         fprintf( outfile, "    \".skip 65536\\n\\t\"\n" );
517     fprintf( outfile, "    \".data\\n\\t\"\n" );
518     fprintf( outfile, "    \".align %d\\n\"\n", get_alignment(4) );
519     fprintf( outfile, "    \"%s:\\t.long 1\");\n", asm_name("__wine_spec_data_start") );
520     fprintf( outfile, "#ifndef __GNUC__\n" );
521     fprintf( outfile, "}\n" );
522     fprintf( outfile, "#endif\n" );
523
524     if (target_platform == PLATFORM_APPLE)
525         fprintf( outfile, "static char _end[4];\n" );
526     else
527         fprintf( outfile, "extern char _end[];\n" );
528
529     fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
530
531     if (target_cpu == CPU_x86)
532         fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
533     else
534         fprintf( outfile, "#define __stdcall\n\n" );
535
536     output_stub_funcs( outfile, spec );
537     output_export_names( outfile, spec );
538
539     /* Output the DLL imports */
540
541     nr_imports = output_imports( outfile, spec, &nr_delayed );
542
543     /* Output the resources */
544
545     output_resources( outfile, spec );
546
547     /* Output the entry point function */
548
549     fprintf( outfile, "static int __wine_spec_init_state;\n" );
550     fprintf( outfile, "extern int __wine_main_argc;\n" );
551     fprintf( outfile, "extern char **__wine_main_argv;\n" );
552     fprintf( outfile, "extern char **__wine_main_environ;\n" );
553     fprintf( outfile, "extern unsigned short **__wine_main_wargv;\n" );
554     if (target_platform == PLATFORM_APPLE)
555     {
556         fprintf( outfile, "extern _dyld_func_lookup(char *, void *);" );
557         fprintf( outfile, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
558         fprintf( outfile, "{\n" );
559         fprintf( outfile, "    void (*init)(void);\n" );
560         fprintf( outfile, "    _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
561         fprintf( outfile, "    init();\n" );
562         fprintf( outfile, "}\n" );
563         fprintf( outfile, "static void __wine_spec_hidden_fini()\n" );
564         fprintf( outfile, "{\n" );
565         fprintf( outfile, "    void (*fini)(void);\n" );
566         fprintf( outfile, "    _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
567         fprintf( outfile, "    fini();\n" );
568         fprintf( outfile, "}\n" );
569         fprintf( outfile, "#define _init __wine_spec_hidden_init\n" );
570         fprintf( outfile, "#define _fini __wine_spec_hidden_fini\n" );
571     }
572     else
573     {
574         fprintf( outfile, "extern void _init(int, char**, char**);\n" );
575         fprintf( outfile, "extern void _fini();\n" );
576     }
577
578     if (spec->characteristics & IMAGE_FILE_DLL)
579     {
580         if (init_func)
581             fprintf( outfile, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func );
582         else
583         {
584             declare_weak_function( outfile, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
585             init_func = "DllMain";
586         }
587         fprintf( outfile,
588                  "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
589                  "{\n"
590                  "    int ret;\n"
591                  "    if (reason == %d && __wine_spec_init_state == 1)\n"
592                  "        _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
593                  "    ret = %s ? %s( inst, reason, reserved ) : 1;\n"
594                  "    if (reason == %d && __wine_spec_init_state == 1)\n",
595                  DLL_PROCESS_ATTACH, init_func, init_func, DLL_PROCESS_DETACH );
596         if (!nr_delayed)
597             fprintf( outfile, "        _fini();\n" );
598         else
599             fprintf( outfile,
600                      "    {\n"
601                      "        extern int __stdcall FreeLibrary(void *);\n"
602                      "        unsigned int i;\n"
603                      "        _fini();\n"
604                      "        for (i = 0; i < sizeof(__wine_delay_imp_hmod)/sizeof(__wine_delay_imp_hmod[0]); i++)\n"
605                      "            if (__wine_delay_imp_hmod[i]) FreeLibrary( __wine_delay_imp_hmod[i] );\n"
606                      "    }\n" );
607         fprintf( outfile, "    return ret;\n}\n" );
608         init_func = "__wine_dll_main";
609     }
610     else switch(spec->subsystem)
611     {
612     case IMAGE_SUBSYSTEM_NATIVE:
613         if (init_func)
614             fprintf( outfile, "extern int __stdcall %s( void*, void* );\n\n", init_func );
615         else
616         {
617             declare_weak_function( outfile, "int __stdcall", "DriverEntry", "void*, void*");
618             init_func = "DriverEntry";
619         }
620         fprintf( outfile,
621                  "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
622                  "{\n"
623                  "    int ret;\n"
624                  "    if (__wine_spec_init_state == 1)\n"
625                  "        _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
626                  "    ret = %s ? %s( obj, path ) : 0;\n"
627                  "    if (__wine_spec_init_state == 1) _fini();\n"
628                  "    return ret;\n"
629                  "}\n",
630                  init_func, init_func );
631         init_func = "__wine_driver_entry";
632         break;
633     case IMAGE_SUBSYSTEM_WINDOWS_GUI:
634     case IMAGE_SUBSYSTEM_WINDOWS_CUI:
635         if (init_func)
636             fprintf( outfile, "extern int %s( int argc, char *argv[] );\n", init_func );
637         else
638         {
639             declare_weak_function( outfile, "int", "main", "int argc, char *argv[]" );
640             declare_weak_function( outfile, "int", "wmain", "int argc, unsigned short *argv[]" );
641             declare_weak_function( outfile, "int __stdcall", "WinMain", "void *,void *,char *,int" );
642         }
643         fprintf( outfile,
644                  "\ntypedef struct {\n"
645                  "    unsigned int cb;\n"
646                  "    char *lpReserved, *lpDesktop, *lpTitle;\n"
647                  "    unsigned int dwX, dwY, dwXSize, dwYSize;\n"
648                  "    unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
649                  "    unsigned short wShowWindow, cbReserved2;\n"
650                  "    char *lpReserved2;\n"
651                  "    void *hStdInput, *hStdOutput, *hStdError;\n"
652                  "} STARTUPINFOA;\n"
653                  "extern char * __stdcall GetCommandLineA(void);\n"
654                  "extern void * __stdcall GetModuleHandleA(char *);\n"
655                  "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
656                  "extern void __stdcall ExitProcess(unsigned int);\n"
657                  "static void __wine_exe_main(void)\n"
658                  "{\n"
659                  "    int ret;\n"
660                  "    if (__wine_spec_init_state == 1)\n"
661                  "        _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
662         if (init_func)
663             fprintf( outfile,
664                      "    ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func );
665         else
666             fprintf( outfile,
667                      "    if (WinMain) {\n"
668                      "        STARTUPINFOA info;\n"
669                      "        char *cmdline = GetCommandLineA();\n"
670                      "        int bcount=0, in_quotes=0;\n"
671                      "        while (*cmdline) {\n"
672                      "            if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
673                      "            else if (*cmdline=='\\\\') bcount++;\n"
674                      "            else if (*cmdline=='\\\"') {\n"
675                      "                if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
676                      "                bcount=0;\n"
677                      "            }\n"
678                      "            else bcount=0;\n"
679                      "            cmdline++;\n"
680                      "        }\n"
681                      "        while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
682                      "        GetStartupInfoA( &info );\n"
683                      "        if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
684                      "        ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
685                      "    }\n"
686                      "    else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
687                      "    else ret = main( __wine_main_argc, __wine_main_argv );\n" );
688         fprintf( outfile,
689                  "    if (__wine_spec_init_state == 1) _fini();\n"
690                  "    ExitProcess( ret );\n"
691                  "}\n\n" );
692         init_func = "__wine_exe_main";
693         break;
694     }
695
696     /* Output the NT header */
697
698     /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
699     fprintf( outfile, "static const struct image_nt_headers\n{\n" );
700     fprintf( outfile, "  int Signature;\n" );
701     fprintf( outfile, "  struct file_header {\n" );
702     fprintf( outfile, "    short Machine;\n" );
703     fprintf( outfile, "    short NumberOfSections;\n" );
704     fprintf( outfile, "    int   TimeDateStamp;\n" );
705     fprintf( outfile, "    void *PointerToSymbolTable;\n" );
706     fprintf( outfile, "    int   NumberOfSymbols;\n" );
707     fprintf( outfile, "    short SizeOfOptionalHeader;\n" );
708     fprintf( outfile, "    short Characteristics;\n" );
709     fprintf( outfile, "  } FileHeader;\n" );
710     fprintf( outfile, "  struct opt_header {\n" );
711     fprintf( outfile, "    short Magic;\n" );
712     fprintf( outfile, "    char  MajorLinkerVersion, MinorLinkerVersion;\n" );
713     fprintf( outfile, "    int   SizeOfCode;\n" );
714     fprintf( outfile, "    int   SizeOfInitializedData;\n" );
715     fprintf( outfile, "    int   SizeOfUninitializedData;\n" );
716     fprintf( outfile, "    void *AddressOfEntryPoint;\n" );
717     fprintf( outfile, "    void *BaseOfCode;\n" );
718     fprintf( outfile, "    void *BaseOfData;\n" );
719     fprintf( outfile, "    void *ImageBase;\n" );
720     fprintf( outfile, "    int   SectionAlignment;\n" );
721     fprintf( outfile, "    int   FileAlignment;\n" );
722     fprintf( outfile, "    short MajorOperatingSystemVersion;\n" );
723     fprintf( outfile, "    short MinorOperatingSystemVersion;\n" );
724     fprintf( outfile, "    short MajorImageVersion;\n" );
725     fprintf( outfile, "    short MinorImageVersion;\n" );
726     fprintf( outfile, "    short MajorSubsystemVersion;\n" );
727     fprintf( outfile, "    short MinorSubsystemVersion;\n" );
728     fprintf( outfile, "    int   Win32VersionValue;\n" );
729     fprintf( outfile, "    void *SizeOfImage;\n" );
730     fprintf( outfile, "    int   SizeOfHeaders;\n" );
731     fprintf( outfile, "    int   CheckSum;\n" );
732     fprintf( outfile, "    short Subsystem;\n" );
733     fprintf( outfile, "    short DllCharacteristics;\n" );
734     fprintf( outfile, "    int   SizeOfStackReserve;\n" );
735     fprintf( outfile, "    int   SizeOfStackCommit;\n" );
736     fprintf( outfile, "    int   SizeOfHeapReserve;\n" );
737     fprintf( outfile, "    int   SizeOfHeapCommit;\n" );
738     fprintf( outfile, "    int   LoaderFlags;\n" );
739     fprintf( outfile, "    int   NumberOfRvaAndSizes;\n" );
740     fprintf( outfile, "    struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
741              IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
742     fprintf( outfile, "  } OptionalHeader;\n" );
743     fprintf( outfile, "} nt_header = {\n" );
744     fprintf( outfile, "  0x%04x,\n", IMAGE_NT_SIGNATURE );   /* Signature */
745     switch(target_cpu)
746     {
747     case CPU_x86:
748         fprintf( outfile, "  { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 );  /* Machine */
749         break;
750     case CPU_POWERPC:
751         fprintf( outfile, "  { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC ); /* Machine */
752         break;
753     case CPU_ALPHA:
754         fprintf( outfile, "  { 0x%04x,\n", IMAGE_FILE_MACHINE_ALPHA ); /* Machine */
755         break;
756     case CPU_SPARC:
757         fprintf( outfile, "  { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN );  /* Machine */
758         break;
759     }
760     fprintf( outfile, "    0, 0, 0, 0,\n" );
761     fprintf( outfile, "    sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
762     fprintf( outfile, "    0x%04x },\n", spec->characteristics );  /* Characteristics */
763
764     fprintf( outfile, "  { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC );  /* Magic */
765     fprintf( outfile, "    0, 0,\n" );                   /* Major/MinorLinkerVersion */
766     fprintf( outfile, "    0, 0, 0,\n" );                /* SizeOfCode/Data */
767     fprintf( outfile, "    %s,\n", init_func );          /* AddressOfEntryPoint */
768     fprintf( outfile, "    0, __wine_spec_data_start,\n" );              /* BaseOfCode/Data */
769     fprintf( outfile, "    __wine_spec_pe_header,\n" );  /* ImageBase */
770     fprintf( outfile, "    %u,\n", page_size );          /* SectionAlignment */
771     fprintf( outfile, "    %u,\n", page_size );          /* FileAlignment */
772     fprintf( outfile, "    1, 0,\n" );                   /* Major/MinorOperatingSystemVersion */
773     fprintf( outfile, "    0, 0,\n" );                   /* Major/MinorImageVersion */
774     fprintf( outfile, "    %d,\n", spec->subsystem_major );             /* MajorSubsystemVersion */
775     fprintf( outfile, "    %d,\n", spec->subsystem_minor );             /* MinorSubsystemVersion */
776     fprintf( outfile, "    0,\n" );                      /* Win32VersionValue */
777     fprintf( outfile, "    _end,\n" );                   /* SizeOfImage */
778     fprintf( outfile, "    %u,\n", page_size );          /* SizeOfHeaders */
779     fprintf( outfile, "    0,\n" );                      /* CheckSum */
780     fprintf( outfile, "    0x%04x,\n", spec->subsystem );/* Subsystem */
781     fprintf( outfile, "    0,\n" );                      /* DllCharacteristics */
782     fprintf( outfile, "    %u, %u,\n",                   /* SizeOfStackReserve/Commit */
783              (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
784     fprintf( outfile, "    %u, %u,\n",                   /* SizeOfHeapReserve/Commit */
785              (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
786     fprintf( outfile, "    0,\n" );                      /* LoaderFlags */
787     fprintf( outfile, "    %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES );  /* NumberOfRvaAndSizes */
788     fprintf( outfile, "    {\n" );
789     fprintf( outfile, "      { %s, %d },\n",  /* IMAGE_DIRECTORY_ENTRY_EXPORT */
790              exports_size ? "__wine_spec_exports" : "0", exports_size );
791     fprintf( outfile, "      { %s, %s },\n",  /* IMAGE_DIRECTORY_ENTRY_IMPORT */
792              nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
793     fprintf( outfile, "      { %s, %s },\n",   /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
794              spec->nb_resources ? "&__wine_spec_resources" : "0",
795              spec->nb_resources ? "sizeof(__wine_spec_resources)" : "0" );
796     fprintf( outfile, "    }\n  }\n};\n\n" );
797
798     /* Output the DLL constructor */
799
800     fprintf( outfile,
801              "void __wine_spec_init(void)\n"
802              "{\n"
803              "    extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
804              "    __wine_spec_init_state = 1;\n"
805              "    __wine_dll_register( &nt_header, \"%s\" );\n"
806              "}\n\n",
807              spec->file_name );
808
809     fprintf( outfile,
810              "void __wine_spec_init_ctor(void)\n"
811              "{\n"
812              "    if (__wine_spec_init_state) return;\n"
813              "    __wine_spec_init();\n"
814              "    __wine_spec_init_state = 2;\n"
815              "}\n" );
816
817     fprintf( outfile, "#ifndef __GNUC__\n" );
818     fprintf( outfile, "static void __asm__dummy(void) {\n" );
819     fprintf( outfile, "#endif\n" );
820
821     output_exports( outfile, spec );
822     output_import_thunks( outfile, spec );
823     output_dll_init( outfile, "__wine_spec_init_ctor", NULL );
824
825     fprintf( outfile, "#ifndef __GNUC__\n" );
826     fprintf( outfile, "}\n" );
827     fprintf( outfile, "#endif\n" );
828 }
829
830
831 /*******************************************************************
832  *         BuildDef32File
833  *
834  * Build a Win32 def file from a spec file.
835  */
836 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
837 {
838     const char *name;
839     int i;
840
841     if (spec_file_name)
842         fprintf( outfile, "; File generated automatically from %s; do not edit!\n\n",
843                  spec_file_name );
844     else
845         fprintf( outfile, "; File generated automatically; do not edit!\n\n" );
846
847     fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
848
849     fprintf(outfile, "EXPORTS\n");
850
851     /* Output the exports and relay entry points */
852
853     for(i = 0; i < spec->nb_entry_points; i++)
854     {
855         const ORDDEF *odp = &spec->entry_points[i];
856         int is_data = 0;
857
858         if (!odp) continue;
859         if (odp->type == TYPE_STUB) continue;
860
861         if (odp->name) name = odp->name;
862         else if (odp->export_name) name = odp->export_name;
863         else continue;
864
865         fprintf(outfile, "  %s", name);
866
867         switch(odp->type)
868         {
869         case TYPE_EXTERN:
870             is_data = 1;
871             /* fall through */
872         case TYPE_VARARGS:
873         case TYPE_CDECL:
874             /* try to reduce output */
875             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
876                 fprintf(outfile, "=%s", odp->link_name);
877             break;
878         case TYPE_STDCALL:
879         {
880             int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
881             if (!kill_at) fprintf(outfile, "@%d", at_param);
882             if  (odp->flags & FLAG_FORWARD)
883             {
884                 fprintf(outfile, "=%s", odp->link_name);
885             }
886             else if (strcmp(name, odp->link_name)) /* try to reduce output */
887             {
888                 fprintf(outfile, "=%s", odp->link_name);
889                 if (!kill_at) fprintf(outfile, "@%d", at_param);
890             }
891             break;
892         }
893         default:
894             assert(0);
895         }
896         fprintf( outfile, " @%d", odp->ordinal );
897         if (!odp->name) fprintf( outfile, " NONAME" );
898         if (is_data) fprintf( outfile, " DATA" );
899         if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
900         fprintf( outfile, "\n" );
901     }
902 }
903
904
905 /*******************************************************************
906  *         BuildDebugFile
907  *
908  * Build the debugging channels source file.
909  */
910 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
911 {
912     int nr_debug;
913     char *prefix, *p, *constructor, *destructor;
914
915     while (*argv)
916     {
917         if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
918     }
919
920     output_standard_file_header( outfile );
921     nr_debug = output_debug( outfile );
922     if (!nr_debug)
923     {
924         fprintf( outfile, "/* no debug channels found for this module */\n" );
925         return;
926     }
927
928     if (output_file_name)
929     {
930         if ((p = strrchr( output_file_name, '/' ))) p++;
931         prefix = xstrdup( p ? p : output_file_name );
932         if ((p = strchr( prefix, '.' ))) *p = 0;
933         strcpy( p, make_c_identifier(p) );
934     }
935     else prefix = xstrdup( "_" );
936
937     /* Output the DLL constructor */
938
939     constructor = xmalloc( strlen(prefix) + 17 );
940     destructor = xmalloc( strlen(prefix) + 17 );
941     sprintf( constructor, "__wine_dbg_%s_init", prefix );
942     sprintf( destructor, "__wine_dbg_%s_fini", prefix );
943     fprintf( outfile,
944              "#ifdef __GNUC__\n"
945              "void %s(void) __attribute__((constructor));\n"
946              "void %s(void) __attribute__((destructor));\n"
947              "#else\n"
948              "static void __asm__dummy_dll_init(void) {\n",
949              constructor, destructor );
950     output_dll_init( outfile, constructor, destructor );
951     fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
952
953     fprintf( outfile,
954              "void %s(void)\n"
955              "{\n"
956              "    extern void *__wine_dbg_register( char * const *, int );\n"
957              "    if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
958              "}\n\n", constructor, nr_debug );
959     fprintf( outfile,
960              "void %s(void)\n"
961              "{\n"
962              "    extern void __wine_dbg_unregister( void* );\n"
963              "    __wine_dbg_unregister( debug_registration );\n"
964              "}\n", destructor );
965
966     free( constructor );
967     free( destructor );
968     free( prefix );
969 }