12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355 |
- /*
- * Small utility functions for winebuild
- *
- * Copyright 2000 Alexandre Julliard
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
- #include "config.h"
- #include "wine/port.h"
- #include <assert.h>
- #include <ctype.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef HAVE_UNISTD_H
- # include <unistd.h>
- #endif
- #ifdef HAVE_SYS_STAT_H
- # include <sys/stat.h>
- #endif
- #include "build.h"
- #if defined(_WIN32) && !defined(__CYGWIN__)
- # define PATH_SEPARATOR ';'
- #else
- # define PATH_SEPARATOR ':'
- #endif
- static struct strarray tmp_files;
- static struct strarray empty_strarray;
- static const char *output_file_source_name;
- static const struct
- {
- const char *name;
- enum target_cpu cpu;
- } cpu_names[] =
- {
- { "i386", CPU_x86 },
- { "i486", CPU_x86 },
- { "i586", CPU_x86 },
- { "i686", CPU_x86 },
- { "i786", CPU_x86 },
- { "amd64", CPU_x86_64 },
- { "x86_64", CPU_x86_64 },
- { "powerpc", CPU_POWERPC },
- { "arm", CPU_ARM },
- { "armv5", CPU_ARM },
- { "armv6", CPU_ARM },
- { "armv7", CPU_ARM },
- { "armv7a", CPU_ARM },
- { "arm64", CPU_ARM64 },
- { "aarch64", CPU_ARM64 },
- };
- /* atexit handler to clean tmp files */
- void cleanup_tmp_files(void)
- {
- unsigned int i;
- for (i = 0; i < tmp_files.count; i++) if (tmp_files.str[i]) unlink( tmp_files.str[i] );
- }
- void *xmalloc (size_t size)
- {
- void *res;
- res = malloc (size ? size : 1);
- if (res == NULL)
- {
- fprintf (stderr, "Virtual memory exhausted.\n");
- exit (1);
- }
- return res;
- }
- void *xrealloc (void *ptr, size_t size)
- {
- void *res = realloc (ptr, size);
- if (size && res == NULL)
- {
- fprintf (stderr, "Virtual memory exhausted.\n");
- exit (1);
- }
- return res;
- }
- char *xstrdup( const char *str )
- {
- char *res = strdup( str );
- if (!res)
- {
- fprintf (stderr, "Virtual memory exhausted.\n");
- exit (1);
- }
- return res;
- }
- char *strupper(char *s)
- {
- char *p;
- for (p = s; *p; p++) *p = toupper(*p);
- return s;
- }
- int strendswith(const char* str, const char* end)
- {
- int l = strlen(str);
- int m = strlen(end);
- return l >= m && strcmp(str + l - m, end) == 0;
- }
- char *strmake( const char* fmt, ... )
- {
- int n;
- size_t size = 100;
- va_list ap;
- for (;;)
- {
- char *p = xmalloc( size );
- va_start( ap, fmt );
- n = vsnprintf( p, size, fmt, ap );
- va_end( ap );
- if (n == -1) size *= 2;
- else if ((size_t)n >= size) size = n + 1;
- else return p;
- free( p );
- }
- }
- static struct strarray strarray_copy( struct strarray src )
- {
- struct strarray array;
- array.count = src.count;
- array.max = src.max;
- array.str = xmalloc( array.max * sizeof(*array.str) );
- memcpy( array.str, src.str, array.count * sizeof(*array.str) );
- return array;
- }
- static void strarray_add_one( struct strarray *array, const char *str )
- {
- if (array->count == array->max)
- {
- array->max *= 2;
- if (array->max < 16) array->max = 16;
- array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
- }
- array->str[array->count++] = str;
- }
- void strarray_add( struct strarray *array, ... )
- {
- va_list valist;
- const char *str;
- va_start( valist, array );
- while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
- va_end( valist );
- }
- void strarray_addv( struct strarray *array, char * const *argv )
- {
- while (*argv) strarray_add_one( array, *argv++ );
- }
- void strarray_addall( struct strarray *array, struct strarray args )
- {
- unsigned int i;
- for (i = 0; i < args.count; i++) strarray_add_one( array, args.str[i] );
- }
- struct strarray strarray_fromstring( const char *str, const char *delim )
- {
- const char *tok;
- struct strarray array = empty_strarray;
- char *buf = xstrdup( str );
- for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
- strarray_add_one( &array, strdup( tok ));
- free( buf );
- return array;
- }
- void fatal_error( const char *msg, ... )
- {
- va_list valist;
- va_start( valist, msg );
- if (input_file_name)
- {
- fprintf( stderr, "%s:", input_file_name );
- if (current_line)
- fprintf( stderr, "%d:", current_line );
- fputc( ' ', stderr );
- }
- else fprintf( stderr, "winebuild: " );
- vfprintf( stderr, msg, valist );
- va_end( valist );
- exit(1);
- }
- void fatal_perror( const char *msg, ... )
- {
- va_list valist;
- va_start( valist, msg );
- if (input_file_name)
- {
- fprintf( stderr, "%s:", input_file_name );
- if (current_line)
- fprintf( stderr, "%d:", current_line );
- fputc( ' ', stderr );
- }
- vfprintf( stderr, msg, valist );
- perror( " " );
- va_end( valist );
- exit(1);
- }
- void error( const char *msg, ... )
- {
- va_list valist;
- va_start( valist, msg );
- if (input_file_name)
- {
- fprintf( stderr, "%s:", input_file_name );
- if (current_line)
- fprintf( stderr, "%d:", current_line );
- fputc( ' ', stderr );
- }
- vfprintf( stderr, msg, valist );
- va_end( valist );
- nb_errors++;
- }
- void warning( const char *msg, ... )
- {
- va_list valist;
- if (!display_warnings) return;
- va_start( valist, msg );
- if (input_file_name)
- {
- fprintf( stderr, "%s:", input_file_name );
- if (current_line)
- fprintf( stderr, "%d:", current_line );
- fputc( ' ', stderr );
- }
- fprintf( stderr, "warning: " );
- vfprintf( stderr, msg, valist );
- va_end( valist );
- }
- int output( const char *format, ... )
- {
- int ret;
- va_list valist;
- va_start( valist, format );
- ret = vfprintf( output_file, format, valist );
- va_end( valist );
- if (ret < 0) fatal_perror( "Output error" );
- return ret;
- }
- static struct strarray get_tools_path(void)
- {
- static int done;
- static struct strarray dirs;
- if (!done)
- {
- dirs = strarray_copy( tools_path );
- /* then append the PATH directories */
- if (getenv( "PATH" ))
- {
- char *p = xstrdup( getenv( "PATH" ));
- while (*p)
- {
- strarray_add_one( &dirs, p );
- while (*p && *p != PATH_SEPARATOR) p++;
- if (!*p) break;
- *p++ = 0;
- }
- }
- done = 1;
- }
- return dirs;
- }
- /* find a binary in the path */
- static const char *find_binary( const char *prefix, const char *name )
- {
- struct strarray dirs = get_tools_path();
- unsigned int i, maxlen = 0;
- struct stat st;
- char *p, *file;
- if (strchr( name, '/' )) return name;
- if (!prefix) prefix = "";
- for (i = 0; i < dirs.count; i++) maxlen = max( maxlen, strlen(dirs.str[i]) + 2 );
- file = xmalloc( maxlen + strlen(prefix) + strlen(name) + sizeof(EXEEXT) + 1 );
- for (i = 0; i < dirs.count; i++)
- {
- strcpy( file, dirs.str[i] );
- p = file + strlen(file);
- if (p == file) *p++ = '.';
- if (p[-1] != '/') *p++ = '/';
- if (*prefix)
- {
- strcpy( p, prefix );
- p += strlen(p);
- *p++ = '-';
- }
- strcpy( p, name );
- strcat( p, EXEEXT );
- if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
- }
- free( file );
- return NULL;
- }
- void spawn( struct strarray args )
- {
- unsigned int i;
- int status;
- const char *argv0 = find_binary( NULL, args.str[0] );
- if (argv0) args.str[0] = argv0;
- strarray_add_one( &args, NULL );
- if (verbose)
- for (i = 0; args.str[i]; i++)
- fprintf( stderr, "%s%c", args.str[i], args.str[i+1] ? ' ' : '\n' );
- if ((status = _spawnvp( _P_WAIT, args.str[0], args.str )))
- {
- if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
- else fatal_perror( "winebuild" );
- exit( 1 );
- }
- }
- /* find a build tool in the path, trying the various names */
- struct strarray find_tool( const char *name, const char * const *names )
- {
- const char *file;
- const char *alt_names[2];
- if (!names)
- {
- alt_names[0] = name;
- alt_names[1] = NULL;
- names = alt_names;
- }
- while (*names)
- {
- if ((file = find_binary( target_alias, *names ))
- || (names == alt_names && (file = find_binary( "llvm", *names ))))
- {
- struct strarray ret = empty_strarray;
- strarray_add_one( &ret, file );
- return ret;
- }
- names++;
- }
- fatal_error( "cannot find the '%s' tool\n", name );
- }
- /* find a link tool in the path */
- struct strarray find_link_tool(void)
- {
- struct strarray ret = empty_strarray;
- const char *file;
- if (!(file = find_binary( NULL, "lld-link" )))
- fatal_error( "cannot find the 'lld-link tool\n" );
- strarray_add_one( &ret, file );
- return ret;
- }
- struct strarray get_as_command(void)
- {
- struct strarray args;
- unsigned int i;
- if (cc_command.count)
- {
- args = strarray_copy( cc_command );
- strarray_add( &args, "-xassembler", "-c", NULL );
- if (force_pointer_size)
- strarray_add_one( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
- if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
- if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
- if (arch_option) strarray_add_one( &args, strmake("-march=%s", arch_option) );
- for (i = 0; i < tools_path.count; i++)
- strarray_add_one( &args, strmake("-B%s", tools_path.str[i] ));
- return args;
- }
- if (!as_command.count)
- {
- static const char * const commands[] = { "gas", "as", NULL };
- as_command = find_tool( "as", commands );
- }
- args = strarray_copy( as_command );
- if (force_pointer_size)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
- break;
- default:
- switch(target_cpu)
- {
- case CPU_POWERPC:
- strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
- break;
- default:
- strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
- break;
- }
- break;
- }
- }
- if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
- if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
- return args;
- }
- struct strarray get_ld_command(void)
- {
- struct strarray args;
- if (!ld_command.count)
- {
- static const char * const commands[] = { "ld", "gld", NULL };
- ld_command = find_tool( "ld", commands );
- }
- args = strarray_copy( ld_command );
- if (force_pointer_size)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
- break;
- case PLATFORM_FREEBSD:
- strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
- break;
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- strarray_add( &args, "-m", (force_pointer_size == 8) ? "i386pep" : "i386pe", NULL );
- break;
- default:
- switch(target_cpu)
- {
- case CPU_POWERPC:
- strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
- break;
- default:
- strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
- break;
- }
- break;
- }
- }
- if (target_cpu == CPU_ARM && !is_pe())
- strarray_add( &args, "--no-wchar-size-warning", NULL );
- return args;
- }
- const char *get_nm_command(void)
- {
- if (!nm_command.count)
- {
- static const char * const commands[] = { "nm", "gnm", NULL };
- nm_command = find_tool( "nm", commands );
- }
- if (nm_command.count > 1)
- fatal_error( "multiple arguments in nm command not supported yet\n" );
- return nm_command.str[0];
- }
- /* get a name for a temp file, automatically cleaned up on exit */
- char *get_temp_file_name( const char *prefix, const char *suffix )
- {
- char *name;
- const char *ext, *basename;
- int fd;
- if (!prefix || !prefix[0]) prefix = "winebuild";
- if (!suffix) suffix = "";
- if ((basename = strrchr( prefix, '/' ))) basename++;
- else basename = prefix;
- if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
- name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
- memcpy( name, prefix, ext - prefix );
- strcpy( name + (ext - prefix), ".XXXXXX" );
- strcat( name, suffix );
- if ((fd = mkstemps( name, strlen(suffix) )) == -1)
- {
- strcpy( name, "/tmp/" );
- memcpy( name + 5, basename, ext - basename );
- strcpy( name + 5 + (ext - basename), ".XXXXXX" );
- strcat( name, suffix );
- if ((fd = mkstemps( name, strlen(suffix) )) == -1)
- fatal_error( "could not generate a temp file\n" );
- }
- close( fd );
- strarray_add_one( &tmp_files, name );
- return name;
- }
- /*******************************************************************
- * buffer management
- *
- * Function for reading from/writing to a memory buffer.
- */
- int byte_swapped = 0;
- const char *input_buffer_filename;
- const unsigned char *input_buffer;
- size_t input_buffer_pos;
- size_t input_buffer_size;
- unsigned char *output_buffer;
- size_t output_buffer_pos;
- size_t output_buffer_size;
- static void check_output_buffer_space( size_t size )
- {
- if (output_buffer_pos + size >= output_buffer_size)
- {
- output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
- output_buffer = xrealloc( output_buffer, output_buffer_size );
- }
- }
- void init_input_buffer( const char *file )
- {
- int fd;
- struct stat st;
- unsigned char *buffer;
- if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
- if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
- if (!st.st_size) fatal_error( "%s is an empty file\n", file );
- input_buffer = buffer = xmalloc( st.st_size );
- if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
- close( fd );
- input_buffer_filename = xstrdup( file );
- input_buffer_size = st.st_size;
- input_buffer_pos = 0;
- byte_swapped = 0;
- }
- void init_output_buffer(void)
- {
- output_buffer_size = 1024;
- output_buffer_pos = 0;
- output_buffer = xmalloc( output_buffer_size );
- }
- void flush_output_buffer(void)
- {
- open_output_file();
- if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
- fatal_error( "Error writing to %s\n", output_file_name );
- close_output_file();
- free( output_buffer );
- }
- unsigned char get_byte(void)
- {
- if (input_buffer_pos >= input_buffer_size)
- fatal_error( "%s is a truncated file\n", input_buffer_filename );
- return input_buffer[input_buffer_pos++];
- }
- unsigned short get_word(void)
- {
- unsigned short ret;
- if (input_buffer_pos + sizeof(ret) > input_buffer_size)
- fatal_error( "%s is a truncated file\n", input_buffer_filename );
- memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
- if (byte_swapped) ret = (ret << 8) | (ret >> 8);
- input_buffer_pos += sizeof(ret);
- return ret;
- }
- unsigned int get_dword(void)
- {
- unsigned int ret;
- if (input_buffer_pos + sizeof(ret) > input_buffer_size)
- fatal_error( "%s is a truncated file\n", input_buffer_filename );
- memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
- if (byte_swapped)
- ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
- input_buffer_pos += sizeof(ret);
- return ret;
- }
- void put_data( const void *data, size_t size )
- {
- check_output_buffer_space( size );
- memcpy( output_buffer + output_buffer_pos, data, size );
- output_buffer_pos += size;
- }
- void put_byte( unsigned char val )
- {
- check_output_buffer_space( 1 );
- output_buffer[output_buffer_pos++] = val;
- }
- void put_word( unsigned short val )
- {
- if (byte_swapped) val = (val << 8) | (val >> 8);
- put_data( &val, sizeof(val) );
- }
- void put_dword( unsigned int val )
- {
- if (byte_swapped)
- val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
- put_data( &val, sizeof(val) );
- }
- void put_qword( unsigned int val )
- {
- if (byte_swapped)
- {
- put_dword( 0 );
- put_dword( val );
- }
- else
- {
- put_dword( val );
- put_dword( 0 );
- }
- }
- /* pointer-sized word */
- void put_pword( unsigned int val )
- {
- if (get_ptr_size() == 8) put_qword( val );
- else put_dword( val );
- }
- void align_output( unsigned int align )
- {
- size_t size = align - (output_buffer_pos % align);
- if (size == align) return;
- check_output_buffer_space( size );
- memset( output_buffer + output_buffer_pos, 0, size );
- output_buffer_pos += size;
- }
- /* output a standard header for generated files */
- void output_standard_file_header(void)
- {
- if (spec_file_name)
- output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
- else
- output( "/* File generated automatically; do not edit! */\n" );
- output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
- if (safe_seh)
- {
- output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
- output( "\t.globl @feat.00\n" );
- output( ".set @feat.00, 1\n" );
- }
- if (thumb_mode)
- {
- output( "\t.syntax unified\n" );
- output( "\t.thumb\n" );
- }
- }
- /* dump a byte stream into the assembly code */
- void dump_bytes( const void *buffer, unsigned int size )
- {
- unsigned int i;
- const unsigned char *ptr = buffer;
- if (!size) return;
- output( "\t.byte " );
- for (i = 0; i < size - 1; i++, ptr++)
- {
- if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
- else output( "0x%02x,", *ptr );
- }
- output( "0x%02x\n", *ptr );
- }
- /*******************************************************************
- * open_input_file
- *
- * Open a file in the given srcdir and set the input_file_name global variable.
- */
- FILE *open_input_file( const char *srcdir, const char *name )
- {
- char *fullname;
- FILE *file = fopen( name, "r" );
- if (!file && srcdir)
- {
- fullname = strmake( "%s/%s", srcdir, name );
- file = fopen( fullname, "r" );
- }
- else fullname = xstrdup( name );
- if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
- input_file_name = fullname;
- current_line = 1;
- return file;
- }
- /*******************************************************************
- * close_input_file
- *
- * Close the current input file (must have been opened with open_input_file).
- */
- void close_input_file( FILE *file )
- {
- fclose( file );
- free( input_file_name );
- input_file_name = NULL;
- current_line = 0;
- }
- /*******************************************************************
- * open_output_file
- */
- void open_output_file(void)
- {
- if (output_file_name)
- {
- if (strendswith( output_file_name, ".o" ))
- output_file_source_name = open_temp_output_file( ".s" );
- else
- if (!(output_file = fopen( output_file_name, "w" )))
- fatal_error( "Unable to create output file '%s'\n", output_file_name );
- }
- else output_file = stdout;
- }
- /*******************************************************************
- * close_output_file
- */
- void close_output_file(void)
- {
- if (!output_file || !output_file_name) return;
- if (fclose( output_file ) < 0) fatal_perror( "fclose" );
- if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
- output_file = NULL;
- }
- /*******************************************************************
- * open_temp_output_file
- */
- char *open_temp_output_file( const char *suffix )
- {
- char *tmp_file = get_temp_file_name( output_file_name, suffix );
- if (!(output_file = fopen( tmp_file, "w" )))
- fatal_error( "Unable to create output file '%s'\n", tmp_file );
- return tmp_file;
- }
- /*******************************************************************
- * remove_stdcall_decoration
- *
- * Remove a possible @xx suffix from a function name.
- * Return the numerical value of the suffix, or -1 if none.
- */
- int remove_stdcall_decoration( char *name )
- {
- char *p, *end = strrchr( name, '@' );
- if (!end || !end[1] || end == name) return -1;
- if (target_cpu != CPU_x86) return -1;
- /* make sure all the rest is digits */
- for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
- *end = 0;
- return atoi( end + 1 );
- }
- /*******************************************************************
- * assemble_file
- *
- * Run a file through the assembler.
- */
- void assemble_file( const char *src_file, const char *obj_file )
- {
- struct strarray args = get_as_command();
- strarray_add( &args, "-o", obj_file, src_file, NULL );
- spawn( args );
- }
- /*******************************************************************
- * alloc_dll_spec
- *
- * Create a new dll spec file descriptor
- */
- DLLSPEC *alloc_dll_spec(void)
- {
- DLLSPEC *spec;
- spec = xmalloc( sizeof(*spec) );
- memset( spec, 0, sizeof(*spec) );
- spec->type = SPEC_WIN32;
- spec->base = MAX_ORDINALS;
- spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
- spec->subsystem = 0;
- spec->subsystem_major = 4;
- spec->subsystem_minor = 0;
- if (get_ptr_size() > 4)
- spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
- else
- spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
- spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
- return spec;
- }
- /*******************************************************************
- * free_dll_spec
- *
- * Free dll spec file descriptor
- */
- void free_dll_spec( DLLSPEC *spec )
- {
- int i;
- for (i = 0; i < spec->nb_entry_points; i++)
- {
- ORDDEF *odp = &spec->entry_points[i];
- free( odp->name );
- free( odp->export_name );
- free( odp->link_name );
- }
- free( spec->file_name );
- free( spec->dll_name );
- free( spec->c_name );
- free( spec->init_func );
- free( spec->entry_points );
- free( spec->names );
- free( spec->ordinals );
- free( spec->resources );
- free( spec );
- }
- /*******************************************************************
- * make_c_identifier
- *
- * Map a string to a valid C identifier.
- */
- char *make_c_identifier( const char *str )
- {
- char *p, buffer[256];
- for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
- {
- if (isalnum(*str)) *p = *str;
- else *p = '_';
- }
- *p = 0;
- return xstrdup( buffer );
- }
- /*******************************************************************
- * get_stub_name
- *
- * Generate an internal name for a stub entry point.
- */
- const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
- {
- static char *buffer;
- free( buffer );
- if (odp->name || odp->export_name)
- {
- char *p;
- buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
- /* make sure name is a legal C identifier */
- for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
- if (!*p) return buffer;
- free( buffer );
- }
- buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
- return buffer;
- }
- /* return the stdcall-decorated name for an entry point */
- const char *get_link_name( const ORDDEF *odp )
- {
- static char *buffer;
- char *ret;
- if (target_cpu != CPU_x86) return odp->link_name;
- switch (odp->type)
- {
- case TYPE_STDCALL:
- if (is_pe())
- {
- if (odp->flags & FLAG_THISCALL) return odp->link_name;
- if (odp->flags & FLAG_FASTCALL) ret = strmake( "@%s@%u", odp->link_name, get_args_size( odp ));
- else if (!kill_at) ret = strmake( "%s@%u", odp->link_name, get_args_size( odp ));
- else return odp->link_name;
- }
- else
- {
- if (odp->flags & FLAG_THISCALL) ret = strmake( "__thiscall_%s", odp->link_name );
- else if (odp->flags & FLAG_FASTCALL) ret = strmake( "__fastcall_%s", odp->link_name );
- else return odp->link_name;
- }
- break;
- case TYPE_PASCAL:
- if (is_pe() && !kill_at)
- {
- int args = get_args_size( odp );
- if (odp->flags & FLAG_REGISTER) args += get_ptr_size(); /* context argument */
- ret = strmake( "%s@%u", odp->link_name, args );
- }
- else return odp->link_name;
- break;
- default:
- return odp->link_name;
- }
- free( buffer );
- buffer = ret;
- return ret;
- }
- /*******************************************************************
- * sort_func_list
- *
- * Sort a list of functions, removing duplicates.
- */
- int sort_func_list( ORDDEF **list, int count, int (*compare)(const void *, const void *) )
- {
- int i, j;
- if (!count) return 0;
- qsort( list, count, sizeof(*list), compare );
- for (i = j = 0; i < count; i++) if (compare( &list[j], &list[i] )) list[++j] = list[i];
- return j + 1;
- }
- /* parse a cpu name and return the corresponding value */
- int get_cpu_from_name( const char *name )
- {
- unsigned int i;
- for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
- if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
- return -1;
- }
- /*****************************************************************
- * Function: get_alignment
- *
- * Description:
- * According to the info page for gas, the .align directive behaves
- * differently on different systems. On some architectures, the
- * argument of a .align directive is the number of bytes to pad to, so
- * to align on an 8-byte boundary you'd say
- * .align 8
- * On other systems, the argument is "the number of low-order zero bits
- * that the location counter must have after advancement." So to
- * align on an 8-byte boundary you'd say
- * .align 3
- *
- * The reason gas is written this way is that it's trying to mimic
- * native assemblers for the various architectures it runs on. gas
- * provides other directives that work consistently across
- * architectures, but of course we want to work on all arches with or
- * without gas. Hence this function.
- *
- *
- * Parameters:
- * align -- the number of bytes to align to. Must be a power of 2.
- */
- unsigned int get_alignment(unsigned int align)
- {
- unsigned int n;
- assert( !(align & (align - 1)) );
- switch(target_cpu)
- {
- case CPU_x86:
- case CPU_x86_64:
- if (target_platform != PLATFORM_APPLE) return align;
- /* fall through */
- case CPU_POWERPC:
- case CPU_ARM:
- case CPU_ARM64:
- n = 0;
- while ((1u << n) != align) n++;
- return n;
- }
- /* unreached */
- assert(0);
- return 0;
- }
- /* return the page size for the target CPU */
- unsigned int get_page_size(void)
- {
- return 0x1000; /* same on all platforms */
- }
- /* return the size of a pointer on the target CPU */
- unsigned int get_ptr_size(void)
- {
- switch(target_cpu)
- {
- case CPU_x86:
- case CPU_POWERPC:
- case CPU_ARM:
- return 4;
- case CPU_x86_64:
- case CPU_ARM64:
- return 8;
- }
- /* unreached */
- assert(0);
- return 0;
- }
- /* return the total size in bytes of the arguments on the stack */
- unsigned int get_args_size( const ORDDEF *odp )
- {
- int i, size;
- for (i = size = 0; i < odp->u.func.nb_args; i++)
- {
- switch (odp->u.func.args[i])
- {
- case ARG_INT64:
- case ARG_DOUBLE:
- size += 8;
- break;
- case ARG_INT128:
- /* int128 is passed as pointer on x86_64 */
- if (target_cpu != CPU_x86_64)
- {
- size += 16;
- break;
- }
- /* fall through */
- default:
- size += get_ptr_size();
- break;
- }
- }
- return size;
- }
- /* return the assembly name for a C symbol */
- const char *asm_name( const char *sym )
- {
- static char *buffer;
- switch (target_platform)
- {
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- if (target_cpu != CPU_x86) return sym;
- if (sym[0] == '@') return sym; /* fastcall */
- /* fall through */
- case PLATFORM_APPLE:
- if (sym[0] == '.' && sym[1] == 'L') return sym;
- free( buffer );
- buffer = strmake( "_%s", sym );
- return buffer;
- default:
- return sym;
- }
- }
- /* return an assembly function declaration for a C function name */
- const char *func_declaration( const char *func )
- {
- static char *buffer;
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- return "";
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- free( buffer );
- buffer = strmake( ".def %s\n\t.scl 2\n\t.type 32\n\t.endef%s", asm_name(func),
- thumb_mode ? "\n\t.thumb_func" : "" );
- break;
- default:
- free( buffer );
- switch(target_cpu)
- {
- case CPU_ARM:
- buffer = strmake( ".type %s,%%function%s", func,
- thumb_mode ? "\n\t.thumb_func" : "" );
- break;
- case CPU_ARM64:
- buffer = strmake( ".type %s,%%function", func );
- break;
- default:
- buffer = strmake( ".type %s,@function", func );
- break;
- }
- break;
- }
- return buffer;
- }
- /* output a size declaration for an assembly function */
- void output_function_size( const char *name )
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- break;
- default:
- output( "\t.size %s, .-%s\n", name, name );
- break;
- }
- }
- /* output a .cfi directive */
- void output_cfi( const char *format, ... )
- {
- va_list valist;
- if (!unwind_tables) return;
- va_start( valist, format );
- fputc( '\t', output_file );
- vfprintf( output_file, format, valist );
- fputc( '\n', output_file );
- va_end( valist );
- }
- /* output an RVA pointer */
- void output_rva( const char *format, ... )
- {
- va_list valist;
- va_start( valist, format );
- switch (target_platform)
- {
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- output( "\t.rva " );
- vfprintf( output_file, format, valist );
- fputc( '\n', output_file );
- break;
- default:
- output( "\t.long " );
- vfprintf( output_file, format, valist );
- output( " - .L__wine_spec_rva_base\n" );
- break;
- }
- va_end( valist );
- }
- /* output the GNU note for non-exec stack */
- void output_gnu_stack_note(void)
- {
- switch (target_platform)
- {
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- case PLATFORM_APPLE:
- break;
- default:
- switch(target_cpu)
- {
- case CPU_ARM:
- case CPU_ARM64:
- output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
- break;
- default:
- output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
- break;
- }
- break;
- }
- }
- /* return a global symbol declaration for an assembly symbol */
- const char *asm_globl( const char *func )
- {
- static char *buffer;
- free( buffer );
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
- break;
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS:
- buffer = strmake( "\t.globl %s%s\n%s%s:", target_cpu == CPU_x86 ? "_" : "", func,
- target_cpu == CPU_x86 ? "_" : "", func );
- break;
- default:
- buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
- break;
- }
- return buffer;
- }
- const char *get_asm_ptr_keyword(void)
- {
- switch(get_ptr_size())
- {
- case 4: return ".long";
- case 8: return ".quad";
- }
- assert(0);
- return NULL;
- }
- const char *get_asm_string_keyword(void)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- return ".asciz";
- default:
- return ".string";
- }
- }
- const char *get_asm_export_section(void)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE: return ".data";
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS: return ".section .edata";
- default: return ".section .data";
- }
- }
- const char *get_asm_rodata_section(void)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE: return ".const";
- default: return ".section .rodata";
- }
- }
- const char *get_asm_rsrc_section(void)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE: return ".data";
- case PLATFORM_MINGW:
- case PLATFORM_WINDOWS: return ".section .rsrc";
- default: return ".section .data";
- }
- }
- const char *get_asm_string_section(void)
- {
- switch (target_platform)
- {
- case PLATFORM_APPLE: return ".cstring";
- default: return ".section .rodata";
- }
- }
- const char *arm64_page( const char *sym )
- {
- static char *buffer;
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- free( buffer );
- buffer = strmake( "%s@PAGE", sym );
- return buffer;
- default:
- return sym;
- }
- }
- const char *arm64_pageoff( const char *sym )
- {
- static char *buffer;
- free( buffer );
- switch (target_platform)
- {
- case PLATFORM_APPLE:
- buffer = strmake( "%s@PAGEOFF", sym );
- break;
- default:
- buffer = strmake( ":lo12:%s", sym );
- break;
- }
- return buffer;
- }
|