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, DLLFileName,
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(DLLFileName), 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;
134 if (!nr_exports) return 0;
136 fprintf( outfile, "asm(\".data\\n\"\n" );
137 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
138 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
140 /* export directory header */
142 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
143 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
144 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
145 fprintf( outfile, " \"\\t.long " __ASM_NAME("dllname") "\\n\"\n" ); /* Name */
146 fprintf( outfile, " \"\\t.long %d\\n\"\n", Base ); /* Base */
147 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
148 fprintf( outfile, " \"\\t.long %d\\n\"\n", nb_names ); /* NumberOfNames */
149 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
152 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
153 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
157 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
158 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
160 total_size += 10 * sizeof(int);
162 /* output the function pointers */
164 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
165 for (i = Base; i <= Limit; i++)
167 ORDDEF *odp = Ordinals[i];
168 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
169 else switch(odp->type)
172 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", odp->link_name );
177 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
178 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
181 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
184 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "var" ) );
187 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
188 fwd_size += strlen(odp->link_name) + 1;
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" 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.short %d\\n\"\n", Names[i]->ordinal - Base );
225 total_size += nb_names * sizeof(short);
228 fprintf( outfile, " \"\\t.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->type == TYPE_FORWARD)
242 fprintf( outfile, " \"\\t" 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 entry points */
263 if (odp->flags & FLAG_NORELAY) 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.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" );
300 /* output __wine_dllexport symbols */
302 for (i = 0; i < nb_names; i++)
304 /* check for invalid characters in the name */
305 for (p = Names[i]->name; *p; p++)
306 if (!isalnum(*p) && *p != '_' && *p != '.') break;
308 fprintf( outfile, " \"\\t.globl " __ASM_NAME("__wine_dllexport_%s_%d_%s") "\\n\"\n",
309 make_c_identifier(DLLFileName), i, Names[i]->name );
310 fprintf( outfile, " \"" __ASM_NAME("__wine_dllexport_%s_%d_%s") ":\\n\"\n",
311 make_c_identifier(DLLFileName), i, Names[i]->name );
314 /* output ordinal exports */
316 for (i = 0; i < nb_entry_points; i++)
318 ORDDEF *odp = EntryPoints[i];
320 if (odp->name || !odp->export_name) continue;
321 /* check for invalid characters in the name */
322 for (p = odp->export_name; *p; p++)
323 if (!isalnum(*p) && *p != '_' && *p != '.') break;
325 fprintf( outfile, " \"\\t.globl " __ASM_NAME("__wine_ordexport_%s_%d_%s") "\\n\"\n",
326 make_c_identifier(DLLFileName), odp->ordinal, odp->export_name );
327 fprintf( outfile, " \"" __ASM_NAME("__wine_ordexport_%s_%d_%s") ":\\n\"\n",
328 make_c_identifier(DLLFileName), odp->ordinal, odp->export_name );
330 fprintf( outfile, " \"\\t.long 0xffffffff\\n\"\n" );
332 /* output variables */
334 for (i = 0; i < nb_entry_points; i++)
336 ORDDEF *odp = EntryPoints[i];
337 if (odp->type == TYPE_VARIABLE)
340 fprintf( outfile, " \"%s:\\n\"\n", make_internal_name( odp, "var" ) );
341 fprintf( outfile, " \"\\t.long " );
342 for (j = 0; j < odp->u.var.n_values; j++)
344 fprintf( outfile, "0x%08x", odp->u.var.values[j] );
345 if (j < odp->u.var.n_values-1) fputc( ',', outfile );
347 fprintf( outfile, "\\n\"\n" );
351 fprintf( outfile, " \"\\t.text\\n\"\n" );
352 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
353 fprintf( outfile, ");\n\n" );
359 /*******************************************************************
362 * Output the functions for stub entry points
364 static void output_stub_funcs( FILE *outfile )
368 for (i = 0; i < nb_entry_points; i++)
370 ORDDEF *odp = EntryPoints[i];
371 if (odp->type != TYPE_STUB) continue;
372 fprintf( outfile, "#ifdef __GNUC__\n" );
373 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
374 fprintf( outfile, "#endif\n\n" );
375 fprintf( outfile, "struct exc_record {\n" );
376 fprintf( outfile, " unsigned int code, flags;\n" );
377 fprintf( outfile, " void *rec, *addr;\n" );
378 fprintf( outfile, " unsigned int params;\n" );
379 fprintf( outfile, " const void *info[15];\n" );
380 fprintf( outfile, "};\n\n" );
381 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
382 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
383 fprintf( outfile, " struct exc_record rec;\n" );
384 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
385 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
386 fprintf( outfile, " rec.rec = 0;\n" );
387 fprintf( outfile, " rec.params = 2;\n" );
388 fprintf( outfile, " rec.info[0] = dllname;\n" );
389 fprintf( outfile, " rec.info[1] = func;\n" );
390 fprintf( outfile, "#ifdef __GNUC__\n" );
391 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
392 fprintf( outfile, "#else\n" );
393 fprintf( outfile, " rec.addr = 0;\n" );
394 fprintf( outfile, "#endif\n" );
395 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
399 for (i = 0; i < nb_entry_points; i++)
401 ORDDEF *odp = EntryPoints[i];
402 if (odp->type != TYPE_STUB) continue;
403 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
405 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
406 else if (odp->export_name)
407 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
409 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
414 /*******************************************************************
415 * output_register_funcs
417 * Output the functions for register entry points
419 static void output_register_funcs( FILE *outfile )
424 for (i = 0; i < nb_entry_points; i++)
426 ORDDEF *odp = EntryPoints[i];
427 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
428 if (!(odp->flags & FLAG_REGISTER)) continue;
429 name = make_internal_name( odp, "regs" );
431 "asm(\".align %d\\n\\t\"\n"
432 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
433 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
434 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
435 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
436 " \".byte %d,%d\");\n",
438 name, name, odp->link_name,
439 strlen(odp->u.func.arg_types) * sizeof(int),
440 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
445 /*******************************************************************
448 * Output code for calling a dll constructor and destructor.
450 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
452 fprintf( outfile, "#ifndef __GNUC__\n" );
453 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
454 fprintf( outfile, "#endif\n" );
456 #if defined(__i386__)
459 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
460 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
461 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
465 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
466 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
467 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
469 #elif defined(__sparc__)
472 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
473 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
474 fprintf( outfile, " \"\\tnop\\n\"\n" );
475 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
479 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
480 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
481 fprintf( outfile, " \"\\tnop\\n\"\n" );
482 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
484 #elif defined(__PPC__)
487 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
488 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
489 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
493 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
494 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
495 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
498 #error You need to define the DLL constructor for your architecture
500 fprintf( outfile, "#ifndef __GNUC__\n" );
501 fprintf( outfile, "}\n" );
502 fprintf( outfile, "#endif\n" );
506 /*******************************************************************
509 * Build a Win32 C file from a spec file.
511 void BuildSpec32File( FILE *outfile )
513 int exports_size = 0;
514 int nr_exports, nr_imports, nr_resources;
515 int characteristics, subsystem;
517 char constructor[100];
519 #ifdef HAVE_GETPAGESIZE
520 page_size = getpagesize();
521 #elif defined(__svr4__)
522 page_size = sysconf(_SC_PAGESIZE);
523 #elif defined(_WINDOWS)
527 page_size = si.dwPageSize;
530 # error Cannot get the page size on this platform
534 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
537 output_standard_file_header( outfile );
539 /* Reserve some space for the PE header */
541 fprintf( outfile, "extern char pe_header[];\n" );
542 fprintf( outfile, "#ifndef __GNUC__\n" );
543 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
544 fprintf( outfile, "#endif\n" );
545 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
546 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
547 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
548 fprintf( outfile, "#ifndef __GNUC__\n" );
549 fprintf( outfile, "}\n" );
550 fprintf( outfile, "#endif\n" );
552 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLFileName );
553 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
556 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
558 fprintf( outfile, "#define __stdcall\n\n" );
563 /* Output the stub functions */
565 output_stub_funcs( outfile );
567 fprintf( outfile, "#ifndef __GNUC__\n" );
568 fprintf( outfile, "static void __asm__dummy(void) {\n" );
569 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
571 /* Output code for all register functions */
573 output_register_funcs( outfile );
575 /* Output the exports and relay entry points */
577 exports_size = output_exports( outfile, nr_exports );
579 fprintf( outfile, "#ifndef __GNUC__\n" );
580 fprintf( outfile, "}\n" );
581 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
584 /* Output the DLL imports */
586 nr_imports = output_imports( outfile );
588 /* Output the resources */
590 nr_resources = output_resources( outfile );
592 /* Output LibMain function */
594 characteristics = subsystem = 0;
598 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
601 fprintf( outfile, "#ifdef __GNUC__\n" );
602 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
603 fprintf( outfile, "#else\n" );
604 fprintf( outfile, "extern void DllMain();\n" );
605 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
606 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
607 fprintf( outfile, "#endif\n" );
609 characteristics = IMAGE_FILE_DLL;
611 case SPEC_MODE_GUIEXE:
612 if (!init_func) init_func = "WinMain";
614 "\ntypedef struct {\n"
615 " unsigned int cb;\n"
616 " char *lpReserved, *lpDesktop, *lpTitle;\n"
617 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
618 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
619 " unsigned short wShowWindow, cbReserved2;\n"
620 " char *lpReserved2;\n"
621 " void *hStdInput, *hStdOutput, *hStdError;\n"
625 "extern int __stdcall %s(void *,void *,char *,int);\n"
626 "extern char * __stdcall GetCommandLineA(void);\n"
627 "extern void * __stdcall GetModuleHandleA(char *);\n"
628 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
629 "extern void __stdcall ExitProcess(unsigned int);\n"
630 "static void __wine_exe_main(void)\n"
632 " extern int __wine_main_argc;\n"
633 " extern char **__wine_main_argv;\n"
634 " STARTUPINFOA info;\n"
635 " char *cmdline = GetCommandLineA();\n"
636 " int bcount=0, in_quotes=0;\n"
637 " while (*cmdline) {\n"
638 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
639 " else if (*cmdline=='\\\\') bcount++;\n"
640 " else if (*cmdline=='\\\"') {\n"
641 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
647 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
648 " GetStartupInfoA( &info );\n"
649 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
650 " _ARGC = __wine_main_argc;\n"
651 " _ARGV = __wine_main_argv;\n"
652 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
653 "}\n\n", init_func, init_func );
654 init_func = "__wine_exe_main";
655 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
657 case SPEC_MODE_GUIEXE_UNICODE:
658 if (!init_func) init_func = "WinMain";
660 "\ntypedef unsigned short WCHAR;\n"
662 " unsigned int cb;\n"
663 " char *lpReserved, *lpDesktop, *lpTitle;\n"
664 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
665 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
666 " unsigned short wShowWindow, cbReserved2;\n"
667 " char *lpReserved2;\n"
668 " void *hStdInput, *hStdOutput, *hStdError;\n"
672 "extern int __stdcall %s(void *,void *,char *,int);\n"
673 "extern char * __stdcall GetCommandLineA(void);\n"
674 "extern void * __stdcall GetModuleHandleA(char *);\n"
675 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
676 "extern void __stdcall ExitProcess(unsigned int);\n"
677 "static void __wine_exe_main(void)\n"
679 " extern int __wine_main_argc;\n"
680 " extern WCHAR **__wine_main_wargv;\n"
681 " STARTUPINFOA info;\n"
682 " char *cmdline = GetCommandLineA();\n"
683 " int bcount=0, in_quotes=0;\n"
684 " while (*cmdline) {\n"
685 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
686 " else if (*cmdline=='\\\\') bcount++;\n"
687 " else if (*cmdline=='\\\"') {\n"
688 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
694 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
695 " GetStartupInfoA( &info );\n"
696 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
697 " _ARGC = __wine_main_argc;\n"
698 " _ARGV = __wine_main_wargv;\n"
699 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
700 "}\n\n", init_func, init_func );
701 init_func = "__wine_exe_main";
702 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
704 case SPEC_MODE_CUIEXE:
705 if (!init_func) init_func = "main";
709 "extern void __stdcall ExitProcess(int);\n"
710 "static void __wine_exe_main(void)\n"
712 " extern int %s( int argc, char *argv[] );\n"
713 " extern int __wine_main_argc;\n"
714 " extern char **__wine_main_argv;\n"
715 " _ARGC = __wine_main_argc;\n"
716 " _ARGV = __wine_main_argv;\n"
717 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
718 "}\n\n", init_func, init_func );
719 init_func = "__wine_exe_main";
720 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
722 case SPEC_MODE_CUIEXE_UNICODE:
723 if (!init_func) init_func = "wmain";
725 "\ntypedef unsigned short WCHAR;\n"
728 "extern void __stdcall ExitProcess(int);\n"
729 "static void __wine_exe_main(void)\n"
731 " extern int %s( int argc, WCHAR *argv[] );\n"
732 " extern int __wine_main_argc;\n"
733 " extern WCHAR **__wine_main_wargv;\n"
734 " _ARGC = __wine_main_argc;\n"
735 " _ARGV = __wine_main_wargv;\n"
736 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
737 "}\n\n", init_func, init_func );
738 init_func = "__wine_exe_main";
739 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
743 /* Output the NT header */
745 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
746 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
747 fprintf( outfile, " int Signature;\n" );
748 fprintf( outfile, " struct file_header {\n" );
749 fprintf( outfile, " short Machine;\n" );
750 fprintf( outfile, " short NumberOfSections;\n" );
751 fprintf( outfile, " int TimeDateStamp;\n" );
752 fprintf( outfile, " void *PointerToSymbolTable;\n" );
753 fprintf( outfile, " int NumberOfSymbols;\n" );
754 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
755 fprintf( outfile, " short Characteristics;\n" );
756 fprintf( outfile, " } FileHeader;\n" );
757 fprintf( outfile, " struct opt_header {\n" );
758 fprintf( outfile, " short Magic;\n" );
759 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
760 fprintf( outfile, " int SizeOfCode;\n" );
761 fprintf( outfile, " int SizeOfInitializedData;\n" );
762 fprintf( outfile, " int SizeOfUninitializedData;\n" );
763 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
764 fprintf( outfile, " void *BaseOfCode;\n" );
765 fprintf( outfile, " void *BaseOfData;\n" );
766 fprintf( outfile, " void *ImageBase;\n" );
767 fprintf( outfile, " int SectionAlignment;\n" );
768 fprintf( outfile, " int FileAlignment;\n" );
769 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
770 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
771 fprintf( outfile, " short MajorImageVersion;\n" );
772 fprintf( outfile, " short MinorImageVersion;\n" );
773 fprintf( outfile, " short MajorSubsystemVersion;\n" );
774 fprintf( outfile, " short MinorSubsystemVersion;\n" );
775 fprintf( outfile, " int Win32VersionValue;\n" );
776 fprintf( outfile, " int SizeOfImage;\n" );
777 fprintf( outfile, " int SizeOfHeaders;\n" );
778 fprintf( outfile, " int CheckSum;\n" );
779 fprintf( outfile, " short Subsystem;\n" );
780 fprintf( outfile, " short DllCharacteristics;\n" );
781 fprintf( outfile, " int SizeOfStackReserve;\n" );
782 fprintf( outfile, " int SizeOfStackCommit;\n" );
783 fprintf( outfile, " int SizeOfHeapReserve;\n" );
784 fprintf( outfile, " int SizeOfHeapCommit;\n" );
785 fprintf( outfile, " int LoaderFlags;\n" );
786 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
787 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
788 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
789 fprintf( outfile, " } OptionalHeader;\n" );
790 fprintf( outfile, "} nt_header = {\n" );
791 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
793 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
794 fprintf( outfile, " 0, 0, 0, 0,\n" );
795 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
796 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
798 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
799 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
800 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
801 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
802 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
803 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
804 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
805 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
806 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
807 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
808 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
809 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
810 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
811 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
812 fprintf( outfile, " 0,\n" ); /* CheckSum */
813 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
814 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
815 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
816 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
817 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
818 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
819 fprintf( outfile, " {\n" );
820 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
821 exports_size ? "__wine_spec_exports" : "0", exports_size );
822 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
823 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
824 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
825 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
826 fprintf( outfile, " }\n }\n};\n\n" );
828 /* Output the DLL constructor */
830 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(DLLFileName) );
831 output_dll_init( outfile, constructor, NULL );
836 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
837 " extern void *__wine_dbg_register( char * const *, int );\n"
838 " __wine_dll_register( &nt_header, \"%s\" );\n"
840 constructor, DLLFileName );
844 /*******************************************************************
847 * Build a Win32 def file from a spec file.
849 void BuildDef32File(FILE *outfile)
856 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
859 fprintf(outfile, "LIBRARY %s\n\n", DLLFileName);
861 fprintf(outfile, "EXPORTS\n");
863 /* Output the exports and relay entry points */
865 for(i = 0; i < nb_entry_points; i++)
867 ORDDEF *odp = EntryPoints[i];
871 if (odp->flags & FLAG_REGISTER) continue;
872 if (odp->type == TYPE_STUB) continue;
874 if (odp->name) name = odp->name;
875 else if (odp->export_name) name = odp->export_name;
878 fprintf(outfile, " %s", name);
888 /* try to reduce output */
889 if(strcmp(name, odp->link_name))
890 fprintf(outfile, "=%s", odp->link_name);
894 #ifdef NEED_STDCALL_DECORATION
895 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
896 fprintf(outfile, "@%d", at_param);
897 #endif /* NEED_STDCALL_DECORATION */
898 /* try to reduce output */
899 if(strcmp(name, odp->link_name))
901 fprintf(outfile, "=%s", odp->link_name);
902 #ifdef NEED_STDCALL_DECORATION
903 fprintf(outfile, "@%d", at_param);
904 #endif /* NEED_STDCALL_DECORATION */
909 fprintf(outfile, "=%s", odp->link_name);
914 fprintf(outfile, " @%d%s%s\n", odp->ordinal,
915 odp->name ? "" : " NONAME", is_data ? " DATA" : "" );
920 /*******************************************************************
923 * Build the debugging channels source file.
925 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
930 while (*argv) parse_debug_channels( srcdir, *argv++ );
932 output_standard_file_header( outfile );
933 nr_debug = output_debug( outfile );
936 fprintf( outfile, "/* no debug channels found for this module */\n" );
940 if (output_file_name)
942 if ((p = strrchr( output_file_name, '/' ))) p++;
943 prefix = xstrdup( p ? p : output_file_name );
944 if ((p = strchr( prefix, '.' ))) *p = 0;
945 strcpy( p, make_c_identifier(p) );
947 else prefix = xstrdup( "_" );
949 /* Output the DLL constructor */
953 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
954 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
956 "static void __asm__dummy_dll_init(void) {\n",
959 #if defined(__i386__)
960 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
961 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
962 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
963 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
964 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
965 #elif defined(__sparc__)
966 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
967 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
968 fprintf( outfile, " \"\\tnop\\n\"\n" );
969 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
970 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
971 fprintf( outfile, " \"\\tnop\\n\"\n" );
972 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
973 #elif defined(__PPC__)
974 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
975 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
976 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
977 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
978 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
980 #error You need to define the DLL constructor for your architecture
982 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
985 "\n#ifdef __GNUC__\n"
988 "void __wine_dbg_%s_init(void)\n"
990 " extern void *__wine_dbg_register( char * const *, int );\n"
991 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
992 "}\n", prefix, nr_debug );
994 "\n#ifdef __GNUC__\n"
997 "void __wine_dbg_%s_fini(void)\n"
999 " extern void __wine_dbg_unregister( void* );\n"
1000 " __wine_dbg_unregister( debug_registration );\n"