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
->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
->limit
; i
++)
140 ORDDEF
*odp
= spec
->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( "\t.align %d\n", get_alignment(4) );
342 output( "\t%s\n", func_declaration(name
) );
343 output( "%s:\n", name
);
344 output_cfi( ".cfi_startproc" );
345 output( "\tpushl %%ebp\n" );
346 output_cfi( ".cfi_adjust_cfa_offset 4" );
347 output_cfi( ".cfi_rel_offset %%ebp,0" );
348 output( "\tmovl %%esp,%%ebp\n" );
349 output_cfi( ".cfi_def_cfa_register %%ebp" );
353 output( "\tpushl %%esi\n" );
354 output_cfi( ".cfi_rel_offset %%esi,-4" );
358 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
359 output( "1:\tmovl .Lwine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
360 needs_get_pc_thunk
= 1;
363 output( "\tmovl .Lwine_ldt_copy_ptr,%%esi\n" );
366 /* preserve 16-byte stack alignment */
367 stack_words
+= odp
->u
.func
.nb_args
;
368 for (i
= 0; i
< odp
->u
.func
.nb_args
; i
++)
369 if (odp
->u
.func
.args
[i
] == ARG_DOUBLE
|| odp
->u
.func
.args
[i
] == ARG_INT64
) stack_words
++;
370 if ((odp
->flags
& FLAG_REGISTER
) || (odp
->type
== TYPE_VARARGS
)) stack_words
++;
371 if (stack_words
% 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words
% 4) );
373 if (odp
->u
.func
.nb_args
|| odp
->type
== TYPE_VARARGS
)
374 output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
376 if (odp
->flags
& FLAG_REGISTER
)
378 output( "\tpushl 16(%%ebp)\n" ); /* context */
380 else if (odp
->type
== TYPE_VARARGS
)
382 output( "\tleal %d(%%ecx),%%eax\n", argsize
);
383 output( "\tpushl %%eax\n" ); /* va_list16 */
386 pos
= (odp
->type
== TYPE_PASCAL
) ? 0 : argsize
;
387 for (i
= odp
->u
.func
.nb_args
- 1; i
>= 0; i
--)
389 switch (odp
->u
.func
.args
[i
])
392 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
393 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
);
394 output( "\tpushl %%eax\n" );
395 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
399 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
400 output( "\tmovswl %d(%%ecx),%%eax\n", pos
);
401 output( "\tpushl %%eax\n" );
402 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
407 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
408 output( "\tpushl %d(%%ecx)\n", pos
);
409 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
415 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
416 output( "\tpushl %d(%%ecx)\n", pos
);
417 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
424 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
425 output( "\tmovzwl %d(%%ecx),%%edx\n", pos
+ 2 ); /* sel */
426 output( "\tshr $3,%%edx\n" );
427 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
); /* offset */
428 output( "\taddl (%%esi,%%edx,4),%%eax\n" );
429 output( "\tpushl %%eax\n" );
430 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
435 output( "\tcall *8(%%ebp)\n" );
439 output( "\tmovl -4(%%ebp),%%esi\n" );
440 output_cfi( ".cfi_same_value %%esi" );
442 output( "\tleave\n" );
443 output_cfi( ".cfi_def_cfa %%esp,4" );
444 output_cfi( ".cfi_same_value %%ebp" );
446 output_cfi( ".cfi_endproc" );
447 output_function_size( name
);
452 /*******************************************************************
453 * callfrom16_type_compare
455 * Compare two callfrom16 sequences.
457 static int callfrom16_type_compare( const void *e1
, const void *e2
)
459 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
460 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
462 int type1
= odp1
->type
;
463 int type2
= odp2
->type
;
466 if (type1
== TYPE_STUB
) type1
= TYPE_CDECL
;
467 if (type2
== TYPE_STUB
) type2
= TYPE_CDECL
;
469 if ((retval
= type1
- type2
) != 0) return retval
;
471 type1
= odp1
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
472 type2
= odp2
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
474 if ((retval
= type1
- type2
) != 0) return retval
;
476 strcpy( args1
, get_args_str( odp1
));
477 return strcmp( args1
, get_args_str( odp2
));
481 /*******************************************************************
484 * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
486 static int relay_type_compare( const void *e1
, const void *e2
)
488 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
489 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
492 strcpy( name1
, get_relay_name(odp1
) );
493 return strcmp( name1
, get_relay_name(odp2
) );
497 /*******************************************************************
500 * Output code for a 16-bit module.
502 static void output_module16( DLLSPEC
*spec
)
505 ORDDEF
*entry_point
= NULL
;
508 /* store the main entry point as ordinal 0 */
512 assert(spec
->limit
== 0);
513 spec
->ordinals
= xmalloc( sizeof(spec
->ordinals
[0]) );
514 spec
->ordinals
[0] = NULL
;
516 if (spec
->init_func
&& !(spec
->characteristics
& IMAGE_FILE_DLL
))
518 entry_point
= xmalloc( sizeof(*entry_point
) );
519 entry_point
->type
= TYPE_PASCAL
;
520 entry_point
->ordinal
= 0;
521 entry_point
->lineno
= 0;
522 entry_point
->flags
= FLAG_REGISTER
;
523 entry_point
->name
= NULL
;
524 entry_point
->link_name
= xstrdup( spec
->init_func
);
525 entry_point
->export_name
= NULL
;
526 entry_point
->u
.func
.nb_args
= 0;
527 assert( !spec
->ordinals
[0] );
528 spec
->ordinals
[0] = entry_point
;
531 /* Build sorted list of all argument types, without duplicates */
533 typelist
= xmalloc( (spec
->limit
+ 1) * sizeof(*typelist
) );
535 for (i
= nb_funcs
= 0; i
<= spec
->limit
; i
++)
537 ORDDEF
*odp
= spec
->ordinals
[i
];
539 if (is_function( odp
)) typelist
[nb_funcs
++] = odp
;
542 nb_funcs
= sort_func_list( typelist
, nb_funcs
, callfrom16_type_compare
);
544 /* Output the module structure */
546 output( "\n/* module data */\n\n" );
547 output( "\t.data\n" );
548 output( "\t.align %d\n", get_alignment(16) );
549 output( ".L__wine_spec_dos_header:\n" );
550 output( "\t.short 0x5a4d\n" ); /* e_magic */
551 output( "\t.short 0\n" ); /* e_cblp */
552 output( "\t.short 0\n" ); /* e_cp */
553 output( "\t.short 0\n" ); /* e_crlc */
554 output( "\t.short 0\n" ); /* e_cparhdr */
555 output( "\t.short 0\n" ); /* e_minalloc */
556 output( "\t.short 0\n" ); /* e_maxalloc */
557 output( "\t.short 0\n" ); /* e_ss */
558 output( "\t.short 0\n" ); /* e_sp */
559 output( "\t.short 0\n" ); /* e_csum */
560 output( "\t.short 0\n" ); /* e_ip */
561 output( "\t.short 0\n" ); /* e_cs */
562 output( "\t.short 0\n" ); /* e_lfarlc */
563 output( "\t.short 0\n" ); /* e_ovno */
564 output( "\t.short 0,0,0,0\n" ); /* e_res */
565 output( "\t.short 0\n" ); /* e_oemid */
566 output( "\t.short 0\n" ); /* e_oeminfo */
567 output( ".Lwine_ldt_copy_ptr:\n" ); /* e_res2, used for private data */
568 output( "\t.long .L__wine_spec_ne_header_end-.L__wine_spec_dos_header,0,0,0,0\n" );
569 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
571 output( "\t%s \"%s\"\n", get_asm_string_keyword(), fakedll_signature
);
572 output( "\t.align %d\n", get_alignment(16) );
573 output( ".L__wine_spec_ne_header:\n" );
574 output( "\t.short 0x454e\n" ); /* ne_magic */
575 output( "\t.byte 0\n" ); /* ne_ver */
576 output( "\t.byte 0\n" ); /* ne_rev */
577 output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */
578 output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */
579 output( "\t.long 0\n" ); /* ne_crc */
580 output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA
| /* ne_flags */
581 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
582 output( "\t.short 2\n" ); /* ne_autodata */
583 output( "\t.short %u\n", spec
->heap_size
); /* ne_heap */
584 output( "\t.short 0\n" ); /* ne_stack */
585 if (!entry_point
) output( "\t.long 0\n" ); /* ne_csip */
586 else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", spec
->c_name
);
587 output( "\t.short 0,2\n" ); /* ne_sssp */
588 output( "\t.short 2\n" ); /* ne_cseg */
589 output( "\t.short 0\n" ); /* ne_cmod */
590 output( "\t.short 0\n" ); /* ne_cbnrestab */
591 output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */
592 output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */
593 output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */
594 output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */
595 output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */
596 output( "\t.long 0\n" ); /* ne_nrestab */
597 output( "\t.short 0\n" ); /* ne_cmovent */
598 output( "\t.short 0\n" ); /* ne_align */
599 output( "\t.short 0\n" ); /* ne_cres */
600 output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
601 output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
602 output( "\t.short 0\n" ); /* ne_pretthunks */
603 output( "\t.short 0\n" ); /* ne_psegrefbytes */
604 output( "\t.short 0\n" ); /* ne_swaparea */
605 output( "\t.short 0\n" ); /* ne_expver */
609 output( "\n.L__wine_spec_ne_segtab:\n" );
611 /* code segment entry */
613 output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */
614 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */
615 output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */
616 output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */
618 /* data segment entry */
620 output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */
621 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */
622 output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */
623 output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */
625 /* resource directory */
627 output_res16_directory( spec
);
629 /* resident names table */
631 output( "\n\t.align %d\n", get_alignment(2) );
632 output( ".L__wine_spec_ne_restab:\n" );
633 output_resident_name( spec
->dll_name
, 0 );
634 for (i
= 1; i
<= spec
->limit
; i
++)
636 ORDDEF
*odp
= spec
->ordinals
[i
];
637 if (!odp
|| !odp
->name
[0]) continue;
638 if (odp
->flags
& FLAG_EXPORT32
) continue;
639 output_resident_name( odp
->name
, i
);
641 output( "\t.byte 0\n" );
643 /* imported names table */
645 output( "\n\t.align %d\n", get_alignment(2) );
646 output( ".L__wine_spec_ne_modtab:\n" );
647 output( ".L__wine_spec_ne_imptab:\n" );
648 output( "\t.byte 0,0\n" );
652 output( "\n.L__wine_spec_ne_enttab:\n" );
653 output_entry_table( spec
);
654 output( ".L__wine_spec_ne_enttab_end:\n" );
658 output( "\n\t.align %d\n", get_alignment(2) );
659 output( ".L__wine_spec_code_segment:\n" );
661 for ( i
= 0; i
< nb_funcs
; i
++ )
663 unsigned int arg_types
[2];
664 int nop_words
, pos
, argsize
= 0;
666 if ( typelist
[i
]->type
== TYPE_PASCAL
)
667 argsize
= get_function_argsize( typelist
[i
] );
669 /* build the arg types bit fields */
670 arg_types
[0] = arg_types
[1] = 0;
671 for (j
= pos
= 0; j
< typelist
[i
]->u
.func
.nb_args
&& pos
< 20; j
++, pos
++)
674 switch (typelist
[i
]->u
.func
.args
[j
])
676 case ARG_WORD
: type
= ARG16_WORD
; break;
677 case ARG_SWORD
: type
= ARG16_SWORD
; break;
678 case ARG_SEGPTR
: type
= ARG16_LONG
; break;
679 case ARG_SEGSTR
: type
= ARG16_SEGSTR
; break;
680 case ARG_LONG
: type
= ARG16_LONG
; break;
681 case ARG_PTR
: type
= ARG16_PTR
; break;
682 case ARG_STR
: type
= ARG16_STR
; break;
683 case ARG_WSTR
: type
= ARG16_PTR
; break;
684 case ARG_FLOAT
: type
= ARG16_LONG
; break;
685 case ARG_INT128
: type
= ARG16_PTR
; break;
689 arg_types
[pos
/ 10] |= type
<< (3 * (pos
% 10));
693 if (pos
< 20) arg_types
[pos
/ 10] |= type
<< (3 * (pos
% 10));
695 if (typelist
[i
]->type
== TYPE_VARARGS
&& pos
< 20)
696 arg_types
[pos
/ 10] |= ARG16_VARARG
<< (3 * (pos
% 10));
698 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist
[i
]) );
699 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist
[i
]) );
700 output( "\tlcall $0,$0\n" );
702 if (typelist
[i
]->flags
& FLAG_REGISTER
)
706 else if (typelist
[i
]->flags
& FLAG_RET16
)
708 output( "\torw %%ax,%%ax\n" );
709 output( "\tnop\n" ); /* so that the lretw is aligned */
714 output( "\tshld $16,%%eax,%%edx\n" );
715 output( "\torl %%eax,%%eax\n" );
721 output( "\tlretw $%u\n", argsize
);
724 else output( "\tlretw\n" );
726 if (nop_words
) output( "\t%s\n", nop_sequence
[nop_words
-1] );
728 /* the movl is here so that the code contains only valid instructions, */
729 /* it's never actually executed, we only care about the arg_types[] values */
730 output( "\t.short 0x86c7\n" );
731 output( "\t.long 0x%08x,0x%08x\n", arg_types
[0], arg_types
[1] );
734 for (i
= 0; i
<= spec
->limit
; i
++)
736 ORDDEF
*odp
= spec
->ordinals
[i
];
737 if (!odp
|| !is_function( odp
)) continue;
738 output( ".L__wine_%s_%u:\n", spec
->c_name
, i
);
739 output( "\tpushw %%bp\n" );
740 output( "\tpushl $%s\n",
741 asm_name( odp
->type
== TYPE_STUB
? get_stub_name( odp
, spec
) : get_link_name( odp
)));
742 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp
) );
744 output( ".L__wine_spec_code_segment_end:\n" );
748 output( "\n.L__wine_spec_data_segment:\n" );
749 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
750 for (i
= 0; i
<= spec
->limit
; i
++)
752 ORDDEF
*odp
= spec
->ordinals
[i
];
753 if (!odp
|| odp
->type
!= TYPE_VARIABLE
) continue;
754 output( ".L__wine_%s_%u:\n", spec
->c_name
, i
);
755 output( "\t.long " );
756 for (j
= 0; j
< odp
->u
.var
.n_values
-1; j
++)
757 output( "0x%08x,", odp
->u
.var
.values
[j
] );
758 output( "0x%08x\n", odp
->u
.var
.values
[j
] );
760 output( ".L__wine_spec_data_segment_end:\n" );
764 if (spec
->nb_resources
)
766 output( "\n.L__wine_spec_resource_data:\n" );
767 output_res16_data( spec
);
770 output( ".L__wine_spec_ne_header_end:\n" );
771 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
773 /* relay functions */
775 nb_funcs
= sort_func_list( typelist
, nb_funcs
, relay_type_compare
);
778 output( "\n/* relay functions */\n\n" );
779 output( "\t.text\n" );
780 for ( i
= 0; i
< nb_funcs
; i
++ ) output_call16_function( typelist
[i
] );
787 /*******************************************************************
790 * Output the complete data for a spec 16-bit file.
792 void output_spec16_file( DLLSPEC
*spec16
)
794 DLLSPEC
*spec32
= alloc_dll_spec();
796 add_16bit_exports( spec32
, spec16
);
798 needs_get_pc_thunk
= 0;
800 output_standard_file_header();
801 output_module( spec32
);
802 output_module16( spec16
);
803 output_stubs( spec16
);
804 output_exports( spec32
);
805 output_imports( spec16
);
806 if (!strcmp( spec16
->dll_name
, "kernel" )) output_asm_relays16();
807 if (needs_get_pc_thunk
) output_get_pc_thunk();
808 if (spec16
->main_module
)
810 output( "\n\t%s\n", get_asm_string_section() );
811 output( ".L__wine_spec_main_module:\n" );
812 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16
->main_module
);
814 output_gnu_stack_note();
816 free_dll_spec( spec32
);
819 /*******************************************************************
820 * output_fake_module16
822 * Create a fake 16-bit binary module.
824 void output_fake_module16( DLLSPEC
*spec
)
826 static const unsigned char code_segment
[] = { 0x90, 0xc3 };
827 static const unsigned char data_segment
[16] = { 0 };
828 const unsigned int cseg
= 2;
829 const unsigned int lfanew
= (0x40 + sizeof(fakedll_signature
) + 15) & ~15;
830 const unsigned int segtab
= lfanew
+ 0x40;
832 unsigned int i
, rsrctab
, restab
, namelen
, modtab
, imptab
, enttab
, cbenttab
, codeseg
, dataseg
, rsrcdata
, rsrc_size
= 0;
833 void *rsrc_ptr
= NULL
;
835 init_output_buffer();
838 restab
= segtab
+ 8 * cseg
;
839 if (spec
->nb_resources
)
841 output_bin_res16_directory( spec
, 0 );
844 restab
+= output_buffer_pos
;
845 free( output_buffer
);
846 init_output_buffer();
847 output_bin_res16_data( spec
);
848 rsrc_ptr
= output_buffer
;
849 rsrc_size
= output_buffer_pos
;
850 init_output_buffer();
853 namelen
= strlen( spec
->dll_name
);
854 modtab
= restab
+ ((namelen
+ 3) & ~1);
858 codeseg
= (enttab
+ cbenttab
+ 1) & ~1;
859 dataseg
= codeseg
+ sizeof(code_segment
);
860 rsrcdata
= dataseg
+ sizeof(data_segment
);
862 init_output_buffer();
864 put_word( 0x5a4d ); /* e_magic */
865 put_word( 0x40 ); /* e_cblp */
866 put_word( 0x01 ); /* e_cp */
867 put_word( 0 ); /* e_crlc */
868 put_word( lfanew
/ 16 ); /* e_cparhdr */
869 put_word( 0x0000 ); /* e_minalloc */
870 put_word( 0xffff ); /* e_maxalloc */
871 put_word( 0x0000 ); /* e_ss */
872 put_word( 0x00b8 ); /* e_sp */
873 put_word( 0 ); /* e_csum */
874 put_word( 0 ); /* e_ip */
875 put_word( 0 ); /* e_cs */
876 put_word( lfanew
); /* e_lfarlc */
877 put_word( 0 ); /* e_ovno */
878 put_dword( 0 ); /* e_res */
880 put_word( 0 ); /* e_oemid */
881 put_word( 0 ); /* e_oeminfo */
882 put_dword( rsrcdata
+ rsrc_size
); /* e_res2 */
889 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
892 put_word( 0x454e ); /* ne_magic */
893 put_byte( 0 ); /* ne_ver */
894 put_byte( 0 ); /* ne_rev */
895 put_word( enttab
- lfanew
); /* ne_enttab */
896 put_word( cbenttab
); /* ne_cbenttab */
897 put_dword( 0 ); /* ne_crc */
898 put_word( NE_FFLAGS_SINGLEDATA
| /* ne_flags */
899 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
900 put_word( 2 ); /* ne_autodata */
901 put_word( spec
->heap_size
); /* ne_heap */
902 put_word( 0 ); /* ne_stack */
903 put_word( 0 ); put_word( 0 ); /* ne_csip */
904 put_word( 0 ); put_word( 2 ); /* ne_sssp */
905 put_word( cseg
); /* ne_cseg */
906 put_word( 0 ); /* ne_cmod */
907 put_word( 0 ); /* ne_cbnrestab */
908 put_word( segtab
- lfanew
); /* ne_segtab */
909 put_word( rsrctab
- lfanew
); /* ne_rsrctab */
910 put_word( restab
- lfanew
); /* ne_restab */
911 put_word( modtab
- lfanew
); /* ne_modtab */
912 put_word( imptab
- lfanew
); /* ne_imptab */
913 put_dword( 0 ); /* ne_nrestab */
914 put_word( 0 ); /* ne_cmovent */
915 put_word( 0 ); /* ne_align */
916 put_word( 0 ); /* ne_cres */
917 put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
918 put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
919 put_word( 0 ); /* ne_pretthunks */
920 put_word( 0 ); /* ne_psegrefbytes */
921 put_word( 0 ); /* ne_swaparea */
922 put_word( 0 ); /* ne_expver */
926 put_word( sizeof(code_segment
) );
927 put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
928 put_word( sizeof(code_segment
) );
930 put_word( sizeof(data_segment
) );
931 put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
932 put_word( sizeof(data_segment
) );
934 /* resource directory */
935 if (spec
->nb_resources
)
937 output_bin_res16_directory( spec
, rsrcdata
);
941 /* resident names table */
943 for (i
= 0; i
< namelen
; i
++) put_byte( toupper(spec
->dll_name
[i
]) );
947 /* imported names table */
955 put_data( code_segment
, sizeof(code_segment
) );
958 put_data( data_segment
, sizeof(data_segment
) );
961 put_data( rsrc_ptr
, rsrc_size
);