ws2_32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / tools / winebuild / spec16.c
blobdaa9c63a8c919904299edcf2a97b68d7eabbc58e
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 * output_module16
501 * Output code for a 16-bit module.
503 static void output_module16( DLLSPEC *spec )
505 ORDDEF **typelist;
506 ORDDEF *entry_point = NULL;
507 int i, j, nb_funcs;
509 /* store the main entry point as ordinal 0 */
511 if (!spec->ordinals)
513 assert(spec->limit == 0);
514 spec->ordinals = xmalloc( sizeof(spec->ordinals[0]) );
515 spec->ordinals[0] = NULL;
517 if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
519 entry_point = xmalloc( sizeof(*entry_point) );
520 entry_point->type = TYPE_PASCAL;
521 entry_point->ordinal = 0;
522 entry_point->lineno = 0;
523 entry_point->flags = FLAG_REGISTER;
524 entry_point->name = NULL;
525 entry_point->link_name = xstrdup( spec->init_func );
526 entry_point->export_name = NULL;
527 entry_point->u.func.nb_args = 0;
528 assert( !spec->ordinals[0] );
529 spec->ordinals[0] = entry_point;
532 /* Build sorted list of all argument types, without duplicates */
534 typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) );
536 for (i = nb_funcs = 0; i <= spec->limit; i++)
538 ORDDEF *odp = spec->ordinals[i];
539 if (!odp) continue;
540 if (is_function( odp )) typelist[nb_funcs++] = odp;
543 nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
545 /* Output the module structure */
547 output( "\n/* module data */\n\n" );
548 output( "\t.data\n" );
549 output( "\t.align %d\n", get_alignment(16) );
550 output( ".L__wine_spec_dos_header:\n" );
551 output( "\t.short 0x5a4d\n" ); /* e_magic */
552 output( "\t.short 0\n" ); /* e_cblp */
553 output( "\t.short 0\n" ); /* e_cp */
554 output( "\t.short 0\n" ); /* e_crlc */
555 output( "\t.short 0\n" ); /* e_cparhdr */
556 output( "\t.short 0\n" ); /* e_minalloc */
557 output( "\t.short 0\n" ); /* e_maxalloc */
558 output( "\t.short 0\n" ); /* e_ss */
559 output( "\t.short 0\n" ); /* e_sp */
560 output( "\t.short 0\n" ); /* e_csum */
561 output( "\t.short 0\n" ); /* e_ip */
562 output( "\t.short 0\n" ); /* e_cs */
563 output( "\t.short 0\n" ); /* e_lfarlc */
564 output( "\t.short 0\n" ); /* e_ovno */
565 output( "\t.short 0,0,0,0\n" ); /* e_res */
566 output( "\t.short 0\n" ); /* e_oemid */
567 output( "\t.short 0\n" ); /* e_oeminfo */
568 output( ".Lwine_ldt_copy_ptr:\n" ); /* e_res2, used for private data */
569 output( "\t.long .L__wine_spec_ne_header_end-.L__wine_spec_dos_header,0,0,0,0\n" );
570 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
572 output( "\t%s \"%s\"\n", get_asm_string_keyword(), fakedll_signature );
573 output( "\t.align %d\n", get_alignment(16) );
574 output( ".L__wine_spec_ne_header:\n" );
575 output( "\t.short 0x454e\n" ); /* ne_magic */
576 output( "\t.byte 0\n" ); /* ne_ver */
577 output( "\t.byte 0\n" ); /* ne_rev */
578 output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */
579 output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */
580 output( "\t.long 0\n" ); /* ne_crc */
581 output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA | /* ne_flags */
582 ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
583 output( "\t.short 2\n" ); /* ne_autodata */
584 output( "\t.short %u\n", spec->heap_size ); /* ne_heap */
585 output( "\t.short 0\n" ); /* ne_stack */
586 if (!entry_point) output( "\t.long 0\n" ); /* ne_csip */
587 else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", spec->c_name );
588 output( "\t.short 0,2\n" ); /* ne_sssp */
589 output( "\t.short 2\n" ); /* ne_cseg */
590 output( "\t.short 0\n" ); /* ne_cmod */
591 output( "\t.short 0\n" ); /* ne_cbnrestab */
592 output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */
593 output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */
594 output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */
595 output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */
596 output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */
597 output( "\t.long 0\n" ); /* ne_nrestab */
598 output( "\t.short 0\n" ); /* ne_cmovent */
599 output( "\t.short 0\n" ); /* ne_align */
600 output( "\t.short 0\n" ); /* ne_cres */
601 output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
602 output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
603 output( "\t.short 0\n" ); /* ne_pretthunks */
604 output( "\t.short 0\n" ); /* ne_psegrefbytes */
605 output( "\t.short 0\n" ); /* ne_swaparea */
606 output( "\t.short 0\n" ); /* ne_expver */
608 /* segment table */
610 output( "\n.L__wine_spec_ne_segtab:\n" );
612 /* code segment entry */
614 output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */
615 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */
616 output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */
617 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */
619 /* data segment entry */
621 output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */
622 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */
623 output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */
624 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */
626 /* resource directory */
628 output_res16_directory( spec );
630 /* resident names table */
632 output( "\n\t.align %d\n", get_alignment(2) );
633 output( ".L__wine_spec_ne_restab:\n" );
634 output_resident_name( spec->dll_name, 0 );
635 for (i = 1; i <= spec->limit; i++)
637 ORDDEF *odp = spec->ordinals[i];
638 if (!odp || !odp->name[0]) continue;
639 if (odp->flags & FLAG_EXPORT32) continue;
640 output_resident_name( odp->name, i );
642 output( "\t.byte 0\n" );
644 /* imported names table */
646 output( "\n\t.align %d\n", get_alignment(2) );
647 output( ".L__wine_spec_ne_modtab:\n" );
648 output( ".L__wine_spec_ne_imptab:\n" );
649 output( "\t.byte 0,0\n" );
651 /* entry table */
653 output( "\n.L__wine_spec_ne_enttab:\n" );
654 output_entry_table( spec );
655 output( ".L__wine_spec_ne_enttab_end:\n" );
657 /* code segment */
659 output( "\n\t.align %d\n", get_alignment(2) );
660 output( ".L__wine_spec_code_segment:\n" );
662 for ( i = 0; i < nb_funcs; i++ )
664 unsigned int arg_types[2];
665 int nop_words, pos, argsize = 0;
667 if ( typelist[i]->type == TYPE_PASCAL )
668 argsize = get_function_argsize( typelist[i] );
670 /* build the arg types bit fields */
671 arg_types[0] = arg_types[1] = 0;
672 for (j = pos = 0; j < typelist[i]->u.func.nb_args && pos < 20; j++, pos++)
674 int type = 0;
675 switch (typelist[i]->u.func.args[j])
677 case ARG_WORD: type = ARG16_WORD; break;
678 case ARG_SWORD: type = ARG16_SWORD; break;
679 case ARG_SEGPTR: type = ARG16_LONG; break;
680 case ARG_SEGSTR: type = ARG16_SEGSTR; break;
681 case ARG_LONG: type = ARG16_LONG; break;
682 case ARG_PTR: type = ARG16_PTR; break;
683 case ARG_STR: type = ARG16_STR; break;
684 case ARG_WSTR: type = ARG16_PTR; break;
685 case ARG_FLOAT: type = ARG16_LONG; break;
686 case ARG_INT128: type = ARG16_PTR; break;
687 case ARG_INT64:
688 case ARG_DOUBLE:
689 type = ARG16_LONG;
690 arg_types[pos / 10] |= type << (3 * (pos % 10));
691 pos++;
692 break;
694 if (pos < 20) arg_types[pos / 10] |= type << (3 * (pos % 10));
696 if (typelist[i]->type == TYPE_VARARGS && pos < 20)
697 arg_types[pos / 10] |= ARG16_VARARG << (3 * (pos % 10));
699 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
700 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
701 output( "\tlcall $0,$0\n" );
703 if (typelist[i]->flags & FLAG_REGISTER)
705 nop_words = 4;
707 else if (typelist[i]->flags & FLAG_RET16)
709 output( "\torw %%ax,%%ax\n" );
710 output( "\tnop\n" ); /* so that the lretw is aligned */
711 nop_words = 2;
713 else
715 output( "\tshld $16,%%eax,%%edx\n" );
716 output( "\torl %%eax,%%eax\n" );
717 nop_words = 1;
720 if (argsize)
722 output( "\tlretw $%u\n", argsize );
723 nop_words--;
725 else output( "\tlretw\n" );
727 if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
729 /* the movl is here so that the code contains only valid instructions, */
730 /* it's never actually executed, we only care about the arg_types[] values */
731 output( "\t.short 0x86c7\n" );
732 output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
735 for (i = 0; i <= spec->limit; i++)
737 ORDDEF *odp = spec->ordinals[i];
738 if (!odp || !is_function( odp )) continue;
739 output( ".L__wine_%s_%u:\n", spec->c_name, i );
740 output( "\tpushw %%bp\n" );
741 output( "\tpushl $%s\n",
742 asm_name( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : get_link_name( odp )));
743 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
745 output( ".L__wine_spec_code_segment_end:\n" );
747 /* data segment */
749 output( "\n.L__wine_spec_data_segment:\n" );
750 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
751 for (i = 0; i <= spec->limit; i++)
753 ORDDEF *odp = spec->ordinals[i];
754 if (!odp || odp->type != TYPE_VARIABLE) continue;
755 output( ".L__wine_%s_%u:\n", spec->c_name, i );
756 output( "\t.long " );
757 for (j = 0; j < odp->u.var.n_values-1; j++)
758 output( "0x%08x,", odp->u.var.values[j] );
759 output( "0x%08x\n", odp->u.var.values[j] );
761 output( ".L__wine_spec_data_segment_end:\n" );
763 /* resource data */
765 if (spec->nb_resources)
767 output( "\n.L__wine_spec_resource_data:\n" );
768 output_res16_data( spec );
771 output( ".L__wine_spec_ne_header_end:\n" );
772 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
774 /* relay functions */
776 nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
777 if (nb_funcs)
779 output( "\n/* relay functions */\n\n" );
780 output( "\t.text\n" );
781 for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
784 free( typelist );
788 /*******************************************************************
789 * output_spec16_file
791 * Output the complete data for a spec 16-bit file.
793 void output_spec16_file( DLLSPEC *spec16 )
795 DLLSPEC *spec32 = alloc_dll_spec();
797 add_16bit_exports( spec32, spec16 );
799 needs_get_pc_thunk = 0;
800 open_output_file();
801 output_standard_file_header();
802 output_module( spec32 );
803 output_module16( spec16 );
804 output_stubs( spec16 );
805 output_exports( spec32 );
806 output_imports( spec16 );
807 if (!strcmp( spec16->dll_name, "kernel" )) output_asm_relays16();
808 if (needs_get_pc_thunk) output_get_pc_thunk();
809 if (spec16->main_module)
811 output( "\n\t%s\n", get_asm_string_section() );
812 output( ".L__wine_spec_main_module:\n" );
813 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
815 output_gnu_stack_note();
816 close_output_file();
817 free_dll_spec( spec32 );
820 /*******************************************************************
821 * output_fake_module16
823 * Create a fake 16-bit binary module.
825 void output_fake_module16( DLLSPEC *spec )
827 static const unsigned char code_segment[] = { 0x90, 0xc3 };
828 static const unsigned char data_segment[16] = { 0 };
829 const unsigned int cseg = 2;
830 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
831 const unsigned int segtab = lfanew + 0x40;
833 unsigned int i, rsrctab, restab, namelen, modtab, imptab, enttab, cbenttab, codeseg, dataseg, rsrcdata;
835 init_output_buffer();
837 rsrctab = lfanew;
838 restab = segtab + 8 * cseg;
839 if (spec->nb_resources)
841 output_bin_res16_directory( spec, 0 );
842 align_output( 2 );
843 rsrctab = restab;
844 restab += output_buffer_pos;
845 free( output_buffer );
846 init_output_buffer();
849 namelen = strlen( spec->dll_name );
850 modtab = restab + ((namelen + 3) & ~1);
851 imptab = modtab;
852 enttab = modtab + 2;
853 cbenttab = 1;
854 codeseg = (enttab + cbenttab + 1) & ~1;
855 dataseg = codeseg + sizeof(code_segment);
856 rsrcdata = dataseg + sizeof(data_segment);
858 init_output_buffer();
860 put_word( 0x5a4d ); /* e_magic */
861 put_word( 0x40 ); /* e_cblp */
862 put_word( 0x01 ); /* e_cp */
863 put_word( 0 ); /* e_crlc */
864 put_word( lfanew / 16 ); /* e_cparhdr */
865 put_word( 0x0000 ); /* e_minalloc */
866 put_word( 0xffff ); /* e_maxalloc */
867 put_word( 0x0000 ); /* e_ss */
868 put_word( 0x00b8 ); /* e_sp */
869 put_word( 0 ); /* e_csum */
870 put_word( 0 ); /* e_ip */
871 put_word( 0 ); /* e_cs */
872 put_word( lfanew ); /* e_lfarlc */
873 put_word( 0 ); /* e_ovno */
874 put_dword( 0 ); /* e_res */
875 put_dword( 0 );
876 put_word( 0 ); /* e_oemid */
877 put_word( 0 ); /* e_oeminfo */
878 put_dword( 0 ); /* e_res2 */
879 put_dword( 0 );
880 put_dword( 0 );
881 put_dword( 0 );
882 put_dword( 0 );
883 put_dword( lfanew );
885 put_data( fakedll_signature, sizeof(fakedll_signature) );
886 align_output( 16 );
888 put_word( 0x454e ); /* ne_magic */
889 put_byte( 0 ); /* ne_ver */
890 put_byte( 0 ); /* ne_rev */
891 put_word( enttab - lfanew ); /* ne_enttab */
892 put_word( cbenttab ); /* ne_cbenttab */
893 put_dword( 0 ); /* ne_crc */
894 put_word( NE_FFLAGS_SINGLEDATA | /* ne_flags */
895 ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
896 put_word( 2 ); /* ne_autodata */
897 put_word( spec->heap_size ); /* ne_heap */
898 put_word( 0 ); /* ne_stack */
899 put_word( 0 ); put_word( 0 ); /* ne_csip */
900 put_word( 0 ); put_word( 2 ); /* ne_sssp */
901 put_word( cseg ); /* ne_cseg */
902 put_word( 0 ); /* ne_cmod */
903 put_word( 0 ); /* ne_cbnrestab */
904 put_word( segtab - lfanew ); /* ne_segtab */
905 put_word( rsrctab - lfanew ); /* ne_rsrctab */
906 put_word( restab - lfanew ); /* ne_restab */
907 put_word( modtab - lfanew ); /* ne_modtab */
908 put_word( imptab - lfanew ); /* ne_imptab */
909 put_dword( 0 ); /* ne_nrestab */
910 put_word( 0 ); /* ne_cmovent */
911 put_word( 0 ); /* ne_align */
912 put_word( 0 ); /* ne_cres */
913 put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
914 put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
915 put_word( 0 ); /* ne_pretthunks */
916 put_word( 0 ); /* ne_psegrefbytes */
917 put_word( 0 ); /* ne_swaparea */
918 put_word( 0 ); /* ne_expver */
920 /* segment table */
921 put_word( codeseg );
922 put_word( sizeof(code_segment) );
923 put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
924 put_word( sizeof(code_segment) );
925 put_word( dataseg );
926 put_word( sizeof(data_segment) );
927 put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
928 put_word( sizeof(data_segment) );
930 /* resource directory */
931 if (spec->nb_resources)
933 output_bin_res16_directory( spec, rsrcdata );
934 align_output( 2 );
937 /* resident names table */
938 put_byte( namelen );
939 for (i = 0; i < namelen; i++) put_byte( toupper(spec->dll_name[i]) );
940 put_byte( 0 );
941 align_output( 2 );
943 /* imported names table */
944 put_word( 0 );
946 /* entry table */
947 put_byte( 0 );
948 align_output( 2 );
950 /* code segment */
951 put_data( code_segment, sizeof(code_segment) );
953 /* data segment */
954 put_data( data_segment, sizeof(data_segment) );
956 /* resource data */
957 output_bin_res16_data( spec );
959 flush_output_buffer();