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