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 struct strarray
get_as_command(void)
392 struct strarray args
;
395 if (cc_command
.count
)
397 args
= strarray_copy( cc_command
);
398 strarray_add( &args
, "-xassembler", "-c", NULL
);
399 if (force_pointer_size
)
400 strarray_add_one( &args
, (force_pointer_size
== 8) ? "-m64" : "-m32" );
401 if (cpu_option
) strarray_add_one( &args
, strmake("-mcpu=%s", cpu_option
) );
402 if (fpu_option
) strarray_add_one( &args
, strmake("-mfpu=%s", fpu_option
) );
403 if (arch_option
) strarray_add_one( &args
, strmake("-march=%s", arch_option
) );
404 for (i
= 0; i
< tools_path
.count
; i
++)
405 strarray_add_one( &args
, strmake("-B%s", tools_path
.str
[i
] ));
409 if (!as_command
.count
)
411 static const char * const commands
[] = { "gas", "as", NULL
};
412 as_command
= find_tool( "as", commands
);
415 args
= strarray_copy( as_command
);
417 if (force_pointer_size
)
419 switch (target_platform
)
422 strarray_add( &args
, "-arch", (force_pointer_size
== 8) ? "x86_64" : "i386", NULL
);
428 strarray_add_one( &args
, (force_pointer_size
== 8) ? "-a64" : "-a32" );
431 strarray_add_one( &args
, (force_pointer_size
== 8) ? "--64" : "--32" );
438 if (cpu_option
) strarray_add_one( &args
, strmake("-mcpu=%s", cpu_option
) );
439 if (fpu_option
) strarray_add_one( &args
, strmake("-mfpu=%s", fpu_option
) );
443 struct strarray
get_ld_command(void)
445 struct strarray args
;
447 if (!ld_command
.count
)
449 static const char * const commands
[] = { "ld", "gld", NULL
};
450 ld_command
= find_tool( "ld", commands
);
453 args
= strarray_copy( ld_command
);
455 if (force_pointer_size
)
457 switch (target_platform
)
460 strarray_add( &args
, "-arch", (force_pointer_size
== 8) ? "x86_64" : "i386", NULL
);
462 case PLATFORM_FREEBSD
:
463 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL
);
465 case PLATFORM_WINDOWS
:
466 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "i386pep" : "i386pe", NULL
);
472 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "elf64ppc" : "elf32ppc", NULL
);
475 strarray_add( &args
, "-m", (force_pointer_size
== 8) ? "elf_x86_64" : "elf_i386", NULL
);
482 if (target_cpu
== CPU_ARM
&& target_platform
!= PLATFORM_WINDOWS
)
483 strarray_add( &args
, "--no-wchar-size-warning", NULL
);
488 const char *get_nm_command(void)
490 if (!nm_command
.count
)
492 static const char * const commands
[] = { "nm", "gnm", NULL
};
493 nm_command
= find_tool( "nm", commands
);
495 if (nm_command
.count
> 1)
496 fatal_error( "multiple arguments in nm command not supported yet\n" );
497 return nm_command
.str
[0];
500 /* get a name for a temp file, automatically cleaned up on exit */
501 char *get_temp_file_name( const char *prefix
, const char *suffix
)
504 const char *ext
, *basename
;
507 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
508 if (!suffix
) suffix
= "";
509 if ((basename
= strrchr( prefix
, '/' ))) basename
++;
510 else basename
= prefix
;
511 if (!(ext
= strchr( basename
, '.' ))) ext
= prefix
+ strlen(prefix
);
512 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
513 memcpy( name
, prefix
, ext
- prefix
);
514 strcpy( name
+ (ext
- prefix
), ".XXXXXX" );
515 strcat( name
, suffix
);
517 if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
519 strcpy( name
, "/tmp/" );
520 memcpy( name
+ 5, basename
, ext
- basename
);
521 strcpy( name
+ 5 + (ext
- basename
), ".XXXXXX" );
522 strcat( name
, suffix
);
523 if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
524 fatal_error( "could not generate a temp file\n" );
528 strarray_add_one( &tmp_files
, name
);
532 /*******************************************************************
535 * Function for reading from/writing to a memory buffer.
538 int byte_swapped
= 0;
539 const char *input_buffer_filename
;
540 const unsigned char *input_buffer
;
541 size_t input_buffer_pos
;
542 size_t input_buffer_size
;
543 unsigned char *output_buffer
;
544 size_t output_buffer_pos
;
545 size_t output_buffer_size
;
547 static void check_output_buffer_space( size_t size
)
549 if (output_buffer_pos
+ size
>= output_buffer_size
)
551 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
552 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
556 void init_input_buffer( const char *file
)
560 unsigned char *buffer
;
562 if ((fd
= open( file
, O_RDONLY
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", file
);
563 if ((fstat( fd
, &st
) == -1)) fatal_perror( "Cannot stat %s", file
);
564 if (!st
.st_size
) fatal_error( "%s is an empty file\n", file
);
565 input_buffer
= buffer
= xmalloc( st
.st_size
);
566 if (read( fd
, buffer
, st
.st_size
) != st
.st_size
) fatal_error( "Cannot read %s\n", file
);
568 input_buffer_filename
= xstrdup( file
);
569 input_buffer_size
= st
.st_size
;
570 input_buffer_pos
= 0;
574 void init_output_buffer(void)
576 output_buffer_size
= 1024;
577 output_buffer_pos
= 0;
578 output_buffer
= xmalloc( output_buffer_size
);
581 void flush_output_buffer(void)
584 if (fwrite( output_buffer
, 1, output_buffer_pos
, output_file
) != output_buffer_pos
)
585 fatal_error( "Error writing to %s\n", output_file_name
);
587 free( output_buffer
);
590 unsigned char get_byte(void)
592 if (input_buffer_pos
>= input_buffer_size
)
593 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
594 return input_buffer
[input_buffer_pos
++];
597 unsigned short get_word(void)
601 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
602 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
603 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
604 if (byte_swapped
) ret
= (ret
<< 8) | (ret
>> 8);
605 input_buffer_pos
+= sizeof(ret
);
609 unsigned int get_dword(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
) );
617 ret
= ((ret
<< 24) | ((ret
<< 8) & 0x00ff0000) | ((ret
>> 8) & 0x0000ff00) | (ret
>> 24));
618 input_buffer_pos
+= sizeof(ret
);
622 void put_data( const void *data
, size_t size
)
624 check_output_buffer_space( size
);
625 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
626 output_buffer_pos
+= size
;
629 void put_byte( unsigned char val
)
631 check_output_buffer_space( 1 );
632 output_buffer
[output_buffer_pos
++] = val
;
635 void put_word( unsigned short val
)
637 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
638 put_data( &val
, sizeof(val
) );
641 void put_dword( unsigned int val
)
644 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
645 put_data( &val
, sizeof(val
) );
648 void put_qword( unsigned int val
)
662 /* pointer-sized word */
663 void put_pword( unsigned int val
)
665 if (get_ptr_size() == 8) put_qword( val
);
666 else put_dword( val
);
669 void align_output( unsigned int align
)
671 size_t size
= align
- (output_buffer_pos
% align
);
673 if (size
== align
) return;
674 check_output_buffer_space( size
);
675 memset( output_buffer
+ output_buffer_pos
, 0, size
);
676 output_buffer_pos
+= size
;
679 /* output a standard header for generated files */
680 void output_standard_file_header(void)
683 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name
);
685 output( "/* File generated automatically; do not edit! */\n" );
686 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
689 output( "\t.def @feat.00; .scl 3; .type 0; .endef\n" );
690 output( "\t.globl @feat.00\n" );
691 output( ".set @feat.00, 1\n" );
695 /* dump a byte stream into the assembly code */
696 void dump_bytes( const void *buffer
, unsigned int size
)
699 const unsigned char *ptr
= buffer
;
702 output( "\t.byte " );
703 for (i
= 0; i
< size
- 1; i
++, ptr
++)
705 if ((i
% 16) == 15) output( "0x%02x\n\t.byte ", *ptr
);
706 else output( "0x%02x,", *ptr
);
708 output( "0x%02x\n", *ptr
);
712 /*******************************************************************
715 * Open a file in the given srcdir and set the input_file_name global variable.
717 FILE *open_input_file( const char *srcdir
, const char *name
)
720 FILE *file
= fopen( name
, "r" );
724 fullname
= strmake( "%s/%s", srcdir
, name
);
725 file
= fopen( fullname
, "r" );
727 else fullname
= xstrdup( name
);
729 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
730 input_file_name
= fullname
;
736 /*******************************************************************
739 * Close the current input file (must have been opened with open_input_file).
741 void close_input_file( FILE *file
)
744 free( input_file_name
);
745 input_file_name
= NULL
;
750 /*******************************************************************
753 void open_output_file(void)
755 if (output_file_name
)
757 if (strendswith( output_file_name
, ".o" ))
758 output_file_source_name
= open_temp_output_file( ".s" );
760 if (!(output_file
= fopen( output_file_name
, "w" )))
761 fatal_error( "Unable to create output file '%s'\n", output_file_name
);
763 else output_file
= stdout
;
767 /*******************************************************************
770 void close_output_file(void)
772 if (!output_file
|| !output_file_name
) return;
773 if (fclose( output_file
) < 0) fatal_perror( "fclose" );
774 if (output_file_source_name
) assemble_file( output_file_source_name
, output_file_name
);
779 /*******************************************************************
780 * open_temp_output_file
782 char *open_temp_output_file( const char *suffix
)
784 char *tmp_file
= get_temp_file_name( output_file_name
, suffix
);
785 if (!(output_file
= fopen( tmp_file
, "w" )))
786 fatal_error( "Unable to create output file '%s'\n", tmp_file
);
791 /*******************************************************************
792 * remove_stdcall_decoration
794 * Remove a possible @xx suffix from a function name.
795 * Return the numerical value of the suffix, or -1 if none.
797 int remove_stdcall_decoration( char *name
)
799 char *p
, *end
= strrchr( name
, '@' );
800 if (!end
|| !end
[1] || end
== name
) return -1;
801 if (target_cpu
!= CPU_x86
) return -1;
802 /* make sure all the rest is digits */
803 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
805 return atoi( end
+ 1 );
809 /*******************************************************************
812 * Run a file through the assembler.
814 void assemble_file( const char *src_file
, const char *obj_file
)
816 struct strarray args
= get_as_command();
817 strarray_add( &args
, "-o", obj_file
, src_file
, NULL
);
822 /*******************************************************************
825 * Create a new dll spec file descriptor
827 DLLSPEC
*alloc_dll_spec(void)
831 spec
= xmalloc( sizeof(*spec
) );
832 memset( spec
, 0, sizeof(*spec
) );
833 spec
->type
= SPEC_WIN32
;
834 spec
->base
= MAX_ORDINALS
;
835 spec
->characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
837 spec
->subsystem_major
= 4;
838 spec
->subsystem_minor
= 0;
839 if (get_ptr_size() > 4)
840 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
842 spec
->characteristics
|= IMAGE_FILE_32BIT_MACHINE
;
843 spec
->dll_characteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
848 /*******************************************************************
851 * Free dll spec file descriptor
853 void free_dll_spec( DLLSPEC
*spec
)
857 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
859 ORDDEF
*odp
= &spec
->entry_points
[i
];
861 free( odp
->export_name
);
862 free( odp
->link_name
);
864 free( spec
->file_name
);
865 free( spec
->dll_name
);
866 free( spec
->c_name
);
867 free( spec
->init_func
);
868 free( spec
->entry_points
);
870 free( spec
->ordinals
);
871 free( spec
->resources
);
876 /*******************************************************************
879 * Map a string to a valid C identifier.
881 char *make_c_identifier( const char *str
)
883 char *p
, buffer
[256];
885 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
887 if (isalnum(*str
)) *p
= *str
;
891 return xstrdup( buffer
);
895 /*******************************************************************
898 * Generate an internal name for a stub entry point.
900 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
905 if (odp
->name
|| odp
->export_name
)
908 buffer
= strmake( "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
909 /* make sure name is a legal C identifier */
910 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
911 if (!*p
) return buffer
;
914 buffer
= strmake( "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
918 /* return the stdcall-decorated name for an entry point */
919 const char *get_link_name( const ORDDEF
*odp
)
924 if (target_cpu
!= CPU_x86
) return odp
->link_name
;
929 if (target_platform
== PLATFORM_WINDOWS
)
931 if (odp
->flags
& FLAG_THISCALL
) return odp
->link_name
;
932 if (odp
->flags
& FLAG_FASTCALL
) ret
= strmake( "@%s@%u", odp
->link_name
, get_args_size( odp
));
933 else if (!kill_at
) ret
= strmake( "%s@%u", odp
->link_name
, get_args_size( odp
));
934 else return odp
->link_name
;
938 if (odp
->flags
& FLAG_THISCALL
) ret
= strmake( "__thiscall_%s", odp
->link_name
);
939 else if (odp
->flags
& FLAG_FASTCALL
) ret
= strmake( "__fastcall_%s", odp
->link_name
);
940 else return odp
->link_name
;
945 if (target_platform
== PLATFORM_WINDOWS
&& !kill_at
)
947 int args
= get_args_size( odp
);
948 if (odp
->flags
& FLAG_REGISTER
) args
+= get_ptr_size(); /* context argument */
949 ret
= strmake( "%s@%u", odp
->link_name
, args
);
951 else return odp
->link_name
;
955 return odp
->link_name
;
963 /*******************************************************************
966 * Sort a list of functions, removing duplicates.
968 int sort_func_list( ORDDEF
**list
, int count
, int (*compare
)(const void *, const void *) )
972 if (!count
) return 0;
973 qsort( list
, count
, sizeof(*list
), compare
);
974 for (i
= j
= 0; i
< count
; i
++) if (compare( &list
[j
], &list
[i
] )) list
[++j
] = list
[i
];
979 /* parse a cpu name and return the corresponding value */
980 int get_cpu_from_name( const char *name
)
984 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
985 if (!strcmp( cpu_names
[i
].name
, name
)) return cpu_names
[i
].cpu
;
989 /*****************************************************************
990 * Function: get_alignment
993 * According to the info page for gas, the .align directive behaves
994 * differently on different systems. On some architectures, the
995 * argument of a .align directive is the number of bytes to pad to, so
996 * to align on an 8-byte boundary you'd say
998 * On other systems, the argument is "the number of low-order zero bits
999 * that the location counter must have after advancement." So to
1000 * align on an 8-byte boundary you'd say
1003 * The reason gas is written this way is that it's trying to mimic
1004 * native assemblers for the various architectures it runs on. gas
1005 * provides other directives that work consistently across
1006 * architectures, but of course we want to work on all arches with or
1007 * without gas. Hence this function.
1011 * align -- the number of bytes to align to. Must be a power of 2.
1013 unsigned int get_alignment(unsigned int align
)
1017 assert( !(align
& (align
- 1)) );
1023 if (target_platform
!= PLATFORM_APPLE
) return align
;
1029 while ((1u << n
) != align
) n
++;
1037 /* return the page size for the target CPU */
1038 unsigned int get_page_size(void)
1040 return 0x1000; /* same on all platforms */
1043 /* return the size of a pointer on the target CPU */
1044 unsigned int get_ptr_size(void)
1061 /* return the total size in bytes of the arguments on the stack */
1062 unsigned int get_args_size( const ORDDEF
*odp
)
1066 for (i
= size
= 0; i
< odp
->u
.func
.nb_args
; i
++)
1068 switch (odp
->u
.func
.args
[i
])
1075 /* int128 is passed as pointer on x86_64 */
1076 if (target_cpu
!= CPU_x86_64
)
1083 size
+= get_ptr_size();
1090 /* return the assembly name for a C symbol */
1091 const char *asm_name( const char *sym
)
1093 static char *buffer
;
1095 switch (target_platform
)
1097 case PLATFORM_WINDOWS
:
1098 if (target_cpu
!= CPU_x86
) return sym
;
1099 if (sym
[0] == '@') return sym
; /* fastcall */
1101 case PLATFORM_APPLE
:
1102 if (sym
[0] == '.' && sym
[1] == 'L') return sym
;
1104 buffer
= strmake( "_%s", sym
);
1111 /* return an assembly function declaration for a C function name */
1112 const char *func_declaration( const char *func
)
1114 static char *buffer
;
1116 switch (target_platform
)
1118 case PLATFORM_APPLE
:
1120 case PLATFORM_WINDOWS
:
1122 buffer
= strmake( ".def %s; .scl 2; .type 32; .endef", asm_name(func
) );
1130 buffer
= strmake( ".type %s,%%function", func
);
1133 buffer
= strmake( ".type %s,@function", func
);
1141 /* output a size declaration for an assembly function */
1142 void output_function_size( const char *name
)
1144 switch (target_platform
)
1146 case PLATFORM_APPLE
:
1147 case PLATFORM_WINDOWS
:
1150 output( "\t.size %s, .-%s\n", name
, name
);
1155 /* output a .cfi directive */
1156 void output_cfi( const char *format
, ... )
1160 if (!unwind_tables
) return;
1161 va_start( valist
, format
);
1162 fputc( '\t', output_file
);
1163 vfprintf( output_file
, format
, valist
);
1164 fputc( '\n', output_file
);
1168 /* output an RVA pointer */
1169 void output_rva( const char *format
, ... )
1173 va_start( valist
, format
);
1174 switch (target_platform
)
1176 case PLATFORM_WINDOWS
:
1177 output( "\t.rva " );
1178 vfprintf( output_file
, format
, valist
);
1179 fputc( '\n', output_file
);
1182 output( "\t.long " );
1183 vfprintf( output_file
, format
, valist
);
1184 output( " - .L__wine_spec_rva_base\n" );
1190 /* output the GNU note for non-exec stack */
1191 void output_gnu_stack_note(void)
1193 switch (target_platform
)
1195 case PLATFORM_WINDOWS
:
1196 case PLATFORM_APPLE
:
1203 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1206 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1213 /* return a global symbol declaration for an assembly symbol */
1214 const char *asm_globl( const char *func
)
1216 static char *buffer
;
1219 switch (target_platform
)
1221 case PLATFORM_APPLE
:
1222 buffer
= strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
1224 case PLATFORM_WINDOWS
:
1225 buffer
= strmake( "\t.globl %s%s\n%s%s:", target_cpu
== CPU_x86
? "_" : "", func
,
1226 target_cpu
== CPU_x86
? "_" : "", func
);
1229 buffer
= strmake( "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
1235 const char *get_asm_ptr_keyword(void)
1237 switch(get_ptr_size())
1239 case 4: return ".long";
1240 case 8: return ".quad";
1246 const char *get_asm_string_keyword(void)
1248 switch (target_platform
)
1250 case PLATFORM_APPLE
:
1257 const char *get_asm_export_section(void)
1259 switch (target_platform
)
1261 case PLATFORM_APPLE
: return ".data";
1262 case PLATFORM_WINDOWS
: return ".section .edata";
1263 default: return ".section .data";
1267 const char *get_asm_rodata_section(void)
1269 switch (target_platform
)
1271 case PLATFORM_APPLE
: return ".const";
1272 default: return ".section .rodata";
1276 const char *get_asm_rsrc_section(void)
1278 switch (target_platform
)
1280 case PLATFORM_APPLE
: return ".data";
1281 case PLATFORM_WINDOWS
: return ".section .rsrc";
1282 default: return ".section .data";
1286 const char *get_asm_string_section(void)
1288 switch (target_platform
)
1290 case PLATFORM_APPLE
: return ".cstring";
1291 default: return ".section .rodata";
1295 const char *arm64_page( const char *sym
)
1297 static char *buffer
;
1299 switch (target_platform
)
1301 case PLATFORM_APPLE
:
1303 buffer
= strmake( "%s@PAGE", sym
);
1310 const char *arm64_pageoff( const char *sym
)
1312 static char *buffer
;
1315 switch (target_platform
)
1317 case PLATFORM_APPLE
:
1318 buffer
= strmake( "%s@PAGEOFF", sym
);
1321 buffer
= strmake( ":lo12:%s", sym
);