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"
33 #include "wine/exception.h"
37 static int string_compare( const void *ptr1
, const void *ptr2
)
39 const char * const *str1
= ptr1
;
40 const char * const *str2
= ptr2
;
41 return strcmp( *str1
, *str2
);
45 /*******************************************************************
48 * Generate an internal name for an entry point. Used for stubs etc.
50 static const char *make_internal_name( const ORDDEF
*odp
, const char *prefix
)
52 static char buffer
[256];
53 if (odp
->name
|| odp
->export_name
)
56 sprintf( buffer
, "__wine_%s_%s_%s", prefix
, DLLFileName
,
57 odp
->name
? odp
->name
: odp
->export_name
);
58 /* make sure name is a legal C identifier */
59 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
60 if (!*p
) return buffer
;
62 sprintf( buffer
, "__wine_%s_%s_%d", prefix
, make_c_identifier(DLLFileName
), odp
->ordinal
);
66 /*******************************************************************
69 * Assign ordinals to all entry points.
71 static void AssignOrdinals(void)
75 if ( !nb_names
) return;
77 /* start assigning from Base, or from 1 if no ordinal defined yet */
78 if (Base
== MAX_ORDINALS
) Base
= 1;
79 for (i
= 0, ordinal
= Base
; i
< nb_names
; i
++)
81 if (Names
[i
]->ordinal
!= -1) continue; /* already has an ordinal */
82 while (Ordinals
[ordinal
]) ordinal
++;
83 if (ordinal
>= MAX_ORDINALS
)
85 current_line
= Names
[i
]->lineno
;
86 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS
);
88 Names
[i
]->ordinal
= ordinal
;
89 Ordinals
[ordinal
] = Names
[i
];
91 if (ordinal
> Limit
) Limit
= ordinal
;
95 /*******************************************************************
98 * Output the debug channels.
100 static int output_debug( FILE *outfile
)
104 if (!nb_debug_channels
) return 0;
105 qsort( debug_channels
, nb_debug_channels
, sizeof(debug_channels
[0]), string_compare
);
107 for (i
= 0; i
< nb_debug_channels
; i
++)
108 fprintf( outfile
, "char __wine_dbch_%s[] = \"\\003%s\";\n",
109 debug_channels
[i
], debug_channels
[i
] );
111 fprintf( outfile
, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels
);
112 for (i
= 0; i
< nb_debug_channels
; i
++)
114 fprintf( outfile
, " __wine_dbch_%s", debug_channels
[i
] );
115 if (i
< nb_debug_channels
- 1) fprintf( outfile
, ",\n" );
117 fprintf( outfile
, "\n};\n\n" );
118 fprintf( outfile
, "static void *debug_registration;\n\n" );
120 return nb_debug_channels
;
124 /*******************************************************************
127 * Output the export table for a Win32 module.
129 static int output_exports( FILE *outfile
, int nr_exports
)
131 int i
, fwd_size
= 0, total_size
= 0;
134 if (!nr_exports
) return 0;
136 fprintf( outfile
, "asm(\".data\\n\"\n" );
137 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
138 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
140 /* export directory header */
142 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
143 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
144 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
145 fprintf( outfile
, " \"\\t.long " __ASM_NAME("dllname") "\\n\"\n" ); /* Name */
146 fprintf( outfile
, " \"\\t.long %d\\n\"\n", Base
); /* Base */
147 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nr_exports
); /* NumberOfFunctions */
148 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nb_names
); /* NumberOfNames */
149 fprintf( outfile
, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
152 fprintf( outfile
, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
153 fprintf( outfile
, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
157 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
158 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
160 total_size
+= 10 * sizeof(int);
162 /* output the function pointers */
164 fprintf( outfile
, " \"__wine_spec_exports_funcs:\\n\"\n" );
165 for (i
= Base
; i
<= Limit
; i
++)
167 ORDDEF
*odp
= Ordinals
[i
];
168 if (!odp
) fprintf( outfile
, " \"\\t.long 0\\n\"\n" );
169 else switch(odp
->type
)
172 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", odp
->link_name
);
177 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
178 (odp
->flags
& FLAG_REGISTER
) ? make_internal_name(odp
,"regs") : odp
->link_name
);
181 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp
, "stub" ) );
184 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp
, "var" ) );
187 fprintf( outfile
, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size
);
188 fwd_size
+= strlen(odp
->link_name
) + 1;
194 total_size
+= (Limit
- Base
+ 1) * sizeof(int);
198 /* output the function name pointers */
202 fprintf( outfile
, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
203 for (i
= 0; i
< nb_names
; i
++)
205 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos
);
206 namepos
+= strlen(Names
[i
]->name
) + 1;
208 total_size
+= nb_names
* sizeof(int);
210 /* output the function names */
212 fprintf( outfile
, " \"\\t.text\\n\"\n" );
213 fprintf( outfile
, " \"__wine_spec_exp_names:\\n\"\n" );
214 for (i
= 0; i
< nb_names
; i
++)
215 fprintf( outfile
, " \"\\t" STRING
" \\\"%s\\\"\\n\"\n", Names
[i
]->name
);
216 fprintf( outfile
, " \"\\t.data\\n\"\n" );
218 /* output the function ordinals */
220 fprintf( outfile
, " \"__wine_spec_exp_ordinals:\\n\"\n" );
221 for (i
= 0; i
< nb_names
; i
++)
223 fprintf( outfile
, " \"\\t.short %d\\n\"\n", Names
[i
]->ordinal
- Base
);
225 total_size
+= nb_names
* sizeof(short);
228 fprintf( outfile
, " \"\\t.short 0\\n\"\n" );
229 total_size
+= sizeof(short);
233 /* output forward strings */
237 fprintf( outfile
, " \"__wine_spec_forwards:\\n\"\n" );
238 for (i
= Base
; i
<= Limit
; i
++)
240 ORDDEF
*odp
= Ordinals
[i
];
241 if (odp
&& odp
->type
== TYPE_FORWARD
)
242 fprintf( outfile
, " \"\\t" STRING
" \\\"%s\\\"\\n\"\n", odp
->link_name
);
244 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
245 total_size
+= (fwd_size
+ 3) & ~3;
252 for (i
= Base
; i
<= Limit
; i
++)
254 ORDDEF
*odp
= Ordinals
[i
];
255 unsigned int j
, args
, mask
= 0;
258 /* skip non-existent entry points */
259 if (!odp
) goto ignore
;
260 /* skip non-functions */
261 if ((odp
->type
!= TYPE_STDCALL
) && (odp
->type
!= TYPE_CDECL
)) goto ignore
;
262 /* skip norelay entry points */
263 if (odp
->flags
& FLAG_NORELAY
) goto ignore
;
265 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
267 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
268 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
270 if ((odp
->flags
& FLAG_RET64
) && (j
< 16)) mask
|= 0x80000000;
272 name
= odp
->link_name
;
273 args
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
274 if (odp
->flags
& FLAG_REGISTER
)
276 name
= make_internal_name( odp
, "regs" );
283 fprintf( outfile
, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name
);
284 fprintf( outfile
, " \"\\tret $0x%04x\\n\"\n", args
);
285 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name
, mask
);
288 fprintf( outfile
, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name
);
289 fprintf( outfile
, " \"\\tret\\n\"\n" );
290 fprintf( outfile
, " \"\\t.short 0x%04x\\n\"\n", args
);
291 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name
, mask
);
299 fprintf( outfile
, " \"\\t.long 0,0,0,0\\n\"\n" );
304 /* output __wine_dllexport symbols */
306 for (i
= 0; i
< nb_names
; i
++)
308 if (Names
[i
]->flags
& FLAG_NOIMPORT
) continue;
309 /* check for invalid characters in the name */
310 for (p
= Names
[i
]->name
; *p
; p
++)
311 if (!isalnum(*p
) && *p
!= '_' && *p
!= '.') break;
313 fprintf( outfile
, " \"\\t.globl " __ASM_NAME("__wine_dllexport_%s_%d_%s") "\\n\"\n",
314 make_c_identifier(DLLFileName
), i
, Names
[i
]->name
);
315 fprintf( outfile
, " \"" __ASM_NAME("__wine_dllexport_%s_%d_%s") ":\\n\"\n",
316 make_c_identifier(DLLFileName
), i
, Names
[i
]->name
);
319 /* output ordinal exports */
321 for (i
= 0; i
< nb_entry_points
; i
++)
323 ORDDEF
*odp
= EntryPoints
[i
];
325 if (odp
->flags
& FLAG_NOIMPORT
) continue;
326 if (odp
->name
|| !odp
->export_name
) continue;
327 /* check for invalid characters in the name */
328 for (p
= odp
->export_name
; *p
; p
++)
329 if (!isalnum(*p
) && *p
!= '_' && *p
!= '.') break;
331 fprintf( outfile
, " \"\\t.globl " __ASM_NAME("__wine_ordexport_%s_%d_%s") "\\n\"\n",
332 make_c_identifier(DLLFileName
), odp
->ordinal
, odp
->export_name
);
333 fprintf( outfile
, " \"" __ASM_NAME("__wine_ordexport_%s_%d_%s") ":\\n\"\n",
334 make_c_identifier(DLLFileName
), odp
->ordinal
, odp
->export_name
);
336 fprintf( outfile
, " \"\\t.long 0xffffffff\\n\"\n" );
338 /* output variables */
340 for (i
= 0; i
< nb_entry_points
; i
++)
342 ORDDEF
*odp
= EntryPoints
[i
];
343 if (odp
->type
== TYPE_VARIABLE
)
346 fprintf( outfile
, " \"%s:\\n\"\n", make_internal_name( odp
, "var" ) );
347 fprintf( outfile
, " \"\\t.long " );
348 for (j
= 0; j
< odp
->u
.var
.n_values
; j
++)
350 fprintf( outfile
, "0x%08x", odp
->u
.var
.values
[j
] );
351 if (j
< odp
->u
.var
.n_values
-1) fputc( ',', outfile
);
353 fprintf( outfile
, "\\n\"\n" );
357 fprintf( outfile
, " \"\\t.text\\n\"\n" );
358 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
359 fprintf( outfile
, ");\n\n" );
365 /*******************************************************************
368 * Output the functions for stub entry points
370 static void output_stub_funcs( FILE *outfile
)
374 for (i
= 0; i
< nb_entry_points
; i
++)
376 ORDDEF
*odp
= EntryPoints
[i
];
377 if (odp
->type
!= TYPE_STUB
) continue;
378 fprintf( outfile
, "#ifdef __GNUC__\n" );
379 fprintf( outfile
, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
380 fprintf( outfile
, "#endif\n\n" );
381 fprintf( outfile
, "struct exc_record {\n" );
382 fprintf( outfile
, " unsigned int code, flags;\n" );
383 fprintf( outfile
, " void *rec, *addr;\n" );
384 fprintf( outfile
, " unsigned int params;\n" );
385 fprintf( outfile
, " const void *info[15];\n" );
386 fprintf( outfile
, "};\n\n" );
387 fprintf( outfile
, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
388 fprintf( outfile
, "static void __wine_unimplemented( const char *func )\n{\n" );
389 fprintf( outfile
, " struct exc_record rec;\n" );
390 fprintf( outfile
, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB
);
391 fprintf( outfile
, " rec.flags = %d;\n", EH_NONCONTINUABLE
);
392 fprintf( outfile
, " rec.rec = 0;\n" );
393 fprintf( outfile
, " rec.params = 2;\n" );
394 fprintf( outfile
, " rec.info[0] = dllname;\n" );
395 fprintf( outfile
, " rec.info[1] = func;\n" );
396 fprintf( outfile
, "#ifdef __GNUC__\n" );
397 fprintf( outfile
, " rec.addr = __builtin_return_address(1);\n" );
398 fprintf( outfile
, "#else\n" );
399 fprintf( outfile
, " rec.addr = 0;\n" );
400 fprintf( outfile
, "#endif\n" );
401 fprintf( outfile
, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
405 for (i
= 0; i
< nb_entry_points
; i
++)
407 ORDDEF
*odp
= EntryPoints
[i
];
408 if (odp
->type
!= TYPE_STUB
) continue;
409 fprintf( outfile
, "void %s(void) ", make_internal_name( odp
, "stub" ) );
411 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->name
);
412 else if (odp
->export_name
)
413 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->export_name
);
415 fprintf( outfile
, "{ __wine_unimplemented(\"%d\"); }\n", odp
->ordinal
);
420 /*******************************************************************
421 * output_register_funcs
423 * Output the functions for register entry points
425 static void output_register_funcs( FILE *outfile
)
430 for (i
= 0; i
< nb_entry_points
; i
++)
432 ORDDEF
*odp
= EntryPoints
[i
];
433 if (odp
->type
!= TYPE_STDCALL
&& odp
->type
!= TYPE_CDECL
) continue;
434 if (!(odp
->flags
& FLAG_REGISTER
)) continue;
435 name
= make_internal_name( odp
, "regs" );
437 "asm(\".align %d\\n\\t\"\n"
438 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
439 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
440 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
441 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
442 " \".byte %d,%d\");\n",
444 name
, name
, odp
->link_name
,
445 strlen(odp
->u
.func
.arg_types
) * sizeof(int),
446 (odp
->type
== TYPE_CDECL
) ? 0 : (strlen(odp
->u
.func
.arg_types
) * sizeof(int)) );
451 /*******************************************************************
454 * Output code for calling a dll constructor and destructor.
456 void output_dll_init( FILE *outfile
, const char *constructor
, const char *destructor
)
458 fprintf( outfile
, "#ifndef __GNUC__\n" );
459 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
460 fprintf( outfile
, "#endif\n" );
462 #if defined(__i386__)
465 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
466 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor
);
467 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
471 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
472 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor
);
473 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
475 #elif defined(__sparc__)
478 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
479 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor
);
480 fprintf( outfile
, " \"\\tnop\\n\"\n" );
481 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
485 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
486 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor
);
487 fprintf( outfile
, " \"\\tnop\\n\"\n" );
488 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
490 #elif defined(__PPC__)
493 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
494 fprintf( outfile
, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor
);
495 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
499 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
500 fprintf( outfile
, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor
);
501 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
504 #error You need to define the DLL constructor for your architecture
506 fprintf( outfile
, "#ifndef __GNUC__\n" );
507 fprintf( outfile
, "}\n" );
508 fprintf( outfile
, "#endif\n" );
512 /*******************************************************************
515 * Build a Win32 C file from a spec file.
517 void BuildSpec32File( FILE *outfile
)
519 int exports_size
= 0;
520 int nr_exports
, nr_imports
, nr_resources
;
521 int characteristics
, subsystem
;
523 char constructor
[100];
525 #ifdef HAVE_GETPAGESIZE
526 page_size
= getpagesize();
527 #elif defined(__svr4__)
528 page_size
= sysconf(_SC_PAGESIZE
);
529 #elif defined(_WINDOWS)
533 page_size
= si
.dwPageSize
;
536 # error Cannot get the page size on this platform
540 nr_exports
= Base
<= Limit
? Limit
- Base
+ 1 : 0;
543 output_standard_file_header( outfile
);
545 /* Reserve some space for the PE header */
547 fprintf( outfile
, "extern char pe_header[];\n" );
548 fprintf( outfile
, "#ifndef __GNUC__\n" );
549 fprintf( outfile
, "static void __asm__dummy_header(void) {\n" );
550 fprintf( outfile
, "#endif\n" );
551 fprintf( outfile
, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
552 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(page_size
) );
553 fprintf( outfile
, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
554 fprintf( outfile
, "#ifndef __GNUC__\n" );
555 fprintf( outfile
, "}\n" );
556 fprintf( outfile
, "#endif\n" );
558 fprintf( outfile
, "static const char dllname[] = \"%s\";\n\n", DLLFileName
);
559 fprintf( outfile
, "extern int __wine_spec_exports[];\n\n" );
562 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
564 fprintf( outfile
, "#define __stdcall\n\n" );
569 /* Output the stub functions */
571 output_stub_funcs( outfile
);
573 fprintf( outfile
, "#ifndef __GNUC__\n" );
574 fprintf( outfile
, "static void __asm__dummy(void) {\n" );
575 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
577 /* Output code for all register functions */
579 output_register_funcs( outfile
);
581 /* Output the exports and relay entry points */
583 exports_size
= output_exports( outfile
, nr_exports
);
585 fprintf( outfile
, "#ifndef __GNUC__\n" );
586 fprintf( outfile
, "}\n" );
587 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
590 /* Output the DLL imports */
592 nr_imports
= output_imports( outfile
);
594 /* Output the resources */
596 nr_resources
= output_resources( outfile
);
598 /* Output LibMain function */
600 characteristics
= subsystem
= 0;
604 if (init_func
) fprintf( outfile
, "extern void %s();\n", init_func
);
605 characteristics
= IMAGE_FILE_DLL
;
607 case SPEC_MODE_GUIEXE
:
608 if (!init_func
) init_func
= "WinMain";
610 "\ntypedef struct {\n"
611 " unsigned int cb;\n"
612 " char *lpReserved, *lpDesktop, *lpTitle;\n"
613 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
614 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
615 " unsigned short wShowWindow, cbReserved2;\n"
616 " char *lpReserved2;\n"
617 " void *hStdInput, *hStdOutput, *hStdError;\n"
621 "extern int __stdcall %s(void *,void *,char *,int);\n"
622 "extern char * __stdcall GetCommandLineA(void);\n"
623 "extern void * __stdcall GetModuleHandleA(char *);\n"
624 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
625 "extern void __stdcall ExitProcess(unsigned int);\n"
626 "static void __wine_exe_main(void)\n"
628 " extern int __wine_main_argc;\n"
629 " extern char **__wine_main_argv;\n"
630 " STARTUPINFOA info;\n"
631 " char *cmdline = GetCommandLineA();\n"
632 " int bcount=0, in_quotes=0;\n"
633 " while (*cmdline) {\n"
634 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
635 " else if (*cmdline=='\\\\') bcount++;\n"
636 " else if (*cmdline=='\\\"') {\n"
637 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
643 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
644 " GetStartupInfoA( &info );\n"
645 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
646 " _ARGC = __wine_main_argc;\n"
647 " _ARGV = __wine_main_argv;\n"
648 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
649 "}\n\n", init_func
, init_func
);
650 init_func
= "__wine_exe_main";
651 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
653 case SPEC_MODE_GUIEXE_UNICODE
:
654 if (!init_func
) init_func
= "WinMain";
656 "\ntypedef unsigned short WCHAR;\n"
658 " unsigned int cb;\n"
659 " char *lpReserved, *lpDesktop, *lpTitle;\n"
660 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
661 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
662 " unsigned short wShowWindow, cbReserved2;\n"
663 " char *lpReserved2;\n"
664 " void *hStdInput, *hStdOutput, *hStdError;\n"
668 "extern int __stdcall %s(void *,void *,char *,int);\n"
669 "extern char * __stdcall GetCommandLineA(void);\n"
670 "extern void * __stdcall GetModuleHandleA(char *);\n"
671 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
672 "extern void __stdcall ExitProcess(unsigned int);\n"
673 "static void __wine_exe_main(void)\n"
675 " extern int __wine_main_argc;\n"
676 " extern WCHAR **__wine_main_wargv;\n"
677 " STARTUPINFOA info;\n"
678 " char *cmdline = GetCommandLineA();\n"
679 " int bcount=0, in_quotes=0;\n"
680 " while (*cmdline) {\n"
681 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
682 " else if (*cmdline=='\\\\') bcount++;\n"
683 " else if (*cmdline=='\\\"') {\n"
684 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
690 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
691 " GetStartupInfoA( &info );\n"
692 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
693 " _ARGC = __wine_main_argc;\n"
694 " _ARGV = __wine_main_wargv;\n"
695 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
696 "}\n\n", init_func
, init_func
);
697 init_func
= "__wine_exe_main";
698 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_GUI
;
700 case SPEC_MODE_CUIEXE
:
701 if (!init_func
) init_func
= "main";
705 "extern void __stdcall ExitProcess(int);\n"
706 "static void __wine_exe_main(void)\n"
708 " extern int %s( int argc, char *argv[] );\n"
709 " extern int __wine_main_argc;\n"
710 " extern char **__wine_main_argv;\n"
711 " _ARGC = __wine_main_argc;\n"
712 " _ARGV = __wine_main_argv;\n"
713 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
714 "}\n\n", init_func
, init_func
);
715 init_func
= "__wine_exe_main";
716 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
718 case SPEC_MODE_CUIEXE_UNICODE
:
719 if (!init_func
) init_func
= "wmain";
721 "\ntypedef unsigned short WCHAR;\n"
724 "extern void __stdcall ExitProcess(int);\n"
725 "static void __wine_exe_main(void)\n"
727 " extern int %s( int argc, WCHAR *argv[] );\n"
728 " extern int __wine_main_argc;\n"
729 " extern WCHAR **__wine_main_wargv;\n"
730 " _ARGC = __wine_main_argc;\n"
731 " _ARGV = __wine_main_wargv;\n"
732 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
733 "}\n\n", init_func
, init_func
);
734 init_func
= "__wine_exe_main";
735 subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
739 /* Output the NT header */
741 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
742 fprintf( outfile
, "static const struct image_nt_headers\n{\n" );
743 fprintf( outfile
, " int Signature;\n" );
744 fprintf( outfile
, " struct file_header {\n" );
745 fprintf( outfile
, " short Machine;\n" );
746 fprintf( outfile
, " short NumberOfSections;\n" );
747 fprintf( outfile
, " int TimeDateStamp;\n" );
748 fprintf( outfile
, " void *PointerToSymbolTable;\n" );
749 fprintf( outfile
, " int NumberOfSymbols;\n" );
750 fprintf( outfile
, " short SizeOfOptionalHeader;\n" );
751 fprintf( outfile
, " short Characteristics;\n" );
752 fprintf( outfile
, " } FileHeader;\n" );
753 fprintf( outfile
, " struct opt_header {\n" );
754 fprintf( outfile
, " short Magic;\n" );
755 fprintf( outfile
, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
756 fprintf( outfile
, " int SizeOfCode;\n" );
757 fprintf( outfile
, " int SizeOfInitializedData;\n" );
758 fprintf( outfile
, " int SizeOfUninitializedData;\n" );
759 fprintf( outfile
, " void *AddressOfEntryPoint;\n" );
760 fprintf( outfile
, " void *BaseOfCode;\n" );
761 fprintf( outfile
, " void *BaseOfData;\n" );
762 fprintf( outfile
, " void *ImageBase;\n" );
763 fprintf( outfile
, " int SectionAlignment;\n" );
764 fprintf( outfile
, " int FileAlignment;\n" );
765 fprintf( outfile
, " short MajorOperatingSystemVersion;\n" );
766 fprintf( outfile
, " short MinorOperatingSystemVersion;\n" );
767 fprintf( outfile
, " short MajorImageVersion;\n" );
768 fprintf( outfile
, " short MinorImageVersion;\n" );
769 fprintf( outfile
, " short MajorSubsystemVersion;\n" );
770 fprintf( outfile
, " short MinorSubsystemVersion;\n" );
771 fprintf( outfile
, " int Win32VersionValue;\n" );
772 fprintf( outfile
, " int SizeOfImage;\n" );
773 fprintf( outfile
, " int SizeOfHeaders;\n" );
774 fprintf( outfile
, " int CheckSum;\n" );
775 fprintf( outfile
, " short Subsystem;\n" );
776 fprintf( outfile
, " short DllCharacteristics;\n" );
777 fprintf( outfile
, " int SizeOfStackReserve;\n" );
778 fprintf( outfile
, " int SizeOfStackCommit;\n" );
779 fprintf( outfile
, " int SizeOfHeapReserve;\n" );
780 fprintf( outfile
, " int SizeOfHeapCommit;\n" );
781 fprintf( outfile
, " int LoaderFlags;\n" );
782 fprintf( outfile
, " int NumberOfRvaAndSizes;\n" );
783 fprintf( outfile
, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
784 IMAGE_NUMBEROF_DIRECTORY_ENTRIES
);
785 fprintf( outfile
, " } OptionalHeader;\n" );
786 fprintf( outfile
, "} nt_header = {\n" );
787 fprintf( outfile
, " 0x%04x,\n", IMAGE_NT_SIGNATURE
); /* Signature */
789 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386
); /* Machine */
790 fprintf( outfile
, " 0, 0, 0, 0,\n" );
791 fprintf( outfile
, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
792 fprintf( outfile
, " 0x%04x },\n", characteristics
); /* Characteristics */
794 fprintf( outfile
, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC
); /* Magic */
795 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
796 fprintf( outfile
, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
797 fprintf( outfile
, " %s,\n", init_func
? init_func
: "0" ); /* AddressOfEntryPoint */
798 fprintf( outfile
, " 0, 0,\n" ); /* BaseOfCode/Data */
799 fprintf( outfile
, " pe_header,\n" ); /* ImageBase */
800 fprintf( outfile
, " %ld,\n", page_size
); /* SectionAlignment */
801 fprintf( outfile
, " %ld,\n", page_size
); /* FileAlignment */
802 fprintf( outfile
, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
803 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorImageVersion */
804 fprintf( outfile
, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
805 fprintf( outfile
, " 0,\n" ); /* Win32VersionValue */
806 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfImage */
807 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfHeaders */
808 fprintf( outfile
, " 0,\n" ); /* CheckSum */
809 fprintf( outfile
, " 0x%04x,\n", subsystem
); /* Subsystem */
810 fprintf( outfile
, " 0,\n" ); /* DllCharacteristics */
811 fprintf( outfile
, " %d, 0,\n", stack_size
*1024 ); /* SizeOfStackReserve/Commit */
812 fprintf( outfile
, " %d, 0,\n", DLLHeapSize
*1024 );/* SizeOfHeapReserve/Commit */
813 fprintf( outfile
, " 0,\n" ); /* LoaderFlags */
814 fprintf( outfile
, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES
); /* NumberOfRvaAndSizes */
815 fprintf( outfile
, " {\n" );
816 fprintf( outfile
, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
817 exports_size
? "__wine_spec_exports" : "0", exports_size
);
818 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
819 nr_imports
? "&imports" : "0", nr_imports
? "sizeof(imports)" : "0" );
820 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
821 nr_resources
? "&resources" : "0", nr_resources
? "sizeof(resources)" : "0" );
822 fprintf( outfile
, " }\n }\n};\n\n" );
824 /* Output the DLL constructor */
826 sprintf( constructor
, "__wine_spec_%s_init", make_c_identifier(DLLFileName
) );
827 output_dll_init( outfile
, constructor
, NULL
);
832 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
833 " extern void *__wine_dbg_register( char * const *, int );\n"
834 " __wine_dll_register( &nt_header, \"%s\" );\n"
836 constructor
, DLLFileName
);
840 /*******************************************************************
843 * Build a Win32 def file from a spec file.
845 void BuildDef32File(FILE *outfile
)
852 fprintf(outfile
, "; File generated automatically from %s; do not edit!\n\n",
855 fprintf(outfile
, "LIBRARY %s\n\n", DLLFileName
);
857 fprintf(outfile
, "EXPORTS\n");
859 /* Output the exports and relay entry points */
861 for(i
= 0; i
< nb_entry_points
; i
++)
863 ORDDEF
*odp
= EntryPoints
[i
];
866 if (odp
->flags
& FLAG_NOIMPORT
) continue;
867 if (odp
->type
== TYPE_STUB
) continue;
869 if (odp
->name
) name
= odp
->name
;
870 else if (odp
->export_name
) name
= odp
->export_name
;
873 fprintf(outfile
, " %s", name
);
881 /* try to reduce output */
882 if(strcmp(name
, odp
->link_name
))
883 fprintf(outfile
, "=%s", odp
->link_name
);
887 #ifdef NEED_STDCALL_DECORATION
888 int at_param
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
889 fprintf(outfile
, "@%d", at_param
);
890 #endif /* NEED_STDCALL_DECORATION */
891 /* try to reduce output */
892 if(strcmp(name
, odp
->link_name
))
894 fprintf(outfile
, "=%s", odp
->link_name
);
895 #ifdef NEED_STDCALL_DECORATION
896 fprintf(outfile
, "@%d", at_param
);
897 #endif /* NEED_STDCALL_DECORATION */
902 fprintf(outfile
, "=%s", odp
->link_name
);
907 fprintf(outfile
, " @%d%s\n", odp
->ordinal
, odp
->name
? "" : " NONAME" );
912 /*******************************************************************
915 * Build the debugging channels source file.
917 void BuildDebugFile( FILE *outfile
, const char *srcdir
, char **argv
)
922 while (*argv
) parse_debug_channels( srcdir
, *argv
++ );
924 output_standard_file_header( outfile
);
925 nr_debug
= output_debug( outfile
);
928 fprintf( outfile
, "/* no debug channels found for this module */\n" );
932 if (output_file_name
)
934 if ((p
= strrchr( output_file_name
, '/' ))) p
++;
935 prefix
= xstrdup( p
? p
: output_file_name
);
936 if ((p
= strchr( prefix
, '.' ))) *p
= 0;
937 strcpy( p
, make_c_identifier(p
) );
939 else prefix
= xstrdup( "_" );
941 /* Output the DLL constructor */
945 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
946 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
948 "static void __asm__dummy_dll_init(void) {\n",
951 #if defined(__i386__)
952 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
953 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
954 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
955 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
956 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
957 #elif defined(__sparc__)
958 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
959 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
960 fprintf( outfile
, " \"\\tnop\\n\"\n" );
961 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
962 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
963 fprintf( outfile
, " \"\\tnop\\n\"\n" );
964 fprintf( outfile
, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
965 #elif defined(__PPC__)
966 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
967 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
968 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
969 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
970 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
972 #error You need to define the DLL constructor for your architecture
974 fprintf( outfile
, "}\n#endif /* defined(__GNUC__) */\n" );
977 "\n#ifdef __GNUC__\n"
980 "void __wine_dbg_%s_init(void)\n"
982 " extern void *__wine_dbg_register( char * const *, int );\n"
983 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
984 "}\n", prefix
, nr_debug
);
986 "\n#ifdef __GNUC__\n"
989 "void __wine_dbg_%s_fini(void)\n"
991 " extern void __wine_dbg_unregister( void* );\n"
992 " __wine_dbg_unregister( debug_registration );\n"