mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / tools / winebuild / utils.c
blob411a53c7133bf759e292fa80ef0af81b4fef3803
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 /* find a link tool in the path */
391 struct strarray find_link_tool(void)
393 struct strarray ret = empty_strarray;
394 const char *file;
395 if (!(file = find_binary( NULL, "lld-link" )))
396 fatal_error( "cannot find the 'lld-link tool\n" );
397 strarray_add_one( &ret, file );
398 return ret;
401 struct strarray get_as_command(void)
403 struct strarray args;
404 unsigned int i;
406 if (cc_command.count)
408 args = strarray_copy( cc_command );
409 strarray_add( &args, "-xassembler", "-c", NULL );
410 if (force_pointer_size)
411 strarray_add_one( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
412 if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
413 if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
414 if (arch_option) strarray_add_one( &args, strmake("-march=%s", arch_option) );
415 for (i = 0; i < tools_path.count; i++)
416 strarray_add_one( &args, strmake("-B%s", tools_path.str[i] ));
417 return args;
420 if (!as_command.count)
422 static const char * const commands[] = { "gas", "as", NULL };
423 as_command = find_tool( "as", commands );
426 args = strarray_copy( as_command );
428 if (force_pointer_size)
430 switch (target_platform)
432 case PLATFORM_APPLE:
433 strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
434 break;
435 default:
436 switch(target_cpu)
438 case CPU_POWERPC:
439 strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
440 break;
441 default:
442 strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
443 break;
445 break;
449 if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
450 if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
451 return args;
454 struct strarray get_ld_command(void)
456 struct strarray args;
458 if (!ld_command.count)
460 static const char * const commands[] = { "ld", "gld", NULL };
461 ld_command = find_tool( "ld", commands );
464 args = strarray_copy( ld_command );
466 if (force_pointer_size)
468 switch (target_platform)
470 case PLATFORM_APPLE:
471 strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
472 break;
473 case PLATFORM_FREEBSD:
474 strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
475 break;
476 case PLATFORM_MINGW:
477 case PLATFORM_WINDOWS:
478 strarray_add( &args, "-m", (force_pointer_size == 8) ? "i386pep" : "i386pe", NULL );
479 break;
480 default:
481 switch(target_cpu)
483 case CPU_POWERPC:
484 strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
485 break;
486 default:
487 strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
488 break;
490 break;
494 if (target_cpu == CPU_ARM && !is_pe())
495 strarray_add( &args, "--no-wchar-size-warning", NULL );
497 return args;
500 const char *get_nm_command(void)
502 if (!nm_command.count)
504 static const char * const commands[] = { "nm", "gnm", NULL };
505 nm_command = find_tool( "nm", commands );
507 if (nm_command.count > 1)
508 fatal_error( "multiple arguments in nm command not supported yet\n" );
509 return nm_command.str[0];
512 /* get a name for a temp file, automatically cleaned up on exit */
513 char *get_temp_file_name( const char *prefix, const char *suffix )
515 char *name;
516 const char *ext, *basename;
517 int fd;
519 if (!prefix || !prefix[0]) prefix = "winebuild";
520 if (!suffix) suffix = "";
521 if ((basename = strrchr( prefix, '/' ))) basename++;
522 else basename = prefix;
523 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
524 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
525 memcpy( name, prefix, ext - prefix );
526 strcpy( name + (ext - prefix), ".XXXXXX" );
527 strcat( name, suffix );
529 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
531 strcpy( name, "/tmp/" );
532 memcpy( name + 5, basename, ext - basename );
533 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
534 strcat( name, suffix );
535 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
536 fatal_error( "could not generate a temp file\n" );
539 close( fd );
540 strarray_add_one( &tmp_files, name );
541 return name;
544 /*******************************************************************
545 * buffer management
547 * Function for reading from/writing to a memory buffer.
550 int byte_swapped = 0;
551 const char *input_buffer_filename;
552 const unsigned char *input_buffer;
553 size_t input_buffer_pos;
554 size_t input_buffer_size;
555 unsigned char *output_buffer;
556 size_t output_buffer_pos;
557 size_t output_buffer_size;
559 static void check_output_buffer_space( size_t size )
561 if (output_buffer_pos + size >= output_buffer_size)
563 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
564 output_buffer = xrealloc( output_buffer, output_buffer_size );
568 void init_input_buffer( const char *file )
570 int fd;
571 struct stat st;
572 unsigned char *buffer;
574 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
575 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
576 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
577 input_buffer = buffer = xmalloc( st.st_size );
578 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
579 close( fd );
580 input_buffer_filename = xstrdup( file );
581 input_buffer_size = st.st_size;
582 input_buffer_pos = 0;
583 byte_swapped = 0;
586 void init_output_buffer(void)
588 output_buffer_size = 1024;
589 output_buffer_pos = 0;
590 output_buffer = xmalloc( output_buffer_size );
593 void flush_output_buffer(void)
595 open_output_file();
596 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
597 fatal_error( "Error writing to %s\n", output_file_name );
598 close_output_file();
599 free( output_buffer );
602 unsigned char get_byte(void)
604 if (input_buffer_pos >= input_buffer_size)
605 fatal_error( "%s is a truncated file\n", input_buffer_filename );
606 return input_buffer[input_buffer_pos++];
609 unsigned short get_word(void)
611 unsigned short 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) ret = (ret << 8) | (ret >> 8);
617 input_buffer_pos += sizeof(ret);
618 return ret;
621 unsigned int get_dword(void)
623 unsigned int ret;
625 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
626 fatal_error( "%s is a truncated file\n", input_buffer_filename );
627 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
628 if (byte_swapped)
629 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
630 input_buffer_pos += sizeof(ret);
631 return ret;
634 void put_data( const void *data, size_t size )
636 check_output_buffer_space( size );
637 memcpy( output_buffer + output_buffer_pos, data, size );
638 output_buffer_pos += size;
641 void put_byte( unsigned char val )
643 check_output_buffer_space( 1 );
644 output_buffer[output_buffer_pos++] = val;
647 void put_word( unsigned short val )
649 if (byte_swapped) val = (val << 8) | (val >> 8);
650 put_data( &val, sizeof(val) );
653 void put_dword( unsigned int val )
655 if (byte_swapped)
656 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
657 put_data( &val, sizeof(val) );
660 void put_qword( unsigned int val )
662 if (byte_swapped)
664 put_dword( 0 );
665 put_dword( val );
667 else
669 put_dword( val );
670 put_dword( 0 );
674 /* pointer-sized word */
675 void put_pword( unsigned int val )
677 if (get_ptr_size() == 8) put_qword( val );
678 else put_dword( val );
681 void align_output( unsigned int align )
683 size_t size = align - (output_buffer_pos % align);
685 if (size == align) return;
686 check_output_buffer_space( size );
687 memset( output_buffer + output_buffer_pos, 0, size );
688 output_buffer_pos += size;
691 /* output a standard header for generated files */
692 void output_standard_file_header(void)
694 if (spec_file_name)
695 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
696 else
697 output( "/* File generated automatically; do not edit! */\n" );
698 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
699 if (safe_seh)
701 output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
702 output( "\t.globl @feat.00\n" );
703 output( ".set @feat.00, 1\n" );
705 if (thumb_mode)
707 output( "\t.syntax unified\n" );
708 output( "\t.thumb\n" );
712 /* dump a byte stream into the assembly code */
713 void dump_bytes( const void *buffer, unsigned int size )
715 unsigned int i;
716 const unsigned char *ptr = buffer;
718 if (!size) return;
719 output( "\t.byte " );
720 for (i = 0; i < size - 1; i++, ptr++)
722 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
723 else output( "0x%02x,", *ptr );
725 output( "0x%02x\n", *ptr );
729 /*******************************************************************
730 * open_input_file
732 * Open a file in the given srcdir and set the input_file_name global variable.
734 FILE *open_input_file( const char *srcdir, const char *name )
736 char *fullname;
737 FILE *file = fopen( name, "r" );
739 if (!file && srcdir)
741 fullname = strmake( "%s/%s", srcdir, name );
742 file = fopen( fullname, "r" );
744 else fullname = xstrdup( name );
746 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
747 input_file_name = fullname;
748 current_line = 1;
749 return file;
753 /*******************************************************************
754 * close_input_file
756 * Close the current input file (must have been opened with open_input_file).
758 void close_input_file( FILE *file )
760 fclose( file );
761 free( input_file_name );
762 input_file_name = NULL;
763 current_line = 0;
767 /*******************************************************************
768 * open_output_file
770 void open_output_file(void)
772 if (output_file_name)
774 if (strendswith( output_file_name, ".o" ))
775 output_file_source_name = open_temp_output_file( ".s" );
776 else
777 if (!(output_file = fopen( output_file_name, "w" )))
778 fatal_error( "Unable to create output file '%s'\n", output_file_name );
780 else output_file = stdout;
784 /*******************************************************************
785 * close_output_file
787 void close_output_file(void)
789 if (!output_file || !output_file_name) return;
790 if (fclose( output_file ) < 0) fatal_perror( "fclose" );
791 if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
792 output_file = NULL;
796 /*******************************************************************
797 * open_temp_output_file
799 char *open_temp_output_file( const char *suffix )
801 char *tmp_file = get_temp_file_name( output_file_name, suffix );
802 if (!(output_file = fopen( tmp_file, "w" )))
803 fatal_error( "Unable to create output file '%s'\n", tmp_file );
804 return tmp_file;
808 /*******************************************************************
809 * remove_stdcall_decoration
811 * Remove a possible @xx suffix from a function name.
812 * Return the numerical value of the suffix, or -1 if none.
814 int remove_stdcall_decoration( char *name )
816 char *p, *end = strrchr( name, '@' );
817 if (!end || !end[1] || end == name) return -1;
818 if (target_cpu != CPU_x86) return -1;
819 /* make sure all the rest is digits */
820 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
821 *end = 0;
822 return atoi( end + 1 );
826 /*******************************************************************
827 * assemble_file
829 * Run a file through the assembler.
831 void assemble_file( const char *src_file, const char *obj_file )
833 struct strarray args = get_as_command();
834 strarray_add( &args, "-o", obj_file, src_file, NULL );
835 spawn( args );
839 /*******************************************************************
840 * alloc_dll_spec
842 * Create a new dll spec file descriptor
844 DLLSPEC *alloc_dll_spec(void)
846 DLLSPEC *spec;
848 spec = xmalloc( sizeof(*spec) );
849 memset( spec, 0, sizeof(*spec) );
850 spec->type = SPEC_WIN32;
851 spec->base = MAX_ORDINALS;
852 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
853 spec->subsystem = 0;
854 spec->subsystem_major = 4;
855 spec->subsystem_minor = 0;
856 if (get_ptr_size() > 4)
857 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
858 else
859 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
860 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
861 return spec;
865 /*******************************************************************
866 * free_dll_spec
868 * Free dll spec file descriptor
870 void free_dll_spec( DLLSPEC *spec )
872 int i;
874 for (i = 0; i < spec->nb_entry_points; i++)
876 ORDDEF *odp = &spec->entry_points[i];
877 free( odp->name );
878 free( odp->export_name );
879 free( odp->link_name );
881 free( spec->file_name );
882 free( spec->dll_name );
883 free( spec->c_name );
884 free( spec->init_func );
885 free( spec->entry_points );
886 free( spec->names );
887 free( spec->ordinals );
888 free( spec->resources );
889 free( spec );
893 /*******************************************************************
894 * make_c_identifier
896 * Map a string to a valid C identifier.
898 char *make_c_identifier( const char *str )
900 char *p, buffer[256];
902 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
904 if (isalnum(*str)) *p = *str;
905 else *p = '_';
907 *p = 0;
908 return xstrdup( buffer );
912 /*******************************************************************
913 * get_stub_name
915 * Generate an internal name for a stub entry point.
917 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
919 static char *buffer;
921 free( buffer );
922 if (odp->name || odp->export_name)
924 char *p;
925 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
926 /* make sure name is a legal C identifier */
927 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
928 if (!*p) return buffer;
929 free( buffer );
931 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
932 return buffer;
935 /* return the stdcall-decorated name for an entry point */
936 const char *get_link_name( const ORDDEF *odp )
938 static char *buffer;
939 char *ret;
941 if (target_cpu != CPU_x86) return odp->link_name;
943 switch (odp->type)
945 case TYPE_STDCALL:
946 if (is_pe())
948 if (odp->flags & FLAG_THISCALL) return odp->link_name;
949 if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", odp->link_name, get_args_size( odp ));
950 else if (!kill_at) ret = strmake( "%s@%u", odp->link_name, get_args_size( odp ));
951 else return odp->link_name;
953 else
955 if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", odp->link_name );
956 else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", odp->link_name );
957 else return odp->link_name;
959 break;
961 case TYPE_PASCAL:
962 if (is_pe() && !kill_at)
964 int args = get_args_size( odp );
965 if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
966 ret = strmake( "%s@%u", odp->link_name, args );
968 else return odp->link_name;
969 break;
971 default:
972 return odp->link_name;
975 free( buffer );
976 buffer = ret;
977 return ret;
980 /*******************************************************************
981 * sort_func_list
983 * Sort a list of functions, removing duplicates.
985 int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
987 int i, j;
989 if (!count) return 0;
990 qsort( list, count, sizeof(*list), compare );
991 for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
992 return j + 1;
996 /* parse a cpu name and return the corresponding value */
997 int get_cpu_from_name( const char *name )
999 unsigned int i;
1001 for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
1002 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
1003 return -1;
1006 /*****************************************************************
1007 * Function: get_alignment
1009 * Description:
1010 * According to the info page for gas, the .align directive behaves
1011 * differently on different systems. On some architectures, the
1012 * argument of a .align directive is the number of bytes to pad to, so
1013 * to align on an 8-byte boundary you'd say
1014 * .align 8
1015 * On other systems, the argument is "the number of low-order zero bits
1016 * that the location counter must have after advancement." So to
1017 * align on an 8-byte boundary you'd say
1018 * .align 3
1020 * The reason gas is written this way is that it's trying to mimic
1021 * native assemblers for the various architectures it runs on. gas
1022 * provides other directives that work consistently across
1023 * architectures, but of course we want to work on all arches with or
1024 * without gas. Hence this function.
1027 * Parameters:
1028 * align -- the number of bytes to align to. Must be a power of 2.
1030 unsigned int get_alignment(unsigned int align)
1032 unsigned int n;
1034 assert( !(align & (align - 1)) );
1036 switch(target_cpu)
1038 case CPU_x86:
1039 case CPU_x86_64:
1040 if (target_platform != PLATFORM_APPLE) return align;
1041 /* fall through */
1042 case CPU_POWERPC:
1043 case CPU_ARM:
1044 case CPU_ARM64:
1045 n = 0;
1046 while ((1u << n) != align) n++;
1047 return n;
1049 /* unreached */
1050 assert(0);
1051 return 0;
1054 /* return the page size for the target CPU */
1055 unsigned int get_page_size(void)
1057 return 0x1000; /* same on all platforms */
1060 /* return the size of a pointer on the target CPU */
1061 unsigned int get_ptr_size(void)
1063 switch(target_cpu)
1065 case CPU_x86:
1066 case CPU_POWERPC:
1067 case CPU_ARM:
1068 return 4;
1069 case CPU_x86_64:
1070 case CPU_ARM64:
1071 return 8;
1073 /* unreached */
1074 assert(0);
1075 return 0;
1078 /* return the total size in bytes of the arguments on the stack */
1079 unsigned int get_args_size( const ORDDEF *odp )
1081 int i, size;
1083 for (i = size = 0; i < odp->u.func.nb_args; i++)
1085 switch (odp->u.func.args[i])
1087 case ARG_INT64:
1088 case ARG_DOUBLE:
1089 size += 8;
1090 break;
1091 case ARG_INT128:
1092 /* int128 is passed as pointer on x86_64 */
1093 if (target_cpu != CPU_x86_64)
1095 size += 16;
1096 break;
1098 /* fall through */
1099 default:
1100 size += get_ptr_size();
1101 break;
1104 return size;
1107 /* return the assembly name for a C symbol */
1108 const char *asm_name( const char *sym )
1110 static char *buffer;
1112 switch (target_platform)
1114 case PLATFORM_MINGW:
1115 case PLATFORM_WINDOWS:
1116 if (target_cpu != CPU_x86) return sym;
1117 if (sym[0] == '@') return sym; /* fastcall */
1118 /* fall through */
1119 case PLATFORM_APPLE:
1120 if (sym[0] == '.' && sym[1] == 'L') return sym;
1121 free( buffer );
1122 buffer = strmake( "_%s", sym );
1123 return buffer;
1124 default:
1125 return sym;
1129 /* return an assembly function declaration for a C function name */
1130 const char *func_declaration( const char *func )
1132 static char *buffer;
1134 switch (target_platform)
1136 case PLATFORM_APPLE:
1137 return "";
1138 case PLATFORM_MINGW:
1139 case PLATFORM_WINDOWS:
1140 free( buffer );
1141 buffer = strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func),
1142 thumb_mode ? "\n\t.thumb_func" : "" );
1143 break;
1144 default:
1145 free( buffer );
1146 switch(target_cpu)
1148 case CPU_ARM:
1149 buffer = strmake( ".type %s,%%function%s", func,
1150 thumb_mode ? "\n\t.thumb_func" : "" );
1151 break;
1152 case CPU_ARM64:
1153 buffer = strmake( ".type %s,%%function", func );
1154 break;
1155 default:
1156 buffer = strmake( ".type %s,@function", func );
1157 break;
1159 break;
1161 return buffer;
1164 /* output a size declaration for an assembly function */
1165 void output_function_size( const char *name )
1167 switch (target_platform)
1169 case PLATFORM_APPLE:
1170 case PLATFORM_MINGW:
1171 case PLATFORM_WINDOWS:
1172 break;
1173 default:
1174 output( "\t.size %s, .-%s\n", name, name );
1175 break;
1179 /* output a .cfi directive */
1180 void output_cfi( const char *format, ... )
1182 va_list valist;
1184 if (!unwind_tables) return;
1185 va_start( valist, format );
1186 fputc( '\t', output_file );
1187 vfprintf( output_file, format, valist );
1188 fputc( '\n', output_file );
1189 va_end( valist );
1192 /* output an RVA pointer */
1193 void output_rva( const char *format, ... )
1195 va_list valist;
1197 va_start( valist, format );
1198 switch (target_platform)
1200 case PLATFORM_MINGW:
1201 case PLATFORM_WINDOWS:
1202 output( "\t.rva " );
1203 vfprintf( output_file, format, valist );
1204 fputc( '\n', output_file );
1205 break;
1206 default:
1207 output( "\t.long " );
1208 vfprintf( output_file, format, valist );
1209 output( " - .L__wine_spec_rva_base\n" );
1210 break;
1212 va_end( valist );
1215 /* output the GNU note for non-exec stack */
1216 void output_gnu_stack_note(void)
1218 switch (target_platform)
1220 case PLATFORM_MINGW:
1221 case PLATFORM_WINDOWS:
1222 case PLATFORM_APPLE:
1223 break;
1224 default:
1225 switch(target_cpu)
1227 case CPU_ARM:
1228 case CPU_ARM64:
1229 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1230 break;
1231 default:
1232 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1233 break;
1235 break;
1239 /* return a global symbol declaration for an assembly symbol */
1240 const char *asm_globl( const char *func )
1242 static char *buffer;
1244 free( buffer );
1245 switch (target_platform)
1247 case PLATFORM_APPLE:
1248 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1249 break;
1250 case PLATFORM_MINGW:
1251 case PLATFORM_WINDOWS:
1252 buffer = strmake( "\t.globl %s%s\n%s%s:", target_cpu == CPU_x86 ? "_" : "", func,
1253 target_cpu == CPU_x86 ? "_" : "", func );
1254 break;
1255 default:
1256 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1257 break;
1259 return buffer;
1262 const char *get_asm_ptr_keyword(void)
1264 switch(get_ptr_size())
1266 case 4: return ".long";
1267 case 8: return ".quad";
1269 assert(0);
1270 return NULL;
1273 const char *get_asm_string_keyword(void)
1275 switch (target_platform)
1277 case PLATFORM_APPLE:
1278 return ".asciz";
1279 default:
1280 return ".string";
1284 const char *get_asm_export_section(void)
1286 switch (target_platform)
1288 case PLATFORM_APPLE: return ".data";
1289 case PLATFORM_MINGW:
1290 case PLATFORM_WINDOWS: return ".section .edata";
1291 default: return ".section .data";
1295 const char *get_asm_rodata_section(void)
1297 switch (target_platform)
1299 case PLATFORM_APPLE: return ".const";
1300 default: return ".section .rodata";
1304 const char *get_asm_rsrc_section(void)
1306 switch (target_platform)
1308 case PLATFORM_APPLE: return ".data";
1309 case PLATFORM_MINGW:
1310 case PLATFORM_WINDOWS: return ".section .rsrc";
1311 default: return ".section .data";
1315 const char *get_asm_string_section(void)
1317 switch (target_platform)
1319 case PLATFORM_APPLE: return ".cstring";
1320 default: return ".section .rodata";
1324 const char *arm64_page( const char *sym )
1326 static char *buffer;
1328 switch (target_platform)
1330 case PLATFORM_APPLE:
1331 free( buffer );
1332 buffer = strmake( "%s@PAGE", sym );
1333 return buffer;
1334 default:
1335 return sym;
1339 const char *arm64_pageoff( const char *sym )
1341 static char *buffer;
1343 free( buffer );
1344 switch (target_platform)
1346 case PLATFORM_APPLE:
1347 buffer = strmake( "%s@PAGEOFF", sym );
1348 break;
1349 default:
1350 buffer = strmake( ":lo12:%s", sym );
1351 break;
1353 return buffer;