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
26 #include "wine/port.h"
35 #include "wine/exception.h"
39 static int string_compare( const void *ptr1, const void *ptr2 )
41 const char * const *str1 = ptr1;
42 const char * const *str2 = ptr2;
43 return strcmp( *str1, *str2 );
47 /*******************************************************************
50 * Generate an internal name for an entry point. Used for stubs etc.
52 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
54 static char buffer[256];
55 if (odp->name || odp->export_name)
58 sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->file_name,
59 odp->name ? odp->name : odp->export_name );
60 /* make sure name is a legal C identifier */
61 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
62 if (!*p) return buffer;
64 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
68 /*******************************************************************
69 * declare_weak_function
71 * Output a prototype for a weak function.
73 static void declare_weak_function( FILE *outfile, const char *ret_type, const char *name, const char *params)
75 fprintf( outfile, "#ifdef __GNUC__\n" );
76 if (target_platform == PLATFORM_APPLE)
78 fprintf( outfile, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type, name, params );
79 fprintf( outfile, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type, name, params, name );
80 fprintf( outfile, "#define %s __wine_spec_weak_%s\n", name, name );
81 fprintf( outfile, "asm(\".weak_reference %s\");\n", asm_name(name) );
83 else fprintf( outfile, "extern %s %s(%s) __attribute__((weak));\n", ret_type, name, params );
85 fprintf( outfile, "#else\n" );
86 fprintf( outfile, "extern %s %s(%s);\n", ret_type, name, params );
87 fprintf( outfile, "static void __asm__dummy_%s(void)", name );
88 fprintf( outfile, " { asm(\".weak %s\"); }\n", asm_name(name) );
89 fprintf( outfile, "#endif\n\n" );
93 /*******************************************************************
96 * Output the debug channels.
98 static int output_debug( FILE *outfile )
102 if (!nb_debug_channels) return 0;
103 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
105 for (i = 0; i < nb_debug_channels; i++)
106 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
107 debug_channels[i], debug_channels[i] );
109 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
110 for (i = 0; i < nb_debug_channels; i++)
112 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
113 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
115 fprintf( outfile, "\n};\n\n" );
116 fprintf( outfile, "static void *debug_registration;\n\n" );
118 return nb_debug_channels;
122 /*******************************************************************
125 * Compute the size of the export table.
127 static int get_exports_size( DLLSPEC *spec )
129 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
130 int i, fwd_size = 0, total_size;
132 if (!nr_exports) return 0;
134 /* export directory header */
135 total_size = 10 * sizeof(int);
137 /* function pointers */
138 total_size += nr_exports * sizeof(int);
140 /* function name pointers */
141 total_size += spec->nb_names * sizeof(int);
143 /* function ordinals */
144 total_size += spec->nb_names * sizeof(short);
145 if (spec->nb_names % 2) total_size += sizeof(short);
147 /* forward strings */
148 for (i = spec->base; i <= spec->limit; i++)
150 ORDDEF *odp = spec->ordinals[i];
151 if (odp && odp->flags & FLAG_FORWARD) fwd_size += strlen(odp->link_name) + 1;
153 total_size += (fwd_size + 3) & ~3;
159 /*******************************************************************
160 * output_export_names
162 * Output all the exported names for a Win32 module.
164 static void output_export_names( FILE *outfile, DLLSPEC *spec )
166 int i, nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
168 if (!nr_exports) return;
170 fprintf( outfile, "\nconst char __wine_spec_exp_names[] =" );
171 fprintf( outfile, "\n \"%s\\0\"", spec->file_name );
172 for (i = 0; i < spec->nb_names; i++)
173 fprintf( outfile, "\n \"%s\\0\"", spec->names[i]->name );
174 fprintf( outfile, ";\n" );
178 /*******************************************************************
181 * Output the export table for a Win32 module.
183 static void output_exports( FILE *outfile, DLLSPEC *spec )
186 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
188 if (!nr_exports) return;
190 fprintf( outfile, "/* export table */\n" );
191 fprintf( outfile, "asm(\".data\\n\"\n" );
192 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
193 fprintf( outfile, " \"%s:\\n\"\n", asm_name("__wine_spec_exports") );
195 /* export directory header */
197 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
198 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
199 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
200 fprintf( outfile, " \"\\t.long %s\\n\"\n", asm_name("__wine_spec_exp_names") ); /* Name */
201 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->base ); /* Base */
202 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
203 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->nb_names ); /* NumberOfNames */
204 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
207 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
208 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
212 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
213 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
216 /* output the function pointers */
218 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
219 for (i = spec->base; i <= spec->limit; i++)
221 ORDDEF *odp = spec->ordinals[i];
222 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
223 else switch(odp->type)
229 if (!(odp->flags & FLAG_FORWARD))
231 fprintf( outfile, " \"\\t.long %s\\n\"\n", asm_name(odp->link_name) );
235 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
236 fwd_size += strlen(odp->link_name) + 1;
240 fprintf( outfile, " \"\\t.long %s\\n\"\n",
241 asm_name( make_internal_name( odp, spec, "stub" )) );
250 /* output the function name pointers */
252 int namepos = strlen(spec->file_name) + 1;
254 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
255 for (i = 0; i < spec->nb_names; i++)
257 fprintf( outfile, " \"\\t.long %s+%d\\n\"\n", asm_name("__wine_spec_exp_names"), namepos );
258 namepos += strlen(spec->names[i]->name) + 1;
264 /* output the function ordinals */
266 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
267 for (i = 0; i < spec->nb_names; i++)
269 fprintf( outfile, " \"\\t%s %d\\n\"\n",
270 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
272 if (spec->nb_names % 2)
274 fprintf( outfile, " \"\\t%s 0\\n\"\n", get_asm_short_keyword() );
278 /* output forward strings */
282 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
283 for (i = spec->base; i <= spec->limit; i++)
285 ORDDEF *odp = spec->ordinals[i];
286 if (odp && (odp->flags & FLAG_FORWARD))
287 fprintf( outfile, " \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), odp->link_name );
289 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
296 for (i = spec->base; i <= spec->limit; i++)
298 ORDDEF *odp = spec->ordinals[i];
299 unsigned int j, args, mask = 0;
301 /* skip nonexistent entry points */
302 if (!odp) goto ignore;
303 /* skip non-functions */
304 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
305 /* skip norelay and forward entry points */
306 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
308 for (j = 0; odp->u.func.arg_types[j]; j++)
310 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
311 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
313 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
315 args = strlen(odp->u.func.arg_types) * sizeof(int);
320 fprintf( outfile, " \"\\tjmp %s\\n\"\n", asm_name(odp->link_name) );
321 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
322 fprintf( outfile, " \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp->link_name), mask );
325 fprintf( outfile, " \"\\tjmp %s\\n\"\n", asm_name(odp->link_name) );
326 fprintf( outfile, " \"\\tret\\n\"\n" );
327 fprintf( outfile, " \"\\t%s %d\\n\"\n", get_asm_short_keyword(), args );
328 fprintf( outfile, " \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp->link_name), mask );
336 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
339 fprintf( outfile, ");\n" );
343 /*******************************************************************
346 * Output the functions for stub entry points
348 static void output_stub_funcs( FILE *outfile, DLLSPEC *spec )
352 for (i = 0; i < spec->nb_entry_points; i++)
354 ORDDEF *odp = &spec->entry_points[i];
355 if (odp->type != TYPE_STUB) continue;
356 fprintf( outfile, "#ifdef __GNUC__\n" );
357 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
358 fprintf( outfile, "#endif\n\n" );
359 fprintf( outfile, "struct exc_record {\n" );
360 fprintf( outfile, " unsigned int code, flags;\n" );
361 fprintf( outfile, " void *rec, *addr;\n" );
362 fprintf( outfile, " unsigned int params;\n" );
363 fprintf( outfile, " const void *info[15];\n" );
364 fprintf( outfile, "};\n\n" );
365 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
366 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
367 fprintf( outfile, " struct exc_record rec;\n" );
368 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
369 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
370 fprintf( outfile, " rec.rec = 0;\n" );
371 fprintf( outfile, " rec.params = 2;\n" );
372 fprintf( outfile, " rec.info[0] = \"%s\";\n", spec->file_name );
373 fprintf( outfile, " rec.info[1] = func;\n" );
374 fprintf( outfile, "#ifdef __GNUC__\n" );
375 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
376 fprintf( outfile, "#else\n" );
377 fprintf( outfile, " rec.addr = 0;\n" );
378 fprintf( outfile, "#endif\n" );
379 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
383 for (i = 0; i < spec->nb_entry_points; i++)
385 const ORDDEF *odp = &spec->entry_points[i];
386 if (odp->type != TYPE_STUB) continue;
387 fprintf( outfile, "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
389 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
390 else if (odp->export_name)
391 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
393 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
398 /*******************************************************************
401 * Output code for calling a dll constructor and destructor.
403 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
405 if (target_platform == PLATFORM_APPLE)
407 /* Mach-O doesn't have an init section */
410 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
411 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
412 fprintf( outfile, " \"\\t.long %s\\n\"\n", asm_name(constructor) );
413 fprintf( outfile, " \"\\t.text\\n\");\n" );
417 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
418 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
419 fprintf( outfile, " \"\\t.long %s\\n\"\n", asm_name(destructor) );
420 fprintf( outfile, " \"\\t.text\\n\");\n" );
423 else switch(target_cpu)
428 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
429 fprintf( outfile, " \"\\tcall %s\\n\"\n", asm_name(constructor) );
430 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
434 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
435 fprintf( outfile, " \"\\tcall %s\\n\"\n", asm_name(destructor) );
436 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
442 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
443 fprintf( outfile, " \"\\tcall %s\\n\"\n", asm_name(constructor) );
444 fprintf( outfile, " \"\\tnop\\n\"\n" );
445 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
449 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
450 fprintf( outfile, " \"\\tcall %s\\n\"\n", asm_name(destructor) );
451 fprintf( outfile, " \"\\tnop\\n\"\n" );
452 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
458 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
459 fprintf( outfile, " \"\\tjsr $26,%s\\n\"\n", asm_name(constructor) );
460 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
464 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
465 fprintf( outfile, " \"\\tjsr $26,%s\\n\"\n", asm_name(destructor) );
466 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
472 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
473 fprintf( outfile, " \"\\tbl %s\\n\"\n", asm_name(constructor) );
474 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
478 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
479 fprintf( outfile, " \"\\tbl %s\\n\"\n", asm_name(destructor) );
480 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
487 /*******************************************************************
490 * Build a Win32 C file from a spec file.
492 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
494 int exports_size = 0;
495 int nr_exports, nr_imports, nr_delayed;
496 unsigned int page_size = get_page_size();
497 const char *init_func = spec->init_func;
499 nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
500 resolve_imports( spec );
501 exports_size = get_exports_size( spec );
502 output_standard_file_header( outfile );
504 /* Reserve some space for the PE header */
506 fprintf( outfile, "extern char __wine_spec_pe_header[];\n" );
507 fprintf( outfile, "#ifndef __GNUC__\n" );
508 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
509 fprintf( outfile, "#endif\n" );
510 fprintf( outfile, "asm(\".text\\n\\t\"\n" );
511 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
512 fprintf( outfile, " \"%s:\\t\"\n", asm_name("__wine_spec_pe_header") );
513 if (target_platform == PLATFORM_APPLE)
514 fprintf( outfile, " \".space 65536\\n\\t\"\n" );
516 fprintf( outfile, " \".skip 65536\\n\\t\"\n" );
517 fprintf( outfile, " \".data\\n\\t\"\n" );
518 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(4) );
519 fprintf( outfile, " \"%s:\\t.long 1\");\n", asm_name("__wine_spec_data_start") );
520 fprintf( outfile, "#ifndef __GNUC__\n" );
521 fprintf( outfile, "}\n" );
522 fprintf( outfile, "#endif\n" );
524 if (target_platform == PLATFORM_APPLE)
525 fprintf( outfile, "static char _end[4];\n" );
527 fprintf( outfile, "extern char _end[];\n" );
529 fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
531 if (target_cpu == CPU_x86)
532 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
534 fprintf( outfile, "#define __stdcall\n\n" );
536 output_stub_funcs( outfile, spec );
537 output_export_names( outfile, spec );
539 /* Output the DLL imports */
541 nr_imports = output_imports( outfile, spec, &nr_delayed );
543 /* Output the resources */
545 output_resources( outfile, spec );
547 /* Output the entry point function */
549 fprintf( outfile, "static int __wine_spec_init_state;\n" );
550 fprintf( outfile, "extern int __wine_main_argc;\n" );
551 fprintf( outfile, "extern char **__wine_main_argv;\n" );
552 fprintf( outfile, "extern char **__wine_main_environ;\n" );
553 fprintf( outfile, "extern unsigned short **__wine_main_wargv;\n" );
554 if (target_platform == PLATFORM_APPLE)
556 fprintf( outfile, "extern _dyld_func_lookup(char *, void *);" );
557 fprintf( outfile, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
558 fprintf( outfile, "{\n" );
559 fprintf( outfile, " void (*init)(void);\n" );
560 fprintf( outfile, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
561 fprintf( outfile, " init();\n" );
562 fprintf( outfile, "}\n" );
563 fprintf( outfile, "static void __wine_spec_hidden_fini()\n" );
564 fprintf( outfile, "{\n" );
565 fprintf( outfile, " void (*fini)(void);\n" );
566 fprintf( outfile, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
567 fprintf( outfile, " fini();\n" );
568 fprintf( outfile, "}\n" );
569 fprintf( outfile, "#define _init __wine_spec_hidden_init\n" );
570 fprintf( outfile, "#define _fini __wine_spec_hidden_fini\n" );
574 fprintf( outfile, "extern void _init(int, char**, char**);\n" );
575 fprintf( outfile, "extern void _fini();\n" );
578 if (spec->characteristics & IMAGE_FILE_DLL)
581 fprintf( outfile, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func );
584 declare_weak_function( outfile, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
585 init_func = "DllMain";
588 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
591 " if (reason == %d && __wine_spec_init_state == 1)\n"
592 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
593 " ret = %s ? %s( inst, reason, reserved ) : 1;\n"
594 " if (reason == %d && __wine_spec_init_state == 1)\n",
595 DLL_PROCESS_ATTACH, init_func, init_func, DLL_PROCESS_DETACH );
597 fprintf( outfile, " _fini();\n" );
601 " extern int __stdcall FreeLibrary(void *);\n"
604 " for (i = 0; i < sizeof(__wine_delay_imp_hmod)/sizeof(__wine_delay_imp_hmod[0]); i++)\n"
605 " if (__wine_delay_imp_hmod[i]) FreeLibrary( __wine_delay_imp_hmod[i] );\n"
607 fprintf( outfile, " return ret;\n}\n" );
608 init_func = "__wine_dll_main";
610 else switch(spec->subsystem)
612 case IMAGE_SUBSYSTEM_NATIVE:
614 fprintf( outfile, "extern int __stdcall %s( void*, void* );\n\n", init_func );
617 declare_weak_function( outfile, "int __stdcall", "DriverEntry", "void*, void*");
618 init_func = "DriverEntry";
621 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
624 " if (__wine_spec_init_state == 1)\n"
625 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
626 " ret = %s ? %s( obj, path ) : 0;\n"
627 " if (__wine_spec_init_state == 1) _fini();\n"
630 init_func, init_func );
631 init_func = "__wine_driver_entry";
633 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
634 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
636 fprintf( outfile, "extern int %s( int argc, char *argv[] );\n", init_func );
639 declare_weak_function( outfile, "int", "main", "int argc, char *argv[]" );
640 declare_weak_function( outfile, "int", "wmain", "int argc, unsigned short *argv[]" );
641 declare_weak_function( outfile, "int __stdcall", "WinMain", "void *,void *,char *,int" );
644 "\ntypedef struct {\n"
645 " unsigned int cb;\n"
646 " char *lpReserved, *lpDesktop, *lpTitle;\n"
647 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
648 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
649 " unsigned short wShowWindow, cbReserved2;\n"
650 " char *lpReserved2;\n"
651 " void *hStdInput, *hStdOutput, *hStdError;\n"
653 "extern char * __stdcall GetCommandLineA(void);\n"
654 "extern void * __stdcall GetModuleHandleA(char *);\n"
655 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
656 "extern void __stdcall ExitProcess(unsigned int);\n"
657 "static void __wine_exe_main(void)\n"
660 " if (__wine_spec_init_state == 1)\n"
661 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
664 " ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func );
668 " STARTUPINFOA info;\n"
669 " char *cmdline = GetCommandLineA();\n"
670 " int bcount=0, in_quotes=0;\n"
671 " while (*cmdline) {\n"
672 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
673 " else if (*cmdline=='\\\\') bcount++;\n"
674 " else if (*cmdline=='\\\"') {\n"
675 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
681 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
682 " GetStartupInfoA( &info );\n"
683 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
684 " ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
686 " else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
687 " else ret = main( __wine_main_argc, __wine_main_argv );\n" );
689 " if (__wine_spec_init_state == 1) _fini();\n"
690 " ExitProcess( ret );\n"
692 init_func = "__wine_exe_main";
696 /* Output the NT header */
698 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
699 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
700 fprintf( outfile, " int Signature;\n" );
701 fprintf( outfile, " struct file_header {\n" );
702 fprintf( outfile, " short Machine;\n" );
703 fprintf( outfile, " short NumberOfSections;\n" );
704 fprintf( outfile, " int TimeDateStamp;\n" );
705 fprintf( outfile, " void *PointerToSymbolTable;\n" );
706 fprintf( outfile, " int NumberOfSymbols;\n" );
707 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
708 fprintf( outfile, " short Characteristics;\n" );
709 fprintf( outfile, " } FileHeader;\n" );
710 fprintf( outfile, " struct opt_header {\n" );
711 fprintf( outfile, " short Magic;\n" );
712 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
713 fprintf( outfile, " int SizeOfCode;\n" );
714 fprintf( outfile, " int SizeOfInitializedData;\n" );
715 fprintf( outfile, " int SizeOfUninitializedData;\n" );
716 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
717 fprintf( outfile, " void *BaseOfCode;\n" );
718 fprintf( outfile, " void *BaseOfData;\n" );
719 fprintf( outfile, " void *ImageBase;\n" );
720 fprintf( outfile, " int SectionAlignment;\n" );
721 fprintf( outfile, " int FileAlignment;\n" );
722 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
723 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
724 fprintf( outfile, " short MajorImageVersion;\n" );
725 fprintf( outfile, " short MinorImageVersion;\n" );
726 fprintf( outfile, " short MajorSubsystemVersion;\n" );
727 fprintf( outfile, " short MinorSubsystemVersion;\n" );
728 fprintf( outfile, " int Win32VersionValue;\n" );
729 fprintf( outfile, " void *SizeOfImage;\n" );
730 fprintf( outfile, " int SizeOfHeaders;\n" );
731 fprintf( outfile, " int CheckSum;\n" );
732 fprintf( outfile, " short Subsystem;\n" );
733 fprintf( outfile, " short DllCharacteristics;\n" );
734 fprintf( outfile, " int SizeOfStackReserve;\n" );
735 fprintf( outfile, " int SizeOfStackCommit;\n" );
736 fprintf( outfile, " int SizeOfHeapReserve;\n" );
737 fprintf( outfile, " int SizeOfHeapCommit;\n" );
738 fprintf( outfile, " int LoaderFlags;\n" );
739 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
740 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
741 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
742 fprintf( outfile, " } OptionalHeader;\n" );
743 fprintf( outfile, "} nt_header = {\n" );
744 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
748 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
751 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC ); /* Machine */
754 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_ALPHA ); /* Machine */
757 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN ); /* Machine */
760 fprintf( outfile, " 0, 0, 0, 0,\n" );
761 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
762 fprintf( outfile, " 0x%04x },\n", spec->characteristics ); /* Characteristics */
764 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
765 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
766 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
767 fprintf( outfile, " %s,\n", init_func ); /* AddressOfEntryPoint */
768 fprintf( outfile, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
769 fprintf( outfile, " __wine_spec_pe_header,\n" ); /* ImageBase */
770 fprintf( outfile, " %u,\n", page_size ); /* SectionAlignment */
771 fprintf( outfile, " %u,\n", page_size ); /* FileAlignment */
772 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
773 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
774 fprintf( outfile, " %d,\n", spec->subsystem_major ); /* MajorSubsystemVersion */
775 fprintf( outfile, " %d,\n", spec->subsystem_minor ); /* MinorSubsystemVersion */
776 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
777 fprintf( outfile, " _end,\n" ); /* SizeOfImage */
778 fprintf( outfile, " %u,\n", page_size ); /* SizeOfHeaders */
779 fprintf( outfile, " 0,\n" ); /* CheckSum */
780 fprintf( outfile, " 0x%04x,\n", spec->subsystem );/* Subsystem */
781 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
782 fprintf( outfile, " %u, %u,\n", /* SizeOfStackReserve/Commit */
783 (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
784 fprintf( outfile, " %u, %u,\n", /* SizeOfHeapReserve/Commit */
785 (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
786 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
787 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
788 fprintf( outfile, " {\n" );
789 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
790 exports_size ? "__wine_spec_exports" : "0", exports_size );
791 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
792 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
793 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
794 spec->nb_resources ? "&__wine_spec_resources" : "0",
795 spec->nb_resources ? "sizeof(__wine_spec_resources)" : "0" );
796 fprintf( outfile, " }\n }\n};\n\n" );
798 /* Output the DLL constructor */
801 "void __wine_spec_init(void)\n"
803 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
804 " __wine_spec_init_state = 1;\n"
805 " __wine_dll_register( &nt_header, \"%s\" );\n"
810 "void __wine_spec_init_ctor(void)\n"
812 " if (__wine_spec_init_state) return;\n"
813 " __wine_spec_init();\n"
814 " __wine_spec_init_state = 2;\n"
817 fprintf( outfile, "#ifndef __GNUC__\n" );
818 fprintf( outfile, "static void __asm__dummy(void) {\n" );
819 fprintf( outfile, "#endif\n" );
821 output_exports( outfile, spec );
822 output_import_thunks( outfile, spec );
823 output_dll_init( outfile, "__wine_spec_init_ctor", NULL );
825 fprintf( outfile, "#ifndef __GNUC__\n" );
826 fprintf( outfile, "}\n" );
827 fprintf( outfile, "#endif\n" );
831 /*******************************************************************
834 * Build a Win32 def file from a spec file.
836 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
842 fprintf( outfile, "; File generated automatically from %s; do not edit!\n\n",
845 fprintf( outfile, "; File generated automatically; do not edit!\n\n" );
847 fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
849 fprintf(outfile, "EXPORTS\n");
851 /* Output the exports and relay entry points */
853 for(i = 0; i < spec->nb_entry_points; i++)
855 const ORDDEF *odp = &spec->entry_points[i];
859 if (odp->type == TYPE_STUB) continue;
861 if (odp->name) name = odp->name;
862 else if (odp->export_name) name = odp->export_name;
865 fprintf(outfile, " %s", name);
874 /* try to reduce output */
875 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
876 fprintf(outfile, "=%s", odp->link_name);
880 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
881 if (!kill_at) fprintf(outfile, "@%d", at_param);
882 if (odp->flags & FLAG_FORWARD)
884 fprintf(outfile, "=%s", odp->link_name);
886 else if (strcmp(name, odp->link_name)) /* try to reduce output */
888 fprintf(outfile, "=%s", odp->link_name);
889 if (!kill_at) fprintf(outfile, "@%d", at_param);
896 fprintf( outfile, " @%d", odp->ordinal );
897 if (!odp->name) fprintf( outfile, " NONAME" );
898 if (is_data) fprintf( outfile, " DATA" );
899 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
900 fprintf( outfile, "\n" );
905 /*******************************************************************
908 * Build the debugging channels source file.
910 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
913 char *prefix, *p, *constructor, *destructor;
917 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
920 output_standard_file_header( outfile );
921 nr_debug = output_debug( outfile );
924 fprintf( outfile, "/* no debug channels found for this module */\n" );
928 if (output_file_name)
930 if ((p = strrchr( output_file_name, '/' ))) p++;
931 prefix = xstrdup( p ? p : output_file_name );
932 if ((p = strchr( prefix, '.' ))) *p = 0;
933 strcpy( p, make_c_identifier(p) );
935 else prefix = xstrdup( "_" );
937 /* Output the DLL constructor */
939 constructor = xmalloc( strlen(prefix) + 17 );
940 destructor = xmalloc( strlen(prefix) + 17 );
941 sprintf( constructor, "__wine_dbg_%s_init", prefix );
942 sprintf( destructor, "__wine_dbg_%s_fini", prefix );
945 "void %s(void) __attribute__((constructor));\n"
946 "void %s(void) __attribute__((destructor));\n"
948 "static void __asm__dummy_dll_init(void) {\n",
949 constructor, destructor );
950 output_dll_init( outfile, constructor, destructor );
951 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
956 " extern void *__wine_dbg_register( char * const *, int );\n"
957 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
958 "}\n\n", constructor, nr_debug );
962 " extern void __wine_dbg_unregister( void* );\n"
963 " __wine_dbg_unregister( debug_registration );\n"