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
32 #include "wine/exception.h"
36 static int string_compare( const void *ptr1
, const void *ptr2
)
38 const char * const *str1
= ptr1
;
39 const char * const *str2
= ptr2
;
40 return strcmp( *str1
, *str2
);
44 /*******************************************************************
47 * Generate an internal name for an entry point. Used for stubs etc.
49 static const char *make_internal_name( const ORDDEF
*odp
, const char *prefix
)
51 static char buffer
[256];
55 sprintf( buffer
, "__wine_%s_%s_%s", prefix
, DLLName
, odp
->name
);
56 /* make sure name is a legal C identifier */
57 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
58 if (!*p
) return buffer
;
60 sprintf( buffer
, "__wine_%s_%s_%d", prefix
, DLLName
, odp
->ordinal
);
64 /*******************************************************************
67 * Assign ordinals to all entry points.
69 static void AssignOrdinals(void)
73 if ( !nb_names
) return;
75 /* start assigning from Base, or from 1 if no ordinal defined yet */
76 if (Base
== MAX_ORDINALS
) Base
= 1;
77 for (i
= 0, ordinal
= Base
; i
< nb_names
; i
++)
79 if (Names
[i
]->ordinal
!= -1) continue; /* already has an ordinal */
80 while (Ordinals
[ordinal
]) ordinal
++;
81 if (ordinal
>= MAX_ORDINALS
)
83 current_line
= Names
[i
]->lineno
;
84 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS
);
86 Names
[i
]->ordinal
= ordinal
;
87 Ordinals
[ordinal
] = Names
[i
];
89 if (ordinal
> Limit
) Limit
= ordinal
;
93 /*******************************************************************
96 * Output the debug channels.
98 static int output_debug( FILE *outfile
)
102 if (!nb_debug_channels
) return 0;
103 qsort( debug_channels
, nb_debug_channels
, sizeof(debug_channels
[0]), string_compare
);
105 for (i
= 0; i
< nb_debug_channels
; i
++)
106 fprintf( outfile
, "char __wine_dbch_%s[] = \"\\003%s\";\n",
107 debug_channels
[i
], debug_channels
[i
] );
109 fprintf( outfile
, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels
);
110 for (i
= 0; i
< nb_debug_channels
; i
++)
112 fprintf( outfile
, " __wine_dbch_%s", debug_channels
[i
] );
113 if (i
< nb_debug_channels
- 1) fprintf( outfile
, ",\n" );
115 fprintf( outfile
, "\n};\n\n" );
116 fprintf( outfile
, "static void *debug_registration;\n\n" );
118 return nb_debug_channels
;
122 /*******************************************************************
125 * Output the export table for a Win32 module.
127 static int output_exports( FILE *outfile
, int nr_exports
)
129 int i
, fwd_size
= 0, total_size
= 0;
132 if (!nr_exports
) return 0;
134 fprintf( outfile
, "asm(\".data\\n\"\n" );
135 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
136 fprintf( outfile
, " \"" PREFIX
"__wine_spec_exports:\\n\"\n" );
138 /* export directory header */
140 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
141 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
142 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
143 fprintf( outfile
, " \"\\t.long " PREFIX
"dllname\\n\"\n" ); /* Name */
144 fprintf( outfile
, " \"\\t.long %d\\n\"\n", Base
); /* Base */
145 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nr_exports
); /* NumberOfFunctions */
146 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nb_names
); /* NumberOfNames */
147 fprintf( outfile
, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
150 fprintf( outfile
, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
151 fprintf( outfile
, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
155 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
156 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
158 total_size
+= 10 * sizeof(int);
160 /* output the function pointers */
162 fprintf( outfile
, " \"__wine_spec_exports_funcs:\\n\"\n" );
163 for (i
= Base
; i
<= Limit
; i
++)
165 ORDDEF
*odp
= Ordinals
[i
];
166 if (!odp
) fprintf( outfile
, " \"\\t.long 0\\n\"\n" );
167 else switch(odp
->type
)
170 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n", odp
->link_name
);
175 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n",
176 (odp
->flags
& FLAG_REGISTER
) ? make_internal_name(odp
,"regs") : odp
->link_name
);
179 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n", make_internal_name( odp
, "stub" ) );
182 fprintf( outfile
, " \"\\t.long " PREFIX
"%s\\n\"\n", make_internal_name( odp
, "var" ) );
185 fprintf( outfile
, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size
);
186 fwd_size
+= strlen(odp
->link_name
) + 1;
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 1\\n\"\n" );
211 fprintf( outfile
, " \"__wine_spec_exp_names:\\n\"\n" );
212 for (i
= 0; i
< nb_names
; i
++)
213 fprintf( outfile
, " \"\\t" 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.short %d\\n\"\n", Names
[i
]->ordinal
- Base
);
223 total_size
+= nb_names
* sizeof(short);
226 fprintf( outfile
, " \"\\t.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
->type
== TYPE_FORWARD
)
240 fprintf( outfile
, " \"\\t" 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 entry points */
261 if (odp
->flags
& FLAG_NORELAY
) 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
)
274 name
= make_internal_name( odp
, "regs" );
281 fprintf( outfile
, " \"\\tjmp " PREFIX
"%s\\n\"\n", name
);
282 fprintf( outfile
, " \"\\tret $0x%04x\\n\"\n", args
);
283 fprintf( outfile
, " \"\\t.long " PREFIX
"%s,0x%08x\\n\"\n", name
, mask
);
286 fprintf( outfile
, " \"\\tjmp " PREFIX
"%s\\n\"\n", name
);
287 fprintf( outfile
, " \"\\tret\\n\"\n" );
288 fprintf( outfile
, " \"\\t.short 0x%04x\\n\"\n", args
);
289 fprintf( outfile
, " \"\\t.long " PREFIX
"%s,0x%08x\\n\"\n", name
, mask
);
297 fprintf( outfile
, " \"\\t.long 0,0,0,0\\n\"\n" );
302 /* output __wine_dllexport symbols */
304 for (i
= 0; i
< nb_names
; i
++)
306 if (Names
[i
]->flags
& FLAG_NOIMPORT
) continue;
307 /* check for invalid characters in the name */
308 for (p
= Names
[i
]->name
; *p
; p
++)
309 if (!isalnum(*p
) && *p
!= '_' && *p
!= '.') break;
311 fprintf( outfile
, " \"\\t.globl " PREFIX
"__wine_dllexport_%s_%s\\n\"\n",
312 DLLName
, Names
[i
]->name
);
313 fprintf( outfile
, " \"" PREFIX
"__wine_dllexport_%s_%s:\\n\"\n",
314 DLLName
, Names
[i
]->name
);
316 fprintf( outfile
, " \"\\t.long 0xffffffff\\n\"\n" );
318 /* output variables */
320 for (i
= 0; i
< nb_entry_points
; i
++)
322 ORDDEF
*odp
= EntryPoints
[i
];
323 if (odp
->type
== TYPE_VARIABLE
)
326 fprintf( outfile
, " \"%s:\\n\"\n", make_internal_name( odp
, "var" ) );
327 fprintf( outfile
, " \"\\t.long " );
328 for (j
= 0; j
< odp
->u
.var
.n_values
; j
++)
330 fprintf( outfile
, "0x%08x", odp
->u
.var
.values
[j
] );
331 if (j
< odp
->u
.var
.n_values
-1) fputc( ',', outfile
);
333 fprintf( outfile
, "\\n\"\n" );
337 fprintf( outfile
, ");\n\n" );
343 /*******************************************************************
346 * Output the functions for stub entry points
348 static void output_stub_funcs( FILE *outfile
)
352 for (i
= 0; i
< nb_entry_points
; i
++)
354 ORDDEF
*odp
= EntryPoints
[i
];
355 if (odp
->type
!= TYPE_STUB
) continue;
356 fprintf( outfile
, "#ifdef __GNUC__\n" );
357 fprintf( outfile
, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
358 fprintf( outfile
, "#endif\n\n" );
359 fprintf( outfile
, "struct exc_record {\n" );
360 fprintf( outfile
, " unsigned int code, flags;\n" );
361 fprintf( outfile
, " void *rec, *addr;\n" );
362 fprintf( outfile
, " unsigned int params;\n" );
363 fprintf( outfile
, " const void *info[15];\n" );
364 fprintf( outfile
, "};\n\n" );
365 fprintf( outfile
, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
366 fprintf( outfile
, "static void __wine_unimplemented( const char *func )\n{\n" );
367 fprintf( outfile
, " struct exc_record rec;\n" );
368 fprintf( outfile
, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB
);
369 fprintf( outfile
, " rec.flags = %d;\n", EH_NONCONTINUABLE
);
370 fprintf( outfile
, " rec.rec = 0;\n" );
371 fprintf( outfile
, " rec.params = 2;\n" );
372 fprintf( outfile
, " rec.info[0] = dllname;\n" );
373 fprintf( outfile
, " rec.info[1] = func;\n" );
374 fprintf( outfile
, "#ifdef __GNUC__\n" );
375 fprintf( outfile
, " rec.addr = __builtin_return_address(1);\n" );
376 fprintf( outfile
, "#else\n" );
377 fprintf( outfile
, " rec.addr = 0;\n" );
378 fprintf( outfile
, "#endif\n" );
379 fprintf( outfile
, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
383 for (i
= 0; i
< nb_entry_points
; i
++)
385 ORDDEF
*odp
= EntryPoints
[i
];
386 if (odp
->type
!= TYPE_STUB
) continue;
387 fprintf( outfile
, "void %s(void) ", make_internal_name( odp
, "stub" ) );
389 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->name
);
391 fprintf( outfile
, "{ __wine_unimplemented(\"%d\"); }\n", odp
->ordinal
);
396 /*******************************************************************
397 * output_register_funcs
399 * Output the functions for register entry points
401 static void output_register_funcs( FILE *outfile
)
406 for (i
= 0; i
< nb_entry_points
; i
++)
408 ORDDEF
*odp
= EntryPoints
[i
];
409 if (odp
->type
!= TYPE_STDCALL
&& odp
->type
!= TYPE_CDECL
) continue;
410 if (!(odp
->flags
& FLAG_REGISTER
)) continue;
411 name
= make_internal_name( odp
, "regs" );
413 "asm(\".align %d\\n\\t\"\n"
414 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
415 " \"" PREFIX
"%s:\\n\\t\"\n"
416 " \"call " PREFIX
"__wine_call_from_32_regs\\n\\t\"\n"
417 " \".long " PREFIX
"%s\\n\\t\"\n"
418 " \".byte %d,%d\");\n",
420 name
, name
, odp
->link_name
,
421 strlen(odp
->u
.func
.arg_types
) * sizeof(int),
422 (odp
->type
== TYPE_CDECL
) ? 0 : (strlen(odp
->u
.func
.arg_types
) * sizeof(int)) );
427 /*******************************************************************
430 * Build a Win32 C file from a spec file.
432 void BuildSpec32File( FILE *outfile
)
434 int exports_size
= 0;
435 int nr_exports
, nr_imports
, nr_resources
, nr_debug
;
436 int characteristics
, subsystem
;
439 #ifdef HAVE_GETPAGESIZE
440 page_size
= getpagesize();
441 #elif defined(__svr4__)
442 page_size
= sysconf(_SC_PAGESIZE
);
443 #elif defined(_WINDOWS)
447 page_size
= si
.dwPageSize
;
450 # error Cannot get the page size on this platform
454 nr_exports
= Base
<= Limit
? Limit
- Base
+ 1 : 0;
457 output_standard_file_header( outfile
);
459 /* Reserve some space for the PE header */
461 fprintf( outfile
, "extern char pe_header[];\n" );
462 fprintf( outfile
, "asm(\".section .text\\n\\t\"\n" );
463 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(page_size
) );
464 fprintf( outfile
, " \"" PREFIX
"pe_header:\\t.fill %ld,1,0\\n\\t\");\n", page_size
);
466 fprintf( outfile
, "static const char dllname[] = \"%s\";\n\n", DLLName
);
467 fprintf( outfile
, "extern int __wine_spec_exports[];\n\n" );
470 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
472 fprintf( outfile
, "#define __stdcall\n\n" );
477 /* Output the stub functions */
479 output_stub_funcs( outfile
);
481 fprintf( outfile
, "#ifndef __GNUC__\n" );
482 fprintf( outfile
, "static void __asm__dummy(void) {\n" );
483 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
485 /* Output code for all register functions */
487 output_register_funcs( outfile
);
489 /* Output the exports and relay entry points */
491 exports_size
= output_exports( outfile
, nr_exports
);
493 fprintf( outfile
, "#ifndef __GNUC__\n" );
494 fprintf( outfile
, "}\n" );
495 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
498 /* Output the DLL imports */
500 nr_imports
= output_imports( outfile
);
502 /* Output the resources */
504 nr_resources
= output_resources( outfile
);
506 /* Output the debug channels */
508 nr_debug
= output_debug( outfile
);
510 /* Output LibMain function */
512 characteristics
= subsystem
= 0;
516 if (init_func
) fprintf( outfile
, "extern void %s();\n", init_func
);
517 characteristics
= IMAGE_FILE_DLL
;
519 case SPEC_MODE_GUIEXE
:
520 if (!init_func
) init_func
= "WinMain";
522 "\ntypedef struct {\n"
523 " unsigned int cb;\n"
524 " char *lpReserved, *lpDesktop, *lpTitle;\n"
525 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
526 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
527 " unsigned short wShowWindow, cbReserved2;\n"
528 " char *lpReserved2;\n"
529 " void *hStdInput, *hStdOutput, *hStdError;\n"
533 "extern int __stdcall %s(void *,void *,char *,int);\n"
534 "extern char * __stdcall GetCommandLineA(void);\n"
535 "extern void * __stdcall GetModuleHandleA(char *);\n"
536 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
537 "extern void __stdcall ExitProcess(unsigned int);\n"
538 "static void __wine_exe_main(void)\n"
540 " extern int __wine_get_main_args( char ***argv );\n"
541 " STARTUPINFOA info;\n"
542 " char *cmdline = GetCommandLineA();\n"
543 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
544 " if (*cmdline) cmdline++;\n"
545 " GetStartupInfoA( &info );\n"
546 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
547 " _ARGC = __wine_get_main_args( &_ARGV );\n"
548 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
549 "}\n\n", init_func
, init_func
);
550 init_func
= "__wine_exe_main";
551 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
553 case SPEC_MODE_GUIEXE_UNICODE
:
554 if (!init_func
) init_func
= "WinMain";
556 "\ntypedef unsigned short WCHAR;\n"
558 " unsigned int cb;\n"
559 " char *lpReserved, *lpDesktop, *lpTitle;\n"
560 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
561 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
562 " unsigned short wShowWindow, cbReserved2;\n"
563 " char *lpReserved2;\n"
564 " void *hStdInput, *hStdOutput, *hStdError;\n"
568 "extern int __stdcall %s(void *,void *,char *,int);\n"
569 "extern char * __stdcall GetCommandLineA(void);\n"
570 "extern void * __stdcall GetModuleHandleA(char *);\n"
571 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
572 "extern void __stdcall ExitProcess(unsigned int);\n"
573 "static void __wine_exe_main(void)\n"
575 " extern int __wine_get_wmain_args( WCHAR ***argv );\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_get_wmain_args( &_ARGV );\n"
583 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
584 "}\n\n", init_func
, init_func
);
585 init_func
= "__wine_exe_main";
586 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
588 case SPEC_MODE_CUIEXE
:
589 if (!init_func
) init_func
= "main";
593 "extern void __stdcall ExitProcess(int);\n"
594 "static void __wine_exe_main(void)\n"
596 " extern int %s( int argc, char *argv[] );\n"
597 " extern int __wine_get_main_args( char ***argv );\n"
598 " _ARGC = __wine_get_main_args( &_ARGV );\n"
599 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
600 "}\n\n", init_func
, init_func
);
601 init_func
= "__wine_exe_main";
602 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
604 case SPEC_MODE_CUIEXE_UNICODE
:
605 if (!init_func
) init_func
= "wmain";
607 "\ntypedef unsigned short WCHAR;\n"
610 "extern void __stdcall ExitProcess(int);\n"
611 "static void __wine_exe_main(void)\n"
613 " extern int %s( int argc, WCHAR *argv[] );\n"
614 " extern int __wine_get_wmain_args( WCHAR ***argv );\n"
615 " _ARGC = __wine_get_wmain_args( &_ARGV );\n"
616 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
617 "}\n\n", init_func
, init_func
);
618 init_func
= "__wine_exe_main";
619 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
623 /* Output the NT header */
625 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
626 fprintf( outfile
, "static const struct image_nt_headers\n{\n" );
627 fprintf( outfile
, " int Signature;\n" );
628 fprintf( outfile
, " struct file_header {\n" );
629 fprintf( outfile
, " short Machine;\n" );
630 fprintf( outfile
, " short NumberOfSections;\n" );
631 fprintf( outfile
, " int TimeDateStamp;\n" );
632 fprintf( outfile
, " void *PointerToSymbolTable;\n" );
633 fprintf( outfile
, " int NumberOfSymbols;\n" );
634 fprintf( outfile
, " short SizeOfOptionalHeader;\n" );
635 fprintf( outfile
, " short Characteristics;\n" );
636 fprintf( outfile
, " } FileHeader;\n" );
637 fprintf( outfile
, " struct opt_header {\n" );
638 fprintf( outfile
, " short Magic;\n" );
639 fprintf( outfile
, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
640 fprintf( outfile
, " int SizeOfCode;\n" );
641 fprintf( outfile
, " int SizeOfInitializedData;\n" );
642 fprintf( outfile
, " int SizeOfUninitializedData;\n" );
643 fprintf( outfile
, " void *AddressOfEntryPoint;\n" );
644 fprintf( outfile
, " void *BaseOfCode;\n" );
645 fprintf( outfile
, " void *BaseOfData;\n" );
646 fprintf( outfile
, " void *ImageBase;\n" );
647 fprintf( outfile
, " int SectionAlignment;\n" );
648 fprintf( outfile
, " int FileAlignment;\n" );
649 fprintf( outfile
, " short MajorOperatingSystemVersion;\n" );
650 fprintf( outfile
, " short MinorOperatingSystemVersion;\n" );
651 fprintf( outfile
, " short MajorImageVersion;\n" );
652 fprintf( outfile
, " short MinorImageVersion;\n" );
653 fprintf( outfile
, " short MajorSubsystemVersion;\n" );
654 fprintf( outfile
, " short MinorSubsystemVersion;\n" );
655 fprintf( outfile
, " int Win32VersionValue;\n" );
656 fprintf( outfile
, " int SizeOfImage;\n" );
657 fprintf( outfile
, " int SizeOfHeaders;\n" );
658 fprintf( outfile
, " int CheckSum;\n" );
659 fprintf( outfile
, " short Subsystem;\n" );
660 fprintf( outfile
, " short DllCharacteristics;\n" );
661 fprintf( outfile
, " int SizeOfStackReserve;\n" );
662 fprintf( outfile
, " int SizeOfStackCommit;\n" );
663 fprintf( outfile
, " int SizeOfHeapReserve;\n" );
664 fprintf( outfile
, " int SizeOfHeapCommit;\n" );
665 fprintf( outfile
, " int LoaderFlags;\n" );
666 fprintf( outfile
, " int NumberOfRvaAndSizes;\n" );
667 fprintf( outfile
, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
668 IMAGE_NUMBEROF_DIRECTORY_ENTRIES
);
669 fprintf( outfile
, " } OptionalHeader;\n" );
670 fprintf( outfile
, "} nt_header = {\n" );
671 fprintf( outfile
, " 0x%04x,\n", IMAGE_NT_SIGNATURE
); /* Signature */
673 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386
); /* Machine */
674 fprintf( outfile
, " 0, 0, 0, 0,\n" );
675 fprintf( outfile
, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
676 fprintf( outfile
, " 0x%04x },\n", characteristics
); /* Characteristics */
678 fprintf( outfile
, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC
); /* Magic */
679 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
680 fprintf( outfile
, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
681 fprintf( outfile
, " %s,\n", init_func
? init_func
: "0" ); /* AddressOfEntryPoint */
682 fprintf( outfile
, " 0, 0,\n" ); /* BaseOfCode/Data */
683 fprintf( outfile
, " pe_header,\n" ); /* ImageBase */
684 fprintf( outfile
, " %ld,\n", page_size
); /* SectionAlignment */
685 fprintf( outfile
, " %ld,\n", page_size
); /* FileAlignment */
686 fprintf( outfile
, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
687 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorImageVersion */
688 fprintf( outfile
, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
689 fprintf( outfile
, " 0,\n" ); /* Win32VersionValue */
690 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfImage */
691 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfHeaders */
692 fprintf( outfile
, " 0,\n" ); /* CheckSum */
693 fprintf( outfile
, " 0x%04x,\n", subsystem
); /* Subsystem */
694 fprintf( outfile
, " 0,\n" ); /* DllCharacteristics */
695 fprintf( outfile
, " %d, 0,\n", stack_size
*1024 ); /* SizeOfStackReserve/Commit */
696 fprintf( outfile
, " %d, 0,\n", DLLHeapSize
*1024 );/* SizeOfHeapReserve/Commit */
697 fprintf( outfile
, " 0,\n" ); /* LoaderFlags */
698 fprintf( outfile
, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES
); /* NumberOfRvaAndSizes */
699 fprintf( outfile
, " {\n" );
700 fprintf( outfile
, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
701 exports_size
? "__wine_spec_exports" : "0", exports_size
);
702 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
703 nr_imports
? "&imports" : "0", nr_imports
? "sizeof(imports)" : "0" );
704 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
705 nr_resources
? "&resources" : "0", nr_resources
? "sizeof(resources)" : "0" );
706 fprintf( outfile
, " }\n }\n};\n\n" );
708 /* Output the DLL constructor */
710 fprintf( outfile
, "#ifndef __GNUC__\n" );
711 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
712 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n" );
714 #if defined(__i386__)
715 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
716 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_init\\n\"\n", DLLName
);
717 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
720 fprintf( outfile
, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
721 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_fini\\n\"\n", DLLName
);
722 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", DLLName
);
727 fprintf( outfile
, " \"\\tnop\\n\"\n" );
728 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
731 fprintf( outfile
, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
732 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_fini\\n\"\n", DLLName
);
733 fprintf( outfile
, " \"\\tnop\\n\"\n" );
734 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
736 #elif defined(__PPC__)
737 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
738 fprintf( outfile
, " \"\\tbl " PREFIX
"__wine_spec_%s_init\\n\"\n",
740 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
743 fprintf( outfile
, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
744 fprintf( outfile
, " \"\\tbl " PREFIX
"__wine_spec_%s_fini\\n\"\n",
746 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
749 #error You need to define the DLL constructor for your architecture
752 fprintf( outfile
, "#ifndef __GNUC__\n" );
753 fprintf( outfile
, "}\n" );
754 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n\n" );
757 "void __wine_spec_%s_init(void)\n"
759 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
760 " extern void *__wine_dbg_register( char * const *, int );\n"
761 " __wine_dll_register( &nt_header, \"%s\" );\n",
762 DLLName
, DLLFileName
);
764 fprintf( outfile
, " debug_registration = __wine_dbg_register( debug_channels, %d );\n",
766 fprintf( outfile
, "}\n" );
770 "\nvoid __wine_spec_%s_fini(void)\n"
772 " extern void __wine_dbg_unregister( void* );\n"
773 " __wine_dbg_unregister( debug_registration );\n"
779 /*******************************************************************
782 * Build a Win32 def file from a spec file.
784 void BuildDef32File(FILE *outfile
)
790 fprintf(outfile
, "; File generated automatically from %s; do not edit!\n\n",
793 fprintf(outfile
, "LIBRARY %s\n\n", DLLFileName
);
795 fprintf(outfile
, "EXPORTS\n");
797 /* Output the exports and relay entry points */
799 for(i
= 0; i
< nb_entry_points
; i
++)
801 ORDDEF
*odp
= EntryPoints
[i
];
802 if(!odp
|| !*odp
->name
|| (odp
->flags
& FLAG_NOIMPORT
)) continue;
804 fprintf(outfile
, " %s", odp
->name
);
812 /* try to reduce output */
813 if(strcmp(odp
->name
, odp
->link_name
))
814 fprintf(outfile
, "=%s", odp
->link_name
);
818 #ifdef NEED_STDCALL_DECORATION
819 int at_param
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
820 fprintf(outfile
, "@%d", at_param
);
821 #endif /* NEED_STDCALL_DECORATION */
822 /* try to reduce output */
823 if(strcmp(odp
->name
, odp
->link_name
))
825 fprintf(outfile
, "=%s", odp
->link_name
);
826 #ifdef NEED_STDCALL_DECORATION
827 fprintf(outfile
, "@%d", at_param
);
828 #endif /* NEED_STDCALL_DECORATION */
833 fprintf(outfile
, "=%s", make_internal_name( odp
, "stub" ));
836 fprintf(outfile
, "=%s", odp
->link_name
);
841 fprintf(outfile
, " @%d\n", odp
->ordinal
);