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