Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/spec32.c
8695 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_crt_sections
708
*
709
* Generate the start/end symbols for .CRT$X?? sections. The start symbol is put into
710
* .CRT$X?A, the end .CRT$X?Z. Since the linker sort .CRT$X?? sections by name, these symbols
711
* will end up at the right location.
712
*/
713
void output_crt_sections(void)
714
{
715
static const char sections[] = "ict";
716
int i;
717
718
for (i = 0; sections[i]; i++)
719
{
720
char *symbol_name = strmake( "__x%c_a", sections[i] );
721
output( "\t.section .CRT$X%cA\n", toupper( sections[i] ) );
722
output( "\t.globl %s\n", asm_name( symbol_name ) );
723
output( "\t.balign %u\n", get_ptr_size() );
724
output( "%s:\n", asm_name( symbol_name ) );
725
output( "\t%s 0\n", get_asm_ptr_keyword() );
726
727
symbol_name = strmake( "__x%c_z", sections[i] );
728
output( "\t.section .CRT$X%cZ\n", toupper( sections[i] ) );
729
output( "\t.globl %s\n", asm_name( symbol_name ) );
730
output( "\t.balign %u\n", get_ptr_size() );
731
output( "%s:\n", asm_name( symbol_name ) );
732
output( "\t%s 0\n", get_asm_ptr_keyword() );
733
}
734
}
735
736
737
/*******************************************************************
738
* output_module
739
*
740
* Output the module data.
741
*/
742
void output_module( DLLSPEC *spec )
743
{
744
int machine = 0;
745
unsigned int page_size = 0x1000;
746
const char *data_dirs[16] = { NULL };
747
748
/* Reserve some space for the PE header */
749
750
switch (target.platform)
751
{
752
case PLATFORM_MINGW:
753
case PLATFORM_WINDOWS:
754
return; /* nothing to do */
755
case PLATFORM_APPLE:
756
output( "\t.text\n" );
757
output( "\t.balign %u\n", page_size );
758
output( "__wine_spec_pe_header:\n" );
759
output( "\t.space 65536\n" );
760
break;
761
case PLATFORM_SOLARIS:
762
output( "\n\t.section \".text\",\"ax\"\n" );
763
output( "__wine_spec_pe_header:\n" );
764
output( "\t.skip %u\n", 65536 + page_size );
765
break;
766
default:
767
output( "\n\t.section \".init\",\"ax\"\n" );
768
output( "\tjmp 1f\n" );
769
output( "__wine_spec_pe_header:\n" );
770
output( "\t.skip %u\n", 65536 + page_size );
771
output( "1:\n" );
772
break;
773
}
774
775
/* Output the NT header */
776
777
output( "\n\t.data\n" );
778
output( "\t.balign %u\n", get_ptr_size() );
779
output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
780
output( "%s:\n", asm_name("__wine_spec_nt_header") );
781
output( ".L__wine_spec_rva_base:\n" );
782
783
output( "\t.long 0x4550\n" ); /* Signature */
784
switch (target.cpu)
785
{
786
case CPU_i386: machine = IMAGE_FILE_MACHINE_I386; break;
787
case CPU_ARM64EC:
788
case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
789
case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
790
case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
791
}
792
output( "\t.short 0x%04x\n", /* Machine */
793
machine );
794
output( "\t.short 0\n" ); /* NumberOfSections */
795
output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
796
output( "\t.long 0\n" ); /* PointerToSymbolTable */
797
output( "\t.long 0\n" ); /* NumberOfSymbols */
798
output( "\t.short %d\n", /* SizeOfOptionalHeader */
799
get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
800
output( "\t.short 0x%04x\n", /* Characteristics */
801
spec->characteristics );
802
output( "\t.short 0x%04x\n", /* Magic */
803
get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
804
output( "\t.byte 7\n" ); /* MajorLinkerVersion */
805
output( "\t.byte 10\n" ); /* MinorLinkerVersion */
806
output( "\t.long 0\n" ); /* SizeOfCode */
807
output( "\t.long 0\n" ); /* SizeOfInitializedData */
808
output( "\t.long 0\n" ); /* SizeOfUninitializedData */
809
810
STRARRAY_FOR_EACH( sym, &spec_extra_ld_symbols ) output( "\t.globl %s\n", asm_name(sym) );
811
812
/* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
813
output( "\t%s %s\n", /* AddressOfEntryPoint */
814
get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
815
if (get_ptr_size() == 4)
816
{
817
output( "\t.long 0\n" ); /* BaseOfCode */
818
output( "\t.long 0\n" ); /* BaseOfData */
819
}
820
output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
821
get_asm_ptr_keyword() );
822
output( "\t.long %u\n", page_size ); /* SectionAlignment */
823
output( "\t.long %u\n", page_size ); /* FileAlignment */
824
output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
825
output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
826
output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
827
spec->subsystem_major, spec->subsystem_minor );
828
output( "\t.long 0\n" ); /* Win32VersionValue */
829
output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
830
output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
831
output( "\t.long 0\n" ); /* CheckSum */
832
output( "\t.short 0x%04x\n", /* Subsystem */
833
spec->subsystem );
834
output( "\t.short 0x%04x\n", /* DllCharacteristics */
835
spec->dll_characteristics );
836
output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
837
get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
838
output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
839
get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
840
output( "\t.long 0\n" ); /* LoaderFlags */
841
output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
842
843
if (get_exports_count( &spec->exports ))
844
data_dirs[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
845
if (has_imports())
846
data_dirs[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
847
if (spec->resources.count)
848
data_dirs[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
849
if (has_delay_imports())
850
data_dirs[13] = ".L__wine_spec_delay_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
851
852
output_data_directories( data_dirs );
853
854
if (target.platform == PLATFORM_APPLE)
855
output( "\t.lcomm %s,4\n", asm_name("_end") );
856
}
857
858
859
/*******************************************************************
860
* output_spec32_file
861
*
862
* Build a Win32 C file from a spec file.
863
*/
864
void output_spec32_file( DLLSPEC *spec )
865
{
866
needs_get_pc_thunk = 0;
867
open_output_file();
868
output_standard_file_header();
869
output_module( spec );
870
output_stubs( spec );
871
output_exports( spec );
872
output_imports( spec );
873
if (needs_get_pc_thunk) output_get_pc_thunk();
874
output_load_config();
875
output_crt_sections();
876
output_resources( spec );
877
output_gnu_stack_note();
878
close_output_file();
879
}
880
881
882
struct sec_data
883
{
884
char name[8];
885
const void *ptr;
886
unsigned int size;
887
unsigned int flags;
888
unsigned int file_size;
889
unsigned int virt_size;
890
unsigned int rva;
891
};
892
893
struct dir_data
894
{
895
unsigned int rva;
896
unsigned int size;
897
};
898
899
struct exp_data
900
{
901
unsigned int rva;
902
const char *name;
903
};
904
905
static struct
906
{
907
unsigned int section_align;
908
unsigned int file_align;
909
unsigned int sec_count;
910
unsigned int exp_count;
911
struct dir_data dir[16];
912
struct sec_data sec[8];
913
struct exp_data exp[8];
914
} pe;
915
916
static void set_dir( unsigned int idx, unsigned int rva, unsigned int size )
917
{
918
pe.dir[idx].rva = rva;
919
pe.dir[idx].size = size;
920
}
921
922
static void add_export( unsigned int rva, const char *name )
923
{
924
pe.exp[pe.exp_count].rva = rva;
925
pe.exp[pe.exp_count].name = name;
926
pe.exp_count++;
927
}
928
929
static unsigned int current_rva(void)
930
{
931
if (!pe.sec_count) return pe.section_align;
932
return pe.sec[pe.sec_count - 1].rva + pe.sec[pe.sec_count - 1].virt_size;
933
}
934
935
static unsigned int align_pos( unsigned int pos, unsigned int align )
936
{
937
return (pos + align - 1) & ~(align - 1);
938
}
939
940
static unsigned int flush_output_to_section( const char *name, int dir_idx, unsigned int flags )
941
{
942
struct sec_data *sec = &pe.sec[pe.sec_count];
943
944
if (!output_buffer_pos) return 0;
945
946
memset( sec->name, 0, sizeof(sec->name) );
947
memcpy( sec->name, name, min( strlen(name), sizeof(sec->name) ));
948
sec->ptr = output_buffer;
949
sec->size = output_buffer_pos;
950
sec->flags = flags;
951
sec->rva = current_rva();
952
sec->file_size = align_pos( sec->size, pe.file_align );
953
sec->virt_size = align_pos( sec->size, pe.section_align );
954
if (dir_idx >= 0) set_dir( dir_idx, sec->rva, sec->size );
955
init_output_buffer();
956
pe.sec_count++;
957
return sec->size;
958
}
959
960
static void output_pe_exports( DLLSPEC *spec )
961
{
962
struct exports *exports = &spec->exports;
963
unsigned int i, exp_count = get_exports_count( exports );
964
unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
965
unsigned int pos, str_rva = exp_rva + 4 * exp_count + 6 * exports->nb_names;
966
967
if (!exports->nb_entry_points) return;
968
969
init_output_buffer();
970
put_dword( 0 ); /* Characteristics */
971
put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
972
put_word( 0 ); /* MajorVersion */
973
put_word( 0 ); /* MinorVersion */
974
put_dword( str_rva ); /* Name */
975
put_dword( exports->base ); /* Base */
976
put_dword( exp_count ); /* NumberOfFunctions */
977
put_dword( exports->nb_names ); /* NumberOfNames */
978
put_dword( exp_rva ); /* AddressOfFunctions */
979
if (exports->nb_names)
980
{
981
put_dword( exp_rva + 4 * exp_count ); /* AddressOfNames */
982
put_dword( exp_rva + 4 * exp_count + 4 * exports->nb_names ); /* AddressOfNameOrdinals */
983
}
984
else
985
{
986
put_dword( 0 ); /* AddressOfNames */
987
put_dword( 0 ); /* AddressOfNameOrdinals */
988
}
989
990
/* functions */
991
for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < exports->nb_names; i++)
992
pos += strlen( exports->names[i]->name ) + 1;
993
for (i = exports->base; i <= exports->limit; i++)
994
{
995
ORDDEF *odp = exports->ordinals[i];
996
if (odp && (odp->flags & FLAG_FORWARD))
997
{
998
put_dword( pos );
999
pos += strlen(odp->link_name) + 1;
1000
}
1001
else put_dword( 0 );
1002
}
1003
1004
/* names */
1005
for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < exports->nb_names; i++)
1006
{
1007
put_dword( pos );
1008
pos += strlen(exports->names[i]->name) + 1;
1009
}
1010
1011
/* ordinals */
1012
for (i = 0; i < exports->nb_names; i++) put_word( exports->names[i]->ordinal - exports->base );
1013
1014
/* strings */
1015
put_data( spec->file_name, strlen(spec->file_name) + 1 );
1016
for (i = 0; i < exports->nb_names; i++)
1017
put_data( exports->names[i]->name, strlen(exports->names[i]->name) + 1 );
1018
1019
for (i = exports->base; i <= exports->limit; i++)
1020
{
1021
ORDDEF *odp = exports->ordinals[i];
1022
if (odp && (odp->flags & FLAG_FORWARD)) put_data( odp->link_name, strlen(odp->link_name) + 1 );
1023
}
1024
1025
flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1026
0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1027
}
1028
1029
1030
/* apiset hash table */
1031
struct apiset_hash_entry
1032
{
1033
unsigned int hash;
1034
unsigned int index;
1035
};
1036
1037
static int apiset_hash_cmp( const void *h1, const void *h2 )
1038
{
1039
const struct apiset_hash_entry *entry1 = h1;
1040
const struct apiset_hash_entry *entry2 = h2;
1041
1042
if (entry1->hash > entry2->hash) return 1;
1043
if (entry1->hash < entry2->hash) return -1;
1044
return 0;
1045
}
1046
1047
static void output_apiset_section( const struct apiset *apiset )
1048
{
1049
struct apiset_hash_entry *hash;
1050
unsigned int i, j, str_pos, value_pos, hash_pos, size, count = apiset->entries.count;
1051
1052
init_output_buffer();
1053
1054
value_pos = 0x1c /* header */ + count * 0x18; /* names */
1055
str_pos = value_pos;
1056
ARRAY_FOR_EACH( e, &apiset->entries, struct apiset_entry )
1057
str_pos += 0x14 * max( 1, e->val_count ); /* values */
1058
1059
hash_pos = str_pos + ((apiset->str_pos * 2 + 3) & ~3);
1060
size = hash_pos + count * 8; /* hashes */
1061
1062
/* header */
1063
1064
put_dword( 6 ); /* Version */
1065
put_dword( size ); /* Size */
1066
put_dword( 0 ); /* Flags */
1067
put_dword( count ); /* Count */
1068
put_dword( 0x1c ); /* EntryOffset */
1069
put_dword( hash_pos ); /* HashOffset */
1070
put_dword( apiset_hash_factor ); /* HashFactor */
1071
1072
/* name entries */
1073
1074
value_pos = 0x1c /* header */ + count * 0x18; /* names */
1075
ARRAY_FOR_EACH( e, &apiset->entries, struct apiset_entry )
1076
{
1077
put_dword( 1 ); /* Flags */
1078
put_dword( str_pos + e->name_off * 2 ); /* NameOffset */
1079
put_dword( e->name_len * 2 ); /* NameLength */
1080
put_dword( e->hash_len * 2 ); /* HashedLength */
1081
put_dword( value_pos ); /* ValueOffset */
1082
put_dword( max( 1, e->val_count )); /* ValueCount */
1083
value_pos += 0x14 * max( 1, e->val_count );
1084
}
1085
1086
/* values */
1087
1088
ARRAY_FOR_EACH( e, &apiset->entries, struct apiset_entry )
1089
{
1090
if (!e->val_count)
1091
{
1092
put_dword( 0 ); /* Flags */
1093
put_dword( 0 ); /* NameOffset */
1094
put_dword( 0 ); /* NameLength */
1095
put_dword( 0 ); /* ValueOffset */
1096
put_dword( 0 ); /* ValueLength */
1097
}
1098
else for (j = 0; j < e->val_count; j++)
1099
{
1100
put_dword( 0 ); /* Flags */
1101
if (e->values[j].name_off)
1102
{
1103
put_dword( str_pos + e->values[j].name_off * 2 ); /* NameOffset */
1104
put_dword( e->values[j].name_len * 2 ); /* NameLength */
1105
}
1106
else
1107
{
1108
put_dword( 0 ); /* NameOffset */
1109
put_dword( 0 ); /* NameLength */
1110
}
1111
put_dword( str_pos + e->values[j].val_off * 2 ); /* ValueOffset */
1112
put_dword( e->values[j].val_len * 2 ); /* ValueLength */
1113
}
1114
}
1115
1116
/* strings */
1117
1118
for (i = 0; i < apiset->str_pos; i++) put_word( apiset->strings[i] );
1119
align_output( 4 );
1120
1121
/* hash table */
1122
1123
hash = xmalloc( count * sizeof(*hash) );
1124
i = 0;
1125
ARRAY_FOR_EACH( e, &apiset->entries, struct apiset_entry )
1126
{
1127
hash[i].hash = e->hash;
1128
hash[i].index = i;
1129
i++;
1130
}
1131
qsort( hash, count, sizeof(*hash), apiset_hash_cmp );
1132
for (i = 0; i < count; i++)
1133
{
1134
put_dword( hash[i].hash );
1135
put_dword( hash[i].index );
1136
}
1137
free( hash );
1138
1139
flush_output_to_section( ".apiset", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1140
}
1141
1142
1143
static void output_pe_file( DLLSPEC *spec, const char signature[32] )
1144
{
1145
const unsigned int lfanew = 0x40 + 32;
1146
unsigned int i, filepos, code_size = 0, data_size = 0;
1147
1148
init_output_buffer();
1149
1150
for (i = 0; i < pe.sec_count; i++)
1151
if (pe.sec[i].flags & 0x20) /* CNT_CODE */
1152
code_size += pe.sec[i].file_size;
1153
1154
/* .rsrc section */
1155
if (spec->type == SPEC_WIN32)
1156
{
1157
output_bin_resources( spec, current_rva() );
1158
flush_output_to_section( ".rsrc", 2 /* IMAGE_DIRECTORY_ENTRY_RESOURCE */,
1159
0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1160
}
1161
1162
/* .reloc section */
1163
if (code_size)
1164
{
1165
put_dword( 0 ); /* VirtualAddress */
1166
put_dword( 0 ); /* Size */
1167
flush_output_to_section( ".reloc", 5 /* IMAGE_DIRECTORY_ENTRY_BASERELOC */,
1168
0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ );
1169
}
1170
1171
for (i = 0; i < pe.sec_count; i++)
1172
if ((pe.sec[i].flags & 0x60) == 0x40) /* CNT_INITIALIZED_DATA */
1173
data_size += pe.sec[i].file_size;
1174
1175
put_word( 0x5a4d ); /* e_magic */
1176
put_word( 0x40 ); /* e_cblp */
1177
put_word( 0x01 ); /* e_cp */
1178
put_word( 0 ); /* e_crlc */
1179
put_word( lfanew / 16 ); /* e_cparhdr */
1180
put_word( 0x0000 ); /* e_minalloc */
1181
put_word( 0xffff ); /* e_maxalloc */
1182
put_word( 0x0000 ); /* e_ss */
1183
put_word( 0x00b8 ); /* e_sp */
1184
put_word( 0 ); /* e_csum */
1185
put_word( 0 ); /* e_ip */
1186
put_word( 0 ); /* e_cs */
1187
put_word( lfanew ); /* e_lfarlc */
1188
put_word( 0 ); /* e_ovno */
1189
put_dword( 0 ); /* e_res */
1190
put_dword( 0 );
1191
put_word( 0 ); /* e_oemid */
1192
put_word( 0 ); /* e_oeminfo */
1193
put_dword( 0 ); /* e_res2 */
1194
put_dword( 0 );
1195
put_dword( 0 );
1196
put_dword( 0 );
1197
put_dword( 0 );
1198
put_dword( lfanew );
1199
1200
put_data( signature, 32 );
1201
1202
put_dword( 0x4550 ); /* Signature */
1203
switch (target.cpu)
1204
{
1205
case CPU_i386: put_word( IMAGE_FILE_MACHINE_I386 ); break;
1206
case CPU_ARM64EC:
1207
case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
1208
case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
1209
case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
1210
}
1211
put_word( pe.sec_count ); /* NumberOfSections */
1212
put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1213
put_dword( 0 ); /* PointerToSymbolTable */
1214
put_dword( 0 ); /* NumberOfSymbols */
1215
put_word( get_ptr_size() == 8 ?
1216
IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
1217
IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
1218
put_word( spec->characteristics ); /* Characteristics */
1219
put_word( get_ptr_size() == 8 ?
1220
IMAGE_NT_OPTIONAL_HDR64_MAGIC :
1221
IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
1222
put_byte( 7 ); /* MajorLinkerVersion */
1223
put_byte( 10 ); /* MinorLinkerVersion */
1224
put_dword( code_size ); /* SizeOfCode */
1225
put_dword( data_size ); /* SizeOfInitializedData */
1226
put_dword( 0 ); /* SizeOfUninitializedData */
1227
put_dword( code_size ? pe.sec[0].rva : 0 ); /* AddressOfEntryPoint */
1228
put_dword( code_size ? pe.sec[0].rva : 0 ); /* BaseOfCode */
1229
if (get_ptr_size() == 4)
1230
{
1231
put_dword( 0 ); /* BaseOfData */
1232
put_dword( 0x10000000 ); /* ImageBase */
1233
}
1234
else
1235
{
1236
put_dword( 0x80000000 ); /* ImageBase */
1237
put_dword( 0x00000001 );
1238
}
1239
put_dword( pe.section_align ); /* SectionAlignment */
1240
put_dword( pe.file_align ); /* FileAlignment */
1241
put_word( 1 ); /* MajorOperatingSystemVersion */
1242
put_word( 0 ); /* MinorOperatingSystemVersion */
1243
put_word( 0 ); /* MajorImageVersion */
1244
put_word( 0 ); /* MinorImageVersion */
1245
put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
1246
put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
1247
put_dword( 0 ); /* Win32VersionValue */
1248
put_dword( current_rva() ); /* SizeOfImage */
1249
put_dword( pe.file_align ); /* SizeOfHeaders */
1250
put_dword( 0 ); /* CheckSum */
1251
put_word( spec->subsystem ); /* Subsystem */
1252
put_word( spec->dll_characteristics ); /* DllCharacteristics */
1253
put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
1254
put_pword( pe.section_align ); /* SizeOfStackCommit */
1255
put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
1256
put_pword( pe.section_align ); /* SizeOfHeapCommit */
1257
put_dword( 0 ); /* LoaderFlags */
1258
put_dword( 16 ); /* NumberOfRvaAndSizes */
1259
1260
/* image directories */
1261
for (i = 0; i < 16; i++)
1262
{
1263
put_dword( pe.dir[i].rva ); /* VirtualAddress */
1264
put_dword( pe.dir[i].size ); /* Size */
1265
}
1266
1267
/* sections */
1268
filepos = align_pos( output_buffer_pos + pe.sec_count * 40, pe.file_align );
1269
for (i = 0; i < pe.sec_count; i++)
1270
{
1271
put_data( pe.sec[i].name, 8 ); /* Name */
1272
put_dword( pe.sec[i].size ); /* VirtualSize */
1273
put_dword( pe.sec[i].rva ); /* VirtualAddress */
1274
put_dword( pe.sec[i].file_size ); /* SizeOfRawData */
1275
put_dword( filepos ); /* PointerToRawData */
1276
put_dword( 0 ); /* PointerToRelocations */
1277
put_dword( 0 ); /* PointerToLinenumbers */
1278
put_word( 0 ); /* NumberOfRelocations */
1279
put_word( 0 ); /* NumberOfLinenumbers */
1280
put_dword( pe.sec[i].flags ); /* Characteristics */
1281
filepos += pe.sec[i].file_size;
1282
}
1283
align_output( pe.file_align );
1284
1285
/* section data */
1286
for (i = 0; i < pe.sec_count; i++)
1287
{
1288
put_data( pe.sec[i].ptr, pe.sec[i].size );
1289
align_output( pe.file_align );
1290
}
1291
1292
flush_output_buffer( output_file_name ? output_file_name : spec->file_name );
1293
}
1294
1295
/*******************************************************************
1296
* output_fake_module
1297
*
1298
* Build a fake binary module from a spec file.
1299
*/
1300
void output_fake_module( DLLSPEC *spec )
1301
{
1302
static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
1303
0xc2, 0x0c, 0x00 }; /* ret $12 */
1304
1305
static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
1306
0xc2, 0x04, 0x00 }; /* ret $4 */
1307
unsigned int i;
1308
1309
resolve_imports( spec );
1310
pe.section_align = 0x1000;
1311
pe.file_align = 0x200;
1312
init_output_buffer();
1313
1314
/* .text section */
1315
if (spec->characteristics & IMAGE_FILE_DLL) put_data( dll_code_section, sizeof(dll_code_section) );
1316
else put_data( exe_code_section, sizeof(exe_code_section) );
1317
flush_output_to_section( ".text", -1, 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ );
1318
1319
if (spec->type == SPEC_WIN16)
1320
{
1321
add_export( current_rva(), "__wine_spec_dos_header" );
1322
1323
/* .rdata section */
1324
output_fake_module16( spec );
1325
if (spec->main_module)
1326
{
1327
add_export( current_rva() + output_buffer_pos, "__wine_spec_main_module" );
1328
put_data( spec->main_module, strlen(spec->main_module) + 1 );
1329
}
1330
flush_output_to_section( ".rdata", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1331
}
1332
1333
/* .edata section */
1334
if (pe.exp_count || spec->exports.nb_entry_points)
1335
{
1336
unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
1337
unsigned int pos, str_rva = exp_rva + 10 * pe.exp_count;
1338
1339
put_dword( 0 ); /* Characteristics */
1340
put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1341
put_word( 0 ); /* MajorVersion */
1342
put_word( 0 ); /* MinorVersion */
1343
put_dword( str_rva ); /* Name */
1344
put_dword( 1 ); /* Base */
1345
put_dword( pe.exp_count ); /* NumberOfFunctions */
1346
put_dword( pe.exp_count ); /* NumberOfNames */
1347
put_dword( exp_rva ); /* AddressOfFunctions */
1348
put_dword( exp_rva + 4 * pe.exp_count ); /* AddressOfNames */
1349
put_dword( exp_rva + 8 * pe.exp_count ); /* AddressOfNameOrdinals */
1350
1351
/* functions */
1352
for (i = 0; i < pe.exp_count; i++) put_dword( pe.exp[i].rva );
1353
/* names */
1354
for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < pe.exp_count; i++)
1355
{
1356
put_dword( pos );
1357
pos += strlen( pe.exp[i].name ) + 1;
1358
}
1359
/* ordinals */
1360
for (i = 0; i < pe.exp_count; i++) put_word( i );
1361
/* strings */
1362
put_data( spec->file_name, strlen(spec->file_name) + 1 );
1363
for (i = 0; i < pe.exp_count; i++) put_data( pe.exp[i].name, strlen(pe.exp[i].name) + 1 );
1364
flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1365
0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1366
}
1367
1368
output_pe_file( spec, fakedll_signature );
1369
}
1370
1371
1372
/*******************************************************************
1373
* output_data_module
1374
*
1375
* Build a data-only module from a spec file.
1376
*/
1377
void output_data_module( DLLSPEC *spec )
1378
{
1379
pe.section_align = pe.file_align = get_section_alignment();
1380
1381
output_pe_exports( spec );
1382
if (spec->apiset.entries.count) output_apiset_section( &spec->apiset );
1383
output_pe_file( spec, builtin_signature );
1384
}
1385
1386
1387
/*******************************************************************
1388
* output_def_file
1389
*
1390
* Build a Win32 def file from a spec file.
1391
*/
1392
void output_def_file( DLLSPEC *spec, struct exports *exports, int import_only )
1393
{
1394
DLLSPEC *spec32 = NULL;
1395
const char *name;
1396
int i, total;
1397
1398
if (spec->type == SPEC_WIN16)
1399
{
1400
spec32 = alloc_dll_spec();
1401
add_16bit_exports( spec32, spec );
1402
spec = spec32;
1403
exports = &spec->exports;
1404
}
1405
1406
if (spec_file_name)
1407
output( "; File generated automatically from %s; do not edit!\n\n",
1408
spec_file_name );
1409
else
1410
output( "; File generated automatically; do not edit!\n\n" );
1411
1412
output( "LIBRARY %s\n\n", spec->file_name);
1413
output( "EXPORTS\n");
1414
1415
/* Output the exports and relay entry points */
1416
1417
for (i = total = 0; i < exports->nb_entry_points; i++)
1418
{
1419
const ORDDEF *odp = exports->entry_points[i];
1420
int is_data = 0, is_private = odp->flags & FLAG_PRIVATE;
1421
1422
if (odp->name) name = odp->name;
1423
else if (odp->export_name) name = odp->export_name;
1424
else continue;
1425
1426
if (!is_private) total++;
1427
if (import_only && odp->type == TYPE_STUB) continue;
1428
1429
if ((odp->flags & FLAG_FASTCALL) && is_pe())
1430
name = strmake( "@%s", name );
1431
1432
output( " %s", name );
1433
1434
switch(odp->type)
1435
{
1436
case TYPE_EXTERN:
1437
is_data = 1;
1438
break;
1439
case TYPE_STUB:
1440
is_private = 1;
1441
/* fall through */
1442
case TYPE_STDCALL:
1443
if (!kill_at && target.cpu == CPU_i386) output( "@%d", get_args_size( odp ));
1444
break;
1445
default:
1446
break;
1447
}
1448
if (!import_only)
1449
{
1450
if (odp->flags & FLAG_FORWARD)
1451
output( "=%s", odp->link_name );
1452
else if (strcmp(name, odp->link_name)) /* try to reduce output */
1453
output( "=%s", get_link_name( odp ));
1454
}
1455
output( " @%d", odp->ordinal );
1456
if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
1457
if (is_data) output( " DATA" );
1458
if (is_private) output( " PRIVATE" );
1459
output( "\n" );
1460
}
1461
if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1462
}
1463
1464
1465
/*******************************************************************
1466
* make_builtin_files
1467
*/
1468
void make_builtin_files( struct strarray files )
1469
{
1470
int fd;
1471
struct
1472
{
1473
unsigned short e_magic;
1474
unsigned short unused[29];
1475
unsigned int e_lfanew;
1476
} header;
1477
1478
if (strip_command.count) /* strip the files first */
1479
{
1480
struct strarray args = strip_command;
1481
strarray_addall( &args, files );
1482
spawn( args );
1483
}
1484
1485
STRARRAY_FOR_EACH( file, &files )
1486
{
1487
if ((fd = open( file, O_RDWR | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
1488
if (read( fd, &header, sizeof(header) ) == sizeof(header) && !memcmp( &header.e_magic, "MZ", 2 ))
1489
{
1490
if (header.e_lfanew < sizeof(header) + sizeof(builtin_signature))
1491
fatal_error( "%s: Not enough space (%x) for Wine signature\n", file, header.e_lfanew );
1492
write( fd, builtin_signature, sizeof(builtin_signature) );
1493
1494
if (prefer_native)
1495
{
1496
unsigned int pos = header.e_lfanew + 0x5e; /* OptionalHeader.DllCharacteristics */
1497
unsigned short dll_charact;
1498
lseek( fd, pos, SEEK_SET );
1499
if (read( fd, &dll_charact, sizeof(dll_charact) ) == sizeof(dll_charact))
1500
{
1501
dll_charact |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
1502
lseek( fd, pos, SEEK_SET );
1503
write( fd, &dll_charact, sizeof(dll_charact) );
1504
}
1505
}
1506
}
1507
else fatal_error( "%s: Unrecognized file format\n", file );
1508
close( fd );
1509
}
1510
}
1511
1512
static void fixup_elf32( const char *name, int fd, void *header, size_t header_size )
1513
{
1514
struct
1515
{
1516
unsigned char e_ident[16];
1517
unsigned short e_type;
1518
unsigned short e_machine;
1519
unsigned int e_version;
1520
unsigned int e_entry;
1521
unsigned int e_phoff;
1522
unsigned int e_shoff;
1523
unsigned int e_flags;
1524
unsigned short e_ehsize;
1525
unsigned short e_phentsize;
1526
unsigned short e_phnum;
1527
unsigned short e_shentsize;
1528
unsigned short e_shnum;
1529
unsigned short e_shstrndx;
1530
} *elf = header;
1531
struct
1532
{
1533
unsigned int p_type;
1534
unsigned int p_offset;
1535
unsigned int p_vaddr;
1536
unsigned int p_paddr;
1537
unsigned int p_filesz;
1538
unsigned int p_memsz;
1539
unsigned int p_flags;
1540
unsigned int p_align;
1541
} *phdr;
1542
struct
1543
{
1544
unsigned int d_tag;
1545
unsigned int d_val;
1546
} *dyn;
1547
1548
unsigned int i, size;
1549
1550
if (header_size < sizeof(*elf)) return;
1551
if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1552
1553
size = elf->e_phnum * elf->e_phentsize;
1554
phdr = xmalloc( size );
1555
lseek( fd, elf->e_phoff, SEEK_SET );
1556
if (read( fd, phdr, size ) != size) return;
1557
1558
for (i = 0; i < elf->e_phnum; i++)
1559
{
1560
if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1561
phdr = (void *)((char *)phdr + elf->e_phentsize);
1562
}
1563
if (i == elf->e_phnum) return;
1564
1565
dyn = xmalloc( phdr->p_filesz );
1566
lseek( fd, phdr->p_offset, SEEK_SET );
1567
if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1568
for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1569
{
1570
switch (dyn[i].d_tag)
1571
{
1572
case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1573
case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1574
case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1575
}
1576
}
1577
lseek( fd, phdr->p_offset, SEEK_SET );
1578
write( fd, dyn, phdr->p_filesz );
1579
}
1580
1581
static void fixup_elf64( const char *name, int fd, void *header, size_t header_size )
1582
{
1583
struct
1584
{
1585
unsigned char e_ident[16];
1586
unsigned short e_type;
1587
unsigned short e_machine;
1588
unsigned int e_version;
1589
unsigned __int64 e_entry;
1590
unsigned __int64 e_phoff;
1591
unsigned __int64 e_shoff;
1592
unsigned int e_flags;
1593
unsigned short e_ehsize;
1594
unsigned short e_phentsize;
1595
unsigned short e_phnum;
1596
unsigned short e_shentsize;
1597
unsigned short e_shnum;
1598
unsigned short e_shstrndx;
1599
} *elf = header;
1600
struct
1601
{
1602
unsigned int p_type;
1603
unsigned int p_flags;
1604
unsigned __int64 p_offset;
1605
unsigned __int64 p_vaddr;
1606
unsigned __int64 p_paddr;
1607
unsigned __int64 p_filesz;
1608
unsigned __int64 p_memsz;
1609
unsigned __int64 p_align;
1610
} *phdr;
1611
struct
1612
{
1613
unsigned __int64 d_tag;
1614
unsigned __int64 d_val;
1615
} *dyn;
1616
1617
unsigned int i, size;
1618
1619
if (header_size < sizeof(*elf)) return;
1620
if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1621
1622
size = elf->e_phnum * elf->e_phentsize;
1623
phdr = xmalloc( size );
1624
lseek( fd, elf->e_phoff, SEEK_SET );
1625
if (read( fd, phdr, size ) != size) return;
1626
1627
for (i = 0; i < elf->e_phnum; i++)
1628
{
1629
if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1630
phdr = (void *)((char *)phdr + elf->e_phentsize);
1631
}
1632
if (i == elf->e_phnum) return;
1633
1634
dyn = xmalloc( phdr->p_filesz );
1635
lseek( fd, phdr->p_offset, SEEK_SET );
1636
if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1637
for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1638
{
1639
switch (dyn[i].d_tag)
1640
{
1641
case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1642
case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1643
case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1644
}
1645
}
1646
lseek( fd, phdr->p_offset, SEEK_SET );
1647
write( fd, dyn, phdr->p_filesz );
1648
}
1649
1650
/*******************************************************************
1651
* fixup_constructors
1652
*/
1653
void fixup_constructors( struct strarray files )
1654
{
1655
int fd, size;
1656
unsigned int header[64];
1657
1658
STRARRAY_FOR_EACH( file, &files )
1659
{
1660
if ((fd = open( file, O_RDWR | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
1661
size = read( fd, &header, sizeof(header) );
1662
if (size > 5)
1663
{
1664
if (!memcmp( header, "\177ELF\001", 5 )) fixup_elf32( file, fd, header, size );
1665
else if (!memcmp( header, "\177ELF\002", 5 )) fixup_elf64( file, fd, header, size );
1666
}
1667
close( fd );
1668
}
1669
}
1670
1671