winebuild: Support building krnl386.exe in PE format.
[wine/zf.git] / tools / winebuild / spec16.c
bloba52c03aaa6a79c5ca00edc3ddd1c811d32512343
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
31 #include "build.h"
33 #define NE_FFLAGS_SINGLEDATA 0x0001
34 #define NE_FFLAGS_LIBMODULE 0x8000
36 /* argument type flags for relay debugging */
37 enum arg_types
39 ARG16_NONE = 0, /* indicates end of arg list */
40 ARG16_WORD, /* unsigned word */
41 ARG16_SWORD, /* signed word */
42 ARG16_LONG, /* long or segmented pointer */
43 ARG16_PTR, /* linear pointer */
44 ARG16_STR, /* linear pointer to null-terminated string */
45 ARG16_SEGSTR, /* segmented pointer to null-terminated string */
46 ARG16_VARARG /* start of varargs */
49 /* sequences of nops to fill a certain number of words */
50 static const char * const nop_sequence[4] =
52 ".byte 0x89,0xf6", /* mov %esi,%esi */
53 ".byte 0x8d,0x74,0x26,0x00", /* lea 0x00(%esi),%esi */
54 ".byte 0x8d,0xb6,0x00,0x00,0x00,0x00", /* lea 0x00000000(%esi),%esi */
55 ".byte 0x8d,0x74,0x26,0x00,0x8d,0x74,0x26,0x00" /* lea 0x00(%esi),%esi; lea 0x00(%esi),%esi */
58 static const char fakedll_signature[] = "Wine placeholder DLL";
60 static inline int is_function( const ORDDEF *odp )
62 if (odp->flags & FLAG_EXPORT32) return 0;
63 return (odp->type == TYPE_CDECL ||
64 odp->type == TYPE_PASCAL ||
65 odp->type == TYPE_VARARGS ||
66 odp->type == TYPE_STUB);
69 static const char *get_args_str( const ORDDEF *odp )
71 static char buffer[MAX_ARGUMENTS*2+1];
72 int i;
74 buffer[0] = 0;
75 for (i = 0; i < odp->u.func.nb_args; i++)
77 switch (odp->u.func.args[i])
79 case ARG_WORD: strcat( buffer, "w" ); break;
80 case ARG_SWORD: strcat( buffer, "s" ); break;
81 case ARG_SEGSTR: strcat( buffer, "T" ); break;
82 case ARG_STR: strcat( buffer, "t" ); break;
83 case ARG_LONG:
84 case ARG_FLOAT:
85 case ARG_SEGPTR: strcat( buffer, "l" ); break;
86 case ARG_PTR:
87 case ARG_WSTR:
88 case ARG_INT128: strcat( buffer, "p" ); break;
89 case ARG_INT64:
90 case ARG_DOUBLE: strcat( buffer, "ll" ); break;
93 return buffer;
96 /*******************************************************************
97 * output_entries
99 * Output entries for individual symbols in the entry table.
101 static void output_entries( DLLSPEC *spec, int first, int count )
103 int i;
105 for (i = 0; i < count; i++)
107 ORDDEF *odp = spec->ordinals[first + i];
108 output( "\t.byte 0x03\n" ); /* flags: exported & public data */
109 switch (odp->type)
111 case TYPE_CDECL:
112 case TYPE_PASCAL:
113 case TYPE_VARARGS:
114 case TYPE_STUB:
115 output( "\t.short .L__wine_%s_%u-.L__wine_spec_code_segment\n", spec->c_name, first + i );
116 break;
117 case TYPE_VARIABLE:
118 output( "\t.short .L__wine_%s_%u-.L__wine_spec_data_segment\n", spec->c_name, first + i );
119 break;
120 case TYPE_ABS:
121 output( "\t.short 0x%04x /* %s */\n",
122 odp->u.abs.value, odp->name );
123 break;
124 default:
125 assert(0);
131 /*******************************************************************
132 * output_entry_table
134 static void output_entry_table( DLLSPEC *spec )
136 int i, prev = 0, prev_sel = -1, bundle_count = 0;
138 for (i = 1; i <= spec->limit; i++)
140 int selector = 0;
141 ORDDEF *odp = spec->ordinals[i];
142 if (!odp) continue;
143 if (odp->flags & FLAG_EXPORT32) continue;
145 switch (odp->type)
147 case TYPE_CDECL:
148 case TYPE_PASCAL:
149 case TYPE_VARARGS:
150 case TYPE_STUB:
151 selector = 1; /* Code selector */
152 break;
153 case TYPE_VARIABLE:
154 selector = 2; /* Data selector */
155 break;
156 case TYPE_ABS:
157 selector = 0xfe; /* Constant selector */
158 break;
159 default:
160 continue;
163 if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
165 /* need to start a new bundle */
167 /* flush previous bundle */
168 if (bundle_count)
170 output( "\t/* %s.%d - %s.%d */\n",
171 spec->dll_name, prev - bundle_count + 1, spec->dll_name, prev );
172 output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
173 output_entries( spec, prev - bundle_count + 1, bundle_count );
176 if (prev + 1 != i)
178 int skip = i - (prev + 1);
179 while (skip > 255)
181 output( "\t.byte 0xff,0x00\n" );
182 skip -= 255;
184 output( "\t.byte 0x%02x,0x00\n", skip );
187 bundle_count = 0;
188 prev_sel = selector;
190 bundle_count++;
191 prev = i;
194 /* flush last bundle */
195 if (bundle_count)
197 output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
198 output_entries( spec, prev - bundle_count + 1, bundle_count );
200 output( "\t.byte 0x00\n" );
204 /*******************************************************************
205 * output_resident_name
207 static void output_resident_name( const char *string, int ordinal )
209 unsigned int i, len = strlen(string);
211 output( "\t.byte 0x%02x", len );
212 for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)toupper(string[i]) );
213 output( " /* %s */\n", string );
214 output( "\t.short %u\n", ordinal );
218 /*******************************************************************
219 * get_callfrom16_name
221 static const char *get_callfrom16_name( const ORDDEF *odp )
223 static char *buffer;
225 free( buffer );
226 buffer = strmake( "%s_%s_%s",
227 (odp->type == TYPE_PASCAL) ? "p" :
228 (odp->type == TYPE_VARARGS) ? "v" : "c",
229 (odp->flags & FLAG_REGISTER) ? "regs" :
230 (odp->flags & FLAG_RET16) ? "word" : "long",
231 get_args_str(odp) );
232 return buffer;
236 /*******************************************************************
237 * get_relay_name
239 static const char *get_relay_name( const ORDDEF *odp )
241 static char buffer[80];
242 char *p;
244 switch(odp->type)
246 case TYPE_PASCAL:
247 strcpy( buffer, "p_" );
248 break;
249 case TYPE_VARARGS:
250 strcpy( buffer, "v_" );
251 break;
252 case TYPE_CDECL:
253 case TYPE_STUB:
254 strcpy( buffer, "c_" );
255 break;
256 default:
257 assert(0);
259 strcat( buffer, get_args_str(odp) );
260 for (p = buffer + 2; *p; p++)
262 /* map string types to the corresponding plain pointer type */
263 if (*p == 't') *p = 'p';
264 else if (*p == 'T') *p = 'l';
266 if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
267 return buffer;
271 /*******************************************************************
272 * get_function_argsize
274 static int get_function_argsize( const ORDDEF *odp )
276 int i, argsize = 0;
278 for (i = 0; i < odp->u.func.nb_args; i++)
280 switch (odp->u.func.args[i])
282 case ARG_WORD:
283 case ARG_SWORD:
284 argsize += 2;
285 break;
286 case ARG_SEGPTR:
287 case ARG_SEGSTR:
288 case ARG_LONG:
289 case ARG_PTR:
290 case ARG_STR:
291 case ARG_WSTR:
292 case ARG_FLOAT:
293 case ARG_INT128:
294 argsize += 4;
295 break;
296 case ARG_INT64:
297 case ARG_DOUBLE:
298 argsize += 8;
299 break;
302 return argsize;
306 /*******************************************************************
307 * output_call16_function
309 * Build a 16-bit-to-Wine callback glue function.
311 * The generated routines are intended to be used as argument conversion
312 * routines to be called by the CallFrom16... core. Thus, the prototypes of
313 * the generated routines are (see also CallFrom16):
315 * extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
316 * extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
317 * extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
319 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
320 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
321 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
322 * 'T'=segmented pointer to null-terminated string).
324 * The generated routines fetch the arguments from the 16-bit stack (pointed
325 * to by 'args'); the offsets of the single argument values are computed
326 * according to the calling convention and the argument types. Then, the
327 * 32-bit entry point is called with these arguments.
329 * For register functions, the arguments (if present) are converted just
330 * the same as for normal functions, but in addition the CONTEXT86 pointer
331 * filled with the current register values is passed to the 32-bit routine.
333 static void output_call16_function( ORDDEF *odp )
335 char *name;
336 int i, pos, stack_words;
337 int argsize = get_function_argsize( odp );
338 int needs_ldt = (strpbrk( get_args_str( odp ), "pt" ) != NULL);
340 name = strmake( ".L__wine_spec_call16_%s", get_relay_name(odp) );
342 output( "\t.align %d\n", get_alignment(4) );
343 output( "\t%s\n", func_declaration(name) );
344 output( "%s:\n", name );
345 output_cfi( ".cfi_startproc" );
346 output( "\tpushl %%ebp\n" );
347 output_cfi( ".cfi_adjust_cfa_offset 4" );
348 output_cfi( ".cfi_rel_offset %%ebp,0" );
349 output( "\tmovl %%esp,%%ebp\n" );
350 output_cfi( ".cfi_def_cfa_register %%ebp" );
351 stack_words = 2;
352 if (needs_ldt)
354 output( "\tpushl %%esi\n" );
355 output_cfi( ".cfi_rel_offset %%esi,-4" );
356 stack_words++;
357 if (UsePIC)
359 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
360 output( "1:\tmovl .Lwine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
361 needs_get_pc_thunk = 1;
363 else
364 output( "\tmovl .Lwine_ldt_copy_ptr,%%esi\n" );
367 /* preserve 16-byte stack alignment */
368 stack_words += odp->u.func.nb_args;
369 for (i = 0; i < odp->u.func.nb_args; i++)
370 if (odp->u.func.args[i] == ARG_DOUBLE || odp->u.func.args[i] == ARG_INT64) stack_words++;
371 if ((odp->flags & FLAG_REGISTER) || (odp->type == TYPE_VARARGS)) stack_words++;
372 if (stack_words % 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words % 4) );
374 if (odp->u.func.nb_args || odp->type == TYPE_VARARGS)
375 output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
377 if (odp->flags & FLAG_REGISTER)
379 output( "\tpushl 16(%%ebp)\n" ); /* context */
381 else if (odp->type == TYPE_VARARGS)
383 output( "\tleal %d(%%ecx),%%eax\n", argsize );
384 output( "\tpushl %%eax\n" ); /* va_list16 */
387 pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
388 for (i = odp->u.func.nb_args - 1; i >= 0; i--)
390 switch (odp->u.func.args[i])
392 case ARG_WORD:
393 if (odp->type != TYPE_PASCAL) pos -= 2;
394 output( "\tmovzwl %d(%%ecx),%%eax\n", pos );
395 output( "\tpushl %%eax\n" );
396 if (odp->type == TYPE_PASCAL) pos += 2;
397 break;
399 case ARG_SWORD:
400 if (odp->type != TYPE_PASCAL) pos -= 2;
401 output( "\tmovswl %d(%%ecx),%%eax\n", pos );
402 output( "\tpushl %%eax\n" );
403 if (odp->type == TYPE_PASCAL) pos += 2;
404 break;
406 case ARG_INT64:
407 case ARG_DOUBLE:
408 if (odp->type != TYPE_PASCAL) pos -= 4;
409 output( "\tpushl %d(%%ecx)\n", pos );
410 if (odp->type == TYPE_PASCAL) pos += 4;
411 /* fall through */
412 case ARG_LONG:
413 case ARG_FLOAT:
414 case ARG_SEGPTR:
415 case ARG_SEGSTR:
416 if (odp->type != TYPE_PASCAL) pos -= 4;
417 output( "\tpushl %d(%%ecx)\n", pos );
418 if (odp->type == TYPE_PASCAL) pos += 4;
419 break;
421 case ARG_PTR:
422 case ARG_STR:
423 case ARG_WSTR:
424 case ARG_INT128:
425 if (odp->type != TYPE_PASCAL) pos -= 4;
426 output( "\tmovzwl %d(%%ecx),%%edx\n", pos + 2 ); /* sel */
427 output( "\tshr $3,%%edx\n" );
428 output( "\tmovzwl %d(%%ecx),%%eax\n", pos ); /* offset */
429 output( "\taddl (%%esi,%%edx,4),%%eax\n" );
430 output( "\tpushl %%eax\n" );
431 if (odp->type == TYPE_PASCAL) pos += 4;
432 break;
436 output( "\tcall *8(%%ebp)\n" );
438 if (needs_ldt)
440 output( "\tmovl -4(%%ebp),%%esi\n" );
441 output_cfi( ".cfi_same_value %%esi" );
443 output( "\tleave\n" );
444 output_cfi( ".cfi_def_cfa %%esp,4" );
445 output_cfi( ".cfi_same_value %%ebp" );
446 output( "\tret\n" );
447 output_cfi( ".cfi_endproc" );
448 output_function_size( name );
449 free( name );
453 /*******************************************************************
454 * callfrom16_type_compare
456 * Compare two callfrom16 sequences.
458 static int callfrom16_type_compare( const void *e1, const void *e2 )
460 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
461 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
462 int retval;
463 int type1 = odp1->type;
464 int type2 = odp2->type;
465 char args1[80];
467 if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
468 if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
470 if ((retval = type1 - type2) != 0) return retval;
472 type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
473 type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
475 if ((retval = type1 - type2) != 0) return retval;
477 strcpy( args1, get_args_str( odp1 ));
478 return strcmp( args1, get_args_str( odp2 ));
482 /*******************************************************************
483 * relay_type_compare
485 * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
487 static int relay_type_compare( const void *e1, const void *e2 )
489 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
490 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
491 char name1[80];
493 strcpy( name1, get_relay_name(odp1) );
494 return strcmp( name1, get_relay_name(odp2) );
498 /*******************************************************************
499 * sort_func_list
501 * Sort a list of functions, removing duplicates.
503 static int sort_func_list( ORDDEF **list, int count,
504 int (*compare)(const void *, const void *) )
506 int i, j;
508 if (!count) return 0;
509 qsort( list, count, sizeof(*list), compare );
511 for (i = j = 0; i < count; i++)
513 if (compare( &list[j], &list[i] )) list[++j] = list[i];
515 return j + 1;
519 /*******************************************************************
520 * output_module16
522 * Output code for a 16-bit module.
524 static void output_module16( DLLSPEC *spec )
526 ORDDEF **typelist;
527 ORDDEF *entry_point = NULL;
528 int i, j, nb_funcs;
530 /* store the main entry point as ordinal 0 */
532 if (!spec->ordinals)
534 assert(spec->limit == 0);
535 spec->ordinals = xmalloc( sizeof(spec->ordinals[0]) );
536 spec->ordinals[0] = NULL;
538 if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
540 entry_point = xmalloc( sizeof(*entry_point) );
541 entry_point->type = TYPE_PASCAL;
542 entry_point->ordinal = 0;
543 entry_point->lineno = 0;
544 entry_point->flags = FLAG_REGISTER;
545 entry_point->name = NULL;
546 entry_point->link_name = xstrdup( spec->init_func );
547 entry_point->export_name = NULL;
548 entry_point->u.func.nb_args = 0;
549 assert( !spec->ordinals[0] );
550 spec->ordinals[0] = entry_point;
553 /* Build sorted list of all argument types, without duplicates */
555 typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) );
557 for (i = nb_funcs = 0; i <= spec->limit; i++)
559 ORDDEF *odp = spec->ordinals[i];
560 if (!odp) continue;
561 if (is_function( odp )) typelist[nb_funcs++] = odp;
564 nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
566 /* Output the module structure */
568 output( "\n/* module data */\n\n" );
569 output( "\t.data\n" );
570 output( "\t.align %d\n", get_alignment(16) );
571 output( ".L__wine_spec_dos_header:\n" );
572 output( "\t.short 0x5a4d\n" ); /* e_magic */
573 output( "\t.short 0\n" ); /* e_cblp */
574 output( "\t.short 0\n" ); /* e_cp */
575 output( "\t.short 0\n" ); /* e_crlc */
576 output( "\t.short 0\n" ); /* e_cparhdr */
577 output( "\t.short 0\n" ); /* e_minalloc */
578 output( "\t.short 0\n" ); /* e_maxalloc */
579 output( "\t.short 0\n" ); /* e_ss */
580 output( "\t.short 0\n" ); /* e_sp */
581 output( "\t.short 0\n" ); /* e_csum */
582 output( "\t.short 0\n" ); /* e_ip */
583 output( "\t.short 0\n" ); /* e_cs */
584 output( "\t.short 0\n" ); /* e_lfarlc */
585 output( "\t.short 0\n" ); /* e_ovno */
586 output( "\t.short 0,0,0,0\n" ); /* e_res */
587 output( "\t.short 0\n" ); /* e_oemid */
588 output( "\t.short 0\n" ); /* e_oeminfo */
589 output( ".Lwine_ldt_copy_ptr:\n" ); /* e_res2, used for private data */
590 output( "\t.long .L__wine_spec_ne_header_end-.L__wine_spec_dos_header,0,0,0,0\n" );
591 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
593 output( "\t%s \"%s\"\n", get_asm_string_keyword(), fakedll_signature );
594 output( "\t.align %d\n", get_alignment(16) );
595 output( ".L__wine_spec_ne_header:\n" );
596 output( "\t.short 0x454e\n" ); /* ne_magic */
597 output( "\t.byte 0\n" ); /* ne_ver */
598 output( "\t.byte 0\n" ); /* ne_rev */
599 output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */
600 output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */
601 output( "\t.long 0\n" ); /* ne_crc */
602 output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA | /* ne_flags */
603 ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
604 output( "\t.short 2\n" ); /* ne_autodata */
605 output( "\t.short %u\n", spec->heap_size ); /* ne_heap */
606 output( "\t.short 0\n" ); /* ne_stack */
607 if (!entry_point) output( "\t.long 0\n" ); /* ne_csip */
608 else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", spec->c_name );
609 output( "\t.short 0,2\n" ); /* ne_sssp */
610 output( "\t.short 2\n" ); /* ne_cseg */
611 output( "\t.short 0\n" ); /* ne_cmod */
612 output( "\t.short 0\n" ); /* ne_cbnrestab */
613 output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */
614 output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */
615 output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */
616 output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */
617 output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */
618 output( "\t.long 0\n" ); /* ne_nrestab */
619 output( "\t.short 0\n" ); /* ne_cmovent */
620 output( "\t.short 0\n" ); /* ne_align */
621 output( "\t.short 0\n" ); /* ne_cres */
622 output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
623 output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
624 output( "\t.short 0\n" ); /* ne_pretthunks */
625 output( "\t.short 0\n" ); /* ne_psegrefbytes */
626 output( "\t.short 0\n" ); /* ne_swaparea */
627 output( "\t.short 0\n" ); /* ne_expver */
629 /* segment table */
631 output( "\n.L__wine_spec_ne_segtab:\n" );
633 /* code segment entry */
635 output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */
636 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */
637 output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */
638 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */
640 /* data segment entry */
642 output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */
643 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */
644 output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */
645 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */
647 /* resource directory */
649 output_res16_directory( spec );
651 /* resident names table */
653 output( "\n\t.align %d\n", get_alignment(2) );
654 output( ".L__wine_spec_ne_restab:\n" );
655 output_resident_name( spec->dll_name, 0 );
656 for (i = 1; i <= spec->limit; i++)
658 ORDDEF *odp = spec->ordinals[i];
659 if (!odp || !odp->name[0]) continue;
660 if (odp->flags & FLAG_EXPORT32) continue;
661 output_resident_name( odp->name, i );
663 output( "\t.byte 0\n" );
665 /* imported names table */
667 output( "\n\t.align %d\n", get_alignment(2) );
668 output( ".L__wine_spec_ne_modtab:\n" );
669 output( ".L__wine_spec_ne_imptab:\n" );
670 output( "\t.byte 0,0\n" );
672 /* entry table */
674 output( "\n.L__wine_spec_ne_enttab:\n" );
675 output_entry_table( spec );
676 output( ".L__wine_spec_ne_enttab_end:\n" );
678 /* code segment */
680 output( "\n\t.align %d\n", get_alignment(2) );
681 output( ".L__wine_spec_code_segment:\n" );
683 for ( i = 0; i < nb_funcs; i++ )
685 unsigned int arg_types[2];
686 int nop_words, pos, argsize = 0;
688 if ( typelist[i]->type == TYPE_PASCAL )
689 argsize = get_function_argsize( typelist[i] );
691 /* build the arg types bit fields */
692 arg_types[0] = arg_types[1] = 0;
693 for (j = pos = 0; j < typelist[i]->u.func.nb_args && pos < 20; j++, pos++)
695 int type = 0;
696 switch (typelist[i]->u.func.args[j])
698 case ARG_WORD: type = ARG16_WORD; break;
699 case ARG_SWORD: type = ARG16_SWORD; break;
700 case ARG_SEGPTR: type = ARG16_LONG; break;
701 case ARG_SEGSTR: type = ARG16_SEGSTR; break;
702 case ARG_LONG: type = ARG16_LONG; break;
703 case ARG_PTR: type = ARG16_PTR; break;
704 case ARG_STR: type = ARG16_STR; break;
705 case ARG_WSTR: type = ARG16_PTR; break;
706 case ARG_FLOAT: type = ARG16_LONG; break;
707 case ARG_INT128: type = ARG16_PTR; break;
708 case ARG_INT64:
709 case ARG_DOUBLE:
710 type = ARG16_LONG;
711 arg_types[pos / 10] |= type << (3 * (pos % 10));
712 pos++;
713 break;
715 if (pos < 20) arg_types[pos / 10] |= type << (3 * (pos % 10));
717 if (typelist[i]->type == TYPE_VARARGS && pos < 20)
718 arg_types[pos / 10] |= ARG16_VARARG << (3 * (pos % 10));
720 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
721 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
722 output( "\tlcall $0,$0\n" );
724 if (typelist[i]->flags & FLAG_REGISTER)
726 nop_words = 4;
728 else if (typelist[i]->flags & FLAG_RET16)
730 output( "\torw %%ax,%%ax\n" );
731 output( "\tnop\n" ); /* so that the lretw is aligned */
732 nop_words = 2;
734 else
736 output( "\tshld $16,%%eax,%%edx\n" );
737 output( "\torl %%eax,%%eax\n" );
738 nop_words = 1;
741 if (argsize)
743 output( "\tlretw $%u\n", argsize );
744 nop_words--;
746 else output( "\tlretw\n" );
748 if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
750 /* the movl is here so that the code contains only valid instructions, */
751 /* it's never actually executed, we only care about the arg_types[] values */
752 output( "\t.short 0x86c7\n" );
753 output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
756 for (i = 0; i <= spec->limit; i++)
758 ORDDEF *odp = spec->ordinals[i];
759 if (!odp || !is_function( odp )) continue;
760 output( ".L__wine_%s_%u:\n", spec->c_name, i );
761 output( "\tpushw %%bp\n" );
762 output( "\tpushl $%s\n",
763 asm_name( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : get_link_name( odp )));
764 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
766 output( ".L__wine_spec_code_segment_end:\n" );
768 /* data segment */
770 output( "\n.L__wine_spec_data_segment:\n" );
771 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
772 for (i = 0; i <= spec->limit; i++)
774 ORDDEF *odp = spec->ordinals[i];
775 if (!odp || odp->type != TYPE_VARIABLE) continue;
776 output( ".L__wine_%s_%u:\n", spec->c_name, i );
777 output( "\t.long " );
778 for (j = 0; j < odp->u.var.n_values-1; j++)
779 output( "0x%08x,", odp->u.var.values[j] );
780 output( "0x%08x\n", odp->u.var.values[j] );
782 output( ".L__wine_spec_data_segment_end:\n" );
784 /* resource data */
786 if (spec->nb_resources)
788 output( "\n.L__wine_spec_resource_data:\n" );
789 output_res16_data( spec );
792 output( ".L__wine_spec_ne_header_end:\n" );
793 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
795 /* relay functions */
797 nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
798 if (nb_funcs)
800 output( "\n/* relay functions */\n\n" );
801 output( "\t.text\n" );
802 for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
805 free( typelist );
809 /*******************************************************************
810 * output_spec16_file
812 * Output the complete data for a spec 16-bit file.
814 void output_spec16_file( DLLSPEC *spec16 )
816 DLLSPEC *spec32 = alloc_dll_spec();
818 add_16bit_exports( spec32, spec16 );
820 needs_get_pc_thunk = 0;
821 open_output_file();
822 output_standard_file_header();
823 output_module( spec32 );
824 output_module16( spec16 );
825 output_stubs( spec16 );
826 output_exports( spec32 );
827 output_imports( spec16 );
828 if (!strcmp( spec16->dll_name, "kernel" )) output_asm_relays16();
829 if (needs_get_pc_thunk) output_get_pc_thunk();
830 if (spec16->main_module)
832 output( "\n\t%s\n", get_asm_string_section() );
833 output( ".L__wine_spec_main_module:\n" );
834 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
836 output_gnu_stack_note();
837 close_output_file();
838 free_dll_spec( spec32 );
841 /*******************************************************************
842 * output_fake_module16
844 * Create a fake 16-bit binary module.
846 void output_fake_module16( DLLSPEC *spec )
848 static const unsigned char code_segment[] = { 0x90, 0xc3 };
849 static const unsigned char data_segment[16] = { 0 };
850 const unsigned int cseg = 2;
851 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
852 const unsigned int segtab = lfanew + 0x40;
854 unsigned int i, rsrctab, restab, namelen, modtab, imptab, enttab, cbenttab, codeseg, dataseg, rsrcdata;
856 init_output_buffer();
858 rsrctab = lfanew;
859 restab = segtab + 8 * cseg;
860 if (spec->nb_resources)
862 output_bin_res16_directory( spec, 0 );
863 align_output( 2 );
864 rsrctab = restab;
865 restab += output_buffer_pos;
866 free( output_buffer );
867 init_output_buffer();
870 namelen = strlen( spec->dll_name );
871 modtab = restab + ((namelen + 3) & ~1);
872 imptab = modtab;
873 enttab = modtab + 2;
874 cbenttab = 1;
875 codeseg = (enttab + cbenttab + 1) & ~1;
876 dataseg = codeseg + sizeof(code_segment);
877 rsrcdata = dataseg + sizeof(data_segment);
879 init_output_buffer();
881 put_word( 0x5a4d ); /* e_magic */
882 put_word( 0x40 ); /* e_cblp */
883 put_word( 0x01 ); /* e_cp */
884 put_word( 0 ); /* e_crlc */
885 put_word( lfanew / 16 ); /* e_cparhdr */
886 put_word( 0x0000 ); /* e_minalloc */
887 put_word( 0xffff ); /* e_maxalloc */
888 put_word( 0x0000 ); /* e_ss */
889 put_word( 0x00b8 ); /* e_sp */
890 put_word( 0 ); /* e_csum */
891 put_word( 0 ); /* e_ip */
892 put_word( 0 ); /* e_cs */
893 put_word( lfanew ); /* e_lfarlc */
894 put_word( 0 ); /* e_ovno */
895 put_dword( 0 ); /* e_res */
896 put_dword( 0 );
897 put_word( 0 ); /* e_oemid */
898 put_word( 0 ); /* e_oeminfo */
899 put_dword( 0 ); /* e_res2 */
900 put_dword( 0 );
901 put_dword( 0 );
902 put_dword( 0 );
903 put_dword( 0 );
904 put_dword( lfanew );
906 put_data( fakedll_signature, sizeof(fakedll_signature) );
907 align_output( 16 );
909 put_word( 0x454e ); /* ne_magic */
910 put_byte( 0 ); /* ne_ver */
911 put_byte( 0 ); /* ne_rev */
912 put_word( enttab - lfanew ); /* ne_enttab */
913 put_word( cbenttab ); /* ne_cbenttab */
914 put_dword( 0 ); /* ne_crc */
915 put_word( NE_FFLAGS_SINGLEDATA | /* ne_flags */
916 ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
917 put_word( 2 ); /* ne_autodata */
918 put_word( spec->heap_size ); /* ne_heap */
919 put_word( 0 ); /* ne_stack */
920 put_word( 0 ); put_word( 0 ); /* ne_csip */
921 put_word( 0 ); put_word( 2 ); /* ne_sssp */
922 put_word( cseg ); /* ne_cseg */
923 put_word( 0 ); /* ne_cmod */
924 put_word( 0 ); /* ne_cbnrestab */
925 put_word( segtab - lfanew ); /* ne_segtab */
926 put_word( rsrctab - lfanew ); /* ne_rsrctab */
927 put_word( restab - lfanew ); /* ne_restab */
928 put_word( modtab - lfanew ); /* ne_modtab */
929 put_word( imptab - lfanew ); /* ne_imptab */
930 put_dword( 0 ); /* ne_nrestab */
931 put_word( 0 ); /* ne_cmovent */
932 put_word( 0 ); /* ne_align */
933 put_word( 0 ); /* ne_cres */
934 put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
935 put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
936 put_word( 0 ); /* ne_pretthunks */
937 put_word( 0 ); /* ne_psegrefbytes */
938 put_word( 0 ); /* ne_swaparea */
939 put_word( 0 ); /* ne_expver */
941 /* segment table */
942 put_word( codeseg );
943 put_word( sizeof(code_segment) );
944 put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
945 put_word( sizeof(code_segment) );
946 put_word( dataseg );
947 put_word( sizeof(data_segment) );
948 put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
949 put_word( sizeof(data_segment) );
951 /* resource directory */
952 if (spec->nb_resources)
954 output_bin_res16_directory( spec, rsrcdata );
955 align_output( 2 );
958 /* resident names table */
959 put_byte( namelen );
960 for (i = 0; i < namelen; i++) put_byte( toupper(spec->dll_name[i]) );
961 put_byte( 0 );
962 align_output( 2 );
964 /* imported names table */
965 put_word( 0 );
967 /* entry table */
968 put_byte( 0 );
969 align_output( 2 );
971 /* code segment */
972 put_data( code_segment, sizeof(code_segment) );
974 /* data segment */
975 put_data( data_segment, sizeof(data_segment) );
977 /* resource data */
978 output_bin_res16_data( spec );
980 flush_output_buffer();