1 /* hash.c -- hash table routines for BFD
2 Copyright 1993, 1994, 1995, 1997, 1999, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain <sac@cygnus.com>
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 BFD provides a simple set of hash table functions. Routines
33 are provided to initialize a hash table, to free a hash table,
34 to look up a string in a hash table and optionally create an
35 entry for it, and to traverse a hash table. There is
36 currently no routine to delete an string from a hash table.
38 The basic hash table does not permit any data to be stored
39 with a string. However, a hash table is designed to present a
40 base class from which other types of hash tables may be
41 derived. These derived types may store additional information
42 with the string. Hash tables were implemented in this way,
43 rather than simply providing a data pointer in a hash table
44 entry, because they were designed for use by the linker back
45 ends. The linker may create thousands of hash table entries,
46 and the overhead of allocating private data and storing and
47 following pointers becomes noticeable.
49 The basic hash table code is in <<hash.c>>.
52 @* Creating and Freeing a Hash Table::
53 @* Looking Up or Entering a String::
54 @* Traversing a Hash Table::
55 @* Deriving a New Hash Table Type::
59 Creating and Freeing a Hash Table, Looking Up or Entering a String, Hash Tables, Hash Tables
61 Creating and freeing a hash table
63 @findex bfd_hash_table_init
64 @findex bfd_hash_table_init_n
65 To create a hash table, create an instance of a <<struct
66 bfd_hash_table>> (defined in <<bfd.h>>) and call
67 <<bfd_hash_table_init>> (if you know approximately how many
68 entries you will need, the function <<bfd_hash_table_init_n>>,
69 which takes a @var{size} argument, may be used).
70 <<bfd_hash_table_init>> returns <<FALSE>> if some sort of
73 @findex bfd_hash_newfunc
74 The function <<bfd_hash_table_init>> take as an argument a
75 function to use to create new entries. For a basic hash
76 table, use the function <<bfd_hash_newfunc>>. @xref{Deriving
77 a New Hash Table Type}, for why you would want to use a
78 different value for this argument.
80 @findex bfd_hash_allocate
81 <<bfd_hash_table_init>> will create an objalloc which will be
82 used to allocate new entries. You may allocate memory on this
83 objalloc using <<bfd_hash_allocate>>.
85 @findex bfd_hash_table_free
86 Use <<bfd_hash_table_free>> to free up all the memory that has
87 been allocated for a hash table. This will not free up the
88 <<struct bfd_hash_table>> itself, which you must provide.
91 Looking Up or Entering a String, Traversing a Hash Table, Creating and Freeing a Hash Table, Hash Tables
93 Looking up or entering a string
95 @findex bfd_hash_lookup
96 The function <<bfd_hash_lookup>> is used both to look up a
97 string in the hash table and to create a new entry.
99 If the @var{create} argument is <<FALSE>>, <<bfd_hash_lookup>>
100 will look up a string. If the string is found, it will
101 returns a pointer to a <<struct bfd_hash_entry>>. If the
102 string is not found in the table <<bfd_hash_lookup>> will
103 return <<NULL>>. You should not modify any of the fields in
104 the returns <<struct bfd_hash_entry>>.
106 If the @var{create} argument is <<TRUE>>, the string will be
107 entered into the hash table if it is not already there.
108 Either way a pointer to a <<struct bfd_hash_entry>> will be
109 returned, either to the existing structure or to a newly
110 created one. In this case, a <<NULL>> return means that an
113 If the @var{create} argument is <<TRUE>>, and a new entry is
114 created, the @var{copy} argument is used to decide whether to
115 copy the string onto the hash table objalloc or not. If
116 @var{copy} is passed as <<FALSE>>, you must be careful not to
117 deallocate or modify the string as long as the hash table
121 Traversing a Hash Table, Deriving a New Hash Table Type, Looking Up or Entering a String, Hash Tables
123 Traversing a hash table
125 @findex bfd_hash_traverse
126 The function <<bfd_hash_traverse>> may be used to traverse a
127 hash table, calling a function on each element. The traversal
128 is done in a random order.
130 <<bfd_hash_traverse>> takes as arguments a function and a
131 generic <<void *>> pointer. The function is called with a
132 hash table entry (a <<struct bfd_hash_entry *>>) and the
133 generic pointer passed to <<bfd_hash_traverse>>. The function
134 must return a <<boolean>> value, which indicates whether to
135 continue traversing the hash table. If the function returns
136 <<FALSE>>, <<bfd_hash_traverse>> will stop the traversal and
140 Deriving a New Hash Table Type, , Traversing a Hash Table, Hash Tables
142 Deriving a new hash table type
144 Many uses of hash tables want to store additional information
145 which each entry in the hash table. Some also find it
146 convenient to store additional information with the hash table
147 itself. This may be done using a derived hash table.
149 Since C is not an object oriented language, creating a derived
150 hash table requires sticking together some boilerplate
151 routines with a few differences specific to the type of hash
152 table you want to create.
154 An example of a derived hash table is the linker hash table.
155 The structures for this are defined in <<bfdlink.h>>. The
156 functions are in <<linker.c>>.
158 You may also derive a hash table from an already derived hash
159 table. For example, the a.out linker backend code uses a hash
160 table derived from the linker hash table.
163 @* Define the Derived Structures::
164 @* Write the Derived Creation Routine::
165 @* Write Other Derived Routines::
169 Define the Derived Structures, Write the Derived Creation Routine, Deriving a New Hash Table Type, Deriving a New Hash Table Type
171 Define the derived structures
173 You must define a structure for an entry in the hash table,
174 and a structure for the hash table itself.
176 The first field in the structure for an entry in the hash
177 table must be of the type used for an entry in the hash table
178 you are deriving from. If you are deriving from a basic hash
179 table this is <<struct bfd_hash_entry>>, which is defined in
180 <<bfd.h>>. The first field in the structure for the hash
181 table itself must be of the type of the hash table you are
182 deriving from itself. If you are deriving from a basic hash
183 table, this is <<struct bfd_hash_table>>.
185 For example, the linker hash table defines <<struct
186 bfd_link_hash_entry>> (in <<bfdlink.h>>). The first field,
187 <<root>>, is of type <<struct bfd_hash_entry>>. Similarly,
188 the first field in <<struct bfd_link_hash_table>>, <<table>>,
189 is of type <<struct bfd_hash_table>>.
192 Write the Derived Creation Routine, Write Other Derived Routines, Define the Derived Structures, Deriving a New Hash Table Type
194 Write the derived creation routine
196 You must write a routine which will create and initialize an
197 entry in the hash table. This routine is passed as the
198 function argument to <<bfd_hash_table_init>>.
200 In order to permit other hash tables to be derived from the
201 hash table you are creating, this routine must be written in a
204 The first argument to the creation routine is a pointer to a
205 hash table entry. This may be <<NULL>>, in which case the
206 routine should allocate the right amount of space. Otherwise
207 the space has already been allocated by a hash table type
208 derived from this one.
210 After allocating space, the creation routine must call the
211 creation routine of the hash table type it is derived from,
212 passing in a pointer to the space it just allocated. This
213 will initialize any fields used by the base hash table.
215 Finally the creation routine must initialize any local fields
216 for the new hash table type.
218 Here is a boilerplate example of a creation routine.
219 @var{function_name} is the name of the routine.
220 @var{entry_type} is the type of an entry in the hash table you
221 are creating. @var{base_newfunc} is the name of the creation
222 routine of the hash table type your hash table is derived
227 .struct bfd_hash_entry *
228 .@var{function_name} (entry, table, string)
229 . struct bfd_hash_entry *entry;
230 . struct bfd_hash_table *table;
231 . const char *string;
233 . struct @var{entry_type} *ret = (@var{entry_type} *) entry;
235 . {* Allocate the structure if it has not already been allocated by a
237 . if (ret == (@var{entry_type} *) NULL)
239 . ret = ((@var{entry_type} *)
240 . bfd_hash_allocate (table, sizeof (@var{entry_type})));
241 . if (ret == (@var{entry_type} *) NULL)
245 . {* Call the allocation method of the base class. *}
246 . ret = ((@var{entry_type} *)
247 . @var{base_newfunc} ((struct bfd_hash_entry *) ret, table, string));
249 . {* Initialize the local fields here. *}
251 . return (struct bfd_hash_entry *) ret;
255 The creation routine for the linker hash table, which is in
256 <<linker.c>>, looks just like this example.
257 @var{function_name} is <<_bfd_link_hash_newfunc>>.
258 @var{entry_type} is <<struct bfd_link_hash_entry>>.
259 @var{base_newfunc} is <<bfd_hash_newfunc>>, the creation
260 routine for a basic hash table.
262 <<_bfd_link_hash_newfunc>> also initializes the local fields
263 in a linker hash table entry: <<type>>, <<written>> and
267 Write Other Derived Routines, , Write the Derived Creation Routine, Deriving a New Hash Table Type
269 Write other derived routines
271 You will want to write other routines for your new hash table,
274 You will want an initialization routine which calls the
275 initialization routine of the hash table you are deriving from
276 and initializes any other local fields. For the linker hash
277 table, this is <<_bfd_link_hash_table_init>> in <<linker.c>>.
279 You will want a lookup routine which calls the lookup routine
280 of the hash table you are deriving from and casts the result.
281 The linker hash table uses <<bfd_link_hash_lookup>> in
282 <<linker.c>> (this actually takes an additional argument which
283 it uses to decide how to return the looked up value).
285 You may want a traversal routine. This should just call the
286 traversal routine of the hash table you are deriving from with
287 appropriate casts. The linker hash table uses
288 <<bfd_link_hash_traverse>> in <<linker.c>>.
290 These routines may simply be defined as macros. For example,
291 the a.out backend linker hash table, which is derived from the
292 linker hash table, uses macros for the lookup and traversal
293 routines. These are <<aout_link_hash_lookup>> and
294 <<aout_link_hash_traverse>> in aoutx.h.
297 /* The default number of entries to use when creating a hash table. */
298 #define DEFAULT_SIZE (4051)
300 /* Create a new hash table, given a number of entries. */
303 bfd_hash_table_init_n (table
, newfunc
, size
)
304 struct bfd_hash_table
*table
;
305 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
306 struct bfd_hash_table
*,
312 alloc
= size
* sizeof (struct bfd_hash_entry
*);
314 table
->memory
= (PTR
) objalloc_create ();
315 if (table
->memory
== NULL
)
317 bfd_set_error (bfd_error_no_memory
);
320 table
->table
= ((struct bfd_hash_entry
**)
321 objalloc_alloc ((struct objalloc
*) table
->memory
, alloc
));
322 if (table
->table
== NULL
)
324 bfd_set_error (bfd_error_no_memory
);
327 memset ((PTR
) table
->table
, 0, alloc
);
329 table
->newfunc
= newfunc
;
333 /* Create a new hash table with the default number of entries. */
336 bfd_hash_table_init (table
, newfunc
)
337 struct bfd_hash_table
*table
;
338 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
339 struct bfd_hash_table
*,
342 return bfd_hash_table_init_n (table
, newfunc
, DEFAULT_SIZE
);
345 /* Free a hash table. */
348 bfd_hash_table_free (table
)
349 struct bfd_hash_table
*table
;
351 objalloc_free ((struct objalloc
*) table
->memory
);
352 table
->memory
= NULL
;
355 /* Look up a string in a hash table. */
357 struct bfd_hash_entry
*
358 bfd_hash_lookup (table
, string
, create
, copy
)
359 struct bfd_hash_table
*table
;
364 register const unsigned char *s
;
365 register unsigned long hash
;
366 register unsigned int c
;
367 struct bfd_hash_entry
*hashp
;
373 s
= (const unsigned char *) string
;
374 while ((c
= *s
++) != '\0')
376 hash
+= c
+ (c
<< 17);
379 len
= (s
- (const unsigned char *) string
) - 1;
380 hash
+= len
+ (len
<< 17);
383 index
= hash
% table
->size
;
384 for (hashp
= table
->table
[index
];
385 hashp
!= (struct bfd_hash_entry
*) NULL
;
388 if (hashp
->hash
== hash
389 && strcmp (hashp
->string
, string
) == 0)
394 return (struct bfd_hash_entry
*) NULL
;
396 hashp
= (*table
->newfunc
) ((struct bfd_hash_entry
*) NULL
, table
, string
);
397 if (hashp
== (struct bfd_hash_entry
*) NULL
)
398 return (struct bfd_hash_entry
*) NULL
;
403 new = (char *) objalloc_alloc ((struct objalloc
*) table
->memory
,
407 bfd_set_error (bfd_error_no_memory
);
408 return (struct bfd_hash_entry
*) NULL
;
410 memcpy (new, string
, len
+ 1);
413 hashp
->string
= string
;
415 hashp
->next
= table
->table
[index
];
416 table
->table
[index
] = hashp
;
421 /* Replace an entry in a hash table. */
424 bfd_hash_replace (table
, old
, nw
)
425 struct bfd_hash_table
*table
;
426 struct bfd_hash_entry
*old
;
427 struct bfd_hash_entry
*nw
;
430 struct bfd_hash_entry
**pph
;
432 index
= old
->hash
% table
->size
;
433 for (pph
= &table
->table
[index
];
434 (*pph
) != (struct bfd_hash_entry
*) NULL
;
447 /* Base method for creating a new hash table entry. */
449 struct bfd_hash_entry
*
450 bfd_hash_newfunc (entry
, table
, string
)
451 struct bfd_hash_entry
*entry
;
452 struct bfd_hash_table
*table
;
453 const char *string ATTRIBUTE_UNUSED
;
455 if (entry
== (struct bfd_hash_entry
*) NULL
)
456 entry
= ((struct bfd_hash_entry
*)
457 bfd_hash_allocate (table
, sizeof (struct bfd_hash_entry
)));
461 /* Allocate space in a hash table. */
464 bfd_hash_allocate (table
, size
)
465 struct bfd_hash_table
*table
;
470 ret
= objalloc_alloc ((struct objalloc
*) table
->memory
, size
);
471 if (ret
== NULL
&& size
!= 0)
472 bfd_set_error (bfd_error_no_memory
);
476 /* Traverse a hash table. */
479 bfd_hash_traverse (table
, func
, info
)
480 struct bfd_hash_table
*table
;
481 bfd_boolean (*func
) PARAMS ((struct bfd_hash_entry
*, PTR
));
486 for (i
= 0; i
< table
->size
; i
++)
488 struct bfd_hash_entry
*p
;
490 for (p
= table
->table
[i
]; p
!= NULL
; p
= p
->next
)
492 if (! (*func
) (p
, info
))
498 /* A few different object file formats (a.out, COFF, ELF) use a string
499 table. These functions support adding strings to a string table,
500 returning the byte offset, and writing out the table.
502 Possible improvements:
503 + look for strings matching trailing substrings of other strings
504 + better data structures? balanced trees?
505 + look at reducing memory use elsewhere -- maybe if we didn't have
506 to construct the entire symbol table at once, we could get by
507 with smaller amounts of VM? (What effect does that have on the
508 string table reductions?) */
510 /* An entry in the strtab hash table. */
512 struct strtab_hash_entry
514 struct bfd_hash_entry root
;
515 /* Index in string table. */
517 /* Next string in strtab. */
518 struct strtab_hash_entry
*next
;
521 /* The strtab hash table. */
523 struct bfd_strtab_hash
525 struct bfd_hash_table table
;
526 /* Size of strtab--also next available index. */
528 /* First string in strtab. */
529 struct strtab_hash_entry
*first
;
530 /* Last string in strtab. */
531 struct strtab_hash_entry
*last
;
532 /* Whether to precede strings with a two byte length, as in the
533 XCOFF .debug section. */
537 static struct bfd_hash_entry
*strtab_hash_newfunc
538 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
540 /* Routine to create an entry in a strtab. */
542 static struct bfd_hash_entry
*
543 strtab_hash_newfunc (entry
, table
, string
)
544 struct bfd_hash_entry
*entry
;
545 struct bfd_hash_table
*table
;
548 struct strtab_hash_entry
*ret
= (struct strtab_hash_entry
*) entry
;
550 /* Allocate the structure if it has not already been allocated by a
552 if (ret
== (struct strtab_hash_entry
*) NULL
)
553 ret
= ((struct strtab_hash_entry
*)
554 bfd_hash_allocate (table
, sizeof (struct strtab_hash_entry
)));
555 if (ret
== (struct strtab_hash_entry
*) NULL
)
558 /* Call the allocation method of the superclass. */
559 ret
= ((struct strtab_hash_entry
*)
560 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
564 /* Initialize the local fields. */
565 ret
->index
= (bfd_size_type
) -1;
569 return (struct bfd_hash_entry
*) ret
;
572 /* Look up an entry in an strtab. */
574 #define strtab_hash_lookup(t, string, create, copy) \
575 ((struct strtab_hash_entry *) \
576 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
578 /* Create a new strtab. */
580 struct bfd_strtab_hash
*
581 _bfd_stringtab_init ()
583 struct bfd_strtab_hash
*table
;
584 bfd_size_type amt
= sizeof (struct bfd_strtab_hash
);
586 table
= (struct bfd_strtab_hash
*) bfd_malloc (amt
);
590 if (! bfd_hash_table_init (&table
->table
, strtab_hash_newfunc
))
599 table
->xcoff
= FALSE
;
604 /* Create a new strtab in which the strings are output in the format
605 used in the XCOFF .debug section: a two byte length precedes each
608 struct bfd_strtab_hash
*
609 _bfd_xcoff_stringtab_init ()
611 struct bfd_strtab_hash
*ret
;
613 ret
= _bfd_stringtab_init ();
622 _bfd_stringtab_free (table
)
623 struct bfd_strtab_hash
*table
;
625 bfd_hash_table_free (&table
->table
);
629 /* Get the index of a string in a strtab, adding it if it is not
630 already present. If HASH is FALSE, we don't really use the hash
631 table, and we don't eliminate duplicate strings. */
634 _bfd_stringtab_add (tab
, str
, hash
, copy
)
635 struct bfd_strtab_hash
*tab
;
640 register struct strtab_hash_entry
*entry
;
644 entry
= strtab_hash_lookup (tab
, str
, TRUE
, copy
);
646 return (bfd_size_type
) -1;
650 entry
= ((struct strtab_hash_entry
*)
651 bfd_hash_allocate (&tab
->table
,
652 sizeof (struct strtab_hash_entry
)));
654 return (bfd_size_type
) -1;
656 entry
->root
.string
= str
;
661 n
= (char *) bfd_hash_allocate (&tab
->table
, strlen (str
) + 1);
663 return (bfd_size_type
) -1;
664 entry
->root
.string
= n
;
666 entry
->index
= (bfd_size_type
) -1;
670 if (entry
->index
== (bfd_size_type
) -1)
672 entry
->index
= tab
->size
;
673 tab
->size
+= strlen (str
) + 1;
679 if (tab
->first
== NULL
)
682 tab
->last
->next
= entry
;
689 /* Get the number of bytes in a strtab. */
692 _bfd_stringtab_size (tab
)
693 struct bfd_strtab_hash
*tab
;
698 /* Write out a strtab. ABFD must already be at the right location in
702 _bfd_stringtab_emit (abfd
, tab
)
704 struct bfd_strtab_hash
*tab
;
706 register bfd_boolean xcoff
;
707 register struct strtab_hash_entry
*entry
;
711 for (entry
= tab
->first
; entry
!= NULL
; entry
= entry
->next
)
716 str
= entry
->root
.string
;
717 len
= strlen (str
) + 1;
723 /* The output length includes the null byte. */
724 bfd_put_16 (abfd
, (bfd_vma
) len
, buf
);
725 if (bfd_bwrite ((PTR
) buf
, (bfd_size_type
) 2, abfd
) != 2)
729 if (bfd_bwrite ((PTR
) str
, (bfd_size_type
) len
, abfd
) != len
)