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