Sync to Wine-20050628:
[reactos.git] / reactos / tools / winebuild / spec16.c
1 /*
2 * 16-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
30 #include "wine/exception.h"
31 #include "wine/winbase16.h"
32
33 #include "build.h"
34
35
36 /*******************************************************************
37 * get_cs
38 */
39 static inline unsigned short get_cs(void)
40 {
41 unsigned short res = 0;
42 #ifdef __i386__
43 # ifdef __GNUC__
44 __asm__("movw %%cs,%w0" : "=r"(res));
45 # elif defined(_MSC_VER)
46 __asm { mov res, cs }
47 # endif
48 #endif /* __i386__ */
49 return res;
50 }
51
52
53 /*******************************************************************
54 * output_file_header
55 *
56 * Output a file header with the common declarations we need.
57 */
58 static void output_file_header( FILE *outfile )
59 {
60 output_standard_file_header( outfile );
61 fprintf( outfile, "extern struct\n{\n" );
62 fprintf( outfile, " void *base[8192];\n" );
63 fprintf( outfile, " unsigned long limit[8192];\n" );
64 fprintf( outfile, " unsigned char flags[8192];\n" );
65 fprintf( outfile, "} wine_ldt_copy;\n\n" );
66 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
67 }
68
69
70 /*******************************************************************
71 * output_entry_table
72 */
73 static int output_entry_table( unsigned char **ret_buff, DLLSPEC *spec )
74 {
75 int i, prev = 0, prev_sel = -1;
76 unsigned char *pstr, *buffer;
77 unsigned char *bundle = NULL;
78
79 buffer = xmalloc( spec->limit * 5 ); /* we use at most 5 bytes per entry-point */
80 pstr = buffer;
81
82 for (i = 1; i <= spec->limit; i++)
83 {
84 int selector = 0;
85 WORD offset;
86 ORDDEF *odp = spec->ordinals[i];
87 if (!odp) continue;
88
89 switch (odp->type)
90 {
91 case TYPE_CDECL:
92 case TYPE_PASCAL:
93 case TYPE_VARARGS:
94 case TYPE_STUB:
95 selector = 1; /* Code selector */
96 break;
97 case TYPE_VARIABLE:
98 selector = 2; /* Data selector */
99 break;
100 case TYPE_ABS:
101 selector = 0xfe; /* Constant selector */
102 break;
103 default:
104 continue;
105 }
106
107 if (!bundle || prev + 1 != i || prev_sel != selector || *bundle == 255)
108 {
109 /* need to start a new bundle */
110
111 if (prev + 1 != i)
112 {
113 int skip = i - (prev + 1);
114 while (skip > 255)
115 {
116 *pstr++ = 255;
117 *pstr++ = 0;
118 skip -= 255;
119 }
120 *pstr++ = skip;
121 *pstr++ = 0;
122 }
123
124 bundle = pstr;
125 *pstr++ = 0;
126 *pstr++ = selector;
127 prev_sel = selector;
128 }
129 /* output the entry */
130 *pstr++ = 3; /* flags: exported & public data */
131 offset = odp->offset;
132 memcpy( pstr, &offset, sizeof(WORD) );
133 pstr += sizeof(WORD);
134 (*bundle)++; /* increment bundle entry count */
135 prev = i;
136 }
137 *pstr++ = 0;
138 if ((pstr - buffer) & 1) *pstr++ = 0;
139 *ret_buff = xrealloc( buffer, pstr - buffer );
140 return pstr - buffer;
141 }
142
143
144 /*******************************************************************
145 * output_bytes
146 */
147 static void output_bytes( FILE *outfile, const void *buffer, unsigned int size )
148 {
149 unsigned int i;
150 const unsigned char *ptr = buffer;
151
152 fprintf( outfile, " {" );
153 for (i = 0; i < size; i++)
154 {
155 if (!(i & 7)) fprintf( outfile, "\n " );
156 fprintf( outfile, " 0x%02x", *ptr++ );
157 if (i < size - 1) fprintf( outfile, "," );
158 }
159 fprintf( outfile, "\n },\n" );
160 }
161
162
163 /*******************************************************************
164 * BuildCallFrom16Func
165 *
166 * Build a 16-bit-to-Wine callback glue function.
167 *
168 * The generated routines are intended to be used as argument conversion
169 * routines to be called by the CallFrom16... core. Thus, the prototypes of
170 * the generated routines are (see also CallFrom16):
171 *
172 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
173 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
174 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
175 * CONTEXT86 *context );
176 *
177 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
178 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
179 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
180 * 'T'=segmented pointer to null-terminated string).
181 *
182 * The generated routines fetch the arguments from the 16-bit stack (pointed
183 * to by 'args'); the offsets of the single argument values are computed
184 * according to the calling convention and the argument types. Then, the
185 * 32-bit entry point is called with these arguments.
186 *
187 * For register functions, the arguments (if present) are converted just
188 * the same as for normal functions, but in addition the CONTEXT86 pointer
189 * filled with the current register values is passed to the 32-bit routine.
190 */
191 static void BuildCallFrom16Func( FILE *outfile, const char *profile, const char *prefix )
192 {
193 int i, pos, argsize = 0;
194 int short_ret = 0;
195 int reg_func = 0;
196 int usecdecl = 0;
197 int varargs = 0;
198 const char *args = profile + 7;
199 const char *ret_type;
200
201 /* Parse function type */
202
203 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
204 else if (!strncmp( "v_", profile, 2 )) varargs = usecdecl = 1;
205 else if (strncmp( "p_", profile, 2 ))
206 {
207 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
208 return;
209 }
210
211 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
212 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
213 else if (strncmp( "long_", profile + 2, 5 ))
214 {
215 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
216 return;
217 }
218
219 for ( i = 0; args[i]; i++ )
220 switch ( args[i] )
221 {
222 case 'w': /* word */
223 case 's': /* s_word */
224 argsize += 2;
225 break;
226 case 'l': /* long or segmented pointer */
227 case 'T': /* segmented pointer to null-terminated string */
228 case 'p': /* linear pointer */
229 case 't': /* linear pointer to null-terminated string */
230 argsize += 4;
231 break;
232 }
233
234 ret_type = reg_func? "void" : short_ret ? "unsigned short" : "unsigned int";
235
236 fprintf( outfile, "typedef %s (%s*proc_%s_t)( ",
237 ret_type, usecdecl ? "" : "__stdcall ", profile );
238 args = profile + 7;
239 for ( i = 0; args[i]; i++ )
240 {
241 if ( i ) fprintf( outfile, ", " );
242 switch (args[i])
243 {
244 case 'w': fprintf( outfile, "unsigned short" ); break;
245 case 's': fprintf( outfile, "short" ); break;
246 case 'l': case 'T': fprintf( outfile, "unsigned int" ); break;
247 case 'p': case 't': fprintf( outfile, "void *" ); break;
248 }
249 }
250 if (reg_func || varargs)
251 fprintf( outfile, "%svoid *", i? ", " : "" );
252 else if ( !i )
253 fprintf( outfile, "void" );
254 fprintf( outfile, " );\n" );
255
256 fprintf( outfile, "static %s __stdcall __wine_%s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
257 ret_type, make_c_identifier(prefix), profile, profile,
258 reg_func? ", void *context" : "" );
259
260 fprintf( outfile, "{\n %sproc(\n", reg_func ? "" : "return " );
261 args = profile + 7;
262 pos = !usecdecl? argsize : 0;
263 for ( i = 0; args[i]; i++ )
264 {
265 if ( i ) fprintf( outfile, ",\n" );
266 fprintf( outfile, " " );
267 switch (args[i])
268 {
269 case 'w': /* word */
270 if ( !usecdecl ) pos -= 2;
271 fprintf( outfile, "*(unsigned short *)(args+%d)", pos );
272 if ( usecdecl ) pos += 2;
273 break;
274
275 case 's': /* s_word */
276 if ( !usecdecl ) pos -= 2;
277 fprintf( outfile, "*(short *)(args+%d)", pos );
278 if ( usecdecl ) pos += 2;
279 break;
280
281 case 'l': /* long or segmented pointer */
282 case 'T': /* segmented pointer to null-terminated string */
283 if ( !usecdecl ) pos -= 4;
284 fprintf( outfile, "*(unsigned int *)(args+%d)", pos );
285 if ( usecdecl ) pos += 4;
286 break;
287
288 case 'p': /* linear pointer */
289 case 't': /* linear pointer to null-terminated string */
290 if ( !usecdecl ) pos -= 4;
291 fprintf( outfile, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
292 pos + 2, pos );
293 if ( usecdecl ) pos += 4;
294 break;
295
296 default:
297 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
298 }
299 }
300 if ( reg_func )
301 fprintf( outfile, "%s context", i? ",\n" : "" );
302 else if (varargs)
303 fprintf( outfile, "%s args + %d", i? ",\n" : "", argsize );
304 fprintf( outfile, " );\n}\n\n" );
305 }
306
307
308 /*******************************************************************
309 * get_function_name
310 */
311 static const char *get_function_name( const ORDDEF *odp )
312 {
313 static char buffer[80];
314
315 sprintf( buffer, "%s_%s_%s",
316 (odp->type == TYPE_PASCAL) ? "p" :
317 (odp->type == TYPE_VARARGS) ? "v" : "c",
318 (odp->flags & FLAG_REGISTER) ? "regs" :
319 (odp->flags & FLAG_RET16) ? "word" : "long",
320 odp->u.func.arg_types );
321 return buffer;
322 }
323
324
325 /*******************************************************************
326 * Spec16TypeCompare
327 */
328 static int Spec16TypeCompare( const void *e1, const void *e2 )
329 {
330 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
331 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
332 int retval;
333 int type1 = odp1->type;
334 int type2 = odp2->type;
335
336 if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
337 if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
338
339 if ((retval = type1 - type2) != 0) return retval;
340
341 type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
342 type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
343
344 if ((retval = type1 - type2) != 0) return retval;
345
346 return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
347 }
348
349
350 /*******************************************************************
351 * output_stub_funcs
352 *
353 * Output the functions for stub entry points
354 */
355 static void output_stub_funcs( FILE *outfile, const DLLSPEC *spec )
356 {
357 int i;
358 char *p;
359
360 for (i = 0; i <= spec->limit; i++)
361 {
362 ORDDEF *odp = spec->ordinals[i];
363 if (!odp || odp->type != TYPE_STUB) continue;
364 fprintf( outfile, "#ifdef __GNUC__\n" );
365 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
366 fprintf( outfile, "#endif\n" );
367 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
368 fprintf( outfile, " extern void __stdcall RaiseException( unsigned int, unsigned int, unsigned int, const void ** );\n" );
369 fprintf( outfile, " const void *args[2];\n" );
370 fprintf( outfile, " args[0] = \"%s\";\n", spec->file_name );
371 fprintf( outfile, " args[1] = func;\n" );
372 fprintf( outfile, " for (;;) RaiseException( 0x%08x, %d, 2, args );\n}\n\n",
373 EXCEPTION_WINE_STUB, EH_NONCONTINUABLE );
374 break;
375 }
376 for (i = 0; i <= spec->limit; i++)
377 {
378 ORDDEF *odp = spec->ordinals[i];
379 if (!odp || odp->type != TYPE_STUB) continue;
380 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
381 strcpy( odp->link_name, "__wine_stub_" );
382 strcat( odp->link_name, odp->name );
383 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
384 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
385 odp->link_name, odp->name );
386 }
387 }
388
389
390 /*******************************************************************
391 * BuildSpec16File
392 *
393 * Build a Win16 assembly file from a spec file.
394 */
395 void BuildSpec16File( FILE *outfile, DLLSPEC *spec )
396 {
397 ORDDEF **type, **typelist;
398 int i, nFuncs, nTypes;
399 unsigned char *resdir_buffer, *resdata_buffer, *et_buffer, *data_buffer;
400 unsigned char string[256];
401 unsigned int ne_offset, segtable_offset, impnames_offset;
402 unsigned int entrypoint_size, callfrom_size;
403 unsigned int code_size, code_offset;
404 unsigned int data_size, data_offset;
405 unsigned int resnames_size, resnames_offset;
406 unsigned int resdir_size, resdir_offset;
407 unsigned int resdata_size, resdata_offset, resdata_align;
408 unsigned int et_size, et_offset;
409
410 char constructor[100], destructor[100];
411 unsigned short code_selector = get_cs();
412
413 /* File header */
414
415 output_file_header( outfile );
416 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
417 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
418 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
419 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
420
421 data_buffer = xmalloc( 0x10000 );
422 memset( data_buffer, 0, 16 );
423 data_size = 16;
424
425 if (!spec->dll_name) /* set default name from file name */
426 {
427 char *p;
428 spec->dll_name = xstrdup( spec->file_name );
429 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
430 }
431
432 output_stub_funcs( outfile, spec );
433
434 /* Build sorted list of all argument types, without duplicates */
435
436 typelist = (ORDDEF **)calloc( spec->limit+1, sizeof(ORDDEF *) );
437
438 for (i = nFuncs = 0; i <= spec->limit; i++)
439 {
440 ORDDEF *odp = spec->ordinals[i];
441 if (!odp) continue;
442 switch (odp->type)
443 {
444 case TYPE_CDECL:
445 case TYPE_PASCAL:
446 case TYPE_VARARGS:
447 case TYPE_STUB:
448 typelist[nFuncs++] = odp;
449
450 default:
451 break;
452 }
453 }
454
455 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
456
457 i = nTypes = 0;
458 while ( i < nFuncs )
459 {
460 typelist[nTypes++] = typelist[i++];
461 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
462 i++;
463 }
464
465 /* Output CallFrom16 routines needed by this .spec file */
466 for ( i = 0; i < nTypes; i++ )
467 {
468 char profile[101];
469
470 strcpy( profile, get_function_name( typelist[i] ));
471 BuildCallFrom16Func( outfile, profile, spec->file_name );
472 }
473
474 /* compute code and data sizes, set offsets, and output prototypes */
475
476 entrypoint_size = 2 + 5 + 4; /* pushw bp + pushl target + call */
477 callfrom_size = 5 + 7 + 4 + 8; /* pushl relay + lcall cs:glue + lret n + args */
478 code_size = nTypes * callfrom_size;
479
480 for (i = 0; i <= spec->limit; i++)
481 {
482 ORDDEF *odp = spec->ordinals[i];
483 if (!odp) continue;
484 switch (odp->type)
485 {
486 case TYPE_ABS:
487 odp->offset = LOWORD(odp->u.abs.value);
488 break;
489 case TYPE_VARIABLE:
490 odp->offset = data_size;
491 memcpy( data_buffer + data_size, odp->u.var.values, odp->u.var.n_values * sizeof(int) );
492 data_size += odp->u.var.n_values * sizeof(int);
493 break;
494 case TYPE_CDECL:
495 case TYPE_PASCAL:
496 case TYPE_VARARGS:
497 fprintf( outfile, "extern void %s();\n", odp->link_name );
498 /* fall through */
499 case TYPE_STUB:
500 odp->offset = code_size;
501 code_size += entrypoint_size;
502 break;
503 default:
504 assert(0);
505 break;
506 }
507 }
508 data_buffer = xrealloc( data_buffer, data_size ); /* free unneeded data */
509
510 /* Output the module structure */
511
512 /* DOS header */
513
514 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
515 fprintf( outfile, "static const struct module_data\n{\n" );
516 fprintf( outfile, " struct\n {\n" );
517 fprintf( outfile, " unsigned short e_magic;\n" );
518 fprintf( outfile, " unsigned short e_cblp;\n" );
519 fprintf( outfile, " unsigned short e_cp;\n" );
520 fprintf( outfile, " unsigned short e_crlc;\n" );
521 fprintf( outfile, " unsigned short e_cparhdr;\n" );
522 fprintf( outfile, " unsigned short e_minalloc;\n" );
523 fprintf( outfile, " unsigned short e_maxalloc;\n" );
524 fprintf( outfile, " unsigned short e_ss;\n" );
525 fprintf( outfile, " unsigned short e_sp;\n" );
526 fprintf( outfile, " unsigned short e_csum;\n" );
527 fprintf( outfile, " unsigned short e_ip;\n" );
528 fprintf( outfile, " unsigned short e_cs;\n" );
529 fprintf( outfile, " unsigned short e_lfarlc;\n" );
530 fprintf( outfile, " unsigned short e_ovno;\n" );
531 fprintf( outfile, " unsigned short e_res[4];\n" );
532 fprintf( outfile, " unsigned short e_oemid;\n" );
533 fprintf( outfile, " unsigned short e_oeminfo;\n" );
534 fprintf( outfile, " unsigned short e_res2[10];\n" );
535 fprintf( outfile, " unsigned int e_lfanew;\n" );
536 fprintf( outfile, " } dos_header;\n" );
537
538 /* NE header */
539
540 ne_offset = 64;
541 fprintf( outfile, " struct\n {\n" );
542 fprintf( outfile, " unsigned short ne_magic;\n" );
543 fprintf( outfile, " unsigned char ne_ver;\n" );
544 fprintf( outfile, " unsigned char ne_rev;\n" );
545 fprintf( outfile, " unsigned short ne_enttab;\n" );
546 fprintf( outfile, " unsigned short ne_cbenttab;\n" );
547 fprintf( outfile, " int ne_crc;\n" );
548 fprintf( outfile, " unsigned short ne_flags;\n" );
549 fprintf( outfile, " unsigned short ne_autodata;\n" );
550 fprintf( outfile, " unsigned short ne_heap;\n" );
551 fprintf( outfile, " unsigned short ne_stack;\n" );
552 fprintf( outfile, " unsigned int ne_csip;\n" );
553 fprintf( outfile, " unsigned int ne_sssp;\n" );
554 fprintf( outfile, " unsigned short ne_cseg;\n" );
555 fprintf( outfile, " unsigned short ne_cmod;\n" );
556 fprintf( outfile, " unsigned short ne_cbnrestab;\n" );
557 fprintf( outfile, " unsigned short ne_segtab;\n" );
558 fprintf( outfile, " unsigned short ne_rsrctab;\n" );
559 fprintf( outfile, " unsigned short ne_restab;\n" );
560 fprintf( outfile, " unsigned short ne_modtab;\n" );
561 fprintf( outfile, " unsigned short ne_imptab;\n" );
562 fprintf( outfile, " unsigned int ne_nrestab;\n" );
563 fprintf( outfile, " unsigned short ne_cmovent;\n" );
564 fprintf( outfile, " unsigned short ne_align;\n" );
565 fprintf( outfile, " unsigned short ne_cres;\n" );
566 fprintf( outfile, " unsigned char ne_exetyp;\n" );
567 fprintf( outfile, " unsigned char ne_flagsothers;\n" );
568 fprintf( outfile, " unsigned short ne_pretthunks;\n" );
569 fprintf( outfile, " unsigned short ne_psegrefbytes;\n" );
570 fprintf( outfile, " unsigned short ne_swaparea;\n" );
571 fprintf( outfile, " unsigned short ne_expver;\n" );
572 fprintf( outfile, " } os2_header;\n" );
573
574 /* segment table */
575
576 segtable_offset = 64;
577 fprintf( outfile, " struct\n {\n" );
578 fprintf( outfile, " unsigned short filepos;\n" );
579 fprintf( outfile, " unsigned short size;\n" );
580 fprintf( outfile, " unsigned short flags;\n" );
581 fprintf( outfile, " unsigned short minsize;\n" );
582 fprintf( outfile, " } segtable[2];\n" );
583
584 /* resource directory */
585
586 resdir_offset = segtable_offset + 2 * 8;
587 resdir_size = get_res16_directory_size( spec );
588 fprintf( outfile, " unsigned char resdir[%d];\n", resdir_size );
589
590 /* resident names table */
591
592 resnames_offset = resdir_offset + resdir_size;
593 fprintf( outfile, " struct\n {\n" );
594 fprintf( outfile, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_0;\n",
595 strlen( spec->dll_name ) );
596 resnames_size = 3 + strlen( spec->dll_name );
597 for (i = 1; i <= spec->limit; i++)
598 {
599 ORDDEF *odp = spec->ordinals[i];
600 if (!odp || !odp->name[0]) continue;
601 fprintf( outfile, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_%d;\n",
602 strlen(odp->name), i );
603 resnames_size += 3 + strlen( odp->name );
604 }
605 fprintf( outfile, " unsigned char name_last[%d];\n", 2 - (resnames_size & 1) );
606 resnames_size = (resnames_size + 2) & ~1;
607 fprintf( outfile, " } resnames;\n" );
608
609 /* imported names table */
610
611 impnames_offset = resnames_offset + resnames_size;
612 fprintf( outfile, " unsigned char impnames[2];\n" );
613
614 /* entry table */
615
616 et_offset = impnames_offset + 2;
617 et_size = output_entry_table( &et_buffer, spec );
618 fprintf( outfile, " unsigned char entry_table[%d];\n", et_size );
619
620 /* code segment */
621
622 code_offset = et_offset + et_size;
623 fprintf( outfile, " struct {\n" );
624 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
625 fprintf( outfile, " void *relay;\n" );
626 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
627 fprintf( outfile, " void *glue;\n" );
628 fprintf( outfile, " unsigned short flatcs;\n" );
629 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
630 fprintf( outfile, " unsigned short args;\n" );
631 fprintf( outfile, " unsigned int arg_types[2];\n" );
632 fprintf( outfile, " } call[%d];\n", nTypes );
633 fprintf( outfile, " struct {\n" );
634 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
635 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
636 fprintf( outfile, " void (*target)();\n" );
637 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
638 fprintf( outfile, " short callfrom16;\n" );
639 fprintf( outfile, " } entry[%d];\n", nFuncs );
640
641 /* data segment */
642
643 data_offset = code_offset + code_size;
644 fprintf( outfile, " unsigned char data_segment[%d];\n", data_size );
645 if (data_offset + data_size >= 0x10000)
646 fatal_error( "Not supported yet: 16-bit module data larger than 64K\n" );
647
648 /* resource data */
649
650 resdata_offset = ne_offset + data_offset + data_size;
651 for (resdata_align = 0; resdata_align < 16; resdata_align++)
652 {
653 unsigned int size = get_res16_data_size( spec, resdata_offset, resdata_align );
654 if ((resdata_offset + size) >> resdata_align <= 0xffff) break;
655 }
656 output_res16_directory( &resdir_buffer, spec, resdata_offset, resdata_align );
657 resdata_size = output_res16_data( &resdata_buffer, spec, resdata_offset, resdata_align );
658 if (resdata_size) fprintf( outfile, " unsigned char resources[%d];\n", resdata_size );
659
660 /* Output the module data */
661
662 /* DOS header */
663
664 fprintf( outfile, "} module =\n{\n {\n" );
665 fprintf( outfile, " 0x%04x,\n", IMAGE_DOS_SIGNATURE ); /* e_magic */
666 fprintf( outfile, " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" );
667 fprintf( outfile, " { 0, 0, 0, 0, }, 0, 0,\n" );
668 fprintf( outfile, " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\n" );
669 fprintf( outfile, " sizeof(module.dos_header)\n" ); /* e_lfanew */
670
671 /* NE header */
672
673 fprintf( outfile, " },\n {\n" );
674 fprintf( outfile, " 0x%04x,\n", IMAGE_OS2_SIGNATURE ); /* ne_magic */
675 fprintf( outfile, " 0, 0,\n" );
676 fprintf( outfile, " %d,\n", et_offset ); /* ne_enttab */
677 fprintf( outfile, " sizeof(module.entry_table),\n" ); /* ne_cbenttab */
678 fprintf( outfile, " 0,\n" ); /* ne_crc */
679 fprintf( outfile, " 0x%04x,\n", /* ne_flags */
680 NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE );
681 fprintf( outfile, " 2,\n" ); /* ne_autodata */
682 fprintf( outfile, " %d,\n", spec->heap_size ); /* ne_heap */
683 fprintf( outfile, " 0, 0, 0,\n" );
684 fprintf( outfile, " 2,\n" ); /* ne_cseg */
685 fprintf( outfile, " 0,\n" ); /* ne_cmod */
686 fprintf( outfile, " 0,\n" ); /* ne_cbnrestab */
687 fprintf( outfile, " %d,\n", segtable_offset ); /* ne_segtab */
688 fprintf( outfile, " %d,\n", resdir_offset ); /* ne_rsrctab */
689 fprintf( outfile, " %d,\n", resnames_offset ); /* ne_restab */
690 fprintf( outfile, " %d,\n", impnames_offset ); /* ne_modtab */
691 fprintf( outfile, " %d,\n", impnames_offset ); /* ne_imptab */
692 fprintf( outfile, " 0,\n" ); /* ne_nrestab */
693 fprintf( outfile, " 0,\n" ); /* ne_cmovent */
694 fprintf( outfile, " 0,\n" ); /* ne_align */
695 fprintf( outfile, " 0,\n" ); /* ne_cres */
696 fprintf( outfile, " 0x%04x,\n", NE_OSFLAGS_WINDOWS ); /* ne_exetyp */
697 fprintf( outfile, " 0x%04x,\n", NE_AFLAGS_FASTLOAD ); /* ne_flagsothers */
698 fprintf( outfile, " 0,\n" ); /* ne_pretthunks */
699 fprintf( outfile, " 0,\n" ); /* ne_psegrefbytes */
700 fprintf( outfile, " 0,\n" ); /* ne_swaparea */
701 fprintf( outfile, " 0\n" ); /* ne_expver */
702 fprintf( outfile, " },\n" );
703
704 /* segment table */
705
706 fprintf( outfile, " {\n" );
707 fprintf( outfile, " { %d, %d, 0x%04x, %d },\n",
708 ne_offset + code_offset, code_size, NE_SEGFLAGS_32BIT, code_size );
709 fprintf( outfile, " { %d, %d, 0x%04x, %d },\n",
710 ne_offset + data_offset, data_size, NE_SEGFLAGS_DATA, data_size );
711 fprintf( outfile, " },\n" );
712
713 /* resource directory */
714
715 output_bytes( outfile, resdir_buffer, resdir_size );
716 free( resdir_buffer );
717
718 /* resident names table */
719
720 fprintf( outfile, " {\n" );
721 strcpy( string, spec->dll_name );
722 fprintf( outfile, " { %d, \"%s\", 0 },\n", strlen(string), strupper(string) );
723 for (i = 1; i <= spec->limit; i++)
724 {
725 ORDDEF *odp = spec->ordinals[i];
726 if (!odp || !odp->name[0]) continue;
727 strcpy( string, odp->name );
728 fprintf( outfile, " { %d, \"%s\", %d },\n", strlen(string), strupper(string), i );
729 }
730 fprintf( outfile, " { 0 }\n },\n" );
731
732 /* imported names table */
733
734 fprintf( outfile, " { 0, 0 },\n" );
735
736 /* entry table */
737
738 output_bytes( outfile, et_buffer, et_size );
739 free( et_buffer );
740
741 /* code segment */
742
743 fprintf( outfile, " {\n" );
744 for ( i = 0; i < nTypes; i++ )
745 {
746 char profile[101], *arg;
747 unsigned int arg_types[2];
748 int j, argsize = 0;
749
750 strcpy( profile, get_function_name( typelist[i] ));
751 if ( typelist[i]->type == TYPE_PASCAL )
752 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
753 switch ( *arg )
754 {
755 case 'w': /* word */
756 case 's': /* s_word */
757 argsize += 2;
758 break;
759 case 'l': /* long or segmented pointer */
760 case 'T': /* segmented pointer to null-terminated string */
761 case 'p': /* linear pointer */
762 case 't': /* linear pointer to null-terminated string */
763 argsize += 4;
764 break;
765 }
766
767 /* build the arg types bit fields */
768 arg_types[0] = arg_types[1] = 0;
769 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
770 {
771 int type = 0;
772 switch(typelist[i]->u.func.arg_types[j])
773 {
774 case 'w': type = ARG_WORD; break;
775 case 's': type = ARG_SWORD; break;
776 case 'l': type = ARG_LONG; break;
777 case 'p': type = ARG_PTR; break;
778 case 't': type = ARG_STR; break;
779 case 'T': type = ARG_SEGSTR; break;
780 }
781 arg_types[j / 10] |= type << (3 * (j % 10));
782 }
783 if (typelist[i]->flags & FLAG_REGISTER) arg_types[0] |= ARG_REGISTER;
784 if (typelist[i]->flags & FLAG_RET16) arg_types[0] |= ARG_RET16;
785
786 fprintf( outfile, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
787 make_c_identifier(spec->file_name), profile,
788 (typelist[i]->flags & FLAG_REGISTER) ? "regs":
789 (typelist[i]->flags & FLAG_RET16) ? "word" : "long" );
790 if (argsize)
791 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
792 code_selector, argsize, arg_types[0], arg_types[1] );
793 else
794 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
795 code_selector, arg_types[0], arg_types[1] );
796 }
797 fprintf( outfile, " },\n {\n" );
798
799 for (i = 0; i <= spec->limit; i++)
800 {
801 ORDDEF *odp = spec->ordinals[i];
802 if (!odp) continue;
803 switch (odp->type)
804 {
805 case TYPE_CDECL:
806 case TYPE_PASCAL:
807 case TYPE_VARARGS:
808 case TYPE_STUB:
809 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
810 assert( type );
811
812 fprintf( outfile, " /* %s.%d */ ", spec->dll_name, i );
813 fprintf( outfile,
814 "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
815 odp->link_name,
816 (type - typelist) * callfrom_size - (odp->offset + entrypoint_size),
817 get_function_name( odp ) );
818 break;
819 default:
820 break;
821 }
822 }
823 fprintf( outfile, " },\n" );
824
825
826 /* data_segment */
827
828 output_bytes( outfile, data_buffer, data_size );
829 free( data_buffer );
830
831 /* resource data */
832
833 if (resdata_size)
834 {
835 output_bytes( outfile, resdata_buffer, resdata_size );
836 free( resdata_buffer );
837 }
838
839 fprintf( outfile, "};\n" );
840 fprintf( outfile, "#include \"poppack.h\"\n\n" );
841
842 /* Output the DLL constructor */
843
844 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(spec->file_name) );
845 sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(spec->file_name) );
846
847 fprintf( outfile,
848 "void %s(void)\n"
849 "{\n"
850 " extern void __wine_dll_register_16( const struct module_data *, const char * );\n"
851 " __wine_dll_register_16( &module, \"%s\" );\n"
852 "}\n", constructor, spec->file_name );
853 fprintf( outfile,
854 "void %s(void)\n"
855 "{\n"
856 " extern void __wine_dll_unregister_16( const struct module_data * );\n"
857 " __wine_dll_unregister_16( &module );\n"
858 "}\n", destructor );
859
860 fprintf( outfile, "#ifndef __GNUC__\n" );
861 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
862 fprintf( outfile, "#endif\n" );
863
864 output_dll_init( outfile, constructor, destructor );
865
866 fprintf( outfile, "#ifndef __GNUC__\n" );
867 fprintf( outfile, "}\n" );
868 fprintf( outfile, "#endif\n" );
869 }