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