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