Added Korean resources.
[wine.git] / tools / winebuild / spec32.c
blobc9760badf861b7ddd8639bedb2cf2ec3cb34120f
1 /*
2 * 32-bit spec files
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
32 #include "winbase.h"
33 #include "wine/exception.h"
34 #include "build.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 /*******************************************************************
46 * make_internal_name
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[0])
55 char *p;
56 sprintf( buffer, "__wine_%s_%s_%s", prefix, DLLName, odp->name );
57 /* make sure name is a legal C identifier */
58 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
59 if (!*p) return buffer;
61 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(DLLName), odp->ordinal );
62 return buffer;
65 /*******************************************************************
66 * AssignOrdinals
68 * Assign ordinals to all entry points.
70 static void AssignOrdinals(void)
72 int i, ordinal;
74 if ( !nb_names ) return;
76 /* start assigning from Base, or from 1 if no ordinal defined yet */
77 if (Base == MAX_ORDINALS) Base = 1;
78 for (i = 0, ordinal = Base; i < nb_names; i++)
80 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
81 while (Ordinals[ordinal]) ordinal++;
82 if (ordinal >= MAX_ORDINALS)
84 current_line = Names[i]->lineno;
85 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
87 Names[i]->ordinal = ordinal;
88 Ordinals[ordinal] = Names[i];
90 if (ordinal > Limit) Limit = ordinal;
94 /*******************************************************************
95 * output_debug
97 * Output the debug channels.
99 static int output_debug( FILE *outfile )
101 int i;
103 if (!nb_debug_channels) return 0;
104 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
106 for (i = 0; i < nb_debug_channels; i++)
107 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
108 debug_channels[i], debug_channels[i] );
110 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
111 for (i = 0; i < nb_debug_channels; i++)
113 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
114 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
116 fprintf( outfile, "\n};\n\n" );
117 fprintf( outfile, "static void *debug_registration;\n\n" );
119 return nb_debug_channels;
123 /*******************************************************************
124 * output_exports
126 * Output the export table for a Win32 module.
128 static int output_exports( FILE *outfile, int nr_exports )
130 int i, fwd_size = 0, total_size = 0;
131 char *p;
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, " \"" PREFIX "__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 " PREFIX "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 */
149 if (nb_names)
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 */
154 else
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)
170 case TYPE_EXTERN:
171 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n", odp->link_name );
172 break;
173 case TYPE_STDCALL:
174 case TYPE_VARARGS:
175 case TYPE_CDECL:
176 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n",
177 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
178 break;
179 case TYPE_STUB:
180 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "stub" ) );
181 break;
182 case TYPE_VARIABLE:
183 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "var" ) );
184 break;
185 case TYPE_FORWARD:
186 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
187 fwd_size += strlen(odp->link_name) + 1;
188 break;
189 default:
190 assert(0);
193 total_size += (Limit - Base + 1) * sizeof(int);
195 if (nb_names)
197 /* output the function name pointers */
199 int namepos = 0;
201 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
202 for (i = 0; i < nb_names; i++)
204 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
205 namepos += strlen(Names[i]->name) + 1;
207 total_size += nb_names * sizeof(int);
209 /* output the function names */
211 fprintf( outfile, " \"\\t.text 1\\n\"\n" );
212 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
213 for (i = 0; i < nb_names; i++)
214 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
215 fprintf( outfile, " \"\\t.data\\n\"\n" );
217 /* output the function ordinals */
219 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
220 for (i = 0; i < nb_names; i++)
222 fprintf( outfile, " \"\\t.short %d\\n\"\n", Names[i]->ordinal - Base );
224 total_size += nb_names * sizeof(short);
225 if (nb_names % 2)
227 fprintf( outfile, " \"\\t.short 0\\n\"\n" );
228 total_size += sizeof(short);
232 /* output forward strings */
234 if (fwd_size)
236 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
237 for (i = Base; i <= Limit; i++)
239 ORDDEF *odp = Ordinals[i];
240 if (odp && odp->type == TYPE_FORWARD)
241 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
243 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
244 total_size += (fwd_size + 3) & ~3;
247 /* output relays */
249 if (debugging)
251 for (i = Base; i <= Limit; i++)
253 ORDDEF *odp = Ordinals[i];
254 unsigned int j, args, mask = 0;
255 const char *name;
257 /* skip non-existent entry points */
258 if (!odp) goto ignore;
259 /* skip non-functions */
260 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
261 /* skip norelay entry points */
262 if (odp->flags & FLAG_NORELAY) goto ignore;
264 for (j = 0; odp->u.func.arg_types[j]; j++)
266 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
267 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
269 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
271 name = odp->link_name;
272 args = strlen(odp->u.func.arg_types) * sizeof(int);
273 if (odp->flags & FLAG_REGISTER)
275 name = make_internal_name( odp, "regs" );
276 args |= 0x8000;
279 switch(odp->type)
281 case TYPE_STDCALL:
282 fprintf( outfile, " \"\\tjmp " PREFIX "%s\\n\"\n", name );
283 fprintf( outfile, " \"\\tret $0x%04x\\n\"\n", args );
284 fprintf( outfile, " \"\\t.long " PREFIX "%s,0x%08x\\n\"\n", name, mask );
285 break;
286 case TYPE_CDECL:
287 fprintf( outfile, " \"\\tjmp " PREFIX "%s\\n\"\n", name );
288 fprintf( outfile, " \"\\tret\\n\"\n" );
289 fprintf( outfile, " \"\\t.short 0x%04x\\n\"\n", args );
290 fprintf( outfile, " \"\\t.long " PREFIX "%s,0x%08x\\n\"\n", name, mask );
291 break;
292 default:
293 assert(0);
295 continue;
297 ignore:
298 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
303 /* output __wine_dllexport symbols */
305 for (i = 0; i < nb_names; i++)
307 if (Names[i]->flags & FLAG_NOIMPORT) continue;
308 /* check for invalid characters in the name */
309 for (p = Names[i]->name; *p; p++)
310 if (!isalnum(*p) && *p != '_' && *p != '.') break;
311 if (*p) continue;
312 fprintf( outfile, " \"\\t.globl " PREFIX "__wine_dllexport_%s_%s\\n\"\n",
313 make_c_identifier(DLLName), Names[i]->name );
314 fprintf( outfile, " \"" PREFIX "__wine_dllexport_%s_%s:\\n\"\n",
315 make_c_identifier(DLLName), Names[i]->name );
317 fprintf( outfile, " \"\\t.long 0xffffffff\\n\"\n" );
319 /* output variables */
321 for (i = 0; i < nb_entry_points; i++)
323 ORDDEF *odp = EntryPoints[i];
324 if (odp->type == TYPE_VARIABLE)
326 int j;
327 fprintf( outfile, " \"%s:\\n\"\n", make_internal_name( odp, "var" ) );
328 fprintf( outfile, " \"\\t.long " );
329 for (j = 0; j < odp->u.var.n_values; j++)
331 fprintf( outfile, "0x%08x", odp->u.var.values[j] );
332 if (j < odp->u.var.n_values-1) fputc( ',', outfile );
334 fprintf( outfile, "\\n\"\n" );
338 fprintf( outfile, ");\n\n" );
340 return total_size;
344 /*******************************************************************
345 * output_stub_funcs
347 * Output the functions for stub entry points
349 static void output_stub_funcs( FILE *outfile )
351 int i;
353 for (i = 0; i < nb_entry_points; i++)
355 ORDDEF *odp = EntryPoints[i];
356 if (odp->type != TYPE_STUB) continue;
357 fprintf( outfile, "#ifdef __GNUC__\n" );
358 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
359 fprintf( outfile, "#endif\n\n" );
360 fprintf( outfile, "struct exc_record {\n" );
361 fprintf( outfile, " unsigned int code, flags;\n" );
362 fprintf( outfile, " void *rec, *addr;\n" );
363 fprintf( outfile, " unsigned int params;\n" );
364 fprintf( outfile, " const void *info[15];\n" );
365 fprintf( outfile, "};\n\n" );
366 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
367 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
368 fprintf( outfile, " struct exc_record rec;\n" );
369 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
370 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
371 fprintf( outfile, " rec.rec = 0;\n" );
372 fprintf( outfile, " rec.params = 2;\n" );
373 fprintf( outfile, " rec.info[0] = dllname;\n" );
374 fprintf( outfile, " rec.info[1] = func;\n" );
375 fprintf( outfile, "#ifdef __GNUC__\n" );
376 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
377 fprintf( outfile, "#else\n" );
378 fprintf( outfile, " rec.addr = 0;\n" );
379 fprintf( outfile, "#endif\n" );
380 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
381 break;
384 for (i = 0; i < nb_entry_points; i++)
386 ORDDEF *odp = EntryPoints[i];
387 if (odp->type != TYPE_STUB) continue;
388 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
389 if (odp->name[0])
390 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
391 else
392 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
397 /*******************************************************************
398 * output_register_funcs
400 * Output the functions for register entry points
402 static void output_register_funcs( FILE *outfile )
404 const char *name;
405 int i;
407 for (i = 0; i < nb_entry_points; i++)
409 ORDDEF *odp = EntryPoints[i];
410 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
411 if (!(odp->flags & FLAG_REGISTER)) continue;
412 name = make_internal_name( odp, "regs" );
413 fprintf( outfile,
414 "asm(\".align %d\\n\\t\"\n"
415 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
416 " \"" PREFIX "%s:\\n\\t\"\n"
417 " \"call " PREFIX "__wine_call_from_32_regs\\n\\t\"\n"
418 " \".long " PREFIX "%s\\n\\t\"\n"
419 " \".byte %d,%d\");\n",
420 get_alignment(4),
421 name, name, odp->link_name,
422 strlen(odp->u.func.arg_types) * sizeof(int),
423 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
428 /*******************************************************************
429 * BuildSpec32File
431 * Build a Win32 C file from a spec file.
433 void BuildSpec32File( FILE *outfile )
435 int exports_size = 0;
436 int nr_exports, nr_imports, nr_resources;
437 int characteristics, subsystem;
438 DWORD page_size;
440 #ifdef HAVE_GETPAGESIZE
441 page_size = getpagesize();
442 #elif defined(__svr4__)
443 page_size = sysconf(_SC_PAGESIZE);
444 #elif defined(_WINDOWS)
446 SYSTEM_INFO si;
447 GetSystemInfo(&si);
448 page_size = si.dwPageSize;
450 #else
451 # error Cannot get the page size on this platform
452 #endif
454 AssignOrdinals();
455 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
457 resolve_imports();
458 output_standard_file_header( outfile );
460 /* Reserve some space for the PE header */
462 fprintf( outfile, "extern char pe_header[];\n" );
463 fprintf( outfile, "asm(\".section .text\\n\\t\"\n" );
464 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
465 fprintf( outfile, " \"" PREFIX "pe_header:\\t.fill %ld,1,0\\n\\t\");\n", page_size );
467 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLName );
468 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
470 #ifdef __i386__
471 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
472 #else
473 fprintf( outfile, "#define __stdcall\n\n" );
474 #endif
476 if (nr_exports)
478 /* Output the stub functions */
480 output_stub_funcs( outfile );
482 fprintf( outfile, "#ifndef __GNUC__\n" );
483 fprintf( outfile, "static void __asm__dummy(void) {\n" );
484 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
486 /* Output code for all register functions */
488 output_register_funcs( outfile );
490 /* Output the exports and relay entry points */
492 exports_size = output_exports( outfile, nr_exports );
494 fprintf( outfile, "#ifndef __GNUC__\n" );
495 fprintf( outfile, "}\n" );
496 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
499 /* Output the DLL imports */
501 nr_imports = output_imports( outfile );
503 /* Output the resources */
505 nr_resources = output_resources( outfile );
507 /* Output LibMain function */
509 characteristics = subsystem = 0;
510 switch(SpecMode)
512 case SPEC_MODE_DLL:
513 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
514 characteristics = IMAGE_FILE_DLL;
515 break;
516 case SPEC_MODE_GUIEXE:
517 if (!init_func) init_func = "WinMain";
518 fprintf( outfile,
519 "\ntypedef struct {\n"
520 " unsigned int cb;\n"
521 " char *lpReserved, *lpDesktop, *lpTitle;\n"
522 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
523 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
524 " unsigned short wShowWindow, cbReserved2;\n"
525 " char *lpReserved2;\n"
526 " void *hStdInput, *hStdOutput, *hStdError;\n"
527 "} STARTUPINFOA;\n"
528 "int _ARGC;\n"
529 "char **_ARGV;\n"
530 "extern int __stdcall %s(void *,void *,char *,int);\n"
531 "extern char * __stdcall GetCommandLineA(void);\n"
532 "extern void * __stdcall GetModuleHandleA(char *);\n"
533 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
534 "extern void __stdcall ExitProcess(unsigned int);\n"
535 "static void __wine_exe_main(void)\n"
536 "{\n"
537 " extern int __wine_main_argc;\n"
538 " extern char **__wine_main_argv;\n"
539 " STARTUPINFOA info;\n"
540 " char *cmdline = GetCommandLineA();\n"
541 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
542 " if (*cmdline) cmdline++;\n"
543 " GetStartupInfoA( &info );\n"
544 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
545 " _ARGC = __wine_main_argc;\n"
546 " _ARGV = __wine_main_argv;\n"
547 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
548 "}\n\n", init_func, init_func );
549 init_func = "__wine_exe_main";
550 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
551 break;
552 case SPEC_MODE_GUIEXE_UNICODE:
553 if (!init_func) init_func = "WinMain";
554 fprintf( outfile,
555 "\ntypedef unsigned short WCHAR;\n"
556 "typedef struct {\n"
557 " unsigned int cb;\n"
558 " char *lpReserved, *lpDesktop, *lpTitle;\n"
559 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
560 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
561 " unsigned short wShowWindow, cbReserved2;\n"
562 " char *lpReserved2;\n"
563 " void *hStdInput, *hStdOutput, *hStdError;\n"
564 "} STARTUPINFOA;\n"
565 "int _ARGC;\n"
566 "WCHAR **_ARGV;\n"
567 "extern int __stdcall %s(void *,void *,char *,int);\n"
568 "extern char * __stdcall GetCommandLineA(void);\n"
569 "extern void * __stdcall GetModuleHandleA(char *);\n"
570 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
571 "extern void __stdcall ExitProcess(unsigned int);\n"
572 "static void __wine_exe_main(void)\n"
573 "{\n"
574 " extern int __wine_main_argc;\n"
575 " extern WCHAR **__wine_main_wargv;\n"
576 " STARTUPINFOA info;\n"
577 " char *cmdline = GetCommandLineA();\n"
578 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
579 " if (*cmdline) cmdline++;\n"
580 " GetStartupInfoA( &info );\n"
581 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
582 " _ARGC = __wine_main_argc;\n"
583 " _ARGV = __wine_main_wargv;\n"
584 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
585 "}\n\n", init_func, init_func );
586 init_func = "__wine_exe_main";
587 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
588 break;
589 case SPEC_MODE_CUIEXE:
590 if (!init_func) init_func = "main";
591 fprintf( outfile,
592 "\nint _ARGC;\n"
593 "char **_ARGV;\n"
594 "extern void __stdcall ExitProcess(int);\n"
595 "static void __wine_exe_main(void)\n"
596 "{\n"
597 " extern int %s( int argc, char *argv[] );\n"
598 " extern int __wine_main_argc;\n"
599 " extern char **__wine_main_argv;\n"
600 " _ARGC = __wine_main_argc;\n"
601 " _ARGV = __wine_main_argv;\n"
602 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
603 "}\n\n", init_func, init_func );
604 init_func = "__wine_exe_main";
605 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
606 break;
607 case SPEC_MODE_CUIEXE_UNICODE:
608 if (!init_func) init_func = "wmain";
609 fprintf( outfile,
610 "\ntypedef unsigned short WCHAR;\n"
611 "int _ARGC;\n"
612 "WCHAR **_ARGV;\n"
613 "extern void __stdcall ExitProcess(int);\n"
614 "static void __wine_exe_main(void)\n"
615 "{\n"
616 " extern int %s( int argc, WCHAR *argv[] );\n"
617 " extern int __wine_main_argc;\n"
618 " extern WCHAR **__wine_main_wargv;\n"
619 " _ARGC = __wine_main_argc;\n"
620 " _ARGV = __wine_main_wargv;\n"
621 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
622 "}\n\n", init_func, init_func );
623 init_func = "__wine_exe_main";
624 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
625 break;
628 /* Output the NT header */
630 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
631 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
632 fprintf( outfile, " int Signature;\n" );
633 fprintf( outfile, " struct file_header {\n" );
634 fprintf( outfile, " short Machine;\n" );
635 fprintf( outfile, " short NumberOfSections;\n" );
636 fprintf( outfile, " int TimeDateStamp;\n" );
637 fprintf( outfile, " void *PointerToSymbolTable;\n" );
638 fprintf( outfile, " int NumberOfSymbols;\n" );
639 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
640 fprintf( outfile, " short Characteristics;\n" );
641 fprintf( outfile, " } FileHeader;\n" );
642 fprintf( outfile, " struct opt_header {\n" );
643 fprintf( outfile, " short Magic;\n" );
644 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
645 fprintf( outfile, " int SizeOfCode;\n" );
646 fprintf( outfile, " int SizeOfInitializedData;\n" );
647 fprintf( outfile, " int SizeOfUninitializedData;\n" );
648 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
649 fprintf( outfile, " void *BaseOfCode;\n" );
650 fprintf( outfile, " void *BaseOfData;\n" );
651 fprintf( outfile, " void *ImageBase;\n" );
652 fprintf( outfile, " int SectionAlignment;\n" );
653 fprintf( outfile, " int FileAlignment;\n" );
654 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
655 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
656 fprintf( outfile, " short MajorImageVersion;\n" );
657 fprintf( outfile, " short MinorImageVersion;\n" );
658 fprintf( outfile, " short MajorSubsystemVersion;\n" );
659 fprintf( outfile, " short MinorSubsystemVersion;\n" );
660 fprintf( outfile, " int Win32VersionValue;\n" );
661 fprintf( outfile, " int SizeOfImage;\n" );
662 fprintf( outfile, " int SizeOfHeaders;\n" );
663 fprintf( outfile, " int CheckSum;\n" );
664 fprintf( outfile, " short Subsystem;\n" );
665 fprintf( outfile, " short DllCharacteristics;\n" );
666 fprintf( outfile, " int SizeOfStackReserve;\n" );
667 fprintf( outfile, " int SizeOfStackCommit;\n" );
668 fprintf( outfile, " int SizeOfHeapReserve;\n" );
669 fprintf( outfile, " int SizeOfHeapCommit;\n" );
670 fprintf( outfile, " int LoaderFlags;\n" );
671 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
672 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
673 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
674 fprintf( outfile, " } OptionalHeader;\n" );
675 fprintf( outfile, "} nt_header = {\n" );
676 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
678 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
679 fprintf( outfile, " 0, 0, 0, 0,\n" );
680 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
681 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
683 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
684 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
685 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
686 fprintf( outfile, " %s,\n", init_func ? init_func : "0" ); /* AddressOfEntryPoint */
687 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
688 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
689 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
690 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
691 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
692 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
693 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
694 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
695 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
696 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
697 fprintf( outfile, " 0,\n" ); /* CheckSum */
698 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
699 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
700 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
701 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
702 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
703 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
704 fprintf( outfile, " {\n" );
705 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
706 exports_size ? "__wine_spec_exports" : "0", exports_size );
707 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
708 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
709 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
710 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
711 fprintf( outfile, " }\n }\n};\n\n" );
713 /* Output the DLL constructor */
715 fprintf( outfile, "#ifndef __GNUC__\n" );
716 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
717 fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
719 #if defined(__i386__)
720 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
721 fprintf( outfile, " \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n",
722 make_c_identifier(DLLName) );
723 fprintf( outfile, " \"\\t.previous\\n\");\n" );
724 #elif defined(__sparc__)
725 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
726 fprintf( outfile, " \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n",
727 make_c_identifier(DLLName) );
728 fprintf( outfile, " \"\\tnop\\n\"\n" );
729 fprintf( outfile, " \"\\t.previous\\n\");\n" );
730 #elif defined(__PPC__)
731 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
732 fprintf( outfile, " \"\\tbl " PREFIX "__wine_spec_%s_init\\n\"\n",
733 make_c_identifier(DLLName) );
734 fprintf( outfile, " \"\\t.previous\\n\");\n" );
735 #else
736 #error You need to define the DLL constructor for your architecture
737 #endif
739 fprintf( outfile, "#ifndef __GNUC__\n" );
740 fprintf( outfile, "}\n" );
741 fprintf( outfile, "#endif /* defined(__GNUC__) */\n\n" );
743 fprintf( outfile,
744 "void __wine_spec_%s_init(void)\n"
745 "{\n"
746 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
747 " extern void *__wine_dbg_register( char * const *, int );\n"
748 " __wine_dll_register( &nt_header, \"%s\" );\n",
749 make_c_identifier(DLLName), DLLFileName );
750 fprintf( outfile, "}\n" );
754 /*******************************************************************
755 * BuildDef32File
757 * Build a Win32 def file from a spec file.
759 void BuildDef32File(FILE *outfile)
761 int i;
763 AssignOrdinals();
765 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
766 input_file_name );
768 fprintf(outfile, "LIBRARY %s\n\n", DLLFileName);
770 fprintf(outfile, "EXPORTS\n");
772 /* Output the exports and relay entry points */
774 for(i = 0; i < nb_entry_points; i++)
776 ORDDEF *odp = EntryPoints[i];
777 if(!odp || !*odp->name || (odp->flags & FLAG_NOIMPORT)) continue;
778 if (odp->type == TYPE_STUB) continue;
780 fprintf(outfile, " %s", odp->name);
782 switch(odp->type)
784 case TYPE_EXTERN:
785 case TYPE_VARARGS:
786 case TYPE_CDECL:
787 case TYPE_VARIABLE:
788 /* try to reduce output */
789 if(strcmp(odp->name, odp->link_name))
790 fprintf(outfile, "=%s", odp->link_name);
791 break;
792 case TYPE_STDCALL:
794 #ifdef NEED_STDCALL_DECORATION
795 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
796 fprintf(outfile, "@%d", at_param);
797 #endif /* NEED_STDCALL_DECORATION */
798 /* try to reduce output */
799 if(strcmp(odp->name, odp->link_name))
801 fprintf(outfile, "=%s", odp->link_name);
802 #ifdef NEED_STDCALL_DECORATION
803 fprintf(outfile, "@%d", at_param);
804 #endif /* NEED_STDCALL_DECORATION */
806 break;
808 case TYPE_FORWARD:
809 fprintf(outfile, "=%s", odp->link_name);
810 break;
811 default:
812 assert(0);
814 fprintf(outfile, " @%d\n", odp->ordinal);
819 /*******************************************************************
820 * BuildDebugFile
822 * Build the debugging channels source file.
824 void BuildDebugFile( FILE *outfile )
826 int nr_debug;
827 char *prefix, *p;
829 output_standard_file_header( outfile );
830 nr_debug = output_debug( outfile );
831 if (!nr_debug)
833 fprintf( outfile, "/* no debug channels found for this module */\n" );
834 return;
837 if (output_file_name)
839 if ((p = strrchr( output_file_name, '/' ))) p++;
840 prefix = xstrdup( p ? p : output_file_name );
841 if ((p = strchr( prefix, '.' ))) *p = 0;
842 strcpy( p, make_c_identifier(p) );
844 else prefix = xstrdup( "_" );
846 /* Output the DLL constructor */
848 fprintf( outfile,
849 "#ifdef __GNUC__\n"
850 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
851 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
852 "#else\n"
853 "static void __asm__dummy_dll_init(void) {\n",
854 prefix, prefix );
856 #if defined(__i386__)
857 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
858 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_init\\n\"\n", prefix );
859 fprintf( outfile, " \"\\t.previous\\n\");\n" );
860 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
861 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_fini\\n\"\n", prefix );
862 fprintf( outfile, " \"\\t.previous\\n\");\n" );
863 #elif defined(__sparc__)
864 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
865 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_init\\n\"\n", prefix );
866 fprintf( outfile, " \"\\tnop\\n\"\n" );
867 fprintf( outfile, " \"\\t.previous\\n\");\n" );
868 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
869 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_fini\\n\"\n", prefix );
870 fprintf( outfile, " \"\\tnop\\n\"\n" );
871 fprintf( outfile, " \"\\t.previous\\n\");\n" );
872 #elif defined(__PPC__)
873 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
874 fprintf( outfile, " \"\\tbl " PREFIX "__wine_dbg_%s_init\\n\"\n", prefix );
875 fprintf( outfile, " \"\\t.previous\\n\");\n" );
876 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
877 fprintf( outfile, " \"\\tbl " PREFIX "__wine_dbg_%s_fini\\n\"\n", prefix );
878 fprintf( outfile, " \"\\t.previous\\n\");\n" );
879 #else
880 #error You need to define the DLL constructor for your architecture
881 #endif
882 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
884 fprintf( outfile,
885 "\n#ifdef __GNUC__\n"
886 "static\n"
887 "#endif\n"
888 "void __wine_dbg_%s_init(void)\n"
889 "{\n"
890 " extern void *__wine_dbg_register( char * const *, int );\n"
891 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
892 "}\n", prefix, nr_debug );
893 fprintf( outfile,
894 "\n#ifdef __GNUC__\n"
895 "static\n"
896 "#endif\n"
897 "void __wine_dbg_%s_fini(void)\n"
898 "{\n"
899 " extern void __wine_dbg_unregister( void* );\n"
900 " __wine_dbg_unregister( debug_registration );\n"
901 "}\n", prefix );
903 free( prefix );