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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2000-06-23 22:15:35 +02:00
|
|
|
|
2001-11-06 21:57:11 +01:00
|
|
|
#include "config.h"
|
2002-04-25 23:40:56 +02:00
|
|
|
#include "wine/port.h"
|
2001-11-06 21:57:11 +01:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
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);
|
2005-03-18 15:03:38 +01:00
|
|
|
if (size && res == NULL)
|
2000-06-23 22:15:35 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-05-06 17:54:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-06-23 22:15:35 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
/* output a standard header for generated files */
|
|
|
|
void output_standard_file_header( FILE *outfile )
|
|
|
|
{
|
2005-03-15 16:42:32 +01:00
|
|
|
if (spec_file_name)
|
2002-05-12 01:06:32 +02:00
|
|
|
fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
|
2005-03-15 16:42:32 +01:00
|
|
|
spec_file_name );
|
2002-05-12 01:06:32 +02:00
|
|
|
else
|
|
|
|
fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
|
2002-03-10 00:29:33 +01:00
|
|
|
fprintf( outfile,
|
|
|
|
"/* This file can be copied, modified and distributed without restriction. */\n\n" );
|
|
|
|
}
|
|
|
|
|
2000-06-23 22:15:35 +02:00
|
|
|
/* dump a byte stream into the assembly code */
|
2000-10-25 22:33:58 +02:00
|
|
|
void dump_bytes( FILE *outfile, const unsigned char *data, int len,
|
|
|
|
const char *label, int constant )
|
2000-06-23 22:15:35 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2000-10-25 22:33:58 +02:00
|
|
|
fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
|
|
|
|
constant ? "const " : "", label, len );
|
2000-06-23 22:15:35 +02:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2000-10-25 22:33:58 +02:00
|
|
|
if (!(i & 7)) fprintf( outfile, "\n " );
|
|
|
|
fprintf( outfile, "0x%02x", *data++ );
|
|
|
|
if (i < len - 1) fprintf( outfile, "," );
|
2000-06-23 22:15:35 +02:00
|
|
|
}
|
|
|
|
fprintf( outfile, "\n};\n" );
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
|
|
|
|
strcpy( fullname, srcdir );
|
|
|
|
strcat( fullname, "/" );
|
|
|
|
strcat( fullname, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
/* make sure all the rest is digits */
|
|
|
|
for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
|
|
|
|
*end = 0;
|
|
|
|
return atoi( end + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* alloc_dll_spec
|
|
|
|
*
|
|
|
|
* Create a new dll spec file descriptor
|
|
|
|
*/
|
|
|
|
DLLSPEC *alloc_dll_spec(void)
|
|
|
|
{
|
|
|
|
DLLSPEC *spec;
|
|
|
|
|
|
|
|
spec = xmalloc( sizeof(*spec) );
|
|
|
|
spec->file_name = NULL;
|
|
|
|
spec->dll_name = NULL;
|
|
|
|
spec->owner_name = NULL;
|
|
|
|
spec->init_func = NULL;
|
|
|
|
spec->type = SPEC_WIN32;
|
|
|
|
spec->base = MAX_ORDINALS;
|
|
|
|
spec->limit = 0;
|
|
|
|
spec->stack_size = 0;
|
|
|
|
spec->heap_size = 0;
|
|
|
|
spec->nb_entry_points = 0;
|
|
|
|
spec->alloc_entry_points = 0;
|
|
|
|
spec->nb_names = 0;
|
|
|
|
spec->nb_resources = 0;
|
2004-03-25 01:40:52 +01:00
|
|
|
spec->characteristics = 0;
|
|
|
|
spec->subsystem = 0;
|
|
|
|
spec->subsystem_major = 4;
|
|
|
|
spec->subsystem_minor = 0;
|
2004-02-17 21:36:16 +01:00
|
|
|
spec->entry_points = NULL;
|
|
|
|
spec->names = NULL;
|
|
|
|
spec->ordinals = NULL;
|
|
|
|
spec->resources = NULL;
|
|
|
|
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->owner_name );
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const char *make_c_identifier( const char *str )
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
|
|
|
|
{
|
|
|
|
if (isalnum(*str)) *p = *str;
|
|
|
|
else *p = '_';
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* The reason gas is written this way is that it's trying to mimick
|
|
|
|
* native assemblers for the various architectures it runs on. gas
|
|
|
|
* provides other directives that work consistantly across
|
|
|
|
* 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
|
|
|
|
2005-06-27 13:23:24 +02:00
|
|
|
switch(target_cpu)
|
2001-02-13 03:06:38 +01:00
|
|
|
{
|
2005-06-27 13:23:24 +02:00
|
|
|
case CPU_x86:
|
|
|
|
case CPU_SPARC:
|
|
|
|
if (target_platform != PLATFORM_APPLE) return align;
|
|
|
|
/* fall through */
|
|
|
|
case CPU_POWERPC:
|
|
|
|
case CPU_ALPHA:
|
|
|
|
n = 0;
|
|
|
|
while ((1 << n) != align) n++;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
switch(target_cpu)
|
|
|
|
{
|
|
|
|
case CPU_x86: return 4096;
|
|
|
|
case CPU_POWERPC: return 4096;
|
|
|
|
case CPU_SPARC: return 8192;
|
|
|
|
case CPU_ALPHA: return 8192;
|
|
|
|
}
|
|
|
|
/* unreached */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
static char buffer[256];
|
2005-06-29 22:18:58 +02:00
|
|
|
|
|
|
|
switch (target_platform)
|
|
|
|
{
|
|
|
|
case PLATFORM_APPLE:
|
|
|
|
case PLATFORM_WINDOWS:
|
|
|
|
buffer[0] = '_';
|
|
|
|
strcpy( buffer + 1, sym );
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
2005-06-29 22:18:58 +02:00
|
|
|
|
|
|
|
switch (target_platform)
|
|
|
|
{
|
2005-06-30 13:00:28 +02:00
|
|
|
case PLATFORM_APPLE:
|
|
|
|
return "";
|
2005-06-29 22:18:58 +02:00
|
|
|
case PLATFORM_WINDOWS:
|
|
|
|
sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
|
|
|
|
break;
|
|
|
|
case PLATFORM_SVR4:
|
|
|
|
sprintf( buffer, ".type %s,2", func );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sprintf( buffer, ".type %s,@function", func );
|
|
|
|
break;
|
|
|
|
}
|
2005-06-27 13:23:24 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2005-06-27 13:23:24 +02:00
|
|
|
/* return a size declaration for an assembly function */
|
|
|
|
const char *func_size( const char *func )
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
2005-06-29 22:18:58 +02:00
|
|
|
|
|
|
|
switch (target_platform)
|
|
|
|
{
|
|
|
|
case PLATFORM_APPLE:
|
|
|
|
case PLATFORM_WINDOWS:
|
|
|
|
return "";
|
|
|
|
default:
|
|
|
|
sprintf( buffer, ".size %s, .-%s", func, func );
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *get_asm_string_keyword(void)
|
|
|
|
{
|
|
|
|
switch (target_platform)
|
|
|
|
{
|
2005-06-30 13:00:28 +02:00
|
|
|
case PLATFORM_APPLE:
|
|
|
|
case PLATFORM_SVR4:
|
|
|
|
return ".asciz";
|
|
|
|
default:
|
|
|
|
return ".string";
|
2005-06-29 22:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *get_asm_short_keyword(void)
|
|
|
|
{
|
|
|
|
switch (target_platform)
|
|
|
|
{
|
|
|
|
case PLATFORM_SVR4: return ".half";
|
|
|
|
default: return ".short";
|
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
}
|