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 " __ASM_NAME("dllname") "\\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 */
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);
210 /* output the function names */
212 fprintf( outfile, " \"\\t.text\\n\"\n" );
213 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
214 for (i = 0; i < nb_names; i++)
215 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
216 fprintf( outfile, " \"\\t.data\\n\"\n" );
218 /* output the function ordinals */
220 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
221 for (i = 0; i < nb_names; i++)
223 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", Names[i]->ordinal - Base );
225 total_size += nb_names * sizeof(short);
228 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
229 total_size += sizeof(short);
233 /* output forward strings */
237 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
238 for (i = Base; i <= Limit; i++)
240 ORDDEF *odp = Ordinals[i];
241 if (odp && (odp->flags & FLAG_FORWARD))
242 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
244 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
245 total_size += (fwd_size + 3) & ~3;
252 for (i = Base; i <= Limit; i++)
254 ORDDEF *odp = Ordinals[i];
255 unsigned int j, args, mask = 0;
258 /* skip non-existent entry points */
259 if (!odp) goto ignore;
260 /* skip non-functions */
261 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
262 /* skip norelay and forward entry points */
263 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
265 for (j = 0; odp->u.func.arg_types[j]; j++)
267 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
268 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
270 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
272 name = odp->link_name;
273 args = strlen(odp->u.func.arg_types) * sizeof(int);
274 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, "regs" );
279 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
280 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
281 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
284 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
285 fprintf( outfile, " \"\\tret\\n\"\n" );
286 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
287 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
295 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
299 fprintf( outfile, " \"\\t.text\\n\"\n" );
300 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
301 fprintf( outfile, ");\n\n" );
307 /*******************************************************************
310 * Output the functions for stub entry points
312 static void output_stub_funcs( FILE *outfile )
316 for (i = 0; i < nb_entry_points; i++)
318 ORDDEF *odp = EntryPoints[i];
319 if (odp->type != TYPE_STUB) continue;
320 fprintf( outfile, "#ifdef __GNUC__\n" );
321 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
322 fprintf( outfile, "#endif\n\n" );
323 fprintf( outfile, "struct exc_record {\n" );
324 fprintf( outfile, " unsigned int code, flags;\n" );
325 fprintf( outfile, " void *rec, *addr;\n" );
326 fprintf( outfile, " unsigned int params;\n" );
327 fprintf( outfile, " const void *info[15];\n" );
328 fprintf( outfile, "};\n\n" );
329 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
330 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
331 fprintf( outfile, " struct exc_record rec;\n" );
332 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
333 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
334 fprintf( outfile, " rec.rec = 0;\n" );
335 fprintf( outfile, " rec.params = 2;\n" );
336 fprintf( outfile, " rec.info[0] = dllname;\n" );
337 fprintf( outfile, " rec.info[1] = func;\n" );
338 fprintf( outfile, "#ifdef __GNUC__\n" );
339 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
340 fprintf( outfile, "#else\n" );
341 fprintf( outfile, " rec.addr = 0;\n" );
342 fprintf( outfile, "#endif\n" );
343 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
347 for (i = 0; i < nb_entry_points; i++)
349 ORDDEF *odp = EntryPoints[i];
350 if (odp->type != TYPE_STUB) continue;
351 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
353 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
354 else if (odp->export_name)
355 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
357 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
362 /*******************************************************************
363 * output_register_funcs
365 * Output the functions for register entry points
367 static void output_register_funcs( FILE *outfile )
372 for (i = 0; i < nb_entry_points; i++)
374 ORDDEF *odp = EntryPoints[i];
375 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
376 if (!(odp->flags & FLAG_REGISTER)) continue;
377 if (odp->flags & FLAG_FORWARD) continue;
378 name = make_internal_name( odp, "regs" );
380 "asm(\".align %d\\n\\t\"\n"
381 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
382 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
383 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
384 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
385 " \".byte %d,%d\");\n",
387 name, name, odp->link_name,
388 strlen(odp->u.func.arg_types) * sizeof(int),
389 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
394 /*******************************************************************
397 * Output code for calling a dll constructor and destructor.
399 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
401 fprintf( outfile, "#ifndef __GNUC__\n" );
402 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
403 fprintf( outfile, "#endif\n" );
405 #if defined(__i386__)
408 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
409 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
410 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
414 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
415 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
416 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
418 #elif defined(__sparc__)
421 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
422 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
423 fprintf( outfile, " \"\\tnop\\n\"\n" );
424 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
428 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
429 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
430 fprintf( outfile, " \"\\tnop\\n\"\n" );
431 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
433 #elif defined(__PPC__)
436 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
437 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
438 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
442 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
443 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
444 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
447 #error You need to define the DLL constructor for your architecture
449 fprintf( outfile, "#ifndef __GNUC__\n" );
450 fprintf( outfile, "}\n" );
451 fprintf( outfile, "#endif\n" );
455 /*******************************************************************
458 * Build a Win32 C file from a spec file.
460 void BuildSpec32File( FILE *outfile )
462 int exports_size = 0;
463 int nr_exports, nr_imports, nr_resources;
464 int characteristics, subsystem;
466 char constructor[100];
468 #ifdef HAVE_GETPAGESIZE
469 page_size = getpagesize();
470 #elif defined(__svr4__)
471 page_size = sysconf(_SC_PAGESIZE);
472 #elif defined(_WINDOWS)
476 page_size = si.dwPageSize;
479 # error Cannot get the page size on this platform
483 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
486 output_standard_file_header( outfile );
488 /* Reserve some space for the PE header */
490 fprintf( outfile, "extern char pe_header[];\n" );
491 fprintf( outfile, "#ifndef __GNUC__\n" );
492 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
493 fprintf( outfile, "#endif\n" );
494 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
495 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
496 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
497 fprintf( outfile, "#ifndef __GNUC__\n" );
498 fprintf( outfile, "}\n" );
499 fprintf( outfile, "#endif\n" );
501 fprintf( outfile, "const char dllname[] = \"%s\";\n\n", dll_file_name );
502 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
505 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
507 fprintf( outfile, "#define __stdcall\n\n" );
512 /* Output the stub functions */
514 output_stub_funcs( outfile );
516 fprintf( outfile, "#ifndef __GNUC__\n" );
517 fprintf( outfile, "static void __asm__dummy(void) {\n" );
518 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
520 /* Output code for all register functions */
522 output_register_funcs( outfile );
524 /* Output the exports and relay entry points */
526 exports_size = output_exports( outfile, nr_exports );
528 fprintf( outfile, "#ifndef __GNUC__\n" );
529 fprintf( outfile, "}\n" );
530 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
533 /* Output the DLL imports */
535 nr_imports = output_imports( outfile );
537 /* Output the resources */
539 nr_resources = output_resources( outfile );
541 /* Output LibMain function */
543 characteristics = subsystem = 0;
547 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
550 fprintf( outfile, "#ifdef __GNUC__\n" );
551 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
552 fprintf( outfile, "#else\n" );
553 fprintf( outfile, "extern void DllMain();\n" );
554 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
555 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
556 fprintf( outfile, "#endif\n" );
558 characteristics = IMAGE_FILE_DLL;
560 case SPEC_MODE_GUIEXE:
561 if (!init_func) init_func = "WinMain";
563 "\ntypedef struct {\n"
564 " unsigned int cb;\n"
565 " char *lpReserved, *lpDesktop, *lpTitle;\n"
566 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
567 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
568 " unsigned short wShowWindow, cbReserved2;\n"
569 " char *lpReserved2;\n"
570 " void *hStdInput, *hStdOutput, *hStdError;\n"
574 "extern int __stdcall %s(void *,void *,char *,int);\n"
575 "extern char * __stdcall GetCommandLineA(void);\n"
576 "extern void * __stdcall GetModuleHandleA(char *);\n"
577 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
578 "extern void __stdcall ExitProcess(unsigned int);\n"
579 "static void __wine_exe_main(void)\n"
581 " extern int __wine_main_argc;\n"
582 " extern char **__wine_main_argv;\n"
583 " STARTUPINFOA info;\n"
584 " char *cmdline = GetCommandLineA();\n"
585 " int bcount=0, in_quotes=0;\n"
586 " while (*cmdline) {\n"
587 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
588 " else if (*cmdline=='\\\\') bcount++;\n"
589 " else if (*cmdline=='\\\"') {\n"
590 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
596 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
597 " GetStartupInfoA( &info );\n"
598 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
599 " _ARGC = __wine_main_argc;\n"
600 " _ARGV = __wine_main_argv;\n"
601 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
602 "}\n\n", init_func, init_func );
603 init_func = "__wine_exe_main";
604 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
606 case SPEC_MODE_GUIEXE_UNICODE:
607 if (!init_func) init_func = "WinMain";
609 "\ntypedef unsigned short WCHAR;\n"
611 " unsigned int cb;\n"
612 " char *lpReserved, *lpDesktop, *lpTitle;\n"
613 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
614 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
615 " unsigned short wShowWindow, cbReserved2;\n"
616 " char *lpReserved2;\n"
617 " void *hStdInput, *hStdOutput, *hStdError;\n"
621 "extern int __stdcall %s(void *,void *,char *,int);\n"
622 "extern char * __stdcall GetCommandLineA(void);\n"
623 "extern void * __stdcall GetModuleHandleA(char *);\n"
624 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
625 "extern void __stdcall ExitProcess(unsigned int);\n"
626 "static void __wine_exe_main(void)\n"
628 " extern int __wine_main_argc;\n"
629 " extern WCHAR **__wine_main_wargv;\n"
630 " STARTUPINFOA info;\n"
631 " char *cmdline = GetCommandLineA();\n"
632 " int bcount=0, in_quotes=0;\n"
633 " while (*cmdline) {\n"
634 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
635 " else if (*cmdline=='\\\\') bcount++;\n"
636 " else if (*cmdline=='\\\"') {\n"
637 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
643 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
644 " GetStartupInfoA( &info );\n"
645 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
646 " _ARGC = __wine_main_argc;\n"
647 " _ARGV = __wine_main_wargv;\n"
648 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
649 "}\n\n", init_func, init_func );
650 init_func = "__wine_exe_main";
651 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
653 case SPEC_MODE_CUIEXE:
654 if (!init_func) init_func = "main";
658 "extern void __stdcall ExitProcess(int);\n"
659 "static void __wine_exe_main(void)\n"
661 " extern int %s( int argc, char *argv[] );\n"
662 " extern int __wine_main_argc;\n"
663 " extern char **__wine_main_argv;\n"
664 " _ARGC = __wine_main_argc;\n"
665 " _ARGV = __wine_main_argv;\n"
666 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
667 "}\n\n", init_func, init_func );
668 init_func = "__wine_exe_main";
669 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
671 case SPEC_MODE_CUIEXE_UNICODE:
672 if (!init_func) init_func = "wmain";
674 "\ntypedef unsigned short WCHAR;\n"
677 "extern void __stdcall ExitProcess(int);\n"
678 "static void __wine_exe_main(void)\n"
680 " extern int %s( int argc, WCHAR *argv[] );\n"
681 " extern int __wine_main_argc;\n"
682 " extern WCHAR **__wine_main_wargv;\n"
683 " _ARGC = __wine_main_argc;\n"
684 " _ARGV = __wine_main_wargv;\n"
685 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
686 "}\n\n", init_func, init_func );
687 init_func = "__wine_exe_main";
688 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
692 /* Output the NT header */
694 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
695 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
696 fprintf( outfile, " int Signature;\n" );
697 fprintf( outfile, " struct file_header {\n" );
698 fprintf( outfile, " short Machine;\n" );
699 fprintf( outfile, " short NumberOfSections;\n" );
700 fprintf( outfile, " int TimeDateStamp;\n" );
701 fprintf( outfile, " void *PointerToSymbolTable;\n" );
702 fprintf( outfile, " int NumberOfSymbols;\n" );
703 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
704 fprintf( outfile, " short Characteristics;\n" );
705 fprintf( outfile, " } FileHeader;\n" );
706 fprintf( outfile, " struct opt_header {\n" );
707 fprintf( outfile, " short Magic;\n" );
708 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
709 fprintf( outfile, " int SizeOfCode;\n" );
710 fprintf( outfile, " int SizeOfInitializedData;\n" );
711 fprintf( outfile, " int SizeOfUninitializedData;\n" );
712 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
713 fprintf( outfile, " void *BaseOfCode;\n" );
714 fprintf( outfile, " void *BaseOfData;\n" );
715 fprintf( outfile, " void *ImageBase;\n" );
716 fprintf( outfile, " int SectionAlignment;\n" );
717 fprintf( outfile, " int FileAlignment;\n" );
718 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
719 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
720 fprintf( outfile, " short MajorImageVersion;\n" );
721 fprintf( outfile, " short MinorImageVersion;\n" );
722 fprintf( outfile, " short MajorSubsystemVersion;\n" );
723 fprintf( outfile, " short MinorSubsystemVersion;\n" );
724 fprintf( outfile, " int Win32VersionValue;\n" );
725 fprintf( outfile, " int SizeOfImage;\n" );
726 fprintf( outfile, " int SizeOfHeaders;\n" );
727 fprintf( outfile, " int CheckSum;\n" );
728 fprintf( outfile, " short Subsystem;\n" );
729 fprintf( outfile, " short DllCharacteristics;\n" );
730 fprintf( outfile, " int SizeOfStackReserve;\n" );
731 fprintf( outfile, " int SizeOfStackCommit;\n" );
732 fprintf( outfile, " int SizeOfHeapReserve;\n" );
733 fprintf( outfile, " int SizeOfHeapCommit;\n" );
734 fprintf( outfile, " int LoaderFlags;\n" );
735 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
736 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
737 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
738 fprintf( outfile, " } OptionalHeader;\n" );
739 fprintf( outfile, "} nt_header = {\n" );
740 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
742 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
743 fprintf( outfile, " 0, 0, 0, 0,\n" );
744 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
745 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
747 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
748 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
749 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
750 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
751 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
752 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
753 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
754 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
755 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
756 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
757 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
758 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
759 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
760 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
761 fprintf( outfile, " 0,\n" ); /* CheckSum */
762 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
763 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
764 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
765 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
766 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
767 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
768 fprintf( outfile, " {\n" );
769 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
770 exports_size ? "__wine_spec_exports" : "0", exports_size );
771 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
772 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
773 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
774 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
775 fprintf( outfile, " }\n }\n};\n\n" );
777 /* Output the DLL constructor */
779 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(dll_file_name) );
780 output_dll_init( outfile, constructor, NULL );
785 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
786 " extern void *__wine_dbg_register( char * const *, int );\n"
787 " __wine_dll_register( &nt_header, \"%s\" );\n"
789 constructor, dll_file_name );
793 /*******************************************************************
796 * Build a Win32 def file from a spec file.
798 void BuildDef32File(FILE *outfile)
805 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
808 fprintf(outfile, "LIBRARY %s\n\n", dll_file_name);
810 fprintf(outfile, "EXPORTS\n");
812 /* Output the exports and relay entry points */
814 for(i = 0; i < nb_entry_points; i++)
816 ORDDEF *odp = EntryPoints[i];
820 if (odp->flags & FLAG_REGISTER) continue;
821 if (odp->type == TYPE_STUB) continue;
823 if (odp->name) name = odp->name;
824 else if (odp->export_name) name = odp->export_name;
827 fprintf(outfile, " %s", name);
836 /* try to reduce output */
837 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
838 fprintf(outfile, "=%s", odp->link_name);
842 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
843 if (!kill_at) fprintf(outfile, "@%d", at_param);
844 if (odp->flags & FLAG_FORWARD)
846 fprintf(outfile, "=%s", odp->link_name);
848 else if (strcmp(name, odp->link_name)) /* try to reduce output */
850 fprintf(outfile, "=%s", odp->link_name);
851 if (!kill_at) fprintf(outfile, "@%d", at_param);
858 fprintf( outfile, " @%d", odp->ordinal );
859 if (!odp->name) fprintf( outfile, " NONAME" );
860 if (is_data) fprintf( outfile, " DATA" );
861 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
862 fprintf( outfile, "\n" );
867 /*******************************************************************
870 * Build the debugging channels source file.
872 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
879 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
882 output_standard_file_header( outfile );
883 nr_debug = output_debug( outfile );
886 fprintf( outfile, "/* no debug channels found for this module */\n" );
890 if (output_file_name)
892 if ((p = strrchr( output_file_name, '/' ))) p++;
893 prefix = xstrdup( p ? p : output_file_name );
894 if ((p = strchr( prefix, '.' ))) *p = 0;
895 strcpy( p, make_c_identifier(p) );
897 else prefix = xstrdup( "_" );
899 /* Output the DLL constructor */
903 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
904 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
906 "static void __asm__dummy_dll_init(void) {\n",
909 #if defined(__i386__)
910 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
911 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
912 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
913 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
914 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
915 #elif defined(__sparc__)
916 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
917 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
918 fprintf( outfile, " \"\\tnop\\n\"\n" );
919 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
920 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
921 fprintf( outfile, " \"\\tnop\\n\"\n" );
922 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
923 #elif defined(__PPC__)
924 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
925 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
926 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
927 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
928 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
930 #error You need to define the DLL constructor for your architecture
932 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
935 "\n#ifdef __GNUC__\n"
938 "void __wine_dbg_%s_init(void)\n"
940 " extern void *__wine_dbg_register( char * const *, int );\n"
941 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
942 "}\n", prefix, nr_debug );
944 "\n#ifdef __GNUC__\n"
947 "void __wine_dbg_%s_fini(void)\n"
949 " extern void __wine_dbg_unregister( void* );\n"
950 " __wine_dbg_unregister( debug_registration );\n"