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