Winebuild darwin/x86-specific fixes:
[wine/multimedia.git] / tools / winebuild / spec32.c
blob4b57c8f0d1cee7b7583058608d053256fbff1f33
1 /*
2 * 32-bit spec files
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wine/exception.h"
36 #include "build.h"
39 #ifdef __APPLE__
40 # define __ASM_SKIP ".space"
41 #else
42 # define __ASM_SKIP ".skip"
43 #endif
45 static int string_compare( const void *ptr1, const void *ptr2 )
47 const char * const *str1 = ptr1;
48 const char * const *str2 = ptr2;
49 return strcmp( *str1, *str2 );
53 /*******************************************************************
54 * make_internal_name
56 * Generate an internal name for an entry point. Used for stubs etc.
58 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
60 static char buffer[256];
61 if (odp->name || odp->export_name)
63 char *p;
64 sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->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;
70 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
71 return buffer;
74 /*******************************************************************
75 * declare_weak_function
77 * Output a prototype for a weak function.
79 static void declare_weak_function( FILE *outfile, const char *ret_type, const char *name, const char *params)
81 fprintf( outfile, "#ifdef __GNUC__\n" );
82 fprintf( outfile, "# ifdef __APPLE__\n" );
83 fprintf( outfile, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type, name, params );
84 fprintf( outfile, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type, name, params, name );
85 fprintf( outfile, "#define %s __wine_spec_weak_%s\n", name, name );
86 fprintf( outfile, "asm(\".weak_reference " __ASM_NAME("%s") "\");\n", name );
87 fprintf( outfile, "# else\n" );
88 fprintf( outfile, "extern %s %s(%s) __attribute__((weak));\n", ret_type, name, params );
89 fprintf( outfile, "# endif\n" );
90 fprintf( outfile, "#else\n" );
91 fprintf( outfile, "extern %s %s(%s);\n", ret_type, name, params );
92 fprintf( outfile, "static void __asm__dummy_%s(void)", name );
93 fprintf( outfile, " { asm(\".weak " __ASM_NAME("%s") "\"); }\n", name );
94 fprintf( outfile, "#endif\n\n" );
98 /*******************************************************************
99 * output_debug
101 * Output the debug channels.
103 static int output_debug( FILE *outfile )
105 int i;
107 if (!nb_debug_channels) return 0;
108 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
110 for (i = 0; i < nb_debug_channels; i++)
111 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
112 debug_channels[i], debug_channels[i] );
114 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
115 for (i = 0; i < nb_debug_channels; i++)
117 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
118 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
120 fprintf( outfile, "\n};\n\n" );
121 fprintf( outfile, "static void *debug_registration;\n\n" );
123 return nb_debug_channels;
127 /*******************************************************************
128 * get_exports_size
130 * Compute the size of the export table.
132 static int get_exports_size( DLLSPEC *spec )
134 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
135 int i, fwd_size = 0, total_size;
137 if (!nr_exports) return 0;
139 /* export directory header */
140 total_size = 10 * sizeof(int);
142 /* function pointers */
143 total_size += nr_exports * sizeof(int);
145 /* function name pointers */
146 total_size += spec->nb_names * sizeof(int);
148 /* function ordinals */
149 total_size += spec->nb_names * sizeof(short);
150 if (spec->nb_names % 2) total_size += sizeof(short);
152 /* forward strings */
153 for (i = spec->base; i <= spec->limit; i++)
155 ORDDEF *odp = spec->ordinals[i];
156 if (odp && odp->flags & FLAG_FORWARD) fwd_size += strlen(odp->link_name) + 1;
158 total_size += (fwd_size + 3) & ~3;
160 return total_size;
164 /*******************************************************************
165 * output_export_names
167 * Output all the exported names for a Win32 module.
169 static void output_export_names( FILE *outfile, DLLSPEC *spec )
171 int i, nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
173 if (!nr_exports) return;
175 fprintf( outfile, "\nconst char __wine_spec_exp_names[] =" );
176 fprintf( outfile, "\n \"%s\\0\"", spec->file_name );
177 for (i = 0; i < spec->nb_names; i++)
178 fprintf( outfile, "\n \"%s\\0\"", spec->names[i]->name );
179 fprintf( outfile, ";\n" );
183 /*******************************************************************
184 * output_exports
186 * Output the export table for a Win32 module.
188 static void output_exports( FILE *outfile, DLLSPEC *spec )
190 int i, fwd_size = 0;
191 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
193 if (!nr_exports) return;
195 fprintf( outfile, "/* export table */\n" );
196 fprintf( outfile, "asm(\".data\\n\"\n" );
197 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
198 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
200 /* export directory header */
202 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
203 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
204 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
205 fprintf( outfile, " \"\\t.long " __ASM_NAME("__wine_spec_exp_names") "\\n\"\n" ); /* Name */
206 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->base ); /* Base */
207 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
208 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->nb_names ); /* NumberOfNames */
209 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
210 if (spec->nb_names)
212 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
213 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
215 else
217 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
218 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
221 /* output the function pointers */
223 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
224 for (i = spec->base; i <= spec->limit; i++)
226 ORDDEF *odp = spec->ordinals[i];
227 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
228 else switch(odp->type)
230 case TYPE_EXTERN:
231 case TYPE_STDCALL:
232 case TYPE_VARARGS:
233 case TYPE_CDECL:
234 if (!(odp->flags & FLAG_FORWARD))
236 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", odp->link_name );
238 else
240 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
241 fwd_size += strlen(odp->link_name) + 1;
243 break;
244 case TYPE_STUB:
245 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
246 make_internal_name( odp, spec, "stub" ) );
247 break;
248 default:
249 assert(0);
253 if (spec->nb_names)
255 /* output the function name pointers */
257 int namepos = strlen(spec->file_name) + 1;
259 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
260 for (i = 0; i < spec->nb_names; i++)
262 fprintf( outfile, " \"\\t.long " __ASM_NAME("__wine_spec_exp_names") "+%d\\n\"\n", namepos );
263 namepos += strlen(spec->names[i]->name) + 1;
267 if (spec->nb_names)
269 /* output the function ordinals */
271 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
272 for (i = 0; i < spec->nb_names; i++)
274 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n",
275 spec->names[i]->ordinal - spec->base );
277 if (spec->nb_names % 2)
279 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
283 /* output forward strings */
285 if (fwd_size)
287 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
288 for (i = spec->base; i <= spec->limit; i++)
290 ORDDEF *odp = spec->ordinals[i];
291 if (odp && (odp->flags & FLAG_FORWARD))
292 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
294 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
297 /* output relays */
299 if (debugging)
301 for (i = spec->base; i <= spec->limit; i++)
303 ORDDEF *odp = spec->ordinals[i];
304 unsigned int j, args, mask = 0;
306 /* skip nonexistent entry points */
307 if (!odp) goto ignore;
308 /* skip non-functions */
309 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
310 /* skip norelay and forward entry points */
311 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
313 for (j = 0; odp->u.func.arg_types[j]; j++)
315 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
316 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
318 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
320 args = strlen(odp->u.func.arg_types) * sizeof(int);
322 switch(odp->type)
324 case TYPE_STDCALL:
325 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", odp->link_name );
326 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
327 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", odp->link_name, mask );
328 break;
329 case TYPE_CDECL:
330 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", odp->link_name );
331 fprintf( outfile, " \"\\tret\\n\"\n" );
332 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
333 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", odp->link_name, mask );
334 break;
335 default:
336 assert(0);
338 continue;
340 ignore:
341 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
344 fprintf( outfile, ");\n" );
348 /*******************************************************************
349 * output_stub_funcs
351 * Output the functions for stub entry points
353 static void output_stub_funcs( FILE *outfile, DLLSPEC *spec )
355 int i;
357 for (i = 0; i < spec->nb_entry_points; i++)
359 ORDDEF *odp = &spec->entry_points[i];
360 if (odp->type != TYPE_STUB) continue;
361 fprintf( outfile, "#ifdef __GNUC__\n" );
362 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
363 fprintf( outfile, "#endif\n\n" );
364 fprintf( outfile, "struct exc_record {\n" );
365 fprintf( outfile, " unsigned int code, flags;\n" );
366 fprintf( outfile, " void *rec, *addr;\n" );
367 fprintf( outfile, " unsigned int params;\n" );
368 fprintf( outfile, " const void *info[15];\n" );
369 fprintf( outfile, "};\n\n" );
370 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
371 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
372 fprintf( outfile, " struct exc_record rec;\n" );
373 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
374 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
375 fprintf( outfile, " rec.rec = 0;\n" );
376 fprintf( outfile, " rec.params = 2;\n" );
377 fprintf( outfile, " rec.info[0] = \"%s\";\n", spec->file_name );
378 fprintf( outfile, " rec.info[1] = func;\n" );
379 fprintf( outfile, "#ifdef __GNUC__\n" );
380 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
381 fprintf( outfile, "#else\n" );
382 fprintf( outfile, " rec.addr = 0;\n" );
383 fprintf( outfile, "#endif\n" );
384 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
385 break;
388 for (i = 0; i < spec->nb_entry_points; i++)
390 const ORDDEF *odp = &spec->entry_points[i];
391 if (odp->type != TYPE_STUB) continue;
392 fprintf( outfile, "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
393 if (odp->name)
394 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
395 else if (odp->export_name)
396 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
397 else
398 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
403 /*******************************************************************
404 * output_dll_init
406 * Output code for calling a dll constructor and destructor.
408 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
410 #ifdef __APPLE__
411 /* Mach-O doesn't have an init section */
412 if (constructor)
414 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
415 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
416 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", constructor );
417 fprintf( outfile, " \"\\t.text\\n\");\n" );
419 if (destructor)
421 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
422 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
423 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", destructor );
424 fprintf( outfile, " \"\\t.text\\n\");\n" );
426 #elif defined(__i386__)
427 if (constructor)
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" );
433 if (destructor)
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" );
439 #elif defined(__sparc__)
440 if (constructor)
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" );
447 if (destructor)
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" );
454 #elif defined(__powerpc__)
455 if (constructor)
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" );
461 if (destructor)
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" );
467 #elif defined(__ALPHA__)
468 if (constructor)
470 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
471 fprintf( outfile, " \"\\tjsr $26," __ASM_NAME("%s") "\\n\"\n", constructor );
472 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
474 if (destructor)
476 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
477 fprintf( outfile, " \"\\tjsr $26," __ASM_NAME("%s") "\\n\"\n", destructor );
478 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
480 #else
481 #error You need to define the DLL constructor for your architecture
482 #endif
486 /*******************************************************************
487 * BuildSpec32File
489 * Build a Win32 C file from a spec file.
491 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
493 int exports_size = 0;
494 int nr_exports, nr_imports, nr_delayed;
495 DWORD page_size;
496 const char *init_func = spec->init_func;
498 #ifdef HAVE_GETPAGESIZE
499 page_size = getpagesize();
500 #elif defined(__svr4__)
501 page_size = sysconf(_SC_PAGESIZE);
502 #elif defined(_WINDOWS)
504 SYSTEM_INFO si;
505 GetSystemInfo(&si);
506 page_size = si.dwPageSize;
508 #else
509 # error Cannot get the page size on this platform
510 #endif
512 nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
513 resolve_imports( spec );
514 exports_size = get_exports_size( spec );
515 output_standard_file_header( outfile );
517 /* Reserve some space for the PE header */
519 fprintf( outfile, "extern char __wine_spec_pe_header[];\n" );
520 fprintf( outfile, "#ifndef __GNUC__\n" );
521 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
522 fprintf( outfile, "#endif\n" );
523 fprintf( outfile, "asm(\".text\\n\\t\"\n" );
524 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
525 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP " 65536\\n\\t\"\n" );
526 fprintf( outfile, " \".data\\n\\t\"\n" );
527 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(4) );
528 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" );
529 fprintf( outfile, "#ifndef __GNUC__\n" );
530 fprintf( outfile, "}\n" );
531 fprintf( outfile, "#endif\n" );
533 #ifdef __APPLE__
534 fprintf( outfile, "static char _end[4];\n" );
535 #else
536 fprintf( outfile, "extern char _end[];\n" );
537 #endif
539 fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
541 #ifdef __i386__
542 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
543 #else
544 fprintf( outfile, "#define __stdcall\n\n" );
545 #endif
547 output_stub_funcs( outfile, spec );
548 output_export_names( outfile, spec );
550 /* Output the DLL imports */
552 nr_imports = output_imports( outfile, spec, &nr_delayed );
554 /* Output the resources */
556 output_resources( outfile, spec );
558 /* Output the entry point function */
560 fprintf( outfile, "static int __wine_spec_init_state;\n" );
561 fprintf( outfile, "extern int __wine_main_argc;\n" );
562 fprintf( outfile, "extern char **__wine_main_argv;\n" );
563 fprintf( outfile, "extern char **__wine_main_environ;\n" );
564 fprintf( outfile, "extern unsigned short **__wine_main_wargv;\n" );
565 #ifdef __APPLE__
566 fprintf( outfile, "extern _dyld_func_lookup(char *, void *);" );
567 fprintf( outfile, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
568 fprintf( outfile, "{\n" );
569 fprintf( outfile, " void (*init)(void);\n" );
570 fprintf( outfile, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
571 fprintf( outfile, " init();\n" );
572 fprintf( outfile, "}\n" );
573 fprintf( outfile, "static void __wine_spec_hidden_fini()\n" );
574 fprintf( outfile, "{\n" );
575 fprintf( outfile, " void (*fini)(void);\n" );
576 fprintf( outfile, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
577 fprintf( outfile, " fini();\n" );
578 fprintf( outfile, "}\n" );
579 fprintf( outfile, "#define _init __wine_spec_hidden_init\n" );
580 fprintf( outfile, "#define _fini __wine_spec_hidden_fini\n" );
581 #else
582 fprintf( outfile, "extern void _init(int, char**, char**);\n" );
583 fprintf( outfile, "extern void _fini();\n" );
584 #endif
586 if (spec->characteristics & IMAGE_FILE_DLL)
588 if (init_func)
589 fprintf( outfile, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func );
590 else
592 declare_weak_function( outfile, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
593 init_func = "DllMain";
595 fprintf( outfile,
596 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
597 "{\n"
598 " int ret;\n"
599 " if (reason == %d && __wine_spec_init_state == 1)\n"
600 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
601 " ret = %s ? %s( inst, reason, reserved ) : 1;\n"
602 " if (reason == %d && __wine_spec_init_state == 1)\n",
603 DLL_PROCESS_ATTACH, init_func, init_func, DLL_PROCESS_DETACH );
604 if (!nr_delayed)
605 fprintf( outfile, " _fini();\n" );
606 else
607 fprintf( outfile,
608 " {\n"
609 " extern int __stdcall FreeLibrary(void *);\n"
610 " unsigned int i;\n"
611 " _fini();\n"
612 " for (i = 0; i < sizeof(__wine_delay_imp_hmod)/sizeof(__wine_delay_imp_hmod[0]); i++)\n"
613 " if (__wine_delay_imp_hmod[i]) FreeLibrary( __wine_delay_imp_hmod[i] );\n"
614 " }\n" );
615 fprintf( outfile, " return ret;\n}\n" );
616 init_func = "__wine_dll_main";
618 else switch(spec->subsystem)
620 case IMAGE_SUBSYSTEM_NATIVE:
621 if (init_func)
622 fprintf( outfile, "extern int __stdcall %s( void*, void* );\n\n", init_func );
623 else
625 declare_weak_function( outfile, "int __stdcall", "DriverEntry", "void*, void*");
626 init_func = "DriverEntry";
628 fprintf( outfile,
629 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
630 "{\n"
631 " int ret;\n"
632 " if (__wine_spec_init_state == 1)\n"
633 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
634 " ret = %s ? %s( obj, path ) : 0;\n"
635 " if (__wine_spec_init_state == 1) _fini();\n"
636 " return ret;\n"
637 "}\n",
638 init_func, init_func );
639 init_func = "__wine_driver_entry";
640 break;
641 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
642 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
643 if (init_func)
644 fprintf( outfile, "extern int %s( int argc, char *argv[] );\n", init_func );
645 else
647 declare_weak_function( outfile, "int", "main", "int argc, char *argv[]" );
648 declare_weak_function( outfile, "int", "wmain", "int argc, unsigned short *argv[]" );
649 declare_weak_function( outfile, "int __stdcall", "WinMain", "void *,void *,char *,int" );
651 fprintf( outfile,
652 "\ntypedef struct {\n"
653 " unsigned int cb;\n"
654 " char *lpReserved, *lpDesktop, *lpTitle;\n"
655 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
656 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
657 " unsigned short wShowWindow, cbReserved2;\n"
658 " char *lpReserved2;\n"
659 " void *hStdInput, *hStdOutput, *hStdError;\n"
660 "} STARTUPINFOA;\n"
661 "extern char * __stdcall GetCommandLineA(void);\n"
662 "extern void * __stdcall GetModuleHandleA(char *);\n"
663 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
664 "extern void __stdcall ExitProcess(unsigned int);\n"
665 "static void __wine_exe_main(void)\n"
666 "{\n"
667 " int ret;\n"
668 " if (__wine_spec_init_state == 1)\n"
669 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
670 if (init_func)
671 fprintf( outfile,
672 " ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func );
673 else
674 fprintf( outfile,
675 " if (WinMain) {\n"
676 " STARTUPINFOA info;\n"
677 " char *cmdline = GetCommandLineA();\n"
678 " int bcount=0, in_quotes=0;\n"
679 " while (*cmdline) {\n"
680 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
681 " else if (*cmdline=='\\\\') bcount++;\n"
682 " else if (*cmdline=='\\\"') {\n"
683 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
684 " bcount=0;\n"
685 " }\n"
686 " else bcount=0;\n"
687 " cmdline++;\n"
688 " }\n"
689 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
690 " GetStartupInfoA( &info );\n"
691 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
692 " ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
693 " }\n"
694 " else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
695 " else ret = main( __wine_main_argc, __wine_main_argv );\n" );
696 fprintf( outfile,
697 " if (__wine_spec_init_state == 1) _fini();\n"
698 " ExitProcess( ret );\n"
699 "}\n\n" );
700 init_func = "__wine_exe_main";
701 break;
704 /* Output the NT header */
706 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
707 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
708 fprintf( outfile, " int Signature;\n" );
709 fprintf( outfile, " struct file_header {\n" );
710 fprintf( outfile, " short Machine;\n" );
711 fprintf( outfile, " short NumberOfSections;\n" );
712 fprintf( outfile, " int TimeDateStamp;\n" );
713 fprintf( outfile, " void *PointerToSymbolTable;\n" );
714 fprintf( outfile, " int NumberOfSymbols;\n" );
715 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
716 fprintf( outfile, " short Characteristics;\n" );
717 fprintf( outfile, " } FileHeader;\n" );
718 fprintf( outfile, " struct opt_header {\n" );
719 fprintf( outfile, " short Magic;\n" );
720 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
721 fprintf( outfile, " int SizeOfCode;\n" );
722 fprintf( outfile, " int SizeOfInitializedData;\n" );
723 fprintf( outfile, " int SizeOfUninitializedData;\n" );
724 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
725 fprintf( outfile, " void *BaseOfCode;\n" );
726 fprintf( outfile, " void *BaseOfData;\n" );
727 fprintf( outfile, " void *ImageBase;\n" );
728 fprintf( outfile, " int SectionAlignment;\n" );
729 fprintf( outfile, " int FileAlignment;\n" );
730 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
731 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
732 fprintf( outfile, " short MajorImageVersion;\n" );
733 fprintf( outfile, " short MinorImageVersion;\n" );
734 fprintf( outfile, " short MajorSubsystemVersion;\n" );
735 fprintf( outfile, " short MinorSubsystemVersion;\n" );
736 fprintf( outfile, " int Win32VersionValue;\n" );
737 fprintf( outfile, " void *SizeOfImage;\n" );
738 fprintf( outfile, " int SizeOfHeaders;\n" );
739 fprintf( outfile, " int CheckSum;\n" );
740 fprintf( outfile, " short Subsystem;\n" );
741 fprintf( outfile, " short DllCharacteristics;\n" );
742 fprintf( outfile, " int SizeOfStackReserve;\n" );
743 fprintf( outfile, " int SizeOfStackCommit;\n" );
744 fprintf( outfile, " int SizeOfHeapReserve;\n" );
745 fprintf( outfile, " int SizeOfHeapCommit;\n" );
746 fprintf( outfile, " int LoaderFlags;\n" );
747 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
748 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
749 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
750 fprintf( outfile, " } OptionalHeader;\n" );
751 fprintf( outfile, "} nt_header = {\n" );
752 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
753 #ifdef __i386__
754 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
755 #elif defined(__powerpc__)
756 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC ); /* Machine */
757 #elif defined(__ALPHA__)
758 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_ALPHA ); /* Machine */
759 #else
760 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN ); /* Machine */
761 #endif
762 fprintf( outfile, " 0, 0, 0, 0,\n" );
763 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
764 fprintf( outfile, " 0x%04x },\n", spec->characteristics ); /* Characteristics */
766 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
767 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
768 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
769 fprintf( outfile, " %s,\n", init_func ); /* AddressOfEntryPoint */
770 fprintf( outfile, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
771 fprintf( outfile, " __wine_spec_pe_header,\n" ); /* ImageBase */
772 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
773 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
774 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
775 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
776 fprintf( outfile, " %d,\n", spec->subsystem_major ); /* MajorSubsystemVersion */
777 fprintf( outfile, " %d,\n", spec->subsystem_minor ); /* MinorSubsystemVersion */
778 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
779 fprintf( outfile, " _end,\n" ); /* SizeOfImage */
780 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
781 fprintf( outfile, " 0,\n" ); /* CheckSum */
782 fprintf( outfile, " 0x%04x,\n", spec->subsystem );/* Subsystem */
783 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
784 fprintf( outfile, " %d, %ld,\n", /* SizeOfStackReserve/Commit */
785 (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
786 fprintf( outfile, " %d, %ld,\n", /* SizeOfHeapReserve/Commit */
787 (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
788 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
789 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
790 fprintf( outfile, " {\n" );
791 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
792 exports_size ? "__wine_spec_exports" : "0", exports_size );
793 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
794 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
795 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
796 spec->nb_resources ? "&__wine_spec_resources" : "0",
797 spec->nb_resources ? "sizeof(__wine_spec_resources)" : "0" );
798 fprintf( outfile, " }\n }\n};\n\n" );
800 /* Output the DLL constructor */
802 fprintf( outfile,
803 "void __wine_spec_init(void)\n"
804 "{\n"
805 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
806 " __wine_spec_init_state = 1;\n"
807 " __wine_dll_register( &nt_header, \"%s\" );\n"
808 "}\n\n",
809 spec->file_name );
811 fprintf( outfile,
812 "void __wine_spec_init_ctor(void)\n"
813 "{\n"
814 " if (__wine_spec_init_state) return;\n"
815 " __wine_spec_init();\n"
816 " __wine_spec_init_state = 2;\n"
817 "}\n" );
819 fprintf( outfile, "#ifndef __GNUC__\n" );
820 fprintf( outfile, "static void __asm__dummy(void) {\n" );
821 fprintf( outfile, "#endif\n" );
823 output_exports( outfile, spec );
824 output_import_thunks( outfile, spec );
825 output_dll_init( outfile, "__wine_spec_init_ctor", NULL );
827 fprintf( outfile, "#ifndef __GNUC__\n" );
828 fprintf( outfile, "}\n" );
829 fprintf( outfile, "#endif\n" );
833 /*******************************************************************
834 * BuildDef32File
836 * Build a Win32 def file from a spec file.
838 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
840 const char *name;
841 int i;
843 if (spec_file_name)
844 fprintf( outfile, "; File generated automatically from %s; do not edit!\n\n",
845 spec_file_name );
846 else
847 fprintf( outfile, "; File generated automatically; do not edit!\n\n" );
849 fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
851 fprintf(outfile, "EXPORTS\n");
853 /* Output the exports and relay entry points */
855 for(i = 0; i < spec->nb_entry_points; i++)
857 const ORDDEF *odp = &spec->entry_points[i];
858 int is_data = 0;
860 if (!odp) continue;
861 if (odp->type == TYPE_STUB) continue;
863 if (odp->name) name = odp->name;
864 else if (odp->export_name) name = odp->export_name;
865 else continue;
867 fprintf(outfile, " %s", name);
869 switch(odp->type)
871 case TYPE_EXTERN:
872 is_data = 1;
873 /* fall through */
874 case TYPE_VARARGS:
875 case TYPE_CDECL:
876 /* try to reduce output */
877 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
878 fprintf(outfile, "=%s", odp->link_name);
879 break;
880 case TYPE_STDCALL:
882 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
883 if (!kill_at) fprintf(outfile, "@%d", at_param);
884 if (odp->flags & FLAG_FORWARD)
886 fprintf(outfile, "=%s", odp->link_name);
888 else if (strcmp(name, odp->link_name)) /* try to reduce output */
890 fprintf(outfile, "=%s", odp->link_name);
891 if (!kill_at) fprintf(outfile, "@%d", at_param);
893 break;
895 default:
896 assert(0);
898 fprintf( outfile, " @%d", odp->ordinal );
899 if (!odp->name) fprintf( outfile, " NONAME" );
900 if (is_data) fprintf( outfile, " DATA" );
901 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
902 fprintf( outfile, "\n" );
907 /*******************************************************************
908 * BuildDebugFile
910 * Build the debugging channels source file.
912 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
914 int nr_debug;
915 char *prefix, *p, *constructor, *destructor;
917 while (*argv)
919 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
922 output_standard_file_header( outfile );
923 nr_debug = output_debug( outfile );
924 if (!nr_debug)
926 fprintf( outfile, "/* no debug channels found for this module */\n" );
927 return;
930 if (output_file_name)
932 if ((p = strrchr( output_file_name, '/' ))) p++;
933 prefix = xstrdup( p ? p : output_file_name );
934 if ((p = strchr( prefix, '.' ))) *p = 0;
935 strcpy( p, make_c_identifier(p) );
937 else prefix = xstrdup( "_" );
939 /* Output the DLL constructor */
941 constructor = xmalloc( strlen(prefix) + 17 );
942 destructor = xmalloc( strlen(prefix) + 17 );
943 sprintf( constructor, "__wine_dbg_%s_init", prefix );
944 sprintf( destructor, "__wine_dbg_%s_fini", prefix );
945 fprintf( outfile,
946 "#ifdef __GNUC__\n"
947 "void %s(void) __attribute__((constructor));\n"
948 "void %s(void) __attribute__((destructor));\n"
949 "#else\n"
950 "static void __asm__dummy_dll_init(void) {\n",
951 constructor, destructor );
952 output_dll_init( outfile, constructor, destructor );
953 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
955 fprintf( outfile,
956 "void %s(void)\n"
957 "{\n"
958 " extern void *__wine_dbg_register( char * const *, int );\n"
959 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
960 "}\n\n", constructor, nr_debug );
961 fprintf( outfile,
962 "void %s(void)\n"
963 "{\n"
964 " extern void __wine_dbg_unregister( void* );\n"
965 " __wine_dbg_unregister( debug_registration );\n"
966 "}\n", destructor );
968 free( constructor );
969 free( destructor );
970 free( prefix );