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