2007-06-01 Steve Ellcey <sje@cup.hp.com>
[binutils.git] / bfd / elf-strtab.c
blobf5d6db1c10c0ed8ce72cba5e6b1ec5be8b25e49b
1 /* ELF strtab with GC and suffix merging support.
2 Copyright 2001, 2002, 2003, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Jakub Jelinek <jakub@redhat.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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "hashtab.h"
27 #include "libiberty.h"
29 /* An entry in the strtab hash table. */
31 struct elf_strtab_hash_entry
33 struct bfd_hash_entry root;
34 /* Length of this entry. This includes the zero terminator. */
35 int len;
36 unsigned int refcount;
37 union {
38 /* Index within the merged section. */
39 bfd_size_type index;
40 /* Entry this is a suffix of (if len < 0). */
41 struct elf_strtab_hash_entry *suffix;
42 } u;
45 /* The strtab hash table. */
47 struct elf_strtab_hash
49 struct bfd_hash_table table;
50 /* Next available index. */
51 bfd_size_type size;
52 /* Number of array entries alloced. */
53 bfd_size_type alloced;
54 /* Final strtab size. */
55 bfd_size_type sec_size;
56 /* Array of pointers to strtab entries. */
57 struct elf_strtab_hash_entry **array;
60 /* Routine to create an entry in a section merge hashtab. */
62 static struct bfd_hash_entry *
63 elf_strtab_hash_newfunc (struct bfd_hash_entry *entry,
64 struct bfd_hash_table *table,
65 const char *string)
67 /* Allocate the structure if it has not already been allocated by a
68 subclass. */
69 if (entry == NULL)
70 entry = bfd_hash_allocate (table, sizeof (struct elf_strtab_hash_entry));
71 if (entry == NULL)
72 return NULL;
74 /* Call the allocation method of the superclass. */
75 entry = bfd_hash_newfunc (entry, table, string);
77 if (entry)
79 /* Initialize the local fields. */
80 struct elf_strtab_hash_entry *ret;
82 ret = (struct elf_strtab_hash_entry *) entry;
83 ret->u.index = -1;
84 ret->refcount = 0;
85 ret->len = 0;
88 return entry;
91 /* Create a new hash table. */
93 struct elf_strtab_hash *
94 _bfd_elf_strtab_init (void)
96 struct elf_strtab_hash *table;
97 bfd_size_type amt = sizeof (struct elf_strtab_hash);
99 table = bfd_malloc (amt);
100 if (table == NULL)
101 return NULL;
103 if (!bfd_hash_table_init (&table->table, elf_strtab_hash_newfunc,
104 sizeof (struct elf_strtab_hash_entry)))
106 free (table);
107 return NULL;
110 table->sec_size = 0;
111 table->size = 1;
112 table->alloced = 64;
113 amt = sizeof (struct elf_strtab_hasn_entry *);
114 table->array = bfd_malloc (table->alloced * amt);
115 if (table->array == NULL)
117 free (table);
118 return NULL;
121 table->array[0] = NULL;
123 return table;
126 /* Free a strtab. */
128 void
129 _bfd_elf_strtab_free (struct elf_strtab_hash *tab)
131 bfd_hash_table_free (&tab->table);
132 free (tab->array);
133 free (tab);
136 /* Get the index of an entity in a hash table, adding it if it is not
137 already present. */
139 bfd_size_type
140 _bfd_elf_strtab_add (struct elf_strtab_hash *tab,
141 const char *str,
142 bfd_boolean copy)
144 register struct elf_strtab_hash_entry *entry;
146 /* We handle this specially, since we don't want to do refcounting
147 on it. */
148 if (*str == '\0')
149 return 0;
151 BFD_ASSERT (tab->sec_size == 0);
152 entry = (struct elf_strtab_hash_entry *)
153 bfd_hash_lookup (&tab->table, str, TRUE, copy);
155 if (entry == NULL)
156 return (bfd_size_type) -1;
158 entry->refcount++;
159 if (entry->len == 0)
161 entry->len = strlen (str) + 1;
162 /* 2G strings lose. */
163 BFD_ASSERT (entry->len > 0);
164 if (tab->size == tab->alloced)
166 bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
167 tab->alloced *= 2;
168 tab->array = bfd_realloc (tab->array, tab->alloced * amt);
169 if (tab->array == NULL)
170 return (bfd_size_type) -1;
173 entry->u.index = tab->size++;
174 tab->array[entry->u.index] = entry;
176 return entry->u.index;
179 void
180 _bfd_elf_strtab_addref (struct elf_strtab_hash *tab, bfd_size_type idx)
182 if (idx == 0 || idx == (bfd_size_type) -1)
183 return;
184 BFD_ASSERT (tab->sec_size == 0);
185 BFD_ASSERT (idx < tab->size);
186 ++tab->array[idx]->refcount;
189 void
190 _bfd_elf_strtab_delref (struct elf_strtab_hash *tab, bfd_size_type idx)
192 if (idx == 0 || idx == (bfd_size_type) -1)
193 return;
194 BFD_ASSERT (tab->sec_size == 0);
195 BFD_ASSERT (idx < tab->size);
196 BFD_ASSERT (tab->array[idx]->refcount > 0);
197 --tab->array[idx]->refcount;
200 void
201 _bfd_elf_strtab_clear_all_refs (struct elf_strtab_hash *tab)
203 bfd_size_type idx;
205 for (idx = 1; idx < tab->size; ++idx)
206 tab->array[idx]->refcount = 0;
209 bfd_size_type
210 _bfd_elf_strtab_size (struct elf_strtab_hash *tab)
212 return tab->sec_size ? tab->sec_size : tab->size;
215 bfd_size_type
216 _bfd_elf_strtab_offset (struct elf_strtab_hash *tab, bfd_size_type idx)
218 struct elf_strtab_hash_entry *entry;
220 if (idx == 0)
221 return 0;
222 BFD_ASSERT (idx < tab->size);
223 BFD_ASSERT (tab->sec_size);
224 entry = tab->array[idx];
225 BFD_ASSERT (entry->refcount > 0);
226 entry->refcount--;
227 return tab->array[idx]->u.index;
230 bfd_boolean
231 _bfd_elf_strtab_emit (register bfd *abfd, struct elf_strtab_hash *tab)
233 bfd_size_type off = 1, i;
235 if (bfd_bwrite ("", 1, abfd) != 1)
236 return FALSE;
238 for (i = 1; i < tab->size; ++i)
240 register const char *str;
241 register unsigned int len;
243 BFD_ASSERT (tab->array[i]->refcount == 0);
244 len = tab->array[i]->len;
245 if ((int) len < 0)
246 continue;
248 str = tab->array[i]->root.string;
249 if (bfd_bwrite (str, len, abfd) != len)
250 return FALSE;
252 off += len;
255 BFD_ASSERT (off == tab->sec_size);
256 return TRUE;
259 /* Compare two elf_strtab_hash_entry structures. Called via qsort. */
261 static int
262 strrevcmp (const void *a, const void *b)
264 struct elf_strtab_hash_entry *A = *(struct elf_strtab_hash_entry **) a;
265 struct elf_strtab_hash_entry *B = *(struct elf_strtab_hash_entry **) b;
266 unsigned int lenA = A->len;
267 unsigned int lenB = B->len;
268 const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
269 const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
270 int l = lenA < lenB ? lenA : lenB;
272 while (l)
274 if (*s != *t)
275 return (int) *s - (int) *t;
276 s--;
277 t--;
278 l--;
280 return lenA - lenB;
283 static inline int
284 is_suffix (const struct elf_strtab_hash_entry *A,
285 const struct elf_strtab_hash_entry *B)
287 if (A->len <= B->len)
288 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
289 not to be equal by the hash table. */
290 return 0;
292 return memcmp (A->root.string + (A->len - B->len),
293 B->root.string, B->len - 1) == 0;
296 /* This function assigns final string table offsets for used strings,
297 merging strings matching suffixes of longer strings if possible. */
299 void
300 _bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
302 struct elf_strtab_hash_entry **array, **a, *e;
303 bfd_size_type size, amt;
305 /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
306 a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
307 Besides, indexing with a long long wouldn't give anything but extra
308 cycles. */
309 size_t i;
311 /* Sort the strings by suffix and length. */
312 amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
313 array = bfd_malloc (amt);
314 if (array == NULL)
315 goto alloc_failure;
317 for (i = 1, a = array; i < tab->size; ++i)
319 e = tab->array[i];
320 if (e->refcount)
322 *a++ = e;
323 /* Adjust the length to not include the zero terminator. */
324 e->len -= 1;
326 else
327 e->len = 0;
330 size = a - array;
331 if (size != 0)
333 qsort (array, size, sizeof (struct elf_strtab_hash_entry *), strrevcmp);
335 /* Loop over the sorted array and merge suffixes. Start from the
336 end because we want eg.
338 s1 -> "d"
339 s2 -> "bcd"
340 s3 -> "abcd"
342 to end up as
344 s3 -> "abcd"
345 s2 _____^
346 s1 _______^
348 ie. we don't want s1 pointing into the old s2. */
349 e = *--a;
350 e->len += 1;
351 while (--a >= array)
353 struct elf_strtab_hash_entry *cmp = *a;
355 cmp->len += 1;
356 if (is_suffix (e, cmp))
358 cmp->u.suffix = e;
359 cmp->len = -cmp->len;
361 else
362 e = cmp;
366 alloc_failure:
367 if (array)
368 free (array);
370 /* Assign positions to the strings we want to keep. */
371 size = 1;
372 for (i = 1; i < tab->size; ++i)
374 e = tab->array[i];
375 if (e->refcount && e->len > 0)
377 e->u.index = size;
378 size += e->len;
382 tab->sec_size = size;
384 /* Adjust the rest. */
385 for (i = 1; i < tab->size; ++i)
387 e = tab->array[i];
388 if (e->refcount && e->len < 0)
389 e->u.index = e->u.suffix->u.index + (e->u.suffix->len + e->len);