makefiles: Generate makefile dependencies from makedep.
[wine.git] / tools / winebuild / utils.c
blob08e6b01f2cfe9a716acde765651890c97b10276c
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 { "armv5", CPU_ARM },
68 { "armv6", CPU_ARM },
69 { "armv7", CPU_ARM },
70 { "arm64", CPU_ARM64 },
71 { "aarch64", CPU_ARM64 },
74 /* atexit handler to clean tmp files */
75 void cleanup_tmp_files(void)
77 unsigned int i;
78 for (i = 0; i < nb_tmp_files; i++) if (tmp_files[i]) unlink( tmp_files[i] );
82 void *xmalloc (size_t size)
84 void *res;
86 res = malloc (size ? size : 1);
87 if (res == NULL)
89 fprintf (stderr, "Virtual memory exhausted.\n");
90 exit (1);
92 return res;
95 void *xrealloc (void *ptr, size_t size)
97 void *res = realloc (ptr, size);
98 if (size && res == NULL)
100 fprintf (stderr, "Virtual memory exhausted.\n");
101 exit (1);
103 return res;
106 char *xstrdup( const char *str )
108 char *res = strdup( str );
109 if (!res)
111 fprintf (stderr, "Virtual memory exhausted.\n");
112 exit (1);
114 return res;
117 char *strupper(char *s)
119 char *p;
120 for (p = s; *p; p++) *p = toupper(*p);
121 return s;
124 int strendswith(const char* str, const char* end)
126 int l = strlen(str);
127 int m = strlen(end);
128 return l >= m && strcmp(str + l - m, end) == 0;
131 char *strmake( const char* fmt, ... )
133 int n;
134 size_t size = 100;
135 va_list ap;
137 for (;;)
139 char *p = xmalloc( size );
140 va_start( ap, fmt );
141 n = vsnprintf( p, size, fmt, ap );
142 va_end( ap );
143 if (n == -1) size *= 2;
144 else if ((size_t)n >= size) size = n + 1;
145 else return p;
146 free( p );
150 static struct strarray *strarray_init( const char *str )
152 struct strarray *array = xmalloc( sizeof(*array) );
153 array->count = 0;
154 array->max = 16;
155 array->str = xmalloc( array->max * sizeof(*array->str) );
156 if (str) array->str[array->count++] = str;
157 return array;
160 static struct strarray *strarray_copy( const struct strarray *src )
162 struct strarray *array = xmalloc( sizeof(*array) );
163 array->count = src->count;
164 array->max = src->max;
165 array->str = xmalloc( array->max * sizeof(*array->str) );
166 memcpy( array->str, src->str, array->count * sizeof(*array->str) );
167 return array;
170 static void strarray_add_one( struct strarray *array, const char *str )
172 if (array->count == array->max)
174 array->max *= 2;
175 array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
177 array->str[array->count++] = str;
180 void strarray_add( struct strarray *array, ... )
182 va_list valist;
183 const char *str;
185 va_start( valist, array );
186 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
187 va_end( valist );
190 void strarray_addv( struct strarray *array, char * const *argv )
192 while (*argv) strarray_add_one( array, *argv++ );
195 struct strarray *strarray_fromstring( const char *str, const char *delim )
197 const char *tok;
198 struct strarray *array = strarray_init( NULL );
199 char *buf = strdup( str );
201 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
202 strarray_add_one( array, strdup( tok ));
204 free( buf );
205 return array;
208 void strarray_free( struct strarray *array )
210 free( array->str );
211 free( array );
214 void fatal_error( const char *msg, ... )
216 va_list valist;
217 va_start( valist, msg );
218 if (input_file_name)
220 fprintf( stderr, "%s:", input_file_name );
221 if (current_line)
222 fprintf( stderr, "%d:", current_line );
223 fputc( ' ', stderr );
225 else fprintf( stderr, "winebuild: " );
226 vfprintf( stderr, msg, valist );
227 va_end( valist );
228 exit(1);
231 void fatal_perror( const char *msg, ... )
233 va_list valist;
234 va_start( valist, msg );
235 if (input_file_name)
237 fprintf( stderr, "%s:", input_file_name );
238 if (current_line)
239 fprintf( stderr, "%d:", current_line );
240 fputc( ' ', stderr );
242 vfprintf( stderr, msg, valist );
243 perror( " " );
244 va_end( valist );
245 exit(1);
248 void error( const char *msg, ... )
250 va_list valist;
251 va_start( valist, msg );
252 if (input_file_name)
254 fprintf( stderr, "%s:", input_file_name );
255 if (current_line)
256 fprintf( stderr, "%d:", current_line );
257 fputc( ' ', stderr );
259 vfprintf( stderr, msg, valist );
260 va_end( valist );
261 nb_errors++;
264 void warning( const char *msg, ... )
266 va_list valist;
268 if (!display_warnings) return;
269 va_start( valist, msg );
270 if (input_file_name)
272 fprintf( stderr, "%s:", input_file_name );
273 if (current_line)
274 fprintf( stderr, "%d:", current_line );
275 fputc( ' ', stderr );
277 fprintf( stderr, "warning: " );
278 vfprintf( stderr, msg, valist );
279 va_end( valist );
282 int output( const char *format, ... )
284 int ret;
285 va_list valist;
287 va_start( valist, format );
288 ret = vfprintf( output_file, format, valist );
289 va_end( valist );
290 if (ret < 0) fatal_perror( "Output error" );
291 return ret;
294 void spawn( struct strarray *args )
296 unsigned int i;
297 int status;
299 strarray_add_one( args, NULL );
300 if (verbose)
301 for (i = 0; args->str[i]; i++)
302 fprintf( stderr, "%s%c", args->str[i], args->str[i+1] ? ' ' : '\n' );
304 if ((status = _spawnvp( _P_WAIT, args->str[0], args->str )))
306 if (status > 0) fatal_error( "%s failed with status %u\n", args->str[0], status );
307 else fatal_perror( "winebuild" );
308 exit( 1 );
312 /* find a build tool in the path, trying the various names */
313 struct strarray *find_tool( const char *name, const char * const *names )
315 static char **dirs;
316 static unsigned int count, maxlen;
318 char *p, *file;
319 const char *alt_names[2];
320 unsigned int i, len;
321 struct stat st;
323 if (!dirs)
325 char *path;
327 /* split the path in directories */
329 if (!getenv( "PATH" )) fatal_error( "PATH not set, cannot find required tools\n" );
330 path = xstrdup( getenv( "PATH" ));
331 for (p = path, count = 2; *p; p++) if (*p == PATH_SEPARATOR) count++;
332 dirs = xmalloc( count * sizeof(*dirs) );
333 count = 0;
334 dirs[count++] = p = path;
335 while (*p)
337 while (*p && *p != PATH_SEPARATOR) p++;
338 if (!*p) break;
339 *p++ = 0;
340 dirs[count++] = p;
342 for (i = 0; i < count; i++) maxlen = max( maxlen, strlen(dirs[i])+2 );
345 if (!names)
347 alt_names[0] = name;
348 alt_names[1] = NULL;
349 names = alt_names;
352 while (*names)
354 len = strlen(*names) + sizeof(EXEEXT) + 1;
355 if (target_alias)
356 len += strlen(target_alias) + 1;
357 file = xmalloc( maxlen + len );
359 for (i = 0; i < count; i++)
361 strcpy( file, dirs[i] );
362 p = file + strlen(file);
363 if (p == file) *p++ = '.';
364 if (p[-1] != '/') *p++ = '/';
365 if (target_alias)
367 strcpy( p, target_alias );
368 p += strlen(p);
369 *p++ = '-';
371 strcpy( p, *names );
372 strcat( p, EXEEXT );
374 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111))
375 return strarray_init( file );
377 free( file );
378 names++;
380 fatal_error( "cannot find the '%s' tool\n", name );
383 struct strarray *get_as_command(void)
385 struct strarray *args;
387 if (cc_command)
389 args = strarray_copy( cc_command );
390 strarray_add( args, "-xassembler", "-c", NULL );
391 if (force_pointer_size)
392 strarray_add_one( args, (force_pointer_size == 8) ? "-m64" : "-m32" );
393 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
394 if (arch_option) strarray_add_one( args, strmake("-march=%s", arch_option) );
395 return args;
398 if (!as_command)
400 static const char * const commands[] = { "gas", "as", NULL };
401 as_command = find_tool( "as", commands );
404 args = strarray_copy( as_command );
406 if (force_pointer_size)
408 switch (target_platform)
410 case PLATFORM_APPLE:
411 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
412 break;
413 default:
414 switch(target_cpu)
416 case CPU_POWERPC:
417 strarray_add_one( args, (force_pointer_size == 8) ? "-a64" : "-a32" );
418 break;
419 default:
420 strarray_add_one( args, (force_pointer_size == 8) ? "--64" : "--32" );
421 break;
423 break;
427 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
428 return args;
431 struct strarray *get_ld_command(void)
433 struct strarray *args;
435 if (!ld_command)
437 static const char * const commands[] = { "ld", "gld", NULL };
438 ld_command = find_tool( "ld", commands );
441 args = strarray_copy( ld_command );
443 if (force_pointer_size)
445 switch (target_platform)
447 case PLATFORM_APPLE:
448 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
449 break;
450 case PLATFORM_FREEBSD:
451 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
452 break;
453 default:
454 switch(target_cpu)
456 case CPU_POWERPC:
457 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
458 break;
459 default:
460 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
461 break;
463 break;
466 return args;
469 const char *get_nm_command(void)
471 if (!nm_command)
473 static const char * const commands[] = { "nm", "gnm", NULL };
474 nm_command = find_tool( "nm", commands );
476 if (nm_command->count > 1)
477 fatal_error( "multiple arguments in nm command not supported yet\n" );
478 return nm_command->str[0];
481 /* get a name for a temp file, automatically cleaned up on exit */
482 char *get_temp_file_name( const char *prefix, const char *suffix )
484 char *name;
485 const char *ext, *basename;
486 int fd;
488 if (!prefix || !prefix[0]) prefix = "winebuild";
489 if (!suffix) suffix = "";
490 if ((basename = strrchr( prefix, '/' ))) basename++;
491 else basename = prefix;
492 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
493 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
494 memcpy( name, prefix, ext - prefix );
495 strcpy( name + (ext - prefix), ".XXXXXX" );
496 strcat( name, suffix );
498 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
500 strcpy( name, "/tmp/" );
501 memcpy( name + 5, basename, ext - basename );
502 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
503 strcat( name, suffix );
504 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
505 fatal_error( "could not generate a temp file\n" );
508 close( fd );
509 if (nb_tmp_files >= max_tmp_files)
511 max_tmp_files = max( 2 * max_tmp_files, 8 );
512 tmp_files = xrealloc( tmp_files, max_tmp_files * sizeof(tmp_files[0]) );
514 tmp_files[nb_tmp_files++] = name;
515 return name;
518 /*******************************************************************
519 * buffer management
521 * Function for reading from/writing to a memory buffer.
524 int byte_swapped = 0;
525 const char *input_buffer_filename;
526 const unsigned char *input_buffer;
527 size_t input_buffer_pos;
528 size_t input_buffer_size;
529 unsigned char *output_buffer;
530 size_t output_buffer_pos;
531 size_t output_buffer_size;
533 static void check_output_buffer_space( size_t size )
535 if (output_buffer_pos + size >= output_buffer_size)
537 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
538 output_buffer = xrealloc( output_buffer, output_buffer_size );
542 void init_input_buffer( const char *file )
544 int fd;
545 struct stat st;
547 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
548 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
549 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
550 #ifdef HAVE_MMAP
551 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
552 #endif
554 unsigned char *buffer = xmalloc( st.st_size );
555 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
556 input_buffer = buffer;
558 close( fd );
559 input_buffer_filename = xstrdup( file );
560 input_buffer_size = st.st_size;
561 input_buffer_pos = 0;
562 byte_swapped = 0;
565 void init_output_buffer(void)
567 output_buffer_size = 1024;
568 output_buffer_pos = 0;
569 output_buffer = xmalloc( output_buffer_size );
572 void flush_output_buffer(void)
574 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
575 fatal_error( "Error writing to %s\n", output_file_name );
576 free( output_buffer );
579 unsigned char get_byte(void)
581 if (input_buffer_pos >= input_buffer_size)
582 fatal_error( "%s is a truncated file\n", input_buffer_filename );
583 return input_buffer[input_buffer_pos++];
586 unsigned short get_word(void)
588 unsigned short ret;
590 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
591 fatal_error( "%s is a truncated file\n", input_buffer_filename );
592 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
593 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
594 input_buffer_pos += sizeof(ret);
595 return ret;
598 unsigned int get_dword(void)
600 unsigned int ret;
602 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
603 fatal_error( "%s is a truncated file\n", input_buffer_filename );
604 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
605 if (byte_swapped)
606 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
607 input_buffer_pos += sizeof(ret);
608 return ret;
611 void put_data( const void *data, size_t size )
613 check_output_buffer_space( size );
614 memcpy( output_buffer + output_buffer_pos, data, size );
615 output_buffer_pos += size;
618 void put_byte( unsigned char val )
620 check_output_buffer_space( 1 );
621 output_buffer[output_buffer_pos++] = val;
624 void put_word( unsigned short val )
626 if (byte_swapped) val = (val << 8) | (val >> 8);
627 put_data( &val, sizeof(val) );
630 void put_dword( unsigned int val )
632 if (byte_swapped)
633 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
634 put_data( &val, sizeof(val) );
637 void put_qword( unsigned int val )
639 if (byte_swapped)
641 put_dword( 0 );
642 put_dword( val );
644 else
646 put_dword( val );
647 put_dword( 0 );
651 /* pointer-sized word */
652 void put_pword( unsigned int val )
654 if (get_ptr_size() == 8) put_qword( val );
655 else put_dword( val );
658 void align_output( unsigned int align )
660 size_t size = align - (output_buffer_pos % align);
662 if (size == align) return;
663 check_output_buffer_space( size );
664 memset( output_buffer + output_buffer_pos, 0, size );
665 output_buffer_pos += size;
668 /* output a standard header for generated files */
669 void output_standard_file_header(void)
671 if (spec_file_name)
672 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
673 else
674 output( "/* File generated automatically; do not edit! */\n" );
675 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
678 /* dump a byte stream into the assembly code */
679 void dump_bytes( const void *buffer, unsigned int size )
681 unsigned int i;
682 const unsigned char *ptr = buffer;
684 if (!size) return;
685 output( "\t.byte " );
686 for (i = 0; i < size - 1; i++, ptr++)
688 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
689 else output( "0x%02x,", *ptr );
691 output( "0x%02x\n", *ptr );
695 /*******************************************************************
696 * open_input_file
698 * Open a file in the given srcdir and set the input_file_name global variable.
700 FILE *open_input_file( const char *srcdir, const char *name )
702 char *fullname;
703 FILE *file = fopen( name, "r" );
705 if (!file && srcdir)
707 fullname = strmake( "%s/%s", srcdir, name );
708 file = fopen( fullname, "r" );
710 else fullname = xstrdup( name );
712 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
713 input_file_name = fullname;
714 current_line = 1;
715 return file;
719 /*******************************************************************
720 * close_input_file
722 * Close the current input file (must have been opened with open_input_file).
724 void close_input_file( FILE *file )
726 fclose( file );
727 free( input_file_name );
728 input_file_name = NULL;
729 current_line = 0;
733 /*******************************************************************
734 * remove_stdcall_decoration
736 * Remove a possible @xx suffix from a function name.
737 * Return the numerical value of the suffix, or -1 if none.
739 int remove_stdcall_decoration( char *name )
741 char *p, *end = strrchr( name, '@' );
742 if (!end || !end[1] || end == name) return -1;
743 if (target_cpu != CPU_x86) return -1;
744 /* make sure all the rest is digits */
745 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
746 *end = 0;
747 return atoi( end + 1 );
751 /*******************************************************************
752 * assemble_file
754 * Run a file through the assembler.
756 void assemble_file( const char *src_file, const char *obj_file )
758 struct strarray *args = get_as_command();
759 strarray_add( args, "-o", obj_file, src_file, NULL );
760 spawn( args );
761 strarray_free( args );
765 /*******************************************************************
766 * alloc_dll_spec
768 * Create a new dll spec file descriptor
770 DLLSPEC *alloc_dll_spec(void)
772 DLLSPEC *spec;
774 spec = xmalloc( sizeof(*spec) );
775 spec->file_name = NULL;
776 spec->dll_name = NULL;
777 spec->init_func = NULL;
778 spec->main_module = NULL;
779 spec->type = SPEC_WIN32;
780 spec->base = MAX_ORDINALS;
781 spec->limit = 0;
782 spec->stack_size = 0;
783 spec->heap_size = 0;
784 spec->nb_entry_points = 0;
785 spec->alloc_entry_points = 0;
786 spec->nb_names = 0;
787 spec->nb_resources = 0;
788 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
789 if (get_ptr_size() > 4)
790 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
791 else
792 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
793 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
794 spec->subsystem = 0;
795 spec->subsystem_major = 4;
796 spec->subsystem_minor = 0;
797 spec->entry_points = NULL;
798 spec->names = NULL;
799 spec->ordinals = NULL;
800 spec->resources = NULL;
801 return spec;
805 /*******************************************************************
806 * free_dll_spec
808 * Free dll spec file descriptor
810 void free_dll_spec( DLLSPEC *spec )
812 int i;
814 for (i = 0; i < spec->nb_entry_points; i++)
816 ORDDEF *odp = &spec->entry_points[i];
817 free( odp->name );
818 free( odp->export_name );
819 free( odp->link_name );
821 free( spec->file_name );
822 free( spec->dll_name );
823 free( spec->init_func );
824 free( spec->entry_points );
825 free( spec->names );
826 free( spec->ordinals );
827 free( spec->resources );
828 free( spec );
832 /*******************************************************************
833 * make_c_identifier
835 * Map a string to a valid C identifier.
837 const char *make_c_identifier( const char *str )
839 static char buffer[256];
840 char *p;
842 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
844 if (isalnum(*str)) *p = *str;
845 else *p = '_';
847 *p = 0;
848 return buffer;
852 /*******************************************************************
853 * get_stub_name
855 * Generate an internal name for a stub entry point.
857 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
859 static char *buffer;
861 free( buffer );
862 if (odp->name || odp->export_name)
864 char *p;
865 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
866 /* make sure name is a legal C identifier */
867 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
868 if (!*p) return buffer;
869 free( buffer );
871 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
872 return buffer;
875 /* parse a cpu name and return the corresponding value */
876 int get_cpu_from_name( const char *name )
878 unsigned int i;
880 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
881 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
882 return -1;
885 /*****************************************************************
886 * Function: get_alignment
888 * Description:
889 * According to the info page for gas, the .align directive behaves
890 * differently on different systems. On some architectures, the
891 * argument of a .align directive is the number of bytes to pad to, so
892 * to align on an 8-byte boundary you'd say
893 * .align 8
894 * On other systems, the argument is "the number of low-order zero bits
895 * that the location counter must have after advancement." So to
896 * align on an 8-byte boundary you'd say
897 * .align 3
899 * The reason gas is written this way is that it's trying to mimick
900 * native assemblers for the various architectures it runs on. gas
901 * provides other directives that work consistently across
902 * architectures, but of course we want to work on all arches with or
903 * without gas. Hence this function.
906 * Parameters:
907 * align -- the number of bytes to align to. Must be a power of 2.
909 unsigned int get_alignment(unsigned int align)
911 unsigned int n;
913 assert( !(align & (align - 1)) );
915 switch(target_cpu)
917 case CPU_x86:
918 case CPU_x86_64:
919 if (target_platform != PLATFORM_APPLE) return align;
920 /* fall through */
921 case CPU_POWERPC:
922 case CPU_ARM:
923 case CPU_ARM64:
924 n = 0;
925 while ((1u << n) != align) n++;
926 return n;
928 /* unreached */
929 assert(0);
930 return 0;
933 /* return the page size for the target CPU */
934 unsigned int get_page_size(void)
936 switch(target_cpu)
938 case CPU_x86:
939 case CPU_x86_64:
940 case CPU_POWERPC:
941 case CPU_ARM:
942 return 0x1000;
943 case CPU_ARM64:
944 return 0x10000;
946 /* unreached */
947 assert(0);
948 return 0;
951 /* return the size of a pointer on the target CPU */
952 unsigned int get_ptr_size(void)
954 switch(target_cpu)
956 case CPU_x86:
957 case CPU_POWERPC:
958 case CPU_ARM:
959 return 4;
960 case CPU_x86_64:
961 case CPU_ARM64:
962 return 8;
964 /* unreached */
965 assert(0);
966 return 0;
969 /* return the total size in bytes of the arguments on the stack */
970 unsigned int get_args_size( const ORDDEF *odp )
972 int i, size;
974 for (i = size = 0; i < odp->u.func.nb_args; i++)
976 switch (odp->u.func.args[i])
978 case ARG_INT64:
979 case ARG_DOUBLE:
980 size += 8;
981 break;
982 case ARG_INT128:
983 /* int128 is passed as pointer on x86_64 */
984 if (target_cpu != CPU_x86_64)
986 size += 16;
987 break;
989 /* fall through */
990 default:
991 size += get_ptr_size();
992 break;
995 return size;
998 /* return the assembly name for a C symbol */
999 const char *asm_name( const char *sym )
1001 static char *buffer;
1003 switch (target_platform)
1005 case PLATFORM_APPLE:
1006 case PLATFORM_WINDOWS:
1007 if (sym[0] == '.' && sym[1] == 'L') return sym;
1008 free( buffer );
1009 buffer = strmake( "_%s", sym );
1010 return buffer;
1011 default:
1012 return sym;
1016 /* return an assembly function declaration for a C function name */
1017 const char *func_declaration( const char *func )
1019 static char *buffer;
1021 switch (target_platform)
1023 case PLATFORM_APPLE:
1024 return "";
1025 case PLATFORM_WINDOWS:
1026 free( buffer );
1027 buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
1028 break;
1029 default:
1030 free( buffer );
1031 switch(target_cpu)
1033 case CPU_ARM:
1034 case CPU_ARM64:
1035 buffer = strmake( ".type %s,%%function", func );
1036 break;
1037 default:
1038 buffer = strmake( ".type %s,@function", func );
1039 break;
1041 break;
1043 return buffer;
1046 /* output a size declaration for an assembly function */
1047 void output_function_size( const char *name )
1049 switch (target_platform)
1051 case PLATFORM_APPLE:
1052 case PLATFORM_WINDOWS:
1053 break;
1054 default:
1055 output( "\t.size %s, .-%s\n", name, name );
1056 break;
1060 /* output a .cfi directive */
1061 void output_cfi( const char *format, ... )
1063 va_list valist;
1065 if (!unwind_tables) return;
1066 va_start( valist, format );
1067 fputc( '\t', output_file );
1068 vfprintf( output_file, format, valist );
1069 fputc( '\n', output_file );
1070 va_end( valist );
1073 /* output the GNU note for non-exec stack */
1074 void output_gnu_stack_note(void)
1076 switch (target_platform)
1078 case PLATFORM_WINDOWS:
1079 case PLATFORM_APPLE:
1080 break;
1081 default:
1082 switch(target_cpu)
1084 case CPU_ARM:
1085 case CPU_ARM64:
1086 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1087 break;
1088 default:
1089 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1090 break;
1092 break;
1096 /* return a global symbol declaration for an assembly symbol */
1097 const char *asm_globl( const char *func )
1099 static char *buffer;
1101 free( buffer );
1102 switch (target_platform)
1104 case PLATFORM_APPLE:
1105 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1106 break;
1107 case PLATFORM_WINDOWS:
1108 buffer = strmake( "\t.globl _%s\n_%s:", func, func );
1109 break;
1110 default:
1111 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1112 break;
1114 return buffer;
1117 const char *get_asm_ptr_keyword(void)
1119 switch(get_ptr_size())
1121 case 4: return ".long";
1122 case 8: return ".quad";
1124 assert(0);
1125 return NULL;
1128 const char *get_asm_string_keyword(void)
1130 switch (target_platform)
1132 case PLATFORM_APPLE:
1133 return ".asciz";
1134 default:
1135 return ".string";
1139 const char *get_asm_rodata_section(void)
1141 switch (target_platform)
1143 case PLATFORM_APPLE: return ".const";
1144 default: return ".section .rodata";
1148 const char *get_asm_string_section(void)
1150 switch (target_platform)
1152 case PLATFORM_APPLE: return ".cstring";
1153 default: return ".section .rodata";