ntoskrnl.exe/tests: Avoid sizeof() in traces.
[wine.git] / tools / winebuild / utils.c
blob49d2468b236228b9a1303dd7e4a4c254d23d051f
1 /*
2 * Small utility functions for winebuild
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "build.h"
32 const char *temp_dir = NULL;
33 struct strarray temp_files = { 0 };
34 static const char *output_file_source_name;
36 char *strupper(char *s)
38 char *p;
39 for (p = s; *p; p++) *p = toupper(*p);
40 return s;
43 void fatal_error( const char *msg, ... )
45 va_list valist;
46 va_start( valist, msg );
47 if (input_file_name)
49 fprintf( stderr, "%s:", input_file_name );
50 if (current_line)
51 fprintf( stderr, "%d:", current_line );
52 fputc( ' ', stderr );
54 else fprintf( stderr, "winebuild: " );
55 vfprintf( stderr, msg, valist );
56 va_end( valist );
57 exit(1);
60 void fatal_perror( const char *msg, ... )
62 va_list valist;
63 va_start( valist, msg );
64 if (input_file_name)
66 fprintf( stderr, "%s:", input_file_name );
67 if (current_line)
68 fprintf( stderr, "%d:", current_line );
69 fputc( ' ', stderr );
71 vfprintf( stderr, msg, valist );
72 perror( " " );
73 va_end( valist );
74 exit(1);
77 void error( const char *msg, ... )
79 va_list valist;
80 va_start( valist, msg );
81 if (input_file_name)
83 fprintf( stderr, "%s:", input_file_name );
84 if (current_line)
85 fprintf( stderr, "%d:", current_line );
86 fputc( ' ', stderr );
88 vfprintf( stderr, msg, valist );
89 va_end( valist );
90 nb_errors++;
93 void warning( const char *msg, ... )
95 va_list valist;
97 if (!display_warnings) return;
98 va_start( valist, msg );
99 if (input_file_name)
101 fprintf( stderr, "%s:", input_file_name );
102 if (current_line)
103 fprintf( stderr, "%d:", current_line );
104 fputc( ' ', stderr );
106 fprintf( stderr, "warning: " );
107 vfprintf( stderr, msg, valist );
108 va_end( valist );
111 int output( const char *format, ... )
113 int ret;
114 va_list valist;
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;
123 static struct strarray get_tools_path(void)
125 static int done;
126 static struct strarray dirs;
128 if (!done)
130 strarray_addall( &dirs, tools_path );
131 strarray_addall( &dirs, strarray_frompath( getenv( "PATH" )));
132 done = 1;
134 return dirs;
137 /* find a binary in the path */
138 static const char *find_binary( const char *prefix, const char *name )
140 struct strarray dirs = get_tools_path();
141 unsigned int i, maxlen = 0;
142 struct stat st;
143 char *p, *file;
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 );
150 for (i = 0; i < dirs.count; i++)
152 strcpy( file, dirs.str[i] );
153 p = file + strlen(file);
154 if (p == file) *p++ = '.';
155 if (p[-1] != '/') *p++ = '/';
156 if (*prefix)
158 strcpy( p, prefix );
159 p += strlen(p);
160 *p++ = '-';
162 strcpy( p, name );
163 strcat( p, EXEEXT );
164 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
166 free( file );
167 return NULL;
170 void spawn( struct strarray args )
172 int status;
173 const char *argv0 = find_binary( NULL, args.str[0] );
175 if (argv0) args.str[0] = argv0;
176 if (verbose) strarray_trace( args );
178 if ((status = strarray_spawn( args )))
180 if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
181 else fatal_perror( "winebuild" );
182 exit( 1 );
186 static const char *find_clang_tool( struct strarray clang, const char *tool )
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;
195 strarray_addall( &args, clang );
196 strarray_add( &args, strmake( "-print-prog-name=%s", tool ));
197 if (verbose) strarray_add( &args, "-v" );
199 sout = dup( fileno(stdout) );
200 freopen( out, "w", stdout );
201 spawn( args );
202 if (sout >= 0)
204 dup2( sout, fileno(stdout) );
205 close( sout );
208 if (stat(out, &st) || !st.st_size) return NULL;
210 path = xmalloc(st.st_size + 1);
211 sout = open(out, O_RDONLY);
212 if (sout == -1) return NULL;
213 cnt = read(sout, path, st.st_size);
214 close(sout);
215 path[cnt] = 0;
216 if ((p = strchr(path, '\n'))) *p = 0;
217 /* clang returns passed command instead of full path if the tool could not be found */
218 if (!strcmp(path, tool))
220 free( path );
221 return NULL;
223 return path;
226 /* find a build tool in the path, trying the various names */
227 struct strarray find_tool( const char *name, const char * const *names )
229 struct strarray ret = empty_strarray;
230 const char *file;
231 const char *alt_names[2];
233 if (!names)
235 alt_names[0] = name;
236 alt_names[1] = NULL;
237 names = alt_names;
240 while (*names)
242 if ((file = find_binary( target_alias, *names ))) break;
243 names++;
246 if (!file)
248 if (cc_command.count) file = find_clang_tool( cc_command, name );
249 if (!file && !(file = find_binary( "llvm", name )))
251 struct strarray clang = empty_strarray;
252 strarray_add( &clang, "clang" );
253 file = find_clang_tool( clang, strmake( "llvm-%s", name ));
256 if (!file) fatal_error( "cannot find the '%s' tool\n", name );
258 strarray_add( &ret, file );
259 return ret;
262 /* find a link tool in the path */
263 struct strarray find_link_tool(void)
265 struct strarray ret = empty_strarray;
266 const char *file = NULL;
268 if (cc_command.count) file = find_clang_tool( cc_command, "lld-link" );
269 if (!file) file = find_binary( NULL, "lld-link" );
270 if (!file)
272 struct strarray clang = empty_strarray;
273 strarray_add( &clang, "clang" );
274 file = find_clang_tool( clang, "lld-link" );
277 if (!file) fatal_error( "cannot find the 'lld-link' tool\n" );
278 strarray_add( &ret, file );
279 return ret;
282 struct strarray get_as_command(void)
284 struct strarray args = empty_strarray;
285 const char *file;
286 unsigned int i;
287 int using_cc = 0;
289 if (cc_command.count)
291 strarray_addall( &args, cc_command );
292 using_cc = 1;
294 else if (as_command.count)
296 strarray_addall( &args, as_command );
298 else if ((file = find_binary( target_alias, "as" )) || (file = find_binary( target_alias, "gas ")))
300 strarray_add( &args, file );
302 else if ((file = find_binary( NULL, "clang" )))
304 strarray_add( &args, file );
305 if (target_alias)
307 strarray_add( &args, "-target" );
308 strarray_add( &args, target_alias );
310 using_cc = 1;
313 if (using_cc)
315 strarray_add( &args, "-xassembler" );
316 strarray_add( &args, "-c" );
317 if (force_pointer_size)
318 strarray_add( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
319 if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
320 if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
321 if (arch_option) strarray_add( &args, strmake("-march=%s", arch_option) );
322 for (i = 0; i < tools_path.count; i++)
323 strarray_add( &args, strmake("-B%s", tools_path.str[i] ));
324 return args;
327 if (force_pointer_size)
329 switch (target.platform)
331 case PLATFORM_APPLE:
332 strarray_add( &args, "-arch" );
333 strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
334 break;
335 default:
336 strarray_add( &args, (force_pointer_size == 8) ? "--64" : "--32" );
337 break;
341 if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
342 if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
343 return args;
346 struct strarray get_ld_command(void)
348 struct strarray args = empty_strarray;
350 if (!ld_command.count)
352 static const char * const commands[] = { "ld", "gld", NULL };
353 ld_command = find_tool( "ld", commands );
356 strarray_addall( &args, ld_command );
358 if (force_pointer_size)
360 switch (target.platform)
362 case PLATFORM_APPLE:
363 strarray_add( &args, "-arch" );
364 strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
365 break;
366 case PLATFORM_FREEBSD:
367 strarray_add( &args, "-m" );
368 strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd" );
369 break;
370 case PLATFORM_MINGW:
371 case PLATFORM_WINDOWS:
372 strarray_add( &args, "-m" );
373 strarray_add( &args, (force_pointer_size == 8) ? "i386pep" : "i386pe" );
374 break;
375 default:
376 strarray_add( &args, "-m" );
377 strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386" );
378 break;
382 if (target.cpu == CPU_ARM && !is_pe())
383 strarray_add( &args, "--no-wchar-size-warning" );
385 return args;
388 const char *get_nm_command(void)
390 if (!nm_command.count)
392 static const char * const commands[] = { "nm", "gnm", NULL };
393 nm_command = find_tool( "nm", commands );
395 if (nm_command.count > 1)
396 fatal_error( "multiple arguments in nm command not supported yet\n" );
397 return nm_command.str[0];
401 /*******************************************************************
402 * buffer management
404 * Function for reading from/writing to a memory buffer.
407 int byte_swapped = 0;
408 const char *input_buffer_filename;
409 const unsigned char *input_buffer;
410 size_t input_buffer_pos;
411 size_t input_buffer_size;
412 unsigned char *output_buffer;
413 size_t output_buffer_pos;
414 size_t output_buffer_size;
416 void init_input_buffer( const char *file )
418 if (!(input_buffer = read_file( file, &input_buffer_size ))) fatal_perror( "Cannot read %s", file );
419 if (!input_buffer_size) fatal_error( "%s is an empty file\n", file );
420 input_buffer_filename = xstrdup( file );
421 input_buffer_pos = 0;
422 byte_swapped = 0;
425 unsigned char get_byte(void)
427 if (input_buffer_pos >= input_buffer_size)
428 fatal_error( "%s is a truncated file\n", input_buffer_filename );
429 return input_buffer[input_buffer_pos++];
432 unsigned short get_word(void)
434 unsigned short ret;
436 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
437 fatal_error( "%s is a truncated file\n", input_buffer_filename );
438 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
439 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
440 input_buffer_pos += sizeof(ret);
441 return ret;
444 unsigned int get_dword(void)
446 unsigned int ret;
448 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
449 fatal_error( "%s is a truncated file\n", input_buffer_filename );
450 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
451 if (byte_swapped)
452 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
453 input_buffer_pos += sizeof(ret);
454 return ret;
457 /* pointer-sized word */
458 void put_pword( unsigned int val )
460 if (get_ptr_size() == 8) put_qword( val );
461 else put_dword( val );
464 /* output a standard header for generated files */
465 void output_standard_file_header(void)
467 if (spec_file_name)
468 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
469 else
470 output( "/* File generated automatically; do not edit! */\n" );
471 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
472 if (safe_seh)
474 output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
475 output( "\t.globl @feat.00\n" );
476 output( ".set @feat.00, 1\n" );
478 if (thumb_mode)
480 output( "\t.syntax unified\n" );
481 output( "\t.thumb\n" );
485 /* dump a byte stream into the assembly code */
486 void dump_bytes( const void *buffer, unsigned int size )
488 unsigned int i;
489 const unsigned char *ptr = buffer;
491 if (!size) return;
492 output( "\t.byte " );
493 for (i = 0; i < size - 1; i++, ptr++)
495 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
496 else output( "0x%02x,", *ptr );
498 output( "0x%02x\n", *ptr );
502 /*******************************************************************
503 * open_input_file
505 * Open a file in the given srcdir and set the input_file_name global variable.
507 FILE *open_input_file( const char *srcdir, const char *name )
509 char *fullname;
510 FILE *file = fopen( name, "r" );
512 if (!file && srcdir)
514 fullname = strmake( "%s/%s", srcdir, name );
515 file = fopen( fullname, "r" );
517 else fullname = xstrdup( name );
519 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
520 input_file_name = fullname;
521 current_line = 1;
522 return file;
526 /*******************************************************************
527 * close_input_file
529 * Close the current input file (must have been opened with open_input_file).
531 void close_input_file( FILE *file )
533 fclose( file );
534 free( input_file_name );
535 input_file_name = NULL;
536 current_line = 0;
540 /*******************************************************************
541 * open_output_file
543 void open_output_file(void)
545 if (output_file_name)
547 if (strendswith( output_file_name, ".o" ))
548 output_file_source_name = open_temp_output_file( ".s" );
549 else
550 if (!(output_file = fopen( output_file_name, "w" )))
551 fatal_error( "Unable to create output file '%s'\n", output_file_name );
553 else output_file = stdout;
557 /*******************************************************************
558 * close_output_file
560 void close_output_file(void)
562 if (!output_file || !output_file_name) return;
563 if (fclose( output_file ) < 0) fatal_perror( "fclose" );
564 if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
565 output_file = NULL;
569 /*******************************************************************
570 * open_temp_output_file
572 char *open_temp_output_file( const char *suffix )
574 char *tmp_file = make_temp_file( output_file_name, suffix );
575 if (!(output_file = fopen( tmp_file, "w" )))
576 fatal_error( "Unable to create output file '%s'\n", tmp_file );
577 return tmp_file;
581 /*******************************************************************
582 * remove_stdcall_decoration
584 * Remove a possible @xx suffix from a function name.
585 * Return the numerical value of the suffix, or -1 if none.
587 int remove_stdcall_decoration( char *name )
589 char *p, *end = strrchr( name, '@' );
590 if (!end || !end[1] || end == name) return -1;
591 if (target.cpu != CPU_i386) return -1;
592 /* make sure all the rest is digits */
593 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
594 *end = 0;
595 return atoi( end + 1 );
599 /*******************************************************************
600 * assemble_file
602 * Run a file through the assembler.
604 void assemble_file( const char *src_file, const char *obj_file )
606 struct strarray args = get_as_command();
607 strarray_add( &args, "-o" );
608 strarray_add( &args, obj_file );
609 strarray_add( &args, src_file );
610 spawn( args );
614 /*******************************************************************
615 * alloc_dll_spec
617 * Create a new dll spec file descriptor
619 DLLSPEC *alloc_dll_spec(void)
621 DLLSPEC *spec;
623 spec = xmalloc( sizeof(*spec) );
624 memset( spec, 0, sizeof(*spec) );
625 spec->type = SPEC_WIN32;
626 spec->base = MAX_ORDINALS;
627 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
628 spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
629 spec->subsystem_major = 4;
630 spec->subsystem_minor = 0;
631 spec->syscall_table = 0;
632 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
633 return spec;
637 /*******************************************************************
638 * free_dll_spec
640 * Free dll spec file descriptor
642 void free_dll_spec( DLLSPEC *spec )
644 int i;
646 for (i = 0; i < spec->nb_entry_points; i++)
648 ORDDEF *odp = &spec->entry_points[i];
649 free( odp->name );
650 free( odp->export_name );
651 free( odp->link_name );
653 free( spec->file_name );
654 free( spec->dll_name );
655 free( spec->c_name );
656 free( spec->init_func );
657 free( spec->entry_points );
658 free( spec->names );
659 free( spec->ordinals );
660 free( spec->resources );
661 free( spec );
665 /*******************************************************************
666 * make_c_identifier
668 * Map a string to a valid C identifier.
670 char *make_c_identifier( const char *str )
672 char *p, buffer[256];
674 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
676 if (isalnum(*str)) *p = *str;
677 else *p = '_';
679 *p = 0;
680 return xstrdup( buffer );
684 /*******************************************************************
685 * get_stub_name
687 * Generate an internal name for a stub entry point.
689 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
691 static char *buffer;
693 free( buffer );
694 if (odp->name || odp->export_name)
696 char *p;
697 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
698 /* make sure name is a legal C identifier */
699 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
700 if (!*p) return buffer;
701 free( buffer );
703 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
704 return buffer;
707 /* return the stdcall-decorated name for an entry point */
708 const char *get_abi_name( const ORDDEF *odp, const char *name )
710 static char *buffer;
711 char *ret;
713 if (target.cpu != CPU_i386) return name;
715 switch (odp->type)
717 case TYPE_STDCALL:
718 if (is_pe())
720 if (odp->flags & FLAG_THISCALL) return name;
721 if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", name, get_args_size( odp ));
722 else if (!kill_at) ret = strmake( "%s@%u", name, get_args_size( odp ));
723 else return name;
725 else
727 if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", name );
728 else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", name );
729 else return name;
731 break;
733 case TYPE_PASCAL:
734 if (is_pe() && !kill_at)
736 int args = get_args_size( odp );
737 if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
738 ret = strmake( "%s@%u", name, args );
740 else return name;
741 break;
743 default:
744 return name;
747 free( buffer );
748 buffer = ret;
749 return ret;
752 const char *get_link_name( const ORDDEF *odp )
754 return get_abi_name( odp, odp->link_name );
757 /*******************************************************************
758 * sort_func_list
760 * Sort a list of functions, removing duplicates.
762 int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
764 int i, j;
766 if (!count) return 0;
767 qsort( list, count, sizeof(*list), compare );
768 for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
769 return j + 1;
773 /*****************************************************************
774 * Function: get_alignment
776 * Description:
777 * According to the info page for gas, the .align directive behaves
778 * differently on different systems. On some architectures, the
779 * argument of a .align directive is the number of bytes to pad to, so
780 * to align on an 8-byte boundary you'd say
781 * .align 8
782 * On other systems, the argument is "the number of low-order zero bits
783 * that the location counter must have after advancement." So to
784 * align on an 8-byte boundary you'd say
785 * .align 3
787 * The reason gas is written this way is that it's trying to mimic
788 * native assemblers for the various architectures it runs on. gas
789 * provides other directives that work consistently across
790 * architectures, but of course we want to work on all arches with or
791 * without gas. Hence this function.
794 * Parameters:
795 * align -- the number of bytes to align to. Must be a power of 2.
797 unsigned int get_alignment(unsigned int align)
799 unsigned int n;
801 assert( !(align & (align - 1)) );
803 switch (target.cpu)
805 case CPU_i386:
806 case CPU_x86_64:
807 if (target.platform != PLATFORM_APPLE) return align;
808 /* fall through */
809 case CPU_ARM:
810 case CPU_ARM64:
811 n = 0;
812 while ((1u << n) != align) n++;
813 return n;
815 /* unreached */
816 assert(0);
817 return 0;
820 /* return the page size for the target CPU */
821 unsigned int get_page_size(void)
823 return 0x1000; /* same on all platforms */
826 /* return the total size in bytes of the arguments on the stack */
827 unsigned int get_args_size( const ORDDEF *odp )
829 int i, size;
831 for (i = size = 0; i < odp->u.func.nb_args; i++)
833 switch (odp->u.func.args[i])
835 case ARG_INT64:
836 case ARG_DOUBLE:
837 if (target.cpu == CPU_ARM) size = (size + 7) & ~7;
838 size += 8;
839 break;
840 case ARG_INT128:
841 /* int128 is passed as pointer on x86_64 */
842 if (target.cpu != CPU_x86_64)
844 size += 16;
845 break;
847 /* fall through */
848 default:
849 size += get_ptr_size();
850 break;
853 return size;
856 /* return the assembly name for a C symbol */
857 const char *asm_name( const char *sym )
859 static char *buffer;
861 switch (target.platform)
863 case PLATFORM_MINGW:
864 case PLATFORM_WINDOWS:
865 if (target.cpu != CPU_i386) return sym;
866 if (sym[0] == '@') return sym; /* fastcall */
867 /* fall through */
868 case PLATFORM_APPLE:
869 if (sym[0] == '.' && sym[1] == 'L') return sym;
870 free( buffer );
871 buffer = strmake( "_%s", sym );
872 return buffer;
873 default:
874 return sym;
878 /* return an assembly function declaration for a C function name */
879 const char *func_declaration( const char *func )
881 static char *buffer;
883 switch (target.platform)
885 case PLATFORM_APPLE:
886 return "";
887 case PLATFORM_MINGW:
888 case PLATFORM_WINDOWS:
889 free( buffer );
890 buffer = strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func),
891 thumb_mode ? "\n\t.thumb_func" : "" );
892 break;
893 default:
894 free( buffer );
895 switch (target.cpu)
897 case CPU_ARM:
898 buffer = strmake( ".type %s,%%function%s", func,
899 thumb_mode ? "\n\t.thumb_func" : "" );
900 break;
901 case CPU_ARM64:
902 buffer = strmake( ".type %s,%%function", func );
903 break;
904 default:
905 buffer = strmake( ".type %s,@function", func );
906 break;
908 break;
910 return buffer;
913 /* output a size declaration for an assembly function */
914 void output_function_size( const char *name )
916 switch (target.platform)
918 case PLATFORM_APPLE:
919 case PLATFORM_MINGW:
920 case PLATFORM_WINDOWS:
921 break;
922 default:
923 output( "\t.size %s, .-%s\n", name, name );
924 break;
928 /* output a .cfi directive */
929 void output_cfi( const char *format, ... )
931 va_list valist;
933 if (!unwind_tables) return;
934 va_start( valist, format );
935 fputc( '\t', output_file );
936 vfprintf( output_file, format, valist );
937 fputc( '\n', output_file );
938 va_end( valist );
941 /* output an RVA pointer */
942 void output_rva( const char *format, ... )
944 va_list valist;
946 va_start( valist, format );
947 switch (target.platform)
949 case PLATFORM_MINGW:
950 case PLATFORM_WINDOWS:
951 output( "\t.rva " );
952 vfprintf( output_file, format, valist );
953 fputc( '\n', output_file );
954 break;
955 default:
956 output( "\t.long " );
957 vfprintf( output_file, format, valist );
958 output( " - .L__wine_spec_rva_base\n" );
959 break;
961 va_end( valist );
964 /* output an RVA pointer or ordinal for a function thunk */
965 void output_thunk_rva( int ordinal, const char *format, ... )
967 if (ordinal == -1)
969 va_list valist;
971 va_start( valist, format );
972 switch (target.platform)
974 case PLATFORM_MINGW:
975 case PLATFORM_WINDOWS:
976 output( "\t.rva " );
977 vfprintf( output_file, format, valist );
978 fputc( '\n', output_file );
979 if (get_ptr_size() == 8) output( "\t.long 0\n" );
980 break;
981 default:
982 output( "\t%s ", get_asm_ptr_keyword() );
983 vfprintf( output_file, format, valist );
984 output( " - .L__wine_spec_rva_base\n" );
985 break;
987 va_end( valist );
989 else
991 if (get_ptr_size() == 4) output( "\t.long 0x8000%04x\n", ordinal );
992 else output( "\t.quad 0x800000000000%04x\n", ordinal );
996 /* output the GNU note for non-exec stack */
997 void output_gnu_stack_note(void)
999 switch (target.platform)
1001 case PLATFORM_MINGW:
1002 case PLATFORM_WINDOWS:
1003 case PLATFORM_APPLE:
1004 break;
1005 default:
1006 switch (target.cpu)
1008 case CPU_ARM:
1009 case CPU_ARM64:
1010 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1011 break;
1012 default:
1013 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1014 break;
1016 break;
1020 /* return a global symbol declaration for an assembly symbol */
1021 const char *asm_globl( const char *func )
1023 static char *buffer;
1025 free( buffer );
1026 switch (target.platform)
1028 case PLATFORM_APPLE:
1029 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1030 break;
1031 case PLATFORM_MINGW:
1032 case PLATFORM_WINDOWS:
1034 const char *name = asm_name( func );
1035 buffer = strmake( "\t.globl %s\n%s:", name, name );
1036 break;
1038 default:
1039 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1040 break;
1042 return buffer;
1045 const char *get_asm_ptr_keyword(void)
1047 switch(get_ptr_size())
1049 case 4: return ".long";
1050 case 8: return ".quad";
1052 assert(0);
1053 return NULL;
1056 const char *get_asm_string_keyword(void)
1058 switch (target.platform)
1060 case PLATFORM_APPLE:
1061 return ".asciz";
1062 default:
1063 return ".string";
1067 const char *get_asm_export_section(void)
1069 switch (target.platform)
1071 case PLATFORM_APPLE: return ".data";
1072 case PLATFORM_MINGW:
1073 case PLATFORM_WINDOWS: return ".section .edata";
1074 default: return ".section .data";
1078 const char *get_asm_rodata_section(void)
1080 switch (target.platform)
1082 case PLATFORM_APPLE: return ".const";
1083 default: return ".section .rodata";
1087 const char *get_asm_rsrc_section(void)
1089 switch (target.platform)
1091 case PLATFORM_APPLE: return ".data";
1092 case PLATFORM_MINGW:
1093 case PLATFORM_WINDOWS: return ".section .rsrc";
1094 default: return ".section .data";
1098 const char *get_asm_string_section(void)
1100 switch (target.platform)
1102 case PLATFORM_APPLE: return ".cstring";
1103 default: return ".section .rodata";
1107 const char *arm64_page( const char *sym )
1109 static char *buffer;
1111 switch (target.platform)
1113 case PLATFORM_APPLE:
1114 free( buffer );
1115 buffer = strmake( "%s@PAGE", sym );
1116 return buffer;
1117 default:
1118 return sym;
1122 const char *arm64_pageoff( const char *sym )
1124 static char *buffer;
1126 free( buffer );
1127 switch (target.platform)
1129 case PLATFORM_APPLE:
1130 buffer = strmake( "%s@PAGEOFF", sym );
1131 break;
1132 default:
1133 buffer = strmake( ":lo12:%s", sym );
1134 break;
1136 return buffer;