xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/doc/hash.texi (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod@section Hash Tables
2*3d8817e4Smiod@cindex Hash tables
3*3d8817e4SmiodBFD provides a simple set of hash table functions.  Routines
4*3d8817e4Smiodare provided to initialize a hash table, to free a hash table,
5*3d8817e4Smiodto look up a string in a hash table and optionally create an
6*3d8817e4Smiodentry for it, and to traverse a hash table.  There is
7*3d8817e4Smiodcurrently no routine to delete an string from a hash table.
8*3d8817e4Smiod
9*3d8817e4SmiodThe basic hash table does not permit any data to be stored
10*3d8817e4Smiodwith a string.  However, a hash table is designed to present a
11*3d8817e4Smiodbase class from which other types of hash tables may be
12*3d8817e4Smiodderived.  These derived types may store additional information
13*3d8817e4Smiodwith the string.  Hash tables were implemented in this way,
14*3d8817e4Smiodrather than simply providing a data pointer in a hash table
15*3d8817e4Smiodentry, because they were designed for use by the linker back
16*3d8817e4Smiodends.  The linker may create thousands of hash table entries,
17*3d8817e4Smiodand the overhead of allocating private data and storing and
18*3d8817e4Smiodfollowing pointers becomes noticeable.
19*3d8817e4Smiod
20*3d8817e4SmiodThe basic hash table code is in @code{hash.c}.
21*3d8817e4Smiod
22*3d8817e4Smiod@menu
23*3d8817e4Smiod* Creating and Freeing a Hash Table::
24*3d8817e4Smiod* Looking Up or Entering a String::
25*3d8817e4Smiod* Traversing a Hash Table::
26*3d8817e4Smiod* Deriving a New Hash Table Type::
27*3d8817e4Smiod@end menu
28*3d8817e4Smiod
29*3d8817e4Smiod@node Creating and Freeing a Hash Table, Looking Up or Entering a String, Hash Tables, Hash Tables
30*3d8817e4Smiod@subsection Creating and freeing a hash table
31*3d8817e4Smiod@findex bfd_hash_table_init
32*3d8817e4Smiod@findex bfd_hash_table_init_n
33*3d8817e4SmiodTo create a hash table, create an instance of a @code{struct
34*3d8817e4Smiodbfd_hash_table} (defined in @code{bfd.h}) and call
35*3d8817e4Smiod@code{bfd_hash_table_init} (if you know approximately how many
36*3d8817e4Smiodentries you will need, the function @code{bfd_hash_table_init_n},
37*3d8817e4Smiodwhich takes a @var{size} argument, may be used).
38*3d8817e4Smiod@code{bfd_hash_table_init} returns @code{FALSE} if some sort of
39*3d8817e4Smioderror occurs.
40*3d8817e4Smiod
41*3d8817e4Smiod@findex bfd_hash_newfunc
42*3d8817e4SmiodThe function @code{bfd_hash_table_init} take as an argument a
43*3d8817e4Smiodfunction to use to create new entries.  For a basic hash
44*3d8817e4Smiodtable, use the function @code{bfd_hash_newfunc}.  @xref{Deriving
45*3d8817e4Smioda New Hash Table Type}, for why you would want to use a
46*3d8817e4Smioddifferent value for this argument.
47*3d8817e4Smiod
48*3d8817e4Smiod@findex bfd_hash_allocate
49*3d8817e4Smiod@code{bfd_hash_table_init} will create an objalloc which will be
50*3d8817e4Smiodused to allocate new entries.  You may allocate memory on this
51*3d8817e4Smiodobjalloc using @code{bfd_hash_allocate}.
52*3d8817e4Smiod
53*3d8817e4Smiod@findex bfd_hash_table_free
54*3d8817e4SmiodUse @code{bfd_hash_table_free} to free up all the memory that has
55*3d8817e4Smiodbeen allocated for a hash table.  This will not free up the
56*3d8817e4Smiod@code{struct bfd_hash_table} itself, which you must provide.
57*3d8817e4Smiod
58*3d8817e4Smiod@findex bfd_hash_set_default_size
59*3d8817e4SmiodUse @code{bfd_hash_set_default_size} to set the default size of
60*3d8817e4Smiodhash table to use.
61*3d8817e4Smiod
62*3d8817e4Smiod@node Looking Up or Entering a String, Traversing a Hash Table, Creating and Freeing a Hash Table, Hash Tables
63*3d8817e4Smiod@subsection Looking up or entering a string
64*3d8817e4Smiod@findex bfd_hash_lookup
65*3d8817e4SmiodThe function @code{bfd_hash_lookup} is used both to look up a
66*3d8817e4Smiodstring in the hash table and to create a new entry.
67*3d8817e4Smiod
68*3d8817e4SmiodIf the @var{create} argument is @code{FALSE}, @code{bfd_hash_lookup}
69*3d8817e4Smiodwill look up a string.  If the string is found, it will
70*3d8817e4Smiodreturns a pointer to a @code{struct bfd_hash_entry}.  If the
71*3d8817e4Smiodstring is not found in the table @code{bfd_hash_lookup} will
72*3d8817e4Smiodreturn @code{NULL}.  You should not modify any of the fields in
73*3d8817e4Smiodthe returns @code{struct bfd_hash_entry}.
74*3d8817e4Smiod
75*3d8817e4SmiodIf the @var{create} argument is @code{TRUE}, the string will be
76*3d8817e4Smiodentered into the hash table if it is not already there.
77*3d8817e4SmiodEither way a pointer to a @code{struct bfd_hash_entry} will be
78*3d8817e4Smiodreturned, either to the existing structure or to a newly
79*3d8817e4Smiodcreated one.  In this case, a @code{NULL} return means that an
80*3d8817e4Smioderror occurred.
81*3d8817e4Smiod
82*3d8817e4SmiodIf the @var{create} argument is @code{TRUE}, and a new entry is
83*3d8817e4Smiodcreated, the @var{copy} argument is used to decide whether to
84*3d8817e4Smiodcopy the string onto the hash table objalloc or not.  If
85*3d8817e4Smiod@var{copy} is passed as @code{FALSE}, you must be careful not to
86*3d8817e4Smioddeallocate or modify the string as long as the hash table
87*3d8817e4Smiodexists.
88*3d8817e4Smiod
89*3d8817e4Smiod@node Traversing a Hash Table, Deriving a New Hash Table Type, Looking Up or Entering a String, Hash Tables
90*3d8817e4Smiod@subsection Traversing a hash table
91*3d8817e4Smiod@findex bfd_hash_traverse
92*3d8817e4SmiodThe function @code{bfd_hash_traverse} may be used to traverse a
93*3d8817e4Smiodhash table, calling a function on each element.  The traversal
94*3d8817e4Smiodis done in a random order.
95*3d8817e4Smiod
96*3d8817e4Smiod@code{bfd_hash_traverse} takes as arguments a function and a
97*3d8817e4Smiodgeneric @code{void *} pointer.  The function is called with a
98*3d8817e4Smiodhash table entry (a @code{struct bfd_hash_entry *}) and the
99*3d8817e4Smiodgeneric pointer passed to @code{bfd_hash_traverse}.  The function
100*3d8817e4Smiodmust return a @code{boolean} value, which indicates whether to
101*3d8817e4Smiodcontinue traversing the hash table.  If the function returns
102*3d8817e4Smiod@code{FALSE}, @code{bfd_hash_traverse} will stop the traversal and
103*3d8817e4Smiodreturn immediately.
104*3d8817e4Smiod
105*3d8817e4Smiod@node Deriving a New Hash Table Type, , Traversing a Hash Table, Hash Tables
106*3d8817e4Smiod@subsection Deriving a new hash table type
107*3d8817e4SmiodMany uses of hash tables want to store additional information
108*3d8817e4Smiodwhich each entry in the hash table.  Some also find it
109*3d8817e4Smiodconvenient to store additional information with the hash table
110*3d8817e4Smioditself.  This may be done using a derived hash table.
111*3d8817e4Smiod
112*3d8817e4SmiodSince C is not an object oriented language, creating a derived
113*3d8817e4Smiodhash table requires sticking together some boilerplate
114*3d8817e4Smiodroutines with a few differences specific to the type of hash
115*3d8817e4Smiodtable you want to create.
116*3d8817e4Smiod
117*3d8817e4SmiodAn example of a derived hash table is the linker hash table.
118*3d8817e4SmiodThe structures for this are defined in @code{bfdlink.h}.  The
119*3d8817e4Smiodfunctions are in @code{linker.c}.
120*3d8817e4Smiod
121*3d8817e4SmiodYou may also derive a hash table from an already derived hash
122*3d8817e4Smiodtable.  For example, the a.out linker backend code uses a hash
123*3d8817e4Smiodtable derived from the linker hash table.
124*3d8817e4Smiod
125*3d8817e4Smiod@menu
126*3d8817e4Smiod* Define the Derived Structures::
127*3d8817e4Smiod* Write the Derived Creation Routine::
128*3d8817e4Smiod* Write Other Derived Routines::
129*3d8817e4Smiod@end menu
130*3d8817e4Smiod
131*3d8817e4Smiod@node Define the Derived Structures, Write the Derived Creation Routine, Deriving a New Hash Table Type, Deriving a New Hash Table Type
132*3d8817e4Smiod@subsubsection Define the derived structures
133*3d8817e4SmiodYou must define a structure for an entry in the hash table,
134*3d8817e4Smiodand a structure for the hash table itself.
135*3d8817e4Smiod
136*3d8817e4SmiodThe first field in the structure for an entry in the hash
137*3d8817e4Smiodtable must be of the type used for an entry in the hash table
138*3d8817e4Smiodyou are deriving from.  If you are deriving from a basic hash
139*3d8817e4Smiodtable this is @code{struct bfd_hash_entry}, which is defined in
140*3d8817e4Smiod@code{bfd.h}.  The first field in the structure for the hash
141*3d8817e4Smiodtable itself must be of the type of the hash table you are
142*3d8817e4Smiodderiving from itself.  If you are deriving from a basic hash
143*3d8817e4Smiodtable, this is @code{struct bfd_hash_table}.
144*3d8817e4Smiod
145*3d8817e4SmiodFor example, the linker hash table defines @code{struct
146*3d8817e4Smiodbfd_link_hash_entry} (in @code{bfdlink.h}).  The first field,
147*3d8817e4Smiod@code{root}, is of type @code{struct bfd_hash_entry}.  Similarly,
148*3d8817e4Smiodthe first field in @code{struct bfd_link_hash_table}, @code{table},
149*3d8817e4Smiodis of type @code{struct bfd_hash_table}.
150*3d8817e4Smiod
151*3d8817e4Smiod@node Write the Derived Creation Routine, Write Other Derived Routines, Define the Derived Structures, Deriving a New Hash Table Type
152*3d8817e4Smiod@subsubsection Write the derived creation routine
153*3d8817e4SmiodYou must write a routine which will create and initialize an
154*3d8817e4Smiodentry in the hash table.  This routine is passed as the
155*3d8817e4Smiodfunction argument to @code{bfd_hash_table_init}.
156*3d8817e4Smiod
157*3d8817e4SmiodIn order to permit other hash tables to be derived from the
158*3d8817e4Smiodhash table you are creating, this routine must be written in a
159*3d8817e4Smiodstandard way.
160*3d8817e4Smiod
161*3d8817e4SmiodThe first argument to the creation routine is a pointer to a
162*3d8817e4Smiodhash table entry.  This may be @code{NULL}, in which case the
163*3d8817e4Smiodroutine should allocate the right amount of space.  Otherwise
164*3d8817e4Smiodthe space has already been allocated by a hash table type
165*3d8817e4Smiodderived from this one.
166*3d8817e4Smiod
167*3d8817e4SmiodAfter allocating space, the creation routine must call the
168*3d8817e4Smiodcreation routine of the hash table type it is derived from,
169*3d8817e4Smiodpassing in a pointer to the space it just allocated.  This
170*3d8817e4Smiodwill initialize any fields used by the base hash table.
171*3d8817e4Smiod
172*3d8817e4SmiodFinally the creation routine must initialize any local fields
173*3d8817e4Smiodfor the new hash table type.
174*3d8817e4Smiod
175*3d8817e4SmiodHere is a boilerplate example of a creation routine.
176*3d8817e4Smiod@var{function_name} is the name of the routine.
177*3d8817e4Smiod@var{entry_type} is the type of an entry in the hash table you
178*3d8817e4Smiodare creating.  @var{base_newfunc} is the name of the creation
179*3d8817e4Smiodroutine of the hash table type your hash table is derived
180*3d8817e4Smiodfrom.
181*3d8817e4Smiod
182*3d8817e4Smiod
183*3d8817e4Smiod@example
184*3d8817e4Smiodstruct bfd_hash_entry *
185*3d8817e4Smiod@var{function_name} (struct bfd_hash_entry *entry,
186*3d8817e4Smiod                     struct bfd_hash_table *table,
187*3d8817e4Smiod                     const char *string)
188*3d8817e4Smiod@{
189*3d8817e4Smiod  struct @var{entry_type} *ret = (@var{entry_type} *) entry;
190*3d8817e4Smiod
191*3d8817e4Smiod /* Allocate the structure if it has not already been allocated by a
192*3d8817e4Smiod    derived class.  */
193*3d8817e4Smiod  if (ret == NULL)
194*3d8817e4Smiod    @{
195*3d8817e4Smiod      ret = bfd_hash_allocate (table, sizeof (* ret));
196*3d8817e4Smiod      if (ret == NULL)
197*3d8817e4Smiod        return NULL;
198*3d8817e4Smiod    @}
199*3d8817e4Smiod
200*3d8817e4Smiod /* Call the allocation method of the base class.  */
201*3d8817e4Smiod  ret = ((@var{entry_type} *)
202*3d8817e4Smiod        @var{base_newfunc} ((struct bfd_hash_entry *) ret, table, string));
203*3d8817e4Smiod
204*3d8817e4Smiod /* Initialize the local fields here.  */
205*3d8817e4Smiod
206*3d8817e4Smiod  return (struct bfd_hash_entry *) ret;
207*3d8817e4Smiod@}
208*3d8817e4Smiod@end example
209*3d8817e4Smiod@strong{Description}@*
210*3d8817e4SmiodThe creation routine for the linker hash table, which is in
211*3d8817e4Smiod@code{linker.c}, looks just like this example.
212*3d8817e4Smiod@var{function_name} is @code{_bfd_link_hash_newfunc}.
213*3d8817e4Smiod@var{entry_type} is @code{struct bfd_link_hash_entry}.
214*3d8817e4Smiod@var{base_newfunc} is @code{bfd_hash_newfunc}, the creation
215*3d8817e4Smiodroutine for a basic hash table.
216*3d8817e4Smiod
217*3d8817e4Smiod@code{_bfd_link_hash_newfunc} also initializes the local fields
218*3d8817e4Smiodin a linker hash table entry: @code{type}, @code{written} and
219*3d8817e4Smiod@code{next}.
220*3d8817e4Smiod
221*3d8817e4Smiod@node Write Other Derived Routines, , Write the Derived Creation Routine, Deriving a New Hash Table Type
222*3d8817e4Smiod@subsubsection Write other derived routines
223*3d8817e4SmiodYou will want to write other routines for your new hash table,
224*3d8817e4Smiodas well.
225*3d8817e4Smiod
226*3d8817e4SmiodYou will want an initialization routine which calls the
227*3d8817e4Smiodinitialization routine of the hash table you are deriving from
228*3d8817e4Smiodand initializes any other local fields.  For the linker hash
229*3d8817e4Smiodtable, this is @code{_bfd_link_hash_table_init} in @code{linker.c}.
230*3d8817e4Smiod
231*3d8817e4SmiodYou will want a lookup routine which calls the lookup routine
232*3d8817e4Smiodof the hash table you are deriving from and casts the result.
233*3d8817e4SmiodThe linker hash table uses @code{bfd_link_hash_lookup} in
234*3d8817e4Smiod@code{linker.c} (this actually takes an additional argument which
235*3d8817e4Smiodit uses to decide how to return the looked up value).
236*3d8817e4Smiod
237*3d8817e4SmiodYou may want a traversal routine.  This should just call the
238*3d8817e4Smiodtraversal routine of the hash table you are deriving from with
239*3d8817e4Smiodappropriate casts.  The linker hash table uses
240*3d8817e4Smiod@code{bfd_link_hash_traverse} in @code{linker.c}.
241*3d8817e4Smiod
242*3d8817e4SmiodThese routines may simply be defined as macros.  For example,
243*3d8817e4Smiodthe a.out backend linker hash table, which is derived from the
244*3d8817e4Smiodlinker hash table, uses macros for the lookup and traversal
245*3d8817e4Smiodroutines.  These are @code{aout_link_hash_lookup} and
246*3d8817e4Smiod@code{aout_link_hash_traverse} in aoutx.h.
247*3d8817e4Smiod
248