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