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