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