Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/spec16.c
4389 views
1
/*
2
* 16-bit spec files
3
*
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
9
*
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.
14
*
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.
19
*
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
23
*/
24
25
#include "config.h"
26
27
#include <assert.h>
28
#include <ctype.h>
29
30
#include "build.h"
31
32
#define NE_FFLAGS_SINGLEDATA 0x0001
33
#define NE_FFLAGS_LIBMODULE 0x8000
34
35
/* argument type flags for relay debugging */
36
enum arg_types
37
{
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 */
46
};
47
48
/* sequences of nops to fill a certain number of words */
49
static const char * const nop_sequence[4] =
50
{
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 */
55
};
56
57
static const char fakedll_signature[] = "Wine placeholder DLL";
58
59
static inline int is_function( const ORDDEF *odp )
60
{
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);
66
}
67
68
static const char *get_args_str( const ORDDEF *odp )
69
{
70
static char buffer[MAX_ARGUMENTS*2+1];
71
int i;
72
73
buffer[0] = 0;
74
for (i = 0; i < odp->u.func.nb_args; i++)
75
{
76
switch (odp->u.func.args[i])
77
{
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;
82
case ARG_LONG:
83
case ARG_FLOAT:
84
case ARG_SEGPTR: strcat( buffer, "l" ); break;
85
case ARG_PTR:
86
case ARG_WSTR:
87
case ARG_INT128: strcat( buffer, "p" ); break;
88
case ARG_INT64:
89
case ARG_DOUBLE: strcat( buffer, "ll" ); break;
90
}
91
}
92
return buffer;
93
}
94
95
/*******************************************************************
96
* output_entries
97
*
98
* Output entries for individual symbols in the entry table.
99
*/
100
static void output_entries( DLLSPEC *spec, int first, int count )
101
{
102
int i;
103
104
for (i = 0; i < count; i++)
105
{
106
ORDDEF *odp = spec->exports.ordinals[first + i];
107
output( "\t.byte 0x03\n" ); /* flags: exported & public data */
108
switch (odp->type)
109
{
110
case TYPE_CDECL:
111
case TYPE_PASCAL:
112
case TYPE_VARARGS:
113
case TYPE_STUB:
114
output( "\t.short .L__wine_%s_%u-.L__wine_spec_code_segment\n", spec->c_name, first + i );
115
break;
116
case TYPE_VARIABLE:
117
output( "\t.short .L__wine_%s_%u-.L__wine_spec_data_segment\n", spec->c_name, first + i );
118
break;
119
case TYPE_ABS:
120
output( "\t.short 0x%04x /* %s */\n",
121
odp->u.abs.value, odp->name );
122
break;
123
default:
124
assert(0);
125
}
126
}
127
}
128
129
130
/*******************************************************************
131
* output_entry_table
132
*/
133
static void output_entry_table( DLLSPEC *spec )
134
{
135
int i, prev = 0, prev_sel = -1, bundle_count = 0;
136
137
for (i = 1; i <= spec->exports.limit; i++)
138
{
139
int selector = 0;
140
ORDDEF *odp = spec->exports.ordinals[i];
141
if (!odp) continue;
142
if (odp->flags & FLAG_EXPORT32) continue;
143
144
switch (odp->type)
145
{
146
case TYPE_CDECL:
147
case TYPE_PASCAL:
148
case TYPE_VARARGS:
149
case TYPE_STUB:
150
selector = 1; /* Code selector */
151
break;
152
case TYPE_VARIABLE:
153
selector = 2; /* Data selector */
154
break;
155
case TYPE_ABS:
156
selector = 0xfe; /* Constant selector */
157
break;
158
default:
159
continue;
160
}
161
162
if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
163
{
164
/* need to start a new bundle */
165
166
/* flush previous bundle */
167
if (bundle_count)
168
{
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 );
173
}
174
175
if (prev + 1 != i)
176
{
177
int skip = i - (prev + 1);
178
while (skip > 255)
179
{
180
output( "\t.byte 0xff,0x00\n" );
181
skip -= 255;
182
}
183
output( "\t.byte 0x%02x,0x00\n", skip );
184
}
185
186
bundle_count = 0;
187
prev_sel = selector;
188
}
189
bundle_count++;
190
prev = i;
191
}
192
193
/* flush last bundle */
194
if (bundle_count)
195
{
196
output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
197
output_entries( spec, prev - bundle_count + 1, bundle_count );
198
}
199
output( "\t.byte 0x00\n" );
200
}
201
202
203
/*******************************************************************
204
* output_resident_name
205
*/
206
static void output_resident_name( const char *string, int ordinal )
207
{
208
unsigned int i, len = strlen(string);
209
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 );
214
}
215
216
217
/*******************************************************************
218
* get_callfrom16_name
219
*/
220
static const char *get_callfrom16_name( const ORDDEF *odp )
221
{
222
static char *buffer;
223
224
free( buffer );
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",
230
get_args_str(odp) );
231
return buffer;
232
}
233
234
235
/*******************************************************************
236
* get_relay_name
237
*/
238
static const char *get_relay_name( const ORDDEF *odp )
239
{
240
static char buffer[80];
241
char *p;
242
243
switch(odp->type)
244
{
245
case TYPE_PASCAL:
246
strcpy( buffer, "p_" );
247
break;
248
case TYPE_VARARGS:
249
strcpy( buffer, "v_" );
250
break;
251
case TYPE_CDECL:
252
case TYPE_STUB:
253
strcpy( buffer, "c_" );
254
break;
255
default:
256
assert(0);
257
}
258
strcat( buffer, get_args_str(odp) );
259
for (p = buffer + 2; *p; p++)
260
{
261
/* map string types to the corresponding plain pointer type */
262
if (*p == 't') *p = 'p';
263
else if (*p == 'T') *p = 'l';
264
}
265
if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
266
return buffer;
267
}
268
269
270
/*******************************************************************
271
* get_function_argsize
272
*/
273
static int get_function_argsize( const ORDDEF *odp )
274
{
275
int i, argsize = 0;
276
277
for (i = 0; i < odp->u.func.nb_args; i++)
278
{
279
switch (odp->u.func.args[i])
280
{
281
case ARG_WORD:
282
case ARG_SWORD:
283
argsize += 2;
284
break;
285
case ARG_SEGPTR:
286
case ARG_SEGSTR:
287
case ARG_LONG:
288
case ARG_PTR:
289
case ARG_STR:
290
case ARG_WSTR:
291
case ARG_FLOAT:
292
case ARG_INT128:
293
argsize += 4;
294
break;
295
case ARG_INT64:
296
case ARG_DOUBLE:
297
argsize += 8;
298
break;
299
}
300
}
301
return argsize;
302
}
303
304
305
/*******************************************************************
306
* output_call16_function
307
*
308
* Build a 16-bit-to-Wine callback glue function.
309
*
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):
313
*
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 );
317
*
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).
322
*
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.
327
*
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.
331
*/
332
static void output_call16_function( ORDDEF *odp )
333
{
334
char *name;
335
int i, pos, stack_words;
336
int argsize = get_function_argsize( odp );
337
int needs_ldt = (strpbrk( get_args_str( odp ), "pt" ) != NULL);
338
339
name = strmake( ".L__wine_spec_call16_%s", get_relay_name(odp) );
340
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" );
348
stack_words = 2;
349
if (needs_ldt)
350
{
351
output( "\tpushl %%esi\n" );
352
output_cfi( ".cfi_rel_offset %%esi,-4" );
353
stack_words++;
354
if (UsePIC)
355
{
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;
359
}
360
else
361
output( "\tmovl .Lwine_ldt_copy_ptr,%%esi\n" );
362
}
363
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) );
370
371
if (odp->u.func.nb_args || odp->type == TYPE_VARARGS)
372
output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
373
374
if (odp->flags & FLAG_REGISTER)
375
{
376
output( "\tpushl 16(%%ebp)\n" ); /* context */
377
}
378
else if (odp->type == TYPE_VARARGS)
379
{
380
output( "\tleal %d(%%ecx),%%eax\n", argsize );
381
output( "\tpushl %%eax\n" ); /* va_list16 */
382
}
383
384
pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
385
for (i = odp->u.func.nb_args - 1; i >= 0; i--)
386
{
387
switch (odp->u.func.args[i])
388
{
389
case ARG_WORD:
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;
394
break;
395
396
case ARG_SWORD:
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;
401
break;
402
403
case ARG_INT64:
404
case ARG_DOUBLE:
405
if (odp->type != TYPE_PASCAL) pos -= 4;
406
output( "\tpushl %d(%%ecx)\n", pos );
407
if (odp->type == TYPE_PASCAL) pos += 4;
408
/* fall through */
409
case ARG_LONG:
410
case ARG_FLOAT:
411
case ARG_SEGPTR:
412
case ARG_SEGSTR:
413
if (odp->type != TYPE_PASCAL) pos -= 4;
414
output( "\tpushl %d(%%ecx)\n", pos );
415
if (odp->type == TYPE_PASCAL) pos += 4;
416
break;
417
418
case ARG_PTR:
419
case ARG_STR:
420
case ARG_WSTR:
421
case ARG_INT128:
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;
429
break;
430
}
431
}
432
433
output( "\tcall *8(%%ebp)\n" );
434
435
if (needs_ldt)
436
{
437
output( "\tmovl -4(%%ebp),%%esi\n" );
438
output_cfi( ".cfi_same_value %%esi" );
439
}
440
output( "\tleave\n" );
441
output_cfi( ".cfi_def_cfa %%esp,4" );
442
output_cfi( ".cfi_same_value %%ebp" );
443
output( "\tret\n" );
444
output_cfi( ".cfi_endproc" );
445
output_function_size( name );
446
free( name );
447
}
448
449
450
/*******************************************************************
451
* callfrom16_type_compare
452
*
453
* Compare two callfrom16 sequences.
454
*/
455
static int callfrom16_type_compare( const void *e1, const void *e2 )
456
{
457
const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
458
const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
459
int retval;
460
int type1 = odp1->type;
461
int type2 = odp2->type;
462
char args1[80];
463
464
if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
465
if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
466
467
if ((retval = type1 - type2) != 0) return retval;
468
469
type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
470
type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
471
472
if ((retval = type1 - type2) != 0) return retval;
473
474
strcpy( args1, get_args_str( odp1 ));
475
return strcmp( args1, get_args_str( odp2 ));
476
}
477
478
479
/*******************************************************************
480
* relay_type_compare
481
*
482
* Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
483
*/
484
static int relay_type_compare( const void *e1, const void *e2 )
485
{
486
const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
487
const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
488
char name1[80];
489
490
strcpy( name1, get_relay_name(odp1) );
491
return strcmp( name1, get_relay_name(odp2) );
492
}
493
494
495
/*******************************************************************
496
* output_module16
497
*
498
* Output code for a 16-bit module.
499
*/
500
static void output_module16( DLLSPEC *spec )
501
{
502
struct exports *exports = &spec->exports;
503
ORDDEF **typelist;
504
ORDDEF *entry_point = NULL;
505
int i, j, nb_funcs;
506
507
/* store the main entry point as ordinal 0 */
508
509
if (!exports->ordinals)
510
{
511
assert(exports->limit == 0);
512
exports->ordinals = xmalloc( sizeof(exports->ordinals[0]) );
513
exports->ordinals[0] = NULL;
514
}
515
if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
516
{
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;
528
}
529
530
/* Build sorted list of all argument types, without duplicates */
531
532
typelist = xmalloc( (exports->limit + 1) * sizeof(*typelist) );
533
534
for (i = nb_funcs = 0; i <= exports->limit; i++)
535
{
536
ORDDEF *odp = exports->ordinals[i];
537
if (!odp) continue;
538
if (is_function( odp )) typelist[nb_funcs++] = odp;
539
}
540
541
nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
542
543
/* Output the module structure */
544
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 */
569
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 */
605
606
/* segment table */
607
608
output( "\n.L__wine_spec_ne_segtab:\n" );
609
610
/* code segment entry */
611
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 */
616
617
/* data segment entry */
618
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 */
623
624
/* resource directory */
625
626
output_res16_directory( spec );
627
628
/* resident names table */
629
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++)
634
{
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 );
639
}
640
output( "\t.byte 0\n" );
641
642
/* imported names table */
643
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" );
648
649
/* entry table */
650
651
output( "\n.L__wine_spec_ne_enttab:\n" );
652
output_entry_table( spec );
653
output( ".L__wine_spec_ne_enttab_end:\n" );
654
655
/* code segment */
656
657
output( "\n\t.balign 2\n" );
658
output( ".L__wine_spec_code_segment:\n" );
659
660
for ( i = 0; i < nb_funcs; i++ )
661
{
662
unsigned int arg_types[2];
663
int nop_words, pos, argsize = 0;
664
665
if ( typelist[i]->type == TYPE_PASCAL )
666
argsize = get_function_argsize( typelist[i] );
667
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++)
671
{
672
int type = 0;
673
switch (typelist[i]->u.func.args[j])
674
{
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;
685
case ARG_INT64:
686
case ARG_DOUBLE:
687
type = ARG16_LONG;
688
arg_types[pos / 10] |= type << (3 * (pos % 10));
689
pos++;
690
break;
691
}
692
if (pos < 20) arg_types[pos / 10] |= type << (3 * (pos % 10));
693
}
694
if (typelist[i]->type == TYPE_VARARGS && pos < 20)
695
arg_types[pos / 10] |= ARG16_VARARG << (3 * (pos % 10));
696
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" );
700
701
if (typelist[i]->flags & FLAG_REGISTER)
702
{
703
nop_words = 4;
704
}
705
else if (typelist[i]->flags & FLAG_RET16)
706
{
707
output( "\torw %%ax,%%ax\n" );
708
output( "\tnop\n" ); /* so that the lretw is aligned */
709
nop_words = 2;
710
}
711
else
712
{
713
output( "\tshld $16,%%eax,%%edx\n" );
714
output( "\torl %%eax,%%eax\n" );
715
nop_words = 1;
716
}
717
718
if (argsize)
719
{
720
output( "\tlretw $%u\n", argsize );
721
nop_words--;
722
}
723
else output( "\tlretw\n" );
724
725
if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
726
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] );
731
}
732
733
for (i = 0; i <= exports->limit; i++)
734
{
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", asm_name( get_link_name( odp )));
740
output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
741
}
742
output( ".L__wine_spec_code_segment_end:\n" );
743
744
/* data segment */
745
746
output( "\n.L__wine_spec_data_segment:\n" );
747
output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
748
for (i = 0; i <= exports->limit; i++)
749
{
750
ORDDEF *odp = exports->ordinals[i];
751
if (!odp || odp->type != TYPE_VARIABLE) continue;
752
output( ".L__wine_%s_%u:\n", spec->c_name, i );
753
output( "\t.long " );
754
for (j = 0; j < odp->u.var.n_values-1; j++)
755
output( "0x%08x,", odp->u.var.values[j] );
756
output( "0x%08x\n", odp->u.var.values[j] );
757
}
758
output( ".L__wine_spec_data_segment_end:\n" );
759
760
/* resource data */
761
762
if (spec->nb_resources)
763
{
764
output( "\n.L__wine_spec_resource_data:\n" );
765
output_res16_data( spec );
766
}
767
768
output( ".L__wine_spec_ne_header_end:\n" );
769
output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
770
771
/* relay functions */
772
773
nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
774
if (nb_funcs)
775
{
776
output( "\n/* relay functions */\n\n" );
777
output( "\t.text\n" );
778
for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
779
}
780
781
free( typelist );
782
}
783
784
785
/*******************************************************************
786
* output_spec16_file
787
*
788
* Output the complete data for a spec 16-bit file.
789
*/
790
void output_spec16_file( DLLSPEC *spec16 )
791
{
792
DLLSPEC *spec32 = alloc_dll_spec();
793
794
add_16bit_exports( spec32, spec16 );
795
796
needs_get_pc_thunk = 0;
797
open_output_file();
798
output_standard_file_header();
799
output_module( spec32 );
800
output_module16( spec16 );
801
output_stubs( spec16 );
802
output_exports( spec32 );
803
output_imports( spec16 );
804
if (!strcmp( spec16->dll_name, "kernel" )) output_asm_relays16();
805
if (needs_get_pc_thunk) output_get_pc_thunk();
806
if (spec16->main_module)
807
{
808
output( "\n\t%s\n", get_asm_string_section() );
809
output( ".L__wine_spec_main_module:\n" );
810
output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
811
}
812
output_gnu_stack_note();
813
close_output_file();
814
free_dll_spec( spec32 );
815
}
816
817
/*******************************************************************
818
* output_fake_module16
819
*
820
* Create a fake 16-bit binary module.
821
*/
822
void output_fake_module16( DLLSPEC *spec )
823
{
824
static const unsigned char code_segment[] = { 0x90, 0xc3 };
825
static const unsigned char data_segment[16] = { 0 };
826
const unsigned int cseg = 2;
827
const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
828
const unsigned int segtab = lfanew + 0x40;
829
830
unsigned int i, rsrctab, restab, namelen, modtab, imptab, enttab, cbenttab, codeseg, dataseg, rsrcdata, rsrc_size = 0;
831
void *rsrc_ptr = NULL;
832
833
init_output_buffer();
834
835
rsrctab = lfanew;
836
restab = segtab + 8 * cseg;
837
if (spec->nb_resources)
838
{
839
output_bin_res16_directory( spec, 0 );
840
align_output( 2 );
841
rsrctab = restab;
842
restab += output_buffer_pos;
843
free( output_buffer );
844
init_output_buffer();
845
output_bin_res16_data( spec );
846
rsrc_ptr = output_buffer;
847
rsrc_size = output_buffer_pos;
848
init_output_buffer();
849
}
850
851
namelen = strlen( spec->dll_name );
852
modtab = restab + ((namelen + 3) & ~1);
853
imptab = modtab;
854
enttab = modtab + 2;
855
cbenttab = 1;
856
codeseg = (enttab + cbenttab + 1) & ~1;
857
dataseg = codeseg + sizeof(code_segment);
858
rsrcdata = dataseg + sizeof(data_segment);
859
860
init_output_buffer();
861
862
put_word( 0x5a4d ); /* e_magic */
863
put_word( 0x40 ); /* e_cblp */
864
put_word( 0x01 ); /* e_cp */
865
put_word( 0 ); /* e_crlc */
866
put_word( lfanew / 16 ); /* e_cparhdr */
867
put_word( 0x0000 ); /* e_minalloc */
868
put_word( 0xffff ); /* e_maxalloc */
869
put_word( 0x0000 ); /* e_ss */
870
put_word( 0x00b8 ); /* e_sp */
871
put_word( 0 ); /* e_csum */
872
put_word( 0 ); /* e_ip */
873
put_word( 0 ); /* e_cs */
874
put_word( lfanew ); /* e_lfarlc */
875
put_word( 0 ); /* e_ovno */
876
put_dword( 0 ); /* e_res */
877
put_dword( 0 );
878
put_word( 0 ); /* e_oemid */
879
put_word( 0 ); /* e_oeminfo */
880
put_dword( rsrcdata + rsrc_size ); /* e_res2 */
881
put_dword( 0 );
882
put_dword( 0 );
883
put_dword( 0 );
884
put_dword( 0 );
885
put_dword( lfanew );
886
887
put_data( fakedll_signature, sizeof(fakedll_signature) );
888
align_output( 16 );
889
890
put_word( 0x454e ); /* ne_magic */
891
put_byte( 0 ); /* ne_ver */
892
put_byte( 0 ); /* ne_rev */
893
put_word( enttab - lfanew ); /* ne_enttab */
894
put_word( cbenttab ); /* ne_cbenttab */
895
put_dword( 0 ); /* ne_crc */
896
put_word( NE_FFLAGS_SINGLEDATA | /* ne_flags */
897
((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
898
put_word( 2 ); /* ne_autodata */
899
put_word( spec->heap_size ); /* ne_heap */
900
put_word( 0 ); /* ne_stack */
901
put_word( 0 ); put_word( 0 ); /* ne_csip */
902
put_word( 0 ); put_word( 2 ); /* ne_sssp */
903
put_word( cseg ); /* ne_cseg */
904
put_word( 0 ); /* ne_cmod */
905
put_word( 0 ); /* ne_cbnrestab */
906
put_word( segtab - lfanew ); /* ne_segtab */
907
put_word( rsrctab - lfanew ); /* ne_rsrctab */
908
put_word( restab - lfanew ); /* ne_restab */
909
put_word( modtab - lfanew ); /* ne_modtab */
910
put_word( imptab - lfanew ); /* ne_imptab */
911
put_dword( 0 ); /* ne_nrestab */
912
put_word( 0 ); /* ne_cmovent */
913
put_word( 0 ); /* ne_align */
914
put_word( 0 ); /* ne_cres */
915
put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
916
put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
917
put_word( 0 ); /* ne_pretthunks */
918
put_word( 0 ); /* ne_psegrefbytes */
919
put_word( 0 ); /* ne_swaparea */
920
put_word( 0 ); /* ne_expver */
921
922
/* segment table */
923
put_word( codeseg );
924
put_word( sizeof(code_segment) );
925
put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
926
put_word( sizeof(code_segment) );
927
put_word( dataseg );
928
put_word( sizeof(data_segment) );
929
put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
930
put_word( sizeof(data_segment) );
931
932
/* resource directory */
933
if (spec->nb_resources)
934
{
935
output_bin_res16_directory( spec, rsrcdata );
936
align_output( 2 );
937
}
938
939
/* resident names table */
940
put_byte( namelen );
941
for (i = 0; i < namelen; i++) put_byte( toupper(spec->dll_name[i]) );
942
put_byte( 0 );
943
align_output( 2 );
944
945
/* imported names table */
946
put_word( 0 );
947
948
/* entry table */
949
put_byte( 0 );
950
align_output( 2 );
951
952
/* code segment */
953
put_data( code_segment, sizeof(code_segment) );
954
955
/* data segment */
956
put_data( data_segment, sizeof(data_segment) );
957
958
/* resource data */
959
put_data( rsrc_ptr, rsrc_size );
960
}
961
962