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"
31 #include "wine/exception.h"
32 #include "wine/winbase16.h"
37 /*******************************************************************
41 static inline unsigned short get_cs(void)
45 __asm__("movw %%cs,%w0" : "=r"(res
));
46 #elif defined(_MSC_VER)
56 /*******************************************************************
59 * Output a file header with the common declarations we need.
61 static void output_file_header( FILE *outfile
)
63 output_standard_file_header( outfile
);
64 fprintf( outfile
, "extern struct\n{\n" );
65 fprintf( outfile
, " void *base[8192];\n" );
66 fprintf( outfile
, " unsigned long limit[8192];\n" );
67 fprintf( outfile
, " unsigned char flags[8192];\n" );
68 fprintf( outfile
, "} wine_ldt_copy;\n\n" );
70 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
72 fprintf( outfile
, "#define __stdcall\n\n" );
77 /*******************************************************************
80 * Store a list of ints into a byte array.
82 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
89 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
90 buffer
[i
] = odp
->u
.var
.values
[i
];
93 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
94 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
97 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
98 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
101 return odp
->u
.var
.n_values
* size
;
105 /*******************************************************************
108 static int output_entry_table( unsigned char **ret_buff
, DLLSPEC
*spec
)
110 int i
, prev
= 0, prev_sel
= -1;
111 unsigned char *pstr
, *buffer
;
112 unsigned char *bundle
= NULL
;
114 buffer
= xmalloc( spec
->limit
* 5 ); /* we use at most 5 bytes per entry-point */
117 for (i
= 1; i
<= spec
->limit
; i
++)
121 ORDDEF
*odp
= spec
->ordinals
[i
];
130 selector
= 1; /* Code selector */
133 selector
= 2; /* Data selector */
136 selector
= 0xfe; /* Constant selector */
142 if (!bundle
|| prev
+ 1 != i
|| prev_sel
!= selector
|| *bundle
== 255)
144 /* need to start a new bundle */
148 int skip
= i
- (prev
+ 1);
164 /* output the entry */
165 *pstr
++ = 3; /* flags: exported & public data */
166 offset
= odp
->offset
;
167 memcpy( pstr
, &offset
, sizeof(WORD
) );
168 pstr
+= sizeof(WORD
);
169 (*bundle
)++; /* increment bundle entry count */
173 if ((pstr
- buffer
) & 1) *pstr
++ = 0;
174 *ret_buff
= xrealloc( buffer
, pstr
- buffer
);
175 return pstr
- buffer
;
179 /*******************************************************************
182 static void output_bytes( FILE *outfile
, const void *buffer
, unsigned int size
)
185 const unsigned char *ptr
= buffer
;
187 fprintf( outfile
, " {" );
188 for (i
= 0; i
< size
; i
++)
190 if (!(i
& 7)) fprintf( outfile
, "\n " );
191 fprintf( outfile
, " 0x%02x", *ptr
++ );
192 if (i
< size
- 1) fprintf( outfile
, "," );
194 fprintf( outfile
, "\n },\n" );
198 /*******************************************************************
201 * Output the 16-bit NE module structure.
203 static void output_module_data( FILE *outfile
, int max_code_offset
, const void *data_segment
,
204 unsigned int data_size
, DLLSPEC
*spec
)
206 unsigned char *resdir_buffer
, *resdata_buffer
, *et_buffer
;
207 unsigned char string
[256];
209 unsigned int ne_offset
, segtable_offset
, impnames_offset
, data_offset
;
210 unsigned int resnames_offset
, resnames_size
;
211 unsigned int resdir_size
, resdir_offset
;
212 unsigned int resdata_size
, resdata_offset
, resdata_align
;
213 unsigned int et_size
, et_offset
;
217 fprintf( outfile
, "static const struct module_data\n{\n" );
218 fprintf( outfile
, " struct\n {\n" );
219 fprintf( outfile
, " unsigned short e_magic;\n" );
220 fprintf( outfile
, " unsigned short e_cblp;\n" );
221 fprintf( outfile
, " unsigned short e_cp;\n" );
222 fprintf( outfile
, " unsigned short e_crlc;\n" );
223 fprintf( outfile
, " unsigned short e_cparhdr;\n" );
224 fprintf( outfile
, " unsigned short e_minalloc;\n" );
225 fprintf( outfile
, " unsigned short e_maxalloc;\n" );
226 fprintf( outfile
, " unsigned short e_ss;\n" );
227 fprintf( outfile
, " unsigned short e_sp;\n" );
228 fprintf( outfile
, " unsigned short e_csum;\n" );
229 fprintf( outfile
, " unsigned short e_ip;\n" );
230 fprintf( outfile
, " unsigned short e_cs;\n" );
231 fprintf( outfile
, " unsigned short e_lfarlc;\n" );
232 fprintf( outfile
, " unsigned short e_ovno;\n" );
233 fprintf( outfile
, " unsigned short e_res[4];\n" );
234 fprintf( outfile
, " unsigned short e_oemid;\n" );
235 fprintf( outfile
, " unsigned short e_oeminfo;\n" );
236 fprintf( outfile
, " unsigned short e_res2[10];\n" );
237 fprintf( outfile
, " unsigned int e_lfanew;\n" );
238 fprintf( outfile
, " } dos_header;\n" );
243 fprintf( outfile
, " struct\n {\n" );
244 fprintf( outfile
, " unsigned short ne_magic;\n" );
245 fprintf( outfile
, " unsigned char ne_ver;\n" );
246 fprintf( outfile
, " unsigned char ne_rev;\n" );
247 fprintf( outfile
, " unsigned short ne_enttab;\n" );
248 fprintf( outfile
, " unsigned short ne_cbenttab;\n" );
249 fprintf( outfile
, " int ne_crc;\n" );
250 fprintf( outfile
, " unsigned short ne_flags;\n" );
251 fprintf( outfile
, " unsigned short ne_autodata;\n" );
252 fprintf( outfile
, " unsigned short ne_heap;\n" );
253 fprintf( outfile
, " unsigned short ne_stack;\n" );
254 fprintf( outfile
, " unsigned int ne_csip;\n" );
255 fprintf( outfile
, " unsigned int ne_sssp;\n" );
256 fprintf( outfile
, " unsigned short ne_cseg;\n" );
257 fprintf( outfile
, " unsigned short ne_cmod;\n" );
258 fprintf( outfile
, " unsigned short ne_cbnrestab;\n" );
259 fprintf( outfile
, " unsigned short ne_segtab;\n" );
260 fprintf( outfile
, " unsigned short ne_rsrctab;\n" );
261 fprintf( outfile
, " unsigned short ne_restab;\n" );
262 fprintf( outfile
, " unsigned short ne_modtab;\n" );
263 fprintf( outfile
, " unsigned short ne_imptab;\n" );
264 fprintf( outfile
, " unsigned int ne_nrestab;\n" );
265 fprintf( outfile
, " unsigned short ne_cmovent;\n" );
266 fprintf( outfile
, " unsigned short ne_align;\n" );
267 fprintf( outfile
, " unsigned short ne_cres;\n" );
268 fprintf( outfile
, " unsigned char ne_exetyp;\n" );
269 fprintf( outfile
, " unsigned char ne_flagsothers;\n" );
270 fprintf( outfile
, " unsigned short ne_pretthunks;\n" );
271 fprintf( outfile
, " unsigned short ne_psegrefbytes;\n" );
272 fprintf( outfile
, " unsigned short ne_swaparea;\n" );
273 fprintf( outfile
, " unsigned short ne_expver;\n" );
274 fprintf( outfile
, " } os2_header;\n" );
278 segtable_offset
= 64;
279 fprintf( outfile
, " struct\n {\n" );
280 fprintf( outfile
, " unsigned short filepos;\n" );
281 fprintf( outfile
, " unsigned short size;\n" );
282 fprintf( outfile
, " unsigned short flags;\n" );
283 fprintf( outfile
, " unsigned short minsize;\n" );
284 fprintf( outfile
, " } segtable[2];\n" );
286 /* resource directory */
288 resdir_offset
= segtable_offset
+ 2 * 8;
289 resdir_size
= get_res16_directory_size( spec
);
290 fprintf( outfile
, " unsigned char resdir[%d];\n", resdir_size
);
292 /* resident names table */
294 resnames_offset
= resdir_offset
+ resdir_size
;
295 fprintf( outfile
, " struct\n {\n" );
296 fprintf( outfile
, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_0;\n",
297 strlen( spec
->dll_name
) );
298 resnames_size
= 3 + strlen( spec
->dll_name
);
299 for (i
= 1; i
<= spec
->limit
; i
++)
301 ORDDEF
*odp
= spec
->ordinals
[i
];
302 if (!odp
|| !odp
->name
[0]) continue;
303 fprintf( outfile
, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_%d;\n",
304 strlen(odp
->name
), i
);
305 resnames_size
+= 3 + strlen( odp
->name
);
307 fprintf( outfile
, " unsigned char name_last[%d];\n", 2 - (resnames_size
& 1) );
308 resnames_size
= (resnames_size
+ 2) & ~1;
309 fprintf( outfile
, " } resnames;\n" );
311 /* imported names table */
313 impnames_offset
= resnames_offset
+ resnames_size
;
314 fprintf( outfile
, " unsigned char impnames[2];\n" );
318 et_offset
= impnames_offset
+ 2;
319 et_size
= output_entry_table( &et_buffer
, spec
);
320 fprintf( outfile
, " unsigned char entry_table[%d];\n", et_size
);
324 data_offset
= et_offset
+ et_size
;
325 fprintf( outfile
, " unsigned char data_segment[%d];\n", data_size
);
326 if (data_offset
+ data_size
>= 0x10000)
327 fatal_error( "Not supported yet: 16-bit module data larger than 64K\n" );
331 resdata_offset
= ne_offset
+ data_offset
+ data_size
;
332 for (resdata_align
= 0; resdata_align
< 16; resdata_align
++)
334 unsigned int size
= get_res16_data_size( spec
, resdata_offset
, resdata_align
);
335 if ((resdata_offset
+ size
) >> resdata_align
<= 0xffff) break;
337 output_res16_directory( &resdir_buffer
, spec
, resdata_offset
, resdata_align
);
338 resdata_size
= output_res16_data( &resdata_buffer
, spec
, resdata_offset
, resdata_align
);
339 if (resdata_size
) fprintf( outfile
, " unsigned char resources[%d];\n", resdata_size
);
343 fprintf( outfile
, "} module =\n{\n {\n" );
344 fprintf( outfile
, " 0x%04x,\n", IMAGE_DOS_SIGNATURE
); /* e_magic */
345 fprintf( outfile
, " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" );
346 fprintf( outfile
, " { 0, 0, 0, 0, }, 0, 0,\n" );
347 fprintf( outfile
, " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\n" );
348 fprintf( outfile
, " sizeof(module.dos_header)\n" ); /* e_lfanew */
352 fprintf( outfile
, " },\n {\n" );
353 fprintf( outfile
, " 0x%04x,\n", IMAGE_OS2_SIGNATURE
); /* ne_magic */
354 fprintf( outfile
, " 0, 0,\n" );
355 fprintf( outfile
, " %d,\n", et_offset
); /* ne_enttab */
356 fprintf( outfile
, " sizeof(module.entry_table),\n" ); /* ne_cbenttab */
357 fprintf( outfile
, " 0,\n" ); /* ne_crc */
358 fprintf( outfile
, " 0x%04x,\n", /* ne_flags */
359 NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_LIBMODULE
);
360 fprintf( outfile
, " 2,\n" ); /* ne_autodata */
361 fprintf( outfile
, " %d,\n", spec
->heap_size
); /* ne_heap */
362 fprintf( outfile
, " 0, 0, 0,\n" );
363 fprintf( outfile
, " 2,\n" ); /* ne_cseg */
364 fprintf( outfile
, " 0,\n" ); /* ne_cmod */
365 fprintf( outfile
, " 0,\n" ); /* ne_cbnrestab */
366 fprintf( outfile
, " %d,\n", segtable_offset
); /* ne_segtab */
367 fprintf( outfile
, " %d,\n", resdir_offset
); /* ne_rsrctab */
368 fprintf( outfile
, " %d,\n", resnames_offset
); /* ne_restab */
369 fprintf( outfile
, " %d,\n", impnames_offset
); /* ne_modtab */
370 fprintf( outfile
, " %d,\n", impnames_offset
); /* ne_imptab */
371 fprintf( outfile
, " 0,\n" ); /* ne_nrestab */
372 fprintf( outfile
, " 0,\n" ); /* ne_cmovent */
373 fprintf( outfile
, " 0,\n" ); /* ne_align */
374 fprintf( outfile
, " 0,\n" ); /* ne_cres */
375 fprintf( outfile
, " 0x%04x,\n", NE_OSFLAGS_WINDOWS
); /* ne_exetyp */
376 fprintf( outfile
, " 0x%04x,\n", NE_AFLAGS_FASTLOAD
); /* ne_flagsothers */
377 fprintf( outfile
, " 0,\n" ); /* ne_pretthunks */
378 fprintf( outfile
, " sizeof(module),\n" ); /* ne_psegrefbytes */
379 fprintf( outfile
, " 0,\n" ); /* ne_swaparea */
380 fprintf( outfile
, " 0\n" ); /* ne_expver */
381 fprintf( outfile
, " },\n" );
385 fprintf( outfile
, " {\n" );
386 fprintf( outfile
, " { 0, %d, 0x%04x, %d },\n",
387 max_code_offset
, NE_SEGFLAGS_32BIT
, max_code_offset
);
388 fprintf( outfile
, " { %d, %d, 0x%04x, %d },\n",
389 data_offset
, data_size
, NE_SEGFLAGS_DATA
, data_size
);
390 fprintf( outfile
, " },\n" );
392 /* resource directory */
394 output_bytes( outfile
, resdir_buffer
, resdir_size
);
395 free( resdir_buffer
);
397 /* resident names table */
399 fprintf( outfile
, " {\n" );
400 strcpy( string
, spec
->dll_name
);
401 fprintf( outfile
, " { %d, \"%s\", 0 },\n", strlen(string
), strupper(string
) );
402 for (i
= 1; i
<= spec
->limit
; i
++)
404 ORDDEF
*odp
= spec
->ordinals
[i
];
405 if (!odp
|| !odp
->name
[0]) continue;
406 strcpy( string
, odp
->name
);
407 fprintf( outfile
, " { %d, \"%s\", %d },\n", strlen(string
), strupper(string
), i
);
409 fprintf( outfile
, " { 0 }\n },\n" );
411 /* imported names table */
413 fprintf( outfile
, " { 0, 0 },\n" );
417 output_bytes( outfile
, et_buffer
, et_size
);
422 output_bytes( outfile
, data_segment
, data_size
);
428 output_bytes( outfile
, resdata_buffer
, resdata_size
);
429 free( resdata_buffer
);
432 fprintf( outfile
, "};\n" );
437 /*******************************************************************
438 * BuildCallFrom16Func
440 * Build a 16-bit-to-Wine callback glue function.
442 * The generated routines are intended to be used as argument conversion
443 * routines to be called by the CallFrom16... core. Thus, the prototypes of
444 * the generated routines are (see also CallFrom16):
446 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
447 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
448 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
449 * CONTEXT86 *context );
451 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
452 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
453 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
454 * 'T'=segmented pointer to null-terminated string).
456 * The generated routines fetch the arguments from the 16-bit stack (pointed
457 * to by 'args'); the offsets of the single argument values are computed
458 * according to the calling convention and the argument types. Then, the
459 * 32-bit entry point is called with these arguments.
461 * For register functions, the arguments (if present) are converted just
462 * the same as for normal functions, but in addition the CONTEXT86 pointer
463 * filled with the current register values is passed to the 32-bit routine.
465 static void BuildCallFrom16Func( FILE *outfile
, const char *profile
, const char *prefix
)
467 int i
, pos
, argsize
= 0;
472 const char *args
= profile
+ 7;
473 const char *ret_type
;
475 /* Parse function type */
477 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
478 else if (!strncmp( "v_", profile
, 2 )) varargs
= usecdecl
= 1;
479 else if (strncmp( "p_", profile
, 2 ))
481 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
485 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
486 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
487 else if (strncmp( "long_", profile
+ 2, 5 ))
489 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
493 for ( i
= 0; args
[i
]; i
++ )
497 case 's': /* s_word */
500 case 'l': /* long or segmented pointer */
501 case 'T': /* segmented pointer to null-terminated string */
502 case 'p': /* linear pointer */
503 case 't': /* linear pointer to null-terminated string */
508 ret_type
= reg_func
? "void" : short_ret
? "unsigned short" : "unsigned int";
510 fprintf( outfile
, "typedef %s (%s*proc_%s_t)( ",
511 ret_type
, usecdecl
? "" : "__stdcall ", profile
);
513 for ( i
= 0; args
[i
]; i
++ )
515 if ( i
) fprintf( outfile
, ", " );
518 case 'w': fprintf( outfile
, "unsigned short" ); break;
519 case 's': fprintf( outfile
, "short" ); break;
520 case 'l': case 'T': fprintf( outfile
, "unsigned int" ); break;
521 case 'p': case 't': fprintf( outfile
, "void *" ); break;
524 if (reg_func
|| varargs
)
525 fprintf( outfile
, "%svoid *", i
? ", " : "" );
527 fprintf( outfile
, "void" );
528 fprintf( outfile
, " );\n" );
530 fprintf( outfile
, "static %s __stdcall __wine_%s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
531 ret_type
, make_c_identifier(prefix
), profile
, profile
,
532 reg_func
? ", void *context" : "" );
534 fprintf( outfile
, "{\n %sproc(\n", reg_func
? "" : "return " );
536 pos
= !usecdecl
? argsize
: 0;
537 for ( i
= 0; args
[i
]; i
++ )
539 if ( i
) fprintf( outfile
, ",\n" );
540 fprintf( outfile
, " " );
544 if ( !usecdecl
) pos
-= 2;
545 fprintf( outfile
, "*(unsigned short *)(args+%d)", pos
);
546 if ( usecdecl
) pos
+= 2;
549 case 's': /* s_word */
550 if ( !usecdecl
) pos
-= 2;
551 fprintf( outfile
, "*(short *)(args+%d)", pos
);
552 if ( usecdecl
) pos
+= 2;
555 case 'l': /* long or segmented pointer */
556 case 'T': /* segmented pointer to null-terminated string */
557 if ( !usecdecl
) pos
-= 4;
558 fprintf( outfile
, "*(unsigned int *)(args+%d)", pos
);
559 if ( usecdecl
) pos
+= 4;
562 case 'p': /* linear pointer */
563 case 't': /* linear pointer to null-terminated string */
564 if ( !usecdecl
) pos
-= 4;
565 fprintf( outfile
, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
567 if ( usecdecl
) pos
+= 4;
571 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
575 fprintf( outfile
, "%s context", i
? ",\n" : "" );
577 fprintf( outfile
, "%s args + %d", i
? ",\n" : "", argsize
);
578 fprintf( outfile
, " );\n}\n\n" );
583 /*******************************************************************
586 static const char *get_function_name( const ORDDEF
*odp
)
588 static char buffer
[80];
590 sprintf( buffer
, "%s_%s_%s",
591 (odp
->type
== TYPE_PASCAL
) ? "p" :
592 (odp
->type
== TYPE_VARARGS
) ? "v" : "c",
593 (odp
->flags
& FLAG_REGISTER
) ? "regs" :
594 (odp
->flags
& FLAG_RET16
) ? "word" : "long",
595 odp
->u
.func
.arg_types
);
600 /*******************************************************************
603 static int Spec16TypeCompare( const void *e1
, const void *e2
)
605 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
606 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
608 int type1
= odp1
->type
;
609 int type2
= odp2
->type
;
611 if (type1
== TYPE_STUB
) type1
= TYPE_CDECL
;
612 if (type2
== TYPE_STUB
) type2
= TYPE_CDECL
;
614 if ((retval
= type1
- type2
) != 0) return retval
;
616 type1
= odp1
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
617 type2
= odp2
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
619 if ((retval
= type1
- type2
) != 0) return retval
;
621 return strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
625 /*******************************************************************
628 * Output the functions for stub entry points
630 static void output_stub_funcs( FILE *outfile
, const DLLSPEC
*spec
)
635 for (i
= 0; i
<= spec
->limit
; i
++)
637 ORDDEF
*odp
= spec
->ordinals
[i
];
638 if (!odp
|| odp
->type
!= TYPE_STUB
) continue;
639 fprintf( outfile
, "#ifdef __GNUC__\n" );
640 fprintf( outfile
, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
641 fprintf( outfile
, "#endif\n" );
642 fprintf( outfile
, "static void __wine_unimplemented( const char *func )\n{\n" );
643 fprintf( outfile
, " extern void __stdcall RaiseException( unsigned int, unsigned int, unsigned int, const void ** );\n" );
644 fprintf( outfile
, " const void *args[2];\n" );
645 fprintf( outfile
, " args[0] = \"%s\";\n", spec
->file_name
);
646 fprintf( outfile
, " args[1] = func;\n" );
647 fprintf( outfile
, " for (;;) RaiseException( 0x%08x, %d, 2, args );\n}\n\n",
648 EXCEPTION_WINE_STUB
, EH_NONCONTINUABLE
);
651 for (i
= 0; i
<= spec
->limit
; i
++)
653 ORDDEF
*odp
= spec
->ordinals
[i
];
654 if (!odp
|| odp
->type
!= TYPE_STUB
) continue;
655 odp
->link_name
= xrealloc( odp
->link_name
, strlen(odp
->name
) + 13 );
656 strcpy( odp
->link_name
, "__wine_stub_" );
657 strcat( odp
->link_name
, odp
->name
);
658 for (p
= odp
->link_name
; *p
; p
++) if (!isalnum(*p
)) *p
= '_';
659 fprintf( outfile
, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
660 odp
->link_name
, odp
->name
);
665 /*******************************************************************
668 * Build a Win16 assembly file from a spec file.
670 void BuildSpec16File( FILE *outfile
, DLLSPEC
*spec
)
672 ORDDEF
**type
, **typelist
;
673 int i
, nFuncs
, nTypes
;
674 int code_offset
, data_offset
;
676 char constructor
[100], destructor
[100];
678 unsigned short code_selector
= get_cs();
683 output_file_header( outfile
);
684 fprintf( outfile
, "extern unsigned short __wine_call_from_16_word();\n" );
685 fprintf( outfile
, "extern unsigned int __wine_call_from_16_long();\n" );
686 fprintf( outfile
, "extern void __wine_call_from_16_regs();\n" );
687 fprintf( outfile
, "extern void __wine_call_from_16_thunk();\n" );
689 data
= (unsigned char *)xmalloc( 0x10000 );
690 memset( data
, 0, 16 );
693 if (!spec
->dll_name
) /* set default name from file name */
696 spec
->dll_name
= xstrdup( spec
->file_name
);
697 if ((p
= strrchr( spec
->dll_name
, '.' ))) *p
= 0;
700 output_stub_funcs( outfile
, spec
);
702 /* Build sorted list of all argument types, without duplicates */
704 typelist
= (ORDDEF
**)calloc( spec
->limit
+1, sizeof(ORDDEF
*) );
706 for (i
= nFuncs
= 0; i
<= spec
->limit
; i
++)
708 ORDDEF
*odp
= spec
->ordinals
[i
];
716 typelist
[nFuncs
++] = odp
;
723 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
728 typelist
[nTypes
++] = typelist
[i
++];
729 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
733 /* Output CallFrom16 routines needed by this .spec file */
735 for ( i
= 0; i
< nTypes
; i
++ )
739 strcpy( profile
, get_function_name( typelist
[i
] ));
740 BuildCallFrom16Func( outfile
, profile
, spec
->file_name
);
744 /* Output the DLL functions prototypes */
746 for (i
= 0; i
<= spec
->limit
; i
++)
748 ORDDEF
*odp
= spec
->ordinals
[i
];
755 fprintf( outfile
, "extern void %s();\n", odp
->link_name
);
762 /* Output code segment */
764 fprintf( outfile
, "\n#include \"pshpack1.h\"\n" );
765 fprintf( outfile
, "\nstatic struct code_segment\n{\n" );
766 fprintf( outfile
, " struct {\n" );
768 fprintf( outfile
, " unsigned char pushl;\n" ); /* pushl $relay */
769 fprintf( outfile
, " void *relay;\n" );
770 fprintf( outfile
, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
771 fprintf( outfile
, " void *glue;\n" );
772 fprintf( outfile
, " unsigned short flatcs;\n" );
774 fprintf( outfile
, " unsigned short lret;\n" ); /* lret $args */
775 fprintf( outfile
, " unsigned short args;\n" );
776 fprintf( outfile
, " unsigned int arg_types[2];\n" );
777 fprintf( outfile
, " } call[%d];\n", nTypes
);
778 fprintf( outfile
, " struct {\n" );
780 fprintf( outfile
, " unsigned short pushw_bp;\n" ); /* pushw %bp */
781 fprintf( outfile
, " unsigned char pushl;\n" ); /* pushl $target */
783 fprintf( outfile
, " void (*target)();\n" );
784 fprintf( outfile
, " unsigned short call;\n" ); /* call CALLFROM16 */
785 fprintf( outfile
, " short callfrom16;\n" );
786 fprintf( outfile
, " } entry[%d];\n", nFuncs
);
787 fprintf( outfile
, "} code_segment =\n{\n {\n" );
791 for ( i
= 0; i
< nTypes
; i
++ )
793 char profile
[101], *arg
;
794 unsigned int arg_types
[2];
797 strcpy( profile
, get_function_name( typelist
[i
] ));
798 if ( typelist
[i
]->type
== TYPE_PASCAL
)
799 for ( arg
= typelist
[i
]->u
.func
.arg_types
; *arg
; arg
++ )
803 case 's': /* s_word */
806 case 'l': /* long or segmented pointer */
807 case 'T': /* segmented pointer to null-terminated string */
808 case 'p': /* linear pointer */
809 case 't': /* linear pointer to null-terminated string */
814 /* build the arg types bit fields */
815 arg_types
[0] = arg_types
[1] = 0;
816 for (j
= 0; typelist
[i
]->u
.func
.arg_types
[j
]; j
++)
819 switch(typelist
[i
]->u
.func
.arg_types
[j
])
821 case 'w': type
= ARG_WORD
; break;
822 case 's': type
= ARG_SWORD
; break;
823 case 'l': type
= ARG_LONG
; break;
824 case 'p': type
= ARG_PTR
; break;
825 case 't': type
= ARG_STR
; break;
826 case 'T': type
= ARG_SEGSTR
; break;
828 arg_types
[j
/ 10] |= type
<< (3 * (j
% 10));
830 if (typelist
[i
]->flags
& FLAG_REGISTER
) arg_types
[0] |= ARG_REGISTER
;
831 if (typelist
[i
]->flags
& FLAG_RET16
) arg_types
[0] |= ARG_RET16
;
834 fprintf( outfile
, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
835 make_c_identifier(spec
->file_name
), profile
,
836 (typelist
[i
]->flags
& FLAG_REGISTER
) ? "regs":
837 (typelist
[i
]->flags
& FLAG_RET16
) ? "word" : "long" );
839 fprintf( outfile
, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
840 code_selector
, argsize
, arg_types
[0], arg_types
[1] );
842 fprintf( outfile
, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
843 code_selector
, arg_types
[0], arg_types
[1] );
846 fprintf( outfile
, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
847 argsize
, arg_types
[0], arg_types
[1] );
849 fprintf( outfile
, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
850 arg_types
[0], arg_types
[1] );
852 code_offset
+= sizeof(CALLFROM16
);
854 fprintf( outfile
, " },\n {\n" );
856 for (i
= 0; i
<= spec
->limit
; i
++)
858 ORDDEF
*odp
= spec
->ordinals
[i
];
863 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
867 odp
->offset
= data_offset
;
868 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
875 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
878 fprintf( outfile
, " /* %s.%d */ ", spec
->dll_name
, i
);
880 fprintf( outfile
, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
882 fprintf( outfile
, "{ %s, 0xe866, %d, /* %s */ },\n",
885 (type
-typelist
)*sizeof(CALLFROM16
) -
886 (code_offset
+ sizeof(ENTRYPOINT16
)),
887 get_function_name( odp
) );
889 odp
->offset
= code_offset
;
890 code_offset
+= sizeof(ENTRYPOINT16
);
894 fprintf(stderr
,"build: function type %d not available for Win16\n",
900 fprintf( outfile
, " }\n};\n" );
902 /* Build the module */
904 output_module_data( outfile
, code_offset
, data
, data_offset
, spec
);
906 /* Output the DLL descriptor */
908 fprintf( outfile
, "#include \"poppack.h\"\n\n" );
910 fprintf( outfile
, "static const struct dll_descriptor\n{\n" );
911 fprintf( outfile
, " const struct module_data *module;\n" );
912 fprintf( outfile
, " struct code_segment *code_start;\n" );
913 fprintf( outfile
, " const unsigned char *rsrc;\n" );
914 fprintf( outfile
, "} descriptor =\n{\n" );
915 fprintf( outfile
, " &module,\n" );
916 fprintf( outfile
, " &code_segment\n" );
917 fprintf( outfile
, "};\n" );
919 /* Output the DLL constructor */
921 sprintf( constructor
, "__wine_spec_%s_init", make_c_identifier(spec
->file_name
) );
922 sprintf( destructor
, "__wine_spec_%s_fini", make_c_identifier(spec
->file_name
) );
923 output_dll_init( outfile
, constructor
, destructor
);
928 " extern void __wine_dll_register_16( const struct dll_descriptor *descr, const char *file_name );\n"
929 " __wine_dll_register_16( &descriptor, \"%s\" );\n"
930 "}\n", constructor
, spec
->file_name
);
934 " extern void __wine_dll_unregister_16( const struct dll_descriptor *descr );\n"
935 " __wine_dll_unregister_16( &descriptor );\n"