Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/utils.c
4389 views
1
/*
2
* Small utility functions for winebuild
3
*
4
* Copyright 2000 Alexandre Julliard
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
*/
20
21
#include "config.h"
22
23
#include <assert.h>
24
#include <ctype.h>
25
#include <stdarg.h>
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
30
#include "build.h"
31
32
const char *temp_dir = NULL;
33
struct strarray temp_files = { 0 };
34
static const char *output_file_source_name;
35
36
char *strupper(char *s)
37
{
38
char *p;
39
for (p = s; *p; p++) *p = toupper(*p);
40
return s;
41
}
42
43
void fatal_error( const char *msg, ... )
44
{
45
va_list valist;
46
va_start( valist, msg );
47
if (input_file_name)
48
{
49
fprintf( stderr, "%s:", input_file_name );
50
if (current_line)
51
fprintf( stderr, "%d:", current_line );
52
fputc( ' ', stderr );
53
}
54
else fprintf( stderr, "winebuild: " );
55
vfprintf( stderr, msg, valist );
56
va_end( valist );
57
exit(1);
58
}
59
60
void fatal_perror( const char *msg, ... )
61
{
62
va_list valist;
63
va_start( valist, msg );
64
if (input_file_name)
65
{
66
fprintf( stderr, "%s:", input_file_name );
67
if (current_line)
68
fprintf( stderr, "%d:", current_line );
69
fputc( ' ', stderr );
70
}
71
vfprintf( stderr, msg, valist );
72
perror( " " );
73
va_end( valist );
74
exit(1);
75
}
76
77
void error( const char *msg, ... )
78
{
79
va_list valist;
80
va_start( valist, msg );
81
if (input_file_name)
82
{
83
fprintf( stderr, "%s:", input_file_name );
84
if (current_line)
85
fprintf( stderr, "%d:", current_line );
86
fputc( ' ', stderr );
87
}
88
vfprintf( stderr, msg, valist );
89
va_end( valist );
90
nb_errors++;
91
}
92
93
void warning( const char *msg, ... )
94
{
95
va_list valist;
96
97
if (!display_warnings) return;
98
va_start( valist, msg );
99
if (input_file_name)
100
{
101
fprintf( stderr, "%s:", input_file_name );
102
if (current_line)
103
fprintf( stderr, "%d:", current_line );
104
fputc( ' ', stderr );
105
}
106
fprintf( stderr, "warning: " );
107
vfprintf( stderr, msg, valist );
108
va_end( valist );
109
}
110
111
int output( const char *format, ... )
112
{
113
int ret;
114
va_list valist;
115
116
va_start( valist, format );
117
ret = vfprintf( output_file, format, valist );
118
va_end( valist );
119
if (ret < 0) fatal_perror( "Output error" );
120
return ret;
121
}
122
123
static struct strarray get_tools_path(void)
124
{
125
static int done;
126
static struct strarray dirs;
127
128
if (!done)
129
{
130
strarray_addall( &dirs, tools_path );
131
strarray_addall( &dirs, strarray_frompath( getenv( "PATH" )));
132
done = 1;
133
}
134
return dirs;
135
}
136
137
/* find a binary in the path */
138
static const char *find_binary( const char *prefix, const char *name )
139
{
140
struct strarray dirs = get_tools_path();
141
unsigned int i, maxlen = 0;
142
struct stat st;
143
char *p, *file;
144
145
if (strchr( name, '/' )) return name;
146
if (!prefix) prefix = "";
147
for (i = 0; i < dirs.count; i++) maxlen = max( maxlen, strlen(dirs.str[i]) + 2 );
148
file = xmalloc( maxlen + strlen(prefix) + strlen(name) + sizeof(EXEEXT) + 1 );
149
150
for (i = 0; i < dirs.count; i++)
151
{
152
strcpy( file, dirs.str[i] );
153
p = file + strlen(file);
154
if (p == file) *p++ = '.';
155
if (p[-1] != '/') *p++ = '/';
156
if (*prefix)
157
{
158
strcpy( p, prefix );
159
p += strlen(p);
160
*p++ = '-';
161
}
162
strcpy( p, name );
163
strcat( p, EXEEXT );
164
if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
165
}
166
free( file );
167
return NULL;
168
}
169
170
void spawn( struct strarray args )
171
{
172
int status;
173
const char *argv0 = find_binary( NULL, args.str[0] );
174
175
if (argv0) args.str[0] = argv0;
176
if (verbose) strarray_trace( args );
177
178
if ((status = strarray_spawn( args )))
179
{
180
if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
181
else fatal_perror( "winebuild" );
182
exit( 1 );
183
}
184
}
185
186
static const char *find_clang_tool( struct strarray clang, const char *tool )
187
{
188
const char *out = make_temp_file( "print_tool", ".out" );
189
struct strarray args = empty_strarray;
190
int sout = -1;
191
char *path, *p;
192
struct stat st;
193
size_t cnt;
194
195
strarray_addall( &args, clang );
196
if (!args.count) strarray_add( &args, "clang" );
197
strarray_add( &args, strmake( "-print-prog-name=%s", tool ));
198
if (verbose) strarray_add( &args, "-v" );
199
200
sout = dup( fileno(stdout) );
201
freopen( out, "w", stdout );
202
spawn( args );
203
if (sout >= 0)
204
{
205
dup2( sout, fileno(stdout) );
206
close( sout );
207
}
208
209
if (stat(out, &st) || !st.st_size) return NULL;
210
211
path = xmalloc(st.st_size + 1);
212
sout = open(out, O_RDONLY);
213
if (sout == -1) return NULL;
214
cnt = read(sout, path, st.st_size);
215
close(sout);
216
path[cnt] = 0;
217
if ((p = strchr(path, '\n'))) *p = 0;
218
/* clang returns passed command instead of full path if the tool could not be found */
219
if (!strcmp(path, tool))
220
{
221
free( path );
222
return NULL;
223
}
224
return path;
225
}
226
227
/* find a build tool in the path, trying the various names */
228
struct strarray find_tool( const char *name, const char * const *names )
229
{
230
struct strarray ret = empty_strarray;
231
const char *file;
232
const char *alt_names[2];
233
234
if (!names)
235
{
236
alt_names[0] = name;
237
alt_names[1] = NULL;
238
names = alt_names;
239
}
240
241
while (*names)
242
{
243
if ((file = find_binary( target_alias, *names ))) break;
244
names++;
245
}
246
247
if (!file && cc_command.count) file = find_clang_tool( cc_command, name );
248
if (!file) file = find_binary( "llvm", name );
249
if (!file) file = find_clang_tool( empty_strarray, strmake( "llvm-%s", name ));
250
if (!file) file = find_clang_tool( empty_strarray, name );
251
252
if (!file) fatal_error( "cannot find the '%s' tool\n", name );
253
254
strarray_add( &ret, file );
255
return ret;
256
}
257
258
/* find a link tool in the path */
259
struct strarray find_link_tool(void)
260
{
261
struct strarray ret = empty_strarray;
262
const char *file = NULL;
263
264
if (cc_command.count) file = find_clang_tool( cc_command, "lld-link" );
265
if (!file) file = find_binary( NULL, "lld-link" );
266
if (!file) file = find_clang_tool( empty_strarray, "lld-link" );
267
268
if (!file) fatal_error( "cannot find the 'lld-link' tool\n" );
269
strarray_add( &ret, file );
270
return ret;
271
}
272
273
struct strarray get_as_command(void)
274
{
275
struct strarray args = empty_strarray;
276
const char *file;
277
unsigned int i;
278
int using_cc = 0;
279
280
if (cc_command.count)
281
{
282
strarray_addall( &args, cc_command );
283
using_cc = 1;
284
}
285
else if (as_command.count)
286
{
287
strarray_addall( &args, as_command );
288
}
289
else if ((file = find_binary( target_alias, "as" )) || (file = find_binary( target_alias, "gas ")))
290
{
291
strarray_add( &args, file );
292
}
293
else if ((file = find_binary( NULL, "clang" )))
294
{
295
strarray_add( &args, file );
296
if (target_alias)
297
{
298
strarray_add( &args, "-target" );
299
strarray_add( &args, target_alias );
300
}
301
using_cc = 1;
302
}
303
304
if (using_cc)
305
{
306
strarray_add( &args, "-xassembler" );
307
strarray_add( &args, "-c" );
308
if (force_pointer_size)
309
strarray_add( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
310
if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
311
if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
312
if (arch_option) strarray_add( &args, strmake("-march=%s", arch_option) );
313
for (i = 0; i < tools_path.count; i++)
314
strarray_add( &args, strmake("-B%s", tools_path.str[i] ));
315
return args;
316
}
317
318
if (force_pointer_size)
319
{
320
switch (target.platform)
321
{
322
case PLATFORM_APPLE:
323
strarray_add( &args, "-arch" );
324
strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
325
break;
326
default:
327
strarray_add( &args, (force_pointer_size == 8) ? "--64" : "--32" );
328
break;
329
}
330
}
331
332
if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
333
if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
334
return args;
335
}
336
337
struct strarray get_ld_command(void)
338
{
339
struct strarray args = empty_strarray;
340
341
if (!ld_command.count)
342
{
343
static const char * const commands[] = { "ld", "gld", NULL };
344
ld_command = find_tool( "ld", commands );
345
}
346
347
strarray_addall( &args, ld_command );
348
349
if (force_pointer_size)
350
{
351
switch (target.platform)
352
{
353
case PLATFORM_APPLE:
354
strarray_add( &args, "-arch" );
355
strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
356
break;
357
case PLATFORM_FREEBSD:
358
strarray_add( &args, "-m" );
359
strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd" );
360
break;
361
case PLATFORM_MINGW:
362
case PLATFORM_WINDOWS:
363
strarray_add( &args, "-m" );
364
strarray_add( &args, (force_pointer_size == 8) ? "i386pep" : "i386pe" );
365
break;
366
default:
367
strarray_add( &args, "-m" );
368
strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386" );
369
break;
370
}
371
}
372
return args;
373
}
374
375
const char *get_nm_command(void)
376
{
377
if (!nm_command.count)
378
{
379
static const char * const commands[] = { "nm", "gnm", NULL };
380
nm_command = find_tool( "nm", commands );
381
}
382
if (nm_command.count > 1)
383
fatal_error( "multiple arguments in nm command not supported yet\n" );
384
return nm_command.str[0];
385
}
386
387
388
/*******************************************************************
389
* buffer management
390
*
391
* Function for reading from/writing to a memory buffer.
392
*/
393
394
int byte_swapped = 0;
395
const char *input_buffer_filename;
396
const unsigned char *input_buffer;
397
size_t input_buffer_pos;
398
size_t input_buffer_size;
399
unsigned char *output_buffer;
400
size_t output_buffer_pos;
401
size_t output_buffer_size;
402
403
void init_input_buffer( const char *file )
404
{
405
if (!(input_buffer = read_file( file, &input_buffer_size ))) fatal_perror( "Cannot read %s", file );
406
if (!input_buffer_size) fatal_error( "%s is an empty file\n", file );
407
input_buffer_filename = xstrdup( file );
408
input_buffer_pos = 0;
409
byte_swapped = 0;
410
}
411
412
unsigned char get_byte(void)
413
{
414
if (input_buffer_pos >= input_buffer_size)
415
fatal_error( "%s is a truncated file\n", input_buffer_filename );
416
return input_buffer[input_buffer_pos++];
417
}
418
419
unsigned short get_word(void)
420
{
421
unsigned short ret;
422
423
if (input_buffer_pos + sizeof(ret) > input_buffer_size)
424
fatal_error( "%s is a truncated file\n", input_buffer_filename );
425
memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
426
if (byte_swapped) ret = (ret << 8) | (ret >> 8);
427
input_buffer_pos += sizeof(ret);
428
return ret;
429
}
430
431
unsigned int get_dword(void)
432
{
433
unsigned int ret;
434
435
if (input_buffer_pos + sizeof(ret) > input_buffer_size)
436
fatal_error( "%s is a truncated file\n", input_buffer_filename );
437
memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
438
if (byte_swapped)
439
ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
440
input_buffer_pos += sizeof(ret);
441
return ret;
442
}
443
444
/* pointer-sized word */
445
void put_pword( unsigned int val )
446
{
447
if (get_ptr_size() == 8) put_qword( val );
448
else put_dword( val );
449
}
450
451
/* output a standard header for generated files */
452
void output_standard_file_header(void)
453
{
454
if (spec_file_name)
455
output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
456
else
457
output( "/* File generated automatically; do not edit! */\n" );
458
output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
459
if (safe_seh)
460
{
461
output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
462
output( "\t.globl @feat.00\n" );
463
output( ".set @feat.00, 1\n" );
464
}
465
}
466
467
/* dump a byte stream into the assembly code */
468
void dump_bytes( const void *buffer, unsigned int size )
469
{
470
unsigned int i;
471
const unsigned char *ptr = buffer;
472
473
if (!size) return;
474
output( "\t.byte " );
475
for (i = 0; i < size - 1; i++, ptr++)
476
{
477
if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
478
else output( "0x%02x,", *ptr );
479
}
480
output( "0x%02x\n", *ptr );
481
}
482
483
484
/*******************************************************************
485
* open_input_file
486
*
487
* Open a file in the given srcdir and set the input_file_name global variable.
488
*/
489
FILE *open_input_file( const char *srcdir, const char *name )
490
{
491
char *fullname;
492
FILE *file = fopen( name, "r" );
493
494
if (!file && srcdir)
495
{
496
fullname = strmake( "%s/%s", srcdir, name );
497
file = fopen( fullname, "r" );
498
}
499
else fullname = xstrdup( name );
500
501
if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
502
input_file_name = fullname;
503
current_line = 1;
504
return file;
505
}
506
507
508
/*******************************************************************
509
* close_input_file
510
*
511
* Close the current input file (must have been opened with open_input_file).
512
*/
513
void close_input_file( FILE *file )
514
{
515
fclose( file );
516
free( input_file_name );
517
input_file_name = NULL;
518
current_line = 0;
519
}
520
521
522
/*******************************************************************
523
* open_output_file
524
*/
525
void open_output_file(void)
526
{
527
if (output_file_name)
528
{
529
if (strendswith( output_file_name, ".o" ))
530
output_file_source_name = open_temp_output_file( ".s" );
531
else
532
if (!(output_file = fopen( output_file_name, "w" )))
533
fatal_error( "Unable to create output file '%s'\n", output_file_name );
534
}
535
else output_file = stdout;
536
}
537
538
539
/*******************************************************************
540
* close_output_file
541
*/
542
void close_output_file(void)
543
{
544
if (!output_file || !output_file_name) return;
545
if (fclose( output_file ) < 0) fatal_perror( "fclose" );
546
if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
547
output_file = NULL;
548
}
549
550
551
/*******************************************************************
552
* open_temp_output_file
553
*/
554
char *open_temp_output_file( const char *suffix )
555
{
556
char *tmp_file = make_temp_file( output_file_name, suffix );
557
if (!(output_file = fopen( tmp_file, "w" )))
558
fatal_error( "Unable to create output file '%s'\n", tmp_file );
559
return tmp_file;
560
}
561
562
563
/*******************************************************************
564
* remove_stdcall_decoration
565
*
566
* Remove a possible @xx suffix from a function name.
567
* Return the numerical value of the suffix, or -1 if none.
568
*/
569
int remove_stdcall_decoration( char *name )
570
{
571
char *p, *end = strrchr( name, '@' );
572
if (!end || !end[1] || end == name) return -1;
573
if (target.cpu != CPU_i386) return -1;
574
/* make sure all the rest is digits */
575
for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
576
*end = 0;
577
return atoi( end + 1 );
578
}
579
580
581
/*******************************************************************
582
* assemble_file
583
*
584
* Run a file through the assembler.
585
*/
586
void assemble_file( const char *src_file, const char *obj_file )
587
{
588
struct strarray args = get_as_command();
589
strarray_add( &args, "-o" );
590
strarray_add( &args, obj_file );
591
strarray_add( &args, src_file );
592
spawn( args );
593
}
594
595
596
/*******************************************************************
597
* alloc_dll_spec
598
*
599
* Create a new dll spec file descriptor
600
*/
601
DLLSPEC *alloc_dll_spec(void)
602
{
603
DLLSPEC *spec;
604
605
spec = xmalloc( sizeof(*spec) );
606
memset( spec, 0, sizeof(*spec) );
607
spec->type = SPEC_WIN32;
608
spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
609
spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
610
spec->subsystem_major = 4;
611
spec->subsystem_minor = 0;
612
spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT | IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
613
spec->exports.base = MAX_ORDINALS;
614
spec->native_exports.base = MAX_ORDINALS;
615
return spec;
616
}
617
618
619
static void free_exports( struct exports *entries )
620
{
621
free( entries->entry_points );
622
free( entries->names );
623
free( entries->ordinals );
624
}
625
626
627
/*******************************************************************
628
* free_dll_spec
629
*
630
* Free dll spec file descriptor
631
*/
632
void free_dll_spec( DLLSPEC *spec )
633
{
634
int i;
635
636
for (i = 0; i < spec->nb_entry_points; i++)
637
{
638
ORDDEF *odp = &spec->entry_points[i];
639
free( odp->name );
640
free( odp->export_name );
641
free( odp->link_name );
642
}
643
free_exports( &spec->exports );
644
free_exports( &spec->native_exports );
645
free( spec->file_name );
646
free( spec->dll_name );
647
free( spec->c_name );
648
free( spec->init_func );
649
free( spec->entry_points );
650
free( spec->resources );
651
free( spec );
652
}
653
654
655
/*******************************************************************
656
* make_c_identifier
657
*
658
* Map a string to a valid C identifier.
659
*/
660
char *make_c_identifier( const char *str )
661
{
662
char *p, buffer[256];
663
664
for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
665
{
666
if (isalnum(*str)) *p = *str;
667
else *p = '_';
668
}
669
*p = 0;
670
return xstrdup( buffer );
671
}
672
673
674
/*******************************************************************
675
* get_stub_name
676
*
677
* Generate an internal name for a stub entry point.
678
*/
679
static const char *get_stub_name( const ORDDEF *odp )
680
{
681
static char *buffer;
682
683
free( buffer );
684
if (odp->name || odp->export_name)
685
{
686
char *p;
687
buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
688
/* make sure name is a legal C identifier */
689
for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
690
if (!*p) return buffer;
691
free( buffer );
692
}
693
buffer = strmake( "__wine_stub_%d", odp->ordinal );
694
return buffer;
695
}
696
697
/* return the stdcall-decorated name for an entry point */
698
const char *get_abi_name( const ORDDEF *odp, const char *name )
699
{
700
static char *buffer;
701
char *ret;
702
703
if (target.cpu != CPU_i386) return name;
704
705
switch (odp->type)
706
{
707
case TYPE_STUB:
708
case TYPE_STDCALL:
709
if (is_pe())
710
{
711
if (odp->flags & FLAG_THISCALL) return name;
712
if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", name, get_args_size( odp ));
713
else if (!kill_at) ret = strmake( "%s@%u", name, get_args_size( odp ));
714
else return name;
715
}
716
else
717
{
718
if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", name );
719
else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", name );
720
else return name;
721
}
722
break;
723
724
case TYPE_PASCAL:
725
if (is_pe() && !kill_at)
726
{
727
int args = get_args_size( odp );
728
if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
729
ret = strmake( "%s@%u", name, args );
730
}
731
else return name;
732
break;
733
734
default:
735
return name;
736
}
737
738
free( buffer );
739
buffer = ret;
740
return ret;
741
}
742
743
const char *get_link_name( const ORDDEF *odp )
744
{
745
if (odp->type == TYPE_STUB && !(odp->flags & FLAG_SYSCALL)) return get_stub_name( odp );
746
747
return get_abi_name( odp, odp->link_name );
748
}
749
750
/*******************************************************************
751
* sort_func_list
752
*
753
* Sort a list of functions, removing duplicates.
754
*/
755
int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
756
{
757
int i, j;
758
759
if (!count) return 0;
760
qsort( list, count, sizeof(*list), compare );
761
for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
762
return j + 1;
763
}
764
765
766
/* return the section alignment for the target CPU */
767
unsigned int get_section_alignment(void)
768
{
769
switch (target.cpu)
770
{
771
case CPU_ARM64:
772
case CPU_ARM64EC:
773
return 0x10000;
774
default:
775
return 0x1000;
776
}
777
}
778
779
/* return the total size in bytes of the arguments on the stack */
780
unsigned int get_args_size( const ORDDEF *odp )
781
{
782
int i, size;
783
784
for (i = size = 0; i < odp->u.func.nb_args; i++)
785
{
786
switch (odp->u.func.args[i])
787
{
788
case ARG_INT64:
789
case ARG_DOUBLE:
790
if (target.cpu == CPU_ARM) size = (size + 7) & ~7;
791
size += 8;
792
break;
793
case ARG_INT128:
794
/* int128 is passed as pointer on x86_64 */
795
if (target.cpu != CPU_x86_64)
796
{
797
size += 16;
798
break;
799
}
800
/* fall through */
801
default:
802
size += get_ptr_size();
803
break;
804
}
805
}
806
return size;
807
}
808
809
/* return the assembly name for a C symbol */
810
const char *asm_name( const char *sym )
811
{
812
static char *buffer;
813
814
switch (target.platform)
815
{
816
case PLATFORM_MINGW:
817
case PLATFORM_WINDOWS:
818
if (target.cpu != CPU_i386) return sym;
819
if (sym[0] == '@') return sym; /* fastcall */
820
/* fall through */
821
case PLATFORM_APPLE:
822
if (sym[0] == '.' && sym[1] == 'L') return sym;
823
free( buffer );
824
buffer = strmake( "_%s", sym );
825
return buffer;
826
default:
827
return sym;
828
}
829
}
830
831
/* return the assembly name for an ARM64/ARM64EC function */
832
const char *arm64_name( const char *sym )
833
{
834
if (target.cpu == CPU_ARM64EC) return strmake( "\"#%s\"", sym );
835
return asm_name( sym );
836
}
837
838
/* return an assembly function declaration for a C function name */
839
void output_function_header( const char *func, int global )
840
{
841
const char *name = arm64_name( func );
842
843
output( "\t.text\n" );
844
845
switch (target.platform)
846
{
847
case PLATFORM_APPLE:
848
if (global) output( "\t.globl %s\n\t.private_extern %s\n", name, name );
849
break;
850
case PLATFORM_MINGW:
851
case PLATFORM_WINDOWS:
852
if (target.cpu == CPU_ARM64EC) output( ".section .text,\"xr\",discard,%s\n\t", name );
853
output( "\t.def %s\n\t.scl 2\n\t.type 32\n\t.endef\n", name );
854
if (global) output( "\t.globl %s\n", name );
855
break;
856
default:
857
output( "\t.type %s,@function\n", name );
858
if (global) output( "\t.globl %s\n\t.hidden %s\n", name, name );
859
break;
860
}
861
output( "\t.balign 4\n" );
862
output( "%s:\n", name );
863
}
864
865
/* output a size declaration for an assembly function */
866
void output_function_size( const char *name )
867
{
868
switch (target.platform)
869
{
870
case PLATFORM_APPLE:
871
case PLATFORM_MINGW:
872
case PLATFORM_WINDOWS:
873
break;
874
default:
875
output( "\t.size %s, .-%s\n", name, name );
876
break;
877
}
878
}
879
880
/* output a .cfi directive */
881
void output_cfi( const char *format, ... )
882
{
883
va_list valist;
884
885
if (!unwind_tables) return;
886
va_start( valist, format );
887
fputc( '\t', output_file );
888
vfprintf( output_file, format, valist );
889
fputc( '\n', output_file );
890
va_end( valist );
891
}
892
893
/* output a .seh directive */
894
void output_seh( const char *format, ... )
895
{
896
va_list valist;
897
898
if (!is_pe()) return;
899
va_start( valist, format );
900
fputc( '\t', output_file );
901
vfprintf( output_file, format, valist );
902
fputc( '\n', output_file );
903
va_end( valist );
904
}
905
906
/* output an RVA pointer */
907
void output_rva( const char *format, ... )
908
{
909
va_list valist;
910
911
va_start( valist, format );
912
if (is_pe())
913
{
914
output( "\t.rva " );
915
vfprintf( output_file, format, valist );
916
fputc( '\n', output_file );
917
}
918
else
919
{
920
output( "\t.long " );
921
vfprintf( output_file, format, valist );
922
output( " - .L__wine_spec_rva_base\n" );
923
}
924
va_end( valist );
925
}
926
927
/* output an RVA pointer or ordinal for a function thunk */
928
void output_thunk_rva( int ordinal, const char *format, ... )
929
{
930
if (ordinal == -1)
931
{
932
va_list valist;
933
934
va_start( valist, format );
935
if (is_pe())
936
{
937
output( "\t.rva " );
938
vfprintf( output_file, format, valist );
939
fputc( '\n', output_file );
940
if (get_ptr_size() == 8) output( "\t.long 0\n" );
941
}
942
else
943
{
944
output( "\t%s ", get_asm_ptr_keyword() );
945
vfprintf( output_file, format, valist );
946
output( " - .L__wine_spec_rva_base\n" );
947
}
948
va_end( valist );
949
}
950
else
951
{
952
if (get_ptr_size() == 4) output( "\t.long 0x8000%04x\n", ordinal );
953
else output( "\t.quad 0x800000000000%04x\n", ordinal );
954
}
955
}
956
957
/* output the GNU note for non-exec stack */
958
void output_gnu_stack_note(void)
959
{
960
switch (target.platform)
961
{
962
case PLATFORM_MINGW:
963
case PLATFORM_WINDOWS:
964
case PLATFORM_APPLE:
965
break;
966
default:
967
output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
968
break;
969
}
970
}
971
972
/* return a global symbol declaration for an assembly symbol */
973
const char *asm_globl( const char *func )
974
{
975
static char *buffer;
976
977
free( buffer );
978
switch (target.platform)
979
{
980
case PLATFORM_APPLE:
981
buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
982
break;
983
case PLATFORM_MINGW:
984
case PLATFORM_WINDOWS:
985
{
986
const char *name = asm_name( func );
987
buffer = strmake( "\t.globl %s\n%s:", name, name );
988
break;
989
}
990
default:
991
buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
992
break;
993
}
994
return buffer;
995
}
996
997
const char *get_asm_ptr_keyword(void)
998
{
999
switch(get_ptr_size())
1000
{
1001
case 4: return ".long";
1002
case 8: return ".quad";
1003
}
1004
assert(0);
1005
return NULL;
1006
}
1007
1008
const char *get_asm_string_keyword(void)
1009
{
1010
switch (target.platform)
1011
{
1012
case PLATFORM_APPLE:
1013
return ".asciz";
1014
default:
1015
return ".string";
1016
}
1017
}
1018
1019
const char *get_asm_export_section(void)
1020
{
1021
switch (target.platform)
1022
{
1023
case PLATFORM_APPLE: return ".data";
1024
case PLATFORM_MINGW:
1025
case PLATFORM_WINDOWS: return ".section .edata";
1026
default: return ".section .data";
1027
}
1028
}
1029
1030
const char *get_asm_rodata_section(void)
1031
{
1032
switch (target.platform)
1033
{
1034
case PLATFORM_APPLE: return ".const";
1035
default: return ".section .rodata";
1036
}
1037
}
1038
1039
const char *get_asm_rsrc_section(void)
1040
{
1041
switch (target.platform)
1042
{
1043
case PLATFORM_APPLE: return ".data";
1044
case PLATFORM_MINGW:
1045
case PLATFORM_WINDOWS: return ".section .rsrc";
1046
default: return ".section .data";
1047
}
1048
}
1049
1050
const char *get_asm_string_section(void)
1051
{
1052
switch (target.platform)
1053
{
1054
case PLATFORM_APPLE: return ".cstring";
1055
default: return ".section .rodata";
1056
}
1057
}
1058
1059