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"
33 #include "wine/exception.h"
37 static int string_compare( const void *ptr1, const void *ptr2 )
39 const char * const *str1 = ptr1;
40 const char * const *str2 = ptr2;
41 return strcmp( *str1, *str2 );
45 /*******************************************************************
48 * Generate an internal name for an entry point. Used for stubs etc.
50 static const char *make_internal_name( const ORDDEF *odp, const char *prefix )
52 static char buffer[256];
53 if (odp->name || odp->export_name)
56 sprintf( buffer, "__wine_%s_%s_%s", prefix, dll_file_name,
57 odp->name ? odp->name : odp->export_name );
58 /* make sure name is a legal C identifier */
59 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
60 if (!*p) return buffer;
62 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(dll_file_name), odp->ordinal );
66 /*******************************************************************
69 * Assign ordinals to all entry points.
71 static void AssignOrdinals(void)
75 if ( !nb_names ) return;
77 /* start assigning from Base, or from 1 if no ordinal defined yet */
78 if (Base == MAX_ORDINALS) Base = 1;
79 for (i = 0, ordinal = Base; i < nb_names; i++)
81 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
82 while (Ordinals[ordinal]) ordinal++;
83 if (ordinal >= MAX_ORDINALS)
85 current_line = Names[i]->lineno;
86 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
88 Names[i]->ordinal = ordinal;
89 Ordinals[ordinal] = Names[i];
91 if (ordinal > Limit) Limit = ordinal;
95 /*******************************************************************
98 * Output the debug channels.
100 static int output_debug( FILE *outfile )
104 if (!nb_debug_channels) return 0;
105 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
107 for (i = 0; i < nb_debug_channels; i++)
108 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
109 debug_channels[i], debug_channels[i] );
111 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
112 for (i = 0; i < nb_debug_channels; i++)
114 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
115 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
117 fprintf( outfile, "\n};\n\n" );
118 fprintf( outfile, "static void *debug_registration;\n\n" );
120 return nb_debug_channels;
124 /*******************************************************************
127 * Output the export table for a Win32 module.
129 static int output_exports( FILE *outfile, int nr_exports )
131 int i, fwd_size = 0, total_size = 0;
133 if (!nr_exports) return 0;
135 fprintf( outfile, "asm(\".data\\n\"\n" );
136 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
137 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
139 /* export directory header */
141 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
142 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
143 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
144 fprintf( outfile, " \"\\t.long " __ASM_NAME("dllname") "\\n\"\n" ); /* Name */
145 fprintf( outfile, " \"\\t.long %d\\n\"\n", Base ); /* Base */
146 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
147 fprintf( outfile, " \"\\t.long %d\\n\"\n", nb_names ); /* NumberOfNames */
148 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
151 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
152 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
156 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
157 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
159 total_size += 10 * sizeof(int);
161 /* output the function pointers */
163 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
164 for (i = Base; i <= Limit; i++)
166 ORDDEF *odp = Ordinals[i];
167 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
168 else switch(odp->type)
174 if (!(odp->flags & FLAG_FORWARD))
176 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
177 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
181 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
182 fwd_size += strlen(odp->link_name) + 1;
186 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
192 total_size += (Limit - Base + 1) * sizeof(int);
196 /* output the function name pointers */
200 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
201 for (i = 0; i < nb_names; i++)
203 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
204 namepos += strlen(Names[i]->name) + 1;
206 total_size += nb_names * sizeof(int);
208 /* output the function names */
210 fprintf( outfile, " \"\\t.text\\n\"\n" );
211 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
212 for (i = 0; i < nb_names; i++)
213 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
214 fprintf( outfile, " \"\\t.data\\n\"\n" );
216 /* output the function ordinals */
218 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
219 for (i = 0; i < nb_names; i++)
221 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", Names[i]->ordinal - Base );
223 total_size += nb_names * sizeof(short);
226 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
227 total_size += sizeof(short);
231 /* output forward strings */
235 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
236 for (i = Base; i <= Limit; i++)
238 ORDDEF *odp = Ordinals[i];
239 if (odp && (odp->flags & FLAG_FORWARD))
240 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
242 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
243 total_size += (fwd_size + 3) & ~3;
250 for (i = Base; i <= Limit; i++)
252 ORDDEF *odp = Ordinals[i];
253 unsigned int j, args, mask = 0;
256 /* skip non-existent entry points */
257 if (!odp) goto ignore;
258 /* skip non-functions */
259 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
260 /* skip norelay and forward entry points */
261 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
263 for (j = 0; odp->u.func.arg_types[j]; j++)
265 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
266 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
268 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
270 name = odp->link_name;
271 args = strlen(odp->u.func.arg_types) * sizeof(int);
272 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, "regs" );
277 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
278 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
279 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
282 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
283 fprintf( outfile, " \"\\tret\\n\"\n" );
284 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
285 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
293 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
297 fprintf( outfile, " \"\\t.text\\n\"\n" );
298 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
299 fprintf( outfile, ");\n\n" );
305 /*******************************************************************
308 * Output the functions for stub entry points
310 static void output_stub_funcs( FILE *outfile )
314 for (i = 0; i < nb_entry_points; i++)
316 ORDDEF *odp = EntryPoints[i];
317 if (odp->type != TYPE_STUB) continue;
318 fprintf( outfile, "#ifdef __GNUC__\n" );
319 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
320 fprintf( outfile, "#endif\n\n" );
321 fprintf( outfile, "struct exc_record {\n" );
322 fprintf( outfile, " unsigned int code, flags;\n" );
323 fprintf( outfile, " void *rec, *addr;\n" );
324 fprintf( outfile, " unsigned int params;\n" );
325 fprintf( outfile, " const void *info[15];\n" );
326 fprintf( outfile, "};\n\n" );
327 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
328 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
329 fprintf( outfile, " struct exc_record rec;\n" );
330 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
331 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
332 fprintf( outfile, " rec.rec = 0;\n" );
333 fprintf( outfile, " rec.params = 2;\n" );
334 fprintf( outfile, " rec.info[0] = dllname;\n" );
335 fprintf( outfile, " rec.info[1] = func;\n" );
336 fprintf( outfile, "#ifdef __GNUC__\n" );
337 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
338 fprintf( outfile, "#else\n" );
339 fprintf( outfile, " rec.addr = 0;\n" );
340 fprintf( outfile, "#endif\n" );
341 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
345 for (i = 0; i < nb_entry_points; i++)
347 ORDDEF *odp = EntryPoints[i];
348 if (odp->type != TYPE_STUB) continue;
349 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
351 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
352 else if (odp->export_name)
353 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
355 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
360 /*******************************************************************
361 * output_register_funcs
363 * Output the functions for register entry points
365 static void output_register_funcs( FILE *outfile )
370 for (i = 0; i < nb_entry_points; i++)
372 ORDDEF *odp = EntryPoints[i];
373 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
374 if (!(odp->flags & FLAG_REGISTER)) continue;
375 name = make_internal_name( odp, "regs" );
377 "asm(\".align %d\\n\\t\"\n"
378 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
379 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
380 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
381 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
382 " \".byte %d,%d\");\n",
384 name, name, odp->link_name,
385 strlen(odp->u.func.arg_types) * sizeof(int),
386 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
391 /*******************************************************************
394 * Output code for calling a dll constructor and destructor.
396 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
398 fprintf( outfile, "#ifndef __GNUC__\n" );
399 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
400 fprintf( outfile, "#endif\n" );
402 #if defined(__i386__)
405 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
406 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
407 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
411 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
412 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
413 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
415 #elif defined(__sparc__)
418 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
419 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
420 fprintf( outfile, " \"\\tnop\\n\"\n" );
421 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
425 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
426 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
427 fprintf( outfile, " \"\\tnop\\n\"\n" );
428 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
430 #elif defined(__PPC__)
433 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
434 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
435 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
439 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
440 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
441 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
444 #error You need to define the DLL constructor for your architecture
446 fprintf( outfile, "#ifndef __GNUC__\n" );
447 fprintf( outfile, "}\n" );
448 fprintf( outfile, "#endif\n" );
452 /*******************************************************************
455 * Build a Win32 C file from a spec file.
457 void BuildSpec32File( FILE *outfile )
459 int exports_size = 0;
460 int nr_exports, nr_imports, nr_resources;
461 int characteristics, subsystem;
463 char constructor[100];
465 #ifdef HAVE_GETPAGESIZE
466 page_size = getpagesize();
467 #elif defined(__svr4__)
468 page_size = sysconf(_SC_PAGESIZE);
469 #elif defined(_WINDOWS)
473 page_size = si.dwPageSize;
476 # error Cannot get the page size on this platform
480 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
483 output_standard_file_header( outfile );
485 /* Reserve some space for the PE header */
487 fprintf( outfile, "extern char pe_header[];\n" );
488 fprintf( outfile, "#ifndef __GNUC__\n" );
489 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
490 fprintf( outfile, "#endif\n" );
491 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
492 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
493 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
494 fprintf( outfile, "#ifndef __GNUC__\n" );
495 fprintf( outfile, "}\n" );
496 fprintf( outfile, "#endif\n" );
498 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", dll_file_name );
499 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
502 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
504 fprintf( outfile, "#define __stdcall\n\n" );
509 /* Output the stub functions */
511 output_stub_funcs( outfile );
513 fprintf( outfile, "#ifndef __GNUC__\n" );
514 fprintf( outfile, "static void __asm__dummy(void) {\n" );
515 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
517 /* Output code for all register functions */
519 output_register_funcs( outfile );
521 /* Output the exports and relay entry points */
523 exports_size = output_exports( outfile, nr_exports );
525 fprintf( outfile, "#ifndef __GNUC__\n" );
526 fprintf( outfile, "}\n" );
527 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
530 /* Output the DLL imports */
532 nr_imports = output_imports( outfile );
534 /* Output the resources */
536 nr_resources = output_resources( outfile );
538 /* Output LibMain function */
540 characteristics = subsystem = 0;
544 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
547 fprintf( outfile, "#ifdef __GNUC__\n" );
548 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
549 fprintf( outfile, "#else\n" );
550 fprintf( outfile, "extern void DllMain();\n" );
551 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
552 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
553 fprintf( outfile, "#endif\n" );
555 characteristics = IMAGE_FILE_DLL;
557 case SPEC_MODE_GUIEXE:
558 if (!init_func) init_func = "WinMain";
560 "\ntypedef struct {\n"
561 " unsigned int cb;\n"
562 " char *lpReserved, *lpDesktop, *lpTitle;\n"
563 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
564 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
565 " unsigned short wShowWindow, cbReserved2;\n"
566 " char *lpReserved2;\n"
567 " void *hStdInput, *hStdOutput, *hStdError;\n"
571 "extern int __stdcall %s(void *,void *,char *,int);\n"
572 "extern char * __stdcall GetCommandLineA(void);\n"
573 "extern void * __stdcall GetModuleHandleA(char *);\n"
574 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
575 "extern void __stdcall ExitProcess(unsigned int);\n"
576 "static void __wine_exe_main(void)\n"
578 " extern int __wine_main_argc;\n"
579 " extern char **__wine_main_argv;\n"
580 " STARTUPINFOA info;\n"
581 " char *cmdline = GetCommandLineA();\n"
582 " int bcount=0, in_quotes=0;\n"
583 " while (*cmdline) {\n"
584 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
585 " else if (*cmdline=='\\\\') bcount++;\n"
586 " else if (*cmdline=='\\\"') {\n"
587 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
593 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
594 " GetStartupInfoA( &info );\n"
595 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
596 " _ARGC = __wine_main_argc;\n"
597 " _ARGV = __wine_main_argv;\n"
598 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
599 "}\n\n", init_func, init_func );
600 init_func = "__wine_exe_main";
601 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
603 case SPEC_MODE_GUIEXE_UNICODE:
604 if (!init_func) init_func = "WinMain";
606 "\ntypedef unsigned short WCHAR;\n"
608 " unsigned int cb;\n"
609 " char *lpReserved, *lpDesktop, *lpTitle;\n"
610 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
611 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
612 " unsigned short wShowWindow, cbReserved2;\n"
613 " char *lpReserved2;\n"
614 " void *hStdInput, *hStdOutput, *hStdError;\n"
618 "extern int __stdcall %s(void *,void *,char *,int);\n"
619 "extern char * __stdcall GetCommandLineA(void);\n"
620 "extern void * __stdcall GetModuleHandleA(char *);\n"
621 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
622 "extern void __stdcall ExitProcess(unsigned int);\n"
623 "static void __wine_exe_main(void)\n"
625 " extern int __wine_main_argc;\n"
626 " extern WCHAR **__wine_main_wargv;\n"
627 " STARTUPINFOA info;\n"
628 " char *cmdline = GetCommandLineA();\n"
629 " int bcount=0, in_quotes=0;\n"
630 " while (*cmdline) {\n"
631 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
632 " else if (*cmdline=='\\\\') bcount++;\n"
633 " else if (*cmdline=='\\\"') {\n"
634 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
640 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
641 " GetStartupInfoA( &info );\n"
642 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
643 " _ARGC = __wine_main_argc;\n"
644 " _ARGV = __wine_main_wargv;\n"
645 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
646 "}\n\n", init_func, init_func );
647 init_func = "__wine_exe_main";
648 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
650 case SPEC_MODE_CUIEXE:
651 if (!init_func) init_func = "main";
655 "extern void __stdcall ExitProcess(int);\n"
656 "static void __wine_exe_main(void)\n"
658 " extern int %s( int argc, char *argv[] );\n"
659 " extern int __wine_main_argc;\n"
660 " extern char **__wine_main_argv;\n"
661 " _ARGC = __wine_main_argc;\n"
662 " _ARGV = __wine_main_argv;\n"
663 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
664 "}\n\n", init_func, init_func );
665 init_func = "__wine_exe_main";
666 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
668 case SPEC_MODE_CUIEXE_UNICODE:
669 if (!init_func) init_func = "wmain";
671 "\ntypedef unsigned short WCHAR;\n"
674 "extern void __stdcall ExitProcess(int);\n"
675 "static void __wine_exe_main(void)\n"
677 " extern int %s( int argc, WCHAR *argv[] );\n"
678 " extern int __wine_main_argc;\n"
679 " extern WCHAR **__wine_main_wargv;\n"
680 " _ARGC = __wine_main_argc;\n"
681 " _ARGV = __wine_main_wargv;\n"
682 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
683 "}\n\n", init_func, init_func );
684 init_func = "__wine_exe_main";
685 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
689 /* Output the NT header */
691 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
692 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
693 fprintf( outfile, " int Signature;\n" );
694 fprintf( outfile, " struct file_header {\n" );
695 fprintf( outfile, " short Machine;\n" );
696 fprintf( outfile, " short NumberOfSections;\n" );
697 fprintf( outfile, " int TimeDateStamp;\n" );
698 fprintf( outfile, " void *PointerToSymbolTable;\n" );
699 fprintf( outfile, " int NumberOfSymbols;\n" );
700 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
701 fprintf( outfile, " short Characteristics;\n" );
702 fprintf( outfile, " } FileHeader;\n" );
703 fprintf( outfile, " struct opt_header {\n" );
704 fprintf( outfile, " short Magic;\n" );
705 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
706 fprintf( outfile, " int SizeOfCode;\n" );
707 fprintf( outfile, " int SizeOfInitializedData;\n" );
708 fprintf( outfile, " int SizeOfUninitializedData;\n" );
709 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
710 fprintf( outfile, " void *BaseOfCode;\n" );
711 fprintf( outfile, " void *BaseOfData;\n" );
712 fprintf( outfile, " void *ImageBase;\n" );
713 fprintf( outfile, " int SectionAlignment;\n" );
714 fprintf( outfile, " int FileAlignment;\n" );
715 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
716 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
717 fprintf( outfile, " short MajorImageVersion;\n" );
718 fprintf( outfile, " short MinorImageVersion;\n" );
719 fprintf( outfile, " short MajorSubsystemVersion;\n" );
720 fprintf( outfile, " short MinorSubsystemVersion;\n" );
721 fprintf( outfile, " int Win32VersionValue;\n" );
722 fprintf( outfile, " int SizeOfImage;\n" );
723 fprintf( outfile, " int SizeOfHeaders;\n" );
724 fprintf( outfile, " int CheckSum;\n" );
725 fprintf( outfile, " short Subsystem;\n" );
726 fprintf( outfile, " short DllCharacteristics;\n" );
727 fprintf( outfile, " int SizeOfStackReserve;\n" );
728 fprintf( outfile, " int SizeOfStackCommit;\n" );
729 fprintf( outfile, " int SizeOfHeapReserve;\n" );
730 fprintf( outfile, " int SizeOfHeapCommit;\n" );
731 fprintf( outfile, " int LoaderFlags;\n" );
732 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
733 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
734 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
735 fprintf( outfile, " } OptionalHeader;\n" );
736 fprintf( outfile, "} nt_header = {\n" );
737 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
739 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
740 fprintf( outfile, " 0, 0, 0, 0,\n" );
741 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
742 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
744 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
745 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
746 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
747 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
748 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
749 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
750 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
751 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
752 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
753 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
754 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
755 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
756 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
757 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
758 fprintf( outfile, " 0,\n" ); /* CheckSum */
759 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
760 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
761 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
762 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
763 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
764 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
765 fprintf( outfile, " {\n" );
766 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
767 exports_size ? "__wine_spec_exports" : "0", exports_size );
768 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
769 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
770 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
771 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
772 fprintf( outfile, " }\n }\n};\n\n" );
774 /* Output the DLL constructor */
776 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(dll_file_name) );
777 output_dll_init( outfile, constructor, NULL );
782 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
783 " extern void *__wine_dbg_register( char * const *, int );\n"
784 " __wine_dll_register( &nt_header, \"%s\" );\n"
786 constructor, dll_file_name );
790 /*******************************************************************
793 * Build a Win32 def file from a spec file.
795 void BuildDef32File(FILE *outfile)
802 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
805 fprintf(outfile, "LIBRARY %s\n\n", dll_file_name);
807 fprintf(outfile, "EXPORTS\n");
809 /* Output the exports and relay entry points */
811 for(i = 0; i < nb_entry_points; i++)
813 ORDDEF *odp = EntryPoints[i];
817 if (odp->flags & FLAG_REGISTER) continue;
818 if (odp->type == TYPE_STUB) continue;
820 if (odp->name) name = odp->name;
821 else if (odp->export_name) name = odp->export_name;
824 fprintf(outfile, " %s", name);
833 /* try to reduce output */
834 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
835 fprintf(outfile, "=%s", odp->link_name);
839 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
840 if (!kill_at) fprintf(outfile, "@%d", at_param);
841 if (odp->flags & FLAG_FORWARD)
843 fprintf(outfile, "=%s", odp->link_name);
845 else if (strcmp(name, odp->link_name)) /* try to reduce output */
847 fprintf(outfile, "=%s", odp->link_name);
848 if (!kill_at) fprintf(outfile, "@%d", at_param);
855 fprintf(outfile, " @%d%s%s\n", odp->ordinal,
856 odp->name ? "" : " NONAME", is_data ? " DATA" : "" );
861 /*******************************************************************
864 * Build the debugging channels source file.
866 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
873 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
876 output_standard_file_header( outfile );
877 nr_debug = output_debug( outfile );
880 fprintf( outfile, "/* no debug channels found for this module */\n" );
884 if (output_file_name)
886 if ((p = strrchr( output_file_name, '/' ))) p++;
887 prefix = xstrdup( p ? p : output_file_name );
888 if ((p = strchr( prefix, '.' ))) *p = 0;
889 strcpy( p, make_c_identifier(p) );
891 else prefix = xstrdup( "_" );
893 /* Output the DLL constructor */
897 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
898 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
900 "static void __asm__dummy_dll_init(void) {\n",
903 #if defined(__i386__)
904 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
905 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
906 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
907 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
908 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
909 #elif defined(__sparc__)
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, " \"\\tnop\\n\"\n" );
913 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
914 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
915 fprintf( outfile, " \"\\tnop\\n\"\n" );
916 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
917 #elif defined(__PPC__)
918 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
919 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
920 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
921 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
922 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
924 #error You need to define the DLL constructor for your architecture
926 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
929 "\n#ifdef __GNUC__\n"
932 "void __wine_dbg_%s_init(void)\n"
934 " extern void *__wine_dbg_register( char * const *, int );\n"
935 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
936 "}\n", prefix, nr_debug );
938 "\n#ifdef __GNUC__\n"
941 "void __wine_dbg_%s_fini(void)\n"
943 " extern void __wine_dbg_unregister( void* );\n"
944 " __wine_dbg_unregister( debug_registration );\n"