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 return (odp
->type
== TYPE_CDECL
||
61 odp
->type
== TYPE_PASCAL
||
62 odp
->type
== TYPE_VARARGS
||
63 odp
->type
== TYPE_STUB
);
66 /*******************************************************************
69 * Output entries for individual symbols in the entry table.
71 static void output_entries( DLLSPEC
*spec
, int first
, int count
)
75 for (i
= 0; i
< count
; i
++)
77 ORDDEF
*odp
= spec
->ordinals
[first
+ i
];
78 output( "\t.byte 0x03\n" ); /* flags: exported & public data */
85 output( "\t%s .L__wine_%s_%u-.L__wine_spec_code_segment\n",
86 get_asm_short_keyword(),
87 make_c_identifier(spec
->dll_name
), first
+ i
);
90 output( "\t%s .L__wine_%s_%u-.L__wine_spec_data_segment\n",
91 get_asm_short_keyword(),
92 make_c_identifier(spec
->dll_name
), first
+ i
);
95 output( "\t%s 0x%04x /* %s */\n",
96 get_asm_short_keyword(), odp
->u
.abs
.value
, odp
->name
);
105 /*******************************************************************
108 static void output_entry_table( DLLSPEC
*spec
)
110 int i
, prev
= 0, prev_sel
= -1, bundle_count
= 0;
112 for (i
= 1; i
<= spec
->limit
; i
++)
115 ORDDEF
*odp
= spec
->ordinals
[i
];
124 selector
= 1; /* Code selector */
127 selector
= 2; /* Data selector */
130 selector
= 0xfe; /* Constant selector */
136 if (prev
+ 1 != i
|| prev_sel
!= selector
|| bundle_count
== 255)
138 /* need to start a new bundle */
140 /* flush previous bundle */
143 output( "\t/* %s.%d - %s.%d */\n",
144 spec
->dll_name
, prev
- bundle_count
+ 1, spec
->dll_name
, prev
);
145 output( "\t.byte 0x%02x,0x%02x\n", bundle_count
, prev_sel
);
146 output_entries( spec
, prev
- bundle_count
+ 1, bundle_count
);
151 int skip
= i
- (prev
+ 1);
154 output( "\t.byte 0xff,0x00\n" );
157 output( "\t.byte 0x%02x,0x00\n", skip
);
167 /* flush last bundle */
170 output( "\t.byte 0x%02x,0x%02x\n", bundle_count
, prev_sel
);
171 output_entries( spec
, prev
- bundle_count
+ 1, bundle_count
);
173 output( "\t.byte 0x00\n" );
177 /*******************************************************************
178 * output_resident_name
180 static void output_resident_name( const char *string
, int ordinal
)
182 unsigned int i
, len
= strlen(string
);
184 output( "\t.byte 0x%02x", len
);
185 for (i
= 0; i
< len
; i
++) output( ",0x%02x", (unsigned char)toupper(string
[i
]) );
186 output( " /* %s */\n", string
);
187 output( "\t%s %u\n", get_asm_short_keyword(), ordinal
);
191 /*******************************************************************
192 * get_callfrom16_name
194 static const char *get_callfrom16_name( const ORDDEF
*odp
)
196 static char buffer
[80];
198 sprintf( buffer
, "%s_%s_%s",
199 (odp
->type
== TYPE_PASCAL
) ? "p" :
200 (odp
->type
== TYPE_VARARGS
) ? "v" : "c",
201 (odp
->flags
& FLAG_REGISTER
) ? "regs" :
202 (odp
->flags
& FLAG_RET16
) ? "word" : "long",
203 odp
->u
.func
.arg_types
);
208 /*******************************************************************
211 static const char *get_relay_name( const ORDDEF
*odp
)
213 static char buffer
[80];
219 strcpy( buffer
, "p_" );
222 strcpy( buffer
, "v_" );
226 strcpy( buffer
, "c_" );
231 strcat( buffer
, odp
->u
.func
.arg_types
);
232 for (p
= buffer
+ 2; *p
; p
++)
234 /* map string types to the corresponding plain pointer type */
235 if (*p
== 't') *p
= 'p';
236 else if (*p
== 'T') *p
= 'l';
238 if (odp
->flags
& FLAG_REGISTER
) strcat( buffer
, "_regs" );
243 /*******************************************************************
244 * get_function_argsize
246 static int get_function_argsize( const ORDDEF
*odp
)
251 for (args
= odp
->u
.func
.arg_types
; *args
; args
++)
256 case 's': /* s_word */
259 case 'l': /* long or segmented pointer */
260 case 'T': /* segmented pointer to null-terminated string */
261 case 'p': /* linear pointer */
262 case 't': /* linear pointer to null-terminated string */
273 /*******************************************************************
274 * output_call16_function
276 * Build a 16-bit-to-Wine callback glue function.
278 * The generated routines are intended to be used as argument conversion
279 * routines to be called by the CallFrom16... core. Thus, the prototypes of
280 * the generated routines are (see also CallFrom16):
282 * extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
283 * extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
284 * extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
286 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
287 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
288 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
289 * 'T'=segmented pointer to null-terminated string).
291 * The generated routines fetch the arguments from the 16-bit stack (pointed
292 * to by 'args'); the offsets of the single argument values are computed
293 * according to the calling convention and the argument types. Then, the
294 * 32-bit entry point is called with these arguments.
296 * For register functions, the arguments (if present) are converted just
297 * the same as for normal functions, but in addition the CONTEXT86 pointer
298 * filled with the current register values is passed to the 32-bit routine.
300 static void output_call16_function( ORDDEF
*odp
)
303 int i
, pos
, stack_words
;
304 const char *args
= odp
->u
.func
.arg_types
;
305 int argsize
= get_function_argsize( odp
);
306 int needs_ldt
= strchr( args
, 'p' ) || strchr( args
, 't' );
308 sprintf( name
, ".L__wine_spec_call16_%s", get_relay_name(odp
) );
310 output( "\t.align %d\n", get_alignment(4) );
311 output( "\t%s\n", func_declaration(name
) );
312 output( "%s:\n", name
);
313 output( "\tpushl %%ebp\n" );
314 output( "\tmovl %%esp,%%ebp\n" );
318 output( "\tpushl %%esi\n" );
322 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
323 output( "1:\tmovl wine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
326 output( "\tmovl $%s,%%esi\n", asm_name("wine_ldt_copy") );
329 /* preserve 16-byte stack alignment */
330 stack_words
+= strlen(args
);
331 if ((odp
->flags
& FLAG_REGISTER
) || (odp
->type
== TYPE_VARARGS
)) stack_words
++;
332 if (stack_words
% 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words
% 4) );
334 if (args
[0] || odp
->type
== TYPE_VARARGS
)
335 output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
337 if (odp
->flags
& FLAG_REGISTER
)
339 output( "\tpushl 16(%%ebp)\n" ); /* context */
341 else if (odp
->type
== TYPE_VARARGS
)
343 output( "\tleal %d(%%ecx),%%eax\n", argsize
);
344 output( "\tpushl %%eax\n" ); /* va_list16 */
347 pos
= (odp
->type
== TYPE_PASCAL
) ? 0 : argsize
;
348 for (i
= strlen(args
) - 1; i
>= 0; i
--)
353 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
354 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
);
355 output( "\tpushl %%eax\n" );
356 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
359 case 's': /* s_word */
360 if (odp
->type
!= TYPE_PASCAL
) pos
-= 2;
361 output( "\tmovswl %d(%%ecx),%%eax\n", pos
);
362 output( "\tpushl %%eax\n" );
363 if (odp
->type
== TYPE_PASCAL
) pos
+= 2;
366 case 'l': /* long or segmented pointer */
367 case 'T': /* segmented pointer to null-terminated string */
368 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
369 output( "\tpushl %d(%%ecx)\n", pos
);
370 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
373 case 'p': /* linear pointer */
374 case 't': /* linear pointer to null-terminated string */
375 if (odp
->type
!= TYPE_PASCAL
) pos
-= 4;
376 output( "\tmovzwl %d(%%ecx),%%edx\n", pos
+ 2 ); /* sel */
377 output( "\tshr $3,%%edx\n" );
378 output( "\tmovzwl %d(%%ecx),%%eax\n", pos
); /* offset */
379 output( "\taddl (%%esi,%%edx,4),%%eax\n" );
380 output( "\tpushl %%eax\n" );
381 if (odp
->type
== TYPE_PASCAL
) pos
+= 4;
389 output( "\tcall *8(%%ebp)\n" );
391 if (needs_ldt
) output( "\tmovl -4(%%ebp),%%esi\n" );
393 output( "\tleave\n" );
395 output_function_size( name
);
399 /*******************************************************************
400 * callfrom16_type_compare
402 * Compare two callfrom16 sequences.
404 static int callfrom16_type_compare( const void *e1
, const void *e2
)
406 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
407 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
409 int type1
= odp1
->type
;
410 int type2
= odp2
->type
;
412 if (type1
== TYPE_STUB
) type1
= TYPE_CDECL
;
413 if (type2
== TYPE_STUB
) type2
= TYPE_CDECL
;
415 if ((retval
= type1
- type2
) != 0) return retval
;
417 type1
= odp1
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
418 type2
= odp2
->flags
& (FLAG_RET16
|FLAG_REGISTER
);
420 if ((retval
= type1
- type2
) != 0) return retval
;
422 return strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
426 /*******************************************************************
429 * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
431 static int relay_type_compare( const void *e1
, const void *e2
)
433 const ORDDEF
*odp1
= *(const ORDDEF
* const *)e1
;
434 const ORDDEF
*odp2
= *(const ORDDEF
* const *)e2
;
437 strcpy( name1
, get_relay_name(odp1
) );
438 return strcmp( name1
, get_relay_name(odp2
) );
442 /*******************************************************************
445 * Sort a list of functions, removing duplicates.
447 static int sort_func_list( ORDDEF
**list
, int count
,
448 int (*compare
)(const void *, const void *) )
452 if (!count
) return 0;
453 qsort( list
, count
, sizeof(*list
), compare
);
455 for (i
= j
= 0; i
< count
; i
++)
457 if (compare( &list
[j
], &list
[i
] )) list
[++j
] = list
[i
];
463 /*******************************************************************
466 * Output the dll initialization code.
468 static void output_init_code( const DLLSPEC
*spec
)
472 sprintf( name
, ".L__wine_spec_%s_init", make_c_identifier(spec
->dll_name
) );
474 output( "\n/* dll initialization code */\n\n" );
475 output( "\t.text\n" );
476 output( "\t.align 4\n" );
477 output( "\t%s\n", func_declaration(name
) );
478 output( "%s:\n", name
);
479 output( "\tsubl $4,%%esp\n" );
482 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
483 output( "1:\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
484 output( "\tpushl %%ecx\n" );
485 output( "\tleal .L__wine_spec_dos_header-1b(%%eax),%%ecx\n" );
486 output( "\tpushl %%ecx\n" );
490 output( "\tpushl $.L__wine_spec_file_name\n" );
491 output( "\tpushl $.L__wine_spec_dos_header\n" );
493 output( "\tcall %s\n", asm_name("__wine_dll_register_16") );
494 output( "\taddl $12,%%esp\n" );
496 output_function_size( name
);
498 sprintf( name
, ".L__wine_spec_%s_fini", make_c_identifier(spec
->dll_name
) );
500 output( "\t.align 4\n" );
501 output( "\t%s\n", func_declaration(name
) );
502 output( "%s:\n", name
);
503 output( "\tsubl $8,%%esp\n" );
506 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
507 output( "1:\tleal .L__wine_spec_dos_header-1b(%%eax),%%ecx\n" );
508 output( "\tpushl %%ecx\n" );
512 output( "\tpushl $.L__wine_spec_dos_header\n" );
514 output( "\tcall %s\n", asm_name("__wine_dll_unregister_16") );
515 output( "\taddl $12,%%esp\n" );
517 output_function_size( name
);
519 if (target_platform
== PLATFORM_APPLE
)
521 output( "\t.mod_init_func\n" );
522 output( "\t.align %d\n", get_alignment(4) );
523 output( "\t.long .L__wine_spec_%s_init\n", make_c_identifier(spec
->dll_name
) );
524 output( "\t.mod_term_func\n" );
525 output( "\t.align %d\n", get_alignment(4) );
526 output( "\t.long .L__wine_spec_%s_fini\n", make_c_identifier(spec
->dll_name
) );
530 output( "\t.section \".init\",\"ax\"\n" );
531 output( "\tcall .L__wine_spec_%s_init\n", make_c_identifier(spec
->dll_name
) );
532 output( "\t.section \".fini\",\"ax\"\n" );
533 output( "\tcall .L__wine_spec_%s_fini\n", make_c_identifier(spec
->dll_name
) );
538 /*******************************************************************
541 * Output code for a 16-bit module.
543 static void output_module16( DLLSPEC
*spec
)
546 ORDDEF
*entry_point
= NULL
;
549 if (!spec
->file_name
)
552 spec
->file_name
= xstrdup( output_file_name
);
553 if ((p
= strrchr( spec
->file_name
, '.' ))) *p
= 0;
555 if (!spec
->dll_name
) /* set default name from file name */
558 spec
->dll_name
= xstrdup( spec
->file_name
);
559 if ((p
= strrchr( spec
->dll_name
, '.' ))) *p
= 0;
562 /* store the main entry point as ordinal 0 */
566 assert(spec
->limit
== 0);
567 spec
->ordinals
= xmalloc( sizeof(spec
->ordinals
[0]) );
568 spec
->ordinals
[0] = NULL
;
570 if (spec
->init_func
&& !(spec
->characteristics
& IMAGE_FILE_DLL
))
572 entry_point
= xmalloc( sizeof(*entry_point
) );
573 entry_point
->type
= TYPE_PASCAL
;
574 entry_point
->ordinal
= 0;
575 entry_point
->lineno
= 0;
576 entry_point
->flags
= FLAG_REGISTER
;
577 entry_point
->name
= NULL
;
578 entry_point
->link_name
= xstrdup( spec
->init_func
);
579 entry_point
->export_name
= NULL
;
580 entry_point
->u
.func
.arg_types
[0] = 0;
581 assert( !spec
->ordinals
[0] );
582 spec
->ordinals
[0] = entry_point
;
585 /* Build sorted list of all argument types, without duplicates */
587 typelist
= xmalloc( (spec
->limit
+ 1) * sizeof(*typelist
) );
589 for (i
= nb_funcs
= 0; i
<= spec
->limit
; i
++)
591 ORDDEF
*odp
= spec
->ordinals
[i
];
593 if (is_function( odp
)) typelist
[nb_funcs
++] = odp
;
596 nb_funcs
= sort_func_list( typelist
, nb_funcs
, callfrom16_type_compare
);
598 /* Output the module structure */
600 output( "\n/* module data */\n\n" );
601 output( "\t.data\n" );
602 output( "\t.align %d\n", get_alignment(4) );
603 output( ".L__wine_spec_dos_header:\n" );
604 output( "\t%s 0x5a4d\n", get_asm_short_keyword() ); /* e_magic */
605 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cblp */
606 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cp */
607 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_crlc */
608 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cparhdr */
609 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_minalloc */
610 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_maxalloc */
611 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ss */
612 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_sp */
613 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_csum */
614 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ip */
615 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cs */
616 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_lfarlc */
617 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ovno */
618 output( "\t%s 0,0,0,0\n", get_asm_short_keyword() ); /* e_res */
619 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_oemid */
620 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_oeminfo */
621 output( "\t%s 0,0,0,0,0,0,0,0,0,0\n", get_asm_short_keyword() ); /* e_res2 */
622 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
624 output( ".L__wine_spec_ne_header:\n" );
625 output( "\t%s 0x454e\n", get_asm_short_keyword() ); /* ne_magic */
626 output( "\t.byte 0\n" ); /* ne_ver */
627 output( "\t.byte 0\n" ); /* ne_rev */
628 output( "\t%s .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n", /* ne_enttab */
629 get_asm_short_keyword() );
630 output( "\t%s .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n", /* ne_cbenttab */
631 get_asm_short_keyword() );
632 output( "\t.long 0\n" ); /* ne_crc */
633 output( "\t%s 0x%04x\n", get_asm_short_keyword(), /* ne_flags */
634 NE_FFLAGS_SINGLEDATA
|
635 ((spec
->characteristics
& IMAGE_FILE_DLL
) ? NE_FFLAGS_LIBMODULE
: 0) );
636 output( "\t%s 2\n", get_asm_short_keyword() ); /* ne_autodata */
637 output( "\t%s %u\n", get_asm_short_keyword(), spec
->heap_size
); /* ne_heap */
638 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_stack */
639 if (!entry_point
) output( "\t.long 0\n" ); /* ne_csip */
640 else output( "\t%s .L__wine_%s_0-.L__wine_spec_code_segment,1\n",
641 get_asm_short_keyword(), make_c_identifier(spec
->dll_name
) );
642 output( "\t%s 0,2\n", get_asm_short_keyword() ); /* ne_sssp */
643 output( "\t%s 2\n", get_asm_short_keyword() ); /* ne_cseg */
644 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cmod */
645 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cbnrestab */
646 output( "\t%s .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n", /* ne_segtab */
647 get_asm_short_keyword() );
648 output( "\t%s .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n", /* ne_rsrctab */
649 get_asm_short_keyword() );
650 output( "\t%s .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n", /* ne_restab */
651 get_asm_short_keyword() );
652 output( "\t%s .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n", /* ne_modtab */
653 get_asm_short_keyword() );
654 output( "\t%s .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n", /* ne_imptab */
655 get_asm_short_keyword() );
656 output( "\t.long 0\n" ); /* ne_nrestab */
657 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cmovent */
658 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_align */
659 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cres */
660 output( "\t.byte 0x04\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
661 output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
662 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_pretthunks */
663 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_psegrefbytes */
664 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_swaparea */
665 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_expver */
669 output( "\n.L__wine_spec_ne_segtab:\n" );
671 /* code segment entry */
673 output( "\t%s .L__wine_spec_code_segment-.L__wine_spec_dos_header\n", /* filepos */
674 get_asm_short_keyword() );
675 output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* size */
676 get_asm_short_keyword() );
677 output( "\t%s 0x2000\n", get_asm_short_keyword() ); /* flags = NE_SEGFLAGS_32BIT */
678 output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* minsize */
679 get_asm_short_keyword() );
681 /* data segment entry */
683 output( "\t%s .L__wine_spec_data_segment-.L__wine_spec_dos_header\n", /* filepos */
684 get_asm_short_keyword() );
685 output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* size */
686 get_asm_short_keyword() );
687 output( "\t%s 0x0001\n", get_asm_short_keyword() ); /* flags = NE_SEGFLAGS_DATA */
688 output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* minsize */
689 get_asm_short_keyword() );
691 /* resource directory */
693 output_res16_directory( spec
);
695 /* resident names table */
697 output( "\n\t.align %d\n", get_alignment(2) );
698 output( ".L__wine_spec_ne_restab:\n" );
699 output_resident_name( spec
->dll_name
, 0 );
700 for (i
= 1; i
<= spec
->limit
; i
++)
702 ORDDEF
*odp
= spec
->ordinals
[i
];
703 if (!odp
|| !odp
->name
[0]) continue;
704 output_resident_name( odp
->name
, i
);
706 output( "\t.byte 0\n" );
708 /* imported names table */
710 output( "\n\t.align %d\n", get_alignment(2) );
711 output( ".L__wine_spec_ne_modtab:\n" );
712 output( ".L__wine_spec_ne_imptab:\n" );
713 output( "\t.byte 0,0\n" );
717 output( "\n.L__wine_spec_ne_enttab:\n" );
718 output_entry_table( spec
);
719 output( ".L__wine_spec_ne_enttab_end:\n" );
723 output( "\n\t.align %d\n", get_alignment(2) );
724 output( ".L__wine_spec_code_segment:\n" );
726 for ( i
= 0; i
< nb_funcs
; i
++ )
728 unsigned int arg_types
[2];
729 int nop_words
, argsize
= 0;
731 if ( typelist
[i
]->type
== TYPE_PASCAL
)
732 argsize
= get_function_argsize( typelist
[i
] );
734 /* build the arg types bit fields */
735 arg_types
[0] = arg_types
[1] = 0;
736 for (j
= 0; typelist
[i
]->u
.func
.arg_types
[j
]; j
++)
739 switch(typelist
[i
]->u
.func
.arg_types
[j
])
741 case 'w': type
= ARG_WORD
; break;
742 case 's': type
= ARG_SWORD
; break;
743 case 'l': type
= ARG_LONG
; break;
744 case 'p': type
= ARG_PTR
; break;
745 case 't': type
= ARG_STR
; break;
746 case 'T': type
= ARG_SEGSTR
; break;
748 arg_types
[j
/ 10] |= type
<< (3 * (j
% 10));
750 if (typelist
[i
]->type
== TYPE_VARARGS
) arg_types
[j
/ 10] |= ARG_VARARG
<< (3 * (j
% 10));
752 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist
[i
]) );
753 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist
[i
]) );
754 output( "\tlcall $0,$0\n" );
756 if (typelist
[i
]->flags
& FLAG_REGISTER
)
760 else if (typelist
[i
]->flags
& FLAG_RET16
)
762 output( "\torw %%ax,%%ax\n" );
763 output( "\tnop\n" ); /* so that the lretw is aligned */
768 output( "\tshld $16,%%eax,%%edx\n" );
769 output( "\torl %%eax,%%eax\n" );
775 output( "\tlretw $%u\n", argsize
);
778 else output( "\tlretw\n" );
780 if (nop_words
) output( "\t%s\n", nop_sequence
[nop_words
-1] );
782 /* the movl is here so that the code contains only valid instructions, */
783 /* it's never actually executed, we only care about the arg_types[] values */
784 output( "\t%s 0x86c7\n", get_asm_short_keyword() );
785 output( "\t.long 0x%08x,0x%08x\n", arg_types
[0], arg_types
[1] );
788 for (i
= 0; i
<= spec
->limit
; i
++)
790 ORDDEF
*odp
= spec
->ordinals
[i
];
791 if (!odp
|| !is_function( odp
)) continue;
792 output( ".L__wine_%s_%u:\n", make_c_identifier(spec
->dll_name
), i
);
793 output( "\tpushw %%bp\n" );
794 output( "\tpushl $%s\n",
795 asm_name( odp
->type
== TYPE_STUB
? get_stub_name( odp
, spec
) : odp
->link_name
));
796 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp
) );
798 output( ".L__wine_spec_code_segment_end:\n" );
802 output( "\n.L__wine_spec_data_segment:\n" );
803 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
804 for (i
= 0; i
<= spec
->limit
; i
++)
806 ORDDEF
*odp
= spec
->ordinals
[i
];
807 if (!odp
|| odp
->type
!= TYPE_VARIABLE
) continue;
808 output( ".L__wine_%s_%u:\n", make_c_identifier(spec
->dll_name
), i
);
809 output( "\t.long " );
810 for (j
= 0; j
< odp
->u
.var
.n_values
-1; j
++)
811 output( "0x%08x,", odp
->u
.var
.values
[j
] );
812 output( "0x%08x\n", odp
->u
.var
.values
[j
] );
814 output( ".L__wine_spec_data_segment_end:\n" );
818 if (spec
->nb_resources
)
820 output( "\n.L__wine_spec_resource_data:\n" );
821 output_res16_data( spec
);
824 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
826 /* relay functions */
828 nb_funcs
= sort_func_list( typelist
, nb_funcs
, relay_type_compare
);
831 output( "\n/* relay functions */\n\n" );
832 output( "\t.text\n" );
833 for ( i
= 0; i
< nb_funcs
; i
++ ) output_call16_function( typelist
[i
] );
834 output( "\t.data\n" );
835 output( "wine_ldt_copy_ptr:\n" );
836 output( "\t.long %s\n", asm_name("wine_ldt_copy") );
843 /*******************************************************************
846 * Build a Win16 assembly file from a spec file.
848 void BuildSpec16File( DLLSPEC
*spec
)
850 output_standard_file_header();
851 output_module16( spec
);
852 output_init_code( spec
);
854 output( "\n\t%s\n", get_asm_string_section() );
855 output( ".L__wine_spec_file_name:\n" );
856 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
858 output_stubs( spec
);
859 output_get_pc_thunk();
860 output_gnu_stack_note();
864 /*******************************************************************
867 * Output the complete data for a spec 16-bit file.
869 void output_spec16_file( DLLSPEC
*spec16
)
871 DLLSPEC
*spec32
= alloc_dll_spec();
873 spec32
->file_name
= xstrdup( spec16
->file_name
);
875 if (spec16
->characteristics
& IMAGE_FILE_DLL
)
877 spec32
->characteristics
= IMAGE_FILE_DLL
;
878 spec32
->init_func
= xstrdup( "__wine_spec_dll_entry" );
881 resolve_imports( spec16
);
882 add_16bit_exports( spec32
, spec16
);
884 output_standard_file_header();
885 output_module( spec32
);
886 output_module16( spec16
);
887 output_stubs( spec16
);
888 output_exports( spec32
);
889 output_imports( spec16
);
890 output_resources( spec16
);
891 if (spec16
->main_module
)
893 output( "\n\t%s\n", get_asm_string_section() );
894 output( ".L__wine_spec_main_module:\n" );
895 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16
->main_module
);
897 output_gnu_stack_note();
898 free_dll_spec( spec32
);