Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/spec32.c
4389 views
1
/*
2
* 32-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
#include <stdarg.h>
30
#include <string.h>
31
32
#include "build.h"
33
34
#define IMAGE_FILE_MACHINE_UNKNOWN 0
35
#define IMAGE_FILE_MACHINE_I386 0x014c
36
#define IMAGE_FILE_MACHINE_POWERPC 0x01f0
37
#define IMAGE_FILE_MACHINE_AMD64 0x8664
38
#define IMAGE_FILE_MACHINE_ARMNT 0x01c4
39
#define IMAGE_FILE_MACHINE_ARM64 0xaa64
40
41
#define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
42
#define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
43
44
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
45
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
46
#define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
47
48
int needs_get_pc_thunk = 0;
49
50
static const char builtin_signature[32] = "Wine builtin DLL";
51
static const char fakedll_signature[32] = "Wine placeholder DLL";
52
static struct strarray spec_extra_ld_symbols = { 0 }; /* list of extra symbols that ld should resolve */
53
54
/* add a symbol to the list of extra symbols that ld must resolve */
55
void add_spec_extra_ld_symbol( const char *name )
56
{
57
strarray_add( &spec_extra_ld_symbols, name );
58
}
59
60
static unsigned int hash_filename( const char *name )
61
{
62
/* FNV-1 hash */
63
unsigned int ret = 2166136261u;
64
while (*name) ret = (ret * 16777619) ^ *name++;
65
return ret;
66
}
67
68
/* check if entry point needs a relay thunk */
69
static inline int needs_relay( const ORDDEF *odp )
70
{
71
/* skip nonexistent entry points */
72
if (!odp) return 0;
73
/* skip non-functions */
74
switch (odp->type)
75
{
76
case TYPE_STDCALL:
77
case TYPE_CDECL:
78
break;
79
case TYPE_STUB:
80
if (odp->u.func.nb_args != -1) break;
81
/* fall through */
82
default:
83
return 0;
84
}
85
/* skip norelay and forward entry points */
86
if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
87
return 1;
88
}
89
90
static int is_float_arg( const ORDDEF *odp, int arg )
91
{
92
if (arg >= odp->u.func.nb_args) return 0;
93
return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
94
}
95
96
/* check if dll will output relay thunks */
97
static int has_relays( struct exports *exports )
98
{
99
int i;
100
101
if (target.cpu == CPU_ARM64EC) return 0;
102
103
for (i = exports->base; i <= exports->limit; i++)
104
{
105
ORDDEF *odp = exports->ordinals[i];
106
if (needs_relay( odp )) return 1;
107
}
108
return 0;
109
}
110
111
static int get_exports_count( struct exports *exports )
112
{
113
if (exports->base > exports->limit) return 0;
114
return exports->limit - exports->base + 1;
115
}
116
117
static int cmp_func_args( const void *p1, const void *p2 )
118
{
119
const ORDDEF *odp1 = *(const ORDDEF **)p1;
120
const ORDDEF *odp2 = *(const ORDDEF **)p2;
121
122
return odp2->u.func.nb_args - odp1->u.func.nb_args;
123
}
124
125
static void get_arg_string( ORDDEF *odp, char str[MAX_ARGUMENTS + 1] )
126
{
127
int i;
128
129
for (i = 0; i < odp->u.func.nb_args; i++)
130
{
131
switch (odp->u.func.args[i])
132
{
133
case ARG_STR: str[i] = 's'; break;
134
case ARG_WSTR: str[i] = 'w'; break;
135
case ARG_FLOAT: str[i] = 'f'; break;
136
case ARG_DOUBLE: str[i] = 'd'; break;
137
case ARG_INT64:
138
case ARG_INT128:
139
if (get_ptr_size() == 4)
140
{
141
str[i] = (odp->u.func.args[i] == ARG_INT64) ? 'j' : 'k';
142
break;
143
}
144
/* fall through */
145
case ARG_LONG:
146
case ARG_PTR:
147
default:
148
str[i] = 'i';
149
break;
150
}
151
}
152
if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) str[0] = 't';
153
if ((odp->flags & FLAG_FASTCALL) && odp->u.func.nb_args > 1) str[1] = 't';
154
155
/* append return value */
156
if (get_ptr_size() == 4 && (odp->flags & FLAG_RET64))
157
strcpy( str + i, "J" );
158
else
159
strcpy( str + i, "I" );
160
}
161
162
static void output_data_directories( const char *names[16] )
163
{
164
int i;
165
166
for (i = 0; i < 16; i++)
167
{
168
if (names[i])
169
{
170
output_rva( "%s", names[i] );
171
output( "\t.long %s_end - %s\n", names[i], names[i] );
172
}
173
else output( "\t.long 0,0\n" );
174
}
175
}
176
177
/*******************************************************************
178
* build_args_string
179
*/
180
static char *build_args_string( struct exports *exports )
181
{
182
int i, count = 0, len = 1;
183
char *p, *buffer;
184
char str[MAX_ARGUMENTS + 2];
185
ORDDEF **funcs;
186
187
funcs = xmalloc( (exports->limit + 1 - exports->base) * sizeof(*funcs) );
188
for (i = exports->base; i <= exports->limit; i++)
189
{
190
ORDDEF *odp = exports->ordinals[i];
191
192
if (!needs_relay( odp )) continue;
193
funcs[count++] = odp;
194
len += odp->u.func.nb_args + 1;
195
}
196
/* sort functions by decreasing number of arguments */
197
qsort( funcs, count, sizeof(*funcs), cmp_func_args );
198
buffer = xmalloc( len );
199
buffer[0] = 0;
200
/* build the arguments string, reusing substrings where possible */
201
for (i = 0; i < count; i++)
202
{
203
get_arg_string( funcs[i], str );
204
if (!(p = strstr( buffer, str )))
205
{
206
p = buffer + strlen( buffer );
207
strcpy( p, str );
208
}
209
funcs[i]->u.func.args_str_offset = p - buffer;
210
}
211
free( funcs );
212
return buffer;
213
}
214
215
/*******************************************************************
216
* output_relay_debug
217
*
218
* Output entry points for relay debugging
219
*/
220
static void output_relay_debug( struct exports *exports )
221
{
222
int i;
223
224
/* first the table of entry point offsets */
225
226
output( "\t%s\n", get_asm_rodata_section() );
227
output( "\t.balign 4\n" );
228
output( ".L__wine_spec_relay_entry_point_offsets:\n" );
229
230
for (i = exports->base; i <= exports->limit; i++)
231
{
232
ORDDEF *odp = exports->ordinals[i];
233
234
if (needs_relay( odp ))
235
output( "\t.long __wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
236
else
237
output( "\t.long 0\n" );
238
}
239
240
/* then the strings of argument types */
241
242
output( ".L__wine_spec_relay_args_string:\n" );
243
output( "\t%s \"%s\"\n", get_asm_string_keyword(), build_args_string( exports ));
244
245
/* then the relay thunks */
246
247
output( "\t.text\n" );
248
output( "__wine_spec_relay_entry_points:\n" );
249
output( "\tnop\n" ); /* to avoid 0 offset */
250
251
for (i = exports->base; i <= exports->limit; i++)
252
{
253
ORDDEF *odp = exports->ordinals[i];
254
255
if (!needs_relay( odp )) continue;
256
257
switch (target.cpu)
258
{
259
case CPU_i386:
260
output( "\t.balign 4\n" );
261
output( "\t.long 0x90909090,0x90909090\n" );
262
output( "__wine_spec_relay_entry_point_%d:\n", i );
263
output_cfi( ".cfi_startproc" );
264
output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
265
if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) /* add the register arguments */
266
{
267
output( "\tpopl %%eax\n" );
268
if ((odp->flags & FLAG_FASTCALL) && get_args_size( odp ) > 4) output( "\tpushl %%edx\n" );
269
output( "\tpushl %%ecx\n" );
270
output( "\tpushl %%eax\n" );
271
}
272
output( "\tpushl $%u\n", (odp->u.func.args_str_offset << 16) | (i - exports->base) );
273
output_cfi( ".cfi_adjust_cfa_offset 4" );
274
275
if (UsePIC)
276
{
277
output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
278
output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
279
needs_get_pc_thunk = 1;
280
}
281
else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
282
output( "\tpushl %%eax\n" );
283
output_cfi( ".cfi_adjust_cfa_offset 4" );
284
285
output( "\tcall *4(%%eax)\n" );
286
output_cfi( ".cfi_adjust_cfa_offset -8" );
287
if (odp->type == TYPE_STDCALL)
288
output( "\tret $%u\n", get_args_size( odp ));
289
else
290
output( "\tret\n" );
291
output_cfi( ".cfi_endproc" );
292
break;
293
294
case CPU_ARM:
295
{
296
int j, has_float = 0;
297
298
for (j = 0; j < odp->u.func.nb_args && !has_float; j++)
299
has_float = is_float_arg( odp, j );
300
301
output( "\t.balign 4\n" );
302
output( "__wine_spec_relay_entry_point_%d:\n", i );
303
output( "\t.seh_proc __wine_spec_relay_entry_point_%d\n", i );
304
output( "\tpush {r0-r3}\n" );
305
output( "\t.seh_save_regs {r0-r3}\n" );
306
if (has_float)
307
{
308
output( "\tvpush {d0-d7}\n" );
309
output( "\t.seh_save_fregs {d0-d7}\n" );
310
}
311
output( "\tpush {r4,lr}\n" );
312
output( "\t.seh_save_regs {r4,lr}\n" );
313
output( "\t.seh_endprologue\n" );
314
output( "\tmovw r1,#%u\n", i - exports->base );
315
output( "\tmovt r1,#%u\n", odp->u.func.args_str_offset );
316
output( "\tmovw r0, :lower16:.L__wine_spec_relay_descr\n" );
317
output( "\tmovt r0, :upper16:.L__wine_spec_relay_descr\n" );
318
output( "\tldr IP, [r0, #4]\n");
319
output( "\tblx IP\n");
320
output( "\tldr IP, [SP, #4]\n" );
321
output( "\tadd SP, #%u\n", 24 + (has_float ? 64 : 0) );
322
output( "\tbx IP\n");
323
output( "\t.seh_endproc\n" );
324
break;
325
}
326
327
case CPU_ARM64:
328
{
329
int stack_size = 16 * ((min(odp->u.func.nb_args, 8) + 1) / 2);
330
331
output( "\t.balign 4\n" );
332
output( "__wine_spec_relay_entry_point_%d:\n", i );
333
output( "\t.seh_proc __wine_spec_relay_entry_point_%d\n", i );
334
output( "\tstp x29, x30, [sp, #-%u]!\n", stack_size + 16 );
335
output( "\t.seh_save_fplr_x %u\n", stack_size + 16 );
336
output( "\tmov x29, sp\n" );
337
output( "\t.seh_set_fp\n" );
338
output( "\t.seh_endprologue\n" );
339
switch (stack_size)
340
{
341
case 64: output( "\tstp x6, x7, [sp, #64]\n" );
342
/* fall through */
343
case 48: output( "\tstp x4, x5, [sp, #48]\n" );
344
/* fall through */
345
case 32: output( "\tstp x2, x3, [sp, #32]\n" );
346
/* fall through */
347
case 16: output( "\tstp x0, x1, [sp, #16]\n" );
348
/* fall through */
349
default: break;
350
}
351
output( "\tadd x2, sp, #16\n");
352
output( "\tstp x8, x9, [SP,#-16]!\n" );
353
output( "\tmov w1, #%u\n", odp->u.func.args_str_offset << 16 );
354
if (i - exports->base) output( "\tadd w1, w1, #%u\n", i - exports->base );
355
output( "\tadrp x0, .L__wine_spec_relay_descr\n" );
356
output( "\tadd x0, x0, #:lo12:.L__wine_spec_relay_descr\n" );
357
output( "\tldr x3, [x0, #8]\n");
358
output( "\tblr x3\n");
359
output( "\tmov sp, x29\n" );
360
output( "\tldp x29, x30, [sp], #%u\n", stack_size + 16 );
361
output( "\tret\n");
362
output( "\t.seh_endproc\n" );
363
break;
364
}
365
366
case CPU_x86_64:
367
output( "\t.balign 4\n" );
368
output( "\t.long 0x90909090,0x90909090\n" );
369
output( "__wine_spec_relay_entry_point_%d:\n", i );
370
output_seh( ".seh_proc __wine_spec_relay_entry_point_%d", i );
371
output_seh( ".seh_endprologue" );
372
switch (odp->u.func.nb_args)
373
{
374
default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
375
/* fall through */
376
case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
377
/* fall through */
378
case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
379
/* fall through */
380
case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
381
/* fall through */
382
case 0: break;
383
}
384
output( "\tmovl $%u,%%edx\n", (odp->u.func.args_str_offset << 16) | (i - exports->base) );
385
output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
386
output( "\tcallq *8(%%rcx)\n" );
387
output( "\tret\n" );
388
output_seh( ".seh_endproc" );
389
break;
390
391
default:
392
assert(0);
393
}
394
}
395
}
396
397
/*******************************************************************
398
* output_exports
399
*
400
* Output the export table for a Win32 module.
401
*/
402
void output_exports( DLLSPEC *spec )
403
{
404
struct exports *exports = &spec->exports;
405
int i, fwd_size = 0;
406
int needs_imports = 0;
407
int needs_relay = has_relays( exports );
408
int nr_exports = get_exports_count( exports );
409
const char *func_ptr = is_pe() ? ".rva" : get_asm_ptr_keyword();
410
const char *name;
411
412
if (!nr_exports) return;
413
414
/* ARM64EC exports are more tricky than other targets. For functions implemented in ARM64EC,
415
* linker generates x86_64 thunk and relevant metadata. Use .drectve section to pass export
416
* directives to the linker. */
417
if (target.cpu == CPU_ARM64EC)
418
{
419
output( "\t.section .drectve\n" );
420
for (i = exports->base; i <= exports->limit; i++)
421
{
422
ORDDEF *odp = exports->ordinals[i];
423
const char *symbol;
424
425
if (!odp) continue;
426
if (odp->flags & FLAG_FORWARD)
427
symbol = odp->link_name;
428
else if (odp->flags & FLAG_EXT_LINK)
429
symbol = strmake( "%s_%s", asm_name("__wine_spec_ext_link"), odp->link_name );
430
else
431
symbol = asm_name( get_link_name( odp ));
432
433
output( "\t.ascii \" -export:%s=%s,@%u%s%s\"\n",
434
odp->name ? odp->name : strmake( "_noname%u", i ),
435
symbol, i,
436
odp->name ? "" : ",NONAME",
437
odp->type == TYPE_EXTERN ? ",DATA" : "" );
438
}
439
return;
440
}
441
442
output( "\n/* export table */\n\n" );
443
output( "\t%s\n", get_asm_export_section() );
444
output( "\t.balign 4\n" );
445
output( ".L__wine_spec_exports:\n" );
446
447
/* export directory header */
448
449
output( "\t.long 0\n" ); /* Characteristics */
450
output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
451
output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
452
output_rva( ".L__wine_spec_exp_names" ); /* Name */
453
output( "\t.long %u\n", exports->base ); /* Base */
454
output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
455
output( "\t.long %u\n", exports->nb_names ); /* NumberOfNames */
456
output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
457
if (exports->nb_names)
458
{
459
output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
460
output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
461
}
462
else
463
{
464
output( "\t.long 0\n" ); /* AddressOfNames */
465
output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
466
}
467
468
/* output the function pointers */
469
470
output( "\n.L__wine_spec_exports_funcs:\n" );
471
for (i = exports->base; i <= exports->limit; i++)
472
{
473
ORDDEF *odp = exports->ordinals[i];
474
if (!odp) output( "\t%s 0\n", is_pe() ? ".long" : get_asm_ptr_keyword() );
475
else if (odp->flags & FLAG_FORWARD)
476
{
477
output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr, fwd_size );
478
fwd_size += strlen(odp->link_name) + 1;
479
}
480
else if ((odp->flags & FLAG_IMPORT) && (target.cpu == CPU_i386 || target.cpu == CPU_x86_64))
481
{
482
name = odp->name ? odp->name : odp->export_name;
483
if (name) output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_imp"), name );
484
else output( "\t%s %s_%u\n", func_ptr, asm_name("__wine_spec_imp"), i );
485
needs_imports = 1;
486
}
487
else if (odp->flags & FLAG_EXT_LINK)
488
{
489
output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_ext_link"), odp->link_name );
490
}
491
else
492
{
493
output( "\t%s %s\n", func_ptr, asm_name( get_link_name( odp )));
494
}
495
}
496
497
if (exports->nb_names)
498
{
499
/* output the function name pointers */
500
501
int namepos = strlen(spec->file_name) + 1;
502
503
output( "\n.L__wine_spec_exp_name_ptrs:\n" );
504
for (i = 0; i < exports->nb_names; i++)
505
{
506
output_rva( ".L__wine_spec_exp_names + %u", namepos );
507
namepos += strlen(exports->names[i]->name) + 1;
508
}
509
510
/* output the function ordinals */
511
512
output( "\n.L__wine_spec_exp_ordinals:\n" );
513
for (i = 0; i < exports->nb_names; i++)
514
{
515
output( "\t.short %d\n", exports->names[i]->ordinal - exports->base );
516
}
517
if (exports->nb_names % 2)
518
{
519
output( "\t.short 0\n" );
520
}
521
}
522
523
if (needs_relay)
524
{
525
output( "\t.long 0xdeb90002\n" ); /* magic */
526
if (is_pe()) output_rva( ".L__wine_spec_relay_descr" );
527
else output( "\t.long 0\n" );
528
}
529
530
/* output the export name strings */
531
532
output( "\n.L__wine_spec_exp_names:\n" );
533
output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
534
for (i = 0; i < exports->nb_names; i++)
535
output( "\t%s \"%s\"\n",
536
get_asm_string_keyword(), exports->names[i]->name );
537
538
/* output forward strings */
539
540
if (fwd_size)
541
{
542
output( "\n.L__wine_spec_forwards:\n" );
543
for (i = exports->base; i <= exports->limit; i++)
544
{
545
ORDDEF *odp = exports->ordinals[i];
546
if (odp && (odp->flags & FLAG_FORWARD))
547
output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
548
}
549
}
550
551
/* output relays */
552
553
if (needs_relay)
554
{
555
if (is_pe())
556
{
557
output( "\t.data\n" );
558
output( "\t.balign %u\n", get_ptr_size() );
559
}
560
else
561
{
562
output( "\t.balign %u\n", get_ptr_size() );
563
output( ".L__wine_spec_exports_end:\n" );
564
}
565
566
output( ".L__wine_spec_relay_descr:\n" );
567
output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
568
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
569
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
570
output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
571
output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
572
output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
573
574
output_relay_debug( exports );
575
}
576
else if (!is_pe())
577
{
578
output( "\t.balign %u\n", get_ptr_size() );
579
output( ".L__wine_spec_exports_end:\n" );
580
output( "\t%s 0\n", get_asm_ptr_keyword() );
581
}
582
583
/* output import thunks */
584
585
if (!needs_imports) return;
586
output( "\t.text\n" );
587
for (i = exports->base; i <= exports->limit; i++)
588
{
589
ORDDEF *odp = exports->ordinals[i];
590
if (!odp) continue;
591
if (!(odp->flags & FLAG_IMPORT)) continue;
592
593
name = odp->name ? odp->name : odp->export_name;
594
595
output( "\t.balign 4\n" );
596
output( "\t.long 0x90909090,0x90909090\n" );
597
if (name) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name );
598
else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i );
599
600
switch (target.cpu)
601
{
602
case CPU_i386:
603
output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
604
if (UsePIC)
605
{
606
output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
607
output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp )));
608
needs_get_pc_thunk = 1;
609
}
610
else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp )));
611
break;
612
case CPU_x86_64:
613
output( "\t.byte 0x48,0x8d,0xa4,0x24,0x00,0x00,0x00,0x00\n" ); /* hotpatch prolog */
614
output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp )));
615
break;
616
default:
617
assert(0);
618
}
619
}
620
}
621
622
623
/*******************************************************************
624
* output_load_config
625
*
626
* Output the load configuration structure.
627
*/
628
static void output_load_config(void)
629
{
630
if (!is_pe()) return;
631
632
output( "\n/* load_config */\n\n" );
633
output( "\t%s\n", get_asm_rodata_section() );
634
output( "\t.globl %s\n", asm_name( "_load_config_used" ));
635
output( "\t.balign %u\n", get_ptr_size() );
636
output( "%s:\n", asm_name( "_load_config_used" ));
637
output( "\t.long %u\n", get_ptr_size() == 8 ? 0x140 : 0xc0 ); /* Size */
638
output( "\t.long 0\n" ); /* TimeDateStamp */
639
output( "\t.short 0\n" ); /* MajorVersion */
640
output( "\t.short 0\n" ); /* MinorVersion */
641
output( "\t.long 0\n" ); /* GlobalFlagsClear */
642
output( "\t.long 0\n" ); /* GlobalFlagsSet */
643
output( "\t.long 0\n" ); /* CriticalSectionDefaultTimeout */
644
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* DeCommitFreeBlockThreshold */
645
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* DeCommitTotalFreeThreshold */
646
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* LockPrefixTable */
647
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* MaximumAllocationSize */
648
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* VirtualMemoryThreshold */
649
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* ProcessAffinityMask */
650
output( "\t.long 0\n" ); /* ProcessHeapFlags */
651
output( "\t.short 0\n" ); /* CSDVersion */
652
output( "\t.short 0\n" ); /* DependentLoadFlags */
653
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* EditList */
654
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* SecurityCookie */
655
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* SEHandlerTable */
656
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* SEHandlerCount */
657
if (target.cpu == CPU_ARM64EC)
658
{
659
output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( "__guard_check_icall_fptr" ));
660
output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( "__guard_dispatch_icall_fptr" ));
661
}
662
else
663
{
664
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFCheckFunctionPointer */
665
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFDispatchFunctionPointer */
666
}
667
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFFunctionTable */
668
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFFunctionCount */
669
if (target.cpu == CPU_ARM64EC)
670
output( "\t.long %s\n", asm_name( "__guard_flags" ));
671
else
672
output( "\t.long 0\n" ); /* GuardFlags */
673
output( "\t.short 0\n" ); /* CodeIntegrity.Flags */
674
output( "\t.short 0\n" ); /* CodeIntegrity.Catalog */
675
output( "\t.long 0\n" ); /* CodeIntegrity.CatalogOffset */
676
output( "\t.long 0\n" ); /* CodeIntegrity.Reserved */
677
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardAddressTakenIatEntryTable */
678
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardAddressTakenIatEntryCount */
679
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardLongJumpTargetTable */
680
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardLongJumpTargetCount */
681
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* DynamicValueRelocTable */
682
if (target.cpu == CPU_ARM64EC)
683
output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( "__chpe_metadata" ));
684
else
685
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* CHPEMetadataPointer */
686
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardRFFailureRoutine */
687
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardRFFailureRoutineFunctionPointer */
688
output( "\t.long 0\n" ); /* DynamicValueRelocTableOffset */
689
output( "\t.short 0\n" ); /* DynamicValueRelocTableSection */
690
output( "\t.short 0\n" ); /* Reserved2 */
691
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardRFVerifyStackPointerFunctionPointer */
692
output( "\t.long 0\n" ); /* HotPatchTableOffset */
693
output( "\t.long 0\n" ); /* Reserved3 */
694
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* EnclaveConfigurationPointer */
695
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* VolatileMetadataPointer */
696
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardEHContinuationTable */
697
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardEHContinuationCount */
698
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardXFGCheckFunctionPointer */
699
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardXFGDispatchFunctionPointer */
700
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardXFGTableDispatchFunctionPointer */
701
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* CastGuardOsDeterminedFailureMode */
702
output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardMemcpyFunctionPointer */
703
}
704
705
706
/*******************************************************************
707
* output_module
708
*
709
* Output the module data.
710
*/
711
void output_module( DLLSPEC *spec )
712
{
713
int machine = 0;
714
int i;
715
unsigned int page_size = 0x1000;
716
const char *data_dirs[16] = { NULL };
717
718
/* Reserve some space for the PE header */
719
720
switch (target.platform)
721
{
722
case PLATFORM_MINGW:
723
case PLATFORM_WINDOWS:
724
return; /* nothing to do */
725
case PLATFORM_APPLE:
726
output( "\t.text\n" );
727
output( "\t.balign %u\n", page_size );
728
output( "__wine_spec_pe_header:\n" );
729
output( "\t.space 65536\n" );
730
break;
731
case PLATFORM_SOLARIS:
732
output( "\n\t.section \".text\",\"ax\"\n" );
733
output( "__wine_spec_pe_header:\n" );
734
output( "\t.skip %u\n", 65536 + page_size );
735
break;
736
default:
737
output( "\n\t.section \".init\",\"ax\"\n" );
738
output( "\tjmp 1f\n" );
739
output( "__wine_spec_pe_header:\n" );
740
output( "\t.skip %u\n", 65536 + page_size );
741
output( "1:\n" );
742
break;
743
}
744
745
/* Output the NT header */
746
747
output( "\n\t.data\n" );
748
output( "\t.balign %u\n", get_ptr_size() );
749
output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
750
output( "%s:\n", asm_name("__wine_spec_nt_header") );
751
output( ".L__wine_spec_rva_base:\n" );
752
753
output( "\t.long 0x4550\n" ); /* Signature */
754
switch (target.cpu)
755
{
756
case CPU_i386: machine = IMAGE_FILE_MACHINE_I386; break;
757
case CPU_ARM64EC:
758
case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
759
case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
760
case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
761
}
762
output( "\t.short 0x%04x\n", /* Machine */
763
machine );
764
output( "\t.short 0\n" ); /* NumberOfSections */
765
output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
766
output( "\t.long 0\n" ); /* PointerToSymbolTable */
767
output( "\t.long 0\n" ); /* NumberOfSymbols */
768
output( "\t.short %d\n", /* SizeOfOptionalHeader */
769
get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
770
output( "\t.short 0x%04x\n", /* Characteristics */
771
spec->characteristics );
772
output( "\t.short 0x%04x\n", /* Magic */
773
get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
774
output( "\t.byte 7\n" ); /* MajorLinkerVersion */
775
output( "\t.byte 10\n" ); /* MinorLinkerVersion */
776
output( "\t.long 0\n" ); /* SizeOfCode */
777
output( "\t.long 0\n" ); /* SizeOfInitializedData */
778
output( "\t.long 0\n" ); /* SizeOfUninitializedData */
779
780
for (i = 0; i < spec_extra_ld_symbols.count; i++)
781
output( "\t.globl %s\n", asm_name(spec_extra_ld_symbols.str[i]) );
782
783
/* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
784
output( "\t%s %s\n", /* AddressOfEntryPoint */
785
get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
786
if (get_ptr_size() == 4)
787
{
788
output( "\t.long 0\n" ); /* BaseOfCode */
789
output( "\t.long 0\n" ); /* BaseOfData */
790
}
791
output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
792
get_asm_ptr_keyword() );
793
output( "\t.long %u\n", page_size ); /* SectionAlignment */
794
output( "\t.long %u\n", page_size ); /* FileAlignment */
795
output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
796
output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
797
output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
798
spec->subsystem_major, spec->subsystem_minor );
799
output( "\t.long 0\n" ); /* Win32VersionValue */
800
output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
801
output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
802
output( "\t.long 0\n" ); /* CheckSum */
803
output( "\t.short 0x%04x\n", /* Subsystem */
804
spec->subsystem );
805
output( "\t.short 0x%04x\n", /* DllCharacteristics */
806
spec->dll_characteristics );
807
output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
808
get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
809
output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
810
get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
811
output( "\t.long 0\n" ); /* LoaderFlags */
812
output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
813
814
if (get_exports_count( &spec->exports ))
815
data_dirs[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
816
if (has_imports())
817
data_dirs[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
818
if (spec->nb_resources)
819
data_dirs[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
820
if (has_delay_imports())
821
data_dirs[13] = ".L__wine_spec_delay_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
822
823
output_data_directories( data_dirs );
824
825
if (target.platform == PLATFORM_APPLE)
826
output( "\t.lcomm %s,4\n", asm_name("_end") );
827
}
828
829
830
/*******************************************************************
831
* output_spec32_file
832
*
833
* Build a Win32 C file from a spec file.
834
*/
835
void output_spec32_file( DLLSPEC *spec )
836
{
837
needs_get_pc_thunk = 0;
838
open_output_file();
839
output_standard_file_header();
840
output_module( spec );
841
output_stubs( spec );
842
output_exports( spec );
843
output_imports( spec );
844
if (needs_get_pc_thunk) output_get_pc_thunk();
845
output_load_config();
846
output_resources( spec );
847
output_gnu_stack_note();
848
close_output_file();
849
}
850
851
852
struct sec_data
853
{
854
char name[8];
855
const void *ptr;
856
unsigned int size;
857
unsigned int flags;
858
unsigned int file_size;
859
unsigned int virt_size;
860
unsigned int filepos;
861
unsigned int rva;
862
};
863
864
struct dir_data
865
{
866
unsigned int rva;
867
unsigned int size;
868
};
869
870
struct exp_data
871
{
872
unsigned int rva;
873
const char *name;
874
};
875
876
static struct
877
{
878
unsigned int section_align;
879
unsigned int file_align;
880
unsigned int sec_count;
881
unsigned int exp_count;
882
struct dir_data dir[16];
883
struct sec_data sec[8];
884
struct exp_data exp[8];
885
} pe;
886
887
static void set_dir( unsigned int idx, unsigned int rva, unsigned int size )
888
{
889
pe.dir[idx].rva = rva;
890
pe.dir[idx].size = size;
891
}
892
893
static void add_export( unsigned int rva, const char *name )
894
{
895
pe.exp[pe.exp_count].rva = rva;
896
pe.exp[pe.exp_count].name = name;
897
pe.exp_count++;
898
}
899
900
static unsigned int current_rva(void)
901
{
902
if (!pe.sec_count) return pe.section_align;
903
return pe.sec[pe.sec_count - 1].rva + pe.sec[pe.sec_count - 1].virt_size;
904
}
905
906
static unsigned int current_filepos(void)
907
{
908
if (!pe.sec_count) return pe.file_align;
909
return pe.sec[pe.sec_count - 1].filepos + pe.sec[pe.sec_count - 1].file_size;
910
}
911
912
static unsigned int flush_output_to_section( const char *name, int dir_idx, unsigned int flags )
913
{
914
struct sec_data *sec = &pe.sec[pe.sec_count];
915
916
if (!output_buffer_pos) return 0;
917
918
memset( sec->name, 0, sizeof(sec->name) );
919
memcpy( sec->name, name, min( strlen(name), sizeof(sec->name) ));
920
sec->ptr = output_buffer;
921
sec->size = output_buffer_pos;
922
sec->flags = flags;
923
sec->rva = current_rva();
924
sec->filepos = current_filepos();
925
sec->file_size = (sec->size + pe.file_align - 1) & ~(pe.file_align - 1);
926
sec->virt_size = (sec->size + pe.section_align - 1) & ~(pe.section_align - 1);
927
if (dir_idx >= 0) set_dir( dir_idx, sec->rva, sec->size );
928
init_output_buffer();
929
pe.sec_count++;
930
return sec->size;
931
}
932
933
static void output_pe_exports( DLLSPEC *spec )
934
{
935
struct exports *exports = &spec->exports;
936
unsigned int i, exp_count = get_exports_count( exports );
937
unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
938
unsigned int pos, str_rva = exp_rva + 4 * exp_count + 6 * exports->nb_names;
939
940
if (!exports->nb_entry_points) return;
941
942
init_output_buffer();
943
put_dword( 0 ); /* Characteristics */
944
put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
945
put_word( 0 ); /* MajorVersion */
946
put_word( 0 ); /* MinorVersion */
947
put_dword( str_rva ); /* Name */
948
put_dword( exports->base ); /* Base */
949
put_dword( exp_count ); /* NumberOfFunctions */
950
put_dword( exports->nb_names ); /* NumberOfNames */
951
put_dword( exp_rva ); /* AddressOfFunctions */
952
if (exports->nb_names)
953
{
954
put_dword( exp_rva + 4 * exp_count ); /* AddressOfNames */
955
put_dword( exp_rva + 4 * exp_count + 4 * exports->nb_names ); /* AddressOfNameOrdinals */
956
}
957
else
958
{
959
put_dword( 0 ); /* AddressOfNames */
960
put_dword( 0 ); /* AddressOfNameOrdinals */
961
}
962
963
/* functions */
964
for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < exports->nb_names; i++)
965
pos += strlen( exports->names[i]->name ) + 1;
966
for (i = exports->base; i <= exports->limit; i++)
967
{
968
ORDDEF *odp = exports->ordinals[i];
969
if (odp && (odp->flags & FLAG_FORWARD))
970
{
971
put_dword( pos );
972
pos += strlen(odp->link_name) + 1;
973
}
974
else put_dword( 0 );
975
}
976
977
/* names */
978
for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < exports->nb_names; i++)
979
{
980
put_dword( pos );
981
pos += strlen(exports->names[i]->name) + 1;
982
}
983
984
/* ordinals */
985
for (i = 0; i < exports->nb_names; i++) put_word( exports->names[i]->ordinal - exports->base );
986
987
/* strings */
988
put_data( spec->file_name, strlen(spec->file_name) + 1 );
989
for (i = 0; i < exports->nb_names; i++)
990
put_data( exports->names[i]->name, strlen(exports->names[i]->name) + 1 );
991
992
for (i = exports->base; i <= exports->limit; i++)
993
{
994
ORDDEF *odp = exports->ordinals[i];
995
if (odp && (odp->flags & FLAG_FORWARD)) put_data( odp->link_name, strlen(odp->link_name) + 1 );
996
}
997
998
flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
999
0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1000
}
1001
1002
1003
/* apiset hash table */
1004
struct apiset_hash_entry
1005
{
1006
unsigned int hash;
1007
unsigned int index;
1008
};
1009
1010
static int apiset_hash_cmp( const void *h1, const void *h2 )
1011
{
1012
const struct apiset_hash_entry *entry1 = h1;
1013
const struct apiset_hash_entry *entry2 = h2;
1014
1015
if (entry1->hash > entry2->hash) return 1;
1016
if (entry1->hash < entry2->hash) return -1;
1017
return 0;
1018
}
1019
1020
static void output_apiset_section( const struct apiset *apiset )
1021
{
1022
struct apiset_hash_entry *hash;
1023
struct apiset_entry *e;
1024
unsigned int i, j, str_pos, value_pos, hash_pos, size;
1025
1026
init_output_buffer();
1027
1028
value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
1029
str_pos = value_pos;
1030
for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1031
str_pos += 0x14 * max( 1, e->val_count ); /* values */
1032
1033
hash_pos = str_pos + ((apiset->str_pos * 2 + 3) & ~3);
1034
size = hash_pos + apiset->count * 8; /* hashes */
1035
1036
/* header */
1037
1038
put_dword( 6 ); /* Version */
1039
put_dword( size ); /* Size */
1040
put_dword( 0 ); /* Flags */
1041
put_dword( apiset->count ); /* Count */
1042
put_dword( 0x1c ); /* EntryOffset */
1043
put_dword( hash_pos ); /* HashOffset */
1044
put_dword( apiset_hash_factor ); /* HashFactor */
1045
1046
/* name entries */
1047
1048
value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
1049
for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1050
{
1051
put_dword( 1 ); /* Flags */
1052
put_dword( str_pos + e->name_off * 2 ); /* NameOffset */
1053
put_dword( e->name_len * 2 ); /* NameLength */
1054
put_dword( e->hash_len * 2 ); /* HashedLength */
1055
put_dword( value_pos ); /* ValueOffset */
1056
put_dword( max( 1, e->val_count )); /* ValueCount */
1057
value_pos += 0x14 * max( 1, e->val_count );
1058
}
1059
1060
/* values */
1061
1062
for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1063
{
1064
if (!e->val_count)
1065
{
1066
put_dword( 0 ); /* Flags */
1067
put_dword( 0 ); /* NameOffset */
1068
put_dword( 0 ); /* NameLength */
1069
put_dword( 0 ); /* ValueOffset */
1070
put_dword( 0 ); /* ValueLength */
1071
}
1072
else for (j = 0; j < e->val_count; j++)
1073
{
1074
put_dword( 0 ); /* Flags */
1075
if (e->values[j].name_off)
1076
{
1077
put_dword( str_pos + e->values[j].name_off * 2 ); /* NameOffset */
1078
put_dword( e->values[j].name_len * 2 ); /* NameLength */
1079
}
1080
else
1081
{
1082
put_dword( 0 ); /* NameOffset */
1083
put_dword( 0 ); /* NameLength */
1084
}
1085
put_dword( str_pos + e->values[j].val_off * 2 ); /* ValueOffset */
1086
put_dword( e->values[j].val_len * 2 ); /* ValueLength */
1087
}
1088
}
1089
1090
/* strings */
1091
1092
for (i = 0; i < apiset->str_pos; i++) put_word( apiset->strings[i] );
1093
align_output( 4 );
1094
1095
/* hash table */
1096
1097
hash = xmalloc( apiset->count * sizeof(*hash) );
1098
for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1099
{
1100
hash[i].hash = e->hash;
1101
hash[i].index = i;
1102
}
1103
qsort( hash, apiset->count, sizeof(*hash), apiset_hash_cmp );
1104
for (i = 0; i < apiset->count; i++)
1105
{
1106
put_dword( hash[i].hash );
1107
put_dword( hash[i].index );
1108
}
1109
free( hash );
1110
1111
flush_output_to_section( ".apiset", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1112
}
1113
1114
1115
static void output_pe_file( DLLSPEC *spec, const char signature[32] )
1116
{
1117
const unsigned int lfanew = 0x40 + 32;
1118
unsigned int i, code_size = 0, data_size = 0;
1119
1120
init_output_buffer();
1121
1122
for (i = 0; i < pe.sec_count; i++)
1123
if (pe.sec[i].flags & 0x20) /* CNT_CODE */
1124
code_size += pe.sec[i].file_size;
1125
1126
/* .rsrc section */
1127
if (spec->type == SPEC_WIN32)
1128
{
1129
output_bin_resources( spec, current_rva() );
1130
flush_output_to_section( ".rsrc", 2 /* IMAGE_DIRECTORY_ENTRY_RESOURCE */,
1131
0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1132
}
1133
1134
/* .reloc section */
1135
if (code_size)
1136
{
1137
put_dword( 0 ); /* VirtualAddress */
1138
put_dword( 0 ); /* Size */
1139
flush_output_to_section( ".reloc", 5 /* IMAGE_DIRECTORY_ENTRY_BASERELOC */,
1140
0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ );
1141
}
1142
1143
for (i = 0; i < pe.sec_count; i++)
1144
if ((pe.sec[i].flags & 0x60) == 0x40) /* CNT_INITIALIZED_DATA */
1145
data_size += pe.sec[i].file_size;
1146
1147
put_word( 0x5a4d ); /* e_magic */
1148
put_word( 0x40 ); /* e_cblp */
1149
put_word( 0x01 ); /* e_cp */
1150
put_word( 0 ); /* e_crlc */
1151
put_word( lfanew / 16 ); /* e_cparhdr */
1152
put_word( 0x0000 ); /* e_minalloc */
1153
put_word( 0xffff ); /* e_maxalloc */
1154
put_word( 0x0000 ); /* e_ss */
1155
put_word( 0x00b8 ); /* e_sp */
1156
put_word( 0 ); /* e_csum */
1157
put_word( 0 ); /* e_ip */
1158
put_word( 0 ); /* e_cs */
1159
put_word( lfanew ); /* e_lfarlc */
1160
put_word( 0 ); /* e_ovno */
1161
put_dword( 0 ); /* e_res */
1162
put_dword( 0 );
1163
put_word( 0 ); /* e_oemid */
1164
put_word( 0 ); /* e_oeminfo */
1165
put_dword( 0 ); /* e_res2 */
1166
put_dword( 0 );
1167
put_dword( 0 );
1168
put_dword( 0 );
1169
put_dword( 0 );
1170
put_dword( lfanew );
1171
1172
put_data( signature, 32 );
1173
1174
put_dword( 0x4550 ); /* Signature */
1175
switch (target.cpu)
1176
{
1177
case CPU_i386: put_word( IMAGE_FILE_MACHINE_I386 ); break;
1178
case CPU_ARM64EC:
1179
case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
1180
case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
1181
case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
1182
}
1183
put_word( pe.sec_count ); /* NumberOfSections */
1184
put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1185
put_dword( 0 ); /* PointerToSymbolTable */
1186
put_dword( 0 ); /* NumberOfSymbols */
1187
put_word( get_ptr_size() == 8 ?
1188
IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
1189
IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
1190
put_word( spec->characteristics ); /* Characteristics */
1191
put_word( get_ptr_size() == 8 ?
1192
IMAGE_NT_OPTIONAL_HDR64_MAGIC :
1193
IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
1194
put_byte( 7 ); /* MajorLinkerVersion */
1195
put_byte( 10 ); /* MinorLinkerVersion */
1196
put_dword( code_size ); /* SizeOfCode */
1197
put_dword( data_size ); /* SizeOfInitializedData */
1198
put_dword( 0 ); /* SizeOfUninitializedData */
1199
put_dword( code_size ? pe.sec[0].rva : 0 ); /* AddressOfEntryPoint */
1200
put_dword( code_size ? pe.sec[0].rva : 0 ); /* BaseOfCode */
1201
if (get_ptr_size() == 4)
1202
{
1203
put_dword( 0 ); /* BaseOfData */
1204
put_dword( 0x10000000 ); /* ImageBase */
1205
}
1206
else
1207
{
1208
put_dword( 0x80000000 ); /* ImageBase */
1209
put_dword( 0x00000001 );
1210
}
1211
put_dword( pe.section_align ); /* SectionAlignment */
1212
put_dword( pe.file_align ); /* FileAlignment */
1213
put_word( 1 ); /* MajorOperatingSystemVersion */
1214
put_word( 0 ); /* MinorOperatingSystemVersion */
1215
put_word( 0 ); /* MajorImageVersion */
1216
put_word( 0 ); /* MinorImageVersion */
1217
put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
1218
put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
1219
put_dword( 0 ); /* Win32VersionValue */
1220
put_dword( current_rva() ); /* SizeOfImage */
1221
put_dword( pe.file_align ); /* SizeOfHeaders */
1222
put_dword( 0 ); /* CheckSum */
1223
put_word( spec->subsystem ); /* Subsystem */
1224
put_word( spec->dll_characteristics ); /* DllCharacteristics */
1225
put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
1226
put_pword( pe.section_align ); /* SizeOfStackCommit */
1227
put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
1228
put_pword( pe.section_align ); /* SizeOfHeapCommit */
1229
put_dword( 0 ); /* LoaderFlags */
1230
put_dword( 16 ); /* NumberOfRvaAndSizes */
1231
1232
/* image directories */
1233
for (i = 0; i < 16; i++)
1234
{
1235
put_dword( pe.dir[i].rva ); /* VirtualAddress */
1236
put_dword( pe.dir[i].size ); /* Size */
1237
}
1238
1239
/* sections */
1240
for (i = 0; i < pe.sec_count; i++)
1241
{
1242
put_data( pe.sec[i].name, 8 ); /* Name */
1243
put_dword( pe.sec[i].size ); /* VirtualSize */
1244
put_dword( pe.sec[i].rva ); /* VirtualAddress */
1245
put_dword( pe.sec[i].file_size ); /* SizeOfRawData */
1246
put_dword( pe.sec[i].filepos ); /* PointerToRawData */
1247
put_dword( 0 ); /* PointerToRelocations */
1248
put_dword( 0 ); /* PointerToLinenumbers */
1249
put_word( 0 ); /* NumberOfRelocations */
1250
put_word( 0 ); /* NumberOfLinenumbers */
1251
put_dword( pe.sec[i].flags ); /* Characteristics */
1252
}
1253
align_output( pe.file_align );
1254
1255
/* section data */
1256
for (i = 0; i < pe.sec_count; i++)
1257
{
1258
put_data( pe.sec[i].ptr, pe.sec[i].size );
1259
align_output( pe.file_align );
1260
}
1261
1262
flush_output_buffer( output_file_name ? output_file_name : spec->file_name );
1263
}
1264
1265
/*******************************************************************
1266
* output_fake_module
1267
*
1268
* Build a fake binary module from a spec file.
1269
*/
1270
void output_fake_module( DLLSPEC *spec )
1271
{
1272
static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
1273
0xc2, 0x0c, 0x00 }; /* ret $12 */
1274
1275
static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
1276
0xc2, 0x04, 0x00 }; /* ret $4 */
1277
unsigned int i;
1278
1279
resolve_imports( spec );
1280
pe.section_align = 0x1000;
1281
pe.file_align = 0x200;
1282
init_output_buffer();
1283
1284
/* .text section */
1285
if (spec->characteristics & IMAGE_FILE_DLL) put_data( dll_code_section, sizeof(dll_code_section) );
1286
else put_data( exe_code_section, sizeof(exe_code_section) );
1287
flush_output_to_section( ".text", -1, 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ );
1288
1289
if (spec->type == SPEC_WIN16)
1290
{
1291
add_export( current_rva(), "__wine_spec_dos_header" );
1292
1293
/* .rdata section */
1294
output_fake_module16( spec );
1295
if (spec->main_module)
1296
{
1297
add_export( current_rva() + output_buffer_pos, "__wine_spec_main_module" );
1298
put_data( spec->main_module, strlen(spec->main_module) + 1 );
1299
}
1300
flush_output_to_section( ".rdata", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1301
}
1302
1303
/* .edata section */
1304
if (pe.exp_count)
1305
{
1306
unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
1307
unsigned int pos, str_rva = exp_rva + 10 * pe.exp_count;
1308
1309
put_dword( 0 ); /* Characteristics */
1310
put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1311
put_word( 0 ); /* MajorVersion */
1312
put_word( 0 ); /* MinorVersion */
1313
put_dword( str_rva ); /* Name */
1314
put_dword( 1 ); /* Base */
1315
put_dword( pe.exp_count ); /* NumberOfFunctions */
1316
put_dword( pe.exp_count ); /* NumberOfNames */
1317
put_dword( exp_rva ); /* AddressOfFunctions */
1318
put_dword( exp_rva + 4 * pe.exp_count ); /* AddressOfNames */
1319
put_dword( exp_rva + 8 * pe.exp_count ); /* AddressOfNameOrdinals */
1320
1321
/* functions */
1322
for (i = 0; i < pe.exp_count; i++) put_dword( pe.exp[i].rva );
1323
/* names */
1324
for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < pe.exp_count; i++)
1325
{
1326
put_dword( pos );
1327
pos += strlen( pe.exp[i].name ) + 1;
1328
}
1329
/* ordinals */
1330
for (i = 0; i < pe.exp_count; i++) put_word( i );
1331
/* strings */
1332
put_data( spec->file_name, strlen(spec->file_name) + 1 );
1333
for (i = 0; i < pe.exp_count; i++) put_data( pe.exp[i].name, strlen(pe.exp[i].name) + 1 );
1334
flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1335
0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1336
}
1337
1338
output_pe_file( spec, fakedll_signature );
1339
}
1340
1341
1342
/*******************************************************************
1343
* output_data_module
1344
*
1345
* Build a data-only module from a spec file.
1346
*/
1347
void output_data_module( DLLSPEC *spec )
1348
{
1349
pe.section_align = pe.file_align = get_section_alignment();
1350
1351
output_pe_exports( spec );
1352
if (spec->apiset.count) output_apiset_section( &spec->apiset );
1353
output_pe_file( spec, builtin_signature );
1354
}
1355
1356
1357
/*******************************************************************
1358
* output_def_file
1359
*
1360
* Build a Win32 def file from a spec file.
1361
*/
1362
void output_def_file( DLLSPEC *spec, struct exports *exports, int import_only )
1363
{
1364
DLLSPEC *spec32 = NULL;
1365
const char *name;
1366
int i, total;
1367
1368
if (spec->type == SPEC_WIN16)
1369
{
1370
spec32 = alloc_dll_spec();
1371
add_16bit_exports( spec32, spec );
1372
spec = spec32;
1373
exports = &spec->exports;
1374
}
1375
1376
if (spec_file_name)
1377
output( "; File generated automatically from %s; do not edit!\n\n",
1378
spec_file_name );
1379
else
1380
output( "; File generated automatically; do not edit!\n\n" );
1381
1382
output( "LIBRARY %s\n\n", spec->file_name);
1383
output( "EXPORTS\n");
1384
1385
/* Output the exports and relay entry points */
1386
1387
for (i = total = 0; i < exports->nb_entry_points; i++)
1388
{
1389
const ORDDEF *odp = exports->entry_points[i];
1390
int is_data = 0, is_private = odp->flags & FLAG_PRIVATE;
1391
1392
if (odp->name) name = odp->name;
1393
else if (odp->export_name) name = odp->export_name;
1394
else continue;
1395
1396
if (!is_private) total++;
1397
if (import_only && odp->type == TYPE_STUB) continue;
1398
1399
if ((odp->flags & FLAG_FASTCALL) && is_pe())
1400
name = strmake( "@%s", name );
1401
1402
output( " %s", name );
1403
1404
switch(odp->type)
1405
{
1406
case TYPE_EXTERN:
1407
is_data = 1;
1408
break;
1409
case TYPE_STUB:
1410
is_private = 1;
1411
/* fall through */
1412
case TYPE_STDCALL:
1413
if (!kill_at && target.cpu == CPU_i386) output( "@%d", get_args_size( odp ));
1414
break;
1415
default:
1416
break;
1417
}
1418
if (!import_only)
1419
{
1420
if (odp->flags & FLAG_FORWARD)
1421
output( "=%s", odp->link_name );
1422
else if (strcmp(name, odp->link_name)) /* try to reduce output */
1423
output( "=%s", get_link_name( odp ));
1424
}
1425
output( " @%d", odp->ordinal );
1426
if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
1427
if (is_data) output( " DATA" );
1428
if (is_private) output( " PRIVATE" );
1429
output( "\n" );
1430
}
1431
if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1432
if (spec32) free_dll_spec( spec32 );
1433
}
1434
1435
1436
/*******************************************************************
1437
* make_builtin_files
1438
*/
1439
void make_builtin_files( struct strarray files )
1440
{
1441
int i, fd;
1442
struct
1443
{
1444
unsigned short e_magic;
1445
unsigned short unused[29];
1446
unsigned int e_lfanew;
1447
} header;
1448
1449
for (i = 0; i < files.count; i++)
1450
{
1451
if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1452
fatal_perror( "Cannot open %s", files.str[i] );
1453
if (read( fd, &header, sizeof(header) ) == sizeof(header) && !memcmp( &header.e_magic, "MZ", 2 ))
1454
{
1455
if (header.e_lfanew < sizeof(header) + sizeof(builtin_signature))
1456
fatal_error( "%s: Not enough space (%x) for Wine signature\n", files.str[i], header.e_lfanew );
1457
write( fd, builtin_signature, sizeof(builtin_signature) );
1458
1459
if (prefer_native)
1460
{
1461
unsigned int pos = header.e_lfanew + 0x5e; /* OptionalHeader.DllCharacteristics */
1462
unsigned short dll_charact;
1463
lseek( fd, pos, SEEK_SET );
1464
if (read( fd, &dll_charact, sizeof(dll_charact) ) == sizeof(dll_charact))
1465
{
1466
dll_charact |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
1467
lseek( fd, pos, SEEK_SET );
1468
write( fd, &dll_charact, sizeof(dll_charact) );
1469
}
1470
}
1471
}
1472
else fatal_error( "%s: Unrecognized file format\n", files.str[i] );
1473
close( fd );
1474
}
1475
}
1476
1477
static void fixup_elf32( const char *name, int fd, void *header, size_t header_size )
1478
{
1479
struct
1480
{
1481
unsigned char e_ident[16];
1482
unsigned short e_type;
1483
unsigned short e_machine;
1484
unsigned int e_version;
1485
unsigned int e_entry;
1486
unsigned int e_phoff;
1487
unsigned int e_shoff;
1488
unsigned int e_flags;
1489
unsigned short e_ehsize;
1490
unsigned short e_phentsize;
1491
unsigned short e_phnum;
1492
unsigned short e_shentsize;
1493
unsigned short e_shnum;
1494
unsigned short e_shstrndx;
1495
} *elf = header;
1496
struct
1497
{
1498
unsigned int p_type;
1499
unsigned int p_offset;
1500
unsigned int p_vaddr;
1501
unsigned int p_paddr;
1502
unsigned int p_filesz;
1503
unsigned int p_memsz;
1504
unsigned int p_flags;
1505
unsigned int p_align;
1506
} *phdr;
1507
struct
1508
{
1509
unsigned int d_tag;
1510
unsigned int d_val;
1511
} *dyn;
1512
1513
unsigned int i, size;
1514
1515
if (header_size < sizeof(*elf)) return;
1516
if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1517
1518
size = elf->e_phnum * elf->e_phentsize;
1519
phdr = xmalloc( size );
1520
lseek( fd, elf->e_phoff, SEEK_SET );
1521
if (read( fd, phdr, size ) != size) return;
1522
1523
for (i = 0; i < elf->e_phnum; i++)
1524
{
1525
if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1526
phdr = (void *)((char *)phdr + elf->e_phentsize);
1527
}
1528
if (i == elf->e_phnum) return;
1529
1530
dyn = xmalloc( phdr->p_filesz );
1531
lseek( fd, phdr->p_offset, SEEK_SET );
1532
if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1533
for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1534
{
1535
switch (dyn[i].d_tag)
1536
{
1537
case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1538
case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1539
case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1540
}
1541
}
1542
lseek( fd, phdr->p_offset, SEEK_SET );
1543
write( fd, dyn, phdr->p_filesz );
1544
}
1545
1546
static void fixup_elf64( const char *name, int fd, void *header, size_t header_size )
1547
{
1548
struct
1549
{
1550
unsigned char e_ident[16];
1551
unsigned short e_type;
1552
unsigned short e_machine;
1553
unsigned int e_version;
1554
unsigned __int64 e_entry;
1555
unsigned __int64 e_phoff;
1556
unsigned __int64 e_shoff;
1557
unsigned int e_flags;
1558
unsigned short e_ehsize;
1559
unsigned short e_phentsize;
1560
unsigned short e_phnum;
1561
unsigned short e_shentsize;
1562
unsigned short e_shnum;
1563
unsigned short e_shstrndx;
1564
} *elf = header;
1565
struct
1566
{
1567
unsigned int p_type;
1568
unsigned int p_flags;
1569
unsigned __int64 p_offset;
1570
unsigned __int64 p_vaddr;
1571
unsigned __int64 p_paddr;
1572
unsigned __int64 p_filesz;
1573
unsigned __int64 p_memsz;
1574
unsigned __int64 p_align;
1575
} *phdr;
1576
struct
1577
{
1578
unsigned __int64 d_tag;
1579
unsigned __int64 d_val;
1580
} *dyn;
1581
1582
unsigned int i, size;
1583
1584
if (header_size < sizeof(*elf)) return;
1585
if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1586
1587
size = elf->e_phnum * elf->e_phentsize;
1588
phdr = xmalloc( size );
1589
lseek( fd, elf->e_phoff, SEEK_SET );
1590
if (read( fd, phdr, size ) != size) return;
1591
1592
for (i = 0; i < elf->e_phnum; i++)
1593
{
1594
if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1595
phdr = (void *)((char *)phdr + elf->e_phentsize);
1596
}
1597
if (i == elf->e_phnum) return;
1598
1599
dyn = xmalloc( phdr->p_filesz );
1600
lseek( fd, phdr->p_offset, SEEK_SET );
1601
if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1602
for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1603
{
1604
switch (dyn[i].d_tag)
1605
{
1606
case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1607
case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1608
case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1609
}
1610
}
1611
lseek( fd, phdr->p_offset, SEEK_SET );
1612
write( fd, dyn, phdr->p_filesz );
1613
}
1614
1615
/*******************************************************************
1616
* fixup_constructors
1617
*/
1618
void fixup_constructors( struct strarray files )
1619
{
1620
int i, fd, size;
1621
unsigned int header[64];
1622
1623
for (i = 0; i < files.count; i++)
1624
{
1625
if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1626
fatal_perror( "Cannot open %s", files.str[i] );
1627
size = read( fd, &header, sizeof(header) );
1628
if (size > 5)
1629
{
1630
if (!memcmp( header, "\177ELF\001", 5 )) fixup_elf32( files.str[i], fd, header, size );
1631
else if (!memcmp( header, "\177ELF\002", 5 )) fixup_elf64( files.str[i], fd, header, size );
1632
}
1633
close( fd );
1634
}
1635
}
1636
1637