1 1.1 christos /* hash.c -- hash table routines for BFD 2 1.11 christos Copyright (C) 1993-2024 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.8 christos . struct bfd_hash_table *table, 235 1.8 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.8 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.8 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.11 christos 299 1.11 christos EXTERNAL 300 1.11 christos .{* An element in the hash table. Most uses will actually use a larger 301 1.11 christos . structure, and an instance of this will be the first field. *} 302 1.11 christos . 303 1.11 christos .struct bfd_hash_entry 304 1.11 christos .{ 305 1.11 christos . {* Next entry for this hash code. *} 306 1.11 christos . struct bfd_hash_entry *next; 307 1.11 christos . {* String being hashed. *} 308 1.11 christos . const char *string; 309 1.11 christos . {* Hash code. This is the full hash code, not the index into the 310 1.11 christos . table. *} 311 1.11 christos . unsigned long hash; 312 1.11 christos .}; 313 1.11 christos . 314 1.11 christos .{* A hash table. *} 315 1.11 christos . 316 1.11 christos .struct bfd_hash_table 317 1.11 christos .{ 318 1.11 christos . {* The hash array. *} 319 1.11 christos . struct bfd_hash_entry **table; 320 1.11 christos . {* A function used to create new elements in the hash table. The 321 1.11 christos . first entry is itself a pointer to an element. When this 322 1.11 christos . function is first invoked, this pointer will be NULL. However, 323 1.11 christos . having the pointer permits a hierarchy of method functions to be 324 1.11 christos . built each of which calls the function in the superclass. Thus 325 1.11 christos . each function should be written to allocate a new block of memory 326 1.11 christos . only if the argument is NULL. *} 327 1.11 christos . struct bfd_hash_entry *(*newfunc) 328 1.11 christos . (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); 329 1.11 christos . {* An objalloc for this hash table. This is a struct objalloc *, 330 1.11 christos . but we use void * to avoid requiring the inclusion of objalloc.h. *} 331 1.11 christos . void *memory; 332 1.11 christos . {* The number of slots in the hash table. *} 333 1.11 christos . unsigned int size; 334 1.11 christos . {* The number of entries in the hash table. *} 335 1.11 christos . unsigned int count; 336 1.11 christos . {* The size of elements. *} 337 1.11 christos . unsigned int entsize; 338 1.11 christos . {* If non-zero, don't grow the hash table. *} 339 1.11 christos . unsigned int frozen:1; 340 1.11 christos .}; 341 1.11 christos . 342 1.1 christos */ 343 1.1 christos 344 1.1 christos /* The default number of entries to use when creating a hash table. */ 345 1.1 christos #define DEFAULT_SIZE 4051 346 1.1 christos 347 1.1 christos /* The following function returns a nearest prime number which is 348 1.1 christos greater than N, and near a power of two. Copied from libiberty. 349 1.1 christos Returns zero for ridiculously large N to signify an error. */ 350 1.1 christos 351 1.11 christos static uint32_t 352 1.11 christos higher_prime_number (uint32_t n) 353 1.1 christos { 354 1.1 christos /* These are primes that are near, but slightly smaller than, a 355 1.1 christos power of two. */ 356 1.11 christos static const uint32_t primes[] = 357 1.1 christos { 358 1.11 christos UINT32_C (31), 359 1.11 christos UINT32_C (61), 360 1.11 christos UINT32_C (127), 361 1.11 christos UINT32_C (251), 362 1.11 christos UINT32_C (509), 363 1.11 christos UINT32_C (1021), 364 1.11 christos UINT32_C (2039), 365 1.11 christos UINT32_C (4093), 366 1.11 christos UINT32_C (8191), 367 1.11 christos UINT32_C (16381), 368 1.11 christos UINT32_C (32749), 369 1.11 christos UINT32_C (65521), 370 1.11 christos UINT32_C (131071), 371 1.11 christos UINT32_C (262139), 372 1.11 christos UINT32_C (524287), 373 1.11 christos UINT32_C (1048573), 374 1.11 christos UINT32_C (2097143), 375 1.11 christos UINT32_C (4194301), 376 1.11 christos UINT32_C (8388593), 377 1.11 christos UINT32_C (16777213), 378 1.11 christos UINT32_C (33554393), 379 1.11 christos UINT32_C (67108859), 380 1.11 christos UINT32_C (134217689), 381 1.11 christos UINT32_C (268435399), 382 1.11 christos UINT32_C (536870909), 383 1.11 christos UINT32_C (1073741789), 384 1.11 christos UINT32_C (2147483647), 385 1.11 christos UINT32_C (4294967291) 386 1.1 christos }; 387 1.1 christos 388 1.11 christos const uint32_t *low = &primes[0]; 389 1.11 christos const uint32_t *high = &primes[sizeof (primes) / sizeof (primes[0])]; 390 1.1 christos 391 1.1 christos while (low != high) 392 1.1 christos { 393 1.11 christos const uint32_t *mid = low + (high - low) / 2; 394 1.1 christos if (n >= *mid) 395 1.1 christos low = mid + 1; 396 1.1 christos else 397 1.1 christos high = mid; 398 1.1 christos } 399 1.1 christos 400 1.1 christos if (n >= *low) 401 1.1 christos return 0; 402 1.1 christos 403 1.1 christos return *low; 404 1.1 christos } 405 1.1 christos 406 1.11 christos static unsigned int bfd_default_hash_table_size = DEFAULT_SIZE; 407 1.11 christos 408 1.11 christos /* 409 1.11 christos FUNCTION 410 1.11 christos bfd_hash_table_init_n 411 1.11 christos 412 1.11 christos SYNOPSIS 413 1.11 christos bool bfd_hash_table_init_n 414 1.11 christos (struct bfd_hash_table *, 415 1.11 christos struct bfd_hash_entry *(* {*newfunc*}) 416 1.11 christos (struct bfd_hash_entry *, struct bfd_hash_table *, const char *), 417 1.11 christos unsigned int {*entsize*}, unsigned int {*size*}); 418 1.1 christos 419 1.11 christos DESCRIPTION 420 1.11 christos Create a new hash table, given a number of entries. 421 1.11 christos */ 422 1.1 christos 423 1.10 christos bool 424 1.1 christos bfd_hash_table_init_n (struct bfd_hash_table *table, 425 1.1 christos struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 426 1.1 christos struct bfd_hash_table *, 427 1.1 christos const char *), 428 1.1 christos unsigned int entsize, 429 1.1 christos unsigned int size) 430 1.1 christos { 431 1.1 christos unsigned long alloc; 432 1.1 christos 433 1.1 christos alloc = size; 434 1.1 christos alloc *= sizeof (struct bfd_hash_entry *); 435 1.1 christos if (alloc / sizeof (struct bfd_hash_entry *) != size) 436 1.1 christos { 437 1.1 christos bfd_set_error (bfd_error_no_memory); 438 1.10 christos return false; 439 1.1 christos } 440 1.1 christos 441 1.1 christos table->memory = (void *) objalloc_create (); 442 1.1 christos if (table->memory == NULL) 443 1.1 christos { 444 1.1 christos bfd_set_error (bfd_error_no_memory); 445 1.10 christos return false; 446 1.1 christos } 447 1.1 christos table->table = (struct bfd_hash_entry **) 448 1.1 christos objalloc_alloc ((struct objalloc *) table->memory, alloc); 449 1.1 christos if (table->table == NULL) 450 1.1 christos { 451 1.3 christos bfd_hash_table_free (table); 452 1.1 christos bfd_set_error (bfd_error_no_memory); 453 1.10 christos return false; 454 1.1 christos } 455 1.1 christos memset ((void *) table->table, 0, alloc); 456 1.1 christos table->size = size; 457 1.1 christos table->entsize = entsize; 458 1.1 christos table->count = 0; 459 1.1 christos table->frozen = 0; 460 1.1 christos table->newfunc = newfunc; 461 1.10 christos return true; 462 1.1 christos } 463 1.1 christos 464 1.11 christos /* 465 1.11 christos FUNCTION 466 1.11 christos bfd_hash_table_init 467 1.11 christos 468 1.11 christos SYNOPSIS 469 1.11 christos bool bfd_hash_table_init 470 1.11 christos (struct bfd_hash_table *, 471 1.11 christos struct bfd_hash_entry *(* {*newfunc*}) 472 1.11 christos (struct bfd_hash_entry *, struct bfd_hash_table *, const char *), 473 1.11 christos unsigned int {*entsize*}); 474 1.11 christos 475 1.11 christos DESCRIPTION 476 1.11 christos Create a new hash table with the default number of entries. 477 1.11 christos */ 478 1.1 christos 479 1.10 christos bool 480 1.1 christos bfd_hash_table_init (struct bfd_hash_table *table, 481 1.1 christos struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 482 1.1 christos struct bfd_hash_table *, 483 1.1 christos const char *), 484 1.1 christos unsigned int entsize) 485 1.1 christos { 486 1.1 christos return bfd_hash_table_init_n (table, newfunc, entsize, 487 1.1 christos bfd_default_hash_table_size); 488 1.1 christos } 489 1.1 christos 490 1.11 christos /* 491 1.11 christos FUNCTION 492 1.11 christos bfd_hash_table_free 493 1.11 christos 494 1.11 christos SYNOPSIS 495 1.11 christos void bfd_hash_table_free (struct bfd_hash_table *); 496 1.11 christos 497 1.11 christos DESCRIPTION 498 1.11 christos Free a hash table. 499 1.11 christos */ 500 1.1 christos 501 1.1 christos void 502 1.1 christos bfd_hash_table_free (struct bfd_hash_table *table) 503 1.1 christos { 504 1.1 christos objalloc_free ((struct objalloc *) table->memory); 505 1.1 christos table->memory = NULL; 506 1.1 christos } 507 1.1 christos 508 1.1 christos static inline unsigned long 509 1.1 christos bfd_hash_hash (const char *string, unsigned int *lenp) 510 1.1 christos { 511 1.1 christos const unsigned char *s; 512 1.1 christos unsigned long hash; 513 1.1 christos unsigned int len; 514 1.1 christos unsigned int c; 515 1.1 christos 516 1.8 christos BFD_ASSERT (string != NULL); 517 1.1 christos hash = 0; 518 1.1 christos len = 0; 519 1.1 christos s = (const unsigned char *) string; 520 1.1 christos while ((c = *s++) != '\0') 521 1.1 christos { 522 1.1 christos hash += c + (c << 17); 523 1.1 christos hash ^= hash >> 2; 524 1.1 christos } 525 1.1 christos len = (s - (const unsigned char *) string) - 1; 526 1.1 christos hash += len + (len << 17); 527 1.1 christos hash ^= hash >> 2; 528 1.1 christos if (lenp != NULL) 529 1.1 christos *lenp = len; 530 1.1 christos return hash; 531 1.1 christos } 532 1.1 christos 533 1.11 christos /* 534 1.11 christos FUNCTION 535 1.11 christos bfd_hash_lookup 536 1.11 christos 537 1.11 christos SYNOPSIS 538 1.11 christos struct bfd_hash_entry *bfd_hash_lookup 539 1.11 christos (struct bfd_hash_table *, const char *, 540 1.11 christos bool {*create*}, bool {*copy*}); 541 1.11 christos 542 1.11 christos DESCRIPTION 543 1.11 christos Look up a string in a hash table. 544 1.11 christos */ 545 1.1 christos 546 1.1 christos struct bfd_hash_entry * 547 1.1 christos bfd_hash_lookup (struct bfd_hash_table *table, 548 1.1 christos const char *string, 549 1.10 christos bool create, 550 1.10 christos bool copy) 551 1.1 christos { 552 1.1 christos unsigned long hash; 553 1.1 christos struct bfd_hash_entry *hashp; 554 1.1 christos unsigned int len; 555 1.1 christos unsigned int _index; 556 1.1 christos 557 1.1 christos hash = bfd_hash_hash (string, &len); 558 1.1 christos _index = hash % table->size; 559 1.1 christos for (hashp = table->table[_index]; 560 1.1 christos hashp != NULL; 561 1.1 christos hashp = hashp->next) 562 1.1 christos { 563 1.1 christos if (hashp->hash == hash 564 1.1 christos && strcmp (hashp->string, string) == 0) 565 1.1 christos return hashp; 566 1.1 christos } 567 1.1 christos 568 1.1 christos if (! create) 569 1.1 christos return NULL; 570 1.1 christos 571 1.1 christos if (copy) 572 1.1 christos { 573 1.1 christos char *new_string; 574 1.1 christos 575 1.1 christos new_string = (char *) objalloc_alloc ((struct objalloc *) table->memory, 576 1.8 christos len + 1); 577 1.1 christos if (!new_string) 578 1.1 christos { 579 1.1 christos bfd_set_error (bfd_error_no_memory); 580 1.1 christos return NULL; 581 1.1 christos } 582 1.1 christos memcpy (new_string, string, len + 1); 583 1.1 christos string = new_string; 584 1.1 christos } 585 1.1 christos 586 1.1 christos return bfd_hash_insert (table, string, hash); 587 1.1 christos } 588 1.1 christos 589 1.11 christos /* 590 1.11 christos FUNCTION 591 1.11 christos bfd_hash_insert 592 1.11 christos 593 1.11 christos SYNOPSIS 594 1.11 christos struct bfd_hash_entry *bfd_hash_insert 595 1.11 christos (struct bfd_hash_table *, 596 1.11 christos const char *, 597 1.11 christos unsigned long {*hash*}); 598 1.11 christos 599 1.11 christos DESCRIPTION 600 1.11 christos Insert an entry in a hash table. 601 1.11 christos */ 602 1.1 christos 603 1.1 christos struct bfd_hash_entry * 604 1.1 christos bfd_hash_insert (struct bfd_hash_table *table, 605 1.1 christos const char *string, 606 1.1 christos unsigned long hash) 607 1.1 christos { 608 1.1 christos struct bfd_hash_entry *hashp; 609 1.1 christos unsigned int _index; 610 1.1 christos 611 1.1 christos hashp = (*table->newfunc) (NULL, table, string); 612 1.1 christos if (hashp == NULL) 613 1.1 christos return NULL; 614 1.1 christos hashp->string = string; 615 1.1 christos hashp->hash = hash; 616 1.1 christos _index = hash % table->size; 617 1.1 christos hashp->next = table->table[_index]; 618 1.1 christos table->table[_index] = hashp; 619 1.1 christos table->count++; 620 1.1 christos 621 1.1 christos if (!table->frozen && table->count > table->size * 3 / 4) 622 1.1 christos { 623 1.1 christos unsigned long newsize = higher_prime_number (table->size); 624 1.1 christos struct bfd_hash_entry **newtable; 625 1.1 christos unsigned int hi; 626 1.1 christos unsigned long alloc = newsize * sizeof (struct bfd_hash_entry *); 627 1.1 christos 628 1.1 christos /* If we can't find a higher prime, or we can't possibly alloc 629 1.1 christos that much memory, don't try to grow the table. */ 630 1.1 christos if (newsize == 0 || alloc / sizeof (struct bfd_hash_entry *) != newsize) 631 1.1 christos { 632 1.1 christos table->frozen = 1; 633 1.1 christos return hashp; 634 1.1 christos } 635 1.1 christos 636 1.1 christos newtable = ((struct bfd_hash_entry **) 637 1.1 christos objalloc_alloc ((struct objalloc *) table->memory, alloc)); 638 1.1 christos if (newtable == NULL) 639 1.1 christos { 640 1.1 christos table->frozen = 1; 641 1.1 christos return hashp; 642 1.1 christos } 643 1.1 christos memset (newtable, 0, alloc); 644 1.1 christos 645 1.1 christos for (hi = 0; hi < table->size; hi ++) 646 1.1 christos while (table->table[hi]) 647 1.1 christos { 648 1.1 christos struct bfd_hash_entry *chain = table->table[hi]; 649 1.1 christos struct bfd_hash_entry *chain_end = chain; 650 1.1 christos 651 1.1 christos while (chain_end->next && chain_end->next->hash == chain->hash) 652 1.1 christos chain_end = chain_end->next; 653 1.1 christos 654 1.1 christos table->table[hi] = chain_end->next; 655 1.1 christos _index = chain->hash % newsize; 656 1.1 christos chain_end->next = newtable[_index]; 657 1.1 christos newtable[_index] = chain; 658 1.1 christos } 659 1.1 christos table->table = newtable; 660 1.1 christos table->size = newsize; 661 1.1 christos } 662 1.1 christos 663 1.1 christos return hashp; 664 1.1 christos } 665 1.1 christos 666 1.11 christos /* 667 1.11 christos FUNCTION 668 1.11 christos bfd_hash_rename 669 1.11 christos 670 1.11 christos SYNOPSIS 671 1.11 christos void bfd_hash_rename (struct bfd_hash_table *, 672 1.11 christos const char *, 673 1.11 christos struct bfd_hash_entry *); 674 1.11 christos 675 1.11 christos DESCRIPTION 676 1.11 christos Rename an entry in a hash table. 677 1.11 christos */ 678 1.1 christos 679 1.1 christos void 680 1.1 christos bfd_hash_rename (struct bfd_hash_table *table, 681 1.1 christos const char *string, 682 1.1 christos struct bfd_hash_entry *ent) 683 1.1 christos { 684 1.1 christos unsigned int _index; 685 1.1 christos struct bfd_hash_entry **pph; 686 1.1 christos 687 1.1 christos _index = ent->hash % table->size; 688 1.1 christos for (pph = &table->table[_index]; *pph != NULL; pph = &(*pph)->next) 689 1.1 christos if (*pph == ent) 690 1.1 christos break; 691 1.1 christos if (*pph == NULL) 692 1.1 christos abort (); 693 1.1 christos 694 1.1 christos *pph = ent->next; 695 1.1 christos ent->string = string; 696 1.1 christos ent->hash = bfd_hash_hash (string, NULL); 697 1.1 christos _index = ent->hash % table->size; 698 1.1 christos ent->next = table->table[_index]; 699 1.1 christos table->table[_index] = ent; 700 1.1 christos } 701 1.1 christos 702 1.11 christos /* 703 1.11 christos FUNCTION 704 1.11 christos bfd_hash_replace 705 1.11 christos 706 1.11 christos SYNOPSIS 707 1.11 christos void bfd_hash_replace (struct bfd_hash_table *, 708 1.11 christos struct bfd_hash_entry * {*old*}, 709 1.11 christos struct bfd_hash_entry * {*new*}); 710 1.11 christos 711 1.11 christos DESCRIPTION 712 1.11 christos Replace an entry in a hash table. 713 1.11 christos */ 714 1.1 christos 715 1.1 christos void 716 1.1 christos bfd_hash_replace (struct bfd_hash_table *table, 717 1.1 christos struct bfd_hash_entry *old, 718 1.1 christos struct bfd_hash_entry *nw) 719 1.1 christos { 720 1.1 christos unsigned int _index; 721 1.1 christos struct bfd_hash_entry **pph; 722 1.1 christos 723 1.1 christos _index = old->hash % table->size; 724 1.1 christos for (pph = &table->table[_index]; 725 1.1 christos (*pph) != NULL; 726 1.1 christos pph = &(*pph)->next) 727 1.1 christos { 728 1.1 christos if (*pph == old) 729 1.1 christos { 730 1.1 christos *pph = nw; 731 1.1 christos return; 732 1.1 christos } 733 1.1 christos } 734 1.1 christos 735 1.1 christos abort (); 736 1.1 christos } 737 1.1 christos 738 1.11 christos /* 739 1.11 christos FUNCTION 740 1.11 christos bfd_hash_allocate 741 1.11 christos 742 1.11 christos SYNOPSIS 743 1.11 christos void *bfd_hash_allocate (struct bfd_hash_table *, 744 1.11 christos unsigned int {*size*}); 745 1.11 christos 746 1.11 christos DESCRIPTION 747 1.11 christos Allocate space in a hash table. 748 1.11 christos */ 749 1.1 christos 750 1.1 christos void * 751 1.1 christos bfd_hash_allocate (struct bfd_hash_table *table, 752 1.1 christos unsigned int size) 753 1.1 christos { 754 1.1 christos void * ret; 755 1.1 christos 756 1.1 christos ret = objalloc_alloc ((struct objalloc *) table->memory, size); 757 1.1 christos if (ret == NULL && size != 0) 758 1.1 christos bfd_set_error (bfd_error_no_memory); 759 1.1 christos return ret; 760 1.1 christos } 761 1.1 christos 762 1.11 christos /* 763 1.11 christos FUNCTION 764 1.11 christos bfd_hash_newfunc 765 1.11 christos 766 1.11 christos SYNOPSIS 767 1.11 christos struct bfd_hash_entry *bfd_hash_newfunc 768 1.11 christos (struct bfd_hash_entry *, 769 1.11 christos struct bfd_hash_table *, 770 1.11 christos const char *); 771 1.11 christos 772 1.11 christos DESCRIPTION 773 1.11 christos Base method for creating a new hash table entry. 774 1.11 christos */ 775 1.1 christos 776 1.1 christos struct bfd_hash_entry * 777 1.1 christos bfd_hash_newfunc (struct bfd_hash_entry *entry, 778 1.1 christos struct bfd_hash_table *table, 779 1.1 christos const char *string ATTRIBUTE_UNUSED) 780 1.1 christos { 781 1.1 christos if (entry == NULL) 782 1.1 christos entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, 783 1.8 christos sizeof (* entry)); 784 1.1 christos return entry; 785 1.1 christos } 786 1.1 christos 787 1.11 christos /* 788 1.11 christos FUNCTION 789 1.11 christos bfd_hash_traverse 790 1.11 christos 791 1.11 christos SYNOPSIS 792 1.11 christos void bfd_hash_traverse 793 1.11 christos (struct bfd_hash_table *, 794 1.11 christos bool (*) (struct bfd_hash_entry *, void *), 795 1.11 christos void *); 796 1.11 christos 797 1.11 christos DESCRIPTION 798 1.11 christos Traverse a hash table. 799 1.11 christos */ 800 1.1 christos 801 1.1 christos void 802 1.1 christos bfd_hash_traverse (struct bfd_hash_table *table, 803 1.10 christos bool (*func) (struct bfd_hash_entry *, void *), 804 1.11 christos void *info) 805 1.1 christos { 806 1.1 christos unsigned int i; 807 1.1 christos 808 1.1 christos table->frozen = 1; 809 1.1 christos for (i = 0; i < table->size; i++) 810 1.1 christos { 811 1.1 christos struct bfd_hash_entry *p; 812 1.1 christos 813 1.1 christos for (p = table->table[i]; p != NULL; p = p->next) 814 1.1 christos if (! (*func) (p, info)) 815 1.1 christos goto out; 816 1.1 christos } 817 1.1 christos out: 818 1.1 christos table->frozen = 0; 819 1.1 christos } 820 1.11 christos 821 1.11 christos /* 822 1.11 christos FUNCTION 823 1.11 christos bfd_hash_set_default_size 824 1.11 christos 825 1.11 christos SYNOPSIS 826 1.11 christos unsigned int bfd_hash_set_default_size (unsigned int); 827 1.11 christos 828 1.11 christos DESCRIPTION 829 1.11 christos Set hash table default size. 830 1.11 christos */ 831 1.11 christos 832 1.11 christos unsigned int 833 1.11 christos bfd_hash_set_default_size (unsigned int hash_size) 834 1.1 christos { 835 1.9 christos /* These silly_size values result in around 1G and 32M of memory 836 1.9 christos being allocated for the table of pointers. Note that the number 837 1.9 christos of elements allocated will be almost twice the size of any power 838 1.9 christos of two chosen here. */ 839 1.11 christos unsigned int silly_size = sizeof (size_t) > 4 ? 0x4000000 : 0x400000; 840 1.9 christos if (hash_size > silly_size) 841 1.9 christos hash_size = silly_size; 842 1.9 christos else if (hash_size != 0) 843 1.9 christos hash_size--; 844 1.9 christos hash_size = higher_prime_number (hash_size); 845 1.9 christos BFD_ASSERT (hash_size != 0); 846 1.9 christos bfd_default_hash_table_size = hash_size; 847 1.1 christos return bfd_default_hash_table_size; 848 1.1 christos } 849 1.1 christos 850 1.1 christos /* A few different object file formats (a.out, COFF, ELF) use a string 852 1.1 christos table. These functions support adding strings to a string table, 853 1.1 christos returning the byte offset, and writing out the table. 854 1.1 christos 855 1.1 christos Possible improvements: 856 1.1 christos + look for strings matching trailing substrings of other strings 857 1.1 christos + better data structures? balanced trees? 858 1.1 christos + look at reducing memory use elsewhere -- maybe if we didn't have 859 1.1 christos to construct the entire symbol table at once, we could get by 860 1.1 christos with smaller amounts of VM? (What effect does that have on the 861 1.1 christos string table reductions?) */ 862 1.1 christos 863 1.1 christos /* An entry in the strtab hash table. */ 864 1.1 christos 865 1.1 christos struct strtab_hash_entry 866 1.1 christos { 867 1.1 christos struct bfd_hash_entry root; 868 1.1 christos /* Index in string table. */ 869 1.1 christos bfd_size_type index; 870 1.1 christos /* Next string in strtab. */ 871 1.1 christos struct strtab_hash_entry *next; 872 1.1 christos }; 873 1.1 christos 874 1.1 christos /* The strtab hash table. */ 875 1.1 christos 876 1.1 christos struct bfd_strtab_hash 877 1.1 christos { 878 1.1 christos struct bfd_hash_table table; 879 1.1 christos /* Size of strtab--also next available index. */ 880 1.1 christos bfd_size_type size; 881 1.1 christos /* First string in strtab. */ 882 1.1 christos struct strtab_hash_entry *first; 883 1.1 christos /* Last string in strtab. */ 884 1.10 christos struct strtab_hash_entry *last; 885 1.10 christos /* Whether to precede strings with a two or four byte length, 886 1.10 christos as in the XCOFF .debug section. */ 887 1.1 christos char length_field_size; 888 1.1 christos }; 889 1.10 christos 890 1.1 christos 891 1.1 christos /* Routine to create an entry in a strtab. */ 892 1.1 christos 893 1.1 christos static struct bfd_hash_entry * 894 1.1 christos strtab_hash_newfunc (struct bfd_hash_entry *entry, 895 1.1 christos struct bfd_hash_table *table, 896 1.1 christos const char *string) 897 1.1 christos { 898 1.1 christos struct strtab_hash_entry *ret = (struct strtab_hash_entry *) entry; 899 1.1 christos 900 1.1 christos /* Allocate the structure if it has not already been allocated by a 901 1.1 christos subclass. */ 902 1.1 christos if (ret == NULL) 903 1.8 christos ret = (struct strtab_hash_entry *) bfd_hash_allocate (table, 904 1.1 christos sizeof (* ret)); 905 1.1 christos if (ret == NULL) 906 1.1 christos return NULL; 907 1.1 christos 908 1.1 christos /* Call the allocation method of the superclass. */ 909 1.1 christos ret = (struct strtab_hash_entry *) 910 1.1 christos bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string); 911 1.1 christos 912 1.1 christos if (ret) 913 1.1 christos { 914 1.1 christos /* Initialize the local fields. */ 915 1.1 christos ret->index = (bfd_size_type) -1; 916 1.1 christos ret->next = NULL; 917 1.1 christos } 918 1.1 christos 919 1.1 christos return (struct bfd_hash_entry *) ret; 920 1.1 christos } 921 1.1 christos 922 1.1 christos /* Look up an entry in an strtab. */ 923 1.1 christos 924 1.1 christos #define strtab_hash_lookup(t, string, create, copy) \ 925 1.1 christos ((struct strtab_hash_entry *) \ 926 1.1 christos bfd_hash_lookup (&(t)->table, (string), (create), (copy))) 927 1.11 christos 928 1.11 christos /* 929 1.11 christos INTERNAL_FUNCTION 930 1.11 christos _bfd_stringtab_init 931 1.11 christos 932 1.11 christos SYNOPSIS 933 1.11 christos struct bfd_strtab_hash *_bfd_stringtab_init (void); 934 1.11 christos 935 1.11 christos DESCRIPTION 936 1.11 christos Create a new strtab. 937 1.1 christos */ 938 1.1 christos 939 1.1 christos struct bfd_strtab_hash * 940 1.1 christos _bfd_stringtab_init (void) 941 1.1 christos { 942 1.9 christos struct bfd_strtab_hash *table; 943 1.1 christos size_t amt = sizeof (* table); 944 1.1 christos 945 1.1 christos table = (struct bfd_strtab_hash *) bfd_malloc (amt); 946 1.1 christos if (table == NULL) 947 1.1 christos return NULL; 948 1.1 christos 949 1.1 christos if (!bfd_hash_table_init (&table->table, strtab_hash_newfunc, 950 1.1 christos sizeof (struct strtab_hash_entry))) 951 1.1 christos { 952 1.1 christos free (table); 953 1.1 christos return NULL; 954 1.1 christos } 955 1.1 christos 956 1.1 christos table->size = 0; 957 1.1 christos table->first = NULL; 958 1.10 christos table->last = NULL; 959 1.1 christos table->length_field_size = 0; 960 1.1 christos 961 1.1 christos return table; 962 1.1 christos } 963 1.11 christos 964 1.11 christos /* 965 1.11 christos INTERNAL_FUNCTION 966 1.11 christos _bfd_xcoff_stringtab_init 967 1.11 christos 968 1.11 christos SYNOPSIS 969 1.11 christos struct bfd_strtab_hash *_bfd_xcoff_stringtab_init 970 1.11 christos (bool {*isxcoff64*}); 971 1.11 christos 972 1.11 christos DESCRIPTION 973 1.11 christos Create a new strtab in which the strings are output in the format 974 1.11 christos used in the XCOFF .debug section: a two byte length precedes each 975 1.11 christos string. 976 1.1 christos */ 977 1.1 christos 978 1.10 christos struct bfd_strtab_hash * 979 1.1 christos _bfd_xcoff_stringtab_init (bool isxcoff64) 980 1.1 christos { 981 1.1 christos struct bfd_strtab_hash *ret; 982 1.1 christos 983 1.1 christos ret = _bfd_stringtab_init (); 984 1.10 christos if (ret != NULL) 985 1.1 christos ret->length_field_size = isxcoff64 ? 4 : 2; 986 1.1 christos return ret; 987 1.1 christos } 988 1.11 christos 989 1.11 christos /* 990 1.11 christos INTERNAL_FUNCTION 991 1.11 christos _bfd_stringtab_free 992 1.11 christos 993 1.11 christos SYNOPSIS 994 1.11 christos void _bfd_stringtab_free (struct bfd_strtab_hash *); 995 1.11 christos 996 1.11 christos DESCRIPTION 997 1.11 christos Free a strtab. 998 1.1 christos */ 999 1.1 christos 1000 1.1 christos void 1001 1.1 christos _bfd_stringtab_free (struct bfd_strtab_hash *table) 1002 1.1 christos { 1003 1.1 christos bfd_hash_table_free (&table->table); 1004 1.1 christos free (table); 1005 1.1 christos } 1006 1.11 christos 1007 1.11 christos /* 1008 1.11 christos INTERNAL_FUNCTION 1009 1.11 christos _bfd_stringtab_add 1010 1.11 christos 1011 1.11 christos SYNOPSIS 1012 1.11 christos bfd_size_type _bfd_stringtab_add 1013 1.11 christos (struct bfd_strtab_hash *, const char *, 1014 1.11 christos bool {*hash*}, bool {*copy*}); 1015 1.11 christos 1016 1.11 christos DESCRIPTION 1017 1.11 christos Get the index of a string in a strtab, adding it if it is not 1018 1.11 christos already present. If HASH is FALSE, we don't really use the hash 1019 1.11 christos table, and we don't eliminate duplicate strings. If COPY is true 1020 1.11 christos then store a copy of STR if creating a new entry. 1021 1.1 christos */ 1022 1.1 christos 1023 1.1 christos bfd_size_type 1024 1.1 christos _bfd_stringtab_add (struct bfd_strtab_hash *tab, 1025 1.10 christos const char *str, 1026 1.10 christos bool hash, 1027 1.1 christos bool copy) 1028 1.1 christos { 1029 1.1 christos struct strtab_hash_entry *entry; 1030 1.1 christos 1031 1.1 christos if (hash) 1032 1.10 christos { 1033 1.1 christos entry = strtab_hash_lookup (tab, str, true, copy); 1034 1.1 christos if (entry == NULL) 1035 1.1 christos return (bfd_size_type) -1; 1036 1.1 christos } 1037 1.1 christos else 1038 1.1 christos { 1039 1.8 christos entry = (struct strtab_hash_entry *) bfd_hash_allocate (&tab->table, 1040 1.1 christos sizeof (* entry)); 1041 1.1 christos if (entry == NULL) 1042 1.1 christos return (bfd_size_type) -1; 1043 1.1 christos if (! copy) 1044 1.1 christos entry->root.string = str; 1045 1.1 christos else 1046 1.1 christos { 1047 1.1 christos size_t len = strlen (str) + 1; 1048 1.1 christos char *n; 1049 1.1 christos 1050 1.1 christos n = (char *) bfd_hash_allocate (&tab->table, len); 1051 1.1 christos if (n == NULL) 1052 1.8 christos return (bfd_size_type) -1; 1053 1.1 christos memcpy (n, str, len); 1054 1.1 christos entry->root.string = n; 1055 1.1 christos } 1056 1.1 christos entry->index = (bfd_size_type) -1; 1057 1.1 christos entry->next = NULL; 1058 1.1 christos } 1059 1.1 christos 1060 1.1 christos if (entry->index == (bfd_size_type) -1) 1061 1.1 christos { 1062 1.1 christos entry->index = tab->size; 1063 1.10 christos tab->size += strlen (str) + 1; 1064 1.10 christos entry->index += tab->length_field_size; 1065 1.1 christos tab->size += tab->length_field_size; 1066 1.1 christos if (tab->first == NULL) 1067 1.1 christos tab->first = entry; 1068 1.1 christos else 1069 1.1 christos tab->last->next = entry; 1070 1.1 christos tab->last = entry; 1071 1.1 christos } 1072 1.1 christos 1073 1.1 christos return entry->index; 1074 1.1 christos } 1075 1.11 christos 1076 1.11 christos /* 1077 1.11 christos INTERNAL_FUNCTION 1078 1.11 christos _bfd_stringtab_size 1079 1.11 christos 1080 1.11 christos SYNOPSIS 1081 1.11 christos bfd_size_type _bfd_stringtab_size (struct bfd_strtab_hash *); 1082 1.11 christos 1083 1.11 christos DESCRIPTION 1084 1.11 christos Get the number of bytes in a strtab. 1085 1.1 christos */ 1086 1.1 christos 1087 1.1 christos bfd_size_type 1088 1.1 christos _bfd_stringtab_size (struct bfd_strtab_hash *tab) 1089 1.1 christos { 1090 1.1 christos return tab->size; 1091 1.1 christos } 1092 1.11 christos 1093 1.11 christos /* 1094 1.11 christos INTERNAL_FUNCTION 1095 1.11 christos _bfd_stringtab_emit 1096 1.11 christos 1097 1.11 christos SYNOPSIS 1098 1.11 christos bool _bfd_stringtab_emit (bfd *, struct bfd_strtab_hash *); 1099 1.11 christos 1100 1.11 christos DESCRIPTION 1101 1.11 christos Write out a strtab. ABFD must already be at the right location in 1102 1.11 christos the file. 1103 1.1 christos */ 1104 1.10 christos 1105 1.1 christos bool 1106 1.1 christos _bfd_stringtab_emit (bfd *abfd, struct bfd_strtab_hash *tab) 1107 1.1 christos { 1108 1.1 christos struct strtab_hash_entry *entry; 1109 1.1 christos 1110 1.1 christos for (entry = tab->first; entry != NULL; entry = entry->next) 1111 1.1 christos { 1112 1.1 christos const char *str; 1113 1.1 christos size_t len; 1114 1.1 christos 1115 1.1 christos str = entry->root.string; 1116 1.1 christos len = strlen (str) + 1; 1117 1.10 christos 1118 1.10 christos if (tab->length_field_size == 4) 1119 1.10 christos { 1120 1.10 christos bfd_byte buf[4]; 1121 1.10 christos 1122 1.11 christos /* The output length includes the null byte. */ 1123 1.11 christos bfd_put_32 (abfd, len, buf); 1124 1.10 christos if (bfd_write (buf, 4, abfd) != 4) 1125 1.10 christos return false; 1126 1.10 christos } 1127 1.1 christos else if (tab->length_field_size == 2) 1128 1.1 christos { 1129 1.1 christos bfd_byte buf[2]; 1130 1.1 christos 1131 1.11 christos /* The output length includes the null byte. */ 1132 1.11 christos bfd_put_16 (abfd, len, buf); 1133 1.10 christos if (bfd_write (buf, 2, abfd) != 2) 1134 1.1 christos return false; 1135 1.1 christos } 1136 1.11 christos 1137 1.10 christos if (bfd_write (str, len, abfd) != len) 1138 1.1 christos return false; 1139 1.1 christos } 1140 1.10 christos 1141 1.1 christos return true; 1142 } 1143