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