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
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];
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
, DLLName
, odp
->ordinal
);
65 /*******************************************************************
68 * Assign ordinals to all entry points.
70 static void AssignOrdinals(void)
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 /*******************************************************************
97 * Output the debug channels.
99 static int output_debug( FILE *outfile
)
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 /*******************************************************************
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;
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 */
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
)
171 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n", odp
->link_name
);
176 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n",
177 (odp
->flags
& FLAG_REGISTER
) ? make_internal_name(odp
,"regs") : odp
->link_name
);
180 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n", make_internal_name( odp
, "stub" ) );
183 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n", make_internal_name( odp
, "var" ) );
186 fprintf( outfile
, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size
);
187 fwd_size
+= strlen(odp
->link_name
) + 1;
193 total_size
+= (Limit
- Base
+ 1) * sizeof(int);
197 /* output the function name pointers */
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);
227 fprintf( outfile
, " \"\\t.short 0\\n\"\n" );
228 total_size
+= sizeof(short);
232 /* output forward strings */
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;
251 for (i
= Base
; i
<= Limit
; i
++)
253 ORDDEF
*odp
= Ordinals
[i
];
254 unsigned int j
, args
, mask
= 0;
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" );
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
);
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
);
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;
312 fprintf( outfile
, " \"\\t.globl " PREFIX
"__wine_dllexport_%s_%s\\n\"\n",
313 DLLName
, Names
[i
]->name
);
314 fprintf( outfile
, " \"" PREFIX
"__wine_dllexport_%s_%s:\\n\"\n",
315 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
)
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" );
344 /*******************************************************************
347 * Output the functions for stub entry points
349 static void output_stub_funcs( FILE *outfile
)
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" );
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" ) );
390 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->name
);
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
)
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" );
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",
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 /*******************************************************************
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
, nr_debug
;
437 int characteristics
, subsystem
;
440 #ifdef HAVE_GETPAGESIZE
441 page_size
= getpagesize();
444 page_size
= sysconf(_SC_PAGESIZE
);
446 # error Cannot get the page size on this platform
451 nr_exports
= Base
<= Limit
? Limit
- Base
+ 1 : 0;
454 output_standard_file_header( outfile
);
456 /* Reserve some space for the PE header */
458 fprintf( outfile
, "extern char pe_header[];\n" );
459 fprintf( outfile
, "asm(\".section .text\\n\\t\"\n" );
460 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(page_size
) );
461 fprintf( outfile
, " \"" PREFIX
"pe_header:\\t.fill %ld,1,0\\n\\t\");\n", page_size
);
463 fprintf( outfile
, "static const char dllname[] = \"%s\";\n\n", DLLName
);
464 fprintf( outfile
, "extern int __wine_spec_exports[];\n\n" );
467 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
469 fprintf( outfile
, "#define __stdcall\n\n" );
474 /* Output the stub functions */
476 output_stub_funcs( outfile
);
478 fprintf( outfile
, "#ifndef __GNUC__\n" );
479 fprintf( outfile
, "static void __asm__dummy(void) {\n" );
480 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
482 /* Output code for all register functions */
484 output_register_funcs( outfile
);
486 /* Output the exports and relay entry points */
488 exports_size
= output_exports( outfile
, nr_exports
);
490 fprintf( outfile
, "#ifndef __GNUC__\n" );
491 fprintf( outfile
, "}\n" );
492 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
495 /* Output the DLL imports */
497 nr_imports
= output_imports( outfile
);
499 /* Output the resources */
501 nr_resources
= output_resources( outfile
);
503 /* Output the debug channels */
505 nr_debug
= output_debug( outfile
);
507 /* Output LibMain function */
509 characteristics
= subsystem
= 0;
513 if (init_func
) fprintf( outfile
, "extern void %s();\n", init_func
);
514 characteristics
= IMAGE_FILE_DLL
;
516 case SPEC_MODE_GUIEXE
:
517 if (!init_func
) init_func
= "WinMain";
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"
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"
537 " extern int __wine_get_main_args( char ***argv );\n"
538 " STARTUPINFOA info;\n"
539 " char *cmdline = GetCommandLineA();\n"
540 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
541 " if (*cmdline) cmdline++;\n"
542 " GetStartupInfoA( &info );\n"
543 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
544 " _ARGC = __wine_get_main_args( &_ARGV );\n"
545 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
546 "}\n\n", init_func
, init_func
);
547 init_func
= "__wine_exe_main";
548 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
550 case SPEC_MODE_GUIEXE_UNICODE
:
551 if (!init_func
) init_func
= "WinMain";
553 "\ntypedef unsigned short WCHAR;\n"
555 " unsigned int cb;\n"
556 " char *lpReserved, *lpDesktop, *lpTitle;\n"
557 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
558 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
559 " unsigned short wShowWindow, cbReserved2;\n"
560 " char *lpReserved2;\n"
561 " void *hStdInput, *hStdOutput, *hStdError;\n"
565 "extern int __stdcall %s(void *,void *,char *,int);\n"
566 "extern char * __stdcall GetCommandLineA(void);\n"
567 "extern void * __stdcall GetModuleHandleA(char *);\n"
568 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
569 "extern void __stdcall ExitProcess(unsigned int);\n"
570 "static void __wine_exe_main(void)\n"
572 " extern int __wine_get_wmain_args( WCHAR ***argv );\n"
573 " STARTUPINFOA info;\n"
574 " char *cmdline = GetCommandLineA();\n"
575 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
576 " if (*cmdline) cmdline++;\n"
577 " GetStartupInfoA( &info );\n"
578 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
579 " _ARGC = __wine_get_wmain_args( &_ARGV );\n"
580 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
581 "}\n\n", init_func
, init_func
);
582 init_func
= "__wine_exe_main";
583 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
585 case SPEC_MODE_CUIEXE
:
586 if (!init_func
) init_func
= "main";
590 "extern void __stdcall ExitProcess(int);\n"
591 "static void __wine_exe_main(void)\n"
593 " extern int %s( int argc, char *argv[] );\n"
594 " extern int __wine_get_main_args( char ***argv );\n"
595 " _ARGC = __wine_get_main_args( &_ARGV );\n"
596 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
597 "}\n\n", init_func
, init_func
);
598 init_func
= "__wine_exe_main";
599 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
601 case SPEC_MODE_CUIEXE_UNICODE
:
602 if (!init_func
) init_func
= "wmain";
604 "\ntypedef unsigned short WCHAR;\n"
607 "extern void __stdcall ExitProcess(int);\n"
608 "static void __wine_exe_main(void)\n"
610 " extern int %s( int argc, WCHAR *argv[] );\n"
611 " extern int __wine_get_wmain_args( WCHAR ***argv );\n"
612 " _ARGC = __wine_get_wmain_args( &_ARGV );\n"
613 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
614 "}\n\n", init_func
, init_func
);
615 init_func
= "__wine_exe_main";
616 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
620 /* Output the NT header */
622 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
623 fprintf( outfile
, "static const struct image_nt_headers\n{\n" );
624 fprintf( outfile
, " int Signature;\n" );
625 fprintf( outfile
, " struct file_header {\n" );
626 fprintf( outfile
, " short Machine;\n" );
627 fprintf( outfile
, " short NumberOfSections;\n" );
628 fprintf( outfile
, " int TimeDateStamp;\n" );
629 fprintf( outfile
, " void *PointerToSymbolTable;\n" );
630 fprintf( outfile
, " int NumberOfSymbols;\n" );
631 fprintf( outfile
, " short SizeOfOptionalHeader;\n" );
632 fprintf( outfile
, " short Characteristics;\n" );
633 fprintf( outfile
, " } FileHeader;\n" );
634 fprintf( outfile
, " struct opt_header {\n" );
635 fprintf( outfile
, " short Magic;\n" );
636 fprintf( outfile
, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
637 fprintf( outfile
, " int SizeOfCode;\n" );
638 fprintf( outfile
, " int SizeOfInitializedData;\n" );
639 fprintf( outfile
, " int SizeOfUninitializedData;\n" );
640 fprintf( outfile
, " void *AddressOfEntryPoint;\n" );
641 fprintf( outfile
, " void *BaseOfCode;\n" );
642 fprintf( outfile
, " void *BaseOfData;\n" );
643 fprintf( outfile
, " void *ImageBase;\n" );
644 fprintf( outfile
, " int SectionAlignment;\n" );
645 fprintf( outfile
, " int FileAlignment;\n" );
646 fprintf( outfile
, " short MajorOperatingSystemVersion;\n" );
647 fprintf( outfile
, " short MinorOperatingSystemVersion;\n" );
648 fprintf( outfile
, " short MajorImageVersion;\n" );
649 fprintf( outfile
, " short MinorImageVersion;\n" );
650 fprintf( outfile
, " short MajorSubsystemVersion;\n" );
651 fprintf( outfile
, " short MinorSubsystemVersion;\n" );
652 fprintf( outfile
, " int Win32VersionValue;\n" );
653 fprintf( outfile
, " int SizeOfImage;\n" );
654 fprintf( outfile
, " int SizeOfHeaders;\n" );
655 fprintf( outfile
, " int CheckSum;\n" );
656 fprintf( outfile
, " short Subsystem;\n" );
657 fprintf( outfile
, " short DllCharacteristics;\n" );
658 fprintf( outfile
, " int SizeOfStackReserve;\n" );
659 fprintf( outfile
, " int SizeOfStackCommit;\n" );
660 fprintf( outfile
, " int SizeOfHeapReserve;\n" );
661 fprintf( outfile
, " int SizeOfHeapCommit;\n" );
662 fprintf( outfile
, " int LoaderFlags;\n" );
663 fprintf( outfile
, " int NumberOfRvaAndSizes;\n" );
664 fprintf( outfile
, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
665 IMAGE_NUMBEROF_DIRECTORY_ENTRIES
);
666 fprintf( outfile
, " } OptionalHeader;\n" );
667 fprintf( outfile
, "} nt_header = {\n" );
668 fprintf( outfile
, " 0x%04x,\n", IMAGE_NT_SIGNATURE
); /* Signature */
670 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386
); /* Machine */
671 fprintf( outfile
, " 0, 0, 0, 0,\n" );
672 fprintf( outfile
, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
673 fprintf( outfile
, " 0x%04x },\n", characteristics
); /* Characteristics */
675 fprintf( outfile
, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC
); /* Magic */
676 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
677 fprintf( outfile
, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
678 fprintf( outfile
, " %s,\n", init_func
? init_func
: "0" ); /* AddressOfEntryPoint */
679 fprintf( outfile
, " 0, 0,\n" ); /* BaseOfCode/Data */
680 fprintf( outfile
, " pe_header,\n" ); /* ImageBase */
681 fprintf( outfile
, " %ld,\n", page_size
); /* SectionAlignment */
682 fprintf( outfile
, " %ld,\n", page_size
); /* FileAlignment */
683 fprintf( outfile
, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
684 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorImageVersion */
685 fprintf( outfile
, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
686 fprintf( outfile
, " 0,\n" ); /* Win32VersionValue */
687 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfImage */
688 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfHeaders */
689 fprintf( outfile
, " 0,\n" ); /* CheckSum */
690 fprintf( outfile
, " 0x%04x,\n", subsystem
); /* Subsystem */
691 fprintf( outfile
, " 0,\n" ); /* DllCharacteristics */
692 fprintf( outfile
, " %d, 0,\n", stack_size
*1024 ); /* SizeOfStackReserve/Commit */
693 fprintf( outfile
, " %d, 0,\n", DLLHeapSize
*1024 );/* SizeOfHeapReserve/Commit */
694 fprintf( outfile
, " 0,\n" ); /* LoaderFlags */
695 fprintf( outfile
, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES
); /* NumberOfRvaAndSizes */
696 fprintf( outfile
, " {\n" );
697 fprintf( outfile
, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
698 exports_size
? "__wine_spec_exports" : "0", exports_size
);
699 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
700 nr_imports
? "&imports" : "0", nr_imports
? "sizeof(imports)" : "0" );
701 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
702 nr_resources
? "&resources" : "0", nr_resources
? "sizeof(resources)" : "0" );
703 fprintf( outfile
, " }\n }\n};\n\n" );
705 /* Output the DLL constructor */
707 fprintf( outfile
, "#ifndef __GNUC__\n" );
708 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
709 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n" );
711 #if defined(__i386__)
712 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
713 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_init\\n\"\n", DLLName
);
714 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
717 fprintf( outfile
, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
718 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_fini\\n\"\n", DLLName
);
719 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
721 #elif defined(__sparc__)
722 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
723 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_init\\n\"\n", DLLName
);
724 fprintf( outfile
, " \"\\tnop\\n\"\n" );
725 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
728 fprintf( outfile
, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
729 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_fini\\n\"\n", DLLName
);
730 fprintf( outfile
, " \"\\tnop\\n\"\n" );
731 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
733 #elif defined(__PPC__)
734 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
735 fprintf( outfile
, " \"\\tbl " PREFIX
"__wine_spec_%s_init\\n\"\n",
737 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
740 fprintf( outfile
, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
741 fprintf( outfile
, " \"\\tbl " PREFIX
"__wine_spec_%s_fini\\n\"\n",
743 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
746 #error You need to define the DLL constructor for your architecture
749 fprintf( outfile
, "#ifndef __GNUC__\n" );
750 fprintf( outfile
, "}\n" );
751 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n\n" );
754 "void __wine_spec_%s_init(void)\n"
756 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
757 " extern void *__wine_dbg_register( char * const *, int );\n"
758 " __wine_dll_register( &nt_header, \"%s\" );\n",
759 DLLName
, DLLFileName
);
761 fprintf( outfile
, " debug_registration = __wine_dbg_register( debug_channels, %d );\n",
763 fprintf( outfile
, "}\n" );
767 "\nvoid __wine_spec_%s_fini(void)\n"
769 " extern void __wine_dbg_unregister( void* );\n"
770 " __wine_dbg_unregister( debug_registration );\n"
776 /*******************************************************************
779 * Build a Win32 def file from a spec file.
781 void BuildDef32File(FILE *outfile
)
787 fprintf(outfile
, "; File generated automatically from %s; do not edit!\n\n",
790 fprintf(outfile
, "LIBRARY lib%s\n\n", DLLFileName
);
792 fprintf(outfile
, "EXPORTS\n");
794 /* Output the exports and relay entry points */
796 for(i
= 0; i
< nb_entry_points
; i
++)
798 ORDDEF
*odp
= EntryPoints
[i
];
799 if(!odp
|| !*odp
->name
|| (odp
->flags
& FLAG_NOIMPORT
)) continue;
801 fprintf(outfile
, " %s", odp
->name
);
809 /* try to reduce output */
810 if(strcmp(odp
->name
, odp
->link_name
))
811 fprintf(outfile
, "=%s", odp
->link_name
);
815 #ifdef NEED_STDCALL_DECORATION
816 int at_param
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
817 fprintf(outfile
, "@%d", at_param
);
818 #endif /* NEED_STDCALL_DECORATION */
819 /* try to reduce output */
820 if(strcmp(odp
->name
, odp
->link_name
))
822 fprintf(outfile
, "=%s", odp
->link_name
);
823 #ifdef NEED_STDCALL_DECORATION
824 fprintf(outfile
, "@%d", at_param
);
825 #endif /* NEED_STDCALL_DECORATION */
830 fprintf(outfile
, "=%s", make_internal_name( odp
, "stub" ));
833 fprintf(outfile
, "=lib%s", odp
->link_name
);
838 fprintf(outfile
, " @%d\n", odp
->ordinal
);