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, const char *prefix )
54 static char buffer[256];
55 if (odp->name || odp->export_name)
58 sprintf( buffer, "__wine_%s_%s_%s", prefix, dll_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(dll_file_name), odp->ordinal );
68 /*******************************************************************
71 * Assign ordinals to all entry points.
73 static void AssignOrdinals(void)
77 if ( !nb_names ) return;
79 /* start assigning from Base, or from 1 if no ordinal defined yet */
80 if (Base == MAX_ORDINALS) Base = 1;
81 for (i = 0, ordinal = Base; i < nb_names; i++)
83 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
84 while (Ordinals[ordinal]) ordinal++;
85 if (ordinal >= MAX_ORDINALS)
87 current_line = Names[i]->lineno;
88 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
90 Names[i]->ordinal = ordinal;
91 Ordinals[ordinal] = Names[i];
93 if (ordinal > Limit) Limit = ordinal;
97 /*******************************************************************
100 * Output the debug channels.
102 static int output_debug( FILE *outfile )
106 if (!nb_debug_channels) return 0;
107 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
109 for (i = 0; i < nb_debug_channels; i++)
110 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
111 debug_channels[i], debug_channels[i] );
113 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
114 for (i = 0; i < nb_debug_channels; i++)
116 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
117 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
119 fprintf( outfile, "\n};\n\n" );
120 fprintf( outfile, "static void *debug_registration;\n\n" );
122 return nb_debug_channels;
126 /*******************************************************************
129 * Output the export table for a Win32 module.
131 static int output_exports( FILE *outfile, int nr_exports )
133 int i, fwd_size = 0, total_size = 0;
135 if (!nr_exports) return 0;
137 fprintf( outfile, "asm(\".data\\n\"\n" );
138 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
139 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
141 /* export directory header */
143 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
144 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
145 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
146 fprintf( outfile, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
147 fprintf( outfile, " \"\\t.long %d\\n\"\n", Base ); /* Base */
148 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
149 fprintf( outfile, " \"\\t.long %d\\n\"\n", nb_names ); /* NumberOfNames */
150 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
153 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
154 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
158 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
159 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
161 total_size += 10 * sizeof(int);
163 /* output the function pointers */
165 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
166 for (i = Base; i <= Limit; i++)
168 ORDDEF *odp = Ordinals[i];
169 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
170 else switch(odp->type)
176 if (!(odp->flags & FLAG_FORWARD))
178 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
179 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
183 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
184 fwd_size += strlen(odp->link_name) + 1;
188 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
194 total_size += (Limit - Base + 1) * sizeof(int);
198 /* output the function name pointers */
200 int namepos = strlen(dll_file_name) + 1;
202 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
203 for (i = 0; i < nb_names; i++)
205 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
206 namepos += strlen(Names[i]->name) + 1;
208 total_size += nb_names * sizeof(int);
211 /* output the function names */
213 fprintf( outfile, " \"\\t.text\\n\"\n" );
214 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
215 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", dll_file_name );
216 for (i = 0; i < nb_names; i++)
217 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
218 fprintf( outfile, " \"\\t.data\\n\"\n" );
222 /* output the function ordinals */
224 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
225 for (i = 0; i < nb_names; i++)
227 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", Names[i]->ordinal - Base );
229 total_size += nb_names * sizeof(short);
232 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
233 total_size += sizeof(short);
237 /* output forward strings */
241 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
242 for (i = Base; i <= Limit; i++)
244 ORDDEF *odp = Ordinals[i];
245 if (odp && (odp->flags & FLAG_FORWARD))
246 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
248 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
249 total_size += (fwd_size + 3) & ~3;
256 for (i = Base; i <= Limit; i++)
258 ORDDEF *odp = Ordinals[i];
259 unsigned int j, args, mask = 0;
262 /* skip non-existent entry points */
263 if (!odp) goto ignore;
264 /* skip non-functions */
265 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
266 /* skip norelay and forward entry points */
267 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
269 for (j = 0; odp->u.func.arg_types[j]; j++)
271 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
272 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
274 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
276 name = odp->link_name;
277 args = strlen(odp->u.func.arg_types) * sizeof(int);
278 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, "regs" );
283 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
284 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
285 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
288 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
289 fprintf( outfile, " \"\\tret\\n\"\n" );
290 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
291 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
299 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
303 fprintf( outfile, " \"\\t.text\\n\"\n" );
304 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
305 fprintf( outfile, ");\n\n" );
311 /*******************************************************************
314 * Output the functions for stub entry points
316 static void output_stub_funcs( FILE *outfile )
320 for (i = 0; i < nb_entry_points; i++)
322 ORDDEF *odp = EntryPoints[i];
323 if (odp->type != TYPE_STUB) continue;
324 fprintf( outfile, "#ifdef __GNUC__\n" );
325 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
326 fprintf( outfile, "#endif\n\n" );
327 fprintf( outfile, "struct exc_record {\n" );
328 fprintf( outfile, " unsigned int code, flags;\n" );
329 fprintf( outfile, " void *rec, *addr;\n" );
330 fprintf( outfile, " unsigned int params;\n" );
331 fprintf( outfile, " const void *info[15];\n" );
332 fprintf( outfile, "};\n\n" );
333 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
334 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
335 fprintf( outfile, " struct exc_record rec;\n" );
336 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
337 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
338 fprintf( outfile, " rec.rec = 0;\n" );
339 fprintf( outfile, " rec.params = 2;\n" );
340 fprintf( outfile, " rec.info[0] = \"%s\";\n", dll_file_name );
341 fprintf( outfile, " rec.info[1] = func;\n" );
342 fprintf( outfile, "#ifdef __GNUC__\n" );
343 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
344 fprintf( outfile, "#else\n" );
345 fprintf( outfile, " rec.addr = 0;\n" );
346 fprintf( outfile, "#endif\n" );
347 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
351 for (i = 0; i < nb_entry_points; i++)
353 ORDDEF *odp = EntryPoints[i];
354 if (odp->type != TYPE_STUB) continue;
355 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
357 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
358 else if (odp->export_name)
359 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
361 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
366 /*******************************************************************
367 * output_register_funcs
369 * Output the functions for register entry points
371 static void output_register_funcs( FILE *outfile )
376 for (i = 0; i < nb_entry_points; i++)
378 ORDDEF *odp = EntryPoints[i];
379 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
380 if (!(odp->flags & FLAG_REGISTER)) continue;
381 if (odp->flags & FLAG_FORWARD) continue;
382 name = make_internal_name( odp, "regs" );
384 "asm(\".align %d\\n\\t\"\n"
385 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
386 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
387 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
388 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
389 " \".byte %d,%d\");\n",
391 name, name, odp->link_name,
392 strlen(odp->u.func.arg_types) * sizeof(int),
393 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
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 fprintf( outfile, "#ifndef __GNUC__\n" );
406 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
407 fprintf( outfile, "#endif\n" );
409 #if defined(__i386__)
412 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
413 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
414 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
418 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
419 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
420 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
422 #elif defined(__sparc__)
425 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
426 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
427 fprintf( outfile, " \"\\tnop\\n\"\n" );
428 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
432 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
433 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
434 fprintf( outfile, " \"\\tnop\\n\"\n" );
435 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
437 #elif defined(__PPC__)
440 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
441 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
442 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
446 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
447 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
448 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
451 #error You need to define the DLL constructor for your architecture
453 fprintf( outfile, "#ifndef __GNUC__\n" );
454 fprintf( outfile, "}\n" );
455 fprintf( outfile, "#endif\n" );
459 /*******************************************************************
462 * Build a Win32 C file from a spec file.
464 void BuildSpec32File( FILE *outfile )
466 int exports_size = 0;
467 int nr_exports, nr_imports, nr_resources;
468 int characteristics, subsystem;
470 char constructor[100];
472 #ifdef HAVE_GETPAGESIZE
473 page_size = getpagesize();
474 #elif defined(__svr4__)
475 page_size = sysconf(_SC_PAGESIZE);
476 #elif defined(_WINDOWS)
480 page_size = si.dwPageSize;
483 # error Cannot get the page size on this platform
487 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
490 output_standard_file_header( outfile );
492 /* Reserve some space for the PE header */
494 fprintf( outfile, "extern char pe_header[];\n" );
495 fprintf( outfile, "#ifndef __GNUC__\n" );
496 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
497 fprintf( outfile, "#endif\n" );
498 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
499 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
500 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
501 fprintf( outfile, "#ifndef __GNUC__\n" );
502 fprintf( outfile, "}\n" );
503 fprintf( outfile, "#endif\n" );
505 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
508 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
510 fprintf( outfile, "#define __stdcall\n\n" );
515 /* Output the stub functions */
517 output_stub_funcs( outfile );
519 fprintf( outfile, "#ifndef __GNUC__\n" );
520 fprintf( outfile, "static void __asm__dummy(void) {\n" );
521 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
523 /* Output code for all register functions */
525 output_register_funcs( outfile );
527 /* Output the exports and relay entry points */
529 exports_size = output_exports( outfile, nr_exports );
531 fprintf( outfile, "#ifndef __GNUC__\n" );
532 fprintf( outfile, "}\n" );
533 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
536 /* Output the DLL imports */
538 nr_imports = output_imports( outfile );
540 /* Output the resources */
542 nr_resources = output_resources( outfile );
544 /* Output LibMain function */
546 characteristics = subsystem = 0;
550 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
553 fprintf( outfile, "#ifdef __GNUC__\n" );
554 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
555 fprintf( outfile, "#else\n" );
556 fprintf( outfile, "extern void DllMain();\n" );
557 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
558 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
559 fprintf( outfile, "#endif\n" );
561 characteristics = IMAGE_FILE_DLL;
563 case SPEC_MODE_GUIEXE:
564 if (!init_func) init_func = "WinMain";
566 "\ntypedef struct {\n"
567 " unsigned int cb;\n"
568 " char *lpReserved, *lpDesktop, *lpTitle;\n"
569 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
570 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
571 " unsigned short wShowWindow, cbReserved2;\n"
572 " char *lpReserved2;\n"
573 " void *hStdInput, *hStdOutput, *hStdError;\n"
577 "extern int __stdcall %s(void *,void *,char *,int);\n"
578 "extern char * __stdcall GetCommandLineA(void);\n"
579 "extern void * __stdcall GetModuleHandleA(char *);\n"
580 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
581 "extern void __stdcall ExitProcess(unsigned int);\n"
582 "static void __wine_exe_main(void)\n"
584 " extern int __wine_main_argc;\n"
585 " extern char **__wine_main_argv;\n"
586 " STARTUPINFOA info;\n"
587 " char *cmdline = GetCommandLineA();\n"
588 " int bcount=0, in_quotes=0;\n"
589 " while (*cmdline) {\n"
590 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
591 " else if (*cmdline=='\\\\') bcount++;\n"
592 " else if (*cmdline=='\\\"') {\n"
593 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
599 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
600 " GetStartupInfoA( &info );\n"
601 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
602 " _ARGC = __wine_main_argc;\n"
603 " _ARGV = __wine_main_argv;\n"
604 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
605 "}\n\n", init_func, init_func );
606 init_func = "__wine_exe_main";
607 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
609 case SPEC_MODE_GUIEXE_UNICODE:
610 if (!init_func) init_func = "WinMain";
612 "\ntypedef unsigned short WCHAR;\n"
614 " unsigned int cb;\n"
615 " char *lpReserved, *lpDesktop, *lpTitle;\n"
616 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
617 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
618 " unsigned short wShowWindow, cbReserved2;\n"
619 " char *lpReserved2;\n"
620 " void *hStdInput, *hStdOutput, *hStdError;\n"
624 "extern int __stdcall %s(void *,void *,char *,int);\n"
625 "extern char * __stdcall GetCommandLineA(void);\n"
626 "extern void * __stdcall GetModuleHandleA(char *);\n"
627 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
628 "extern void __stdcall ExitProcess(unsigned int);\n"
629 "static void __wine_exe_main(void)\n"
631 " extern int __wine_main_argc;\n"
632 " extern WCHAR **__wine_main_wargv;\n"
633 " STARTUPINFOA info;\n"
634 " char *cmdline = GetCommandLineA();\n"
635 " int bcount=0, in_quotes=0;\n"
636 " while (*cmdline) {\n"
637 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
638 " else if (*cmdline=='\\\\') bcount++;\n"
639 " else if (*cmdline=='\\\"') {\n"
640 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
646 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
647 " GetStartupInfoA( &info );\n"
648 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
649 " _ARGC = __wine_main_argc;\n"
650 " _ARGV = __wine_main_wargv;\n"
651 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
652 "}\n\n", init_func, init_func );
653 init_func = "__wine_exe_main";
654 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
656 case SPEC_MODE_CUIEXE:
657 if (!init_func) init_func = "main";
661 "extern void __stdcall ExitProcess(int);\n"
662 "static void __wine_exe_main(void)\n"
664 " extern int %s( int argc, char *argv[] );\n"
665 " extern int __wine_main_argc;\n"
666 " extern char **__wine_main_argv;\n"
667 " _ARGC = __wine_main_argc;\n"
668 " _ARGV = __wine_main_argv;\n"
669 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
670 "}\n\n", init_func, init_func );
671 init_func = "__wine_exe_main";
672 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
674 case SPEC_MODE_CUIEXE_UNICODE:
675 if (!init_func) init_func = "wmain";
677 "\ntypedef unsigned short WCHAR;\n"
680 "extern void __stdcall ExitProcess(int);\n"
681 "static void __wine_exe_main(void)\n"
683 " extern int %s( int argc, WCHAR *argv[] );\n"
684 " extern int __wine_main_argc;\n"
685 " extern WCHAR **__wine_main_wargv;\n"
686 " _ARGC = __wine_main_argc;\n"
687 " _ARGV = __wine_main_wargv;\n"
688 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
689 "}\n\n", init_func, init_func );
690 init_func = "__wine_exe_main";
691 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
695 /* Output the NT header */
697 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
698 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
699 fprintf( outfile, " int Signature;\n" );
700 fprintf( outfile, " struct file_header {\n" );
701 fprintf( outfile, " short Machine;\n" );
702 fprintf( outfile, " short NumberOfSections;\n" );
703 fprintf( outfile, " int TimeDateStamp;\n" );
704 fprintf( outfile, " void *PointerToSymbolTable;\n" );
705 fprintf( outfile, " int NumberOfSymbols;\n" );
706 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
707 fprintf( outfile, " short Characteristics;\n" );
708 fprintf( outfile, " } FileHeader;\n" );
709 fprintf( outfile, " struct opt_header {\n" );
710 fprintf( outfile, " short Magic;\n" );
711 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
712 fprintf( outfile, " int SizeOfCode;\n" );
713 fprintf( outfile, " int SizeOfInitializedData;\n" );
714 fprintf( outfile, " int SizeOfUninitializedData;\n" );
715 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
716 fprintf( outfile, " void *BaseOfCode;\n" );
717 fprintf( outfile, " void *BaseOfData;\n" );
718 fprintf( outfile, " void *ImageBase;\n" );
719 fprintf( outfile, " int SectionAlignment;\n" );
720 fprintf( outfile, " int FileAlignment;\n" );
721 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
722 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
723 fprintf( outfile, " short MajorImageVersion;\n" );
724 fprintf( outfile, " short MinorImageVersion;\n" );
725 fprintf( outfile, " short MajorSubsystemVersion;\n" );
726 fprintf( outfile, " short MinorSubsystemVersion;\n" );
727 fprintf( outfile, " int Win32VersionValue;\n" );
728 fprintf( outfile, " int SizeOfImage;\n" );
729 fprintf( outfile, " int SizeOfHeaders;\n" );
730 fprintf( outfile, " int CheckSum;\n" );
731 fprintf( outfile, " short Subsystem;\n" );
732 fprintf( outfile, " short DllCharacteristics;\n" );
733 fprintf( outfile, " int SizeOfStackReserve;\n" );
734 fprintf( outfile, " int SizeOfStackCommit;\n" );
735 fprintf( outfile, " int SizeOfHeapReserve;\n" );
736 fprintf( outfile, " int SizeOfHeapCommit;\n" );
737 fprintf( outfile, " int LoaderFlags;\n" );
738 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
739 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
740 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
741 fprintf( outfile, " } OptionalHeader;\n" );
742 fprintf( outfile, "} nt_header = {\n" );
743 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
745 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
746 fprintf( outfile, " 0, 0, 0, 0,\n" );
747 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
748 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
750 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
751 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
752 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
753 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
754 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
755 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
756 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
757 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
758 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
759 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
760 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
761 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
762 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
763 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
764 fprintf( outfile, " 0,\n" ); /* CheckSum */
765 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
766 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
767 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
768 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
769 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
770 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
771 fprintf( outfile, " {\n" );
772 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
773 exports_size ? "__wine_spec_exports" : "0", exports_size );
774 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
775 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
776 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
777 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
778 fprintf( outfile, " }\n }\n};\n\n" );
780 /* Output the DLL constructor */
782 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(dll_file_name) );
783 output_dll_init( outfile, constructor, NULL );
788 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
789 " extern void *__wine_dbg_register( char * const *, int );\n"
790 " __wine_dll_register( &nt_header, \"%s\" );\n"
792 constructor, dll_file_name );
796 /*******************************************************************
799 * Build a Win32 def file from a spec file.
801 void BuildDef32File(FILE *outfile)
808 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
811 fprintf(outfile, "LIBRARY %s\n\n", dll_file_name);
813 fprintf(outfile, "EXPORTS\n");
815 /* Output the exports and relay entry points */
817 for(i = 0; i < nb_entry_points; i++)
819 ORDDEF *odp = EntryPoints[i];
823 if (odp->flags & FLAG_REGISTER) continue;
824 if (odp->type == TYPE_STUB) continue;
826 if (odp->name) name = odp->name;
827 else if (odp->export_name) name = odp->export_name;
830 fprintf(outfile, " %s", name);
839 /* try to reduce output */
840 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
841 fprintf(outfile, "=%s", odp->link_name);
845 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
846 if (!kill_at) fprintf(outfile, "@%d", at_param);
847 if (odp->flags & FLAG_FORWARD)
849 fprintf(outfile, "=%s", odp->link_name);
851 else if (strcmp(name, odp->link_name)) /* try to reduce output */
853 fprintf(outfile, "=%s", odp->link_name);
854 if (!kill_at) fprintf(outfile, "@%d", at_param);
861 fprintf( outfile, " @%d", odp->ordinal );
862 if (!odp->name) fprintf( outfile, " NONAME" );
863 if (is_data) fprintf( outfile, " DATA" );
864 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
865 fprintf( outfile, "\n" );
870 /*******************************************************************
873 * Build the debugging channels source file.
875 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
882 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
885 output_standard_file_header( outfile );
886 nr_debug = output_debug( outfile );
889 fprintf( outfile, "/* no debug channels found for this module */\n" );
893 if (output_file_name)
895 if ((p = strrchr( output_file_name, '/' ))) p++;
896 prefix = xstrdup( p ? p : output_file_name );
897 if ((p = strchr( prefix, '.' ))) *p = 0;
898 strcpy( p, make_c_identifier(p) );
900 else prefix = xstrdup( "_" );
902 /* Output the DLL constructor */
906 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
907 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
909 "static void __asm__dummy_dll_init(void) {\n",
912 #if defined(__i386__)
913 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
914 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
915 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
916 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
917 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
918 #elif defined(__sparc__)
919 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
920 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
921 fprintf( outfile, " \"\\tnop\\n\"\n" );
922 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
923 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
924 fprintf( outfile, " \"\\tnop\\n\"\n" );
925 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
926 #elif defined(__PPC__)
927 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
928 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
929 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
930 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
931 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
933 #error You need to define the DLL constructor for your architecture
935 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
938 "\n#ifdef __GNUC__\n"
941 "void __wine_dbg_%s_init(void)\n"
943 " extern void *__wine_dbg_register( char * const *, int );\n"
944 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
945 "}\n", prefix, nr_debug );
947 "\n#ifdef __GNUC__\n"
950 "void __wine_dbg_%s_fini(void)\n"
952 " extern void __wine_dbg_unregister( void* );\n"
953 " __wine_dbg_unregister( debug_registration );\n"