Home | History | Annotate | Line # | Download | only in bfd
hash.c revision 1.1.1.3
      1      1.1  christos /* hash.c -- hash table routines for BFD
      2  1.1.1.3  christos    Copyright (C) 1993-2016 Free Software Foundation, Inc.
      3      1.1  christos    Written by Steve Chamberlain <sac (at) cygnus.com>
      4      1.1  christos 
      5      1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program; if not, write to the Free Software
     19      1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20      1.1  christos    MA 02110-1301, USA.  */
     21      1.1  christos 
     22      1.1  christos #include "sysdep.h"
     23      1.1  christos #include "bfd.h"
     24      1.1  christos #include "libbfd.h"
     25      1.1  christos #include "objalloc.h"
     26      1.1  christos #include "libiberty.h"
     27      1.1  christos 
     28      1.1  christos /*
     29      1.1  christos SECTION
     30      1.1  christos 	Hash Tables
     31      1.1  christos 
     32      1.1  christos @cindex Hash tables
     33      1.1  christos 	BFD provides a simple set of hash table functions.  Routines
     34      1.1  christos 	are provided to initialize a hash table, to free a hash table,
     35      1.1  christos 	to look up a string in a hash table and optionally create an
     36      1.1  christos 	entry for it, and to traverse a hash table.  There is
     37      1.1  christos 	currently no routine to delete an string from a hash table.
     38      1.1  christos 
     39      1.1  christos 	The basic hash table does not permit any data to be stored
     40      1.1  christos 	with a string.  However, a hash table is designed to present a
     41      1.1  christos 	base class from which other types of hash tables may be
     42      1.1  christos 	derived.  These derived types may store additional information
     43      1.1  christos 	with the string.  Hash tables were implemented in this way,
     44      1.1  christos 	rather than simply providing a data pointer in a hash table
     45      1.1  christos 	entry, because they were designed for use by the linker back
     46      1.1  christos 	ends.  The linker may create thousands of hash table entries,
     47      1.1  christos 	and the overhead of allocating private data and storing and
     48      1.1  christos 	following pointers becomes noticeable.
     49      1.1  christos 
     50      1.1  christos 	The basic hash table code is in <<hash.c>>.
     51      1.1  christos 
     52      1.1  christos @menu
     53      1.1  christos @* Creating and Freeing a Hash Table::
     54      1.1  christos @* Looking Up or Entering a String::
     55      1.1  christos @* Traversing a Hash Table::
     56      1.1  christos @* Deriving a New Hash Table Type::
     57      1.1  christos @end menu
     58      1.1  christos 
     59      1.1  christos INODE
     60      1.1  christos Creating and Freeing a Hash Table, Looking Up or Entering a String, Hash Tables, Hash Tables
     61      1.1  christos SUBSECTION
     62      1.1  christos 	Creating and freeing a hash table
     63      1.1  christos 
     64      1.1  christos @findex bfd_hash_table_init
     65      1.1  christos @findex bfd_hash_table_init_n
     66      1.1  christos 	To create a hash table, create an instance of a <<struct
     67      1.1  christos 	bfd_hash_table>> (defined in <<bfd.h>>) and call
     68      1.1  christos 	<<bfd_hash_table_init>> (if you know approximately how many
     69      1.1  christos 	entries you will need, the function <<bfd_hash_table_init_n>>,
     70      1.1  christos 	which takes a @var{size} argument, may be used).
     71      1.1  christos 	<<bfd_hash_table_init>> returns <<FALSE>> if some sort of
     72      1.1  christos 	error occurs.
     73      1.1  christos 
     74      1.1  christos @findex bfd_hash_newfunc
     75      1.1  christos 	The function <<bfd_hash_table_init>> take as an argument a
     76      1.1  christos 	function to use to create new entries.  For a basic hash
     77      1.1  christos 	table, use the function <<bfd_hash_newfunc>>.  @xref{Deriving
     78      1.1  christos 	a New Hash Table Type}, for why you would want to use a
     79      1.1  christos 	different value for this argument.
     80      1.1  christos 
     81      1.1  christos @findex bfd_hash_allocate
     82      1.1  christos 	<<bfd_hash_table_init>> will create an objalloc which will be
     83      1.1  christos 	used to allocate new entries.  You may allocate memory on this
     84      1.1  christos 	objalloc using <<bfd_hash_allocate>>.
     85      1.1  christos 
     86      1.1  christos @findex bfd_hash_table_free
     87      1.1  christos 	Use <<bfd_hash_table_free>> to free up all the memory that has
     88      1.1  christos 	been allocated for a hash table.  This will not free up the
     89      1.1  christos 	<<struct bfd_hash_table>> itself, which you must provide.
     90      1.1  christos 
     91      1.1  christos @findex bfd_hash_set_default_size
     92      1.1  christos 	Use <<bfd_hash_set_default_size>> to set the default size of
     93      1.1  christos 	hash table to use.
     94      1.1  christos 
     95      1.1  christos INODE
     96      1.1  christos Looking Up or Entering a String, Traversing a Hash Table, Creating and Freeing a Hash Table, Hash Tables
     97      1.1  christos SUBSECTION
     98      1.1  christos 	Looking up or entering a string
     99      1.1  christos 
    100      1.1  christos @findex bfd_hash_lookup
    101      1.1  christos 	The function <<bfd_hash_lookup>> is used both to look up a
    102      1.1  christos 	string in the hash table and to create a new entry.
    103      1.1  christos 
    104      1.1  christos 	If the @var{create} argument is <<FALSE>>, <<bfd_hash_lookup>>
    105      1.1  christos 	will look up a string.  If the string is found, it will
    106      1.1  christos 	returns a pointer to a <<struct bfd_hash_entry>>.  If the
    107      1.1  christos 	string is not found in the table <<bfd_hash_lookup>> will
    108      1.1  christos 	return <<NULL>>.  You should not modify any of the fields in
    109      1.1  christos 	the returns <<struct bfd_hash_entry>>.
    110      1.1  christos 
    111      1.1  christos 	If the @var{create} argument is <<TRUE>>, the string will be
    112      1.1  christos 	entered into the hash table if it is not already there.
    113      1.1  christos 	Either way a pointer to a <<struct bfd_hash_entry>> will be
    114      1.1  christos 	returned, either to the existing structure or to a newly
    115      1.1  christos 	created one.  In this case, a <<NULL>> return means that an
    116      1.1  christos 	error occurred.
    117      1.1  christos 
    118      1.1  christos 	If the @var{create} argument is <<TRUE>>, and a new entry is
    119      1.1  christos 	created, the @var{copy} argument is used to decide whether to
    120      1.1  christos 	copy the string onto the hash table objalloc or not.  If
    121      1.1  christos 	@var{copy} is passed as <<FALSE>>, you must be careful not to
    122      1.1  christos 	deallocate or modify the string as long as the hash table
    123      1.1  christos 	exists.
    124      1.1  christos 
    125      1.1  christos INODE
    126      1.1  christos Traversing a Hash Table, Deriving a New Hash Table Type, Looking Up or Entering a String, Hash Tables
    127      1.1  christos SUBSECTION
    128      1.1  christos 	Traversing a hash table
    129      1.1  christos 
    130      1.1  christos @findex bfd_hash_traverse
    131      1.1  christos 	The function <<bfd_hash_traverse>> may be used to traverse a
    132      1.1  christos 	hash table, calling a function on each element.  The traversal
    133      1.1  christos 	is done in a random order.
    134      1.1  christos 
    135      1.1  christos 	<<bfd_hash_traverse>> takes as arguments a function and a
    136      1.1  christos 	generic <<void *>> pointer.  The function is called with a
    137      1.1  christos 	hash table entry (a <<struct bfd_hash_entry *>>) and the
    138      1.1  christos 	generic pointer passed to <<bfd_hash_traverse>>.  The function
    139      1.1  christos 	must return a <<boolean>> value, which indicates whether to
    140      1.1  christos 	continue traversing the hash table.  If the function returns
    141      1.1  christos 	<<FALSE>>, <<bfd_hash_traverse>> will stop the traversal and
    142      1.1  christos 	return immediately.
    143      1.1  christos 
    144      1.1  christos INODE
    145      1.1  christos Deriving a New Hash Table Type, , Traversing a Hash Table, Hash Tables
    146      1.1  christos SUBSECTION
    147      1.1  christos 	Deriving a new hash table type
    148      1.1  christos 
    149      1.1  christos 	Many uses of hash tables want to store additional information
    150      1.1  christos 	which each entry in the hash table.  Some also find it
    151      1.1  christos 	convenient to store additional information with the hash table
    152      1.1  christos 	itself.  This may be done using a derived hash table.
    153      1.1  christos 
    154      1.1  christos 	Since C is not an object oriented language, creating a derived
    155      1.1  christos 	hash table requires sticking together some boilerplate
    156      1.1  christos 	routines with a few differences specific to the type of hash
    157      1.1  christos 	table you want to create.
    158      1.1  christos 
    159      1.1  christos 	An example of a derived hash table is the linker hash table.
    160      1.1  christos 	The structures for this are defined in <<bfdlink.h>>.  The
    161      1.1  christos 	functions are in <<linker.c>>.
    162      1.1  christos 
    163      1.1  christos 	You may also derive a hash table from an already derived hash
    164      1.1  christos 	table.  For example, the a.out linker backend code uses a hash
    165      1.1  christos 	table derived from the linker hash table.
    166      1.1  christos 
    167      1.1  christos @menu
    168      1.1  christos @* Define the Derived Structures::
    169      1.1  christos @* Write the Derived Creation Routine::
    170      1.1  christos @* Write Other Derived Routines::
    171      1.1  christos @end menu
    172      1.1  christos 
    173      1.1  christos INODE
    174      1.1  christos Define the Derived Structures, Write the Derived Creation Routine, Deriving a New Hash Table Type, Deriving a New Hash Table Type
    175      1.1  christos SUBSUBSECTION
    176      1.1  christos 	Define the derived structures
    177      1.1  christos 
    178      1.1  christos 	You must define a structure for an entry in the hash table,
    179      1.1  christos 	and a structure for the hash table itself.
    180      1.1  christos 
    181      1.1  christos 	The first field in the structure for an entry in the hash
    182      1.1  christos 	table must be of the type used for an entry in the hash table
    183      1.1  christos 	you are deriving from.  If you are deriving from a basic hash
    184      1.1  christos 	table this is <<struct bfd_hash_entry>>, which is defined in
    185      1.1  christos 	<<bfd.h>>.  The first field in the structure for the hash
    186      1.1  christos 	table itself must be of the type of the hash table you are
    187      1.1  christos 	deriving from itself.  If you are deriving from a basic hash
    188      1.1  christos 	table, this is <<struct bfd_hash_table>>.
    189      1.1  christos 
    190      1.1  christos 	For example, the linker hash table defines <<struct
    191      1.1  christos 	bfd_link_hash_entry>> (in <<bfdlink.h>>).  The first field,
    192      1.1  christos 	<<root>>, is of type <<struct bfd_hash_entry>>.  Similarly,
    193      1.1  christos 	the first field in <<struct bfd_link_hash_table>>, <<table>>,
    194      1.1  christos 	is of type <<struct bfd_hash_table>>.
    195      1.1  christos 
    196      1.1  christos INODE
    197      1.1  christos Write the Derived Creation Routine, Write Other Derived Routines, Define the Derived Structures, Deriving a New Hash Table Type
    198      1.1  christos SUBSUBSECTION
    199      1.1  christos 	Write the derived creation routine
    200      1.1  christos 
    201      1.1  christos 	You must write a routine which will create and initialize an
    202      1.1  christos 	entry in the hash table.  This routine is passed as the
    203      1.1  christos 	function argument to <<bfd_hash_table_init>>.
    204      1.1  christos 
    205      1.1  christos 	In order to permit other hash tables to be derived from the
    206      1.1  christos 	hash table you are creating, this routine must be written in a
    207      1.1  christos 	standard way.
    208      1.1  christos 
    209      1.1  christos 	The first argument to the creation routine is a pointer to a
    210      1.1  christos 	hash table entry.  This may be <<NULL>>, in which case the
    211      1.1  christos 	routine should allocate the right amount of space.  Otherwise
    212      1.1  christos 	the space has already been allocated by a hash table type
    213      1.1  christos 	derived from this one.
    214      1.1  christos 
    215      1.1  christos 	After allocating space, the creation routine must call the
    216      1.1  christos 	creation routine of the hash table type it is derived from,
    217      1.1  christos 	passing in a pointer to the space it just allocated.  This
    218      1.1  christos 	will initialize any fields used by the base hash table.
    219      1.1  christos 
    220      1.1  christos 	Finally the creation routine must initialize any local fields
    221      1.1  christos 	for the new hash table type.
    222      1.1  christos 
    223      1.1  christos 	Here is a boilerplate example of a creation routine.
    224      1.1  christos 	@var{function_name} is the name of the routine.
    225      1.1  christos 	@var{entry_type} is the type of an entry in the hash table you
    226      1.1  christos 	are creating.  @var{base_newfunc} is the name of the creation
    227      1.1  christos 	routine of the hash table type your hash table is derived
    228      1.1  christos 	from.
    229      1.1  christos 
    230      1.1  christos EXAMPLE
    231      1.1  christos 
    232      1.1  christos .struct bfd_hash_entry *
    233      1.1  christos .@var{function_name} (struct bfd_hash_entry *entry,
    234      1.1  christos .                     struct bfd_hash_table *table,
    235      1.1  christos .                     const char *string)
    236      1.1  christos .{
    237      1.1  christos .  struct @var{entry_type} *ret = (@var{entry_type} *) entry;
    238      1.1  christos .
    239      1.1  christos . {* Allocate the structure if it has not already been allocated by a
    240      1.1  christos .    derived class.  *}
    241      1.1  christos .  if (ret == NULL)
    242      1.1  christos .    {
    243      1.1  christos .      ret = bfd_hash_allocate (table, sizeof (* ret));
    244      1.1  christos .      if (ret == NULL)
    245      1.1  christos .        return NULL;
    246      1.1  christos .    }
    247      1.1  christos .
    248      1.1  christos . {* Call the allocation method of the base class.  *}
    249      1.1  christos .  ret = ((@var{entry_type} *)
    250      1.1  christos .	 @var{base_newfunc} ((struct bfd_hash_entry *) ret, table, string));
    251      1.1  christos .
    252      1.1  christos . {* Initialize the local fields here.  *}
    253      1.1  christos .
    254      1.1  christos .  return (struct bfd_hash_entry *) ret;
    255      1.1  christos .}
    256      1.1  christos 
    257      1.1  christos DESCRIPTION
    258      1.1  christos 	The creation routine for the linker hash table, which is in
    259      1.1  christos 	<<linker.c>>, looks just like this example.
    260      1.1  christos 	@var{function_name} is <<_bfd_link_hash_newfunc>>.
    261      1.1  christos 	@var{entry_type} is <<struct bfd_link_hash_entry>>.
    262      1.1  christos 	@var{base_newfunc} is <<bfd_hash_newfunc>>, the creation
    263      1.1  christos 	routine for a basic hash table.
    264      1.1  christos 
    265      1.1  christos 	<<_bfd_link_hash_newfunc>> also initializes the local fields
    266      1.1  christos 	in a linker hash table entry: <<type>>, <<written>> and
    267      1.1  christos 	<<next>>.
    268      1.1  christos 
    269      1.1  christos INODE
    270      1.1  christos Write Other Derived Routines, , Write the Derived Creation Routine, Deriving a New Hash Table Type
    271      1.1  christos SUBSUBSECTION
    272      1.1  christos 	Write other derived routines
    273      1.1  christos 
    274      1.1  christos 	You will want to write other routines for your new hash table,
    275      1.1  christos 	as well.
    276      1.1  christos 
    277      1.1  christos 	You will want an initialization routine which calls the
    278      1.1  christos 	initialization routine of the hash table you are deriving from
    279      1.1  christos 	and initializes any other local fields.  For the linker hash
    280      1.1  christos 	table, this is <<_bfd_link_hash_table_init>> in <<linker.c>>.
    281      1.1  christos 
    282      1.1  christos 	You will want a lookup routine which calls the lookup routine
    283      1.1  christos 	of the hash table you are deriving from and casts the result.
    284      1.1  christos 	The linker hash table uses <<bfd_link_hash_lookup>> in
    285      1.1  christos 	<<linker.c>> (this actually takes an additional argument which
    286      1.1  christos 	it uses to decide how to return the looked up value).
    287      1.1  christos 
    288      1.1  christos 	You may want a traversal routine.  This should just call the
    289      1.1  christos 	traversal routine of the hash table you are deriving from with
    290      1.1  christos 	appropriate casts.  The linker hash table uses
    291      1.1  christos 	<<bfd_link_hash_traverse>> in <<linker.c>>.
    292      1.1  christos 
    293      1.1  christos 	These routines may simply be defined as macros.  For example,
    294      1.1  christos 	the a.out backend linker hash table, which is derived from the
    295      1.1  christos 	linker hash table, uses macros for the lookup and traversal
    296      1.1  christos 	routines.  These are <<aout_link_hash_lookup>> and
    297      1.1  christos 	<<aout_link_hash_traverse>> in aoutx.h.
    298      1.1  christos */
    299      1.1  christos 
    300      1.1  christos /* The default number of entries to use when creating a hash table.  */
    301      1.1  christos #define DEFAULT_SIZE 4051
    302      1.1  christos 
    303      1.1  christos /* The following function returns a nearest prime number which is
    304      1.1  christos    greater than N, and near a power of two.  Copied from libiberty.
    305      1.1  christos    Returns zero for ridiculously large N to signify an error.  */
    306      1.1  christos 
    307      1.1  christos static unsigned long
    308      1.1  christos higher_prime_number (unsigned long n)
    309      1.1  christos {
    310      1.1  christos   /* These are primes that are near, but slightly smaller than, a
    311      1.1  christos      power of two.  */
    312      1.1  christos   static const unsigned long primes[] =
    313      1.1  christos     {
    314      1.1  christos       (unsigned long) 31,
    315      1.1  christos       (unsigned long) 61,
    316      1.1  christos       (unsigned long) 127,
    317      1.1  christos       (unsigned long) 251,
    318      1.1  christos       (unsigned long) 509,
    319      1.1  christos       (unsigned long) 1021,
    320      1.1  christos       (unsigned long) 2039,
    321      1.1  christos       (unsigned long) 4093,
    322      1.1  christos       (unsigned long) 8191,
    323      1.1  christos       (unsigned long) 16381,
    324      1.1  christos       (unsigned long) 32749,
    325      1.1  christos       (unsigned long) 65521,
    326      1.1  christos       (unsigned long) 131071,
    327      1.1  christos       (unsigned long) 262139,
    328      1.1  christos       (unsigned long) 524287,
    329      1.1  christos       (unsigned long) 1048573,
    330      1.1  christos       (unsigned long) 2097143,
    331      1.1  christos       (unsigned long) 4194301,
    332      1.1  christos       (unsigned long) 8388593,
    333      1.1  christos       (unsigned long) 16777213,
    334      1.1  christos       (unsigned long) 33554393,
    335      1.1  christos       (unsigned long) 67108859,
    336      1.1  christos       (unsigned long) 134217689,
    337      1.1  christos       (unsigned long) 268435399,
    338      1.1  christos       (unsigned long) 536870909,
    339      1.1  christos       (unsigned long) 1073741789,
    340      1.1  christos       (unsigned long) 2147483647,
    341      1.1  christos 					/* 4294967291L */
    342      1.1  christos       ((unsigned long) 2147483647) + ((unsigned long) 2147483644),
    343      1.1  christos   };
    344      1.1  christos 
    345      1.1  christos   const unsigned long *low = &primes[0];
    346      1.1  christos   const unsigned long *high = &primes[sizeof (primes) / sizeof (primes[0])];
    347      1.1  christos 
    348      1.1  christos   while (low != high)
    349      1.1  christos     {
    350      1.1  christos       const unsigned long *mid = low + (high - low) / 2;
    351      1.1  christos       if (n >= *mid)
    352      1.1  christos 	low = mid + 1;
    353      1.1  christos       else
    354      1.1  christos 	high = mid;
    355      1.1  christos     }
    356      1.1  christos 
    357      1.1  christos   if (n >= *low)
    358      1.1  christos     return 0;
    359      1.1  christos 
    360      1.1  christos   return *low;
    361      1.1  christos }
    362      1.1  christos 
    363      1.1  christos static unsigned long bfd_default_hash_table_size = DEFAULT_SIZE;
    364      1.1  christos 
    365      1.1  christos /* Create a new hash table, given a number of entries.  */
    366      1.1  christos 
    367      1.1  christos bfd_boolean
    368      1.1  christos bfd_hash_table_init_n (struct bfd_hash_table *table,
    369      1.1  christos 		       struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
    370      1.1  christos 							  struct bfd_hash_table *,
    371      1.1  christos 							  const char *),
    372      1.1  christos 		       unsigned int entsize,
    373      1.1  christos 		       unsigned int size)
    374      1.1  christos {
    375      1.1  christos   unsigned long alloc;
    376      1.1  christos 
    377      1.1  christos   alloc = size;
    378      1.1  christos   alloc *= sizeof (struct bfd_hash_entry *);
    379      1.1  christos   if (alloc / sizeof (struct bfd_hash_entry *) != size)
    380      1.1  christos     {
    381      1.1  christos       bfd_set_error (bfd_error_no_memory);
    382      1.1  christos       return FALSE;
    383      1.1  christos     }
    384      1.1  christos 
    385      1.1  christos   table->memory = (void *) objalloc_create ();
    386      1.1  christos   if (table->memory == NULL)
    387      1.1  christos     {
    388      1.1  christos       bfd_set_error (bfd_error_no_memory);
    389      1.1  christos       return FALSE;
    390      1.1  christos     }
    391      1.1  christos   table->table = (struct bfd_hash_entry **)
    392      1.1  christos       objalloc_alloc ((struct objalloc *) table->memory, alloc);
    393      1.1  christos   if (table->table == NULL)
    394      1.1  christos     {
    395  1.1.1.2  christos       bfd_hash_table_free (table);
    396      1.1  christos       bfd_set_error (bfd_error_no_memory);
    397      1.1  christos       return FALSE;
    398      1.1  christos     }
    399      1.1  christos   memset ((void *) table->table, 0, alloc);
    400      1.1  christos   table->size = size;
    401      1.1  christos   table->entsize = entsize;
    402      1.1  christos   table->count = 0;
    403      1.1  christos   table->frozen = 0;
    404      1.1  christos   table->newfunc = newfunc;
    405      1.1  christos   return TRUE;
    406      1.1  christos }
    407      1.1  christos 
    408      1.1  christos /* Create a new hash table with the default number of entries.  */
    409      1.1  christos 
    410      1.1  christos bfd_boolean
    411      1.1  christos bfd_hash_table_init (struct bfd_hash_table *table,
    412      1.1  christos 		     struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
    413      1.1  christos 							struct bfd_hash_table *,
    414      1.1  christos 							const char *),
    415      1.1  christos 		     unsigned int entsize)
    416      1.1  christos {
    417      1.1  christos   return bfd_hash_table_init_n (table, newfunc, entsize,
    418      1.1  christos 				bfd_default_hash_table_size);
    419      1.1  christos }
    420      1.1  christos 
    421      1.1  christos /* Free a hash table.  */
    422      1.1  christos 
    423      1.1  christos void
    424      1.1  christos bfd_hash_table_free (struct bfd_hash_table *table)
    425      1.1  christos {
    426      1.1  christos   objalloc_free ((struct objalloc *) table->memory);
    427      1.1  christos   table->memory = NULL;
    428      1.1  christos }
    429      1.1  christos 
    430      1.1  christos static inline unsigned long
    431      1.1  christos bfd_hash_hash (const char *string, unsigned int *lenp)
    432      1.1  christos {
    433      1.1  christos   const unsigned char *s;
    434      1.1  christos   unsigned long hash;
    435      1.1  christos   unsigned int len;
    436      1.1  christos   unsigned int c;
    437      1.1  christos 
    438      1.1  christos   hash = 0;
    439      1.1  christos   len = 0;
    440      1.1  christos   s = (const unsigned char *) string;
    441      1.1  christos   while ((c = *s++) != '\0')
    442      1.1  christos     {
    443      1.1  christos       hash += c + (c << 17);
    444      1.1  christos       hash ^= hash >> 2;
    445      1.1  christos     }
    446      1.1  christos   len = (s - (const unsigned char *) string) - 1;
    447      1.1  christos   hash += len + (len << 17);
    448      1.1  christos   hash ^= hash >> 2;
    449      1.1  christos   if (lenp != NULL)
    450      1.1  christos     *lenp = len;
    451      1.1  christos   return hash;
    452      1.1  christos }
    453      1.1  christos 
    454      1.1  christos /* Look up a string in a hash table.  */
    455      1.1  christos 
    456      1.1  christos struct bfd_hash_entry *
    457      1.1  christos bfd_hash_lookup (struct bfd_hash_table *table,
    458      1.1  christos 		 const char *string,
    459      1.1  christos 		 bfd_boolean create,
    460      1.1  christos 		 bfd_boolean copy)
    461      1.1  christos {
    462      1.1  christos   unsigned long hash;
    463      1.1  christos   struct bfd_hash_entry *hashp;
    464      1.1  christos   unsigned int len;
    465      1.1  christos   unsigned int _index;
    466      1.1  christos 
    467      1.1  christos   hash = bfd_hash_hash (string, &len);
    468      1.1  christos   _index = hash % table->size;
    469      1.1  christos   for (hashp = table->table[_index];
    470      1.1  christos        hashp != NULL;
    471      1.1  christos        hashp = hashp->next)
    472      1.1  christos     {
    473      1.1  christos       if (hashp->hash == hash
    474      1.1  christos 	  && strcmp (hashp->string, string) == 0)
    475      1.1  christos 	return hashp;
    476      1.1  christos     }
    477      1.1  christos 
    478      1.1  christos   if (! create)
    479      1.1  christos     return NULL;
    480      1.1  christos 
    481      1.1  christos   if (copy)
    482      1.1  christos     {
    483      1.1  christos       char *new_string;
    484      1.1  christos 
    485      1.1  christos       new_string = (char *) objalloc_alloc ((struct objalloc *) table->memory,
    486      1.1  christos                                             len + 1);
    487      1.1  christos       if (!new_string)
    488      1.1  christos 	{
    489      1.1  christos 	  bfd_set_error (bfd_error_no_memory);
    490      1.1  christos 	  return NULL;
    491      1.1  christos 	}
    492      1.1  christos       memcpy (new_string, string, len + 1);
    493      1.1  christos       string = new_string;
    494      1.1  christos     }
    495      1.1  christos 
    496      1.1  christos   return bfd_hash_insert (table, string, hash);
    497      1.1  christos }
    498      1.1  christos 
    499      1.1  christos /* Insert an entry in a hash table.  */
    500      1.1  christos 
    501      1.1  christos struct bfd_hash_entry *
    502      1.1  christos bfd_hash_insert (struct bfd_hash_table *table,
    503      1.1  christos 		 const char *string,
    504      1.1  christos 		 unsigned long hash)
    505      1.1  christos {
    506      1.1  christos   struct bfd_hash_entry *hashp;
    507      1.1  christos   unsigned int _index;
    508      1.1  christos 
    509      1.1  christos   hashp = (*table->newfunc) (NULL, table, string);
    510      1.1  christos   if (hashp == NULL)
    511      1.1  christos     return NULL;
    512      1.1  christos   hashp->string = string;
    513      1.1  christos   hashp->hash = hash;
    514      1.1  christos   _index = hash % table->size;
    515      1.1  christos   hashp->next = table->table[_index];
    516      1.1  christos   table->table[_index] = hashp;
    517      1.1  christos   table->count++;
    518      1.1  christos 
    519      1.1  christos   if (!table->frozen && table->count > table->size * 3 / 4)
    520      1.1  christos     {
    521      1.1  christos       unsigned long newsize = higher_prime_number (table->size);
    522      1.1  christos       struct bfd_hash_entry **newtable;
    523      1.1  christos       unsigned int hi;
    524      1.1  christos       unsigned long alloc = newsize * sizeof (struct bfd_hash_entry *);
    525      1.1  christos 
    526      1.1  christos       /* If we can't find a higher prime, or we can't possibly alloc
    527      1.1  christos 	 that much memory, don't try to grow the table.  */
    528      1.1  christos       if (newsize == 0 || alloc / sizeof (struct bfd_hash_entry *) != newsize)
    529      1.1  christos 	{
    530      1.1  christos 	  table->frozen = 1;
    531      1.1  christos 	  return hashp;
    532      1.1  christos 	}
    533      1.1  christos 
    534      1.1  christos       newtable = ((struct bfd_hash_entry **)
    535      1.1  christos 		  objalloc_alloc ((struct objalloc *) table->memory, alloc));
    536      1.1  christos       if (newtable == NULL)
    537      1.1  christos 	{
    538      1.1  christos 	  table->frozen = 1;
    539      1.1  christos 	  return hashp;
    540      1.1  christos 	}
    541      1.1  christos       memset (newtable, 0, alloc);
    542      1.1  christos 
    543      1.1  christos       for (hi = 0; hi < table->size; hi ++)
    544      1.1  christos 	while (table->table[hi])
    545      1.1  christos 	  {
    546      1.1  christos 	    struct bfd_hash_entry *chain = table->table[hi];
    547      1.1  christos 	    struct bfd_hash_entry *chain_end = chain;
    548      1.1  christos 
    549      1.1  christos 	    while (chain_end->next && chain_end->next->hash == chain->hash)
    550      1.1  christos 	      chain_end = chain_end->next;
    551      1.1  christos 
    552      1.1  christos 	    table->table[hi] = chain_end->next;
    553      1.1  christos 	    _index = chain->hash % newsize;
    554      1.1  christos 	    chain_end->next = newtable[_index];
    555      1.1  christos 	    newtable[_index] = chain;
    556      1.1  christos 	  }
    557      1.1  christos       table->table = newtable;
    558      1.1  christos       table->size = newsize;
    559      1.1  christos     }
    560      1.1  christos 
    561      1.1  christos   return hashp;
    562      1.1  christos }
    563      1.1  christos 
    564      1.1  christos /* Rename an entry in a hash table.  */
    565      1.1  christos 
    566      1.1  christos void
    567      1.1  christos bfd_hash_rename (struct bfd_hash_table *table,
    568      1.1  christos 		 const char *string,
    569      1.1  christos 		 struct bfd_hash_entry *ent)
    570      1.1  christos {
    571      1.1  christos   unsigned int _index;
    572      1.1  christos   struct bfd_hash_entry **pph;
    573      1.1  christos 
    574      1.1  christos   _index = ent->hash % table->size;
    575      1.1  christos   for (pph = &table->table[_index]; *pph != NULL; pph = &(*pph)->next)
    576      1.1  christos     if (*pph == ent)
    577      1.1  christos       break;
    578      1.1  christos   if (*pph == NULL)
    579      1.1  christos     abort ();
    580      1.1  christos 
    581      1.1  christos   *pph = ent->next;
    582      1.1  christos   ent->string = string;
    583      1.1  christos   ent->hash = bfd_hash_hash (string, NULL);
    584      1.1  christos   _index = ent->hash % table->size;
    585      1.1  christos   ent->next = table->table[_index];
    586      1.1  christos   table->table[_index] = ent;
    587      1.1  christos }
    588      1.1  christos 
    589      1.1  christos /* Replace an entry in a hash table.  */
    590      1.1  christos 
    591      1.1  christos void
    592      1.1  christos bfd_hash_replace (struct bfd_hash_table *table,
    593      1.1  christos 		  struct bfd_hash_entry *old,
    594      1.1  christos 		  struct bfd_hash_entry *nw)
    595      1.1  christos {
    596      1.1  christos   unsigned int _index;
    597      1.1  christos   struct bfd_hash_entry **pph;
    598      1.1  christos 
    599      1.1  christos   _index = old->hash % table->size;
    600      1.1  christos   for (pph = &table->table[_index];
    601      1.1  christos        (*pph) != NULL;
    602      1.1  christos        pph = &(*pph)->next)
    603      1.1  christos     {
    604      1.1  christos       if (*pph == old)
    605      1.1  christos 	{
    606      1.1  christos 	  *pph = nw;
    607      1.1  christos 	  return;
    608      1.1  christos 	}
    609      1.1  christos     }
    610      1.1  christos 
    611      1.1  christos   abort ();
    612      1.1  christos }
    613      1.1  christos 
    614      1.1  christos /* Allocate space in a hash table.  */
    615      1.1  christos 
    616      1.1  christos void *
    617      1.1  christos bfd_hash_allocate (struct bfd_hash_table *table,
    618      1.1  christos 		   unsigned int size)
    619      1.1  christos {
    620      1.1  christos   void * ret;
    621      1.1  christos 
    622      1.1  christos   ret = objalloc_alloc ((struct objalloc *) table->memory, size);
    623      1.1  christos   if (ret == NULL && size != 0)
    624      1.1  christos     bfd_set_error (bfd_error_no_memory);
    625      1.1  christos   return ret;
    626      1.1  christos }
    627      1.1  christos 
    628      1.1  christos /* Base method for creating a new hash table entry.  */
    629      1.1  christos 
    630      1.1  christos struct bfd_hash_entry *
    631      1.1  christos bfd_hash_newfunc (struct bfd_hash_entry *entry,
    632      1.1  christos 		  struct bfd_hash_table *table,
    633      1.1  christos 		  const char *string ATTRIBUTE_UNUSED)
    634      1.1  christos {
    635      1.1  christos   if (entry == NULL)
    636      1.1  christos     entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
    637      1.1  christos                                                          sizeof (* entry));
    638      1.1  christos   return entry;
    639      1.1  christos }
    640      1.1  christos 
    641      1.1  christos /* Traverse a hash table.  */
    642      1.1  christos 
    643      1.1  christos void
    644      1.1  christos bfd_hash_traverse (struct bfd_hash_table *table,
    645      1.1  christos 		   bfd_boolean (*func) (struct bfd_hash_entry *, void *),
    646      1.1  christos 		   void * info)
    647      1.1  christos {
    648      1.1  christos   unsigned int i;
    649      1.1  christos 
    650      1.1  christos   table->frozen = 1;
    651      1.1  christos   for (i = 0; i < table->size; i++)
    652      1.1  christos     {
    653      1.1  christos       struct bfd_hash_entry *p;
    654      1.1  christos 
    655      1.1  christos       for (p = table->table[i]; p != NULL; p = p->next)
    656      1.1  christos 	if (! (*func) (p, info))
    657      1.1  christos 	  goto out;
    658      1.1  christos     }
    659      1.1  christos  out:
    660      1.1  christos   table->frozen = 0;
    661      1.1  christos }
    662      1.1  christos 
    663      1.1  christos unsigned long
    665      1.1  christos bfd_hash_set_default_size (unsigned long hash_size)
    666      1.1  christos {
    667      1.1  christos   /* Extend this prime list if you want more granularity of hash table size.  */
    668      1.1  christos   static const unsigned long hash_size_primes[] =
    669      1.1  christos     {
    670      1.1  christos       31, 61, 127, 251, 509, 1021, 2039, 4091, 8191, 16381, 32749, 65537
    671      1.1  christos     };
    672      1.1  christos   unsigned int _index;
    673      1.1  christos 
    674      1.1  christos   /* Work out best prime number near the hash_size.  */
    675      1.1  christos   for (_index = 0; _index < ARRAY_SIZE (hash_size_primes) - 1; ++_index)
    676      1.1  christos     if (hash_size <= hash_size_primes[_index])
    677      1.1  christos       break;
    678      1.1  christos 
    679      1.1  christos   bfd_default_hash_table_size = hash_size_primes[_index];
    680      1.1  christos   return bfd_default_hash_table_size;
    681      1.1  christos }
    682      1.1  christos 
    683      1.1  christos /* A few different object file formats (a.out, COFF, ELF) use a string
    685      1.1  christos    table.  These functions support adding strings to a string table,
    686      1.1  christos    returning the byte offset, and writing out the table.
    687      1.1  christos 
    688      1.1  christos    Possible improvements:
    689      1.1  christos    + look for strings matching trailing substrings of other strings
    690      1.1  christos    + better data structures?  balanced trees?
    691      1.1  christos    + look at reducing memory use elsewhere -- maybe if we didn't have
    692      1.1  christos      to construct the entire symbol table at once, we could get by
    693      1.1  christos      with smaller amounts of VM?  (What effect does that have on the
    694      1.1  christos      string table reductions?)  */
    695      1.1  christos 
    696      1.1  christos /* An entry in the strtab hash table.  */
    697      1.1  christos 
    698      1.1  christos struct strtab_hash_entry
    699      1.1  christos {
    700      1.1  christos   struct bfd_hash_entry root;
    701      1.1  christos   /* Index in string table.  */
    702      1.1  christos   bfd_size_type index;
    703      1.1  christos   /* Next string in strtab.  */
    704      1.1  christos   struct strtab_hash_entry *next;
    705      1.1  christos };
    706      1.1  christos 
    707      1.1  christos /* The strtab hash table.  */
    708      1.1  christos 
    709      1.1  christos struct bfd_strtab_hash
    710      1.1  christos {
    711      1.1  christos   struct bfd_hash_table table;
    712      1.1  christos   /* Size of strtab--also next available index.  */
    713      1.1  christos   bfd_size_type size;
    714      1.1  christos   /* First string in strtab.  */
    715      1.1  christos   struct strtab_hash_entry *first;
    716      1.1  christos   /* Last string in strtab.  */
    717      1.1  christos   struct strtab_hash_entry *last;
    718      1.1  christos   /* Whether to precede strings with a two byte length, as in the
    719      1.1  christos      XCOFF .debug section.  */
    720      1.1  christos   bfd_boolean xcoff;
    721      1.1  christos };
    722      1.1  christos 
    723      1.1  christos /* Routine to create an entry in a strtab.  */
    724      1.1  christos 
    725      1.1  christos static struct bfd_hash_entry *
    726      1.1  christos strtab_hash_newfunc (struct bfd_hash_entry *entry,
    727      1.1  christos 		     struct bfd_hash_table *table,
    728      1.1  christos 		     const char *string)
    729      1.1  christos {
    730      1.1  christos   struct strtab_hash_entry *ret = (struct strtab_hash_entry *) entry;
    731      1.1  christos 
    732      1.1  christos   /* Allocate the structure if it has not already been allocated by a
    733      1.1  christos      subclass.  */
    734      1.1  christos   if (ret == NULL)
    735      1.1  christos     ret = (struct strtab_hash_entry *) bfd_hash_allocate (table,
    736      1.1  christos                                                           sizeof (* ret));
    737      1.1  christos   if (ret == NULL)
    738      1.1  christos     return NULL;
    739      1.1  christos 
    740      1.1  christos   /* Call the allocation method of the superclass.  */
    741      1.1  christos   ret = (struct strtab_hash_entry *)
    742      1.1  christos 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
    743      1.1  christos 
    744      1.1  christos   if (ret)
    745      1.1  christos     {
    746      1.1  christos       /* Initialize the local fields.  */
    747      1.1  christos       ret->index = (bfd_size_type) -1;
    748      1.1  christos       ret->next = NULL;
    749      1.1  christos     }
    750      1.1  christos 
    751      1.1  christos   return (struct bfd_hash_entry *) ret;
    752      1.1  christos }
    753      1.1  christos 
    754      1.1  christos /* Look up an entry in an strtab.  */
    755      1.1  christos 
    756      1.1  christos #define strtab_hash_lookup(t, string, create, copy) \
    757      1.1  christos   ((struct strtab_hash_entry *) \
    758      1.1  christos    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
    759      1.1  christos 
    760      1.1  christos /* Create a new strtab.  */
    761      1.1  christos 
    762      1.1  christos struct bfd_strtab_hash *
    763      1.1  christos _bfd_stringtab_init (void)
    764      1.1  christos {
    765      1.1  christos   struct bfd_strtab_hash *table;
    766      1.1  christos   bfd_size_type amt = sizeof (* table);
    767      1.1  christos 
    768      1.1  christos   table = (struct bfd_strtab_hash *) bfd_malloc (amt);
    769      1.1  christos   if (table == NULL)
    770      1.1  christos     return NULL;
    771      1.1  christos 
    772      1.1  christos   if (!bfd_hash_table_init (&table->table, strtab_hash_newfunc,
    773      1.1  christos 			    sizeof (struct strtab_hash_entry)))
    774      1.1  christos     {
    775      1.1  christos       free (table);
    776      1.1  christos       return NULL;
    777      1.1  christos     }
    778      1.1  christos 
    779      1.1  christos   table->size = 0;
    780      1.1  christos   table->first = NULL;
    781      1.1  christos   table->last = NULL;
    782      1.1  christos   table->xcoff = FALSE;
    783      1.1  christos 
    784      1.1  christos   return table;
    785      1.1  christos }
    786      1.1  christos 
    787      1.1  christos /* Create a new strtab in which the strings are output in the format
    788      1.1  christos    used in the XCOFF .debug section: a two byte length precedes each
    789      1.1  christos    string.  */
    790      1.1  christos 
    791      1.1  christos struct bfd_strtab_hash *
    792      1.1  christos _bfd_xcoff_stringtab_init (void)
    793      1.1  christos {
    794      1.1  christos   struct bfd_strtab_hash *ret;
    795      1.1  christos 
    796      1.1  christos   ret = _bfd_stringtab_init ();
    797      1.1  christos   if (ret != NULL)
    798      1.1  christos     ret->xcoff = TRUE;
    799      1.1  christos   return ret;
    800      1.1  christos }
    801      1.1  christos 
    802      1.1  christos /* Free a strtab.  */
    803      1.1  christos 
    804      1.1  christos void
    805      1.1  christos _bfd_stringtab_free (struct bfd_strtab_hash *table)
    806      1.1  christos {
    807      1.1  christos   bfd_hash_table_free (&table->table);
    808      1.1  christos   free (table);
    809      1.1  christos }
    810      1.1  christos 
    811      1.1  christos /* Get the index of a string in a strtab, adding it if it is not
    812      1.1  christos    already present.  If HASH is FALSE, we don't really use the hash
    813      1.1  christos    table, and we don't eliminate duplicate strings.  If COPY is true
    814      1.1  christos    then store a copy of STR if creating a new entry.  */
    815      1.1  christos 
    816      1.1  christos bfd_size_type
    817      1.1  christos _bfd_stringtab_add (struct bfd_strtab_hash *tab,
    818      1.1  christos 		    const char *str,
    819      1.1  christos 		    bfd_boolean hash,
    820      1.1  christos 		    bfd_boolean copy)
    821      1.1  christos {
    822      1.1  christos   struct strtab_hash_entry *entry;
    823      1.1  christos 
    824      1.1  christos   if (hash)
    825      1.1  christos     {
    826      1.1  christos       entry = strtab_hash_lookup (tab, str, TRUE, copy);
    827      1.1  christos       if (entry == NULL)
    828      1.1  christos 	return (bfd_size_type) -1;
    829      1.1  christos     }
    830      1.1  christos   else
    831      1.1  christos     {
    832      1.1  christos       entry = (struct strtab_hash_entry *) bfd_hash_allocate (&tab->table,
    833      1.1  christos                                                               sizeof (* entry));
    834      1.1  christos       if (entry == NULL)
    835      1.1  christos 	return (bfd_size_type) -1;
    836      1.1  christos       if (! copy)
    837      1.1  christos 	entry->root.string = str;
    838      1.1  christos       else
    839      1.1  christos 	{
    840      1.1  christos 	  size_t len = strlen (str) + 1;
    841      1.1  christos 	  char *n;
    842      1.1  christos 
    843      1.1  christos 	  n = (char *) bfd_hash_allocate (&tab->table, len);
    844      1.1  christos 	  if (n == NULL)
    845      1.1  christos 	    return (bfd_size_type) -1;
    846      1.1  christos           memcpy (n, str, len);
    847      1.1  christos 	  entry->root.string = n;
    848      1.1  christos 	}
    849      1.1  christos       entry->index = (bfd_size_type) -1;
    850      1.1  christos       entry->next = NULL;
    851      1.1  christos     }
    852      1.1  christos 
    853      1.1  christos   if (entry->index == (bfd_size_type) -1)
    854      1.1  christos     {
    855      1.1  christos       entry->index = tab->size;
    856      1.1  christos       tab->size += strlen (str) + 1;
    857      1.1  christos       if (tab->xcoff)
    858      1.1  christos 	{
    859      1.1  christos 	  entry->index += 2;
    860      1.1  christos 	  tab->size += 2;
    861      1.1  christos 	}
    862      1.1  christos       if (tab->first == NULL)
    863      1.1  christos 	tab->first = entry;
    864      1.1  christos       else
    865      1.1  christos 	tab->last->next = entry;
    866      1.1  christos       tab->last = entry;
    867      1.1  christos     }
    868      1.1  christos 
    869      1.1  christos   return entry->index;
    870      1.1  christos }
    871      1.1  christos 
    872      1.1  christos /* Get the number of bytes in a strtab.  */
    873      1.1  christos 
    874      1.1  christos bfd_size_type
    875      1.1  christos _bfd_stringtab_size (struct bfd_strtab_hash *tab)
    876      1.1  christos {
    877      1.1  christos   return tab->size;
    878      1.1  christos }
    879      1.1  christos 
    880      1.1  christos /* Write out a strtab.  ABFD must already be at the right location in
    881      1.1  christos    the file.  */
    882      1.1  christos 
    883      1.1  christos bfd_boolean
    884      1.1  christos _bfd_stringtab_emit (bfd *abfd, struct bfd_strtab_hash *tab)
    885      1.1  christos {
    886      1.1  christos   bfd_boolean xcoff;
    887      1.1  christos   struct strtab_hash_entry *entry;
    888      1.1  christos 
    889      1.1  christos   xcoff = tab->xcoff;
    890      1.1  christos 
    891      1.1  christos   for (entry = tab->first; entry != NULL; entry = entry->next)
    892      1.1  christos     {
    893      1.1  christos       const char *str;
    894      1.1  christos       size_t len;
    895      1.1  christos 
    896      1.1  christos       str = entry->root.string;
    897      1.1  christos       len = strlen (str) + 1;
    898      1.1  christos 
    899      1.1  christos       if (xcoff)
    900      1.1  christos 	{
    901      1.1  christos 	  bfd_byte buf[2];
    902      1.1  christos 
    903      1.1  christos 	  /* The output length includes the null byte.  */
    904      1.1  christos 	  bfd_put_16 (abfd, (bfd_vma) len, buf);
    905      1.1  christos 	  if (bfd_bwrite ((void *) buf, (bfd_size_type) 2, abfd) != 2)
    906      1.1  christos 	    return FALSE;
    907      1.1  christos 	}
    908      1.1  christos 
    909      1.1  christos       if (bfd_bwrite ((void *) str, (bfd_size_type) len, abfd) != len)
    910      1.1  christos 	return FALSE;
    911      1.1  christos     }
    912      1.1  christos 
    913                      return TRUE;
    914                    }
    915