oops - update date on newly created entry
[binutils.git] / bfd / elf-strtab.c
blobff7d9a4fafe77b1eb61bddaed63484b5e12c5aa6
1 /* ELF strtab with GC and suffix merging support.
2 Copyright 2001 Free Software Foundation, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "hashtab.h"
27 /* An entry in the strtab hash table. */
29 struct elf_strtab_hash_entry
31 struct bfd_hash_entry root;
32 /* Length of this entry. */
33 unsigned int len;
34 unsigned int refcount;
35 union {
36 /* Index within the merged section. */
37 bfd_size_type index;
38 /* Entry this is a suffix of (if len is 0). */
39 struct elf_strtab_hash_entry *suffix;
40 struct elf_strtab_hash_entry *next;
41 } u;
44 /* The strtab hash table. */
46 struct elf_strtab_hash
48 struct bfd_hash_table table;
49 /* Next available index. */
50 bfd_size_type size;
51 /* Number of array entries alloced. */
52 bfd_size_type alloced;
53 /* Final strtab size. */
54 bfd_size_type sec_size;
55 /* Array of pointers to strtab entries. */
56 struct elf_strtab_hash_entry **array;
59 static struct bfd_hash_entry *elf_strtab_hash_newfunc
60 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
61 static int cmplengthentry PARAMS ((const PTR, const PTR));
62 static int last4_eq PARAMS ((const PTR, const PTR));
64 /* Routine to create an entry in a section merge hashtab. */
66 static struct bfd_hash_entry *
67 elf_strtab_hash_newfunc (entry, table, string)
68 struct bfd_hash_entry *entry;
69 struct bfd_hash_table *table;
70 const char *string;
72 struct elf_strtab_hash_entry *ret = (struct elf_strtab_hash_entry *) entry;
74 /* Allocate the structure if it has not already been allocated by a
75 subclass. */
76 if (ret == (struct elf_strtab_hash_entry *) NULL)
77 ret = ((struct elf_strtab_hash_entry *)
78 bfd_hash_allocate (table, sizeof (struct elf_strtab_hash_entry)));
79 if (ret == (struct elf_strtab_hash_entry *) NULL)
80 return NULL;
82 /* Call the allocation method of the superclass. */
83 ret = ((struct elf_strtab_hash_entry *)
84 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
86 if (ret)
88 /* Initialize the local fields. */
89 ret->u.index = -1;
90 ret->refcount = 0;
91 ret->len = 0;
94 return (struct bfd_hash_entry *)ret;
97 /* Create a new hash table. */
99 struct elf_strtab_hash *
100 _bfd_elf_strtab_init ()
102 struct elf_strtab_hash *table;
103 bfd_size_type amt = sizeof (struct elf_strtab_hash);
105 table = (struct elf_strtab_hash *) bfd_malloc (amt);
106 if (table == NULL)
107 return NULL;
109 if (! bfd_hash_table_init (&table->table, elf_strtab_hash_newfunc))
111 free (table);
112 return NULL;
115 table->sec_size = 0;
116 table->size = 1;
117 table->alloced = 64;
118 amt = sizeof (struct elf_strtab_hasn_entry *);
119 table->array = (struct elf_strtab_hash_entry **)
120 bfd_malloc (table->alloced * amt);
121 if (table->array == NULL)
123 free (table);
124 return NULL;
127 table->array[0] = NULL;
129 return table;
132 /* Free a strtab. */
134 void
135 _bfd_elf_strtab_free (tab)
136 struct elf_strtab_hash *tab;
138 bfd_hash_table_free (&tab->table);
139 free (tab->array);
140 free (tab);
143 /* Get the index of an entity in a hash table, adding it if it is not
144 already present. */
146 bfd_size_type
147 _bfd_elf_strtab_add (tab, str, copy)
148 struct elf_strtab_hash *tab;
149 const char *str;
150 boolean copy;
152 register struct elf_strtab_hash_entry *entry;
154 /* We handle this specially, since we don't want to do refcounting
155 on it. */
156 if (*str == '\0')
157 return 0;
159 BFD_ASSERT (tab->sec_size == 0);
160 entry = (struct elf_strtab_hash_entry *)
161 bfd_hash_lookup (&tab->table, str, true, copy);
163 if (entry == NULL)
164 return (bfd_size_type) -1;
166 entry->refcount++;
167 if (entry->len == 0)
169 entry->len = strlen (str) + 1;
170 if (tab->size == tab->alloced)
172 bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
173 tab->alloced *= 2;
174 tab->array = (struct elf_strtab_hash_entry **)
175 bfd_realloc (tab->array, tab->alloced * amt);
176 if (tab->array == NULL)
177 return (bfd_size_type) -1;
180 entry->u.index = tab->size++;
181 tab->array[entry->u.index] = entry;
183 return entry->u.index;
186 void
187 _bfd_elf_strtab_addref (tab, idx)
188 struct elf_strtab_hash *tab;
189 bfd_size_type idx;
191 if (idx == 0 || idx == (bfd_size_type) -1)
192 return;
193 BFD_ASSERT (tab->sec_size == 0);
194 BFD_ASSERT (idx < tab->size);
195 ++tab->array[idx]->refcount;
198 void
199 _bfd_elf_strtab_delref (tab, idx)
200 struct elf_strtab_hash *tab;
201 bfd_size_type idx;
203 if (idx == 0 || idx == (bfd_size_type) -1)
204 return;
205 BFD_ASSERT (tab->sec_size == 0);
206 BFD_ASSERT (idx < tab->size);
207 BFD_ASSERT (tab->array[idx]->refcount > 0);
208 --tab->array[idx]->refcount;
211 void
212 _bfd_elf_strtab_clear_all_refs (tab)
213 struct elf_strtab_hash *tab;
215 bfd_size_type idx;
217 for (idx = 1; idx < tab->size; ++idx)
218 tab->array[idx]->refcount = 0;
221 bfd_size_type
222 _bfd_elf_strtab_size (tab)
223 struct elf_strtab_hash *tab;
225 return tab->sec_size ? tab->sec_size : tab->size;
228 bfd_size_type
229 _bfd_elf_strtab_offset (tab, idx)
230 struct elf_strtab_hash *tab;
231 bfd_size_type idx;
233 struct elf_strtab_hash_entry *entry;
235 if (idx == 0)
236 return 0;
237 BFD_ASSERT (idx < tab->size);
238 BFD_ASSERT (tab->sec_size);
239 entry = tab->array[idx];
240 BFD_ASSERT (entry->refcount > 0);
241 entry->refcount--;
242 return tab->array[idx]->u.index;
245 boolean
246 _bfd_elf_strtab_emit (abfd, tab)
247 register bfd *abfd;
248 struct elf_strtab_hash *tab;
250 bfd_size_type off = 1, i;
252 if (bfd_bwrite ("", 1, abfd) != 1)
253 return false;
255 for (i = 1; i < tab->size; ++i)
257 register const char *str;
258 register size_t len;
260 str = tab->array[i]->root.string;
261 len = tab->array[i]->len;
262 BFD_ASSERT (tab->array[i]->refcount == 0);
263 if (len == 0)
264 continue;
266 if (bfd_bwrite ((PTR) str, (bfd_size_type) len, abfd) != len)
267 return false;
269 off += len;
272 BFD_ASSERT (off == tab->sec_size);
273 return true;
276 /* Compare two elf_strtab_hash_entry structures. This is called via qsort. */
278 static int
279 cmplengthentry (a, b)
280 const PTR a;
281 const PTR b;
283 struct elf_strtab_hash_entry * A = *(struct elf_strtab_hash_entry **) a;
284 struct elf_strtab_hash_entry * B = *(struct elf_strtab_hash_entry **) b;
286 if (A->len < B->len)
287 return 1;
288 else if (A->len > B->len)
289 return -1;
291 return memcmp (A->root.string, B->root.string, A->len);
294 static int
295 last4_eq (a, b)
296 const PTR a;
297 const PTR b;
299 struct elf_strtab_hash_entry * A = (struct elf_strtab_hash_entry *) a;
300 struct elf_strtab_hash_entry * B = (struct elf_strtab_hash_entry *) b;
302 if (memcmp (A->root.string + A->len - 5, B->root.string + B->len - 5, 4)
303 != 0)
304 /* This was a hashtable collision. */
305 return 0;
307 if (A->len <= B->len)
308 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
309 not to be equal by the hash table. */
310 return 0;
312 return memcmp (A->root.string + (A->len - B->len),
313 B->root.string, B->len - 5) == 0;
316 /* This function assigns final string table offsets for used strings,
317 merging strings matching suffixes of longer strings if possible. */
319 void
320 _bfd_elf_strtab_finalize (tab)
321 struct elf_strtab_hash *tab;
323 struct elf_strtab_hash_entry **array, **a, **end, *e;
324 htab_t last4tab = NULL;
325 bfd_size_type size, amt;
326 struct elf_strtab_hash_entry *last[256], **last_ptr[256];
328 /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
329 a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
330 Besides, indexing with a long long wouldn't give anything but extra
331 cycles. */
332 size_t i;
334 /* Now sort the strings by length, longest first. */
335 array = NULL;
336 amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
337 array = (struct elf_strtab_hash_entry **) bfd_malloc (amt);
338 if (array == NULL)
339 goto alloc_failure;
341 memset (last, 0, sizeof (last));
342 for (i = 0; i < 256; ++i)
343 last_ptr[i] = &last[i];
344 for (i = 1, a = array; i < tab->size; ++i)
345 if (tab->array[i]->refcount)
346 *a++ = tab->array[i];
347 else
348 tab->array[i]->len = 0;
350 size = a - array;
352 qsort (array, size, sizeof (struct elf_strtab_hash_entry *), cmplengthentry);
354 last4tab = htab_create (size * 4, NULL, last4_eq, NULL);
355 if (last4tab == NULL)
356 goto alloc_failure;
358 /* Now insert the strings into hash tables (strings with last 4 characters
359 and strings with last character equal), look for longer strings which
360 we're suffix of. */
361 for (a = array, end = array + size; a < end; a++)
363 register hashval_t hash;
364 unsigned int c;
365 unsigned int j;
366 const unsigned char *s;
367 PTR *p;
369 e = *a;
370 if (e->len > 4)
372 s = e->root.string + e->len - 1;
373 hash = 0;
374 for (j = 0; j < 4; j++)
376 c = *--s;
377 hash += c + (c << 17);
378 hash ^= hash >> 2;
380 p = htab_find_slot_with_hash (last4tab, e, hash, INSERT);
381 if (p == NULL)
382 goto alloc_failure;
383 if (*p)
385 struct elf_strtab_hash_entry *ent;
387 ent = (struct elf_strtab_hash_entry *) *p;
388 e->u.suffix = ent;
389 e->len = 0;
390 continue;
392 else
393 *p = (PTR) e;
395 else
397 struct elf_strtab_hash_entry *tem;
399 c = e->root.string[e->len - 2] & 0xff;
401 for (tem = last[c]; tem; tem = tem->u.next)
402 if (tem->len > e->len
403 && memcmp (tem->root.string + (tem->len - e->len),
404 e->root.string, e->len - 1) == 0)
405 break;
406 if (tem)
408 e->u.suffix = tem;
409 e->len = 0;
410 continue;
414 c = e->root.string[e->len - 2] & 0xff;
415 /* Put longest strings first. */
416 *last_ptr[c] = e;
417 last_ptr[c] = &e->u.next;
418 e->u.next = NULL;
421 alloc_failure:
422 if (array)
423 free (array);
424 if (last4tab)
425 htab_delete (last4tab);
427 /* Now assign positions to the strings we want to keep. */
428 size = 1;
429 for (i = 1; i < tab->size; ++i)
431 e = tab->array[i];
432 if (e->refcount && e->len)
434 e->u.index = size;
435 size += e->len;
439 tab->sec_size = size;
441 /* And now adjust the rest. */
442 for (i = 1; i < tab->size; ++i)
444 e = tab->array[i];
445 if (e->refcount && ! e->len)
446 e->u.index = e->u.suffix->u.index
447 + (e->u.suffix->len - strlen (e->root.string) - 1);