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
22 #include "wine/port.h"
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
39 #if defined(_WIN32) && !defined(__CYGWIN__)
40 # define PATH_SEPARATOR ';'
42 # define PATH_SEPARATOR ':'
45 static struct strarray tmp_files
;
46 static struct strarray empty_strarray
;
47 static const char *output_file_source_name
;
60 { "amd64", CPU_x86_64
},
61 { "x86_64", CPU_x86_64
},
62 { "powerpc", CPU_POWERPC
},
67 { "armv7a", CPU_ARM
},
68 { "arm64", CPU_ARM64
},
69 { "aarch64", CPU_ARM64
},
72 /* atexit handler to clean tmp files */
73 void cleanup_tmp_files(void)
76 for (i
= 0; i
< tmp_files
.count
; i
++) if (tmp_files
.str
[i
]) unlink( tmp_files
.str
[i
] );
80 void *xmalloc (size_t size
)
84 res
= malloc (size
? size
: 1);
87 fprintf (stderr
, "Virtual memory exhausted.\n");
93 void *xrealloc (void *ptr
, size_t size
)
95 void *res
= realloc (ptr
, size
);
96 if (size
&& res
== NULL
)
98 fprintf (stderr
, "Virtual memory exhausted.\n");
104 char *xstrdup( const char *str
)
106 char *res
= strdup( str
);
109 fprintf (stderr
, "Virtual memory exhausted.\n");
115 char *strupper(char *s
)
118 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
122 int strendswith(const char* str
, const char* end
)
126 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
129 char *strmake( const char* fmt
, ... )
137 char *p
= xmalloc( size
);
139 n
= vsnprintf( p
, size
, fmt
, ap
);
141 if (n
== -1) size
*= 2;
142 else if ((size_t)n
>= size
) size
= n
+ 1;
148 static struct strarray
strarray_copy( struct strarray src
)
150 struct strarray array
;
151 array
.count
= src
.count
;
153 array
.str
= xmalloc( array
.max
* sizeof(*array
.str
) );
154 memcpy( array
.str
, src
.str
, array
.count
* sizeof(*array
.str
) );
158 static void strarray_add_one( struct strarray
*array
, const char *str
)
160 if (array
->count
== array
->max
)
163 if (array
->max
< 16) array
->max
= 16;
164 array
->str
= xrealloc( array
->str
, array
->max
* sizeof(*array
->str
) );
166 array
->str
[array
->count
++] = str
;
169 void strarray_add( struct strarray
*array
, ... )
174 va_start( valist
, array
);
175 while ((str
= va_arg( valist
, const char *))) strarray_add_one( array
, str
);
179 void strarray_addv( struct strarray
*array
, char * const *argv
)
181 while (*argv
) strarray_add_one( array
, *argv
++ );
184 void strarray_addall( struct strarray
*array
, struct strarray args
)
188 for (i
= 0; i
< args
.count
; i
++) strarray_add_one( array
, args
.str
[i
] );
191 struct strarray
strarray_fromstring( const char *str
, const char *delim
)
194 struct strarray array
= empty_strarray
;
195 char *buf
= xstrdup( str
);
197 for (tok
= strtok( buf
, delim
); tok
; tok
= strtok( NULL
, delim
))
198 strarray_add_one( &array
, strdup( tok
));
204 void fatal_error( const char *msg
, ... )
207 va_start( valist
, msg
);
210 fprintf( stderr
, "%s:", input_file_name
);
212 fprintf( stderr
, "%d:", current_line
);
213 fputc( ' ', stderr
);
215 else fprintf( stderr
, "winebuild: " );
216 vfprintf( stderr
, msg
, valist
);
221 void fatal_perror( const char *msg
, ... )
224 va_start( valist
, msg
);
227 fprintf( stderr
, "%s:", input_file_name
);
229 fprintf( stderr
, "%d:", current_line
);
230 fputc( ' ', stderr
);
232 vfprintf( stderr
, msg
, valist
);
238 void error( const char *msg
, ... )
241 va_start( valist
, msg
);
244 fprintf( stderr
, "%s:", input_file_name
);
246 fprintf( stderr
, "%d:", current_line
);
247 fputc( ' ', stderr
);
249 vfprintf( stderr
, msg
, valist
);
254 void warning( const char *msg
, ... )
258 if (!display_warnings
) return;
259 va_start( valist
, msg
);
262 fprintf( stderr
, "%s:", input_file_name
);
264 fprintf( stderr
, "%d:", current_line
);
265 fputc( ' ', stderr
);
267 fprintf( stderr
, "warning: " );
268 vfprintf( stderr
, msg
, valist
);
272 int output( const char *format
, ... )
277 va_start( valist
, format
);
278 ret
= vfprintf( output_file
, format
, valist
);
280 if (ret
< 0) fatal_perror( "Output error" );
284 static struct strarray
get_tools_path(void)
287 static struct strarray dirs
;
291 dirs
= strarray_copy( tools_path
);
293 /* then append the PATH directories */
294 if (getenv( "PATH" ))
296 char *p
= xstrdup( getenv( "PATH" ));
299 strarray_add_one( &dirs
, p
);
300 while (*p
&& *p
!= PATH_SEPARATOR
) p
++;
310 /* find a binary in the path */
311 static const char *find_binary( const char *prefix
, const char *name
)
313 struct strarray dirs
= get_tools_path();
314 unsigned int i
, maxlen
= 0;
318 if (strchr( name
, '/' )) return name
;
319 if (!prefix
) prefix
= "";
320 for (i
= 0; i
< dirs
.count
; i
++) maxlen
= max( maxlen
, strlen(dirs
.str
[i
]) + 2 );
321 file
= xmalloc( maxlen
+ strlen(prefix
) + strlen(name
) + sizeof(EXEEXT
) + 1 );
323 for (i
= 0; i
< dirs
.count
; i
++)
325 strcpy( file
, dirs
.str
[i
] );
326 p
= file
+ strlen(file
);
327 if (p
== file
) *p
++ = '.';
328 if (p
[-1] != '/') *p
++ = '/';
337 if (!stat( file
, &st
) && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0111)) return file
;
343 void spawn( struct strarray args
)
347 const char *argv0
= find_binary( NULL
, args
.str
[0] );
349 if (argv0
) args
.str
[0] = argv0
;
350 strarray_add_one( &args
, NULL
);
352 for (i
= 0; args
.str
[i
]; i
++)
353 fprintf( stderr
, "%s%c", args
.str
[i
], args
.str
[i
+1] ? ' ' : '\n' );
355 if ((status
= _spawnvp( _P_WAIT
, args
.str
[0], args
.str
)))
357 if (status
> 0) fatal_error( "%s failed with status %u\n", args
.str
[0], status
);
358 else fatal_perror( "winebuild" );
363 /* find a build tool in the path, trying the various names */
364 struct strarray
find_tool( const char *name
, const char * const *names
)
367 const char *alt_names
[2];
378 if ((file
= find_binary( target_alias
, *names
))
379 || (names
== alt_names
&& (file
= find_binary( "llvm", *names
))))
381 struct strarray ret
= empty_strarray
;
382 strarray_add_one( &ret
, file
);
387 fatal_error( "cannot find the '%s' tool\n", name
);
390 /* find a link tool in the path */
391 struct strarray
find_link_tool(void)
393 struct strarray ret
= empty_strarray
;
395 if (!(file
= find_binary( NULL
, "lld-link" )))
396 fatal_error( "cannot find the 'lld-link tool\n" );
397 strarray_add_one( &ret
, file
);
401 struct strarray
get_as_command(void)
403 struct strarray args
;
406 if (cc_command
.count
)
408 args
= strarray_copy( cc_command
);
409 strarray_add( &args
, "-xassembler", "-c", NULL
);
410 if (force_pointer_size
)
411 strarray_add_one( &args
, (force_pointer_size
== 8) ? "-m64" : "-m32" );
412 if (cpu_option
) strarray_add_one( &args
, strmake("-mcpu=%s", cpu_option
) );
413 if (fpu_option
) strarray_add_one( &args
, strmake("-mfpu=%s", fpu_option
) );
414 if (arch_option
) strarray_add_one( &args
, strmake("-march=%s", arch_option
) );
415 for (i
= 0; i
< tools_path
.count
; i
++)
416 strarray_add_one( &args
, strmake("-B%s", tools_path
.str
[i
] ));
420 if (!as_command
.count
)
422 static const char * const commands
[] = { "gas", "as", NULL
};
423 as_command
= find_tool( "as", commands
);
426 args
= strarray_copy( as_command
);
428 if (force_pointer_size
)
430 switch (target_platform
)
433 strarray_add( &args
, "-arch", (force_pointer_size
== 8) ? "x86_64" : "i386", NULL
);
439 strarray_add_one( &args
, (force_pointer_size
== 8) ? "-a64" : "-a32" );
442 strarray_add_one( &args
, (force_pointer_size
== 8) ? "--64" : "--32" );
449 if (cpu_option
) strarray_add_one( &args
, strmake("-mcpu=%s", cpu_option
) );
450 if (fpu_option
) strarray_add_one( &args
, strmake("-mfpu=%s", fpu_option
) );
454 struct strarray
get_ld_command(void)
456 struct strarray args
;
458 if (!ld_command
.count
)
460 static const char * const commands
[] = { "ld", "gld", NULL
};
461 ld_command
= find_tool( "ld", commands
);
464 args
= strarray_copy( ld_command
);
466 if (force_pointer_size
)
468 switch (target_platform
)
471 strarray_add( &args
, "-arch", (force_pointer_size
== 8) ? "x86_64" : "i386", NULL
);
473 case PLATFORM_FREEBSD
:
474 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL
);
477 case PLATFORM_WINDOWS
:
478 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "i386pep" : "i386pe", NULL
);
484 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "elf64ppc" : "elf32ppc", NULL
);
487 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "elf_x86_64" : "elf_i386", NULL
);
494 if (target_cpu
== CPU_ARM
&& !is_pe())
495 strarray_add( &args
, "--no-wchar-size-warning", NULL
);
500 const char *get_nm_command(void)
502 if (!nm_command
.count
)
504 static const char * const commands
[] = { "nm", "gnm", NULL
};
505 nm_command
= find_tool( "nm", commands
);
507 if (nm_command
.count
> 1)
508 fatal_error( "multiple arguments in nm command not supported yet\n" );
509 return nm_command
.str
[0];
512 /* get a name for a temp file, automatically cleaned up on exit */
513 char *get_temp_file_name( const char *prefix
, const char *suffix
)
516 const char *ext
, *basename
;
519 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
520 if (!suffix
) suffix
= "";
521 if ((basename
= strrchr( prefix
, '/' ))) basename
++;
522 else basename
= prefix
;
523 if (!(ext
= strchr( basename
, '.' ))) ext
= prefix
+ strlen(prefix
);
524 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
525 memcpy( name
, prefix
, ext
- prefix
);
526 strcpy( name
+ (ext
- prefix
), ".XXXXXX" );
527 strcat( name
, suffix
);
529 if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
531 strcpy( name
, "/tmp/" );
532 memcpy( name
+ 5, basename
, ext
- basename
);
533 strcpy( name
+ 5 + (ext
- basename
), ".XXXXXX" );
534 strcat( name
, suffix
);
535 if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
536 fatal_error( "could not generate a temp file\n" );
540 strarray_add_one( &tmp_files
, name
);
544 /*******************************************************************
547 * Function for reading from/writing to a memory buffer.
550 int byte_swapped
= 0;
551 const char *input_buffer_filename
;
552 const unsigned char *input_buffer
;
553 size_t input_buffer_pos
;
554 size_t input_buffer_size
;
555 unsigned char *output_buffer
;
556 size_t output_buffer_pos
;
557 size_t output_buffer_size
;
559 static void check_output_buffer_space( size_t size
)
561 if (output_buffer_pos
+ size
>= output_buffer_size
)
563 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
564 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
568 void init_input_buffer( const char *file
)
572 unsigned char *buffer
;
574 if ((fd
= open( file
, O_RDONLY
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", file
);
575 if ((fstat( fd
, &st
) == -1)) fatal_perror( "Cannot stat %s", file
);
576 if (!st
.st_size
) fatal_error( "%s is an empty file\n", file
);
577 input_buffer
= buffer
= xmalloc( st
.st_size
);
578 if (read( fd
, buffer
, st
.st_size
) != st
.st_size
) fatal_error( "Cannot read %s\n", file
);
580 input_buffer_filename
= xstrdup( file
);
581 input_buffer_size
= st
.st_size
;
582 input_buffer_pos
= 0;
586 void init_output_buffer(void)
588 output_buffer_size
= 1024;
589 output_buffer_pos
= 0;
590 output_buffer
= xmalloc( output_buffer_size
);
593 void flush_output_buffer(void)
596 if (fwrite( output_buffer
, 1, output_buffer_pos
, output_file
) != output_buffer_pos
)
597 fatal_error( "Error writing to %s\n", output_file_name
);
599 free( output_buffer
);
602 unsigned char get_byte(void)
604 if (input_buffer_pos
>= input_buffer_size
)
605 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
606 return input_buffer
[input_buffer_pos
++];
609 unsigned short get_word(void)
613 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
614 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
615 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
616 if (byte_swapped
) ret
= (ret
<< 8) | (ret
>> 8);
617 input_buffer_pos
+= sizeof(ret
);
621 unsigned int get_dword(void)
625 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
626 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
627 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
629 ret
= ((ret
<< 24) | ((ret
<< 8) & 0x00ff0000) | ((ret
>> 8) & 0x0000ff00) | (ret
>> 24));
630 input_buffer_pos
+= sizeof(ret
);
634 void put_data( const void *data
, size_t size
)
636 check_output_buffer_space( size
);
637 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
638 output_buffer_pos
+= size
;
641 void put_byte( unsigned char val
)
643 check_output_buffer_space( 1 );
644 output_buffer
[output_buffer_pos
++] = val
;
647 void put_word( unsigned short val
)
649 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
650 put_data( &val
, sizeof(val
) );
653 void put_dword( unsigned int val
)
656 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
657 put_data( &val
, sizeof(val
) );
660 void put_qword( unsigned int val
)
674 /* pointer-sized word */
675 void put_pword( unsigned int val
)
677 if (get_ptr_size() == 8) put_qword( val
);
678 else put_dword( val
);
681 void align_output( unsigned int align
)
683 size_t size
= align
- (output_buffer_pos
% align
);
685 if (size
== align
) return;
686 check_output_buffer_space( size
);
687 memset( output_buffer
+ output_buffer_pos
, 0, size
);
688 output_buffer_pos
+= size
;
691 /* output a standard header for generated files */
692 void output_standard_file_header(void)
695 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name
);
697 output( "/* File generated automatically; do not edit! */\n" );
698 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
701 output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
702 output( "\t.globl @feat.00\n" );
703 output( ".set @feat.00, 1\n" );
707 output( "\t.syntax unified\n" );
708 output( "\t.thumb\n" );
712 /* dump a byte stream into the assembly code */
713 void dump_bytes( const void *buffer
, unsigned int size
)
716 const unsigned char *ptr
= buffer
;
719 output( "\t.byte " );
720 for (i
= 0; i
< size
- 1; i
++, ptr
++)
722 if ((i
% 16) == 15) output( "0x%02x\n\t.byte ", *ptr
);
723 else output( "0x%02x,", *ptr
);
725 output( "0x%02x\n", *ptr
);
729 /*******************************************************************
732 * Open a file in the given srcdir and set the input_file_name global variable.
734 FILE *open_input_file( const char *srcdir
, const char *name
)
737 FILE *file
= fopen( name
, "r" );
741 fullname
= strmake( "%s/%s", srcdir
, name
);
742 file
= fopen( fullname
, "r" );
744 else fullname
= xstrdup( name
);
746 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
747 input_file_name
= fullname
;
753 /*******************************************************************
756 * Close the current input file (must have been opened with open_input_file).
758 void close_input_file( FILE *file
)
761 free( input_file_name
);
762 input_file_name
= NULL
;
767 /*******************************************************************
770 void open_output_file(void)
772 if (output_file_name
)
774 if (strendswith( output_file_name
, ".o" ))
775 output_file_source_name
= open_temp_output_file( ".s" );
777 if (!(output_file
= fopen( output_file_name
, "w" )))
778 fatal_error( "Unable to create output file '%s'\n", output_file_name
);
780 else output_file
= stdout
;
784 /*******************************************************************
787 void close_output_file(void)
789 if (!output_file
|| !output_file_name
) return;
790 if (fclose( output_file
) < 0) fatal_perror( "fclose" );
791 if (output_file_source_name
) assemble_file( output_file_source_name
, output_file_name
);
796 /*******************************************************************
797 * open_temp_output_file
799 char *open_temp_output_file( const char *suffix
)
801 char *tmp_file
= get_temp_file_name( output_file_name
, suffix
);
802 if (!(output_file
= fopen( tmp_file
, "w" )))
803 fatal_error( "Unable to create output file '%s'\n", tmp_file
);
808 /*******************************************************************
809 * remove_stdcall_decoration
811 * Remove a possible @xx suffix from a function name.
812 * Return the numerical value of the suffix, or -1 if none.
814 int remove_stdcall_decoration( char *name
)
816 char *p
, *end
= strrchr( name
, '@' );
817 if (!end
|| !end
[1] || end
== name
) return -1;
818 if (target_cpu
!= CPU_x86
) return -1;
819 /* make sure all the rest is digits */
820 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
822 return atoi( end
+ 1 );
826 /*******************************************************************
829 * Run a file through the assembler.
831 void assemble_file( const char *src_file
, const char *obj_file
)
833 struct strarray args
= get_as_command();
834 strarray_add( &args
, "-o", obj_file
, src_file
, NULL
);
839 /*******************************************************************
842 * Create a new dll spec file descriptor
844 DLLSPEC
*alloc_dll_spec(void)
848 spec
= xmalloc( sizeof(*spec
) );
849 memset( spec
, 0, sizeof(*spec
) );
850 spec
->type
= SPEC_WIN32
;
851 spec
->base
= MAX_ORDINALS
;
852 spec
->characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
854 spec
->subsystem_major
= 4;
855 spec
->subsystem_minor
= 0;
856 if (get_ptr_size() > 4)
857 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
859 spec
->characteristics
|= IMAGE_FILE_32BIT_MACHINE
;
860 spec
->dll_characteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
865 /*******************************************************************
868 * Free dll spec file descriptor
870 void free_dll_spec( DLLSPEC
*spec
)
874 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
876 ORDDEF
*odp
= &spec
->entry_points
[i
];
878 free( odp
->export_name
);
879 free( odp
->link_name
);
881 free( spec
->file_name
);
882 free( spec
->dll_name
);
883 free( spec
->c_name
);
884 free( spec
->init_func
);
885 free( spec
->entry_points
);
887 free( spec
->ordinals
);
888 free( spec
->resources
);
893 /*******************************************************************
896 * Map a string to a valid C identifier.
898 char *make_c_identifier( const char *str
)
900 char *p
, buffer
[256];
902 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
904 if (isalnum(*str
)) *p
= *str
;
908 return xstrdup( buffer
);
912 /*******************************************************************
915 * Generate an internal name for a stub entry point.
917 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
922 if (odp
->name
|| odp
->export_name
)
925 buffer
= strmake( "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
926 /* make sure name is a legal C identifier */
927 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
928 if (!*p
) return buffer
;
931 buffer
= strmake( "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
935 /* return the stdcall-decorated name for an entry point */
936 const char *get_link_name( const ORDDEF
*odp
)
941 if (target_cpu
!= CPU_x86
) return odp
->link_name
;
948 if (odp
->flags
& FLAG_THISCALL
) return odp
->link_name
;
949 if (odp
->flags
& FLAG_FASTCALL
) ret
= strmake( "@%s@%u", odp
->link_name
, get_args_size( odp
));
950 else if (!kill_at
) ret
= strmake( "%s@%u", odp
->link_name
, get_args_size( odp
));
951 else return odp
->link_name
;
955 if (odp
->flags
& FLAG_THISCALL
) ret
= strmake( "__thiscall_%s", odp
->link_name
);
956 else if (odp
->flags
& FLAG_FASTCALL
) ret
= strmake( "__fastcall_%s", odp
->link_name
);
957 else return odp
->link_name
;
962 if (is_pe() && !kill_at
)
964 int args
= get_args_size( odp
);
965 if (odp
->flags
& FLAG_REGISTER
) args
+= get_ptr_size(); /* context argument */
966 ret
= strmake( "%s@%u", odp
->link_name
, args
);
968 else return odp
->link_name
;
972 return odp
->link_name
;
980 /*******************************************************************
983 * Sort a list of functions, removing duplicates.
985 int sort_func_list( ORDDEF
**list
, int count
, int (*compare
)(const void *, const void *) )
989 if (!count
) return 0;
990 qsort( list
, count
, sizeof(*list
), compare
);
991 for (i
= j
= 0; i
< count
; i
++) if (compare( &list
[j
], &list
[i
] )) list
[++j
] = list
[i
];
996 /* parse a cpu name and return the corresponding value */
997 int get_cpu_from_name( const char *name
)
1001 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
1002 if (!strcmp( cpu_names
[i
].name
, name
)) return cpu_names
[i
].cpu
;
1006 /*****************************************************************
1007 * Function: get_alignment
1010 * According to the info page for gas, the .align directive behaves
1011 * differently on different systems. On some architectures, the
1012 * argument of a .align directive is the number of bytes to pad to, so
1013 * to align on an 8-byte boundary you'd say
1015 * On other systems, the argument is "the number of low-order zero bits
1016 * that the location counter must have after advancement." So to
1017 * align on an 8-byte boundary you'd say
1020 * The reason gas is written this way is that it's trying to mimic
1021 * native assemblers for the various architectures it runs on. gas
1022 * provides other directives that work consistently across
1023 * architectures, but of course we want to work on all arches with or
1024 * without gas. Hence this function.
1028 * align -- the number of bytes to align to. Must be a power of 2.
1030 unsigned int get_alignment(unsigned int align
)
1034 assert( !(align
& (align
- 1)) );
1040 if (target_platform
!= PLATFORM_APPLE
) return align
;
1046 while ((1u << n
) != align
) n
++;
1054 /* return the page size for the target CPU */
1055 unsigned int get_page_size(void)
1057 return 0x1000; /* same on all platforms */
1060 /* return the size of a pointer on the target CPU */
1061 unsigned int get_ptr_size(void)
1078 /* return the total size in bytes of the arguments on the stack */
1079 unsigned int get_args_size( const ORDDEF
*odp
)
1083 for (i
= size
= 0; i
< odp
->u
.func
.nb_args
; i
++)
1085 switch (odp
->u
.func
.args
[i
])
1092 /* int128 is passed as pointer on x86_64 */
1093 if (target_cpu
!= CPU_x86_64
)
1100 size
+= get_ptr_size();
1107 /* return the assembly name for a C symbol */
1108 const char *asm_name( const char *sym
)
1110 static char *buffer
;
1112 switch (target_platform
)
1114 case PLATFORM_MINGW
:
1115 case PLATFORM_WINDOWS
:
1116 if (target_cpu
!= CPU_x86
) return sym
;
1117 if (sym
[0] == '@') return sym
; /* fastcall */
1119 case PLATFORM_APPLE
:
1120 if (sym
[0] == '.' && sym
[1] == 'L') return sym
;
1122 buffer
= strmake( "_%s", sym
);
1129 /* return an assembly function declaration for a C function name */
1130 const char *func_declaration( const char *func
)
1132 static char *buffer
;
1134 switch (target_platform
)
1136 case PLATFORM_APPLE
:
1138 case PLATFORM_MINGW
:
1139 case PLATFORM_WINDOWS
:
1141 buffer
= strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func
),
1142 thumb_mode
? "\n\t.thumb_func" : "" );
1149 buffer
= strmake( ".type %s,%%function%s", func
,
1150 thumb_mode
? "\n\t.thumb_func" : "" );
1153 buffer
= strmake( ".type %s,%%function", func
);
1156 buffer
= strmake( ".type %s,@function", func
);
1164 /* output a size declaration for an assembly function */
1165 void output_function_size( const char *name
)
1167 switch (target_platform
)
1169 case PLATFORM_APPLE
:
1170 case PLATFORM_MINGW
:
1171 case PLATFORM_WINDOWS
:
1174 output( "\t.size %s, .-%s\n", name
, name
);
1179 /* output a .cfi directive */
1180 void output_cfi( const char *format
, ... )
1184 if (!unwind_tables
) return;
1185 va_start( valist
, format
);
1186 fputc( '\t', output_file
);
1187 vfprintf( output_file
, format
, valist
);
1188 fputc( '\n', output_file
);
1192 /* output an RVA pointer */
1193 void output_rva( const char *format
, ... )
1197 va_start( valist
, format
);
1198 switch (target_platform
)
1200 case PLATFORM_MINGW
:
1201 case PLATFORM_WINDOWS
:
1202 output( "\t.rva " );
1203 vfprintf( output_file
, format
, valist
);
1204 fputc( '\n', output_file
);
1207 output( "\t.long " );
1208 vfprintf( output_file
, format
, valist
);
1209 output( " - .L__wine_spec_rva_base\n" );
1215 /* output the GNU note for non-exec stack */
1216 void output_gnu_stack_note(void)
1218 switch (target_platform
)
1220 case PLATFORM_MINGW
:
1221 case PLATFORM_WINDOWS
:
1222 case PLATFORM_APPLE
:
1229 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1232 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1239 /* return a global symbol declaration for an assembly symbol */
1240 const char *asm_globl( const char *func
)
1242 static char *buffer
;
1245 switch (target_platform
)
1247 case PLATFORM_APPLE
:
1248 buffer
= strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
1250 case PLATFORM_MINGW
:
1251 case PLATFORM_WINDOWS
:
1252 buffer
= strmake( "\t.globl %s%s\n%s%s:", target_cpu
== CPU_x86
? "_" : "", func
,
1253 target_cpu
== CPU_x86
? "_" : "", func
);
1256 buffer
= strmake( "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
1262 const char *get_asm_ptr_keyword(void)
1264 switch(get_ptr_size())
1266 case 4: return ".long";
1267 case 8: return ".quad";
1273 const char *get_asm_string_keyword(void)
1275 switch (target_platform
)
1277 case PLATFORM_APPLE
:
1284 const char *get_asm_export_section(void)
1286 switch (target_platform
)
1288 case PLATFORM_APPLE
: return ".data";
1289 case PLATFORM_MINGW
:
1290 case PLATFORM_WINDOWS
: return ".section .edata";
1291 default: return ".section .data";
1295 const char *get_asm_rodata_section(void)
1297 switch (target_platform
)
1299 case PLATFORM_APPLE
: return ".const";
1300 default: return ".section .rodata";
1304 const char *get_asm_rsrc_section(void)
1306 switch (target_platform
)
1308 case PLATFORM_APPLE
: return ".data";
1309 case PLATFORM_MINGW
:
1310 case PLATFORM_WINDOWS
: return ".section .rsrc";
1311 default: return ".section .data";
1315 const char *get_asm_string_section(void)
1317 switch (target_platform
)
1319 case PLATFORM_APPLE
: return ".cstring";
1320 default: return ".section .rodata";
1324 const char *arm64_page( const char *sym
)
1326 static char *buffer
;
1328 switch (target_platform
)
1330 case PLATFORM_APPLE
:
1332 buffer
= strmake( "%s@PAGE", sym
);
1339 const char *arm64_pageoff( const char *sym
)
1341 static char *buffer
;
1344 switch (target_platform
)
1346 case PLATFORM_APPLE
:
1347 buffer
= strmake( "%s@PAGEOFF", sym
);
1350 buffer
= strmake( ":lo12:%s", sym
);