Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/spec16.c
8699 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
/* offset of the ldt pointer */
33
#define LDT_OFFSET 0x220 /* FIELD_OFFSET(PEB,SpareUlongs[0]) */
34
35
#define NE_FFLAGS_SINGLEDATA 0x0001
36
#define NE_FFLAGS_LIBMODULE 0x8000
37
38
/* argument type flags for relay debugging */
39
enum arg_types
40
{
41
ARG16_NONE = 0, /* indicates end of arg list */
42
ARG16_WORD, /* unsigned word */
43
ARG16_SWORD, /* signed word */
44
ARG16_LONG, /* long or segmented pointer */
45
ARG16_PTR, /* linear pointer */
46
ARG16_STR, /* linear pointer to null-terminated string */
47
ARG16_SEGSTR, /* segmented pointer to null-terminated string */
48
ARG16_VARARG /* start of varargs */
49
};
50
51
/* sequences of nops to fill a certain number of words */
52
static const char * const nop_sequence[4] =
53
{
54
".byte 0x89,0xf6", /* mov %esi,%esi */
55
".byte 0x8d,0x74,0x26,0x00", /* lea 0x00(%esi),%esi */
56
".byte 0x8d,0xb6,0x00,0x00,0x00,0x00", /* lea 0x00000000(%esi),%esi */
57
".byte 0x8d,0x74,0x26,0x00,0x8d,0x74,0x26,0x00" /* lea 0x00(%esi),%esi; lea 0x00(%esi),%esi */
58
};
59
60
static const char fakedll_signature[] = "Wine placeholder DLL";
61
62
static inline int is_function( const ORDDEF *odp )
63
{
64
if (odp->flags & FLAG_EXPORT32) return 0;
65
return (odp->type == TYPE_CDECL ||
66
odp->type == TYPE_PASCAL ||
67
odp->type == TYPE_VARARGS ||
68
odp->type == TYPE_STUB);
69
}
70
71
static const char *get_args_str( const ORDDEF *odp )
72
{
73
static char buffer[MAX_ARGUMENTS*2+1];
74
int i;
75
76
buffer[0] = 0;
77
for (i = 0; i < odp->u.func.nb_args; i++)
78
{
79
switch (odp->u.func.args[i])
80
{
81
case ARG_WORD: strcat( buffer, "w" ); break;
82
case ARG_SWORD: strcat( buffer, "s" ); break;
83
case ARG_SEGSTR: strcat( buffer, "T" ); break;
84
case ARG_STR: strcat( buffer, "t" ); break;
85
case ARG_LONG:
86
case ARG_FLOAT:
87
case ARG_SEGPTR: strcat( buffer, "l" ); break;
88
case ARG_PTR:
89
case ARG_WSTR:
90
case ARG_INT128: strcat( buffer, "p" ); break;
91
case ARG_INT64:
92
case ARG_DOUBLE: strcat( buffer, "ll" ); break;
93
}
94
}
95
return buffer;
96
}
97
98
/*******************************************************************
99
* output_entries
100
*
101
* Output entries for individual symbols in the entry table.
102
*/
103
static void output_entries( DLLSPEC *spec, int first, int count )
104
{
105
int i;
106
107
for (i = 0; i < count; i++)
108
{
109
ORDDEF *odp = spec->exports.ordinals[first + i];
110
output( "\t.byte 0x03\n" ); /* flags: exported & public data */
111
switch (odp->type)
112
{
113
case TYPE_CDECL:
114
case TYPE_PASCAL:
115
case TYPE_VARARGS:
116
case TYPE_STUB:
117
output( "\t.short .L__wine_%s_%u-.L__wine_spec_code_segment\n", spec->c_name, first + i );
118
break;
119
case TYPE_VARIABLE:
120
output( "\t.short .L__wine_%s_%u-.L__wine_spec_data_segment\n", spec->c_name, first + i );
121
break;
122
case TYPE_ABS:
123
output( "\t.short 0x%04x /* %s */\n", odp->u.abs, odp->name );
124
break;
125
default:
126
assert(0);
127
}
128
}
129
}
130
131
132
/*******************************************************************
133
* output_entry_table
134
*/
135
static void output_entry_table( DLLSPEC *spec )
136
{
137
int i, prev = 0, prev_sel = -1, bundle_count = 0;
138
139
for (i = 1; i <= spec->exports.limit; i++)
140
{
141
int selector = 0;
142
ORDDEF *odp = spec->exports.ordinals[i];
143
if (!odp) continue;
144
if (odp->flags & FLAG_EXPORT32) continue;
145
146
switch (odp->type)
147
{
148
case TYPE_CDECL:
149
case TYPE_PASCAL:
150
case TYPE_VARARGS:
151
case TYPE_STUB:
152
selector = 1; /* Code selector */
153
break;
154
case TYPE_VARIABLE:
155
selector = 2; /* Data selector */
156
break;
157
case TYPE_ABS:
158
selector = 0xfe; /* Constant selector */
159
break;
160
default:
161
continue;
162
}
163
164
if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
165
{
166
/* need to start a new bundle */
167
168
/* flush previous bundle */
169
if (bundle_count)
170
{
171
output( "\t/* %s.%d - %s.%d */\n",
172
spec->dll_name, prev - bundle_count + 1, spec->dll_name, prev );
173
output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
174
output_entries( spec, prev - bundle_count + 1, bundle_count );
175
}
176
177
if (prev + 1 != i)
178
{
179
int skip = i - (prev + 1);
180
while (skip > 255)
181
{
182
output( "\t.byte 0xff,0x00\n" );
183
skip -= 255;
184
}
185
output( "\t.byte 0x%02x,0x00\n", skip );
186
}
187
188
bundle_count = 0;
189
prev_sel = selector;
190
}
191
bundle_count++;
192
prev = i;
193
}
194
195
/* flush last bundle */
196
if (bundle_count)
197
{
198
output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
199
output_entries( spec, prev - bundle_count + 1, bundle_count );
200
}
201
output( "\t.byte 0x00\n" );
202
}
203
204
205
/*******************************************************************
206
* output_resident_name
207
*/
208
static void output_resident_name( const char *string, int ordinal )
209
{
210
unsigned int i, len = strlen(string);
211
212
output( "\t.byte 0x%02x", len );
213
for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)toupper(string[i]) );
214
output( " /* %s */\n", string );
215
output( "\t.short %u\n", ordinal );
216
}
217
218
219
/*******************************************************************
220
* get_callfrom16_name
221
*/
222
static const char *get_callfrom16_name( const ORDDEF *odp )
223
{
224
return strmake( "%s_%s_%s",
225
(odp->type == TYPE_PASCAL) ? "p" :
226
(odp->type == TYPE_VARARGS) ? "v" : "c",
227
(odp->flags & FLAG_REGISTER) ? "regs" :
228
(odp->flags & FLAG_RET16) ? "word" : "long",
229
get_args_str(odp) );
230
}
231
232
233
/*******************************************************************
234
* get_relay_name
235
*/
236
static const char *get_relay_name( const ORDDEF *odp )
237
{
238
static char buffer[80];
239
char *p;
240
241
switch(odp->type)
242
{
243
case TYPE_PASCAL:
244
strcpy( buffer, "p_" );
245
break;
246
case TYPE_VARARGS:
247
strcpy( buffer, "v_" );
248
break;
249
case TYPE_CDECL:
250
case TYPE_STUB:
251
strcpy( buffer, "c_" );
252
break;
253
default:
254
assert(0);
255
}
256
strcat( buffer, get_args_str(odp) );
257
for (p = buffer + 2; *p; p++)
258
{
259
/* map string types to the corresponding plain pointer type */
260
if (*p == 't') *p = 'p';
261
else if (*p == 'T') *p = 'l';
262
}
263
if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
264
return buffer;
265
}
266
267
268
/*******************************************************************
269
* get_function_argsize
270
*/
271
static int get_function_argsize( const ORDDEF *odp )
272
{
273
int i, argsize = 0;
274
275
for (i = 0; i < odp->u.func.nb_args; i++)
276
{
277
switch (odp->u.func.args[i])
278
{
279
case ARG_WORD:
280
case ARG_SWORD:
281
argsize += 2;
282
break;
283
case ARG_SEGPTR:
284
case ARG_SEGSTR:
285
case ARG_LONG:
286
case ARG_PTR:
287
case ARG_STR:
288
case ARG_WSTR:
289
case ARG_FLOAT:
290
case ARG_INT128:
291
argsize += 4;
292
break;
293
case ARG_INT64:
294
case ARG_DOUBLE:
295
argsize += 8;
296
break;
297
}
298
}
299
return argsize;
300
}
301
302
303
/*******************************************************************
304
* output_call16_function
305
*
306
* Build a 16-bit-to-Wine callback glue function.
307
*
308
* The generated routines are intended to be used as argument conversion
309
* routines to be called by the CallFrom16... core. Thus, the prototypes of
310
* the generated routines are (see also CallFrom16):
311
*
312
* extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
313
* extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
314
* extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
315
*
316
* where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
317
* and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
318
* 'p'=linear pointer, 't'=linear pointer to null-terminated string,
319
* 'T'=segmented pointer to null-terminated string).
320
*
321
* The generated routines fetch the arguments from the 16-bit stack (pointed
322
* to by 'args'); the offsets of the single argument values are computed
323
* according to the calling convention and the argument types. Then, the
324
* 32-bit entry point is called with these arguments.
325
*
326
* For register functions, the arguments (if present) are converted just
327
* the same as for normal functions, but in addition the CONTEXT86 pointer
328
* filled with the current register values is passed to the 32-bit routine.
329
*/
330
static void output_call16_function( ORDDEF *odp )
331
{
332
char *name;
333
int i, pos, stack_words;
334
int argsize = get_function_argsize( odp );
335
int needs_ldt = (strpbrk( get_args_str( odp ), "pt" ) != NULL);
336
337
name = strmake( ".L__wine_spec_call16_%s", get_relay_name(odp) );
338
339
output_function_header( name, 0 );
340
output_cfi( ".cfi_startproc" );
341
output( "\tpushl %%ebp\n" );
342
output_cfi( ".cfi_adjust_cfa_offset 4" );
343
output_cfi( ".cfi_rel_offset %%ebp,0" );
344
output( "\tmovl %%esp,%%ebp\n" );
345
output_cfi( ".cfi_def_cfa_register %%ebp" );
346
stack_words = 2;
347
if (needs_ldt)
348
{
349
output( "\tpushl %%esi\n" );
350
output_cfi( ".cfi_rel_offset %%esi,-4" );
351
stack_words++;
352
output( "\tmovl %%fs:(0x30), %%esi\n" ); /* peb */
353
output( "\tmovl %u(%%esi), %%esi\n", LDT_OFFSET ); /* ldt_copy */
354
}
355
356
/* preserve 16-byte stack alignment */
357
stack_words += odp->u.func.nb_args;
358
for (i = 0; i < odp->u.func.nb_args; i++)
359
if (odp->u.func.args[i] == ARG_DOUBLE || odp->u.func.args[i] == ARG_INT64) stack_words++;
360
if ((odp->flags & FLAG_REGISTER) || (odp->type == TYPE_VARARGS)) stack_words++;
361
if (stack_words % 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words % 4) );
362
363
if (odp->u.func.nb_args || odp->type == TYPE_VARARGS)
364
output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
365
366
if (odp->flags & FLAG_REGISTER)
367
{
368
output( "\tpushl 16(%%ebp)\n" ); /* context */
369
}
370
else if (odp->type == TYPE_VARARGS)
371
{
372
output( "\tleal %d(%%ecx),%%eax\n", argsize );
373
output( "\tpushl %%eax\n" ); /* va_list16 */
374
}
375
376
pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
377
for (i = odp->u.func.nb_args - 1; i >= 0; i--)
378
{
379
switch (odp->u.func.args[i])
380
{
381
case ARG_WORD:
382
if (odp->type != TYPE_PASCAL) pos -= 2;
383
output( "\tmovzwl %d(%%ecx),%%eax\n", pos );
384
output( "\tpushl %%eax\n" );
385
if (odp->type == TYPE_PASCAL) pos += 2;
386
break;
387
388
case ARG_SWORD:
389
if (odp->type != TYPE_PASCAL) pos -= 2;
390
output( "\tmovswl %d(%%ecx),%%eax\n", pos );
391
output( "\tpushl %%eax\n" );
392
if (odp->type == TYPE_PASCAL) pos += 2;
393
break;
394
395
case ARG_INT64:
396
case ARG_DOUBLE:
397
if (odp->type != TYPE_PASCAL) pos -= 4;
398
output( "\tpushl %d(%%ecx)\n", pos );
399
if (odp->type == TYPE_PASCAL) pos += 4;
400
/* fall through */
401
case ARG_LONG:
402
case ARG_FLOAT:
403
case ARG_SEGPTR:
404
case ARG_SEGSTR:
405
if (odp->type != TYPE_PASCAL) pos -= 4;
406
output( "\tpushl %d(%%ecx)\n", pos );
407
if (odp->type == TYPE_PASCAL) pos += 4;
408
break;
409
410
case ARG_PTR:
411
case ARG_STR:
412
case ARG_WSTR:
413
case ARG_INT128:
414
if (odp->type != TYPE_PASCAL) pos -= 4;
415
output( "\tmovzwl %d(%%ecx),%%edx\n", pos + 2 ); /* sel */
416
output( "\tshr $3,%%edx\n" );
417
output( "\tmovzwl %d(%%ecx),%%eax\n", pos ); /* offset */
418
output( "\taddl (%%esi,%%edx,4),%%eax\n" );
419
output( "\tpushl %%eax\n" );
420
if (odp->type == TYPE_PASCAL) pos += 4;
421
break;
422
}
423
}
424
425
output( "\tcall *8(%%ebp)\n" );
426
427
if (needs_ldt)
428
{
429
output( "\tmovl -4(%%ebp),%%esi\n" );
430
output_cfi( ".cfi_same_value %%esi" );
431
}
432
output( "\tleave\n" );
433
output_cfi( ".cfi_def_cfa %%esp,4" );
434
output_cfi( ".cfi_same_value %%ebp" );
435
output( "\tret\n" );
436
output_cfi( ".cfi_endproc" );
437
output_function_size( name );
438
}
439
440
441
/*******************************************************************
442
* callfrom16_type_compare
443
*
444
* Compare two callfrom16 sequences.
445
*/
446
static int callfrom16_type_compare( const void *e1, const void *e2 )
447
{
448
const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
449
const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
450
int retval;
451
int type1 = odp1->type;
452
int type2 = odp2->type;
453
char args1[80];
454
455
if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
456
if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
457
458
if ((retval = type1 - type2) != 0) return retval;
459
460
type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
461
type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
462
463
if ((retval = type1 - type2) != 0) return retval;
464
465
strcpy( args1, get_args_str( odp1 ));
466
return strcmp( args1, get_args_str( odp2 ));
467
}
468
469
470
/*******************************************************************
471
* relay_type_compare
472
*
473
* Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
474
*/
475
static int relay_type_compare( const void *e1, const void *e2 )
476
{
477
const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
478
const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
479
char name1[80];
480
481
strcpy( name1, get_relay_name(odp1) );
482
return strcmp( name1, get_relay_name(odp2) );
483
}
484
485
486
/*******************************************************************
487
* output_module16
488
*
489
* Output code for a 16-bit module.
490
*/
491
static void output_module16( DLLSPEC *spec )
492
{
493
struct exports *exports = &spec->exports;
494
ORDDEF **typelist;
495
ORDDEF *entry_point = NULL;
496
int i, j, nb_funcs;
497
498
/* store the main entry point as ordinal 0 */
499
500
if (!exports->ordinals)
501
{
502
assert(exports->limit == 0);
503
exports->ordinals = xmalloc( sizeof(exports->ordinals[0]) );
504
exports->ordinals[0] = NULL;
505
}
506
if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
507
{
508
entry_point = xmalloc( sizeof(*entry_point) );
509
entry_point->type = TYPE_PASCAL;
510
entry_point->ordinal = 0;
511
entry_point->lineno = 0;
512
entry_point->flags = FLAG_REGISTER;
513
entry_point->name = NULL;
514
entry_point->link_name = xstrdup( spec->init_func );
515
entry_point->export_name = NULL;
516
entry_point->u.func.nb_args = 0;
517
assert( !exports->ordinals[0] );
518
exports->ordinals[0] = entry_point;
519
}
520
521
/* Build sorted list of all argument types, without duplicates */
522
523
typelist = xmalloc( (exports->limit + 1) * sizeof(*typelist) );
524
525
for (i = nb_funcs = 0; i <= exports->limit; i++)
526
{
527
ORDDEF *odp = exports->ordinals[i];
528
if (!odp) continue;
529
if (is_function( odp )) typelist[nb_funcs++] = odp;
530
}
531
532
nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
533
534
/* Output the module structure */
535
536
output( "\n/* module data */\n\n" );
537
output( "\t.data\n" );
538
output( "\t.balign 16\n" );
539
output( ".L__wine_spec_dos_header:\n" );
540
output( "\t.short 0x5a4d\n" ); /* e_magic */
541
output( "\t.short 0\n" ); /* e_cblp */
542
output( "\t.short 0\n" ); /* e_cp */
543
output( "\t.short 0\n" ); /* e_crlc */
544
output( "\t.short 0\n" ); /* e_cparhdr */
545
output( "\t.short 0\n" ); /* e_minalloc */
546
output( "\t.short 0\n" ); /* e_maxalloc */
547
output( "\t.short 0\n" ); /* e_ss */
548
output( "\t.short 0\n" ); /* e_sp */
549
output( "\t.short 0\n" ); /* e_csum */
550
output( "\t.short 0\n" ); /* e_ip */
551
output( "\t.short 0\n" ); /* e_cs */
552
output( "\t.short 0\n" ); /* e_lfarlc */
553
output( "\t.short 0\n" ); /* e_ovno */
554
output( "\t.short 0,0,0,0\n" ); /* e_res */
555
output( "\t.short 0\n" ); /* e_oemid */
556
output( "\t.short 0\n" ); /* e_oeminfo */
557
output( "\t.long .L__wine_spec_ne_header_end-.L__wine_spec_dos_header,0,0,0,0\n" ); /* e_res2, used for private data */
558
output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
559
560
output( "\t%s \"%s\"\n", get_asm_string_keyword(), fakedll_signature );
561
output( "\t.balign 16\n" );
562
output( ".L__wine_spec_ne_header:\n" );
563
output( "\t.short 0x454e\n" ); /* ne_magic */
564
output( "\t.byte 0\n" ); /* ne_ver */
565
output( "\t.byte 0\n" ); /* ne_rev */
566
output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */
567
output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */
568
output( "\t.long 0\n" ); /* ne_crc */
569
output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA | /* ne_flags */
570
((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
571
output( "\t.short 2\n" ); /* ne_autodata */
572
output( "\t.short %u\n", spec->heap_size ); /* ne_heap */
573
output( "\t.short 0\n" ); /* ne_stack */
574
if (!entry_point) output( "\t.long 0\n" ); /* ne_csip */
575
else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", spec->c_name );
576
output( "\t.short 0,2\n" ); /* ne_sssp */
577
output( "\t.short 2\n" ); /* ne_cseg */
578
output( "\t.short 0\n" ); /* ne_cmod */
579
output( "\t.short 0\n" ); /* ne_cbnrestab */
580
output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */
581
output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */
582
output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */
583
output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */
584
output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */
585
output( "\t.long 0\n" ); /* ne_nrestab */
586
output( "\t.short 0\n" ); /* ne_cmovent */
587
output( "\t.short 0\n" ); /* ne_align */
588
output( "\t.short 0\n" ); /* ne_cres */
589
output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
590
output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
591
output( "\t.short 0\n" ); /* ne_pretthunks */
592
output( "\t.short 0\n" ); /* ne_psegrefbytes */
593
output( "\t.short 0\n" ); /* ne_swaparea */
594
output( "\t.short 0\n" ); /* ne_expver */
595
596
/* segment table */
597
598
output( "\n.L__wine_spec_ne_segtab:\n" );
599
600
/* code segment entry */
601
602
output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */
603
output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */
604
output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */
605
output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */
606
607
/* data segment entry */
608
609
output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */
610
output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */
611
output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */
612
output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */
613
614
/* resource directory */
615
616
output_res16_directory( spec );
617
618
/* resident names table */
619
620
output( "\n\t.balign 2\n" );
621
output( ".L__wine_spec_ne_restab:\n" );
622
output_resident_name( spec->dll_name, 0 );
623
for (i = 1; i <= exports->limit; i++)
624
{
625
ORDDEF *odp = exports->ordinals[i];
626
if (!odp || !odp->name[0]) continue;
627
if (odp->flags & FLAG_EXPORT32) continue;
628
output_resident_name( odp->name, i );
629
}
630
output( "\t.byte 0\n" );
631
632
/* imported names table */
633
634
output( "\n\t.balign 2\n" );
635
output( ".L__wine_spec_ne_modtab:\n" );
636
output( ".L__wine_spec_ne_imptab:\n" );
637
output( "\t.byte 0,0\n" );
638
639
/* entry table */
640
641
output( "\n.L__wine_spec_ne_enttab:\n" );
642
output_entry_table( spec );
643
output( ".L__wine_spec_ne_enttab_end:\n" );
644
645
/* code segment */
646
647
output( "\n\t.balign 2\n" );
648
output( ".L__wine_spec_code_segment:\n" );
649
650
for ( i = 0; i < nb_funcs; i++ )
651
{
652
unsigned int arg_types[2];
653
int nop_words, pos, argsize = 0;
654
655
if ( typelist[i]->type == TYPE_PASCAL )
656
argsize = get_function_argsize( typelist[i] );
657
658
/* build the arg types bit fields */
659
arg_types[0] = arg_types[1] = 0;
660
for (j = pos = 0; j < typelist[i]->u.func.nb_args && pos < 20; j++, pos++)
661
{
662
int type = 0;
663
switch (typelist[i]->u.func.args[j])
664
{
665
case ARG_WORD: type = ARG16_WORD; break;
666
case ARG_SWORD: type = ARG16_SWORD; break;
667
case ARG_SEGPTR: type = ARG16_LONG; break;
668
case ARG_SEGSTR: type = ARG16_SEGSTR; break;
669
case ARG_LONG: type = ARG16_LONG; break;
670
case ARG_PTR: type = ARG16_PTR; break;
671
case ARG_STR: type = ARG16_STR; break;
672
case ARG_WSTR: type = ARG16_PTR; break;
673
case ARG_FLOAT: type = ARG16_LONG; break;
674
case ARG_INT128: type = ARG16_PTR; break;
675
case ARG_INT64:
676
case ARG_DOUBLE:
677
type = ARG16_LONG;
678
arg_types[pos / 10] |= type << (3 * (pos % 10));
679
pos++;
680
break;
681
}
682
if (pos < 20) arg_types[pos / 10] |= type << (3 * (pos % 10));
683
}
684
if (typelist[i]->type == TYPE_VARARGS && pos < 20)
685
arg_types[pos / 10] |= ARG16_VARARG << (3 * (pos % 10));
686
687
output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
688
output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
689
output( "\tlcall $0,$0\n" );
690
691
if (typelist[i]->flags & FLAG_REGISTER)
692
{
693
nop_words = 4;
694
}
695
else if (typelist[i]->flags & FLAG_RET16)
696
{
697
output( "\torw %%ax,%%ax\n" );
698
output( "\tnop\n" ); /* so that the lretw is aligned */
699
nop_words = 2;
700
}
701
else
702
{
703
output( "\tshld $16,%%eax,%%edx\n" );
704
output( "\torl %%eax,%%eax\n" );
705
nop_words = 1;
706
}
707
708
if (argsize)
709
{
710
output( "\tlretw $%u\n", argsize );
711
nop_words--;
712
}
713
else output( "\tlretw\n" );
714
715
if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
716
717
/* the movl is here so that the code contains only valid instructions, */
718
/* it's never actually executed, we only care about the arg_types[] values */
719
output( "\t.short 0x86c7\n" );
720
output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
721
}
722
723
for (i = 0; i <= exports->limit; i++)
724
{
725
ORDDEF *odp = exports->ordinals[i];
726
if (!odp || !is_function( odp )) continue;
727
output( ".L__wine_%s_%u:\n", spec->c_name, i );
728
output( "\tpushw %%bp\n" );
729
output( "\tpushl $%s\n", asm_name( get_link_name( odp )));
730
output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
731
}
732
output( ".L__wine_spec_code_segment_end:\n" );
733
734
/* data segment */
735
736
output( "\n.L__wine_spec_data_segment:\n" );
737
output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
738
for (i = 0; i <= exports->limit; i++)
739
{
740
ORDDEF *odp = exports->ordinals[i];
741
if (!odp || odp->type != TYPE_VARIABLE) continue;
742
output( ".L__wine_%s_%u:\n", spec->c_name, i );
743
output( "\t.long " );
744
ARRAY_FOR_EACH( val, &odp->u.var, unsigned int ) output( "0x%08x,", *val );
745
output( "0\n" );
746
}
747
output( ".L__wine_spec_data_segment_end:\n" );
748
749
/* resource data */
750
751
if (spec->resources.count)
752
{
753
output( "\n.L__wine_spec_resource_data:\n" );
754
output_res16_data( spec );
755
}
756
757
output( ".L__wine_spec_ne_header_end:\n" );
758
output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
759
760
/* relay functions */
761
762
nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
763
if (nb_funcs)
764
{
765
output( "\n/* relay functions */\n\n" );
766
output( "\t.text\n" );
767
for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
768
}
769
770
free( typelist );
771
}
772
773
774
/*******************************************************************
775
* output_spec16_file
776
*
777
* Output the complete data for a spec 16-bit file.
778
*/
779
void output_spec16_file( DLLSPEC *spec16 )
780
{
781
DLLSPEC *spec32 = alloc_dll_spec();
782
783
add_16bit_exports( spec32, spec16 );
784
785
needs_get_pc_thunk = 0;
786
open_output_file();
787
output_standard_file_header();
788
output_module( spec32 );
789
output_module16( spec16 );
790
output_stubs( spec16 );
791
output_exports( spec32 );
792
output_imports( spec16 );
793
output_crt_sections();
794
if (!strcmp( spec16->dll_name, "kernel" )) output_asm_relays16();
795
if (needs_get_pc_thunk) output_get_pc_thunk();
796
if (spec16->main_module)
797
{
798
output( "\n\t%s\n", get_asm_string_section() );
799
output( ".L__wine_spec_main_module:\n" );
800
output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
801
}
802
output_gnu_stack_note();
803
close_output_file();
804
}
805
806
/*******************************************************************
807
* output_fake_module16
808
*
809
* Create a fake 16-bit binary module.
810
*/
811
void output_fake_module16( DLLSPEC *spec )
812
{
813
static const unsigned char code_segment[] = { 0x90, 0xc3 };
814
static const unsigned char data_segment[16] = { 0 };
815
const unsigned int cseg = 2;
816
const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
817
const unsigned int segtab = lfanew + 0x40;
818
819
unsigned int i, rsrctab, restab, namelen, modtab, imptab, enttab, cbenttab, codeseg, dataseg, rsrcdata, rsrc_size = 0;
820
void *rsrc_ptr = NULL;
821
822
init_output_buffer();
823
824
rsrctab = lfanew;
825
restab = segtab + 8 * cseg;
826
if (spec->resources.count)
827
{
828
output_bin_res16_directory( spec, 0 );
829
align_output( 2 );
830
rsrctab = restab;
831
restab += output_buffer_pos;
832
free( output_buffer );
833
init_output_buffer();
834
output_bin_res16_data( spec );
835
rsrc_ptr = output_buffer;
836
rsrc_size = output_buffer_pos;
837
init_output_buffer();
838
}
839
840
namelen = strlen( spec->dll_name );
841
modtab = restab + ((namelen + 3) & ~1);
842
imptab = modtab;
843
enttab = modtab + 2;
844
cbenttab = 1;
845
codeseg = (enttab + cbenttab + 1) & ~1;
846
dataseg = codeseg + sizeof(code_segment);
847
rsrcdata = dataseg + sizeof(data_segment);
848
849
init_output_buffer();
850
851
put_word( 0x5a4d ); /* e_magic */
852
put_word( 0x40 ); /* e_cblp */
853
put_word( 0x01 ); /* e_cp */
854
put_word( 0 ); /* e_crlc */
855
put_word( lfanew / 16 ); /* e_cparhdr */
856
put_word( 0x0000 ); /* e_minalloc */
857
put_word( 0xffff ); /* e_maxalloc */
858
put_word( 0x0000 ); /* e_ss */
859
put_word( 0x00b8 ); /* e_sp */
860
put_word( 0 ); /* e_csum */
861
put_word( 0 ); /* e_ip */
862
put_word( 0 ); /* e_cs */
863
put_word( lfanew ); /* e_lfarlc */
864
put_word( 0 ); /* e_ovno */
865
put_dword( 0 ); /* e_res */
866
put_dword( 0 );
867
put_word( 0 ); /* e_oemid */
868
put_word( 0 ); /* e_oeminfo */
869
put_dword( rsrcdata + rsrc_size ); /* e_res2 */
870
put_dword( 0 );
871
put_dword( 0 );
872
put_dword( 0 );
873
put_dword( 0 );
874
put_dword( lfanew );
875
876
put_data( fakedll_signature, sizeof(fakedll_signature) );
877
align_output( 16 );
878
879
put_word( 0x454e ); /* ne_magic */
880
put_byte( 0 ); /* ne_ver */
881
put_byte( 0 ); /* ne_rev */
882
put_word( enttab - lfanew ); /* ne_enttab */
883
put_word( cbenttab ); /* ne_cbenttab */
884
put_dword( 0 ); /* ne_crc */
885
put_word( NE_FFLAGS_SINGLEDATA | /* ne_flags */
886
((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
887
put_word( 2 ); /* ne_autodata */
888
put_word( spec->heap_size ); /* ne_heap */
889
put_word( 0 ); /* ne_stack */
890
put_word( 0 ); put_word( 0 ); /* ne_csip */
891
put_word( 0 ); put_word( 2 ); /* ne_sssp */
892
put_word( cseg ); /* ne_cseg */
893
put_word( 0 ); /* ne_cmod */
894
put_word( 0 ); /* ne_cbnrestab */
895
put_word( segtab - lfanew ); /* ne_segtab */
896
put_word( rsrctab - lfanew ); /* ne_rsrctab */
897
put_word( restab - lfanew ); /* ne_restab */
898
put_word( modtab - lfanew ); /* ne_modtab */
899
put_word( imptab - lfanew ); /* ne_imptab */
900
put_dword( 0 ); /* ne_nrestab */
901
put_word( 0 ); /* ne_cmovent */
902
put_word( 0 ); /* ne_align */
903
put_word( 0 ); /* ne_cres */
904
put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
905
put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
906
put_word( 0 ); /* ne_pretthunks */
907
put_word( 0 ); /* ne_psegrefbytes */
908
put_word( 0 ); /* ne_swaparea */
909
put_word( 0 ); /* ne_expver */
910
911
/* segment table */
912
put_word( codeseg );
913
put_word( sizeof(code_segment) );
914
put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
915
put_word( sizeof(code_segment) );
916
put_word( dataseg );
917
put_word( sizeof(data_segment) );
918
put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
919
put_word( sizeof(data_segment) );
920
921
/* resource directory */
922
if (spec->resources.count)
923
{
924
output_bin_res16_directory( spec, rsrcdata );
925
align_output( 2 );
926
}
927
928
/* resident names table */
929
put_byte( namelen );
930
for (i = 0; i < namelen; i++) put_byte( toupper(spec->dll_name[i]) );
931
put_byte( 0 );
932
align_output( 2 );
933
934
/* imported names table */
935
put_word( 0 );
936
937
/* entry table */
938
put_byte( 0 );
939
align_output( 2 );
940
941
/* code segment */
942
put_data( code_segment, sizeof(code_segment) );
943
944
/* data segment */
945
put_data( data_segment, sizeof(data_segment) );
946
947
/* resource data */
948
put_data( rsrc_ptr, rsrc_size );
949
}
950
951