Exported CALL32_Regs from ntdll, renamed to __wine_call_from_32_regs
[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
11 #include "config.h"
12
13 #include <assert.h>
14 #include <ctype.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #include "winbase.h"
19 #include "wine/exception.h"
20 #include "build.h"
21
22
23 static int string_compare( const void *ptr1, const void *ptr2 )
24 {
25     const char * const *str1 = ptr1;
26     const char * const *str2 = ptr2;
27     return strcmp( *str1, *str2 );
28 }
29
30
31 /*******************************************************************
32  *         make_internal_name
33  *
34  * Generate an internal name for an entry point. Used for stubs etc.
35  */
36 static const char *make_internal_name( const ORDDEF *odp, const char *prefix )
37 {
38     static char buffer[256];
39     if (odp->name[0])
40     {
41         char *p;
42         sprintf( buffer, "__wine_%s_%s_%s", prefix, DLLName, odp->name );
43         /* make sure name is a legal C identifier */
44         for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
45         if (!*p) return buffer;
46     }
47     sprintf( buffer, "__wine_%s_%s_%d", prefix, DLLName, odp->ordinal );
48     return buffer;
49 }
50
51 /*******************************************************************
52  *         AssignOrdinals
53  *
54  * Assign ordinals to all entry points.
55  */
56 static void AssignOrdinals(void)
57 {
58     int i, ordinal;
59
60     if ( !nb_names ) return;
61
62     /* start assigning from Base, or from 1 if no ordinal defined yet */
63     if (Base == MAX_ORDINALS) Base = 1;
64     for (i = 0, ordinal = Base; i < nb_names; i++)
65     {
66         if (Names[i]->ordinal != -1) continue;  /* already has an ordinal */
67         while (Ordinals[ordinal]) ordinal++;
68         if (ordinal >= MAX_ORDINALS)
69         {
70             current_line = Names[i]->lineno;
71             fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
72         }
73         Names[i]->ordinal = ordinal;
74         Ordinals[ordinal] = Names[i];
75     }
76     if (ordinal > Limit) Limit = ordinal;
77 }
78
79
80 /*******************************************************************
81  *         output_debug
82  *
83  * Output the debug channels.
84  */
85 static int output_debug( FILE *outfile )
86 {
87     int i;
88
89     if (!nb_debug_channels) return 0;
90     qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
91
92     for (i = 0; i < nb_debug_channels; i++)
93         fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
94                  debug_channels[i], debug_channels[i] );
95
96     fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
97     for (i = 0; i < nb_debug_channels; i++)
98     {
99         fprintf( outfile, "    __wine_dbch_%s", debug_channels[i] );
100         if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
101     }
102     fprintf( outfile, "\n};\n\n" );
103     fprintf( outfile, "static void *debug_registration;\n\n" );
104
105     return nb_debug_channels;
106 }
107
108
109 /*******************************************************************
110  *         output_exports
111  *
112  * Output the export table for a Win32 module.
113  */
114 static int output_exports( FILE *outfile, int nr_exports )
115 {
116     int i, fwd_size = 0, total_size = 0;
117     char *p;
118
119     if (!nr_exports) return 0;
120
121     fprintf( outfile, "asm(\".data\\n\"\n" );
122     fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
123     fprintf( outfile, "    \"" PREFIX "__wine_spec_exports:\\n\"\n" );
124
125     /* export directory header */
126
127     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* Characteristics */
128     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* TimeDateStamp */
129     fprintf( outfile, "    \"\\t.long 0\\n\"\n" );                 /* MajorVersion/MinorVersion */
130     fprintf( outfile, "    \"\\t.long " PREFIX "dllname\\n\"\n" ); /* Name */
131     fprintf( outfile, "    \"\\t.long %d\\n\"\n", Base );          /* Base */
132     fprintf( outfile, "    \"\\t.long %d\\n\"\n", nr_exports );    /* NumberOfFunctions */
133     fprintf( outfile, "    \"\\t.long %d\\n\"\n", nb_names );      /* NumberOfNames */
134     fprintf( outfile, "    \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
135     if (nb_names)
136     {
137         fprintf( outfile, "    \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" );     /* AddressOfNames */
138         fprintf( outfile, "    \"\\t.long __wine_spec_exp_ordinals\\n\"\n" );  /* AddressOfNameOrdinals */
139     }
140     else
141     {
142         fprintf( outfile, "    \"\\t.long 0\\n\"\n" );  /* AddressOfNames */
143         fprintf( outfile, "    \"\\t.long 0\\n\"\n" );  /* AddressOfNameOrdinals */
144     }
145     total_size += 10 * sizeof(int);
146
147     /* output the function pointers */
148
149     fprintf( outfile, "    \"__wine_spec_exports_funcs:\\n\"\n" );
150     for (i = Base; i <= Limit; i++)
151     {
152         ORDDEF *odp = Ordinals[i];
153         if (!odp) fprintf( outfile, "    \"\\t.long 0\\n\"\n" );
154         else switch(odp->type)
155         {
156         case TYPE_EXTERN:
157             fprintf( outfile, "    \"\\t.long " PREFIX "%s\\n\"\n", odp->link_name );
158             break;
159         case TYPE_STDCALL:
160         case TYPE_VARARGS:
161         case TYPE_CDECL:
162             fprintf( outfile, "    \"\\t.long " PREFIX "%s\\n\"\n", odp->link_name);
163             break;
164         case TYPE_STUB:
165             fprintf( outfile, "    \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "stub" ) );
166             break;
167         case TYPE_REGISTER:
168             fprintf( outfile, "    \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "regs" ) );
169             break;
170         case TYPE_VARIABLE:
171             fprintf( outfile, "    \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "var" ) );
172             break;
173         case TYPE_FORWARD:
174             fprintf( outfile, "    \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
175             fwd_size += strlen(odp->link_name) + 1;
176             break;
177         default:
178             assert(0);
179         }
180     }
181     total_size += (Limit - Base + 1) * sizeof(int);
182
183     if (nb_names)
184     {
185         /* output the function name pointers */
186
187         int namepos = 0;
188
189         fprintf( outfile, "    \"__wine_spec_exp_name_ptrs:\\n\"\n" );
190         for (i = 0; i < nb_names; i++)
191         {
192             fprintf( outfile, "    \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
193             namepos += strlen(Names[i]->name) + 1;
194         }
195         total_size += nb_names * sizeof(int);
196
197         /* output the function names */
198
199         fprintf( outfile, "    \"\\t.text 1\\n\"\n" );
200         fprintf( outfile, "    \"__wine_spec_exp_names:\\n\"\n" );
201         for (i = 0; i < nb_names; i++)
202             fprintf( outfile, "    \"\\t" STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
203         fprintf( outfile, "    \"\\t.data\\n\"\n" );
204
205         /* output the function ordinals */
206
207         fprintf( outfile, "    \"__wine_spec_exp_ordinals:\\n\"\n" );
208         for (i = 0; i < nb_names; i++)
209         {
210             fprintf( outfile, "    \"\\t.short %d\\n\"\n", Names[i]->ordinal - Base );
211         }
212         total_size += nb_names * sizeof(short);
213         if (nb_names % 2)
214         {
215             fprintf( outfile, "    \"\\t.short 0\\n\"\n" );
216             total_size += sizeof(short);
217         }
218     }
219
220     /* output forward strings */
221
222     if (fwd_size)
223     {
224         fprintf( outfile, "    \"__wine_spec_forwards:\\n\"\n" );
225         for (i = Base; i <= Limit; i++)
226         {
227             ORDDEF *odp = Ordinals[i];
228             if (odp && odp->type == TYPE_FORWARD)
229                 fprintf( outfile, "    \"\\t" STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
230         }
231         fprintf( outfile, "    \"\\t.align %d\\n\"\n", get_alignment(4) );
232         total_size += (fwd_size + 3) & ~3;
233     }
234
235     /* output relays */
236
237     if (debugging)
238     {
239         for (i = Base; i <= Limit; i++)
240         {
241             ORDDEF *odp = Ordinals[i];
242             unsigned int j, mask = 0;
243
244             /* skip non-existent entry points */
245             if (!odp) goto ignore;
246             /* skip non-functions */
247             if ((odp->type != TYPE_STDCALL) &&
248                 (odp->type != TYPE_CDECL) &&
249                 (odp->type != TYPE_REGISTER)) goto ignore;
250             /* skip norelay entry points */
251             if (odp->flags & FLAG_NORELAY) goto ignore;
252
253             for (j = 0; odp->u.func.arg_types[j]; j++)
254             {
255                 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
256                 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
257             }
258             if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
259
260             switch(odp->type)
261             {
262             case TYPE_STDCALL:
263                 fprintf( outfile, "    \"\\tjmp " PREFIX "%s\\n\"\n", odp->link_name );
264                 fprintf( outfile, "    \"\\tret $%d\\n\"\n",
265                          strlen(odp->u.func.arg_types) * sizeof(int) );
266                 fprintf( outfile, "    \"\\t.long " PREFIX "%s,0x%08x\\n\"\n",
267                          odp->link_name, mask );
268                 break;
269             case TYPE_CDECL:
270                 fprintf( outfile, "    \"\\tjmp " PREFIX "%s\\n\"\n", odp->link_name );
271                 fprintf( outfile, "    \"\\tret\\n\"\n" );
272                 fprintf( outfile, "    \"\\t.short %d\\n\"\n",
273                          strlen(odp->u.func.arg_types) * sizeof(int) );
274                 fprintf( outfile, "    \"\\t.long " PREFIX "%s,0x%08x\\n\"\n",
275                          odp->link_name, mask );
276                 break;
277             case TYPE_REGISTER:
278                 fprintf( outfile, "    \"\\tjmp " PREFIX "%s\\n\"\n",
279                          make_internal_name( odp, "regs" ) );
280                 fprintf( outfile, "    \"\\tret\\n\"\n" );
281                 fprintf( outfile, "    \"\\t.short 0x%04x\\n\"\n",
282                          0x8000 | strlen(odp->u.func.arg_types) * sizeof(int) );
283                 fprintf( outfile, "    \"\\t.long " PREFIX "%s,0x%08x\\n\"\n",
284                          make_internal_name( odp, "regs" ), mask );
285                 break;
286             default:
287                 assert(0);
288             }
289             continue;
290
291         ignore:
292             fprintf( outfile, "    \"\\t.long 0,0,0,0\\n\"\n" );
293         }
294     }
295
296
297     /* output __wine_dllexport symbols */
298
299     for (i = 0; i < nb_names; i++)
300     {
301         if (Names[i]->flags & FLAG_NOIMPORT) continue;
302         /* check for invalid characters in the name */
303         for (p = Names[i]->name; *p; p++)
304             if (!isalnum(*p) && *p != '_' && *p != '.') break;
305         if (*p) continue;
306         fprintf( outfile, "    \"\\t.globl " PREFIX "__wine_dllexport_%s_%s\\n\"\n",
307                  DLLName, Names[i]->name );
308         fprintf( outfile, "    \"" PREFIX "__wine_dllexport_%s_%s:\\n\"\n",
309                  DLLName, Names[i]->name );
310     }
311     fprintf( outfile, "    \"\\t.long 0xffffffff\\n\"\n" );
312
313     /* output variables */
314
315     for (i = 0; i < nb_entry_points; i++)
316     {
317         ORDDEF *odp = EntryPoints[i];
318         if (odp->type == TYPE_VARIABLE)
319         {
320             int j;
321             fprintf( outfile, "    \"%s:\\n\"\n", make_internal_name( odp, "var" ) );
322             fprintf( outfile, "    \"\\t.long " );
323             for (j = 0; j < odp->u.var.n_values; j++)
324             {
325                 fprintf( outfile, "0x%08x", odp->u.var.values[j] );
326                 if (j < odp->u.var.n_values-1) fputc( ',', outfile );
327             }
328             fprintf( outfile, "\\n\"\n" );
329         }
330     }
331
332     fprintf( outfile, ");\n\n" );
333
334     return total_size;
335 }
336
337
338 /*******************************************************************
339  *         output_stub_funcs
340  *
341  * Output the functions for stub entry points
342 */
343 static void output_stub_funcs( FILE *outfile )
344 {
345     int i;
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, "#ifdef __GNUC__\n" );
352         fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
353         fprintf( outfile, "#endif\n\n" );
354         fprintf( outfile, "struct exc_record {\n" );
355         fprintf( outfile, "  unsigned int code, flags;\n" );
356         fprintf( outfile, "  void *rec, *addr;\n" );
357         fprintf( outfile, "  unsigned int params;\n" );
358         fprintf( outfile, "  const void *info[15];\n" );
359         fprintf( outfile, "};\n\n" );
360         fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
361         fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
362         fprintf( outfile, "  struct exc_record rec;\n" );
363         fprintf( outfile, "  rec.code    = 0x%08x;\n", EXCEPTION_WINE_STUB );
364         fprintf( outfile, "  rec.flags   = %d;\n", EH_NONCONTINUABLE );
365         fprintf( outfile, "  rec.rec     = 0;\n" );
366         fprintf( outfile, "  rec.params  = 2;\n" );
367         fprintf( outfile, "  rec.info[0] = dllname;\n" );
368         fprintf( outfile, "  rec.info[1] = func;\n" );
369         fprintf( outfile, "#ifdef __GNUC__\n" );
370         fprintf( outfile, "  rec.addr = __builtin_return_address(1);\n" );
371         fprintf( outfile, "#else\n" );
372         fprintf( outfile, "  rec.addr = 0;\n" );
373         fprintf( outfile, "#endif\n" );
374         fprintf( outfile, "  for (;;) RtlRaiseException( &rec );\n}\n\n" );
375         break;
376     }
377
378     for (i = 0; i < nb_entry_points; i++)
379     {
380         ORDDEF *odp = EntryPoints[i];
381         if (odp->type != TYPE_STUB) continue;
382         fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
383         if (odp->name[0])
384             fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
385         else
386             fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
387     }
388 }
389
390
391 /*******************************************************************
392  *         output_register_funcs
393  *
394  * Output the functions for register entry points
395  */
396 static void output_register_funcs( FILE *outfile )
397 {
398     const char *name;
399     int i;
400
401     for (i = 0; i < nb_entry_points; i++)
402     {
403         ORDDEF *odp = EntryPoints[i];
404         if (odp->type != TYPE_REGISTER) continue;
405         name = make_internal_name( odp, "regs" );
406         fprintf( outfile,
407                  "asm(\".align %d\\n\\t\"\n"
408                  "    \"" __ASM_FUNC("%s") "\\n\\t\"\n"
409                  "    \"" PREFIX "%s:\\n\\t\"\n"
410                  "    \"call " PREFIX "__wine_call_from_32_regs\\n\\t\"\n"
411                  "    \".long " PREFIX "%s\\n\\t\"\n"
412                  "    \".byte %d,%d\");\n",
413                  get_alignment(4),
414                  name, name, odp->link_name,
415                  4 * strlen(odp->u.func.arg_types), 4 * strlen(odp->u.func.arg_types) );
416     }
417 }
418
419
420 /*******************************************************************
421  *         BuildSpec32File
422  *
423  * Build a Win32 C file from a spec file.
424  */
425 void BuildSpec32File( FILE *outfile )
426 {
427     int exports_size = 0;
428     int nr_exports, nr_imports, nr_resources, nr_debug;
429     int characteristics, subsystem;
430     DWORD page_size;
431
432 #ifdef HAVE_GETPAGESIZE
433     page_size = getpagesize();
434 #else
435 # ifdef __svr4__
436     page_size = sysconf(_SC_PAGESIZE);
437 # else
438 #   error Cannot get the page size on this platform
439 # endif
440 #endif
441
442     AssignOrdinals();
443     nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
444
445     resolve_imports();
446
447     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
448              input_file_name );
449
450     /* Reserve some space for the PE header */
451
452     fprintf( outfile, "extern char pe_header[];\n" );
453     fprintf( outfile, "asm(\".section .text\\n\\t\"\n" );
454     fprintf( outfile, "    \".align %d\\n\"\n", get_alignment(page_size) );
455     fprintf( outfile, "    \"" PREFIX "pe_header:\\t.fill %ld,1,0\\n\\t\");\n", page_size );
456
457     fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLName );
458     fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
459
460 #ifdef __i386__
461     fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
462 #else
463     fprintf( outfile, "#define __stdcall\n\n" );
464 #endif
465
466     if (nr_exports)
467     {
468         /* Output the stub functions */
469
470         output_stub_funcs( outfile );
471
472         fprintf( outfile, "#ifndef __GNUC__\n" );
473         fprintf( outfile, "static void __asm__dummy(void) {\n" );
474         fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
475
476         /* Output code for all register functions */
477
478         output_register_funcs( outfile );
479
480         /* Output the exports and relay entry points */
481
482         exports_size = output_exports( outfile, nr_exports );
483
484         fprintf( outfile, "#ifndef __GNUC__\n" );
485         fprintf( outfile, "}\n" );
486         fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
487     }
488
489     /* Output the DLL imports */
490
491     nr_imports = output_imports( outfile );
492
493     /* Output the resources */
494
495     nr_resources = output_resources( outfile );
496
497     /* Output the debug channels */
498
499     nr_debug = output_debug( outfile );
500
501     /* Output LibMain function */
502
503     characteristics = subsystem = 0;
504     switch(SpecMode)
505     {
506     case SPEC_MODE_DLL:
507         if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
508         characteristics = IMAGE_FILE_DLL;
509         break;
510     case SPEC_MODE_GUIEXE:
511         if (!init_func) init_func = "WinMain";
512         fprintf( outfile,
513                  "\ntypedef struct {\n"
514                  "    unsigned int cb;\n"
515                  "    char *lpReserved, *lpDesktop, *lpTitle;\n"
516                  "    unsigned int dwX, dwY, dwXSize, dwYSize;\n"
517                  "    unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
518                  "    unsigned short wShowWindow, cbReserved2;\n"
519                  "    char *lpReserved2;\n"
520                  "    void *hStdInput, *hStdOutput, *hStdError;\n"
521                  "} STARTUPINFOA;\n"
522                  "int _ARGC;\n"
523                  "char **_ARGV;\n"
524                  "extern int __stdcall %s(void *,void *,char *,int);\n"
525                  "extern char * __stdcall GetCommandLineA(void);\n"
526                  "extern void * __stdcall GetModuleHandleA(char *);\n"
527                  "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
528                  "extern void __stdcall ExitProcess(unsigned int);\n"
529                  "static void __wine_exe_main(void)\n"
530                  "{\n"
531                  "    extern int __wine_get_main_args( char ***argv );\n"
532                  "    STARTUPINFOA info;\n"
533                  "    char *cmdline = GetCommandLineA();\n"
534                  "    while (*cmdline && *cmdline != ' ') cmdline++;\n"
535                  "    if (*cmdline) cmdline++;\n"
536                  "    GetStartupInfoA( &info );\n"
537                  "    if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
538                  "    _ARGC = __wine_get_main_args( &_ARGV );\n"
539                  "    ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
540                  "}\n\n", init_func, init_func );
541         init_func = "__wine_exe_main";
542         subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
543         break;
544     case SPEC_MODE_GUIEXE_UNICODE:
545         if (!init_func) init_func = "WinMain";
546         fprintf( outfile,
547                  "\ntypedef unsigned short WCHAR;\n"
548                  "typedef struct {\n"
549                  "    unsigned int cb;\n"
550                  "    char *lpReserved, *lpDesktop, *lpTitle;\n"
551                  "    unsigned int dwX, dwY, dwXSize, dwYSize;\n"
552                  "    unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
553                  "    unsigned short wShowWindow, cbReserved2;\n"
554                  "    char *lpReserved2;\n"
555                  "    void *hStdInput, *hStdOutput, *hStdError;\n"
556                  "} STARTUPINFOA;\n"
557                  "int _ARGC;\n"
558                  "WCHAR **_ARGV;\n"
559                  "extern int __stdcall %s(void *,void *,char *,int);\n"
560                  "extern char * __stdcall GetCommandLineA(void);\n"
561                  "extern void * __stdcall GetModuleHandleA(char *);\n"
562                  "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
563                  "extern void __stdcall ExitProcess(unsigned int);\n"
564                  "static void __wine_exe_main(void)\n"
565                  "{\n"
566                  "    extern int __wine_get_wmain_args( WCHAR ***argv );\n"
567                  "    STARTUPINFOA info;\n"
568                  "    char *cmdline = GetCommandLineA();\n"
569                  "    while (*cmdline && *cmdline != ' ') cmdline++;\n"
570                  "    if (*cmdline) cmdline++;\n"
571                  "    GetStartupInfoA( &info );\n"
572                  "    if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
573                  "    _ARGC = __wine_get_wmain_args( &_ARGV );\n"
574                  "    ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
575                  "}\n\n", init_func, init_func );
576         init_func = "__wine_exe_main";
577         subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
578         break;
579     case SPEC_MODE_CUIEXE:
580         if (!init_func) init_func = "main";
581         fprintf( outfile,
582                  "\nint _ARGC;\n"
583                  "char **_ARGV;\n"
584                  "extern void __stdcall ExitProcess(int);\n"
585                  "static void __wine_exe_main(void)\n"
586                  "{\n"
587                  "    extern int %s( int argc, char *argv[] );\n"
588                  "    extern int __wine_get_main_args( char ***argv );\n"
589                  "    _ARGC = __wine_get_main_args( &_ARGV );\n"
590                  "    ExitProcess( %s( _ARGC, _ARGV ) );\n"
591                  "}\n\n", init_func, init_func );
592         init_func = "__wine_exe_main";
593         subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
594         break;
595     case SPEC_MODE_CUIEXE_UNICODE:
596         if (!init_func) init_func = "wmain";
597         fprintf( outfile,
598                  "\ntypedef unsigned short WCHAR;\n"
599                  "int _ARGC;\n"
600                  "WCHAR **_ARGV;\n"
601                  "extern void __stdcall ExitProcess(int);\n"
602                  "static void __wine_exe_main(void)\n"
603                  "{\n"
604                  "    extern int %s( int argc, WCHAR *argv[] );\n"
605                  "    extern int __wine_get_wmain_args( WCHAR ***argv );\n"
606                  "    _ARGC = __wine_get_wmain_args( &_ARGV );\n"
607                  "    ExitProcess( %s( _ARGC, _ARGV ) );\n"
608                  "}\n\n", init_func, init_func );
609         init_func = "__wine_exe_main";
610         subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
611         break;
612     }
613
614     /* Output the NT header */
615
616     /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
617     fprintf( outfile, "static const struct image_nt_headers\n{\n" );
618     fprintf( outfile, "  int Signature;\n" );
619     fprintf( outfile, "  struct file_header {\n" );
620     fprintf( outfile, "    short Machine;\n" );
621     fprintf( outfile, "    short NumberOfSections;\n" );
622     fprintf( outfile, "    int   TimeDateStamp;\n" );
623     fprintf( outfile, "    void *PointerToSymbolTable;\n" );
624     fprintf( outfile, "    int   NumberOfSymbols;\n" );
625     fprintf( outfile, "    short SizeOfOptionalHeader;\n" );
626     fprintf( outfile, "    short Characteristics;\n" );
627     fprintf( outfile, "  } FileHeader;\n" );
628     fprintf( outfile, "  struct opt_header {\n" );
629     fprintf( outfile, "    short Magic;\n" );
630     fprintf( outfile, "    char  MajorLinkerVersion, MinorLinkerVersion;\n" );
631     fprintf( outfile, "    int   SizeOfCode;\n" );
632     fprintf( outfile, "    int   SizeOfInitializedData;\n" );
633     fprintf( outfile, "    int   SizeOfUninitializedData;\n" );
634     fprintf( outfile, "    void *AddressOfEntryPoint;\n" );
635     fprintf( outfile, "    void *BaseOfCode;\n" );
636     fprintf( outfile, "    void *BaseOfData;\n" );
637     fprintf( outfile, "    void *ImageBase;\n" );
638     fprintf( outfile, "    int   SectionAlignment;\n" );
639     fprintf( outfile, "    int   FileAlignment;\n" );
640     fprintf( outfile, "    short MajorOperatingSystemVersion;\n" );
641     fprintf( outfile, "    short MinorOperatingSystemVersion;\n" );
642     fprintf( outfile, "    short MajorImageVersion;\n" );
643     fprintf( outfile, "    short MinorImageVersion;\n" );
644     fprintf( outfile, "    short MajorSubsystemVersion;\n" );
645     fprintf( outfile, "    short MinorSubsystemVersion;\n" );
646     fprintf( outfile, "    int   Win32VersionValue;\n" );
647     fprintf( outfile, "    int   SizeOfImage;\n" );
648     fprintf( outfile, "    int   SizeOfHeaders;\n" );
649     fprintf( outfile, "    int   CheckSum;\n" );
650     fprintf( outfile, "    short Subsystem;\n" );
651     fprintf( outfile, "    short DllCharacteristics;\n" );
652     fprintf( outfile, "    int   SizeOfStackReserve;\n" );
653     fprintf( outfile, "    int   SizeOfStackCommit;\n" );
654     fprintf( outfile, "    int   SizeOfHeapReserve;\n" );
655     fprintf( outfile, "    int   SizeOfHeapCommit;\n" );
656     fprintf( outfile, "    int   LoaderFlags;\n" );
657     fprintf( outfile, "    int   NumberOfRvaAndSizes;\n" );
658     fprintf( outfile, "    struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
659              IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
660     fprintf( outfile, "  } OptionalHeader;\n" );
661     fprintf( outfile, "} nt_header = {\n" );
662     fprintf( outfile, "  0x%04x,\n", IMAGE_NT_SIGNATURE );   /* Signature */
663
664     fprintf( outfile, "  { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 );  /* Machine */
665     fprintf( outfile, "    0, 0, 0, 0,\n" );
666     fprintf( outfile, "    sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
667     fprintf( outfile, "    0x%04x },\n", characteristics );        /* Characteristics */
668
669     fprintf( outfile, "  { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC );  /* Magic */
670     fprintf( outfile, "    0, 0,\n" );                   /* Major/MinorLinkerVersion */
671     fprintf( outfile, "    0, 0, 0,\n" );                /* SizeOfCode/Data */
672     fprintf( outfile, "    %s,\n", init_func ? init_func : "0" );  /* AddressOfEntryPoint */
673     fprintf( outfile, "    0, 0,\n" );                   /* BaseOfCode/Data */
674     fprintf( outfile, "    pe_header,\n" );              /* ImageBase */
675     fprintf( outfile, "    %ld,\n", page_size );         /* SectionAlignment */
676     fprintf( outfile, "    %ld,\n", page_size );         /* FileAlignment */
677     fprintf( outfile, "    1, 0,\n" );                   /* Major/MinorOperatingSystemVersion */
678     fprintf( outfile, "    0, 0,\n" );                   /* Major/MinorImageVersion */
679     fprintf( outfile, "    4, 0,\n" );                   /* Major/MinorSubsystemVersion */
680     fprintf( outfile, "    0,\n" );                      /* Win32VersionValue */
681     fprintf( outfile, "    %ld,\n", page_size );         /* SizeOfImage */
682     fprintf( outfile, "    %ld,\n", page_size );         /* SizeOfHeaders */
683     fprintf( outfile, "    0,\n" );                      /* CheckSum */
684     fprintf( outfile, "    0x%04x,\n", subsystem );      /* Subsystem */
685     fprintf( outfile, "    0,\n" );                      /* DllCharacteristics */
686     fprintf( outfile, "    %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
687     fprintf( outfile, "    %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
688     fprintf( outfile, "    0,\n" );                      /* LoaderFlags */
689     fprintf( outfile, "    %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES );  /* NumberOfRvaAndSizes */
690     fprintf( outfile, "    {\n" );
691     fprintf( outfile, "      { %s, %d },\n",  /* IMAGE_DIRECTORY_ENTRY_EXPORT */
692              exports_size ? "__wine_spec_exports" : "0", exports_size );
693     fprintf( outfile, "      { %s, %s },\n",  /* IMAGE_DIRECTORY_ENTRY_IMPORT */
694              nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
695     fprintf( outfile, "      { %s, %s },\n",   /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
696              nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
697     fprintf( outfile, "    }\n  }\n};\n\n" );
698
699     /* Output the DLL constructor */
700
701     fprintf( outfile, "#ifndef __GNUC__\n" );
702     fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
703     fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
704
705 #if defined(__i386__)
706     fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
707     fprintf( outfile, "    \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n", DLLName );
708     fprintf( outfile, "    \"\\t.previous\\n\");\n" );
709     if (nr_debug)
710     {
711         fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
712         fprintf( outfile, "    \"\\tcall " PREFIX "__wine_spec_%s_fini\\n\"\n", DLLName );
713         fprintf( outfile, "    \"\\t.previous\\n\");\n" );
714     }
715 #elif defined(__sparc__)
716     fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
717     fprintf( outfile, "    \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n", DLLName );
718     fprintf( outfile, "    \"\\tnop\\n\"\n" );
719     fprintf( outfile, "    \"\\t.previous\\n\");\n" );
720     if (nr_debug)
721     {
722         fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
723         fprintf( outfile, "    \"\\tcall " PREFIX "__wine_spec_%s_fini\\n\"\n", DLLName );
724         fprintf( outfile, "    \"\\tnop\\n\"\n" );
725         fprintf( outfile, "    \"\\t.previous\\n\");\n" );
726     }
727 #elif defined(__PPC__)
728     fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
729     fprintf( outfile, "    \"\\tbl " PREFIX "__wine_spec_%s_init\\n\"\n",
730              DLLName );
731     fprintf( outfile, "    \"\\t.previous\\n\");\n" );
732     if (nr_debug)
733     {
734         fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
735         fprintf( outfile, "    \"\\tbl " PREFIX "__wine_spec_%s_fini\\n\"\n",
736                  DLLName );
737         fprintf( outfile, "    \"\\t.previous\\n\");\n" );
738     }
739 #else
740 #error You need to define the DLL constructor for your architecture
741 #endif
742
743     fprintf( outfile, "#ifndef __GNUC__\n" );
744     fprintf( outfile, "}\n" );
745     fprintf( outfile, "#endif /* defined(__GNUC__) */\n\n" );
746
747     fprintf( outfile,
748              "void __wine_spec_%s_init(void)\n"
749              "{\n"
750              "    extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
751              "    extern void *__wine_dbg_register( char * const *, int );\n"
752              "    __wine_dll_register( &nt_header, \"%s\" );\n",
753              DLLName, DLLFileName );
754     if (nr_debug)
755         fprintf( outfile, "    debug_registration = __wine_dbg_register( debug_channels, %d );\n",
756                  nr_debug );
757     fprintf( outfile, "}\n" );
758     if (nr_debug)
759     {
760         fprintf( outfile,
761                  "\nvoid __wine_spec_%s_fini(void)\n"
762                  "{\n"
763                  "    extern void __wine_dbg_unregister( void* );\n"
764                  "    __wine_dbg_unregister( debug_registration );\n"
765                  "}\n", DLLName );
766     }
767 }