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
32 #define NE_FFLAGS_SINGLEDATA 0x0001
33 #define NE_FFLAGS_LIBMODULE 0x8000
35 /* argument type flags for relay debugging */
38 ARG16_NONE
= 0, /* indicates end of arg list */
39 ARG16_WORD
, /* unsigned word */
40 ARG16_SWORD
, /* signed word */
41 ARG16_LONG
, /* long or segmented pointer */
42 ARG16_PTR
, /* linear pointer */
43 ARG16_STR
, /* linear pointer to null-terminated string */
44 ARG16_SEGSTR
, /* segmented pointer to null-terminated string */
45 ARG16_VARARG
/* start of varargs */
48 /* sequences of nops to fill a certain number of words */
49 static const char * const nop_sequence
[4] =
51 ".byte 0x89,0xf6", /* mov %esi,%esi */
52 ".byte 0x8d,0x74,0x26,0x00", /* lea 0x00(%esi),%esi */
53 ".byte 0x8d,0xb6,0x00,0x00,0x00,0x00", /* lea 0x00000000(%esi),%esi */
54 ".byte 0x8d,0x74,0x26,0x00,0x8d,0x74,0x26,0x00" /* lea 0x00(%esi),%esi; lea 0x00(%esi),%esi */
57 static const char fakedll_signature
[] = "Wine placeholder DLL";
59 static inline int is_function( const ORDDEF
*odp
)
61 if (odp
->flags
& FLAG_EXPORT32
) return 0;
62 return (odp
->type
== TYPE_CDECL
||
63 odp
->type
== TYPE_PASCAL
||
64 odp
->type
== TYPE_VARARGS
||
65 odp
->type
== TYPE_STUB
);
68 static const char *get_args_str( const ORDDEF
*odp
)
70 static char buffer
[MAX_ARGUMENTS
*2+1];
74 for (i
= 0; i
< odp
->u
.func
.nb_args
; i
++)
76 switch (odp
->u
.func
.args
[i
])
78 case ARG_WORD
: strcat( buffer
, "w" ); break;
79 case ARG_SWORD
: strcat( buffer
, "s" ); break;
80 case ARG_SEGSTR
: strcat( buffer
, "T" ); break;
81 case ARG_STR
: strcat( buffer
, "t" ); break;
84 case ARG_SEGPTR
: strcat( buffer
, "l" ); break;
87 case ARG_INT128
: strcat( buffer
, "p" ); break;
89 case ARG_DOUBLE
: strcat( buffer
, "ll" ); break;
95 /*******************************************************************
98 * Output entries for individual symbols in the entry table.
100 static void output_entries( DLLSPEC
*spec
, int first
, int count
)
104 for (i
= 0; i
< count
; i
++)
106 ORDDEF
*odp
= spec
->exports
.ordinals
[first
+ i
];
107 output( "\t.byte 0x03\n" ); /* flags: exported & public data */
114 output( "\t.short .L__wine_%s_%u-.L__wine_spec_code_segment\n", spec
->c_name
, first
+ i
);
117 output( "\t.short .L__wine_%s_%u-.L__wine_spec_data_segment\n", spec
->c_name
, first
+ i
);
120 output( "\t.short 0x%04x /* %s */\n",
121 odp
->u
.abs
.value
, odp
->name
);
130 /*******************************************************************
133 static void output_entry_table( DLLSPEC
*spec
)
135 int i
, prev
= 0, prev_sel
= -1, bundle_count
= 0;
137 for (i
= 1; i
<= spec
->exports
.limit
; i
++)
140 ORDDEF
*odp
= spec
->exports
.ordinals
[i
];
142 if (odp
->flags
& FLAG_EXPORT32
) continue;
150 selector
= 1; /* Code selector */
153 selector
= 2; /* Data selector */
156 selector
= 0xfe; /* Constant selector */
162 if (prev
+ 1 != i
|| prev_sel
!= selector
|| bundle_count
== 255)
164 /* need to start a new bundle */
166 /* flush previous bundle */
169 output( "\t/* %s.%d - %s.%d */\n",
170 spec
->dll_name
, prev
- bundle_count
+ 1, spec
->dll_name
, prev
);
171 output( "\t.byte 0x%02x,0x%02x\n", bundle_count
, prev_sel
);
172 output_entries( spec
, prev
- bundle_count
+ 1, bundle_count
);
177 int skip
= i
- (prev
+ 1);
180 output( "\t.byte 0xff,0x00\n" );
183 output( "\t.byte 0x%02x,0x00\n", skip
);
193 /* flush last bundle */
196 output( "\t.byte 0x%02x,0x%02x\n", bundle_count
, prev_sel
);
197 output_entries( spec
, prev
- bundle_count
+ 1, bundle_count
);
199 output( "\t.byte 0x00\n" );
203 /*******************************************************************
204 * output_resident_name
206 static void output_resident_name( const char *string
, int ordinal
)
208 unsigned int i
, len
= strlen(string
);
210 output( "\t.byte 0x%02x", len
);
211 for (i
= 0; i
< len
; i
++) output( ",0x%02x", (unsigned char)toupper(string
[i
]) );
212 output( " /* %s */\n", string
);
213 output( "\t.short %u\n", ordinal
);
217 /*******************************************************************
218 * get_callfrom16_name
220 static const char *get_callfrom16_name( const ORDDEF
*odp
)
225 buffer
= strmake( "%s_%s_%s",
226 (odp
->type
== TYPE_PASCAL
) ? "p" :
227 (odp
->type
== TYPE_VARARGS
) ? "v" : "c",
228 (odp
->flags
& FLAG_REGISTER
) ? "regs" :
229 (odp
->flags
& FLAG_RET16
) ? "word" : "long",
235 /*******************************************************************
238 static const char *get_relay_name( const ORDDEF
*odp
)
240 static char buffer
[80];
246 strcpy( buffer
, "p_" );
249 strcpy( buffer
, "v_" );
253 strcpy( buffer
, "c_" );
258 strcat( buffer
, get_args_str(odp
) );
259 for (p
= buffer
+ 2; *p
; p
++)
261 /* map string types to the corresponding plain pointer type */
262 if (*p
== 't') *p
= 'p';
263 else if (*p
== 'T') *p
= 'l';
265 if (odp
->flags
& FLAG_REGISTER
) strcat( buffer
, "_regs" );
270 /*******************************************************************
271 * get_function_argsize
273 static int get_function_argsize( const ORDDEF
*odp
)
277 for (i
= 0; i
< odp
->u
.func
.nb_args
; i
++)
279 switch (odp
->u
.func
.args
[i
])
305 /*******************************************************************
306 * output_call16_function
308 * Build a 16-bit-to-Wine callback glue function.
310 * The generated routines are intended to be used as argument conversion
311 * routines to be called by the CallFrom16... core. Thus, the prototypes of
312 * the generated routines are (see also CallFrom16):
314 * extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
315 * extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
316 * extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
318 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
319 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
320 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
321 * 'T'=segmented pointer to null-terminated string).
323 * The generated routines fetch the arguments from the 16-bit stack (pointed
324 * to by 'args'); the offsets of the single argument values are computed
325 * according to the calling convention and the argument types. Then, the
326 * 32-bit entry point is called with these arguments.
328 * For register functions, the arguments (if present) are converted just
329 * the same as for normal functions, but in addition the CONTEXT86 pointer
330 * filled with the current register values is passed to the 32-bit routine.
332 static void output_call16_function( ORDDEF
*odp
)
335 int i
, pos
, stack_words
;
336 int argsize
= get_function_argsize( odp
);
337 int needs_ldt
= (strpbrk( get_args_str( odp
), "pt" ) != NULL
);
339 name
= strmake( ".L__wine_spec_call16_%s", get_relay_name(odp
) );
341 output_function_header( name
, 0 );
342 output_cfi( ".cfi_startproc" );
343 output( "\tpushl %%ebp\n" );
344 output_cfi( ".cfi_adjust_cfa_offset 4" );
345 output_cfi( ".cfi_rel_offset %%ebp,0" );
346 output( "\tmovl %%esp,%%ebp\n" );
347 output_cfi( ".cfi_def_cfa_register %%ebp" );
351 output( "\tpushl %%esi\n" );
352 output_cfi( ".cfi_rel_offset %%esi,-4" );
356 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
357 output( "1:\tmovl .Lwine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
358 needs_get_pc_thunk
= 1;
361 output( "\tmovl .Lwine_ldt_copy_ptr,%%esi\n" );
364 /* preserve 16-byte stack alignment */
365 stack_words
+= odp
->u
.func
.nb_args
;
366 for (i
= 0; i
< odp
->u
.func
.nb_args
; i
++)
367 if (odp
->u
.func
.args
[i
] == ARG_DOUBLE
|| odp
->u
.func
.args
[i
] == ARG_INT64
) stack_words
++;
368 if ((odp
->flags
& FLAG_REGISTER
) || (odp
->type
== TYPE_VARARGS
)) stack_words
++;
369 if (stack_words
% 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words
% 4) );
371 if (odp
->u
.func
.nb_args
|| odp
->type
== TYPE_VARARGS
)
372 output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
374 if (odp
->flags
& FLAG_REGISTER
)
376 output( "\tpushl 16(%%ebp)\n" ); /* context */
378 else if (odp
->type
== TYPE_VARARGS
)
380 output( "\tleal %d(%%ecx),%%eax\n", argsize
);
381 output( "\tpushl %%eax\n" ); /* va_list16 */
384 pos
= (odp
->type
== TYPE_PASCAL
) ? 0 : argsize
;
385 for (i
= odp
->u
.func
.nb_args
- 1; i
>= 0; i
--)
387 switch (odp
->u
.func
.args
[i
])
390 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
391 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
);
392 output( "\tpushl %%eax\n" );
393 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
397 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
398 output( "\tmovswl %d(%%ecx),%%eax\n", pos
);
399 output( "\tpushl %%eax\n" );
400 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
405 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
406 output( "\tpushl %d(%%ecx)\n", pos
);
407 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
413 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
414 output( "\tpushl %d(%%ecx)\n", pos
);
415 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
422 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
423 output( "\tmovzwl %d(%%ecx),%%edx\n", pos
+ 2 ); /* sel */
424 output( "\tshr $3,%%edx\n" );
425 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
); /* offset */
426 output( "\taddl (%%esi,%%edx,4),%%eax\n" );
427 output( "\tpushl %%eax\n" );
428 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
433 output( "\tcall *8(%%ebp)\n" );
437 output( "\tmovl -4(%%ebp),%%esi\n" );
438 output_cfi( ".cfi_same_value %%esi" );
440 output( "\tleave\n" );
441 output_cfi( ".cfi_def_cfa %%esp,4" );
442 output_cfi( ".cfi_same_value %%ebp" );
444 output_cfi( ".cfi_endproc" );
445 output_function_size( name
);
450 /*******************************************************************
451 * callfrom16_type_compare
453 * Compare two callfrom16 sequences.
455 static int callfrom16_type_compare( const void *e1
, const void *e2
)
457 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
458 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
460 int type1
= odp1
->type
;
461 int type2
= odp2
->type
;
464 if (type1
== TYPE_STUB
) type1
= TYPE_CDECL
;
465 if (type2
== TYPE_STUB
) type2
= TYPE_CDECL
;
467 if ((retval
= type1
- type2
) != 0) return retval
;
469 type1
= odp1
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
470 type2
= odp2
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
472 if ((retval
= type1
- type2
) != 0) return retval
;
474 strcpy( args1
, get_args_str( odp1
));
475 return strcmp( args1
, get_args_str( odp2
));
479 /*******************************************************************
482 * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
484 static int relay_type_compare( const void *e1
, const void *e2
)
486 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
487 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
490 strcpy( name1
, get_relay_name(odp1
) );
491 return strcmp( name1
, get_relay_name(odp2
) );
495 /*******************************************************************
498 * Output code for a 16-bit module.
500 static void output_module16( DLLSPEC
*spec
)
502 struct exports
*exports
= &spec
->exports
;
504 ORDDEF
*entry_point
= NULL
;
507 /* store the main entry point as ordinal 0 */
509 if (!exports
->ordinals
)
511 assert(exports
->limit
== 0);
512 exports
->ordinals
= xmalloc( sizeof(exports
->ordinals
[0]) );
513 exports
->ordinals
[0] = NULL
;
515 if (spec
->init_func
&& !(spec
->characteristics
& IMAGE_FILE_DLL
))
517 entry_point
= xmalloc( sizeof(*entry_point
) );
518 entry_point
->type
= TYPE_PASCAL
;
519 entry_point
->ordinal
= 0;
520 entry_point
->lineno
= 0;
521 entry_point
->flags
= FLAG_REGISTER
;
522 entry_point
->name
= NULL
;
523 entry_point
->link_name
= xstrdup( spec
->init_func
);
524 entry_point
->export_name
= NULL
;
525 entry_point
->u
.func
.nb_args
= 0;
526 assert( !exports
->ordinals
[0] );
527 exports
->ordinals
[0] = entry_point
;
530 /* Build sorted list of all argument types, without duplicates */
532 typelist
= xmalloc( (exports
->limit
+ 1) * sizeof(*typelist
) );
534 for (i
= nb_funcs
= 0; i
<= exports
->limit
; i
++)
536 ORDDEF
*odp
= exports
->ordinals
[i
];
538 if (is_function( odp
)) typelist
[nb_funcs
++] = odp
;
541 nb_funcs
= sort_func_list( typelist
, nb_funcs
, callfrom16_type_compare
);
543 /* Output the module structure */
545 output( "\n/* module data */\n\n" );
546 output( "\t.data\n" );
547 output( "\t.balign 16\n" );
548 output( ".L__wine_spec_dos_header:\n" );
549 output( "\t.short 0x5a4d\n" ); /* e_magic */
550 output( "\t.short 0\n" ); /* e_cblp */
551 output( "\t.short 0\n" ); /* e_cp */
552 output( "\t.short 0\n" ); /* e_crlc */
553 output( "\t.short 0\n" ); /* e_cparhdr */
554 output( "\t.short 0\n" ); /* e_minalloc */
555 output( "\t.short 0\n" ); /* e_maxalloc */
556 output( "\t.short 0\n" ); /* e_ss */
557 output( "\t.short 0\n" ); /* e_sp */
558 output( "\t.short 0\n" ); /* e_csum */
559 output( "\t.short 0\n" ); /* e_ip */
560 output( "\t.short 0\n" ); /* e_cs */
561 output( "\t.short 0\n" ); /* e_lfarlc */
562 output( "\t.short 0\n" ); /* e_ovno */
563 output( "\t.short 0,0,0,0\n" ); /* e_res */
564 output( "\t.short 0\n" ); /* e_oemid */
565 output( "\t.short 0\n" ); /* e_oeminfo */
566 output( ".Lwine_ldt_copy_ptr:\n" ); /* e_res2, used for private data */
567 output( "\t.long .L__wine_spec_ne_header_end-.L__wine_spec_dos_header,0,0,0,0\n" );
568 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
570 output( "\t%s \"%s\"\n", get_asm_string_keyword(), fakedll_signature
);
571 output( "\t.balign 16\n" );
572 output( ".L__wine_spec_ne_header:\n" );
573 output( "\t.short 0x454e\n" ); /* ne_magic */
574 output( "\t.byte 0\n" ); /* ne_ver */
575 output( "\t.byte 0\n" ); /* ne_rev */
576 output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */
577 output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */
578 output( "\t.long 0\n" ); /* ne_crc */
579 output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA
| /* ne_flags */
580 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
581 output( "\t.short 2\n" ); /* ne_autodata */
582 output( "\t.short %u\n", spec
->heap_size
); /* ne_heap */
583 output( "\t.short 0\n" ); /* ne_stack */
584 if (!entry_point
) output( "\t.long 0\n" ); /* ne_csip */
585 else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", spec
->c_name
);
586 output( "\t.short 0,2\n" ); /* ne_sssp */
587 output( "\t.short 2\n" ); /* ne_cseg */
588 output( "\t.short 0\n" ); /* ne_cmod */
589 output( "\t.short 0\n" ); /* ne_cbnrestab */
590 output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */
591 output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */
592 output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */
593 output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */
594 output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */
595 output( "\t.long 0\n" ); /* ne_nrestab */
596 output( "\t.short 0\n" ); /* ne_cmovent */
597 output( "\t.short 0\n" ); /* ne_align */
598 output( "\t.short 0\n" ); /* ne_cres */
599 output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
600 output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
601 output( "\t.short 0\n" ); /* ne_pretthunks */
602 output( "\t.short 0\n" ); /* ne_psegrefbytes */
603 output( "\t.short 0\n" ); /* ne_swaparea */
604 output( "\t.short 0\n" ); /* ne_expver */
608 output( "\n.L__wine_spec_ne_segtab:\n" );
610 /* code segment entry */
612 output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */
613 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */
614 output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */
615 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */
617 /* data segment entry */
619 output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */
620 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */
621 output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */
622 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */
624 /* resource directory */
626 output_res16_directory( spec
);
628 /* resident names table */
630 output( "\n\t.balign 2\n" );
631 output( ".L__wine_spec_ne_restab:\n" );
632 output_resident_name( spec
->dll_name
, 0 );
633 for (i
= 1; i
<= exports
->limit
; i
++)
635 ORDDEF
*odp
= exports
->ordinals
[i
];
636 if (!odp
|| !odp
->name
[0]) continue;
637 if (odp
->flags
& FLAG_EXPORT32
) continue;
638 output_resident_name( odp
->name
, i
);
640 output( "\t.byte 0\n" );
642 /* imported names table */
644 output( "\n\t.balign 2\n" );
645 output( ".L__wine_spec_ne_modtab:\n" );
646 output( ".L__wine_spec_ne_imptab:\n" );
647 output( "\t.byte 0,0\n" );
651 output( "\n.L__wine_spec_ne_enttab:\n" );
652 output_entry_table( spec
);
653 output( ".L__wine_spec_ne_enttab_end:\n" );
657 output( "\n\t.balign 2\n" );
658 output( ".L__wine_spec_code_segment:\n" );
660 for ( i
= 0; i
< nb_funcs
; i
++ )
662 unsigned int arg_types
[2];
663 int nop_words
, pos
, argsize
= 0;
665 if ( typelist
[i
]->type
== TYPE_PASCAL
)
666 argsize
= get_function_argsize( typelist
[i
] );
668 /* build the arg types bit fields */
669 arg_types
[0] = arg_types
[1] = 0;
670 for (j
= pos
= 0; j
< typelist
[i
]->u
.func
.nb_args
&& pos
< 20; j
++, pos
++)
673 switch (typelist
[i
]->u
.func
.args
[j
])
675 case ARG_WORD
: type
= ARG16_WORD
; break;
676 case ARG_SWORD
: type
= ARG16_SWORD
; break;
677 case ARG_SEGPTR
: type
= ARG16_LONG
; break;
678 case ARG_SEGSTR
: type
= ARG16_SEGSTR
; break;
679 case ARG_LONG
: type
= ARG16_LONG
; break;
680 case ARG_PTR
: type
= ARG16_PTR
; break;
681 case ARG_STR
: type
= ARG16_STR
; break;
682 case ARG_WSTR
: type
= ARG16_PTR
; break;
683 case ARG_FLOAT
: type
= ARG16_LONG
; break;
684 case ARG_INT128
: type
= ARG16_PTR
; break;
688 arg_types
[pos
/ 10] |= type
<< (3 * (pos
% 10));
692 if (pos
< 20) arg_types
[pos
/ 10] |= type
<< (3 * (pos
% 10));
694 if (typelist
[i
]->type
== TYPE_VARARGS
&& pos
< 20)
695 arg_types
[pos
/ 10] |= ARG16_VARARG
<< (3 * (pos
% 10));
697 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist
[i
]) );
698 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist
[i
]) );
699 output( "\tlcall $0,$0\n" );
701 if (typelist
[i
]->flags
& FLAG_REGISTER
)
705 else if (typelist
[i
]->flags
& FLAG_RET16
)
707 output( "\torw %%ax,%%ax\n" );
708 output( "\tnop\n" ); /* so that the lretw is aligned */
713 output( "\tshld $16,%%eax,%%edx\n" );
714 output( "\torl %%eax,%%eax\n" );
720 output( "\tlretw $%u\n", argsize
);
723 else output( "\tlretw\n" );
725 if (nop_words
) output( "\t%s\n", nop_sequence
[nop_words
-1] );
727 /* the movl is here so that the code contains only valid instructions, */
728 /* it's never actually executed, we only care about the arg_types[] values */
729 output( "\t.short 0x86c7\n" );
730 output( "\t.long 0x%08x,0x%08x\n", arg_types
[0], arg_types
[1] );
733 for (i
= 0; i
<= exports
->limit
; i
++)
735 ORDDEF
*odp
= exports
->ordinals
[i
];
736 if (!odp
|| !is_function( odp
)) continue;
737 output( ".L__wine_%s_%u:\n", spec
->c_name
, i
);
738 output( "\tpushw %%bp\n" );
739 output( "\tpushl $%s\n",
740 asm_name( odp
->type
== TYPE_STUB
? get_stub_name( odp
, spec
) : get_link_name( odp
)));
741 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp
) );
743 output( ".L__wine_spec_code_segment_end:\n" );
747 output( "\n.L__wine_spec_data_segment:\n" );
748 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
749 for (i
= 0; i
<= exports
->limit
; i
++)
751 ORDDEF
*odp
= exports
->ordinals
[i
];
752 if (!odp
|| odp
->type
!= TYPE_VARIABLE
) continue;
753 output( ".L__wine_%s_%u:\n", spec
->c_name
, i
);
754 output( "\t.long " );
755 for (j
= 0; j
< odp
->u
.var
.n_values
-1; j
++)
756 output( "0x%08x,", odp
->u
.var
.values
[j
] );
757 output( "0x%08x\n", odp
->u
.var
.values
[j
] );
759 output( ".L__wine_spec_data_segment_end:\n" );
763 if (spec
->nb_resources
)
765 output( "\n.L__wine_spec_resource_data:\n" );
766 output_res16_data( spec
);
769 output( ".L__wine_spec_ne_header_end:\n" );
770 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
772 /* relay functions */
774 nb_funcs
= sort_func_list( typelist
, nb_funcs
, relay_type_compare
);
777 output( "\n/* relay functions */\n\n" );
778 output( "\t.text\n" );
779 for ( i
= 0; i
< nb_funcs
; i
++ ) output_call16_function( typelist
[i
] );
786 /*******************************************************************
789 * Output the complete data for a spec 16-bit file.
791 void output_spec16_file( DLLSPEC
*spec16
)
793 DLLSPEC
*spec32
= alloc_dll_spec();
795 add_16bit_exports( spec32
, spec16
);
797 needs_get_pc_thunk
= 0;
799 output_standard_file_header();
800 output_module( spec32
);
801 output_module16( spec16
);
802 output_stubs( spec16
);
803 output_exports( spec32
);
804 output_imports( spec16
);
805 if (!strcmp( spec16
->dll_name
, "kernel" )) output_asm_relays16();
806 if (needs_get_pc_thunk
) output_get_pc_thunk();
807 if (spec16
->main_module
)
809 output( "\n\t%s\n", get_asm_string_section() );
810 output( ".L__wine_spec_main_module:\n" );
811 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16
->main_module
);
813 output_gnu_stack_note();
815 free_dll_spec( spec32
);
818 /*******************************************************************
819 * output_fake_module16
821 * Create a fake 16-bit binary module.
823 void output_fake_module16( DLLSPEC
*spec
)
825 static const unsigned char code_segment
[] = { 0x90, 0xc3 };
826 static const unsigned char data_segment
[16] = { 0 };
827 const unsigned int cseg
= 2;
828 const unsigned int lfanew
= (0x40 + sizeof(fakedll_signature
) + 15) & ~15;
829 const unsigned int segtab
= lfanew
+ 0x40;
831 unsigned int i
, rsrctab
, restab
, namelen
, modtab
, imptab
, enttab
, cbenttab
, codeseg
, dataseg
, rsrcdata
, rsrc_size
= 0;
832 void *rsrc_ptr
= NULL
;
834 init_output_buffer();
837 restab
= segtab
+ 8 * cseg
;
838 if (spec
->nb_resources
)
840 output_bin_res16_directory( spec
, 0 );
843 restab
+= output_buffer_pos
;
844 free( output_buffer
);
845 init_output_buffer();
846 output_bin_res16_data( spec
);
847 rsrc_ptr
= output_buffer
;
848 rsrc_size
= output_buffer_pos
;
849 init_output_buffer();
852 namelen
= strlen( spec
->dll_name
);
853 modtab
= restab
+ ((namelen
+ 3) & ~1);
857 codeseg
= (enttab
+ cbenttab
+ 1) & ~1;
858 dataseg
= codeseg
+ sizeof(code_segment
);
859 rsrcdata
= dataseg
+ sizeof(data_segment
);
861 init_output_buffer();
863 put_word( 0x5a4d ); /* e_magic */
864 put_word( 0x40 ); /* e_cblp */
865 put_word( 0x01 ); /* e_cp */
866 put_word( 0 ); /* e_crlc */
867 put_word( lfanew
/ 16 ); /* e_cparhdr */
868 put_word( 0x0000 ); /* e_minalloc */
869 put_word( 0xffff ); /* e_maxalloc */
870 put_word( 0x0000 ); /* e_ss */
871 put_word( 0x00b8 ); /* e_sp */
872 put_word( 0 ); /* e_csum */
873 put_word( 0 ); /* e_ip */
874 put_word( 0 ); /* e_cs */
875 put_word( lfanew
); /* e_lfarlc */
876 put_word( 0 ); /* e_ovno */
877 put_dword( 0 ); /* e_res */
879 put_word( 0 ); /* e_oemid */
880 put_word( 0 ); /* e_oeminfo */
881 put_dword( rsrcdata
+ rsrc_size
); /* e_res2 */
888 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
891 put_word( 0x454e ); /* ne_magic */
892 put_byte( 0 ); /* ne_ver */
893 put_byte( 0 ); /* ne_rev */
894 put_word( enttab
- lfanew
); /* ne_enttab */
895 put_word( cbenttab
); /* ne_cbenttab */
896 put_dword( 0 ); /* ne_crc */
897 put_word( NE_FFLAGS_SINGLEDATA
| /* ne_flags */
898 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
899 put_word( 2 ); /* ne_autodata */
900 put_word( spec
->heap_size
); /* ne_heap */
901 put_word( 0 ); /* ne_stack */
902 put_word( 0 ); put_word( 0 ); /* ne_csip */
903 put_word( 0 ); put_word( 2 ); /* ne_sssp */
904 put_word( cseg
); /* ne_cseg */
905 put_word( 0 ); /* ne_cmod */
906 put_word( 0 ); /* ne_cbnrestab */
907 put_word( segtab
- lfanew
); /* ne_segtab */
908 put_word( rsrctab
- lfanew
); /* ne_rsrctab */
909 put_word( restab
- lfanew
); /* ne_restab */
910 put_word( modtab
- lfanew
); /* ne_modtab */
911 put_word( imptab
- lfanew
); /* ne_imptab */
912 put_dword( 0 ); /* ne_nrestab */
913 put_word( 0 ); /* ne_cmovent */
914 put_word( 0 ); /* ne_align */
915 put_word( 0 ); /* ne_cres */
916 put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
917 put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
918 put_word( 0 ); /* ne_pretthunks */
919 put_word( 0 ); /* ne_psegrefbytes */
920 put_word( 0 ); /* ne_swaparea */
921 put_word( 0 ); /* ne_expver */
925 put_word( sizeof(code_segment
) );
926 put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
927 put_word( sizeof(code_segment
) );
929 put_word( sizeof(data_segment
) );
930 put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
931 put_word( sizeof(data_segment
) );
933 /* resource directory */
934 if (spec
->nb_resources
)
936 output_bin_res16_directory( spec
, rsrcdata
);
940 /* resident names table */
942 for (i
= 0; i
< namelen
; i
++) put_byte( toupper(spec
->dll_name
[i
]) );
946 /* imported names table */
954 put_data( code_segment
, sizeof(code_segment
) );
957 put_data( data_segment
, sizeof(data_segment
) );
960 put_data( rsrc_ptr
, rsrc_size
);