qdvd: Add version resource.
[wine.git] / tools / winebuild / utils.c
blob1af42f10005fce4545f865cf0e265e1b334d4f8e
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
37 #include "build.h"
39 #if defined(_WIN32) && !defined(__CYGWIN__)
40 # define PATH_SEPARATOR ';'
41 #else
42 # define PATH_SEPARATOR ':'
43 #endif
45 static struct strarray tmp_files;
46 static struct strarray empty_strarray;
47 static const char *output_file_source_name;
49 static const struct
51 const char *name;
52 enum target_cpu cpu;
53 } cpu_names[] =
55 { "i386", CPU_x86 },
56 { "i486", CPU_x86 },
57 { "i586", CPU_x86 },
58 { "i686", CPU_x86 },
59 { "i786", CPU_x86 },
60 { "amd64", CPU_x86_64 },
61 { "x86_64", CPU_x86_64 },
62 { "powerpc", CPU_POWERPC },
63 { "arm", CPU_ARM },
64 { "armv5", CPU_ARM },
65 { "armv6", CPU_ARM },
66 { "armv7", CPU_ARM },
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)
75 unsigned int i;
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)
82 void *res;
84 res = malloc (size ? size : 1);
85 if (res == NULL)
87 fprintf (stderr, "Virtual memory exhausted.\n");
88 exit (1);
90 return res;
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");
99 exit (1);
101 return res;
104 char *xstrdup( const char *str )
106 char *res = strdup( str );
107 if (!res)
109 fprintf (stderr, "Virtual memory exhausted.\n");
110 exit (1);
112 return res;
115 char *strupper(char *s)
117 char *p;
118 for (p = s; *p; p++) *p = toupper(*p);
119 return s;
122 int strendswith(const char* str, const char* end)
124 int l = strlen(str);
125 int m = strlen(end);
126 return l >= m && strcmp(str + l - m, end) == 0;
129 char *strmake( const char* fmt, ... )
131 int n;
132 size_t size = 100;
133 va_list ap;
135 for (;;)
137 char *p = xmalloc( size );
138 va_start( ap, fmt );
139 n = vsnprintf( p, size, fmt, ap );
140 va_end( ap );
141 if (n == -1) size *= 2;
142 else if ((size_t)n >= size) size = n + 1;
143 else return p;
144 free( p );
148 static struct strarray strarray_copy( struct strarray src )
150 struct strarray array;
151 array.count = src.count;
152 array.max = src.max;
153 array.str = xmalloc( array.max * sizeof(*array.str) );
154 memcpy( array.str, src.str, array.count * sizeof(*array.str) );
155 return array;
158 static void strarray_add_one( struct strarray *array, const char *str )
160 if (array->count == array->max)
162 array->max *= 2;
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, ... )
171 va_list valist;
172 const char *str;
174 va_start( valist, array );
175 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
176 va_end( valist );
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 )
186 unsigned int i;
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 )
193 const char *tok;
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 ));
200 free( buf );
201 return array;
204 void fatal_error( const char *msg, ... )
206 va_list valist;
207 va_start( valist, msg );
208 if (input_file_name)
210 fprintf( stderr, "%s:", input_file_name );
211 if (current_line)
212 fprintf( stderr, "%d:", current_line );
213 fputc( ' ', stderr );
215 else fprintf( stderr, "winebuild: " );
216 vfprintf( stderr, msg, valist );
217 va_end( valist );
218 exit(1);
221 void fatal_perror( const char *msg, ... )
223 va_list valist;
224 va_start( valist, msg );
225 if (input_file_name)
227 fprintf( stderr, "%s:", input_file_name );
228 if (current_line)
229 fprintf( stderr, "%d:", current_line );
230 fputc( ' ', stderr );
232 vfprintf( stderr, msg, valist );
233 perror( " " );
234 va_end( valist );
235 exit(1);
238 void error( const char *msg, ... )
240 va_list valist;
241 va_start( valist, msg );
242 if (input_file_name)
244 fprintf( stderr, "%s:", input_file_name );
245 if (current_line)
246 fprintf( stderr, "%d:", current_line );
247 fputc( ' ', stderr );
249 vfprintf( stderr, msg, valist );
250 va_end( valist );
251 nb_errors++;
254 void warning( const char *msg, ... )
256 va_list valist;
258 if (!display_warnings) return;
259 va_start( valist, msg );
260 if (input_file_name)
262 fprintf( stderr, "%s:", input_file_name );
263 if (current_line)
264 fprintf( stderr, "%d:", current_line );
265 fputc( ' ', stderr );
267 fprintf( stderr, "warning: " );
268 vfprintf( stderr, msg, valist );
269 va_end( valist );
272 int output( const char *format, ... )
274 int ret;
275 va_list valist;
277 va_start( valist, format );
278 ret = vfprintf( output_file, format, valist );
279 va_end( valist );
280 if (ret < 0) fatal_perror( "Output error" );
281 return ret;
284 static struct strarray get_tools_path(void)
286 static int done;
287 static struct strarray dirs;
289 if (!done)
291 dirs = strarray_copy( tools_path );
293 /* then append the PATH directories */
294 if (getenv( "PATH" ))
296 char *p = xstrdup( getenv( "PATH" ));
297 while (*p)
299 strarray_add_one( &dirs, p );
300 while (*p && *p != PATH_SEPARATOR) p++;
301 if (!*p) break;
302 *p++ = 0;
305 done = 1;
307 return dirs;
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;
315 struct stat st;
316 char *p, *file;
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++ = '/';
329 if (*prefix)
331 strcpy( p, prefix );
332 p += strlen(p);
333 *p++ = '-';
335 strcpy( p, name );
336 strcat( p, EXEEXT );
337 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
339 free( file );
340 return NULL;
343 void spawn( struct strarray args )
345 unsigned int i;
346 int status;
347 const char *argv0 = find_binary( NULL, args.str[0] );
349 if (argv0) args.str[0] = argv0;
350 strarray_add_one( &args, NULL );
351 if (verbose)
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" );
359 exit( 1 );
363 /* find a build tool in the path, trying the various names */
364 struct strarray find_tool( const char *name, const char * const *names )
366 const char *file;
367 const char *alt_names[2];
369 if (!names)
371 alt_names[0] = name;
372 alt_names[1] = NULL;
373 names = alt_names;
376 while (*names)
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 );
383 return ret;
385 names++;
387 fatal_error( "cannot find the '%s' tool\n", name );
390 struct strarray get_as_command(void)
392 struct strarray args;
393 unsigned int i;
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] ));
406 return args;
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)
421 case PLATFORM_APPLE:
422 strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
423 break;
424 default:
425 switch(target_cpu)
427 case CPU_POWERPC:
428 strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
429 break;
430 default:
431 strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
432 break;
434 break;
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) );
440 return args;
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)
459 case PLATFORM_APPLE:
460 strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
461 break;
462 case PLATFORM_FREEBSD:
463 strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
464 break;
465 case PLATFORM_WINDOWS:
466 strarray_add( &args, "-m", (force_pointer_size == 8) ? "i386pep" : "i386pe", NULL );
467 break;
468 default:
469 switch(target_cpu)
471 case CPU_POWERPC:
472 strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
473 break;
474 default:
475 strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
476 break;
478 break;
482 if (target_cpu == CPU_ARM && target_platform != PLATFORM_WINDOWS)
483 strarray_add( &args, "--no-wchar-size-warning", NULL );
485 return args;
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 )
503 char *name;
504 const char *ext, *basename;
505 int fd;
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" );
527 close( fd );
528 strarray_add_one( &tmp_files, name );
529 return name;
532 /*******************************************************************
533 * buffer management
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 )
558 int fd;
559 struct stat st;
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 );
567 close( fd );
568 input_buffer_filename = xstrdup( file );
569 input_buffer_size = st.st_size;
570 input_buffer_pos = 0;
571 byte_swapped = 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)
583 open_output_file();
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 );
586 close_output_file();
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)
599 unsigned short ret;
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);
606 return ret;
609 unsigned int get_dword(void)
611 unsigned int ret;
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)
617 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
618 input_buffer_pos += sizeof(ret);
619 return 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 )
643 if (byte_swapped)
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 )
650 if (byte_swapped)
652 put_dword( 0 );
653 put_dword( val );
655 else
657 put_dword( val );
658 put_dword( 0 );
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)
682 if (spec_file_name)
683 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
684 else
685 output( "/* File generated automatically; do not edit! */\n" );
686 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
687 if (safe_seh)
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 )
698 unsigned int i;
699 const unsigned char *ptr = buffer;
701 if (!size) return;
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 /*******************************************************************
713 * open_input_file
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 )
719 char *fullname;
720 FILE *file = fopen( name, "r" );
722 if (!file && srcdir)
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;
731 current_line = 1;
732 return file;
736 /*******************************************************************
737 * close_input_file
739 * Close the current input file (must have been opened with open_input_file).
741 void close_input_file( FILE *file )
743 fclose( file );
744 free( input_file_name );
745 input_file_name = NULL;
746 current_line = 0;
750 /*******************************************************************
751 * open_output_file
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" );
759 else
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 /*******************************************************************
768 * close_output_file
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 );
775 output_file = NULL;
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 );
787 return 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;
804 *end = 0;
805 return atoi( end + 1 );
809 /*******************************************************************
810 * assemble_file
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 );
818 spawn( args );
822 /*******************************************************************
823 * alloc_dll_spec
825 * Create a new dll spec file descriptor
827 DLLSPEC *alloc_dll_spec(void)
829 DLLSPEC *spec;
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;
836 spec->subsystem = 0;
837 spec->subsystem_major = 4;
838 spec->subsystem_minor = 0;
839 if (get_ptr_size() > 4)
840 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
841 else
842 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
843 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
844 return spec;
848 /*******************************************************************
849 * free_dll_spec
851 * Free dll spec file descriptor
853 void free_dll_spec( DLLSPEC *spec )
855 int i;
857 for (i = 0; i < spec->nb_entry_points; i++)
859 ORDDEF *odp = &spec->entry_points[i];
860 free( odp->name );
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 );
869 free( spec->names );
870 free( spec->ordinals );
871 free( spec->resources );
872 free( spec );
876 /*******************************************************************
877 * make_c_identifier
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;
888 else *p = '_';
890 *p = 0;
891 return xstrdup( buffer );
895 /*******************************************************************
896 * get_stub_name
898 * Generate an internal name for a stub entry point.
900 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
902 static char *buffer;
904 free( buffer );
905 if (odp->name || odp->export_name)
907 char *p;
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;
912 free( buffer );
914 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
915 return buffer;
918 /* return the stdcall-decorated name for an entry point */
919 const char *get_link_name( const ORDDEF *odp )
921 static char *buffer;
922 char *ret;
924 if (target_cpu != CPU_x86) return odp->link_name;
926 switch (odp->type)
928 case TYPE_STDCALL:
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;
936 else
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;
942 break;
944 case TYPE_PASCAL:
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;
952 break;
954 default:
955 return odp->link_name;
958 free( buffer );
959 buffer = ret;
960 return ret;
963 /*******************************************************************
964 * sort_func_list
966 * Sort a list of functions, removing duplicates.
968 int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
970 int i, j;
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];
975 return j + 1;
979 /* parse a cpu name and return the corresponding value */
980 int get_cpu_from_name( const char *name )
982 unsigned int i;
984 for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
985 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
986 return -1;
989 /*****************************************************************
990 * Function: get_alignment
992 * Description:
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
997 * .align 8
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
1001 * .align 3
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.
1010 * Parameters:
1011 * align -- the number of bytes to align to. Must be a power of 2.
1013 unsigned int get_alignment(unsigned int align)
1015 unsigned int n;
1017 assert( !(align & (align - 1)) );
1019 switch(target_cpu)
1021 case CPU_x86:
1022 case CPU_x86_64:
1023 if (target_platform != PLATFORM_APPLE) return align;
1024 /* fall through */
1025 case CPU_POWERPC:
1026 case CPU_ARM:
1027 case CPU_ARM64:
1028 n = 0;
1029 while ((1u << n) != align) n++;
1030 return n;
1032 /* unreached */
1033 assert(0);
1034 return 0;
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)
1046 switch(target_cpu)
1048 case CPU_x86:
1049 case CPU_POWERPC:
1050 case CPU_ARM:
1051 return 4;
1052 case CPU_x86_64:
1053 case CPU_ARM64:
1054 return 8;
1056 /* unreached */
1057 assert(0);
1058 return 0;
1061 /* return the total size in bytes of the arguments on the stack */
1062 unsigned int get_args_size( const ORDDEF *odp )
1064 int i, size;
1066 for (i = size = 0; i < odp->u.func.nb_args; i++)
1068 switch (odp->u.func.args[i])
1070 case ARG_INT64:
1071 case ARG_DOUBLE:
1072 size += 8;
1073 break;
1074 case ARG_INT128:
1075 /* int128 is passed as pointer on x86_64 */
1076 if (target_cpu != CPU_x86_64)
1078 size += 16;
1079 break;
1081 /* fall through */
1082 default:
1083 size += get_ptr_size();
1084 break;
1087 return 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 */
1100 /* fall through */
1101 case PLATFORM_APPLE:
1102 if (sym[0] == '.' && sym[1] == 'L') return sym;
1103 free( buffer );
1104 buffer = strmake( "_%s", sym );
1105 return buffer;
1106 default:
1107 return 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:
1119 return "";
1120 case PLATFORM_WINDOWS:
1121 free( buffer );
1122 buffer = strmake( ".def %s; .scl 2; .type 32; .endef", asm_name(func) );
1123 break;
1124 default:
1125 free( buffer );
1126 switch(target_cpu)
1128 case CPU_ARM:
1129 case CPU_ARM64:
1130 buffer = strmake( ".type %s,%%function", func );
1131 break;
1132 default:
1133 buffer = strmake( ".type %s,@function", func );
1134 break;
1136 break;
1138 return buffer;
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:
1148 break;
1149 default:
1150 output( "\t.size %s, .-%s\n", name, name );
1151 break;
1155 /* output a .cfi directive */
1156 void output_cfi( const char *format, ... )
1158 va_list valist;
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 );
1165 va_end( valist );
1168 /* output an RVA pointer */
1169 void output_rva( const char *format, ... )
1171 va_list valist;
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 );
1180 break;
1181 default:
1182 output( "\t.long " );
1183 vfprintf( output_file, format, valist );
1184 output( " - .L__wine_spec_rva_base\n" );
1185 break;
1187 va_end( valist );
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:
1197 break;
1198 default:
1199 switch(target_cpu)
1201 case CPU_ARM:
1202 case CPU_ARM64:
1203 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1204 break;
1205 default:
1206 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1207 break;
1209 break;
1213 /* return a global symbol declaration for an assembly symbol */
1214 const char *asm_globl( const char *func )
1216 static char *buffer;
1218 free( 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 );
1223 break;
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 );
1227 break;
1228 default:
1229 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1230 break;
1232 return buffer;
1235 const char *get_asm_ptr_keyword(void)
1237 switch(get_ptr_size())
1239 case 4: return ".long";
1240 case 8: return ".quad";
1242 assert(0);
1243 return NULL;
1246 const char *get_asm_string_keyword(void)
1248 switch (target_platform)
1250 case PLATFORM_APPLE:
1251 return ".asciz";
1252 default:
1253 return ".string";
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:
1302 free( buffer );
1303 buffer = strmake( "%s@PAGE", sym );
1304 return buffer;
1305 default:
1306 return sym;
1310 const char *arm64_pageoff( const char *sym )
1312 static char *buffer;
1314 free( buffer );
1315 switch (target_platform)
1317 case PLATFORM_APPLE:
1318 buffer = strmake( "%s@PAGEOFF", sym );
1319 break;
1320 default:
1321 buffer = strmake( ":lo12:%s", sym );
1322 break;
1324 return buffer;