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
26 #include "wine/port.h"
33 #define NE_FFLAGS_SINGLEDATA 0x0001
34 #define NE_FFLAGS_LIBMODULE 0x8000
36 /* argument type flags for relay debugging */
39 ARG_NONE
= 0, /* indicates end of arg list */
40 ARG_WORD
, /* unsigned word */
41 ARG_SWORD
, /* signed word */
42 ARG_LONG
, /* long or segmented pointer */
43 ARG_PTR
, /* linear pointer */
44 ARG_STR
, /* linear pointer to null-terminated string */
45 ARG_SEGSTR
, /* segmented pointer to null-terminated string */
46 ARG_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 inline int is_function( const ORDDEF
*odp
)
60 if (odp
->flags
& FLAG_EXPORT32
) return 0;
61 return (odp
->type
== TYPE_CDECL
||
62 odp
->type
== TYPE_PASCAL
||
63 odp
->type
== TYPE_VARARGS
||
64 odp
->type
== TYPE_STUB
);
67 /*******************************************************************
70 * Output entries for individual symbols in the entry table.
72 static void output_entries( DLLSPEC
*spec
, int first
, int count
)
76 for (i
= 0; i
< count
; i
++)
78 ORDDEF
*odp
= spec
->ordinals
[first
+ i
];
79 output( "\t.byte 0x03\n" ); /* flags: exported & public data */
86 output( "\t%s .L__wine_%s_%u-.L__wine_spec_code_segment\n",
87 get_asm_short_keyword(),
88 make_c_identifier(spec
->dll_name
), first
+ i
);
91 output( "\t%s .L__wine_%s_%u-.L__wine_spec_data_segment\n",
92 get_asm_short_keyword(),
93 make_c_identifier(spec
->dll_name
), first
+ i
);
96 output( "\t%s 0x%04x /* %s */\n",
97 get_asm_short_keyword(), odp
->u
.abs
.value
, odp
->name
);
106 /*******************************************************************
109 static void output_entry_table( DLLSPEC
*spec
)
111 int i
, prev
= 0, prev_sel
= -1, bundle_count
= 0;
113 for (i
= 1; i
<= spec
->limit
; i
++)
116 ORDDEF
*odp
= spec
->ordinals
[i
];
118 if (odp
->flags
& FLAG_EXPORT32
) continue;
126 selector
= 1; /* Code selector */
129 selector
= 2; /* Data selector */
132 selector
= 0xfe; /* Constant selector */
138 if (prev
+ 1 != i
|| prev_sel
!= selector
|| bundle_count
== 255)
140 /* need to start a new bundle */
142 /* flush previous bundle */
145 output( "\t/* %s.%d - %s.%d */\n",
146 spec
->dll_name
, prev
- bundle_count
+ 1, spec
->dll_name
, prev
);
147 output( "\t.byte 0x%02x,0x%02x\n", bundle_count
, prev_sel
);
148 output_entries( spec
, prev
- bundle_count
+ 1, bundle_count
);
153 int skip
= i
- (prev
+ 1);
156 output( "\t.byte 0xff,0x00\n" );
159 output( "\t.byte 0x%02x,0x00\n", skip
);
169 /* flush last bundle */
172 output( "\t.byte 0x%02x,0x%02x\n", bundle_count
, prev_sel
);
173 output_entries( spec
, prev
- bundle_count
+ 1, bundle_count
);
175 output( "\t.byte 0x00\n" );
179 /*******************************************************************
180 * output_resident_name
182 static void output_resident_name( const char *string
, int ordinal
)
184 unsigned int i
, len
= strlen(string
);
186 output( "\t.byte 0x%02x", len
);
187 for (i
= 0; i
< len
; i
++) output( ",0x%02x", (unsigned char)toupper(string
[i
]) );
188 output( " /* %s */\n", string
);
189 output( "\t%s %u\n", get_asm_short_keyword(), ordinal
);
193 /*******************************************************************
194 * get_callfrom16_name
196 static const char *get_callfrom16_name( const ORDDEF
*odp
)
198 static char buffer
[80];
200 sprintf( buffer
, "%s_%s_%s",
201 (odp
->type
== TYPE_PASCAL
) ? "p" :
202 (odp
->type
== TYPE_VARARGS
) ? "v" : "c",
203 (odp
->flags
& FLAG_REGISTER
) ? "regs" :
204 (odp
->flags
& FLAG_RET16
) ? "word" : "long",
205 odp
->u
.func
.arg_types
);
210 /*******************************************************************
213 static const char *get_relay_name( const ORDDEF
*odp
)
215 static char buffer
[80];
221 strcpy( buffer
, "p_" );
224 strcpy( buffer
, "v_" );
228 strcpy( buffer
, "c_" );
233 strcat( buffer
, odp
->u
.func
.arg_types
);
234 for (p
= buffer
+ 2; *p
; p
++)
236 /* map string types to the corresponding plain pointer type */
237 if (*p
== 't') *p
= 'p';
238 else if (*p
== 'T') *p
= 'l';
240 if (odp
->flags
& FLAG_REGISTER
) strcat( buffer
, "_regs" );
245 /*******************************************************************
246 * get_function_argsize
248 static int get_function_argsize( const ORDDEF
*odp
)
253 for (args
= odp
->u
.func
.arg_types
; *args
; args
++)
258 case 's': /* s_word */
261 case 'l': /* long or segmented pointer */
262 case 'T': /* segmented pointer to null-terminated string */
263 case 'p': /* linear pointer */
264 case 't': /* linear pointer to null-terminated string */
275 /*******************************************************************
276 * output_call16_function
278 * Build a 16-bit-to-Wine callback glue function.
280 * The generated routines are intended to be used as argument conversion
281 * routines to be called by the CallFrom16... core. Thus, the prototypes of
282 * the generated routines are (see also CallFrom16):
284 * extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
285 * extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
286 * extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
288 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
289 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
290 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
291 * 'T'=segmented pointer to null-terminated string).
293 * The generated routines fetch the arguments from the 16-bit stack (pointed
294 * to by 'args'); the offsets of the single argument values are computed
295 * according to the calling convention and the argument types. Then, the
296 * 32-bit entry point is called with these arguments.
298 * For register functions, the arguments (if present) are converted just
299 * the same as for normal functions, but in addition the CONTEXT86 pointer
300 * filled with the current register values is passed to the 32-bit routine.
302 static void output_call16_function( ORDDEF
*odp
)
305 int i
, pos
, stack_words
;
306 const char *args
= odp
->u
.func
.arg_types
;
307 int argsize
= get_function_argsize( odp
);
308 int needs_ldt
= strchr( args
, 'p' ) || strchr( args
, 't' );
310 sprintf( name
, ".L__wine_spec_call16_%s", get_relay_name(odp
) );
312 output( "\t.align %d\n", get_alignment(4) );
313 output( "\t%s\n", func_declaration(name
) );
314 output( "%s:\n", name
);
315 output_cfi( ".cfi_startproc" );
316 output( "\tpushl %%ebp\n" );
317 output_cfi( ".cfi_adjust_cfa_offset 4" );
318 output_cfi( ".cfi_rel_offset %%ebp,0" );
319 output( "\tmovl %%esp,%%ebp\n" );
320 output_cfi( ".cfi_def_cfa_register %%ebp" );
324 output( "\tpushl %%esi\n" );
325 output_cfi( ".cfi_rel_offset %%esi,-4" );
329 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
330 output( "1:\tmovl wine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
333 output( "\tmovl $%s,%%esi\n", asm_name("wine_ldt_copy") );
336 /* preserve 16-byte stack alignment */
337 stack_words
+= strlen(args
);
338 if ((odp
->flags
& FLAG_REGISTER
) || (odp
->type
== TYPE_VARARGS
)) stack_words
++;
339 if (stack_words
% 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words
% 4) );
341 if (args
[0] || odp
->type
== TYPE_VARARGS
)
342 output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
344 if (odp
->flags
& FLAG_REGISTER
)
346 output( "\tpushl 16(%%ebp)\n" ); /* context */
348 else if (odp
->type
== TYPE_VARARGS
)
350 output( "\tleal %d(%%ecx),%%eax\n", argsize
);
351 output( "\tpushl %%eax\n" ); /* va_list16 */
354 pos
= (odp
->type
== TYPE_PASCAL
) ? 0 : argsize
;
355 for (i
= strlen(args
) - 1; i
>= 0; i
--)
360 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
361 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
);
362 output( "\tpushl %%eax\n" );
363 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
366 case 's': /* s_word */
367 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
368 output( "\tmovswl %d(%%ecx),%%eax\n", pos
);
369 output( "\tpushl %%eax\n" );
370 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
373 case 'l': /* long or segmented pointer */
374 case 'T': /* segmented pointer to null-terminated string */
375 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
376 output( "\tpushl %d(%%ecx)\n", pos
);
377 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
380 case 'p': /* linear pointer */
381 case 't': /* linear pointer to null-terminated string */
382 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
383 output( "\tmovzwl %d(%%ecx),%%edx\n", pos
+ 2 ); /* sel */
384 output( "\tshr $3,%%edx\n" );
385 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
); /* offset */
386 output( "\taddl (%%esi,%%edx,4),%%eax\n" );
387 output( "\tpushl %%eax\n" );
388 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
396 output( "\tcall *8(%%ebp)\n" );
400 output( "\tmovl -4(%%ebp),%%esi\n" );
401 output_cfi( ".cfi_same_value %%esi" );
403 output( "\tleave\n" );
404 output_cfi( ".cfi_def_cfa %%esp,4" );
405 output_cfi( ".cfi_same_value %%ebp" );
407 output_cfi( ".cfi_endproc" );
408 output_function_size( name
);
412 /*******************************************************************
413 * callfrom16_type_compare
415 * Compare two callfrom16 sequences.
417 static int callfrom16_type_compare( const void *e1
, const void *e2
)
419 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
420 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
422 int type1
= odp1
->type
;
423 int type2
= odp2
->type
;
425 if (type1
== TYPE_STUB
) type1
= TYPE_CDECL
;
426 if (type2
== TYPE_STUB
) type2
= TYPE_CDECL
;
428 if ((retval
= type1
- type2
) != 0) return retval
;
430 type1
= odp1
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
431 type2
= odp2
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
433 if ((retval
= type1
- type2
) != 0) return retval
;
435 return strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
439 /*******************************************************************
442 * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
444 static int relay_type_compare( const void *e1
, const void *e2
)
446 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
447 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
450 strcpy( name1
, get_relay_name(odp1
) );
451 return strcmp( name1
, get_relay_name(odp2
) );
455 /*******************************************************************
458 * Sort a list of functions, removing duplicates.
460 static int sort_func_list( ORDDEF
**list
, int count
,
461 int (*compare
)(const void *, const void *) )
465 if (!count
) return 0;
466 qsort( list
, count
, sizeof(*list
), compare
);
468 for (i
= j
= 0; i
< count
; i
++)
470 if (compare( &list
[j
], &list
[i
] )) list
[++j
] = list
[i
];
476 /*******************************************************************
479 * Output code for a 16-bit module.
481 static void output_module16( DLLSPEC
*spec
)
484 ORDDEF
*entry_point
= NULL
;
487 /* store the main entry point as ordinal 0 */
491 assert(spec
->limit
== 0);
492 spec
->ordinals
= xmalloc( sizeof(spec
->ordinals
[0]) );
493 spec
->ordinals
[0] = NULL
;
495 if (spec
->init_func
&& !(spec
->characteristics
& IMAGE_FILE_DLL
))
497 entry_point
= xmalloc( sizeof(*entry_point
) );
498 entry_point
->type
= TYPE_PASCAL
;
499 entry_point
->ordinal
= 0;
500 entry_point
->lineno
= 0;
501 entry_point
->flags
= FLAG_REGISTER
;
502 entry_point
->name
= NULL
;
503 entry_point
->link_name
= xstrdup( spec
->init_func
);
504 entry_point
->export_name
= NULL
;
505 entry_point
->u
.func
.arg_types
[0] = 0;
506 assert( !spec
->ordinals
[0] );
507 spec
->ordinals
[0] = entry_point
;
510 /* Build sorted list of all argument types, without duplicates */
512 typelist
= xmalloc( (spec
->limit
+ 1) * sizeof(*typelist
) );
514 for (i
= nb_funcs
= 0; i
<= spec
->limit
; i
++)
516 ORDDEF
*odp
= spec
->ordinals
[i
];
518 if (is_function( odp
)) typelist
[nb_funcs
++] = odp
;
521 nb_funcs
= sort_func_list( typelist
, nb_funcs
, callfrom16_type_compare
);
523 /* Output the module structure */
525 output( "\n/* module data */\n\n" );
526 output( "\t.data\n" );
527 output( "\t.align %d\n", get_alignment(4) );
528 output( ".L__wine_spec_dos_header:\n" );
529 output( "\t%s 0x5a4d\n", get_asm_short_keyword() ); /* e_magic */
530 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cblp */
531 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cp */
532 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_crlc */
533 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cparhdr */
534 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_minalloc */
535 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_maxalloc */
536 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ss */
537 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_sp */
538 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_csum */
539 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ip */
540 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cs */
541 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_lfarlc */
542 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ovno */
543 output( "\t%s 0,0,0,0\n", get_asm_short_keyword() ); /* e_res */
544 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_oemid */
545 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_oeminfo */
546 output( "\t%s 0,0,0,0,0,0,0,0,0,0\n", get_asm_short_keyword() ); /* e_res2 */
547 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
549 output( ".L__wine_spec_ne_header:\n" );
550 output( "\t%s 0x454e\n", get_asm_short_keyword() ); /* ne_magic */
551 output( "\t.byte 0\n" ); /* ne_ver */
552 output( "\t.byte 0\n" ); /* ne_rev */
553 output( "\t%s .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n", /* ne_enttab */
554 get_asm_short_keyword() );
555 output( "\t%s .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n", /* ne_cbenttab */
556 get_asm_short_keyword() );
557 output( "\t.long 0\n" ); /* ne_crc */
558 output( "\t%s 0x%04x\n", get_asm_short_keyword(), /* ne_flags */
559 NE_FFLAGS_SINGLEDATA
|
560 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
561 output( "\t%s 2\n", get_asm_short_keyword() ); /* ne_autodata */
562 output( "\t%s %u\n", get_asm_short_keyword(), spec
->heap_size
); /* ne_heap */
563 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_stack */
564 if (!entry_point
) output( "\t.long 0\n" ); /* ne_csip */
565 else output( "\t%s .L__wine_%s_0-.L__wine_spec_code_segment,1\n",
566 get_asm_short_keyword(), make_c_identifier(spec
->dll_name
) );
567 output( "\t%s 0,2\n", get_asm_short_keyword() ); /* ne_sssp */
568 output( "\t%s 2\n", get_asm_short_keyword() ); /* ne_cseg */
569 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cmod */
570 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cbnrestab */
571 output( "\t%s .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n", /* ne_segtab */
572 get_asm_short_keyword() );
573 output( "\t%s .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n", /* ne_rsrctab */
574 get_asm_short_keyword() );
575 output( "\t%s .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n", /* ne_restab */
576 get_asm_short_keyword() );
577 output( "\t%s .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n", /* ne_modtab */
578 get_asm_short_keyword() );
579 output( "\t%s .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n", /* ne_imptab */
580 get_asm_short_keyword() );
581 output( "\t.long 0\n" ); /* ne_nrestab */
582 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cmovent */
583 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_align */
584 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cres */
585 output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
586 output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
587 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_pretthunks */
588 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_psegrefbytes */
589 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_swaparea */
590 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_expver */
594 output( "\n.L__wine_spec_ne_segtab:\n" );
596 /* code segment entry */
598 output( "\t%s .L__wine_spec_code_segment-.L__wine_spec_dos_header\n", /* filepos */
599 get_asm_short_keyword() );
600 output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* size */
601 get_asm_short_keyword() );
602 output( "\t%s 0x2000\n", get_asm_short_keyword() ); /* flags = NE_SEGFLAGS_32BIT */
603 output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* minsize */
604 get_asm_short_keyword() );
606 /* data segment entry */
608 output( "\t%s .L__wine_spec_data_segment-.L__wine_spec_dos_header\n", /* filepos */
609 get_asm_short_keyword() );
610 output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* size */
611 get_asm_short_keyword() );
612 output( "\t%s 0x0001\n", get_asm_short_keyword() ); /* flags = NE_SEGFLAGS_DATA */
613 output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* minsize */
614 get_asm_short_keyword() );
616 /* resource directory */
618 output_res16_directory( spec
);
620 /* resident names table */
622 output( "\n\t.align %d\n", get_alignment(2) );
623 output( ".L__wine_spec_ne_restab:\n" );
624 output_resident_name( spec
->dll_name
, 0 );
625 for (i
= 1; i
<= spec
->limit
; i
++)
627 ORDDEF
*odp
= spec
->ordinals
[i
];
628 if (!odp
|| !odp
->name
[0]) continue;
629 if (odp
->flags
& FLAG_EXPORT32
) continue;
630 output_resident_name( odp
->name
, i
);
632 output( "\t.byte 0\n" );
634 /* imported names table */
636 output( "\n\t.align %d\n", get_alignment(2) );
637 output( ".L__wine_spec_ne_modtab:\n" );
638 output( ".L__wine_spec_ne_imptab:\n" );
639 output( "\t.byte 0,0\n" );
643 output( "\n.L__wine_spec_ne_enttab:\n" );
644 output_entry_table( spec
);
645 output( ".L__wine_spec_ne_enttab_end:\n" );
649 output( "\n\t.align %d\n", get_alignment(2) );
650 output( ".L__wine_spec_code_segment:\n" );
652 for ( i
= 0; i
< nb_funcs
; i
++ )
654 unsigned int arg_types
[2];
655 int nop_words
, argsize
= 0;
657 if ( typelist
[i
]->type
== TYPE_PASCAL
)
658 argsize
= get_function_argsize( typelist
[i
] );
660 /* build the arg types bit fields */
661 arg_types
[0] = arg_types
[1] = 0;
662 for (j
= 0; typelist
[i
]->u
.func
.arg_types
[j
]; j
++)
665 switch(typelist
[i
]->u
.func
.arg_types
[j
])
667 case 'w': type
= ARG_WORD
; break;
668 case 's': type
= ARG_SWORD
; break;
669 case 'l': type
= ARG_LONG
; break;
670 case 'p': type
= ARG_PTR
; break;
671 case 't': type
= ARG_STR
; break;
672 case 'T': type
= ARG_SEGSTR
; break;
674 arg_types
[j
/ 10] |= type
<< (3 * (j
% 10));
676 if (typelist
[i
]->type
== TYPE_VARARGS
) arg_types
[j
/ 10] |= ARG_VARARG
<< (3 * (j
% 10));
678 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist
[i
]) );
679 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist
[i
]) );
680 output( "\tlcall $0,$0\n" );
682 if (typelist
[i
]->flags
& FLAG_REGISTER
)
686 else if (typelist
[i
]->flags
& FLAG_RET16
)
688 output( "\torw %%ax,%%ax\n" );
689 output( "\tnop\n" ); /* so that the lretw is aligned */
694 output( "\tshld $16,%%eax,%%edx\n" );
695 output( "\torl %%eax,%%eax\n" );
701 output( "\tlretw $%u\n", argsize
);
704 else output( "\tlretw\n" );
706 if (nop_words
) output( "\t%s\n", nop_sequence
[nop_words
-1] );
708 /* the movl is here so that the code contains only valid instructions, */
709 /* it's never actually executed, we only care about the arg_types[] values */
710 output( "\t%s 0x86c7\n", get_asm_short_keyword() );
711 output( "\t.long 0x%08x,0x%08x\n", arg_types
[0], arg_types
[1] );
714 for (i
= 0; i
<= spec
->limit
; i
++)
716 ORDDEF
*odp
= spec
->ordinals
[i
];
717 if (!odp
|| !is_function( odp
)) continue;
718 output( ".L__wine_%s_%u:\n", make_c_identifier(spec
->dll_name
), i
);
719 output( "\tpushw %%bp\n" );
720 output( "\tpushl $%s\n",
721 asm_name( odp
->type
== TYPE_STUB
? get_stub_name( odp
, spec
) : odp
->link_name
));
722 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp
) );
724 output( ".L__wine_spec_code_segment_end:\n" );
728 output( "\n.L__wine_spec_data_segment:\n" );
729 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
730 for (i
= 0; i
<= spec
->limit
; i
++)
732 ORDDEF
*odp
= spec
->ordinals
[i
];
733 if (!odp
|| odp
->type
!= TYPE_VARIABLE
) continue;
734 output( ".L__wine_%s_%u:\n", make_c_identifier(spec
->dll_name
), i
);
735 output( "\t.long " );
736 for (j
= 0; j
< odp
->u
.var
.n_values
-1; j
++)
737 output( "0x%08x,", odp
->u
.var
.values
[j
] );
738 output( "0x%08x\n", odp
->u
.var
.values
[j
] );
740 output( ".L__wine_spec_data_segment_end:\n" );
744 if (spec
->nb_resources
)
746 output( "\n.L__wine_spec_resource_data:\n" );
747 output_res16_data( spec
);
750 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
752 /* relay functions */
754 nb_funcs
= sort_func_list( typelist
, nb_funcs
, relay_type_compare
);
757 output( "\n/* relay functions */\n\n" );
758 output( "\t.text\n" );
759 for ( i
= 0; i
< nb_funcs
; i
++ ) output_call16_function( typelist
[i
] );
760 output( "\t.data\n" );
761 output( "wine_ldt_copy_ptr:\n" );
762 output( "\t.long %s\n", asm_name("wine_ldt_copy") );
769 /*******************************************************************
772 * Output the complete data for a spec 16-bit file.
774 void output_spec16_file( DLLSPEC
*spec16
)
776 DLLSPEC
*spec32
= alloc_dll_spec();
778 resolve_imports( spec16
);
779 add_16bit_exports( spec32
, spec16
);
781 output_standard_file_header();
782 output_module( spec32
);
783 output_module16( spec16
);
784 output_stubs( spec16
);
785 output_exports( spec32
);
786 output_imports( spec16
);
787 if (is_undefined( "__wine_call_from_16" )) output_asm_relays16();
788 if (spec16
->main_module
)
790 output( "\n\t%s\n", get_asm_string_section() );
791 output( ".L__wine_spec_main_module:\n" );
792 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16
->main_module
);
794 output_gnu_stack_note();
795 free_dll_spec( spec32
);
798 /*******************************************************************
799 * output_fake_module16
801 * Create a fake 16-bit binary module.
803 void output_fake_module16( DLLSPEC
*spec
)
805 static const unsigned char code_segment
[] = { 0x90, 0xc3 };
806 static const unsigned char data_segment
[16] = { 0 };
807 static const char fakedll_signature
[] = "Wine placeholder DLL";
808 const unsigned int cseg
= 2;
809 const unsigned int lfanew
= (0x40 + sizeof(fakedll_signature
) + 15) & ~15;
810 const unsigned int segtab
= lfanew
+ 0x40;
812 unsigned int i
, rsrctab
, restab
, namelen
, modtab
, imptab
, enttab
, cbenttab
, codeseg
, dataseg
, rsrcdata
;
814 init_output_buffer();
817 restab
= segtab
+ 8 * cseg
;
818 if (spec
->nb_resources
)
820 output_bin_res16_directory( spec
, 0 );
823 restab
+= output_buffer_pos
;
824 free( output_buffer
);
825 init_output_buffer();
828 namelen
= strlen( spec
->dll_name
);
829 modtab
= restab
+ ((namelen
+ 3) & ~1);
833 codeseg
= (enttab
+ cbenttab
+ 1) & ~1;
834 dataseg
= codeseg
+ sizeof(code_segment
);
835 rsrcdata
= dataseg
+ sizeof(data_segment
);
837 init_output_buffer();
839 put_word( 0x5a4d ); /* e_magic */
840 put_word( 0x40 ); /* e_cblp */
841 put_word( 0x01 ); /* e_cp */
842 put_word( 0 ); /* e_crlc */
843 put_word( lfanew
/ 16 ); /* e_cparhdr */
844 put_word( 0x0000 ); /* e_minalloc */
845 put_word( 0xffff ); /* e_maxalloc */
846 put_word( 0x0000 ); /* e_ss */
847 put_word( 0x00b8 ); /* e_sp */
848 put_word( 0 ); /* e_csum */
849 put_word( 0 ); /* e_ip */
850 put_word( 0 ); /* e_cs */
851 put_word( lfanew
); /* e_lfarlc */
852 put_word( 0 ); /* e_ovno */
853 put_dword( 0 ); /* e_res */
855 put_word( 0 ); /* e_oemid */
856 put_word( 0 ); /* e_oeminfo */
857 put_dword( 0 ); /* e_res2 */
864 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
867 put_word( 0x454e ); /* ne_magic */
868 put_byte( 0 ); /* ne_ver */
869 put_byte( 0 ); /* ne_rev */
870 put_word( enttab
- lfanew
); /* ne_enttab */
871 put_word( cbenttab
); /* ne_cbenttab */
872 put_dword( 0 ); /* ne_crc */
873 put_word( NE_FFLAGS_SINGLEDATA
| /* ne_flags */
874 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
875 put_word( 2 ); /* ne_autodata */
876 put_word( spec
->heap_size
); /* ne_heap */
877 put_word( 0 ); /* ne_stack */
878 put_word( 0 ); put_word( 0 ); /* ne_csip */
879 put_word( 0 ); put_word( 2 ); /* ne_sssp */
880 put_word( cseg
); /* ne_cseg */
881 put_word( 0 ); /* ne_cmod */
882 put_word( 0 ); /* ne_cbnrestab */
883 put_word( segtab
- lfanew
); /* ne_segtab */
884 put_word( rsrctab
- lfanew
); /* ne_rsrctab */
885 put_word( restab
- lfanew
); /* ne_restab */
886 put_word( modtab
- lfanew
); /* ne_modtab */
887 put_word( imptab
- lfanew
); /* ne_imptab */
888 put_dword( 0 ); /* ne_nrestab */
889 put_word( 0 ); /* ne_cmovent */
890 put_word( 0 ); /* ne_align */
891 put_word( 0 ); /* ne_cres */
892 put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
893 put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
894 put_word( 0 ); /* ne_pretthunks */
895 put_word( 0 ); /* ne_psegrefbytes */
896 put_word( 0 ); /* ne_swaparea */
897 put_word( 0 ); /* ne_expver */
901 put_word( sizeof(code_segment
) );
902 put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
903 put_word( sizeof(code_segment
) );
905 put_word( sizeof(data_segment
) );
906 put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
907 put_word( sizeof(data_segment
) );
909 /* resource directory */
910 if (spec
->nb_resources
)
912 output_bin_res16_directory( spec
, rsrcdata
);
916 /* resident names table */
918 for (i
= 0; i
< namelen
; i
++) put_byte( toupper(spec
->dll_name
[i
]) );
922 /* imported names table */
930 put_data( code_segment
, sizeof(code_segment
) );
933 put_data( data_segment
, sizeof(data_segment
) );
936 output_bin_res16_data( spec
);
938 flush_output_buffer();