Added some general purpose routines for managing list of symbol

names.
This commit is contained in:
Alexandre Julliard 2005-08-19 10:25:22 +00:00
parent 0f254d0356
commit ec1cdfd12b
1 changed files with 79 additions and 107 deletions

View File

@ -22,6 +22,7 @@
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
@ -51,13 +52,15 @@ struct import
int nb_imports; /* number of imported functions */
};
static char **undef_symbols; /* list of undefined symbols */
static int nb_undef_symbols = -1;
static int undef_size;
struct name_table
{
char **names;
unsigned int count, size;
};
static char **ignore_symbols; /* list of symbols to ignore */
static int nb_ignore_symbols;
static int ignore_size;
static struct name_table undef_symbols; /* list of undefined symbols */
static struct name_table ignore_symbols; /* list of symbols to ignore */
static struct name_table delayed_imports; /* list of delayed import dlls */
static char *ld_tmp_file; /* ld temp file name */
@ -66,8 +69,6 @@ static int nb_imports = 0; /* number of imported dlls (delayed or not) */
static int nb_delayed = 0; /* number of delayed dlls */
static int total_imports = 0; /* total number of imported functions */
static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
static char **delayed_imports; /* names of delayed import dlls */
static int nb_delayed_imports; /* size of the delayed_imports array */
/* list of symbols that are ignored by default */
static const char * const default_ignored_symbols[] =
@ -146,16 +147,50 @@ static int func_cmp( const void *func1, const void *func2 )
odp2->name ? odp2->name : odp2->export_name );
}
/* locate a symbol in a (sorted) list */
inline static const char *find_symbol( const char *name, char **table, int size )
/* add a name to a name table */
inline static void add_name( struct name_table *table, const char *name )
{
if (table->count == table->size)
{
table->size += (table->size / 2);
if (table->size < 32) table->size = 32;
table->names = xrealloc( table->names, table->size * sizeof(*table->names) );
}
table->names[table->count++] = xstrdup( name );
}
/* remove a name from a name table */
inline static void remove_name( struct name_table *table, unsigned int idx )
{
assert( idx < table->count );
free( table->names[idx] );
memmove( table->names + idx, table->names + idx + 1,
(table->count - idx - 1) * sizeof(*table->names) );
table->count--;
}
/* make a name table empty */
inline static void empty_name_table( struct name_table *table )
{
unsigned int i;
for (i = 0; i < table->count; i++) free( table->names[i] );
table->count = 0;
}
/* locate a name in a (sorted) list */
inline static const char *find_name( const char *name, const struct name_table *table )
{
char **res = NULL;
if (table) {
res = bsearch( &name, table, size, sizeof(*table), name_cmp );
if (table->count) res = bsearch( &name, table->names, table->count, sizeof(*table->names), name_cmp );
return res ? *res : NULL;
}
return res ? *res : NULL;
/* sort a name table */
inline static void sort_names( struct name_table *table )
{
if (table->count) qsort( table->names, table->count, sizeof(*table->names), name_cmp );
}
/* locate an export in a (sorted) export list */
@ -170,13 +205,6 @@ inline static ORDDEF *find_export( const char *name, ORDDEF **table, int size )
return res ? *res : NULL;
}
/* sort a symbol table */
inline static void sort_symbols( char **table, int size )
{
if (table )
qsort( table, size, sizeof(*table), name_cmp );
}
inline static void output_function_size( FILE *outfile, const char *name )
{
const char *size = func_size( name );
@ -204,9 +232,9 @@ static int is_delayed_import( const char *name )
{
int i;
for (i = 0; i < nb_delayed_imports; i++)
for (i = 0; i < delayed_imports.count; i++)
{
if (!strcmp( delayed_imports[i], name )) return 1;
if (!strcmp( delayed_imports.names[i], name )) return 1;
}
return 0;
}
@ -362,13 +390,13 @@ void add_delayed_import( const char *name )
struct import *imp;
char *fullname = get_dll_name( name, NULL );
delayed_imports = xrealloc( delayed_imports, (nb_delayed_imports+1) * sizeof(*delayed_imports) );
delayed_imports[nb_delayed_imports++] = fullname;
add_name( &delayed_imports, fullname );
if ((imp = is_already_imported( fullname )) && !imp->delay)
{
imp->delay = 1;
nb_delayed++;
}
free( fullname );
}
/* remove an imported dll, based on its index in the dll_imports array */
@ -385,52 +413,29 @@ static void remove_import_dll( int index )
/* initialize the list of ignored symbols */
static void init_ignored_symbols(void)
{
int i;
unsigned int i;
nb_ignore_symbols = sizeof(default_ignored_symbols)/sizeof(default_ignored_symbols[0]);
ignore_size = nb_ignore_symbols + 32;
ignore_symbols = xmalloc( ignore_size * sizeof(*ignore_symbols) );
for (i = 0; i < nb_ignore_symbols; i++)
ignore_symbols[i] = xstrdup( default_ignored_symbols[i] );
for (i = 0; i < sizeof(default_ignored_symbols)/sizeof(default_ignored_symbols[0]); i++)
add_name( &ignore_symbols, default_ignored_symbols[i] );
}
/* add a symbol to the ignored symbol list */
/* if the name starts with '-' the symbol is removed instead */
void add_ignore_symbol( const char *name )
{
int i;
unsigned int i;
if (!ignore_symbols) init_ignored_symbols(); /* first time around, fill list with defaults */
if (!ignore_symbols.size) init_ignored_symbols(); /* first time around, fill list with defaults */
if (name[0] == '-') /* remove it */
{
if (!name[1]) /* remove everything */
if (!name[1]) empty_name_table( &ignore_symbols ); /* remove everything */
else for (i = 0; i < ignore_symbols.count; i++)
{
for (i = 0; i < nb_ignore_symbols; i++) free( ignore_symbols[i] );
nb_ignore_symbols = 0;
}
else
{
for (i = 0; i < nb_ignore_symbols; i++)
{
if (!strcmp( ignore_symbols[i], name+1 ))
{
free( ignore_symbols[i] );
memmove( &ignore_symbols[i], &ignore_symbols[i+1], nb_ignore_symbols - i - 1 );
nb_ignore_symbols--;
if (!strcmp( ignore_symbols.names[i], name+1 )) remove_name( &ignore_symbols, i-- );
}
}
}
}
else
{
if (nb_ignore_symbols == ignore_size)
{
ignore_size += 32;
ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
}
ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
}
else add_name( &ignore_symbols, name );
}
/* add a function to the list of imports from a given dll */
@ -442,36 +447,12 @@ static void add_import_func( struct import *imp, ORDDEF *func )
if (imp->delay) total_delayed++;
}
/* add a symbol to the undef list */
inline static void add_undef_symbol( const char *name )
{
if (nb_undef_symbols == undef_size)
{
undef_size += 128;
undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
}
undef_symbols[nb_undef_symbols++] = xstrdup( name );
}
/* remove all the holes in the undefined symbol list; return the number of removed symbols */
static int remove_symbol_holes(void)
{
int i, off;
for (i = off = 0; i < nb_undef_symbols; i++)
{
if (!undef_symbols[i]) off++;
else undef_symbols[i - off] = undef_symbols[i];
}
nb_undef_symbols -= off;
return off;
}
/* add a symbol to the extra list, but only if needed */
static int add_extra_symbol( const char **extras, int *count, const char *name, const DLLSPEC *spec )
{
int i;
if (!find_symbol( name, undef_symbols, nb_undef_symbols ))
if (!find_name( name, &undef_symbols ))
{
/* check if the symbol is being exported by this dll */
for (i = 0; i < spec->nb_entry_points; i++)
@ -498,7 +479,7 @@ static void add_extra_undef_symbols( const DLLSPEC *spec )
int i, count = 0, nb_stubs = 0;
int kernel_imports = 0, ntdll_imports = 0;
sort_symbols( undef_symbols, nb_undef_symbols );
sort_names( &undef_symbols );
for (i = 0; i < spec->nb_entry_points; i++)
{
@ -536,8 +517,8 @@ static void add_extra_undef_symbols( const DLLSPEC *spec )
if (count)
{
for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
sort_symbols( undef_symbols, nb_undef_symbols );
for (i = 0; i < count; i++) add_name( &undef_symbols, extras[i] );
sort_names( &undef_symbols );
}
}
@ -606,8 +587,6 @@ void read_undef_symbols( char **argv )
strcpy( name_prefix, asm_name("") );
prefix_len = strlen( name_prefix );
undef_size = nb_undef_symbols = 0;
/* if we have multiple object files, link them together */
if (argv[1]) name = ldcombine_files( argv );
else name = argv[0];
@ -627,7 +606,7 @@ void read_undef_symbols( char **argv )
while (*p == ' ') p++;
if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
add_undef_symbol( p );
add_name( &undef_symbols, p );
}
if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
free( cmd );
@ -635,27 +614,21 @@ void read_undef_symbols( char **argv )
static void remove_ignored_symbols(void)
{
int i;
unsigned int i;
if (!ignore_symbols) init_ignored_symbols();
sort_symbols( ignore_symbols, nb_ignore_symbols );
for (i = 0; i < nb_undef_symbols; i++)
if (!ignore_symbols.size) init_ignored_symbols();
sort_names( &ignore_symbols );
for (i = 0; i < undef_symbols.count; i++)
{
if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
{
free( undef_symbols[i] );
undef_symbols[i] = NULL;
if (find_name( undef_symbols.names[i], &ignore_symbols ))
remove_name( &undef_symbols, i-- );
}
}
remove_symbol_holes();
}
/* resolve the imports for a Win32 module */
int resolve_imports( DLLSPEC *spec )
{
int i, j;
if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
unsigned int i, j, removed;
add_extra_undef_symbols( spec );
remove_ignored_symbols();
@ -664,18 +637,17 @@ int resolve_imports( DLLSPEC *spec )
{
struct import *imp = dll_imports[i];
for (j = 0; j < nb_undef_symbols; j++)
for (j = removed = 0; j < undef_symbols.count; j++)
{
ORDDEF *odp = find_export( undef_symbols[j], imp->exports, imp->nb_exports );
ORDDEF *odp = find_export( undef_symbols.names[j], imp->exports, imp->nb_exports );
if (odp)
{
add_import_func( imp, odp );
free( undef_symbols[j] );
undef_symbols[j] = NULL;
remove_name( &undef_symbols, j-- );
removed++;
}
}
/* remove all the holes in the undef symbols list */
if (!remove_symbol_holes() && check_unused( imp, spec ))
if (!removed && check_unused( imp, spec ))
{
/* the dll is not used, get rid of it */
warning( "%s imported but no symbols used\n", imp->spec->file_name );