hash.c 26 KB

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