- Fix building on Linux.
[reactos.git] / reactos / tools / winebuild / spec32.c
1 /*
2 * 32-bit spec files
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "config.h"
26
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <string.h>
31
32 #include "winglue.h"
33
34 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
35 #define EH_NONCONTINUABLE 0x01
36
37 #include "build.h"
38
39
40 #ifdef __APPLE__
41 # define __ASM_SKIP ".space"
42 #else
43 # define __ASM_SKIP ".skip"
44 #endif
45
46 static int string_compare( const void *ptr1, const void *ptr2 )
47 {
48 const char * const *str1 = ptr1;
49 const char * const *str2 = ptr2;
50 return strcmp( *str1, *str2 );
51 }
52
53
54 /*******************************************************************
55 * make_internal_name
56 *
57 * Generate an internal name for an entry point. Used for stubs etc.
58 */
59 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
60 {
61 static char buffer[256];
62 if (odp->name || odp->export_name)
63 {
64 char *p;
65 sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->file_name,
66 odp->name ? odp->name : odp->export_name );
67 /* make sure name is a legal C identifier */
68 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
69 if (!*p) return buffer;
70 }
71 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
72 return buffer;
73 }
74
75 /*******************************************************************
76 * declare_weak_function
77 *
78 * Output a prototype for a weak function.
79 */
80 static void declare_weak_function( FILE *outfile, const char *ret_type, const char *name, const char *params)
81 {
82 fprintf( outfile, "#ifdef __GNUC__\n" );
83 fprintf( outfile, "# ifdef __APPLE__\n" );
84 fprintf( outfile, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type, name, params );
85 fprintf( outfile, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type, name, params, name );
86 fprintf( outfile, "#define %s __wine_spec_weak_%s\n", name, name );
87 fprintf( outfile, "# else\n" );
88 fprintf( outfile, "extern %s %s(%s) __attribute__((weak));\n", ret_type, name, params );
89 fprintf( outfile, "# endif\n" );
90 fprintf( outfile, "#else\n" );
91 fprintf( outfile, "extern %s %s(%s);\n", ret_type, name, params );
92 fprintf( outfile, "static void __asm__dummy_%s(void)", name );
93 fprintf( outfile, " { asm(\".weak " __ASM_NAME("%s") "\"); }\n", name );
94 fprintf( outfile, "#endif\n\n" );
95 }
96
97
98 /*******************************************************************
99 * output_debug
100 *
101 * Output the debug channels.
102 */
103 static int output_debug( FILE *outfile )
104 {
105 int i;
106
107 if (!nb_debug_channels) return 0;
108 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
109
110 for (i = 0; i < nb_debug_channels; i++)
111 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
112 debug_channels[i], debug_channels[i] );
113
114 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
115 for (i = 0; i < nb_debug_channels; i++)
116 {
117 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
118 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
119 }
120 fprintf( outfile, "\n};\n\n" );
121 fprintf( outfile, "static void *debug_registration;\n\n" );
122
123 return nb_debug_channels;
124 }
125
126
127 /*******************************************************************
128 * output_exports
129 *
130 * Output the export table for a Win32 module.
131 */
132 static int output_exports( FILE *outfile, int nr_exports, DLLSPEC *spec )
133 {
134 int i, fwd_size = 0, total_size = 0;
135
136 if (!nr_exports) return 0;
137
138 fprintf( outfile, "asm(\".data\\n\"\n" );
139 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
140 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
141
142 /* export directory header */
143
144 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
145 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
146 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
147 fprintf( outfile, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
148 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->base ); /* Base */
149 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
150 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->nb_names ); /* NumberOfNames */
151 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
152 if (spec->nb_names)
153 {
154 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
155 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
156 }
157 else
158 {
159 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
160 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
161 }
162 total_size += 10 * sizeof(int);
163
164 /* output the function pointers */
165
166 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
167 for (i = spec->base; i <= spec->limit; i++)
168 {
169 ORDDEF *odp = spec->ordinals[i];
170 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
171 else switch(odp->type)
172 {
173 case TYPE_EXTERN:
174 case TYPE_STDCALL:
175 case TYPE_VARARGS:
176 case TYPE_CDECL:
177 if (!(odp->flags & FLAG_FORWARD))
178 {
179 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
180 (odp->flags & FLAG_REGISTER) ? make_internal_name( odp, spec, "regs" )
181 : odp->link_name );
182 }
183 else
184 {
185 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
186 fwd_size += strlen(odp->link_name) + 1;
187 }
188 break;
189 case TYPE_STUB:
190 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
191 make_internal_name( odp, spec, "stub" ) );
192 break;
193 default:
194 assert(0);
195 }
196 }
197 total_size += (spec->limit - spec->base + 1) * sizeof(int);
198
199 if (spec->nb_names)
200 {
201 /* output the function name pointers */
202
203 int namepos = strlen(spec->file_name) + 1;
204
205 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
206 for (i = 0; i < spec->nb_names; i++)
207 {
208 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
209 namepos += strlen(spec->names[i]->name) + 1;
210 }
211 total_size += spec->nb_names * sizeof(int);
212 }
213
214 /* output the function names */
215
216 fprintf( outfile, " \"\\t.text\\n\"\n" );
217 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
218 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", spec->file_name );
219 for (i = 0; i < spec->nb_names; i++)
220 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", spec->names[i]->name );
221 fprintf( outfile, " \"\\t.data\\n\"\n" );
222
223 if (spec->nb_names)
224 {
225 /* output the function ordinals */
226
227 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
228 for (i = 0; i < spec->nb_names; i++)
229 {
230 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n",
231 spec->names[i]->ordinal - spec->base );
232 }
233 total_size += spec->nb_names * sizeof(short);
234 if (spec->nb_names % 2)
235 {
236 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
237 total_size += sizeof(short);
238 }
239 }
240
241 /* output forward strings */
242
243 if (fwd_size)
244 {
245 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
246 for (i = spec->base; i <= spec->limit; i++)
247 {
248 ORDDEF *odp = spec->ordinals[i];
249 if (odp && (odp->flags & FLAG_FORWARD))
250 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
251 }
252 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
253 total_size += (fwd_size + 3) & ~3;
254 }
255
256 /* output relays */
257
258 if (debugging)
259 {
260 for (i = spec->base; i <= spec->limit; i++)
261 {
262 ORDDEF *odp = spec->ordinals[i];
263 unsigned int j, args, mask = 0;
264 const char *name;
265
266 /* skip non-existent entry points */
267 if (!odp) goto ignore;
268 /* skip non-functions */
269 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
270 /* skip norelay and forward entry points */
271 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
272
273 for (j = 0; odp->u.func.arg_types[j]; j++)
274 {
275 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
276 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
277 }
278 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
279
280 name = odp->link_name;
281 args = strlen(odp->u.func.arg_types) * sizeof(int);
282 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, spec, "regs" );
283
284 switch(odp->type)
285 {
286 case TYPE_STDCALL:
287 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
288 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
289 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
290 break;
291 case TYPE_CDECL:
292 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
293 fprintf( outfile, " \"\\tret\\n\"\n" );
294 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
295 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
296 break;
297 default:
298 assert(0);
299 }
300 continue;
301
302 ignore:
303 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
304 }
305 }
306
307 fprintf( outfile, " \"\\t.text\\n\"\n" );
308 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
309 fprintf( outfile, ");\n\n" );
310
311 return total_size;
312 }
313
314
315 /*******************************************************************
316 * output_stub_funcs
317 *
318 * Output the functions for stub entry points
319 */
320 static void output_stub_funcs( FILE *outfile, DLLSPEC *spec )
321 {
322 int i;
323
324 for (i = 0; i < spec->nb_entry_points; i++)
325 {
326 ORDDEF *odp = &spec->entry_points[i];
327 if (odp->type != TYPE_STUB) continue;
328 fprintf( outfile, "#ifdef __GNUC__\n" );
329 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
330 fprintf( outfile, "#endif\n\n" );
331 fprintf( outfile, "struct exc_record {\n" );
332 fprintf( outfile, " unsigned int code, flags;\n" );
333 fprintf( outfile, " void *rec, *addr;\n" );
334 fprintf( outfile, " unsigned int params;\n" );
335 fprintf( outfile, " const void *info[15];\n" );
336 fprintf( outfile, "};\n\n" );
337 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
338 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
339 fprintf( outfile, " struct exc_record rec;\n" );
340 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
341 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
342 fprintf( outfile, " rec.rec = 0;\n" );
343 fprintf( outfile, " rec.params = 2;\n" );
344 fprintf( outfile, " rec.info[0] = \"%s\";\n", spec->file_name );
345 fprintf( outfile, " rec.info[1] = func;\n" );
346 fprintf( outfile, "#ifdef __GNUC__\n" );
347 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
348 fprintf( outfile, "#else\n" );
349 fprintf( outfile, " rec.addr = 0;\n" );
350 fprintf( outfile, "#endif\n" );
351 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
352 break;
353 }
354
355 for (i = 0; i < spec->nb_entry_points; i++)
356 {
357 const ORDDEF *odp = &spec->entry_points[i];
358 if (odp->type != TYPE_STUB) continue;
359 fprintf( outfile, "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
360 if (odp->name)
361 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
362 else if (odp->export_name)
363 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
364 else
365 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
366 }
367 }
368
369
370 /*******************************************************************
371 * output_register_funcs
372 *
373 * Output the functions for register entry points
374 */
375 static void output_register_funcs( FILE *outfile, DLLSPEC *spec )
376 {
377 const char *name;
378 int i;
379
380 for (i = 0; i < spec->nb_entry_points; i++)
381 {
382 const ORDDEF *odp = &spec->entry_points[i];
383 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
384 if (!(odp->flags & FLAG_REGISTER)) continue;
385 if (odp->flags & FLAG_FORWARD) continue;
386 name = make_internal_name( odp, spec, "regs" );
387 fprintf( outfile,
388 "asm(\".align %d\\n\\t\"\n"
389 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
390 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
391 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
392 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
393 " \".byte %d,%d\");\n",
394 get_alignment(4),
395 name, name, odp->link_name,
396 strlen(odp->u.func.arg_types) * sizeof(int),
397 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
398 }
399 }
400
401
402 /*******************************************************************
403 * output_dll_init
404 *
405 * Output code for calling a dll constructor and destructor.
406 */
407 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
408 {
409 fprintf( outfile, "#ifndef __GNUC__\n" );
410 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
411 fprintf( outfile, "#endif\n" );
412
413 #if defined(__i386__)
414 if (constructor)
415 {
416 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
417 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
418 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
419 }
420 if (destructor)
421 {
422 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
423 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
424 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
425 }
426 #elif defined(__sparc__)
427 if (constructor)
428 {
429 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
430 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
431 fprintf( outfile, " \"\\tnop\\n\"\n" );
432 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
433 }
434 if (destructor)
435 {
436 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
437 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
438 fprintf( outfile, " \"\\tnop\\n\"\n" );
439 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
440 }
441 #elif defined(__powerpc__)
442 # ifdef __APPLE__
443 /* Mach-O doesn't have an init section */
444 if (constructor)
445 {
446 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
447 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
448 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", constructor );
449 fprintf( outfile, " \"\\t.text\\n\");\n" );
450 }
451 if (destructor)
452 {
453 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
454 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
455 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", destructor );
456 fprintf( outfile, " \"\\t.text\\n\");\n" );
457 }
458 # else /* __APPLE__ */
459 if (constructor)
460 {
461 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
462 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
463 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
464 }
465 if (destructor)
466 {
467 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
468 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
469 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
470 }
471 # endif /* __APPLE__ */
472 #else
473 #error You need to define the DLL constructor for your architecture
474 #endif
475 fprintf( outfile, "#ifndef __GNUC__\n" );
476 fprintf( outfile, "}\n" );
477 fprintf( outfile, "#endif\n" );
478 }
479
480
481 /*******************************************************************
482 * BuildSpec32File
483 *
484 * Build a Win32 C file from a spec file.
485 */
486 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
487 {
488 int exports_size = 0;
489 int nr_exports, nr_imports;
490 DWORD page_size;
491 const char *init_func = spec->init_func;
492
493 #ifdef HAVE_GETPAGESIZE
494 page_size = getpagesize();
495 #elif defined(__svr4__)
496 page_size = sysconf(_SC_PAGESIZE);
497 #elif defined(_WINDOWS)
498 {
499 SYSTEM_INFO si;
500 GetSystemInfo(&si);
501 page_size = si.dwPageSize;
502 }
503 #else
504 # error Cannot get the page size on this platform
505 #endif
506
507 nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
508 resolve_imports( spec );
509 output_standard_file_header( outfile );
510
511 /* Reserve some space for the PE header */
512
513 fprintf( outfile, "extern char __wine_spec_pe_header[];\n" );
514 fprintf( outfile, "#ifndef __GNUC__\n" );
515 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
516 fprintf( outfile, "#endif\n" );
517 fprintf( outfile, "asm(\".text\\n\\t\"\n" );
518 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
519 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP " 65536\\n\\t\"\n" );
520 fprintf( outfile, " \".data\\n\\t\"\n" );
521 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(4) );
522 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" );
523 fprintf( outfile, "#ifndef __GNUC__\n" );
524 fprintf( outfile, "}\n" );
525 fprintf( outfile, "#endif\n" );
526
527 #ifdef __APPLE__
528 fprintf( outfile, "static char _end[4];\n" );
529 #else
530 fprintf( outfile, "extern char _end[];\n" );
531 #endif
532
533 fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
534
535 #ifdef __i386__
536 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
537 #else
538 fprintf( outfile, "#define __stdcall\n\n" );
539 #endif
540
541 if (nr_exports)
542 {
543 /* Output the stub functions */
544
545 output_stub_funcs( outfile, spec );
546
547 fprintf( outfile, "#ifndef __GNUC__\n" );
548 fprintf( outfile, "static void __asm__dummy(void) {\n" );
549 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
550
551 /* Output code for all register functions */
552
553 output_register_funcs( outfile, spec );
554
555 /* Output the exports and relay entry points */
556
557 exports_size = output_exports( outfile, nr_exports, spec );
558
559 fprintf( outfile, "#ifndef __GNUC__\n" );
560 fprintf( outfile, "}\n" );
561 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
562 }
563
564 /* Output the DLL imports */
565
566 nr_imports = output_imports( outfile, spec );
567
568 /* Output the resources */
569
570 output_resources( outfile, spec );
571
572 /* Output the entry point function */
573
574 fprintf( outfile, "static int __wine_spec_init_state;\n" );
575 fprintf( outfile, "extern int __wine_main_argc;\n" );
576 fprintf( outfile, "extern char **__wine_main_argv;\n" );
577 fprintf( outfile, "extern char **__wine_main_environ;\n" );
578 fprintf( outfile, "extern unsigned short **__wine_main_wargv;\n" );
579 #ifdef __APPLE__
580 fprintf( outfile, "extern _dyld_func_lookup(char *, void *);" );
581 fprintf( outfile, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
582 fprintf( outfile, "{\n" );
583 fprintf( outfile, " void (*init)(void);\n" );
584 fprintf( outfile, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
585 fprintf( outfile, " init();\n" );
586 fprintf( outfile, "}\n" );
587 fprintf( outfile, "static void __wine_spec_hidden_fini()\n" );
588 fprintf( outfile, "{\n" );
589 fprintf( outfile, " void (*fini)(void);\n" );
590 fprintf( outfile, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
591 fprintf( outfile, " fini();\n" );
592 fprintf( outfile, "}\n" );
593 fprintf( outfile, "#define _init __wine_spec_hidden_init\n" );
594 fprintf( outfile, "#define _fini __wine_spec_hidden_fini\n" );
595 #else
596 fprintf( outfile, "extern void _init(int, char**, char**);\n" );
597 fprintf( outfile, "extern void _fini();\n" );
598 #endif
599
600 if (spec->characteristics & IMAGE_FILE_DLL)
601 {
602 if (init_func)
603 fprintf( outfile, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func );
604 else
605 {
606 declare_weak_function( outfile, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
607 init_func = "DllMain";
608 }
609 fprintf( outfile,
610 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
611 "{\n"
612 " int ret;\n"
613 " if (reason == %d && __wine_spec_init_state == 1)\n"
614 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
615 " ret = %s ? %s( inst, reason, reserved ) : 1;\n"
616 " if (reason == %d && __wine_spec_init_state == 1) _fini();\n"
617 " return ret;\n"
618 "}\n",
619 DLL_PROCESS_ATTACH, init_func, init_func, DLL_PROCESS_DETACH );
620 init_func = "__wine_dll_main";
621 }
622 else switch(spec->subsystem)
623 {
624 case IMAGE_SUBSYSTEM_NATIVE:
625 if (init_func)
626 fprintf( outfile, "extern int __stdcall %s( void*, void* );\n\n", init_func );
627 else
628 {
629 declare_weak_function( outfile, "int __stdcall", "DriverEntry", "void*, void*");
630 init_func = "DriverEntry";
631 }
632 fprintf( outfile,
633 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
634 "{\n"
635 " int ret;\n"
636 " if (__wine_spec_init_state == 1)\n"
637 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
638 " ret = %s ? %s( obj, path ) : 0;\n"
639 " if (__wine_spec_init_state == 1) _fini();\n"
640 " return ret;\n"
641 "}\n",
642 init_func, init_func );
643 init_func = "__wine_driver_entry";
644 break;
645 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
646 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
647 if (init_func)
648 fprintf( outfile, "extern int %s( int argc, char *argv[] );\n", init_func );
649 else
650 {
651 declare_weak_function( outfile, "int", "main", "int argc, char *argv[]" );
652 declare_weak_function( outfile, "int", "wmain", "int argc, unsigned short *argv[]" );
653 declare_weak_function( outfile, "int __stdcall", "WinMain", "void *,void *,char *,int" );
654 }
655 fprintf( outfile,
656 "\ntypedef struct {\n"
657 " unsigned int cb;\n"
658 " char *lpReserved, *lpDesktop, *lpTitle;\n"
659 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
660 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
661 " unsigned short wShowWindow, cbReserved2;\n"
662 " char *lpReserved2;\n"
663 " void *hStdInput, *hStdOutput, *hStdError;\n"
664 "} STARTUPINFOA;\n"
665 "extern char * __stdcall GetCommandLineA(void);\n"
666 "extern void * __stdcall GetModuleHandleA(char *);\n"
667 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
668 "extern void __stdcall ExitProcess(unsigned int);\n"
669 "static void __wine_exe_main(void)\n"
670 "{\n"
671 " int ret;\n"
672 " if (__wine_spec_init_state == 1)\n"
673 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
674 if (init_func)
675 fprintf( outfile,
676 " ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func );
677 else
678 fprintf( outfile,
679 " if (WinMain) {\n"
680 " STARTUPINFOA info;\n"
681 " char *cmdline = GetCommandLineA();\n"
682 " int bcount=0, in_quotes=0;\n"
683 " while (*cmdline) {\n"
684 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
685 " else if (*cmdline=='\\\\') bcount++;\n"
686 " else if (*cmdline=='\\\"') {\n"
687 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
688 " bcount=0;\n"
689 " }\n"
690 " else bcount=0;\n"
691 " cmdline++;\n"
692 " }\n"
693 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
694 " GetStartupInfoA( &info );\n"
695 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
696 " ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
697 " }\n"
698 " else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
699 " else ret = main( __wine_main_argc, __wine_main_argv );\n" );
700 fprintf( outfile,
701 " if (__wine_spec_init_state == 1) _fini();\n"
702 " ExitProcess( ret );\n"
703 "}\n\n" );
704 init_func = "__wine_exe_main";
705 break;
706 }
707
708 /* Output the NT header */
709
710 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
711 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
712 fprintf( outfile, " int Signature;\n" );
713 fprintf( outfile, " struct file_header {\n" );
714 fprintf( outfile, " short Machine;\n" );
715 fprintf( outfile, " short NumberOfSections;\n" );
716 fprintf( outfile, " int TimeDateStamp;\n" );
717 fprintf( outfile, " void *PointerToSymbolTable;\n" );
718 fprintf( outfile, " int NumberOfSymbols;\n" );
719 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
720 fprintf( outfile, " short Characteristics;\n" );
721 fprintf( outfile, " } FileHeader;\n" );
722 fprintf( outfile, " struct opt_header {\n" );
723 fprintf( outfile, " short Magic;\n" );
724 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
725 fprintf( outfile, " int SizeOfCode;\n" );
726 fprintf( outfile, " int SizeOfInitializedData;\n" );
727 fprintf( outfile, " int SizeOfUninitializedData;\n" );
728 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
729 fprintf( outfile, " void *BaseOfCode;\n" );
730 fprintf( outfile, " void *BaseOfData;\n" );
731 fprintf( outfile, " void *ImageBase;\n" );
732 fprintf( outfile, " int SectionAlignment;\n" );
733 fprintf( outfile, " int FileAlignment;\n" );
734 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
735 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
736 fprintf( outfile, " short MajorImageVersion;\n" );
737 fprintf( outfile, " short MinorImageVersion;\n" );
738 fprintf( outfile, " short MajorSubsystemVersion;\n" );
739 fprintf( outfile, " short MinorSubsystemVersion;\n" );
740 fprintf( outfile, " int Win32VersionValue;\n" );
741 fprintf( outfile, " void *SizeOfImage;\n" );
742 fprintf( outfile, " int SizeOfHeaders;\n" );
743 fprintf( outfile, " int CheckSum;\n" );
744 fprintf( outfile, " short Subsystem;\n" );
745 fprintf( outfile, " short DllCharacteristics;\n" );
746 fprintf( outfile, " int SizeOfStackReserve;\n" );
747 fprintf( outfile, " int SizeOfStackCommit;\n" );
748 fprintf( outfile, " int SizeOfHeapReserve;\n" );
749 fprintf( outfile, " int SizeOfHeapCommit;\n" );
750 fprintf( outfile, " int LoaderFlags;\n" );
751 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
752 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
753 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
754 fprintf( outfile, " } OptionalHeader;\n" );
755 fprintf( outfile, "} nt_header = {\n" );
756 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
757 #ifdef __i386__
758 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
759 #elif defined(__powerpc__)
760 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC ); /* Machine */
761 #else
762 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN ); /* Machine */
763 #endif
764 fprintf( outfile, " 0, 0, 0, 0,\n" );
765 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
766 fprintf( outfile, " 0x%04x },\n", spec->characteristics ); /* Characteristics */
767
768 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
769 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
770 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
771 fprintf( outfile, " %s,\n", init_func ); /* AddressOfEntryPoint */
772 fprintf( outfile, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
773 fprintf( outfile, " __wine_spec_pe_header,\n" ); /* ImageBase */
774 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
775 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
776 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
777 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
778 fprintf( outfile, " %d,\n", spec->subsystem_major ); /* MajorSubsystemVersion */
779 fprintf( outfile, " %d,\n", spec->subsystem_minor ); /* MinorSubsystemVersion */
780 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
781 fprintf( outfile, " _end,\n" ); /* SizeOfImage */
782 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
783 fprintf( outfile, " 0,\n" ); /* CheckSum */
784 fprintf( outfile, " 0x%04x,\n", spec->subsystem );/* Subsystem */
785 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
786 fprintf( outfile, " %d, %ld,\n", /* SizeOfStackReserve/Commit */
787 (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
788 fprintf( outfile, " %d, %ld,\n", /* SizeOfHeapReserve/Commit */
789 (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
790 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
791 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
792 fprintf( outfile, " {\n" );
793 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
794 exports_size ? "__wine_spec_exports" : "0", exports_size );
795 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
796 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
797 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
798 spec->nb_resources ? "&resources" : "0",
799 spec->nb_resources ? "sizeof(resources)" : "0" );
800 fprintf( outfile, " }\n }\n};\n\n" );
801
802 /* Output the DLL constructor */
803
804 fprintf( outfile,
805 "void __wine_spec_init(void)\n"
806 "{\n"
807 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
808 " __wine_spec_init_state = 1;\n"
809 " __wine_dll_register( &nt_header, \"%s\" );\n"
810 "}\n\n",
811 spec->file_name );
812
813 output_dll_init( outfile, "__wine_spec_init_ctor", NULL );
814 fprintf( outfile,
815 "void __wine_spec_init_ctor(void)\n"
816 "{\n"
817 " if (__wine_spec_init_state) return;\n"
818 " __wine_spec_init();\n"
819 " __wine_spec_init_state = 2;\n"
820 "}\n" );
821 }
822
823
824 /*******************************************************************
825 * BuildDef32File
826 *
827 * Build a Win32 def file from a spec file.
828 */
829 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
830 {
831 const char *name;
832 int i;
833
834 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
835 input_file_name );
836
837 fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
838
839 fprintf(outfile, "EXPORTS\n");
840
841 /* Output the exports and relay entry points */
842
843 for(i = 0; i < spec->nb_entry_points; i++)
844 {
845 const ORDDEF *odp = &spec->entry_points[i];
846 int is_data = 0;
847
848 if (!odp) continue;
849 if (odp->name) name = odp->name;
850 else if (odp->type == TYPE_STUB) name = make_internal_name( odp, spec, "stub" );
851 else if (odp->export_name) name = odp->export_name;
852 else name = make_internal_name( odp, spec, "noname_export" );
853
854 fprintf(outfile, " %s", name);
855
856 switch(odp->type)
857 {
858 case TYPE_EXTERN:
859 is_data = 1;
860 /* fall through */
861 case TYPE_VARARGS:
862 case TYPE_CDECL:
863 /* try to reduce output */
864 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
865 fprintf(outfile, "=%s", odp->link_name);
866 break;
867 case TYPE_STDCALL:
868 {
869 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
870 if (!kill_at) fprintf(outfile, "@%d", at_param);
871 if (odp->flags & FLAG_FORWARD)
872 {
873 fprintf(outfile, "=%s", odp->link_name);
874 }
875 else if (strcmp(name, odp->link_name)) /* try to reduce output */
876 {
877 fprintf(outfile, "=%s", odp->link_name);
878 if (!kill_at) fprintf(outfile, "@%d", at_param);
879 }
880 break;
881 }
882 case TYPE_STUB:
883 {
884 if (!kill_at)
885 {
886 const char *check = name + strlen(name);
887 while (name != check &&
888 '0' <= check[-1] && check[-1] <= '9')
889 {
890 check--;
891 }
892 if (name != check && check != name + strlen(name) &&
893 '@' == check[-1])
894 {
895 fprintf(outfile, "%s", check - 1);
896 }
897 }
898 if (NULL != odp->name)
899 {
900 fprintf(outfile, "=%s", make_internal_name( odp, spec, "stub" ));
901 }
902 break;
903 }
904 default:
905 assert(0);
906 }
907 fprintf( outfile, " @%d", odp->ordinal );
908 #if 0 /* MinGW binutils cannot handle this correctly */
909 if (!odp->name) fprintf( outfile, " NONAME" );
910 #else
911 if (!odp->name && (odp->type == TYPE_STUB || odp->export_name)) fprintf( outfile, " NONAME" );
912 #endif
913 if (is_data) fprintf( outfile, " DATA" );
914 #if 0
915 /* MinGW binutils cannot handle this correctly */
916 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
917 #endif
918 fprintf( outfile, "\n" );
919 }
920 }
921
922
923 /*******************************************************************
924 * BuildDebugFile
925 *
926 * Build the debugging channels source file.
927 */
928 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
929 {
930 int nr_debug;
931 char *prefix, *p;
932
933 while (*argv)
934 {
935 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
936 }
937
938 output_standard_file_header( outfile );
939 nr_debug = output_debug( outfile );
940 if (!nr_debug)
941 {
942 fprintf( outfile, "/* no debug channels found for this module */\n" );
943 return;
944 }
945
946 if (output_file_name)
947 {
948 if ((p = strrchr( output_file_name, '/' ))) p++;
949 prefix = xstrdup( p ? p : output_file_name );
950 if ((p = strchr( prefix, '.' ))) *p = 0;
951 strcpy( p, make_c_identifier(p) );
952 }
953 else prefix = xstrdup( "_" );
954
955 /* Output the DLL constructor */
956
957 fprintf( outfile,
958 "#ifdef __GNUC__\n"
959 "void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
960 "void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
961 "#else\n"
962 "static void __asm__dummy_dll_init(void) {\n",
963 prefix, prefix );
964
965 #if defined(__i386__)
966 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
967 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
968 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
969 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
970 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
971 #elif defined(__sparc__)
972 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
973 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
974 fprintf( outfile, " \"\\tnop\\n\"\n" );
975 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
976 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
977 fprintf( outfile, " \"\\tnop\\n\"\n" );
978 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
979 #elif defined(__powerpc__)
980 # ifdef __APPLE__
981 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
982 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
983 fprintf( outfile, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
984 fprintf( outfile, " \"\\t.text\\n\");\n" );
985 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
986 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
987 fprintf( outfile, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
988 fprintf( outfile, " \"\\t.text\\n\");\n" );
989 # else
990 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
991 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
992 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
993 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
994 fprintf( outfile, " \"\\t.text\\n\");\n" );
995 # endif
996 #else
997 #error You need to define the DLL constructor for your architecture
998 #endif
999 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
1000
1001 fprintf( outfile,
1002 "void __wine_dbg_%s_init(void)\n"
1003 "{\n"
1004 " extern void *__wine_dbg_register( char * const *, int );\n"
1005 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
1006 "}\n\n", prefix, nr_debug );
1007 fprintf( outfile,
1008 "void __wine_dbg_%s_fini(void)\n"
1009 "{\n"
1010 " extern void __wine_dbg_unregister( void* );\n"
1011 " __wine_dbg_unregister( debug_registration );\n"
1012 "}\n", prefix );
1013
1014 free( prefix );
1015 }
1016
1017
1018 /*******************************************************************
1019 * BuildPedllFile
1020 *
1021 * Build a PE DLL C file from a spec file.
1022 */
1023 void BuildPedllFile( FILE *outfile, DLLSPEC *spec )
1024 {
1025 int nr_exports;
1026
1027 nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
1028 output_standard_file_header( outfile );
1029
1030 if (nr_exports)
1031 {
1032 /* Output the stub functions */
1033
1034 output_stub_funcs( outfile, spec );
1035 }
1036 }