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"
40 # define __ASM_SKIP ".space"
42 # define __ASM_SKIP ".skip"
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 /*******************************************************************
56 * Generate an internal name for an entry point. Used for stubs etc.
58 static const char *make_internal_name( const ORDDEF *odp, const char *prefix )
60 static char buffer[256];
61 if (odp->name || odp->export_name)
64 sprintf( buffer, "__wine_%s_%s_%s", prefix, dll_file_name,
65 odp->name ? odp->name : odp->export_name );
66 /* make sure name is a legal C identifier */
67 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
68 if (!*p) return buffer;
70 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(dll_file_name), odp->ordinal );
74 /*******************************************************************
77 * Assign ordinals to all entry points.
79 static void AssignOrdinals(void)
83 if ( !nb_names ) return;
85 /* start assigning from Base, or from 1 if no ordinal defined yet */
86 if (Base == MAX_ORDINALS) Base = 1;
87 for (i = 0, ordinal = Base; i < nb_names; i++)
89 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
90 while (Ordinals[ordinal]) ordinal++;
91 if (ordinal >= MAX_ORDINALS)
93 current_line = Names[i]->lineno;
94 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
96 Names[i]->ordinal = ordinal;
97 Ordinals[ordinal] = Names[i];
99 if (ordinal > Limit) Limit = ordinal;
103 /*******************************************************************
106 * Output the debug channels.
108 static int output_debug( FILE *outfile )
112 if (!nb_debug_channels) return 0;
113 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
115 for (i = 0; i < nb_debug_channels; i++)
116 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
117 debug_channels[i], debug_channels[i] );
119 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
120 for (i = 0; i < nb_debug_channels; i++)
122 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
123 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
125 fprintf( outfile, "\n};\n\n" );
126 fprintf( outfile, "static void *debug_registration;\n\n" );
128 return nb_debug_channels;
132 /*******************************************************************
135 * Output the export table for a Win32 module.
137 static int output_exports( FILE *outfile, int nr_exports )
139 int i, fwd_size = 0, total_size = 0;
141 if (!nr_exports) return 0;
143 fprintf( outfile, "asm(\".data\\n\"\n" );
144 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
145 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
147 /* export directory header */
149 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
150 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
151 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
152 fprintf( outfile, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
153 fprintf( outfile, " \"\\t.long %d\\n\"\n", Base ); /* Base */
154 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
155 fprintf( outfile, " \"\\t.long %d\\n\"\n", nb_names ); /* NumberOfNames */
156 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
159 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
160 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
164 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
165 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
167 total_size += 10 * sizeof(int);
169 /* output the function pointers */
171 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
172 for (i = Base; i <= Limit; i++)
174 ORDDEF *odp = Ordinals[i];
175 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
176 else switch(odp->type)
182 if (!(odp->flags & FLAG_FORWARD))
184 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
185 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
189 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
190 fwd_size += strlen(odp->link_name) + 1;
194 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
200 total_size += (Limit - Base + 1) * sizeof(int);
204 /* output the function name pointers */
206 int namepos = strlen(dll_file_name) + 1;
208 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
209 for (i = 0; i < nb_names; i++)
211 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
212 namepos += strlen(Names[i]->name) + 1;
214 total_size += nb_names * sizeof(int);
217 /* output the function names */
219 fprintf( outfile, " \"\\t.text\\n\"\n" );
220 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
221 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", dll_file_name );
222 for (i = 0; i < nb_names; i++)
223 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
224 fprintf( outfile, " \"\\t.data\\n\"\n" );
228 /* output the function ordinals */
230 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
231 for (i = 0; i < nb_names; i++)
233 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", Names[i]->ordinal - Base );
235 total_size += nb_names * sizeof(short);
238 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
239 total_size += sizeof(short);
243 /* output forward strings */
247 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
248 for (i = Base; i <= Limit; i++)
250 ORDDEF *odp = Ordinals[i];
251 if (odp && (odp->flags & FLAG_FORWARD))
252 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
254 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
255 total_size += (fwd_size + 3) & ~3;
262 for (i = Base; i <= Limit; i++)
264 ORDDEF *odp = Ordinals[i];
265 unsigned int j, args, mask = 0;
268 /* skip non-existent entry points */
269 if (!odp) goto ignore;
270 /* skip non-functions */
271 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
272 /* skip norelay and forward entry points */
273 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
275 for (j = 0; odp->u.func.arg_types[j]; j++)
277 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
278 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
280 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
282 name = odp->link_name;
283 args = strlen(odp->u.func.arg_types) * sizeof(int);
284 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, "regs" );
289 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
290 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
291 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
294 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
295 fprintf( outfile, " \"\\tret\\n\"\n" );
296 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
297 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
305 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
309 fprintf( outfile, " \"\\t.text\\n\"\n" );
310 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
311 fprintf( outfile, ");\n\n" );
317 /*******************************************************************
320 * Output the functions for stub entry points
322 static void output_stub_funcs( FILE *outfile )
326 for (i = 0; i < nb_entry_points; i++)
328 ORDDEF *odp = EntryPoints[i];
329 if (odp->type != TYPE_STUB) continue;
330 fprintf( outfile, "#ifdef __GNUC__\n" );
331 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
332 fprintf( outfile, "#endif\n\n" );
333 fprintf( outfile, "struct exc_record {\n" );
334 fprintf( outfile, " unsigned int code, flags;\n" );
335 fprintf( outfile, " void *rec, *addr;\n" );
336 fprintf( outfile, " unsigned int params;\n" );
337 fprintf( outfile, " const void *info[15];\n" );
338 fprintf( outfile, "};\n\n" );
339 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
340 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
341 fprintf( outfile, " struct exc_record rec;\n" );
342 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
343 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
344 fprintf( outfile, " rec.rec = 0;\n" );
345 fprintf( outfile, " rec.params = 2;\n" );
346 fprintf( outfile, " rec.info[0] = \"%s\";\n", dll_file_name );
347 fprintf( outfile, " rec.info[1] = func;\n" );
348 fprintf( outfile, "#ifdef __GNUC__\n" );
349 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
350 fprintf( outfile, "#else\n" );
351 fprintf( outfile, " rec.addr = 0;\n" );
352 fprintf( outfile, "#endif\n" );
353 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
357 for (i = 0; i < nb_entry_points; i++)
359 ORDDEF *odp = EntryPoints[i];
360 if (odp->type != TYPE_STUB) continue;
361 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
363 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
364 else if (odp->export_name)
365 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
367 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
372 /*******************************************************************
373 * output_register_funcs
375 * Output the functions for register entry points
377 static void output_register_funcs( FILE *outfile )
382 for (i = 0; i < nb_entry_points; i++)
384 ORDDEF *odp = EntryPoints[i];
385 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
386 if (!(odp->flags & FLAG_REGISTER)) continue;
387 if (odp->flags & FLAG_FORWARD) continue;
388 name = make_internal_name( odp, "regs" );
390 "asm(\".align %d\\n\\t\"\n"
391 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
392 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
393 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
394 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
395 " \".byte %d,%d\");\n",
397 name, name, odp->link_name,
398 strlen(odp->u.func.arg_types) * sizeof(int),
399 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
404 /*******************************************************************
407 * Output code for calling a dll constructor and destructor.
409 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
411 fprintf( outfile, "#ifndef __GNUC__\n" );
412 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
413 fprintf( outfile, "#endif\n" );
415 #if defined(__i386__)
418 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
419 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
420 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
424 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
425 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
426 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
428 #elif defined(__sparc__)
431 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
432 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
433 fprintf( outfile, " \"\\tnop\\n\"\n" );
434 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
438 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
439 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
440 fprintf( outfile, " \"\\tnop\\n\"\n" );
441 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
443 #elif defined(__powerpc__)
445 /* Mach-O doesn't have an init section */
448 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
449 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
450 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", constructor );
451 fprintf( outfile, " \"\\t.text\\n\");\n" );
455 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
456 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
457 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", destructor );
458 fprintf( outfile, " \"\\t.text\\n\");\n" );
460 # else /* __APPLE__ */
463 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
464 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
465 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
469 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
470 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
471 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
473 # endif /* __APPLE__ */
475 #error You need to define the DLL constructor for your architecture
477 fprintf( outfile, "#ifndef __GNUC__\n" );
478 fprintf( outfile, "}\n" );
479 fprintf( outfile, "#endif\n" );
483 /*******************************************************************
486 * Build a Win32 C file from a spec file.
488 void BuildSpec32File( FILE *outfile )
490 int exports_size = 0;
491 int nr_exports, nr_imports, nr_resources;
492 int characteristics, subsystem;
494 char constructor[100];
496 #ifdef HAVE_GETPAGESIZE
497 page_size = getpagesize();
498 #elif defined(__svr4__)
499 page_size = sysconf(_SC_PAGESIZE);
500 #elif defined(_WINDOWS)
504 page_size = si.dwPageSize;
507 # error Cannot get the page size on this platform
511 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
514 output_standard_file_header( outfile );
516 /* Reserve some space for the PE header */
518 fprintf( outfile, "extern char __wine_spec_pe_header[];\n" );
519 fprintf( outfile, "#ifndef __GNUC__\n" );
520 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
521 fprintf( outfile, "#endif\n" );
522 fprintf( outfile, "asm(\".text\\n\\t\"\n" );
523 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
524 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP " 65536\\n\\t\"\n" );
525 fprintf( outfile, " \".data\\n\\t\"\n" );
526 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(4) );
527 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" );
528 fprintf( outfile, "#ifndef __GNUC__\n" );
529 fprintf( outfile, "}\n" );
530 fprintf( outfile, "#endif\n" );
532 fprintf( outfile, "extern char _end[];\n" );
533 fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
536 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
538 fprintf( outfile, "#define __stdcall\n\n" );
543 /* Output the stub functions */
545 output_stub_funcs( outfile );
547 fprintf( outfile, "#ifndef __GNUC__\n" );
548 fprintf( outfile, "static void __asm__dummy(void) {\n" );
549 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
551 /* Output code for all register functions */
553 output_register_funcs( outfile );
555 /* Output the exports and relay entry points */
557 exports_size = output_exports( outfile, nr_exports );
559 fprintf( outfile, "#ifndef __GNUC__\n" );
560 fprintf( outfile, "}\n" );
561 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
564 /* Output the DLL imports */
566 nr_imports = output_imports( outfile );
568 /* Output the resources */
570 nr_resources = output_resources( outfile );
572 /* Output LibMain function */
574 characteristics = subsystem = 0;
578 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
581 fprintf( outfile, "#ifdef __GNUC__\n" );
582 fprintf( outfile, "# ifdef __APPLE__\n" );
583 fprintf( outfile, "extern void DllMain() __attribute__((weak_import));\n" );
584 fprintf( outfile, "# else\n" );
585 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
586 fprintf( outfile, "# endif\n" );
587 fprintf( outfile, "#else\n" );
588 fprintf( outfile, "extern void DllMain();\n" );
589 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
590 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
591 fprintf( outfile, "#endif\n" );
593 characteristics = IMAGE_FILE_DLL;
595 case SPEC_MODE_GUIEXE:
596 if (!init_func) init_func = "WinMain";
598 "\ntypedef struct {\n"
599 " unsigned int cb;\n"
600 " char *lpReserved, *lpDesktop, *lpTitle;\n"
601 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
602 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
603 " unsigned short wShowWindow, cbReserved2;\n"
604 " char *lpReserved2;\n"
605 " void *hStdInput, *hStdOutput, *hStdError;\n"
609 "extern int __stdcall %s(void *,void *,char *,int);\n"
610 "extern char * __stdcall GetCommandLineA(void);\n"
611 "extern void * __stdcall GetModuleHandleA(char *);\n"
612 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
613 "extern void __stdcall ExitProcess(unsigned int);\n"
614 "static void __wine_exe_main(void)\n"
616 " extern int __wine_main_argc;\n"
617 " extern char **__wine_main_argv;\n"
618 " STARTUPINFOA info;\n"
619 " char *cmdline = GetCommandLineA();\n"
620 " int bcount=0, in_quotes=0;\n"
621 " while (*cmdline) {\n"
622 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
623 " else if (*cmdline=='\\\\') bcount++;\n"
624 " else if (*cmdline=='\\\"') {\n"
625 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
631 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
632 " GetStartupInfoA( &info );\n"
633 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
634 " _ARGC = __wine_main_argc;\n"
635 " _ARGV = __wine_main_argv;\n"
636 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
637 "}\n\n", init_func, init_func );
638 init_func = "__wine_exe_main";
639 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
641 case SPEC_MODE_GUIEXE_UNICODE:
642 if (!init_func) init_func = "WinMain";
644 "\ntypedef unsigned short WCHAR;\n"
646 " unsigned int cb;\n"
647 " char *lpReserved, *lpDesktop, *lpTitle;\n"
648 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
649 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
650 " unsigned short wShowWindow, cbReserved2;\n"
651 " char *lpReserved2;\n"
652 " void *hStdInput, *hStdOutput, *hStdError;\n"
656 "extern int __stdcall %s(void *,void *,char *,int);\n"
657 "extern char * __stdcall GetCommandLineA(void);\n"
658 "extern void * __stdcall GetModuleHandleA(char *);\n"
659 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
660 "extern void __stdcall ExitProcess(unsigned int);\n"
661 "static void __wine_exe_main(void)\n"
663 " extern int __wine_main_argc;\n"
664 " extern WCHAR **__wine_main_wargv;\n"
665 " STARTUPINFOA info;\n"
666 " char *cmdline = GetCommandLineA();\n"
667 " int bcount=0, in_quotes=0;\n"
668 " while (*cmdline) {\n"
669 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
670 " else if (*cmdline=='\\\\') bcount++;\n"
671 " else if (*cmdline=='\\\"') {\n"
672 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
678 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
679 " GetStartupInfoA( &info );\n"
680 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
681 " _ARGC = __wine_main_argc;\n"
682 " _ARGV = __wine_main_wargv;\n"
683 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
684 "}\n\n", init_func, init_func );
685 init_func = "__wine_exe_main";
686 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
688 case SPEC_MODE_CUIEXE:
689 if (!init_func) init_func = "main";
693 "extern void __stdcall ExitProcess(int);\n"
694 "static void __wine_exe_main(void)\n"
696 " extern int %s( int argc, char *argv[] );\n"
697 " extern int __wine_main_argc;\n"
698 " extern char **__wine_main_argv;\n"
699 " _ARGC = __wine_main_argc;\n"
700 " _ARGV = __wine_main_argv;\n"
701 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
702 "}\n\n", init_func, init_func );
703 init_func = "__wine_exe_main";
704 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
706 case SPEC_MODE_CUIEXE_UNICODE:
707 if (!init_func) init_func = "wmain";
709 "\ntypedef unsigned short WCHAR;\n"
712 "extern void __stdcall ExitProcess(int);\n"
713 "static void __wine_exe_main(void)\n"
715 " extern int %s( int argc, WCHAR *argv[] );\n"
716 " extern int __wine_main_argc;\n"
717 " extern WCHAR **__wine_main_wargv;\n"
718 " _ARGC = __wine_main_argc;\n"
719 " _ARGV = __wine_main_wargv;\n"
720 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
721 "}\n\n", init_func, init_func );
722 init_func = "__wine_exe_main";
723 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
727 /* Output the NT header */
729 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
730 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
731 fprintf( outfile, " int Signature;\n" );
732 fprintf( outfile, " struct file_header {\n" );
733 fprintf( outfile, " short Machine;\n" );
734 fprintf( outfile, " short NumberOfSections;\n" );
735 fprintf( outfile, " int TimeDateStamp;\n" );
736 fprintf( outfile, " void *PointerToSymbolTable;\n" );
737 fprintf( outfile, " int NumberOfSymbols;\n" );
738 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
739 fprintf( outfile, " short Characteristics;\n" );
740 fprintf( outfile, " } FileHeader;\n" );
741 fprintf( outfile, " struct opt_header {\n" );
742 fprintf( outfile, " short Magic;\n" );
743 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
744 fprintf( outfile, " int SizeOfCode;\n" );
745 fprintf( outfile, " int SizeOfInitializedData;\n" );
746 fprintf( outfile, " int SizeOfUninitializedData;\n" );
747 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
748 fprintf( outfile, " void *BaseOfCode;\n" );
749 fprintf( outfile, " void *BaseOfData;\n" );
750 fprintf( outfile, " void *ImageBase;\n" );
751 fprintf( outfile, " int SectionAlignment;\n" );
752 fprintf( outfile, " int FileAlignment;\n" );
753 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
754 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
755 fprintf( outfile, " short MajorImageVersion;\n" );
756 fprintf( outfile, " short MinorImageVersion;\n" );
757 fprintf( outfile, " short MajorSubsystemVersion;\n" );
758 fprintf( outfile, " short MinorSubsystemVersion;\n" );
759 fprintf( outfile, " int Win32VersionValue;\n" );
760 fprintf( outfile, " void *SizeOfImage;\n" );
761 fprintf( outfile, " int SizeOfHeaders;\n" );
762 fprintf( outfile, " int CheckSum;\n" );
763 fprintf( outfile, " short Subsystem;\n" );
764 fprintf( outfile, " short DllCharacteristics;\n" );
765 fprintf( outfile, " int SizeOfStackReserve;\n" );
766 fprintf( outfile, " int SizeOfStackCommit;\n" );
767 fprintf( outfile, " int SizeOfHeapReserve;\n" );
768 fprintf( outfile, " int SizeOfHeapCommit;\n" );
769 fprintf( outfile, " int LoaderFlags;\n" );
770 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
771 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
772 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
773 fprintf( outfile, " } OptionalHeader;\n" );
774 fprintf( outfile, "} nt_header = {\n" );
775 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
777 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
778 #elif defined(__powerpc__)
779 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC ); /* Machine */
781 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN ); /* Machine */
783 fprintf( outfile, " 0, 0, 0, 0,\n" );
784 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
785 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
787 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
788 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
789 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
790 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
791 fprintf( outfile, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
792 fprintf( outfile, " __wine_spec_pe_header,\n" ); /* ImageBase */
793 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
794 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
795 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
796 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
797 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
798 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
799 fprintf( outfile, " _end,\n" ); /* SizeOfImage */
800 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
801 fprintf( outfile, " 0,\n" ); /* CheckSum */
802 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
803 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
804 fprintf( outfile, " %d, %ld,\n", /* SizeOfStackReserve/Commit */
805 (stack_size ? stack_size : 1024) * 1024, page_size );
806 fprintf( outfile, " %d, %ld,\n", /* SizeOfHeapReserve/Commit */
807 (DLLHeapSize ? DLLHeapSize : 1024) * 1024, page_size );
808 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
809 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
810 fprintf( outfile, " {\n" );
811 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
812 exports_size ? "__wine_spec_exports" : "0", exports_size );
813 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
814 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
815 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
816 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
817 fprintf( outfile, " }\n }\n};\n\n" );
819 /* Output the DLL constructor */
821 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(dll_file_name) );
822 output_dll_init( outfile, constructor, NULL );
827 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
828 " extern void *__wine_dbg_register( char * const *, int );\n"
829 " __wine_dll_register( &nt_header, \"%s\" );\n"
831 constructor, dll_file_name );
835 /*******************************************************************
838 * Build a Win32 def file from a spec file.
840 void BuildDef32File(FILE *outfile)
847 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
850 fprintf(outfile, "LIBRARY %s\n\n", dll_file_name);
852 fprintf(outfile, "EXPORTS\n");
854 /* Output the exports and relay entry points */
856 for(i = 0; i < nb_entry_points; i++)
858 ORDDEF *odp = EntryPoints[i];
862 if (odp->flags & FLAG_REGISTER) continue;
863 if (odp->type == TYPE_STUB) continue;
865 if (odp->name) name = odp->name;
866 else if (odp->export_name) name = odp->export_name;
869 fprintf(outfile, " %s", name);
878 /* try to reduce output */
879 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
880 fprintf(outfile, "=%s", odp->link_name);
884 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
885 if (!kill_at) fprintf(outfile, "@%d", at_param);
886 if (odp->flags & FLAG_FORWARD)
888 fprintf(outfile, "=%s", odp->link_name);
890 else if (strcmp(name, odp->link_name)) /* try to reduce output */
892 fprintf(outfile, "=%s", odp->link_name);
893 if (!kill_at) fprintf(outfile, "@%d", at_param);
900 fprintf( outfile, " @%d", odp->ordinal );
901 if (!odp->name) fprintf( outfile, " NONAME" );
902 if (is_data) fprintf( outfile, " DATA" );
903 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
904 fprintf( outfile, "\n" );
909 /*******************************************************************
912 * Build the debugging channels source file.
914 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
921 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
924 output_standard_file_header( outfile );
925 nr_debug = output_debug( outfile );
928 fprintf( outfile, "/* no debug channels found for this module */\n" );
932 if (output_file_name)
934 if ((p = strrchr( output_file_name, '/' ))) p++;
935 prefix = xstrdup( p ? p : output_file_name );
936 if ((p = strchr( prefix, '.' ))) *p = 0;
937 strcpy( p, make_c_identifier(p) );
939 else prefix = xstrdup( "_" );
941 /* Output the DLL constructor */
945 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
946 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
948 "static void __asm__dummy_dll_init(void) {\n",
951 #if defined(__i386__)
952 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
953 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
954 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
955 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
956 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
957 #elif defined(__sparc__)
958 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
959 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
960 fprintf( outfile, " \"\\tnop\\n\"\n" );
961 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
962 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
963 fprintf( outfile, " \"\\tnop\\n\"\n" );
964 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
965 #elif defined(__powerpc__)
966 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
967 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
968 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
969 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
970 fprintf( outfile, " \"\\t.text\\n\");\n" );
972 #error You need to define the DLL constructor for your architecture
974 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
977 "\n#ifdef __GNUC__\n"
980 "void __wine_dbg_%s_init(void)\n"
982 " extern void *__wine_dbg_register( char * const *, int );\n"
983 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
984 "}\n", prefix, nr_debug );
986 "\n#ifdef __GNUC__\n"
989 "void __wine_dbg_%s_fini(void)\n"
991 " extern void __wine_dbg_unregister( void* );\n"
992 " __wine_dbg_unregister( debug_registration );\n"