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"
35 #include "wine/exception.h"
39 static int string_compare( const void *ptr1
, const void *ptr2
)
41 const char * const *str1
= ptr1
;
42 const char * const *str2
= ptr2
;
43 return strcmp( *str1
, *str2
);
47 /*******************************************************************
50 * Generate an internal name for an entry point. Used for stubs etc.
52 static const char *make_internal_name( const ORDDEF
*odp
, DLLSPEC
*spec
, const char *prefix
)
54 static char buffer
[256];
55 if (odp
->name
|| odp
->export_name
)
58 sprintf( buffer
, "__wine_%s_%s_%s", prefix
, spec
->file_name
,
59 odp
->name
? odp
->name
: odp
->export_name
);
60 /* make sure name is a legal C identifier */
61 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
62 if (!*p
) return buffer
;
64 sprintf( buffer
, "__wine_%s_%s_%d", prefix
, make_c_identifier(spec
->file_name
), odp
->ordinal
);
69 /*******************************************************************
72 * Output the debug channels.
74 static int output_debug( FILE *outfile
)
78 if (!nb_debug_channels
) return 0;
79 qsort( debug_channels
, nb_debug_channels
, sizeof(debug_channels
[0]), string_compare
);
81 for (i
= 0; i
< nb_debug_channels
; i
++)
82 fprintf( outfile
, "char __wine_dbch_%s[] = \"\\003%s\";\n",
83 debug_channels
[i
], debug_channels
[i
] );
85 fprintf( outfile
, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels
);
86 for (i
= 0; i
< nb_debug_channels
; i
++)
88 fprintf( outfile
, " __wine_dbch_%s", debug_channels
[i
] );
89 if (i
< nb_debug_channels
- 1) fprintf( outfile
, ",\n" );
91 fprintf( outfile
, "\n};\n\n" );
92 fprintf( outfile
, "static void *debug_registration;\n\n" );
94 return nb_debug_channels
;
98 /*******************************************************************
101 * Compute the size of the export table.
103 static int get_exports_size( DLLSPEC
*spec
)
105 int nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
106 int i
, strings_size
, total_size
;
108 if (!nr_exports
) return 0;
110 /* export directory header */
111 total_size
= 10 * sizeof(int);
113 /* function pointers */
114 total_size
+= nr_exports
* sizeof(int);
116 /* function name pointers */
117 total_size
+= spec
->nb_names
* sizeof(int);
119 /* function ordinals */
120 total_size
+= spec
->nb_names
* sizeof(short);
121 if (spec
->nb_names
% 2) total_size
+= sizeof(short);
123 /* export name strings */
124 strings_size
= strlen(spec
->file_name
) + 1;
125 for (i
= 0; i
< spec
->nb_names
; i
++)
126 strings_size
+= strlen(spec
->names
[i
]->name
) + 1;
128 /* forward strings */
129 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
131 ORDDEF
*odp
= spec
->ordinals
[i
];
132 if (odp
&& odp
->flags
& FLAG_FORWARD
) strings_size
+= strlen(odp
->link_name
) + 1;
134 total_size
+= (strings_size
+ 3) & ~3;
140 /*******************************************************************
143 * Output the export table for a Win32 module.
145 static void output_exports( FILE *outfile
, DLLSPEC
*spec
)
148 int nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
150 if (!nr_exports
) return;
152 fprintf( outfile
, "/* export table */\n" );
153 fprintf( outfile
, "asm(\".data\\n\"\n" );
154 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
155 fprintf( outfile
, " \"%s:\\n\"\n", asm_name("__wine_spec_exports") );
157 /* export directory header */
159 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
160 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
161 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
162 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
163 fprintf( outfile
, " \"\\t.long %d\\n\"\n", spec
->base
); /* Base */
164 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nr_exports
); /* NumberOfFunctions */
165 fprintf( outfile
, " \"\\t.long %d\\n\"\n", spec
->nb_names
); /* NumberOfNames */
166 fprintf( outfile
, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
169 fprintf( outfile
, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
170 fprintf( outfile
, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
174 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
175 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
178 /* output the function pointers */
180 fprintf( outfile
, " \"__wine_spec_exports_funcs:\\n\"\n" );
181 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
183 ORDDEF
*odp
= spec
->ordinals
[i
];
184 if (!odp
) fprintf( outfile
, " \"\\t.long 0\\n\"\n" );
185 else switch(odp
->type
)
191 if (!(odp
->flags
& FLAG_FORWARD
))
193 fprintf( outfile
, " \"\\t.long %s\\n\"\n", asm_name(odp
->link_name
) );
197 fprintf( outfile
, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size
);
198 fwd_size
+= strlen(odp
->link_name
) + 1;
202 fprintf( outfile
, " \"\\t.long %s\\n\"\n",
203 asm_name( make_internal_name( odp
, spec
, "stub" )) );
212 /* output the function name pointers */
214 int namepos
= strlen(spec
->file_name
) + 1;
216 fprintf( outfile
, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
217 for (i
= 0; i
< spec
->nb_names
; i
++)
219 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos
);
220 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
223 /* output the function ordinals */
225 fprintf( outfile
, " \"__wine_spec_exp_ordinals:\\n\"\n" );
226 for (i
= 0; i
< spec
->nb_names
; i
++)
228 fprintf( outfile
, " \"\\t%s %d\\n\"\n",
229 get_asm_short_keyword(), spec
->names
[i
]->ordinal
- spec
->base
);
231 if (spec
->nb_names
% 2)
233 fprintf( outfile
, " \"\\t%s 0\\n\"\n", get_asm_short_keyword() );
237 /* output the export name strings */
239 fprintf( outfile
, " \"__wine_spec_exp_names:\\n\"\n" );
240 fprintf( outfile
, " \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), spec
->file_name
);
241 for (i
= 0; i
< spec
->nb_names
; i
++)
242 fprintf( outfile
, " \"\\t%s \\\"%s\\\"\\n\"\n",
243 get_asm_string_keyword(), spec
->names
[i
]->name
);
245 /* output forward strings */
249 fprintf( outfile
, " \"__wine_spec_forwards:\\n\"\n" );
250 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
252 ORDDEF
*odp
= spec
->ordinals
[i
];
253 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
254 fprintf( outfile
, " \"\\t%s \\\"%s\\\"\\n\"\n", get_asm_string_keyword(), odp
->link_name
);
257 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
261 /* we only support relay debugging on i386 */
262 if (target_cpu
== CPU_x86
)
264 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
266 ORDDEF
*odp
= spec
->ordinals
[i
];
267 unsigned int j
, args
, mask
= 0;
269 /* skip nonexistent entry points */
270 if (!odp
) goto ignore
;
271 /* skip non-functions */
272 if ((odp
->type
!= TYPE_STDCALL
) && (odp
->type
!= TYPE_CDECL
)) goto ignore
;
273 /* skip norelay and forward entry points */
274 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) goto ignore
;
276 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
278 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
279 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
281 if ((odp
->flags
& FLAG_RET64
) && (j
< 16)) mask
|= 0x80000000;
283 args
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
288 fprintf( outfile
, " \"\\tjmp %s\\n\"\n", asm_name(odp
->link_name
) );
289 fprintf( outfile
, " \"\\tret $%d\\n\"\n", args
);
290 fprintf( outfile
, " \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp
->link_name
), mask
);
293 fprintf( outfile
, " \"\\tjmp %s\\n\"\n", asm_name(odp
->link_name
) );
294 fprintf( outfile
, " \"\\tret\\n\"\n" );
295 fprintf( outfile
, " \"\\t%s %d\\n\"\n", get_asm_short_keyword(), args
);
296 fprintf( outfile
, " \"\\t.long %s,0x%08x\\n\"\n", asm_name(odp
->link_name
), mask
);
304 fprintf( outfile
, " \"\\t.long 0,0,0,0\\n\"\n" );
307 fprintf( outfile
, ");\n" );
311 /*******************************************************************
314 * Output the functions for stub entry points
316 static void output_stub_funcs( FILE *outfile
, DLLSPEC
*spec
)
320 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
322 ORDDEF
*odp
= &spec
->entry_points
[i
];
323 if (odp
->type
!= TYPE_STUB
) continue;
324 fprintf( outfile
, "#ifdef __GNUC__\n" );
325 fprintf( outfile
, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func ) __attribute__((noreturn));\n" );
326 fprintf( outfile
, "#else\n" );
327 fprintf( outfile
, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func );\n" );
328 fprintf( outfile
, "#endif\n\n" );
332 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
334 const ORDDEF
*odp
= &spec
->entry_points
[i
];
335 if (odp
->type
!= TYPE_STUB
) continue;
336 fprintf( outfile
, "void %s(void) ", make_internal_name( odp
, spec
, "stub" ) );
338 fprintf( outfile
, "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp
->name
);
339 else if (odp
->export_name
)
340 fprintf( outfile
, "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp
->export_name
);
342 fprintf( outfile
, "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%d\"); }\n", odp
->ordinal
);
347 /*******************************************************************
350 * Output code for calling a dll constructor and destructor.
352 void output_dll_init( FILE *outfile
, const char *constructor
, const char *destructor
)
354 if (target_platform
== PLATFORM_APPLE
)
356 /* Mach-O doesn't have an init section */
359 fprintf( outfile
, "asm(\"\\t.mod_init_func\\n\"\n" );
360 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
361 fprintf( outfile
, " \"\\t.long %s\\n\"\n", asm_name(constructor
) );
362 fprintf( outfile
, " \"\\t.text\\n\");\n" );
366 fprintf( outfile
, "asm(\"\\t.mod_term_func\\n\"\n" );
367 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
368 fprintf( outfile
, " \"\\t.long %s\\n\"\n", asm_name(destructor
) );
369 fprintf( outfile
, " \"\\t.text\\n\");\n" );
372 else switch(target_cpu
)
377 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
378 fprintf( outfile
, " \"\\tcall %s\\n\"\n", asm_name(constructor
) );
379 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
383 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
384 fprintf( outfile
, " \"\\tcall %s\\n\"\n", asm_name(destructor
) );
385 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
391 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
392 fprintf( outfile
, " \"\\tcall %s\\n\"\n", asm_name(constructor
) );
393 fprintf( outfile
, " \"\\tnop\\n\"\n" );
394 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
398 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
399 fprintf( outfile
, " \"\\tcall %s\\n\"\n", asm_name(destructor
) );
400 fprintf( outfile
, " \"\\tnop\\n\"\n" );
401 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
407 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
408 fprintf( outfile
, " \"\\tjsr $26,%s\\n\"\n", asm_name(constructor
) );
409 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
413 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
414 fprintf( outfile
, " \"\\tjsr $26,%s\\n\"\n", asm_name(destructor
) );
415 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
421 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
422 fprintf( outfile
, " \"\\tbl %s\\n\"\n", asm_name(constructor
) );
423 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
427 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
428 fprintf( outfile
, " \"\\tbl %s\\n\"\n", asm_name(destructor
) );
429 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
436 /*******************************************************************
439 * Build a Win32 C file from a spec file.
441 void BuildSpec32File( FILE *outfile
, DLLSPEC
*spec
)
443 int exports_size
= 0;
444 int nr_exports
, nr_imports
, nr_delayed
;
445 unsigned int page_size
= get_page_size();
446 const char *init_func
= spec
->init_func
;
448 nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
449 resolve_imports( spec
);
450 exports_size
= get_exports_size( spec
);
451 output_standard_file_header( outfile
);
453 /* Reserve some space for the PE header */
455 fprintf( outfile
, "extern char __wine_spec_pe_header[];\n" );
456 fprintf( outfile
, "#ifndef __GNUC__\n" );
457 fprintf( outfile
, "static void __asm__dummy_header(void) {\n" );
458 fprintf( outfile
, "#endif\n" );
459 fprintf( outfile
, "asm(\".text\\n\\t\"\n" );
460 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(page_size
) );
461 fprintf( outfile
, " \"%s:\\t\"\n", asm_name("__wine_spec_pe_header") );
462 if (target_platform
== PLATFORM_APPLE
)
463 fprintf( outfile
, " \".space 65536\\n\\t\"\n" );
465 fprintf( outfile
, " \".skip 65536\\n\\t\"\n" );
466 fprintf( outfile
, " \".data\\n\\t\"\n" );
467 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(4) );
468 fprintf( outfile
, " \"%s:\\t.long 1\");\n", asm_name("__wine_spec_data_start") );
469 fprintf( outfile
, "#ifndef __GNUC__\n" );
470 fprintf( outfile
, "}\n" );
471 fprintf( outfile
, "#endif\n" );
473 if (target_platform
== PLATFORM_APPLE
)
474 fprintf( outfile
, "static char _end[4];\n" );
476 fprintf( outfile
, "extern char _end[];\n" );
478 fprintf( outfile
, "static const char __wine_spec_file_name[] = \"%s\";\n", spec
->file_name
);
479 fprintf( outfile
, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
481 if (target_cpu
== CPU_x86
)
482 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
484 fprintf( outfile
, "#define __stdcall\n\n" );
486 output_stub_funcs( outfile
, spec
);
488 /* Output the DLL imports */
490 nr_imports
= output_imports( outfile
, spec
, &nr_delayed
);
492 /* Output the resources */
494 output_resources( outfile
, spec
);
496 /* Output the entry point function */
498 fprintf( outfile
, "static int __wine_spec_init_state;\n" );
499 fprintf( outfile
, "extern int __wine_main_argc;\n" );
500 fprintf( outfile
, "extern char **__wine_main_argv;\n" );
501 fprintf( outfile
, "extern char **__wine_main_environ;\n" );
502 if (target_platform
== PLATFORM_APPLE
)
504 fprintf( outfile
, "extern _dyld_func_lookup(char *, void *);" );
505 fprintf( outfile
, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
506 fprintf( outfile
, "{\n" );
507 fprintf( outfile
, " void (*init)(void);\n" );
508 fprintf( outfile
, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
509 fprintf( outfile
, " init();\n" );
510 fprintf( outfile
, "}\n" );
511 fprintf( outfile
, "static void __wine_spec_hidden_fini()\n" );
512 fprintf( outfile
, "{\n" );
513 fprintf( outfile
, " void (*fini)(void);\n" );
514 fprintf( outfile
, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
515 fprintf( outfile
, " fini();\n" );
516 fprintf( outfile
, "}\n" );
517 fprintf( outfile
, "#define _init __wine_spec_hidden_init\n" );
518 fprintf( outfile
, "#define _fini __wine_spec_hidden_fini\n" );
522 fprintf( outfile
, "extern void _init(int, char**, char**);\n" );
523 fprintf( outfile
, "extern void _fini();\n" );
526 if (spec
->characteristics
& IMAGE_FILE_DLL
)
528 if (!init_func
) init_func
= "DllMain";
529 fprintf( outfile
, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func
);
531 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
534 " if (reason == %d && __wine_spec_init_state == 1)\n"
535 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
536 " ret = %s( inst, reason, reserved );\n"
537 " if (reason == %d && __wine_spec_init_state == 1)\n",
538 DLL_PROCESS_ATTACH
, init_func
, DLL_PROCESS_DETACH
);
540 fprintf( outfile
, " _fini();\n" );
544 " extern int __stdcall FreeLibrary(void *);\n"
547 " for (i = 0; i < sizeof(__wine_delay_imp_hmod)/sizeof(__wine_delay_imp_hmod[0]); i++)\n"
548 " if (__wine_delay_imp_hmod[i]) FreeLibrary( __wine_delay_imp_hmod[i] );\n"
550 fprintf( outfile
, " return ret;\n}\n" );
551 init_func
= "__wine_dll_main";
553 else switch(spec
->subsystem
)
555 case IMAGE_SUBSYSTEM_NATIVE
:
556 if (!init_func
) init_func
= "DriverEntry";
557 fprintf( outfile
, "extern int __stdcall %s( void*, void* );\n\n", init_func
);
559 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
562 " if (__wine_spec_init_state == 1)\n"
563 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
564 " ret = %s( obj, path );\n"
565 " if (__wine_spec_init_state == 1) _fini();\n"
569 init_func
= "__wine_driver_entry";
571 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
572 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
573 if (!init_func
) init_func
= "main";
574 else if (!strcmp( init_func
, "wmain" )) /* FIXME: temp hack for crt0 support */
576 fprintf( outfile
, "extern int wmain( int argc, unsigned short *argv[] );\n" );
577 fprintf( outfile
, "extern unsigned short **__wine_main_wargv;\n" );
579 "\nextern void __stdcall ExitProcess(unsigned int);\n"
580 "static void __wine_exe_wmain(void)\n"
583 " if (__wine_spec_init_state == 1)\n"
584 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
585 " ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
586 " if (__wine_spec_init_state == 1) _fini();\n"
587 " ExitProcess( ret );\n"
589 init_func
= "__wine_exe_wmain";
592 fprintf( outfile
, "extern int %s( int argc, char *argv[] );\n", init_func
);
594 "\nextern void __stdcall ExitProcess(unsigned int);\n"
595 "static void __wine_exe_main(void)\n"
598 " if (__wine_spec_init_state == 1)\n"
599 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
600 " ret = %s( __wine_main_argc, __wine_main_argv );\n"
601 " if (__wine_spec_init_state == 1) _fini();\n"
602 " ExitProcess( ret );\n"
603 "}\n\n", init_func
);
604 init_func
= "__wine_exe_main";
608 /* Output the NT header */
610 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
611 fprintf( outfile
, "static const struct image_nt_headers\n{\n" );
612 fprintf( outfile
, " int Signature;\n" );
613 fprintf( outfile
, " struct file_header {\n" );
614 fprintf( outfile
, " short Machine;\n" );
615 fprintf( outfile
, " short NumberOfSections;\n" );
616 fprintf( outfile
, " int TimeDateStamp;\n" );
617 fprintf( outfile
, " void *PointerToSymbolTable;\n" );
618 fprintf( outfile
, " int NumberOfSymbols;\n" );
619 fprintf( outfile
, " short SizeOfOptionalHeader;\n" );
620 fprintf( outfile
, " short Characteristics;\n" );
621 fprintf( outfile
, " } FileHeader;\n" );
622 fprintf( outfile
, " struct opt_header {\n" );
623 fprintf( outfile
, " short Magic;\n" );
624 fprintf( outfile
, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
625 fprintf( outfile
, " int SizeOfCode;\n" );
626 fprintf( outfile
, " int SizeOfInitializedData;\n" );
627 fprintf( outfile
, " int SizeOfUninitializedData;\n" );
628 fprintf( outfile
, " void *AddressOfEntryPoint;\n" );
629 fprintf( outfile
, " void *BaseOfCode;\n" );
630 fprintf( outfile
, " void *BaseOfData;\n" );
631 fprintf( outfile
, " void *ImageBase;\n" );
632 fprintf( outfile
, " int SectionAlignment;\n" );
633 fprintf( outfile
, " int FileAlignment;\n" );
634 fprintf( outfile
, " short MajorOperatingSystemVersion;\n" );
635 fprintf( outfile
, " short MinorOperatingSystemVersion;\n" );
636 fprintf( outfile
, " short MajorImageVersion;\n" );
637 fprintf( outfile
, " short MinorImageVersion;\n" );
638 fprintf( outfile
, " short MajorSubsystemVersion;\n" );
639 fprintf( outfile
, " short MinorSubsystemVersion;\n" );
640 fprintf( outfile
, " int Win32VersionValue;\n" );
641 fprintf( outfile
, " void *SizeOfImage;\n" );
642 fprintf( outfile
, " int SizeOfHeaders;\n" );
643 fprintf( outfile
, " int CheckSum;\n" );
644 fprintf( outfile
, " short Subsystem;\n" );
645 fprintf( outfile
, " short DllCharacteristics;\n" );
646 fprintf( outfile
, " int SizeOfStackReserve;\n" );
647 fprintf( outfile
, " int SizeOfStackCommit;\n" );
648 fprintf( outfile
, " int SizeOfHeapReserve;\n" );
649 fprintf( outfile
, " int SizeOfHeapCommit;\n" );
650 fprintf( outfile
, " int LoaderFlags;\n" );
651 fprintf( outfile
, " int NumberOfRvaAndSizes;\n" );
652 fprintf( outfile
, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
653 IMAGE_NUMBEROF_DIRECTORY_ENTRIES
);
654 fprintf( outfile
, " } OptionalHeader;\n" );
655 fprintf( outfile
, "} nt_header = {\n" );
656 fprintf( outfile
, " 0x%04x,\n", IMAGE_NT_SIGNATURE
); /* Signature */
660 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386
); /* Machine */
663 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC
); /* Machine */
666 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_ALPHA
); /* Machine */
669 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN
); /* Machine */
672 fprintf( outfile
, " 0, 0, 0, 0,\n" );
673 fprintf( outfile
, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
674 fprintf( outfile
, " 0x%04x },\n", spec
->characteristics
); /* Characteristics */
676 fprintf( outfile
, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC
); /* Magic */
677 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
678 fprintf( outfile
, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
679 fprintf( outfile
, " %s,\n", init_func
); /* AddressOfEntryPoint */
680 fprintf( outfile
, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
681 fprintf( outfile
, " __wine_spec_pe_header,\n" ); /* ImageBase */
682 fprintf( outfile
, " %u,\n", page_size
); /* SectionAlignment */
683 fprintf( outfile
, " %u,\n", page_size
); /* FileAlignment */
684 fprintf( outfile
, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
685 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorImageVersion */
686 fprintf( outfile
, " %d,\n", spec
->subsystem_major
); /* MajorSubsystemVersion */
687 fprintf( outfile
, " %d,\n", spec
->subsystem_minor
); /* MinorSubsystemVersion */
688 fprintf( outfile
, " 0,\n" ); /* Win32VersionValue */
689 fprintf( outfile
, " _end,\n" ); /* SizeOfImage */
690 fprintf( outfile
, " %u,\n", page_size
); /* SizeOfHeaders */
691 fprintf( outfile
, " 0,\n" ); /* CheckSum */
692 fprintf( outfile
, " 0x%04x,\n", spec
->subsystem
);/* Subsystem */
693 fprintf( outfile
, " 0,\n" ); /* DllCharacteristics */
694 fprintf( outfile
, " %u, %u,\n", /* SizeOfStackReserve/Commit */
695 (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
696 fprintf( outfile
, " %u, %u,\n", /* SizeOfHeapReserve/Commit */
697 (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
698 fprintf( outfile
, " 0,\n" ); /* LoaderFlags */
699 fprintf( outfile
, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES
); /* NumberOfRvaAndSizes */
700 fprintf( outfile
, " {\n" );
701 fprintf( outfile
, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
702 exports_size
? "__wine_spec_exports" : "0", exports_size
);
703 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
704 nr_imports
? "&imports" : "0", nr_imports
? "sizeof(imports)" : "0" );
705 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
706 spec
->nb_resources
? "&__wine_spec_resources" : "0",
707 spec
->nb_resources
? "sizeof(__wine_spec_resources)" : "0" );
708 fprintf( outfile
, " }\n }\n};\n\n" );
710 /* Output the DLL constructor */
713 "void __wine_spec_init(void)\n"
715 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
716 " __wine_spec_init_state = 1;\n"
717 " __wine_dll_register( &nt_header, __wine_spec_file_name );\n"
721 "void __wine_spec_init_ctor(void)\n"
723 " if (__wine_spec_init_state) return;\n"
724 " __wine_spec_init();\n"
725 " __wine_spec_init_state = 2;\n"
728 fprintf( outfile
, "#ifndef __GNUC__\n" );
729 fprintf( outfile
, "static void __asm__dummy(void) {\n" );
730 fprintf( outfile
, "#endif\n" );
732 output_exports( outfile
, spec
);
733 output_import_thunks( outfile
, spec
);
734 output_dll_init( outfile
, "__wine_spec_init_ctor", NULL
);
736 fprintf( outfile
, "#ifndef __GNUC__\n" );
737 fprintf( outfile
, "}\n" );
738 fprintf( outfile
, "#endif\n" );
742 /*******************************************************************
745 * Build a Win32 def file from a spec file.
747 void BuildDef32File( FILE *outfile
, DLLSPEC
*spec
)
753 fprintf( outfile
, "; File generated automatically from %s; do not edit!\n\n",
756 fprintf( outfile
, "; File generated automatically; do not edit!\n\n" );
758 fprintf(outfile
, "LIBRARY %s\n\n", spec
->file_name
);
760 fprintf(outfile
, "EXPORTS\n");
762 /* Output the exports and relay entry points */
764 for (i
= total
= 0; i
< spec
->nb_entry_points
; i
++)
766 const ORDDEF
*odp
= &spec
->entry_points
[i
];
771 if (odp
->name
) name
= odp
->name
;
772 else if (odp
->export_name
) name
= odp
->export_name
;
775 if (!(odp
->flags
& FLAG_PRIVATE
)) total
++;
777 if (odp
->type
== TYPE_STUB
) continue;
779 fprintf(outfile
, " %s", name
);
788 /* try to reduce output */
789 if(strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
))
790 fprintf(outfile
, "=%s", odp
->link_name
);
794 int at_param
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
795 if (!kill_at
) fprintf(outfile
, "@%d", at_param
);
796 if (odp
->flags
& FLAG_FORWARD
)
798 fprintf(outfile
, "=%s", odp
->link_name
);
800 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
802 fprintf(outfile
, "=%s", odp
->link_name
);
803 if (!kill_at
) fprintf(outfile
, "@%d", at_param
);
810 fprintf( outfile
, " @%d", odp
->ordinal
);
811 if (!odp
->name
) fprintf( outfile
, " NONAME" );
812 if (is_data
) fprintf( outfile
, " DATA" );
813 if (odp
->flags
& FLAG_PRIVATE
) fprintf( outfile
, " PRIVATE" );
814 fprintf( outfile
, "\n" );
816 if (!total
) warning( "%s: Import library doesn't export anything\n", spec
->file_name
);
820 /*******************************************************************
823 * Build the debugging channels source file.
825 void BuildDebugFile( FILE *outfile
, const char *srcdir
, char **argv
)
828 char *prefix
, *p
, *constructor
, *destructor
;
832 if (!parse_debug_channels( srcdir
, *argv
++ )) exit(1);
835 output_standard_file_header( outfile
);
836 nr_debug
= output_debug( outfile
);
839 fprintf( outfile
, "/* no debug channels found for this module */\n" );
843 if (output_file_name
)
845 if ((p
= strrchr( output_file_name
, '/' ))) p
++;
846 prefix
= xstrdup( p
? p
: output_file_name
);
847 if ((p
= strchr( prefix
, '.' ))) *p
= 0;
848 strcpy( p
, make_c_identifier(p
) );
850 else prefix
= xstrdup( "_" );
852 /* Output the DLL constructor */
854 constructor
= xmalloc( strlen(prefix
) + 17 );
855 destructor
= xmalloc( strlen(prefix
) + 17 );
856 sprintf( constructor
, "__wine_dbg_%s_init", prefix
);
857 sprintf( destructor
, "__wine_dbg_%s_fini", prefix
);
860 "void %s(void) __attribute__((constructor));\n"
861 "void %s(void) __attribute__((destructor));\n"
863 "static void __asm__dummy_dll_init(void) {\n",
864 constructor
, destructor
);
865 output_dll_init( outfile
, constructor
, destructor
);
866 fprintf( outfile
, "}\n#endif /* defined(__GNUC__) */\n\n" );
871 " extern void *__wine_dbg_register( char * const *, int );\n"
872 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
873 "}\n\n", constructor
, nr_debug
);
877 " extern void __wine_dbg_unregister( void* );\n"
878 " __wine_dbg_unregister( debug_registration );\n"