TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / tools / winebuild / utils.c
blob4860a1988e137e5465d66abc801dd975db582843
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
40 #include "build.h"
42 #if defined(_WIN32) && !defined(__CYGWIN__)
43 # define PATH_SEPARATOR ';'
44 #else
45 # define PATH_SEPARATOR ':'
46 #endif
48 static const char **tmp_files;
49 static unsigned int nb_tmp_files;
50 static unsigned int max_tmp_files;
52 static const struct
54 const char *name;
55 enum target_cpu cpu;
56 } cpu_names[] =
58 { "i386", CPU_x86 },
59 { "i486", CPU_x86 },
60 { "i586", CPU_x86 },
61 { "i686", CPU_x86 },
62 { "i786", CPU_x86 },
63 { "amd64", CPU_x86_64 },
64 { "x86_64", CPU_x86_64 },
65 { "powerpc", CPU_POWERPC },
66 { "arm", CPU_ARM },
67 { "arm64", CPU_ARM64 },
68 { "aarch64", CPU_ARM64 },
71 /* atexit handler to clean tmp files */
72 void cleanup_tmp_files(void)
74 unsigned int i;
75 for (i = 0; i < nb_tmp_files; i++) if (tmp_files[i]) unlink( tmp_files[i] );
79 void *xmalloc (size_t size)
81 void *res;
83 res = malloc (size ? size : 1);
84 if (res == NULL)
86 fprintf (stderr, "Virtual memory exhausted.\n");
87 exit (1);
89 return res;
92 void *xrealloc (void *ptr, size_t size)
94 void *res = realloc (ptr, size);
95 if (size && res == NULL)
97 fprintf (stderr, "Virtual memory exhausted.\n");
98 exit (1);
100 return res;
103 char *xstrdup( const char *str )
105 char *res = strdup( str );
106 if (!res)
108 fprintf (stderr, "Virtual memory exhausted.\n");
109 exit (1);
111 return res;
114 char *strupper(char *s)
116 char *p;
117 for (p = s; *p; p++) *p = toupper(*p);
118 return s;
121 int strendswith(const char* str, const char* end)
123 int l = strlen(str);
124 int m = strlen(end);
125 return l >= m && strcmp(str + l - m, end) == 0;
128 char *strmake( const char* fmt, ... )
130 int n;
131 size_t size = 100;
132 va_list ap;
134 for (;;)
136 char *p = xmalloc( size );
137 va_start( ap, fmt );
138 n = vsnprintf( p, size, fmt, ap );
139 va_end( ap );
140 if (n == -1) size *= 2;
141 else if ((size_t)n >= size) size = n + 1;
142 else return p;
143 free( p );
147 static struct strarray *strarray_init( const char *str )
149 struct strarray *array = xmalloc( sizeof(*array) );
150 array->count = 0;
151 array->max = 16;
152 array->str = xmalloc( array->max * sizeof(*array->str) );
153 if (str) array->str[array->count++] = str;
154 return array;
157 static struct strarray *strarray_copy( const struct strarray *src )
159 struct strarray *array = xmalloc( sizeof(*array) );
160 array->count = src->count;
161 array->max = src->max;
162 array->str = xmalloc( array->max * sizeof(*array->str) );
163 memcpy( array->str, src->str, array->count * sizeof(*array->str) );
164 return array;
167 static void strarray_add_one( struct strarray *array, const char *str )
169 if (array->count == array->max)
171 array->max *= 2;
172 array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
174 array->str[array->count++] = str;
177 void strarray_add( struct strarray *array, ... )
179 va_list valist;
180 const char *str;
182 va_start( valist, array );
183 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
184 va_end( valist );
187 void strarray_addv( struct strarray *array, char * const *argv )
189 while (*argv) strarray_add_one( array, *argv++ );
192 struct strarray *strarray_fromstring( const char *str, const char *delim )
194 const char *tok;
195 struct strarray *array = strarray_init( NULL );
196 char *buf = strdup( str );
198 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
199 strarray_add_one( array, strdup( tok ));
201 free( buf );
202 return array;
205 void strarray_free( struct strarray *array )
207 free( array->str );
208 free( array );
211 void fatal_error( const char *msg, ... )
213 va_list valist;
214 va_start( valist, msg );
215 if (input_file_name)
217 fprintf( stderr, "%s:", input_file_name );
218 if (current_line)
219 fprintf( stderr, "%d:", current_line );
220 fputc( ' ', stderr );
222 else fprintf( stderr, "winebuild: " );
223 vfprintf( stderr, msg, valist );
224 va_end( valist );
225 exit(1);
228 void fatal_perror( const char *msg, ... )
230 va_list valist;
231 va_start( valist, msg );
232 if (input_file_name)
234 fprintf( stderr, "%s:", input_file_name );
235 if (current_line)
236 fprintf( stderr, "%d:", current_line );
237 fputc( ' ', stderr );
239 vfprintf( stderr, msg, valist );
240 perror( " " );
241 va_end( valist );
242 exit(1);
245 void error( const char *msg, ... )
247 va_list valist;
248 va_start( valist, msg );
249 if (input_file_name)
251 fprintf( stderr, "%s:", input_file_name );
252 if (current_line)
253 fprintf( stderr, "%d:", current_line );
254 fputc( ' ', stderr );
256 vfprintf( stderr, msg, valist );
257 va_end( valist );
258 nb_errors++;
261 void warning( const char *msg, ... )
263 va_list valist;
265 if (!display_warnings) return;
266 va_start( valist, msg );
267 if (input_file_name)
269 fprintf( stderr, "%s:", input_file_name );
270 if (current_line)
271 fprintf( stderr, "%d:", current_line );
272 fputc( ' ', stderr );
274 fprintf( stderr, "warning: " );
275 vfprintf( stderr, msg, valist );
276 va_end( valist );
279 int output( const char *format, ... )
281 int ret;
282 va_list valist;
284 va_start( valist, format );
285 ret = vfprintf( output_file, format, valist );
286 va_end( valist );
287 if (ret < 0) fatal_perror( "Output error" );
288 return ret;
291 void spawn( struct strarray *args )
293 unsigned int i;
294 int status;
296 strarray_add_one( args, NULL );
297 if (verbose)
298 for (i = 0; args->str[i]; i++)
299 fprintf( stderr, "%s%c", args->str[i], args->str[i+1] ? ' ' : '\n' );
301 if ((status = _spawnvp( _P_WAIT, args->str[0], args->str )))
303 if (status > 0) fatal_error( "%s failed with status %u\n", args->str[0], status );
304 else fatal_perror( "winebuild" );
305 exit( 1 );
309 /* find a build tool in the path, trying the various names */
310 struct strarray *find_tool( const char *name, const char * const *names )
312 static char **dirs;
313 static unsigned int count, maxlen;
315 char *p, *file;
316 const char *alt_names[2];
317 unsigned int i, len;
318 struct stat st;
320 if (!dirs)
322 char *path;
324 /* split the path in directories */
326 if (!getenv( "PATH" )) fatal_error( "PATH not set, cannot find required tools\n" );
327 path = xstrdup( getenv( "PATH" ));
328 for (p = path, count = 2; *p; p++) if (*p == PATH_SEPARATOR) count++;
329 dirs = xmalloc( count * sizeof(*dirs) );
330 count = 0;
331 dirs[count++] = p = path;
332 while (*p)
334 while (*p && *p != PATH_SEPARATOR) p++;
335 if (!*p) break;
336 *p++ = 0;
337 dirs[count++] = p;
339 for (i = 0; i < count; i++) maxlen = max( maxlen, strlen(dirs[i])+2 );
342 if (!names)
344 alt_names[0] = name;
345 alt_names[1] = NULL;
346 names = alt_names;
349 while (*names)
351 len = strlen(*names) + sizeof(EXEEXT) + 1;
352 if (target_alias)
353 len += strlen(target_alias) + 1;
354 file = xmalloc( maxlen + len );
356 for (i = 0; i < count; i++)
358 strcpy( file, dirs[i] );
359 p = file + strlen(file);
360 if (p == file) *p++ = '.';
361 if (p[-1] != '/') *p++ = '/';
362 if (target_alias)
364 strcpy( p, target_alias );
365 p += strlen(p);
366 *p++ = '-';
368 strcpy( p, *names );
369 strcat( p, EXEEXT );
371 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111))
372 return strarray_init( file );
374 free( file );
375 names++;
377 fatal_error( "cannot find the '%s' tool\n", name );
380 struct strarray *get_as_command(void)
382 struct strarray *args;
384 if (cc_command)
386 args = strarray_copy( cc_command );
387 strarray_add( args, "-xassembler", "-c", NULL );
388 if (force_pointer_size)
389 strarray_add_one( args, (force_pointer_size == 8) ? "-m64" : "-m32" );
390 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
391 if (arch_option) strarray_add_one( args, strmake("-march=%s", arch_option) );
392 return args;
395 if (!as_command)
397 static const char * const commands[] = { "gas", "as", NULL };
398 as_command = find_tool( "as", commands );
401 args = strarray_copy( as_command );
403 if (force_pointer_size)
405 switch (target_platform)
407 case PLATFORM_APPLE:
408 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
409 break;
410 default:
411 switch(target_cpu)
413 case CPU_POWERPC:
414 strarray_add_one( args, (force_pointer_size == 8) ? "-a64" : "-a32" );
415 break;
416 default:
417 strarray_add_one( args, (force_pointer_size == 8) ? "--64" : "--32" );
418 break;
420 break;
424 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
425 return args;
428 struct strarray *get_ld_command(void)
430 struct strarray *args;
432 if (!ld_command)
434 static const char * const commands[] = { "ld", "gld", NULL };
435 ld_command = find_tool( "ld", commands );
438 args = strarray_copy( ld_command );
440 if (force_pointer_size)
442 switch (target_platform)
444 case PLATFORM_APPLE:
445 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
446 break;
447 case PLATFORM_FREEBSD:
448 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
449 break;
450 default:
451 switch(target_cpu)
453 case CPU_POWERPC:
454 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
455 break;
456 default:
457 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
458 break;
460 break;
463 return args;
466 const char *get_nm_command(void)
468 if (!nm_command)
470 static const char * const commands[] = { "nm", "gnm", NULL };
471 nm_command = find_tool( "nm", commands );
473 if (nm_command->count > 1)
474 fatal_error( "multiple arguments in nm command not supported yet\n" );
475 return nm_command->str[0];
478 /* get a name for a temp file, automatically cleaned up on exit */
479 char *get_temp_file_name( const char *prefix, const char *suffix )
481 char *name;
482 const char *ext, *basename;
483 int fd;
485 if (!prefix || !prefix[0]) prefix = "winebuild";
486 if (!suffix) suffix = "";
487 if ((basename = strrchr( prefix, '/' ))) basename++;
488 else basename = prefix;
489 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
490 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
491 memcpy( name, prefix, ext - prefix );
492 strcpy( name + (ext - prefix), ".XXXXXX" );
493 strcat( name, suffix );
495 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
497 strcpy( name, "/tmp/" );
498 memcpy( name + 5, basename, ext - basename );
499 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
500 strcat( name, suffix );
501 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
502 fatal_error( "could not generate a temp file\n" );
505 close( fd );
506 if (nb_tmp_files >= max_tmp_files)
508 max_tmp_files = max( 2 * max_tmp_files, 8 );
509 tmp_files = xrealloc( tmp_files, max_tmp_files * sizeof(tmp_files[0]) );
511 tmp_files[nb_tmp_files++] = name;
512 return name;
515 /*******************************************************************
516 * buffer management
518 * Function for reading from/writing to a memory buffer.
521 int byte_swapped = 0;
522 const char *input_buffer_filename;
523 const unsigned char *input_buffer;
524 size_t input_buffer_pos;
525 size_t input_buffer_size;
526 unsigned char *output_buffer;
527 size_t output_buffer_pos;
528 size_t output_buffer_size;
530 static void check_output_buffer_space( size_t size )
532 if (output_buffer_pos + size >= output_buffer_size)
534 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
535 output_buffer = xrealloc( output_buffer, output_buffer_size );
539 void init_input_buffer( const char *file )
541 int fd;
542 struct stat st;
544 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
545 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
546 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
547 #ifdef HAVE_MMAP
548 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
549 #endif
551 unsigned char *buffer = xmalloc( st.st_size );
552 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
553 input_buffer = buffer;
555 close( fd );
556 input_buffer_filename = xstrdup( file );
557 input_buffer_size = st.st_size;
558 input_buffer_pos = 0;
559 byte_swapped = 0;
562 void init_output_buffer(void)
564 output_buffer_size = 1024;
565 output_buffer_pos = 0;
566 output_buffer = xmalloc( output_buffer_size );
569 void flush_output_buffer(void)
571 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
572 fatal_error( "Error writing to %s\n", output_file_name );
573 free( output_buffer );
576 unsigned char get_byte(void)
578 if (input_buffer_pos >= input_buffer_size)
579 fatal_error( "%s is a truncated file\n", input_buffer_filename );
580 return input_buffer[input_buffer_pos++];
583 unsigned short get_word(void)
585 unsigned short ret;
587 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
588 fatal_error( "%s is a truncated file\n", input_buffer_filename );
589 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
590 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
591 input_buffer_pos += sizeof(ret);
592 return ret;
595 unsigned int get_dword(void)
597 unsigned int ret;
599 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
600 fatal_error( "%s is a truncated file\n", input_buffer_filename );
601 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
602 if (byte_swapped)
603 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
604 input_buffer_pos += sizeof(ret);
605 return ret;
608 void put_data( const void *data, size_t size )
610 check_output_buffer_space( size );
611 memcpy( output_buffer + output_buffer_pos, data, size );
612 output_buffer_pos += size;
615 void put_byte( unsigned char val )
617 check_output_buffer_space( 1 );
618 output_buffer[output_buffer_pos++] = val;
621 void put_word( unsigned short val )
623 if (byte_swapped) val = (val << 8) | (val >> 8);
624 put_data( &val, sizeof(val) );
627 void put_dword( unsigned int val )
629 if (byte_swapped)
630 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
631 put_data( &val, sizeof(val) );
634 void put_qword( unsigned int val )
636 if (byte_swapped)
638 put_dword( 0 );
639 put_dword( val );
641 else
643 put_dword( val );
644 put_dword( 0 );
648 /* pointer-sized word */
649 void put_pword( unsigned int val )
651 if (get_ptr_size() == 8) put_qword( val );
652 else put_dword( val );
655 void align_output( unsigned int align )
657 size_t size = align - (output_buffer_pos % align);
659 if (size == align) return;
660 check_output_buffer_space( size );
661 memset( output_buffer + output_buffer_pos, 0, size );
662 output_buffer_pos += size;
665 /* output a standard header for generated files */
666 void output_standard_file_header(void)
668 if (spec_file_name)
669 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
670 else
671 output( "/* File generated automatically; do not edit! */\n" );
672 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
675 /* dump a byte stream into the assembly code */
676 void dump_bytes( const void *buffer, unsigned int size )
678 unsigned int i;
679 const unsigned char *ptr = buffer;
681 if (!size) return;
682 output( "\t.byte " );
683 for (i = 0; i < size - 1; i++, ptr++)
685 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
686 else output( "0x%02x,", *ptr );
688 output( "0x%02x\n", *ptr );
692 /*******************************************************************
693 * open_input_file
695 * Open a file in the given srcdir and set the input_file_name global variable.
697 FILE *open_input_file( const char *srcdir, const char *name )
699 char *fullname;
700 FILE *file = fopen( name, "r" );
702 if (!file && srcdir)
704 fullname = strmake( "%s/%s", srcdir, name );
705 file = fopen( fullname, "r" );
707 else fullname = xstrdup( name );
709 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
710 input_file_name = fullname;
711 current_line = 1;
712 return file;
716 /*******************************************************************
717 * close_input_file
719 * Close the current input file (must have been opened with open_input_file).
721 void close_input_file( FILE *file )
723 fclose( file );
724 free( input_file_name );
725 input_file_name = NULL;
726 current_line = 0;
730 /*******************************************************************
731 * remove_stdcall_decoration
733 * Remove a possible @xx suffix from a function name.
734 * Return the numerical value of the suffix, or -1 if none.
736 int remove_stdcall_decoration( char *name )
738 char *p, *end = strrchr( name, '@' );
739 if (!end || !end[1] || end == name) return -1;
740 if (target_cpu != CPU_x86) return -1;
741 /* make sure all the rest is digits */
742 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
743 *end = 0;
744 return atoi( end + 1 );
748 /*******************************************************************
749 * assemble_file
751 * Run a file through the assembler.
753 void assemble_file( const char *src_file, const char *obj_file )
755 struct strarray *args = get_as_command();
756 strarray_add( args, "-o", obj_file, src_file, NULL );
757 spawn( args );
758 strarray_free( args );
762 /*******************************************************************
763 * alloc_dll_spec
765 * Create a new dll spec file descriptor
767 DLLSPEC *alloc_dll_spec(void)
769 DLLSPEC *spec;
771 spec = xmalloc( sizeof(*spec) );
772 spec->file_name = NULL;
773 spec->dll_name = NULL;
774 spec->init_func = NULL;
775 spec->main_module = NULL;
776 spec->type = SPEC_WIN32;
777 spec->base = MAX_ORDINALS;
778 spec->limit = 0;
779 spec->stack_size = 0;
780 spec->heap_size = 0;
781 spec->nb_entry_points = 0;
782 spec->alloc_entry_points = 0;
783 spec->nb_names = 0;
784 spec->nb_resources = 0;
785 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
786 if (get_ptr_size() > 4)
787 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
788 else
789 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
790 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
791 spec->subsystem = 0;
792 spec->subsystem_major = 4;
793 spec->subsystem_minor = 0;
794 spec->entry_points = NULL;
795 spec->names = NULL;
796 spec->ordinals = NULL;
797 spec->resources = NULL;
798 return spec;
802 /*******************************************************************
803 * free_dll_spec
805 * Free dll spec file descriptor
807 void free_dll_spec( DLLSPEC *spec )
809 int i;
811 for (i = 0; i < spec->nb_entry_points; i++)
813 ORDDEF *odp = &spec->entry_points[i];
814 free( odp->name );
815 free( odp->export_name );
816 free( odp->link_name );
818 free( spec->file_name );
819 free( spec->dll_name );
820 free( spec->init_func );
821 free( spec->entry_points );
822 free( spec->names );
823 free( spec->ordinals );
824 free( spec->resources );
825 free( spec );
829 /*******************************************************************
830 * make_c_identifier
832 * Map a string to a valid C identifier.
834 const char *make_c_identifier( const char *str )
836 static char buffer[256];
837 char *p;
839 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
841 if (isalnum(*str)) *p = *str;
842 else *p = '_';
844 *p = 0;
845 return buffer;
849 /*******************************************************************
850 * get_stub_name
852 * Generate an internal name for a stub entry point.
854 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
856 static char *buffer;
858 free( buffer );
859 if (odp->name || odp->export_name)
861 char *p;
862 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
863 /* make sure name is a legal C identifier */
864 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
865 if (!*p) return buffer;
866 free( buffer );
868 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
869 return buffer;
872 /* parse a cpu name and return the corresponding value */
873 int get_cpu_from_name( const char *name )
875 unsigned int i;
877 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
878 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
879 return -1;
882 /*****************************************************************
883 * Function: get_alignment
885 * Description:
886 * According to the info page for gas, the .align directive behaves
887 * differently on different systems. On some architectures, the
888 * argument of a .align directive is the number of bytes to pad to, so
889 * to align on an 8-byte boundary you'd say
890 * .align 8
891 * On other systems, the argument is "the number of low-order zero bits
892 * that the location counter must have after advancement." So to
893 * align on an 8-byte boundary you'd say
894 * .align 3
896 * The reason gas is written this way is that it's trying to mimick
897 * native assemblers for the various architectures it runs on. gas
898 * provides other directives that work consistently across
899 * architectures, but of course we want to work on all arches with or
900 * without gas. Hence this function.
903 * Parameters:
904 * align -- the number of bytes to align to. Must be a power of 2.
906 unsigned int get_alignment(unsigned int align)
908 unsigned int n;
910 assert( !(align & (align - 1)) );
912 switch(target_cpu)
914 case CPU_x86:
915 case CPU_x86_64:
916 if (target_platform != PLATFORM_APPLE) return align;
917 /* fall through */
918 case CPU_POWERPC:
919 case CPU_ARM:
920 case CPU_ARM64:
921 n = 0;
922 while ((1u << n) != align) n++;
923 return n;
925 /* unreached */
926 assert(0);
927 return 0;
930 /* return the page size for the target CPU */
931 unsigned int get_page_size(void)
933 switch(target_cpu)
935 case CPU_x86:
936 case CPU_x86_64:
937 case CPU_POWERPC:
938 case CPU_ARM:
939 return 0x1000;
940 case CPU_ARM64:
941 return 0x10000;
943 /* unreached */
944 assert(0);
945 return 0;
948 /* return the size of a pointer on the target CPU */
949 unsigned int get_ptr_size(void)
951 switch(target_cpu)
953 case CPU_x86:
954 case CPU_POWERPC:
955 case CPU_ARM:
956 return 4;
957 case CPU_x86_64:
958 case CPU_ARM64:
959 return 8;
961 /* unreached */
962 assert(0);
963 return 0;
966 /* return the total size in bytes of the arguments on the stack */
967 unsigned int get_args_size( const ORDDEF *odp )
969 int i, size;
971 for (i = size = 0; i < odp->u.func.nb_args; i++)
973 switch (odp->u.func.args[i])
975 case ARG_INT64:
976 case ARG_DOUBLE:
977 size += 8;
978 break;
979 case ARG_INT128:
980 /* int128 is passed as pointer on x86_64 */
981 if (target_cpu != CPU_x86_64)
983 size += 16;
984 break;
986 /* fall through */
987 default:
988 size += get_ptr_size();
989 break;
992 return size;
995 /* return the assembly name for a C symbol */
996 const char *asm_name( const char *sym )
998 static char *buffer;
1000 switch (target_platform)
1002 case PLATFORM_APPLE:
1003 case PLATFORM_WINDOWS:
1004 if (sym[0] == '.' && sym[1] == 'L') return sym;
1005 free( buffer );
1006 buffer = strmake( "_%s", sym );
1007 return buffer;
1008 default:
1009 return sym;
1013 /* return an assembly function declaration for a C function name */
1014 const char *func_declaration( const char *func )
1016 static char *buffer;
1018 switch (target_platform)
1020 case PLATFORM_APPLE:
1021 return "";
1022 case PLATFORM_WINDOWS:
1023 free( buffer );
1024 buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
1025 break;
1026 default:
1027 free( buffer );
1028 switch(target_cpu)
1030 case CPU_ARM:
1031 case CPU_ARM64:
1032 buffer = strmake( ".type %s,%%function", func );
1033 break;
1034 default:
1035 buffer = strmake( ".type %s,@function", func );
1036 break;
1038 break;
1040 return buffer;
1043 /* output a size declaration for an assembly function */
1044 void output_function_size( const char *name )
1046 switch (target_platform)
1048 case PLATFORM_APPLE:
1049 case PLATFORM_WINDOWS:
1050 break;
1051 default:
1052 output( "\t.size %s, .-%s\n", name, name );
1053 break;
1057 /* output a .cfi directive */
1058 void output_cfi( const char *format, ... )
1060 va_list valist;
1062 if (!unwind_tables) return;
1063 va_start( valist, format );
1064 fputc( '\t', output_file );
1065 vfprintf( output_file, format, valist );
1066 fputc( '\n', output_file );
1067 va_end( valist );
1070 /* output the GNU note for non-exec stack */
1071 void output_gnu_stack_note(void)
1073 switch (target_platform)
1075 case PLATFORM_WINDOWS:
1076 case PLATFORM_APPLE:
1077 break;
1078 default:
1079 switch(target_cpu)
1081 case CPU_ARM:
1082 case CPU_ARM64:
1083 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1084 break;
1085 default:
1086 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1087 break;
1089 break;
1093 /* return a global symbol declaration for an assembly symbol */
1094 const char *asm_globl( const char *func )
1096 static char *buffer;
1098 free( buffer );
1099 switch (target_platform)
1101 case PLATFORM_APPLE:
1102 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1103 break;
1104 case PLATFORM_WINDOWS:
1105 buffer = strmake( "\t.globl _%s\n_%s:", func, func );
1106 break;
1107 default:
1108 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1109 break;
1111 return buffer;
1114 const char *get_asm_ptr_keyword(void)
1116 switch(get_ptr_size())
1118 case 4: return ".long";
1119 case 8: return ".quad";
1121 assert(0);
1122 return NULL;
1125 const char *get_asm_string_keyword(void)
1127 switch (target_platform)
1129 case PLATFORM_APPLE:
1130 return ".asciz";
1131 default:
1132 return ".string";
1136 const char *get_asm_rodata_section(void)
1138 switch (target_platform)
1140 case PLATFORM_APPLE: return ".const";
1141 default: return ".section .rodata";
1145 const char *get_asm_string_section(void)
1147 switch (target_platform)
1149 case PLATFORM_APPLE: return ".cstring";
1150 default: return ".section .rodata";