2002-03-10 00:29:33 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
*/
|
2000-06-23 22:15:35 +02:00
|
|
|
|
2001-11-06 21:57:11 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-06-27 13:23:24 +02:00
|
|
|
#include <assert.h>
|
2000-06-23 22:15:35 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "build.h"
|
|
|
|
|
2016-03-19 05:22:30 +01:00
|
|
|
static struct strarray tmp_files;
|
2019-03-04 14:31:48 +01:00
|
|
|
static const char *output_file_source_name;
|
2016-03-19 05:10:09 +01:00
|
|
|
|
2005-09-19 17:24:13 +02:00
|
|
|
/* atexit handler to clean tmp files */
|
2013-03-04 17:21:16 +01:00
|
|
|
void cleanup_tmp_files(void)
|
2005-09-19 17:24:13 +02:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2016-03-19 05:22:30 +01:00
|
|
|
for (i = 0; i < tmp_files.count; i++) if (tmp_files.str[i]) unlink( tmp_files.str[i] );
|
2005-09-19 17:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-23 22:15:35 +02:00
|
|
|
char *strupper(char *s)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
for (p = s; *p; p++) *p = toupper(*p);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fatal_error( const char *msg, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
va_start( valist, msg );
|
2000-09-28 01:40:43 +02:00
|
|
|
if (input_file_name)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s:", input_file_name );
|
|
|
|
if (current_line)
|
|
|
|
fprintf( stderr, "%d:", current_line );
|
|
|
|
fputc( ' ', stderr );
|
|
|
|
}
|
2004-03-25 01:40:52 +01:00
|
|
|
else fprintf( stderr, "winebuild: " );
|
2000-06-23 22:15:35 +02:00
|
|
|
vfprintf( stderr, msg, valist );
|
|
|
|
va_end( valist );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-10-23 23:33:06 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-03-18 06:30:54 +01:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2000-09-28 01:40:43 +02:00
|
|
|
void warning( const char *msg, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
2002-03-21 02:38:53 +01:00
|
|
|
|
|
|
|
if (!display_warnings) return;
|
2000-09-28 01:40:43 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2006-08-11 20:41:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-10 17:53:19 +02:00
|
|
|
static struct strarray get_tools_path(void)
|
|
|
|
{
|
|
|
|
static int done;
|
|
|
|
static struct strarray dirs;
|
|
|
|
|
|
|
|
if (!done)
|
|
|
|
{
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_addall( &dirs, tools_path );
|
|
|
|
strarray_addall( &dirs, strarray_frompath( getenv( "PATH" )));
|
2019-09-10 17:53:19 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-19 05:10:09 +01:00
|
|
|
void spawn( struct strarray args )
|
2010-08-02 12:07:53 +02:00
|
|
|
{
|
|
|
|
int status;
|
2019-09-10 17:53:19 +02:00
|
|
|
const char *argv0 = find_binary( NULL, args.str[0] );
|
2010-08-02 12:07:53 +02:00
|
|
|
|
2019-09-10 17:53:19 +02:00
|
|
|
if (argv0) args.str[0] = argv0;
|
2021-09-28 18:18:49 +02:00
|
|
|
if (verbose) strarray_trace( args );
|
2010-08-02 12:07:53 +02:00
|
|
|
|
2021-09-28 18:18:49 +02:00
|
|
|
if ((status = strarray_spawn( args )))
|
2010-08-02 12:07:53 +02:00
|
|
|
{
|
2016-03-19 05:10:09 +01:00
|
|
|
if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
|
2010-08-02 12:07:53 +02:00
|
|
|
else fatal_perror( "winebuild" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:35:48 +02:00
|
|
|
static const char *find_clang_tool( struct strarray clang, const char *tool )
|
2021-05-07 17:05:46 +02:00
|
|
|
{
|
|
|
|
const char *out = get_temp_file_name( "print_tool", ".out" );
|
2021-09-23 11:35:48 +02:00
|
|
|
struct strarray args = empty_strarray;
|
2021-05-07 17:05:46 +02:00
|
|
|
int sout = -1;
|
|
|
|
char *path, *p;
|
|
|
|
struct stat st;
|
|
|
|
size_t cnt;
|
|
|
|
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_addall( &args, clang );
|
|
|
|
strarray_add( &args, strmake( "-print-prog-name=%s", tool ));
|
|
|
|
if (verbose) strarray_add( &args, "-v" );
|
2021-05-07 17:05:46 +02:00
|
|
|
|
|
|
|
sout = dup( fileno(stdout) );
|
|
|
|
freopen( out, "w", stdout );
|
|
|
|
spawn( args );
|
|
|
|
if (sout >= 0)
|
|
|
|
{
|
|
|
|
dup2( sout, fileno(stdout) );
|
|
|
|
close( sout );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(out, &st) || !st.st_size) return NULL;
|
|
|
|
|
|
|
|
path = xmalloc(st.st_size + 1);
|
|
|
|
sout = open(out, O_RDONLY);
|
|
|
|
if (sout == -1) return NULL;
|
|
|
|
cnt = read(sout, path, st.st_size);
|
|
|
|
close(sout);
|
|
|
|
path[cnt] = 0;
|
|
|
|
if ((p = strchr(path, '\n'))) *p = 0;
|
|
|
|
/* clang returns passed command instead of full path if the tool could not be found */
|
|
|
|
if (!strcmp(path, tool))
|
|
|
|
{
|
|
|
|
free( path );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:37:13 +01:00
|
|
|
/* find a build tool in the path, trying the various names */
|
2016-03-19 05:10:09 +01:00
|
|
|
struct strarray find_tool( const char *name, const char * const *names )
|
2009-02-04 19:37:13 +01:00
|
|
|
{
|
2021-05-07 17:05:46 +02:00
|
|
|
struct strarray ret = empty_strarray;
|
2019-09-10 17:53:19 +02:00
|
|
|
const char *file;
|
2009-12-30 12:42:25 +01:00
|
|
|
const char *alt_names[2];
|
2009-02-04 19:37:13 +01:00
|
|
|
|
2009-12-30 12:42:25 +01:00
|
|
|
if (!names)
|
|
|
|
{
|
|
|
|
alt_names[0] = name;
|
|
|
|
alt_names[1] = NULL;
|
|
|
|
names = alt_names;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:37:13 +01:00
|
|
|
while (*names)
|
|
|
|
{
|
2021-05-07 17:05:46 +02:00
|
|
|
if ((file = find_binary( target_alias, *names ))) break;
|
|
|
|
names++;
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:48:26 +01:00
|
|
|
if (!file && strcmp( name, "as" )) /* llvm-as is not a gas replacement */
|
2021-05-07 17:05:46 +02:00
|
|
|
{
|
2021-11-10 17:48:26 +01:00
|
|
|
if (cc_command.count) file = find_clang_tool( cc_command, name );
|
2021-05-07 17:05:46 +02:00
|
|
|
if (!file && !(file = find_binary( "llvm", name )))
|
2009-02-04 19:37:13 +01:00
|
|
|
{
|
2021-05-07 17:05:46 +02:00
|
|
|
struct strarray clang = empty_strarray;
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &clang, "clang" );
|
2021-05-07 17:05:46 +02:00
|
|
|
file = find_clang_tool( clang, strmake( "llvm-%s", name ));
|
2009-02-04 19:37:13 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 17:05:46 +02:00
|
|
|
if (!file) fatal_error( "cannot find the '%s' tool\n", name );
|
|
|
|
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &ret, file );
|
2021-05-07 17:05:46 +02:00
|
|
|
return ret;
|
2009-02-04 19:37:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:29:27 +01:00
|
|
|
/* find a link tool in the path */
|
|
|
|
struct strarray find_link_tool(void)
|
|
|
|
{
|
|
|
|
struct strarray ret = empty_strarray;
|
2021-05-07 17:05:46 +02:00
|
|
|
const char *file = NULL;
|
|
|
|
|
|
|
|
if (cc_command.count) file = find_clang_tool( cc_command, "lld-link" );
|
|
|
|
if (!file) file = find_binary( NULL, "lld-link" );
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
struct strarray clang = empty_strarray;
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &clang, "clang" );
|
2021-05-07 17:05:46 +02:00
|
|
|
file = find_clang_tool( clang, "lld-link" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file) fatal_error( "cannot find the 'lld-link' tool\n" );
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &ret, file );
|
2021-02-10 17:29:27 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-19 05:10:09 +01:00
|
|
|
struct strarray get_as_command(void)
|
2009-02-04 19:31:54 +01:00
|
|
|
{
|
2021-09-23 11:35:48 +02:00
|
|
|
struct strarray args = empty_strarray;
|
2019-09-10 17:53:19 +02:00
|
|
|
unsigned int i;
|
2010-08-02 12:07:53 +02:00
|
|
|
|
2016-03-19 05:10:09 +01:00
|
|
|
if (cc_command.count)
|
2013-01-28 21:42:12 +01:00
|
|
|
{
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_addall( &args, cc_command );
|
|
|
|
strarray_add( &args, "-xassembler" );
|
|
|
|
strarray_add( &args, "-c" );
|
2013-06-10 13:19:29 +02:00
|
|
|
if (force_pointer_size)
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
|
|
|
|
if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
|
|
|
|
if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
|
|
|
|
if (arch_option) strarray_add( &args, strmake("-march=%s", arch_option) );
|
2019-09-10 17:53:19 +02:00
|
|
|
for (i = 0; i < tools_path.count; i++)
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, strmake("-B%s", tools_path.str[i] ));
|
2013-06-10 13:19:29 +02:00
|
|
|
return args;
|
2013-01-28 21:42:12 +01:00
|
|
|
}
|
|
|
|
|
2016-03-19 05:10:09 +01:00
|
|
|
if (!as_command.count)
|
2009-02-04 19:31:54 +01:00
|
|
|
{
|
2009-12-30 12:42:25 +01:00
|
|
|
static const char * const commands[] = { "gas", "as", NULL };
|
|
|
|
as_command = find_tool( "as", commands );
|
2010-08-02 12:07:53 +02:00
|
|
|
}
|
2013-01-28 21:42:12 +01:00
|
|
|
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_addall( &args, as_command );
|
2009-02-04 19:33:49 +01:00
|
|
|
|
2013-06-10 13:19:29 +02:00
|
|
|
if (force_pointer_size)
|
2010-08-02 12:07:53 +02:00
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2009-02-04 19:33:49 +01:00
|
|
|
{
|
2010-08-02 12:07:53 +02:00
|
|
|
case PLATFORM_APPLE:
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, "-arch" );
|
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
|
2010-08-02 12:07:53 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-11-17 21:32:45 +01:00
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "--64" : "--32" );
|
2010-08-02 12:07:53 +02:00
|
|
|
break;
|
2009-02-04 19:33:49 +01:00
|
|
|
}
|
2009-02-04 19:31:54 +01:00
|
|
|
}
|
2011-04-27 19:45:11 +02:00
|
|
|
|
2021-09-23 11:35:48 +02:00
|
|
|
if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
|
|
|
|
if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
|
2010-08-02 12:07:53 +02:00
|
|
|
return args;
|
2009-02-04 19:31:54 +01:00
|
|
|
}
|
|
|
|
|
2016-03-19 05:10:09 +01:00
|
|
|
struct strarray get_ld_command(void)
|
2009-02-04 19:31:54 +01:00
|
|
|
{
|
2021-09-23 11:35:48 +02:00
|
|
|
struct strarray args = empty_strarray;
|
2010-08-02 12:07:53 +02:00
|
|
|
|
2016-03-19 05:10:09 +01:00
|
|
|
if (!ld_command.count)
|
2009-02-04 19:31:54 +01:00
|
|
|
{
|
2009-12-30 12:42:25 +01:00
|
|
|
static const char * const commands[] = { "ld", "gld", NULL };
|
|
|
|
ld_command = find_tool( "ld", commands );
|
2010-08-02 12:07:53 +02:00
|
|
|
}
|
2013-01-28 21:42:12 +01:00
|
|
|
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_addall( &args, ld_command );
|
2009-02-04 19:33:49 +01:00
|
|
|
|
2010-08-02 12:07:53 +02:00
|
|
|
if (force_pointer_size)
|
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2009-02-04 19:33:49 +01:00
|
|
|
{
|
2010-08-02 12:07:53 +02:00
|
|
|
case PLATFORM_APPLE:
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, "-arch" );
|
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
|
2010-08-02 12:07:53 +02:00
|
|
|
break;
|
|
|
|
case PLATFORM_FREEBSD:
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, "-m" );
|
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd" );
|
2010-08-02 12:07:53 +02:00
|
|
|
break;
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2019-03-06 10:40:23 +01:00
|
|
|
case PLATFORM_WINDOWS:
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, "-m" );
|
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "i386pep" : "i386pe" );
|
2019-03-06 10:40:23 +01:00
|
|
|
break;
|
2010-08-02 12:07:53 +02:00
|
|
|
default:
|
2021-11-17 21:32:45 +01:00
|
|
|
strarray_add( &args, "-m" );
|
|
|
|
strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386" );
|
2010-08-02 12:07:53 +02:00
|
|
|
break;
|
2009-02-04 19:33:49 +01:00
|
|
|
}
|
2009-02-04 19:31:54 +01:00
|
|
|
}
|
2019-05-16 22:55:39 +02:00
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.cpu == CPU_ARM && !is_pe())
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, "--no-wchar-size-warning" );
|
2019-05-16 22:55:39 +02:00
|
|
|
|
2010-08-02 12:07:53 +02:00
|
|
|
return args;
|
2009-02-04 19:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *get_nm_command(void)
|
|
|
|
{
|
2016-03-19 05:10:09 +01:00
|
|
|
if (!nm_command.count)
|
2009-02-04 19:31:54 +01:00
|
|
|
{
|
2009-12-30 12:42:25 +01:00
|
|
|
static const char * const commands[] = { "nm", "gnm", NULL };
|
|
|
|
nm_command = find_tool( "nm", commands );
|
2009-02-04 19:31:54 +01:00
|
|
|
}
|
2016-03-19 05:10:09 +01:00
|
|
|
if (nm_command.count > 1)
|
2013-10-01 17:02:00 +02:00
|
|
|
fatal_error( "multiple arguments in nm command not supported yet\n" );
|
2016-03-19 05:10:09 +01:00
|
|
|
return nm_command.str[0];
|
2009-02-04 19:31:54 +01:00
|
|
|
}
|
|
|
|
|
2005-09-19 17:24:13 +02:00
|
|
|
/* 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;
|
|
|
|
int fd;
|
|
|
|
|
2021-09-29 11:13:36 +02:00
|
|
|
if (prefix) prefix = get_basename_noext( prefix );
|
2021-09-29 09:32:01 +02:00
|
|
|
fd = make_temp_file( prefix, suffix, &name );
|
2005-09-19 17:24:13 +02:00
|
|
|
close( fd );
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &tmp_files, name );
|
2005-09-19 17:24:13 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2009-08-14 13:22:26 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* 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;
|
2009-08-14 13:26:09 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2009-08-14 13:22:26 +02:00
|
|
|
|
|
|
|
void init_input_buffer( const char *file )
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
2019-04-03 15:48:57 +02:00
|
|
|
unsigned char *buffer;
|
2009-08-14 13:22:26 +02:00
|
|
|
|
|
|
|
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 );
|
2019-04-03 15:48:57 +02:00
|
|
|
input_buffer = buffer = xmalloc( st.st_size );
|
|
|
|
if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
|
2009-08-14 13:22:26 +02:00
|
|
|
close( fd );
|
|
|
|
input_buffer_filename = xstrdup( file );
|
|
|
|
input_buffer_size = st.st_size;
|
|
|
|
input_buffer_pos = 0;
|
|
|
|
byte_swapped = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-14 13:26:09 +02:00
|
|
|
void init_output_buffer(void)
|
|
|
|
{
|
|
|
|
output_buffer_size = 1024;
|
|
|
|
output_buffer_pos = 0;
|
|
|
|
output_buffer = xmalloc( output_buffer_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush_output_buffer(void)
|
|
|
|
{
|
2019-03-04 14:31:48 +01:00
|
|
|
open_output_file();
|
2009-08-14 13:26:09 +02:00
|
|
|
if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
|
|
|
|
fatal_error( "Error writing to %s\n", output_file_name );
|
2019-03-04 14:31:48 +01:00
|
|
|
close_output_file();
|
2009-08-14 13:26:09 +02:00
|
|
|
free( output_buffer );
|
|
|
|
}
|
|
|
|
|
2009-08-14 13:22:26 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-14 13:26:09 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
/* output a standard header for generated files */
|
2006-08-11 20:41:29 +02:00
|
|
|
void output_standard_file_header(void)
|
2002-03-10 00:29:33 +01:00
|
|
|
{
|
2005-03-15 16:42:32 +01:00
|
|
|
if (spec_file_name)
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
|
2002-05-12 01:06:32 +02:00
|
|
|
else
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "/* File generated automatically; do not edit! */\n" );
|
|
|
|
output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
|
2020-05-06 16:18:31 +02:00
|
|
|
if (safe_seh)
|
|
|
|
{
|
2020-12-18 23:34:34 +01:00
|
|
|
output( "\t.def @feat.00\n\t.scl 3\n\t.type 0\n\t.endef\n" );
|
2020-05-06 16:18:31 +02:00
|
|
|
output( "\t.globl @feat.00\n" );
|
|
|
|
output( ".set @feat.00, 1\n" );
|
|
|
|
}
|
2021-01-16 11:05:38 +01:00
|
|
|
if (thumb_mode)
|
|
|
|
{
|
|
|
|
output( "\t.syntax unified\n" );
|
|
|
|
output( "\t.thumb\n" );
|
|
|
|
}
|
2002-03-10 00:29:33 +01:00
|
|
|
}
|
|
|
|
|
2000-06-23 22:15:35 +02:00
|
|
|
/* dump a byte stream into the assembly code */
|
2006-08-11 20:41:29 +02:00
|
|
|
void dump_bytes( const void *buffer, unsigned int size )
|
2000-06-23 22:15:35 +02:00
|
|
|
{
|
2005-09-21 13:50:04 +02:00
|
|
|
unsigned int i;
|
|
|
|
const unsigned char *ptr = buffer;
|
2000-06-23 22:15:35 +02:00
|
|
|
|
2005-09-21 13:50:04 +02:00
|
|
|
if (!size) return;
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t.byte " );
|
2005-09-21 13:50:04 +02:00
|
|
|
for (i = 0; i < size - 1; i++, ptr++)
|
2000-06-23 22:15:35 +02:00
|
|
|
{
|
2006-08-11 20:41:29 +02:00
|
|
|
if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
|
|
|
|
else output( "0x%02x,", *ptr );
|
2000-06-23 22:15:35 +02:00
|
|
|
}
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "0x%02x\n", *ptr );
|
2000-06-23 22:15:35 +02:00
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2002-05-14 22:54:58 +02:00
|
|
|
|
2002-08-01 20:34:12 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* 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;
|
2003-11-20 05:21:56 +01:00
|
|
|
FILE *file = fopen( name, "r" );
|
2002-08-01 20:34:12 +02:00
|
|
|
|
2003-11-20 05:21:56 +01:00
|
|
|
if (!file && srcdir)
|
2002-08-01 20:34:12 +02:00
|
|
|
{
|
2010-08-02 10:11:23 +02:00
|
|
|
fullname = strmake( "%s/%s", srcdir, name );
|
2003-11-20 05:21:56 +01:00
|
|
|
file = fopen( fullname, "r" );
|
2002-08-01 20:34:12 +02:00
|
|
|
}
|
|
|
|
else fullname = xstrdup( name );
|
|
|
|
|
2003-11-20 05:21:56 +01:00
|
|
|
if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
|
2002-08-01 20:34:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-04 14:31:48 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* open_output_file
|
|
|
|
*/
|
|
|
|
void open_output_file(void)
|
|
|
|
{
|
|
|
|
if (output_file_name)
|
|
|
|
{
|
|
|
|
if (strendswith( output_file_name, ".o" ))
|
2019-03-04 14:46:02 +01:00
|
|
|
output_file_source_name = open_temp_output_file( ".s" );
|
2019-03-04 14:31:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-04 14:46:02 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-17 21:36:16 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* 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;
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.cpu != CPU_i386) return -1;
|
2004-02-17 21:36:16 +01:00
|
|
|
/* make sure all the rest is digits */
|
|
|
|
for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
|
|
|
|
*end = 0;
|
|
|
|
return atoi( end + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-21 12:22:28 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* assemble_file
|
|
|
|
*
|
|
|
|
* Run a file through the assembler.
|
|
|
|
*/
|
|
|
|
void assemble_file( const char *src_file, const char *obj_file )
|
|
|
|
{
|
2016-03-19 05:10:09 +01:00
|
|
|
struct strarray args = get_as_command();
|
2021-09-23 11:35:48 +02:00
|
|
|
strarray_add( &args, "-o" );
|
|
|
|
strarray_add( &args, obj_file );
|
|
|
|
strarray_add( &args, src_file );
|
2010-08-02 12:07:53 +02:00
|
|
|
spawn( args );
|
2005-09-21 12:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-17 21:36:16 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* alloc_dll_spec
|
|
|
|
*
|
|
|
|
* Create a new dll spec file descriptor
|
|
|
|
*/
|
|
|
|
DLLSPEC *alloc_dll_spec(void)
|
|
|
|
{
|
|
|
|
DLLSPEC *spec;
|
|
|
|
|
|
|
|
spec = xmalloc( sizeof(*spec) );
|
2016-03-22 07:06:27 +01:00
|
|
|
memset( spec, 0, sizeof(*spec) );
|
2004-02-17 21:36:16 +01:00
|
|
|
spec->type = SPEC_WIN32;
|
|
|
|
spec->base = MAX_ORDINALS;
|
2007-05-24 12:54:27 +02:00
|
|
|
spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
|
2016-03-22 07:06:27 +01:00
|
|
|
spec->subsystem = 0;
|
|
|
|
spec->subsystem_major = 4;
|
|
|
|
spec->subsystem_minor = 0;
|
2021-08-30 11:46:51 +02:00
|
|
|
spec->syscall_table = 0;
|
2007-05-28 06:44:46 +02:00
|
|
|
if (get_ptr_size() > 4)
|
|
|
|
spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
|
2007-09-04 09:52:16 +02:00
|
|
|
else
|
|
|
|
spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
|
2006-12-07 20:51:49 +01:00
|
|
|
spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
|
2004-02-17 21:36:16 +01:00
|
|
|
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 );
|
2016-03-22 07:06:27 +01:00
|
|
|
free( spec->c_name );
|
2004-02-17 21:36:16 +01:00
|
|
|
free( spec->init_func );
|
|
|
|
free( spec->entry_points );
|
|
|
|
free( spec->names );
|
|
|
|
free( spec->ordinals );
|
|
|
|
free( spec->resources );
|
|
|
|
free( spec );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-14 22:54:58 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* make_c_identifier
|
|
|
|
*
|
|
|
|
* Map a string to a valid C identifier.
|
|
|
|
*/
|
2016-03-22 07:06:27 +01:00
|
|
|
char *make_c_identifier( const char *str )
|
2002-05-14 22:54:58 +02:00
|
|
|
{
|
2016-03-22 07:06:27 +01:00
|
|
|
char *p, buffer[256];
|
2002-05-14 22:54:58 +02:00
|
|
|
|
|
|
|
for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
|
|
|
|
{
|
|
|
|
if (isalnum(*str)) *p = *str;
|
|
|
|
else *p = '_';
|
|
|
|
}
|
|
|
|
*p = 0;
|
2016-03-22 07:06:27 +01:00
|
|
|
return xstrdup( buffer );
|
2002-05-14 22:54:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-21 12:57:04 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* get_stub_name
|
|
|
|
*
|
|
|
|
* Generate an internal name for a stub entry point.
|
|
|
|
*/
|
|
|
|
const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
|
|
|
|
{
|
2010-08-02 10:11:23 +02:00
|
|
|
static char *buffer;
|
|
|
|
|
|
|
|
free( buffer );
|
2005-09-21 12:57:04 +02:00
|
|
|
if (odp->name || odp->export_name)
|
|
|
|
{
|
|
|
|
char *p;
|
2010-08-02 10:11:23 +02:00
|
|
|
buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
|
2005-09-21 12:57:04 +02:00
|
|
|
/* make sure name is a legal C identifier */
|
|
|
|
for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
|
|
|
|
if (!*p) return buffer;
|
2010-08-02 10:11:23 +02:00
|
|
|
free( buffer );
|
2005-09-21 12:57:04 +02:00
|
|
|
}
|
2010-08-02 10:11:23 +02:00
|
|
|
buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
|
2005-09-21 12:57:04 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:12:55 +01:00
|
|
|
/* return the stdcall-decorated name for an entry point */
|
|
|
|
const char *get_link_name( const ORDDEF *odp )
|
|
|
|
{
|
|
|
|
static char *buffer;
|
2019-05-16 10:12:42 +02:00
|
|
|
char *ret;
|
2019-03-06 10:12:55 +01:00
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.cpu != CPU_i386) return odp->link_name;
|
2019-05-16 10:12:42 +02:00
|
|
|
|
2019-05-18 09:21:49 +02:00
|
|
|
switch (odp->type)
|
2019-03-06 10:12:55 +01:00
|
|
|
{
|
2019-05-18 09:21:49 +02:00
|
|
|
case TYPE_STDCALL:
|
2021-01-25 15:24:23 +01:00
|
|
|
if (is_pe())
|
2019-05-18 09:21:49 +02:00
|
|
|
{
|
|
|
|
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:
|
2021-01-25 15:24:23 +01:00
|
|
|
if (is_pe() && !kill_at)
|
2019-05-18 09:21:49 +02:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
2019-05-16 10:12:42 +02:00
|
|
|
else return odp->link_name;
|
2019-05-18 09:21:49 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return odp->link_name;
|
2019-05-16 10:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free( buffer );
|
|
|
|
buffer = ret;
|
|
|
|
return ret;
|
2019-03-06 10:12:55 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:42:23 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-13 03:06:38 +01:00
|
|
|
/*****************************************************************
|
|
|
|
* 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
|
|
|
|
*
|
2016-04-19 06:14:05 +02:00
|
|
|
* The reason gas is written this way is that it's trying to mimic
|
2001-02-13 03:06:38 +01:00
|
|
|
* native assemblers for the various architectures it runs on. gas
|
2005-12-02 16:15:09 +01:00
|
|
|
* provides other directives that work consistently across
|
2001-02-13 03:06:38 +01:00
|
|
|
* architectures, but of course we want to work on all arches with or
|
|
|
|
* without gas. Hence this function.
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
2001-02-13 03:06:38 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
2005-06-27 13:23:24 +02:00
|
|
|
* align -- the number of bytes to align to. Must be a power of 2.
|
2001-02-13 03:06:38 +01:00
|
|
|
*/
|
2005-06-27 13:23:24 +02:00
|
|
|
unsigned int get_alignment(unsigned int align)
|
2001-02-13 03:06:38 +01:00
|
|
|
{
|
2005-06-27 13:23:24 +02:00
|
|
|
unsigned int n;
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2005-06-27 13:23:24 +02:00
|
|
|
assert( !(align & (align - 1)) );
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.cpu)
|
2001-02-13 03:06:38 +01:00
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
case CPU_i386:
|
2005-09-07 15:31:37 +02:00
|
|
|
case CPU_x86_64:
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.platform != PLATFORM_APPLE) return align;
|
2005-06-27 13:23:24 +02:00
|
|
|
/* fall through */
|
2010-09-18 18:01:16 +02:00
|
|
|
case CPU_ARM:
|
2013-01-16 00:43:01 +01:00
|
|
|
case CPU_ARM64:
|
2005-06-27 13:23:24 +02:00
|
|
|
n = 0;
|
2007-03-03 14:30:03 +01:00
|
|
|
while ((1u << n) != align) n++;
|
2005-06-27 13:23:24 +02:00
|
|
|
return n;
|
2001-02-13 03:06:38 +01:00
|
|
|
}
|
2005-06-27 13:23:24 +02:00
|
|
|
/* unreached */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2005-06-27 13:23:24 +02:00
|
|
|
/* return the page size for the target CPU */
|
|
|
|
unsigned int get_page_size(void)
|
|
|
|
{
|
2020-04-16 10:40:56 +02:00
|
|
|
return 0x1000; /* same on all platforms */
|
2005-06-27 13:23:24 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 12:06:19 +02:00
|
|
|
/* return the total size in bytes of the arguments on the stack */
|
|
|
|
unsigned int get_args_size( const ORDDEF *odp )
|
|
|
|
{
|
2011-05-03 12:49:15 +02:00
|
|
|
int i, size;
|
2010-08-30 12:06:19 +02:00
|
|
|
|
|
|
|
for (i = size = 0; i < odp->u.func.nb_args; i++)
|
|
|
|
{
|
|
|
|
switch (odp->u.func.args[i])
|
|
|
|
{
|
2010-08-30 12:40:53 +02:00
|
|
|
case ARG_INT64:
|
2010-08-30 12:06:19 +02:00
|
|
|
case ARG_DOUBLE:
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.cpu == CPU_ARM) size = (size + 7) & ~7;
|
2010-08-30 12:06:19 +02:00
|
|
|
size += 8;
|
|
|
|
break;
|
2010-08-30 12:40:53 +02:00
|
|
|
case ARG_INT128:
|
|
|
|
/* int128 is passed as pointer on x86_64 */
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.cpu != CPU_x86_64)
|
2010-08-30 12:40:53 +02:00
|
|
|
{
|
|
|
|
size += 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
2010-08-30 12:06:19 +02:00
|
|
|
default:
|
|
|
|
size += get_ptr_size();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2005-06-27 20:59:54 +02:00
|
|
|
/* return the assembly name for a C symbol */
|
|
|
|
const char *asm_name( const char *sym )
|
2005-06-27 13:23:24 +02:00
|
|
|
{
|
2010-08-02 10:11:23 +02:00
|
|
|
static char *buffer;
|
2005-06-29 22:18:58 +02:00
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-06-29 22:18:58 +02:00
|
|
|
{
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2005-06-29 22:18:58 +02:00
|
|
|
case PLATFORM_WINDOWS:
|
2021-11-16 21:02:12 +01:00
|
|
|
if (target.cpu != CPU_i386) return sym;
|
2019-05-16 10:12:42 +02:00
|
|
|
if (sym[0] == '@') return sym; /* fastcall */
|
2019-03-06 10:40:13 +01:00
|
|
|
/* fall through */
|
|
|
|
case PLATFORM_APPLE:
|
2009-02-27 17:39:54 +01:00
|
|
|
if (sym[0] == '.' && sym[1] == 'L') return sym;
|
2010-08-02 10:11:23 +02:00
|
|
|
free( buffer );
|
|
|
|
buffer = strmake( "_%s", sym );
|
2005-06-29 22:18:58 +02:00
|
|
|
return buffer;
|
|
|
|
default:
|
|
|
|
return sym;
|
|
|
|
}
|
2005-06-27 13:23:24 +02:00
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2005-06-27 13:23:24 +02:00
|
|
|
/* return an assembly function declaration for a C function name */
|
|
|
|
const char *func_declaration( const char *func )
|
|
|
|
{
|
2010-08-02 10:11:23 +02:00
|
|
|
static char *buffer;
|
2005-06-29 22:18:58 +02:00
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-06-29 22:18:58 +02:00
|
|
|
{
|
2005-06-30 13:00:28 +02:00
|
|
|
case PLATFORM_APPLE:
|
|
|
|
return "";
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2005-06-29 22:18:58 +02:00
|
|
|
case PLATFORM_WINDOWS:
|
2010-08-02 10:11:23 +02:00
|
|
|
free( buffer );
|
2021-01-16 11:05:38 +01:00
|
|
|
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" : "" );
|
2005-06-29 22:18:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
2010-08-02 10:11:23 +02:00
|
|
|
free( buffer );
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.cpu)
|
2009-11-17 20:20:48 +01:00
|
|
|
{
|
|
|
|
case CPU_ARM:
|
2021-01-16 11:05:38 +01:00
|
|
|
buffer = strmake( ".type %s,%%function%s", func,
|
|
|
|
thumb_mode ? "\n\t.thumb_func" : "" );
|
|
|
|
break;
|
2013-01-16 00:43:01 +01:00
|
|
|
case CPU_ARM64:
|
2010-08-02 10:11:23 +02:00
|
|
|
buffer = strmake( ".type %s,%%function", func );
|
2009-11-17 20:20:48 +01:00
|
|
|
break;
|
|
|
|
default:
|
2010-08-02 10:11:23 +02:00
|
|
|
buffer = strmake( ".type %s,@function", func );
|
2009-11-17 20:20:48 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-06-29 22:18:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2005-06-27 13:23:24 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2005-09-21 13:50:04 +02:00
|
|
|
/* output a size declaration for an assembly function */
|
2006-08-11 20:41:29 +02:00
|
|
|
void output_function_size( const char *name )
|
2005-06-27 13:23:24 +02:00
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-06-29 22:18:58 +02:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE:
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2005-06-29 22:18:58 +02:00
|
|
|
case PLATFORM_WINDOWS:
|
2005-09-21 13:50:04 +02:00
|
|
|
break;
|
2005-06-29 22:18:58 +02:00
|
|
|
default:
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t.size %s, .-%s\n", name, name );
|
2005-09-21 13:50:04 +02:00
|
|
|
break;
|
2005-06-29 22:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-11 12:01:01 +02:00
|
|
|
/* output a .cfi directive */
|
|
|
|
void output_cfi( const char *format, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
|
2010-04-11 12:07:44 +02:00
|
|
|
if (!unwind_tables) return;
|
2010-04-11 12:01:01 +02:00
|
|
|
va_start( valist, format );
|
|
|
|
fputc( '\t', output_file );
|
|
|
|
vfprintf( output_file, format, valist );
|
|
|
|
fputc( '\n', output_file );
|
|
|
|
va_end( valist );
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:13:45 +01:00
|
|
|
/* output an RVA pointer */
|
|
|
|
void output_rva( const char *format, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
|
|
|
|
va_start( valist, format );
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2019-03-06 10:13:45 +01:00
|
|
|
{
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2019-03-06 10:13:45 +01:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2006-01-21 19:23:36 +01:00
|
|
|
/* output the GNU note for non-exec stack */
|
2006-08-11 20:41:29 +02:00
|
|
|
void output_gnu_stack_note(void)
|
2006-01-21 19:23:36 +01:00
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2006-01-21 19:23:36 +01:00
|
|
|
{
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2006-01-21 19:23:36 +01:00
|
|
|
case PLATFORM_WINDOWS:
|
|
|
|
case PLATFORM_APPLE:
|
|
|
|
break;
|
|
|
|
default:
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.cpu)
|
2009-11-17 20:20:48 +01:00
|
|
|
{
|
|
|
|
case CPU_ARM:
|
2013-01-16 00:43:01 +01:00
|
|
|
case CPU_ARM64:
|
2009-11-17 20:20:48 +01:00
|
|
|
output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
|
|
|
|
break;
|
|
|
|
}
|
2006-01-21 19:23:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-15 20:48:39 +02:00
|
|
|
/* return a global symbol declaration for an assembly symbol */
|
|
|
|
const char *asm_globl( const char *func )
|
|
|
|
{
|
2010-08-02 10:11:23 +02:00
|
|
|
static char *buffer;
|
2005-09-15 20:48:39 +02:00
|
|
|
|
2010-08-02 10:11:23 +02:00
|
|
|
free( buffer );
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-09-15 20:48:39 +02:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE:
|
2010-08-02 10:11:23 +02:00
|
|
|
buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
|
|
|
|
break;
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2005-09-15 20:48:39 +02:00
|
|
|
case PLATFORM_WINDOWS:
|
2021-11-16 21:02:12 +01:00
|
|
|
buffer = strmake( "\t.globl %s%s\n%s%s:", target.cpu == CPU_i386 ? "_" : "", func,
|
|
|
|
target.cpu == CPU_i386 ? "_" : "", func );
|
2010-08-02 10:11:23 +02:00
|
|
|
break;
|
2005-09-15 20:48:39 +02:00
|
|
|
default:
|
2010-08-02 10:11:23 +02:00
|
|
|
buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
|
|
|
|
break;
|
2005-09-15 20:48:39 +02:00
|
|
|
}
|
2010-08-02 10:11:23 +02:00
|
|
|
return buffer;
|
2005-09-15 20:48:39 +02:00
|
|
|
}
|
|
|
|
|
2005-09-07 15:31:37 +02:00
|
|
|
const char *get_asm_ptr_keyword(void)
|
|
|
|
{
|
|
|
|
switch(get_ptr_size())
|
|
|
|
{
|
|
|
|
case 4: return ".long";
|
|
|
|
case 8: return ".quad";
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-29 22:18:58 +02:00
|
|
|
const char *get_asm_string_keyword(void)
|
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-06-29 22:18:58 +02:00
|
|
|
{
|
2005-06-30 13:00:28 +02:00
|
|
|
case PLATFORM_APPLE:
|
|
|
|
return ".asciz";
|
|
|
|
default:
|
|
|
|
return ".string";
|
2005-06-29 22:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 10:51:24 +01:00
|
|
|
const char *get_asm_export_section(void)
|
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2019-03-13 10:51:24 +01:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE: return ".data";
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2019-03-13 10:51:24 +01:00
|
|
|
case PLATFORM_WINDOWS: return ".section .edata";
|
|
|
|
default: return ".section .data";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-18 10:45:01 +01:00
|
|
|
const char *get_asm_rodata_section(void)
|
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-12-18 10:45:01 +01:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE: return ".const";
|
|
|
|
default: return ".section .rodata";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 10:51:24 +01:00
|
|
|
const char *get_asm_rsrc_section(void)
|
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2019-03-13 10:51:24 +01:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE: return ".data";
|
2021-01-25 15:24:23 +01:00
|
|
|
case PLATFORM_MINGW:
|
2019-03-13 10:51:24 +01:00
|
|
|
case PLATFORM_WINDOWS: return ".section .rsrc";
|
|
|
|
default: return ".section .data";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-14 21:18:05 +02:00
|
|
|
const char *get_asm_string_section(void)
|
|
|
|
{
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2005-09-14 21:18:05 +02:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE: return ".cstring";
|
|
|
|
default: return ".section .rodata";
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 13:54:14 +02:00
|
|
|
|
|
|
|
const char *arm64_page( const char *sym )
|
|
|
|
{
|
|
|
|
static char *buffer;
|
|
|
|
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2020-08-14 13:54:14 +02:00
|
|
|
{
|
|
|
|
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 );
|
2021-11-16 21:02:12 +01:00
|
|
|
switch (target.platform)
|
2020-08-14 13:54:14 +02:00
|
|
|
{
|
|
|
|
case PLATFORM_APPLE:
|
|
|
|
buffer = strmake( "%s@PAGEOFF", sym );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buffer = strmake( ":lo12:%s", sym );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|