xref: /openbsd-src/gnu/usr.bin/binutils/bfd/elf-strtab.c (revision cf2f2c5620d6d9a4fd01930983c4b9a1f76d7aa3)
1d2201f2fSdrahn /* ELF strtab with GC and suffix merging support.
2*cf2f2c56Smiod    Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3d2201f2fSdrahn    Written by Jakub Jelinek <jakub@redhat.com>.
4d2201f2fSdrahn 
5d2201f2fSdrahn    This file is part of BFD, the Binary File Descriptor library.
6d2201f2fSdrahn 
7d2201f2fSdrahn    This program is free software; you can redistribute it and/or modify
8d2201f2fSdrahn    it under the terms of the GNU General Public License as published by
9d2201f2fSdrahn    the Free Software Foundation; either version 2 of the License, or
10d2201f2fSdrahn    (at your option) any later version.
11d2201f2fSdrahn 
12d2201f2fSdrahn    This program is distributed in the hope that it will be useful,
13d2201f2fSdrahn    but WITHOUT ANY WARRANTY; without even the implied warranty of
14d2201f2fSdrahn    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15d2201f2fSdrahn    GNU General Public License for more details.
16d2201f2fSdrahn 
17d2201f2fSdrahn    You should have received a copy of the GNU General Public License
18d2201f2fSdrahn    along with this program; if not, write to the Free Software
19d2201f2fSdrahn    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20d2201f2fSdrahn 
21d2201f2fSdrahn #include "bfd.h"
22d2201f2fSdrahn #include "sysdep.h"
23d2201f2fSdrahn #include "libbfd.h"
24d2201f2fSdrahn #include "elf-bfd.h"
25d2201f2fSdrahn #include "hashtab.h"
26d2201f2fSdrahn #include "libiberty.h"
27d2201f2fSdrahn 
28d2201f2fSdrahn /* An entry in the strtab hash table.  */
29d2201f2fSdrahn 
30d2201f2fSdrahn struct elf_strtab_hash_entry
31d2201f2fSdrahn {
32d2201f2fSdrahn   struct bfd_hash_entry root;
33*cf2f2c56Smiod   /* Length of this entry.  This includes the zero terminator.  */
34*cf2f2c56Smiod   int len;
35d2201f2fSdrahn   unsigned int refcount;
36d2201f2fSdrahn   union {
37d2201f2fSdrahn     /* Index within the merged section.  */
38d2201f2fSdrahn     bfd_size_type index;
39*cf2f2c56Smiod     /* Entry this is a suffix of (if len < 0).  */
40d2201f2fSdrahn     struct elf_strtab_hash_entry *suffix;
41d2201f2fSdrahn   } u;
42d2201f2fSdrahn };
43d2201f2fSdrahn 
44d2201f2fSdrahn /* The strtab hash table.  */
45d2201f2fSdrahn 
46d2201f2fSdrahn struct elf_strtab_hash
47d2201f2fSdrahn {
48d2201f2fSdrahn   struct bfd_hash_table table;
49d2201f2fSdrahn   /* Next available index.  */
50d2201f2fSdrahn   bfd_size_type size;
51d2201f2fSdrahn   /* Number of array entries alloced.  */
52d2201f2fSdrahn   bfd_size_type alloced;
53d2201f2fSdrahn   /* Final strtab size.  */
54d2201f2fSdrahn   bfd_size_type sec_size;
55d2201f2fSdrahn   /* Array of pointers to strtab entries.  */
56d2201f2fSdrahn   struct elf_strtab_hash_entry **array;
57d2201f2fSdrahn };
58d2201f2fSdrahn 
59d2201f2fSdrahn /* Routine to create an entry in a section merge hashtab.  */
60d2201f2fSdrahn 
61d2201f2fSdrahn static struct bfd_hash_entry *
elf_strtab_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)62*cf2f2c56Smiod elf_strtab_hash_newfunc (struct bfd_hash_entry *entry,
63*cf2f2c56Smiod 			 struct bfd_hash_table *table,
64*cf2f2c56Smiod 			 const char *string)
65d2201f2fSdrahn {
66d2201f2fSdrahn   /* Allocate the structure if it has not already been allocated by a
67d2201f2fSdrahn      subclass.  */
68*cf2f2c56Smiod   if (entry == NULL)
69*cf2f2c56Smiod     entry = bfd_hash_allocate (table, sizeof (struct elf_strtab_hash_entry));
70*cf2f2c56Smiod   if (entry == NULL)
71d2201f2fSdrahn     return NULL;
72d2201f2fSdrahn 
73d2201f2fSdrahn   /* Call the allocation method of the superclass.  */
74*cf2f2c56Smiod   entry = bfd_hash_newfunc (entry, table, string);
75d2201f2fSdrahn 
76*cf2f2c56Smiod   if (entry)
77d2201f2fSdrahn     {
78d2201f2fSdrahn       /* Initialize the local fields.  */
79*cf2f2c56Smiod       struct elf_strtab_hash_entry *ret;
80*cf2f2c56Smiod 
81*cf2f2c56Smiod       ret = (struct elf_strtab_hash_entry *) entry;
82d2201f2fSdrahn       ret->u.index = -1;
83d2201f2fSdrahn       ret->refcount = 0;
84d2201f2fSdrahn       ret->len = 0;
85d2201f2fSdrahn     }
86d2201f2fSdrahn 
87*cf2f2c56Smiod   return entry;
88d2201f2fSdrahn }
89d2201f2fSdrahn 
90d2201f2fSdrahn /* Create a new hash table.  */
91d2201f2fSdrahn 
92d2201f2fSdrahn struct elf_strtab_hash *
_bfd_elf_strtab_init(void)93*cf2f2c56Smiod _bfd_elf_strtab_init (void)
94d2201f2fSdrahn {
95d2201f2fSdrahn   struct elf_strtab_hash *table;
96d2201f2fSdrahn   bfd_size_type amt = sizeof (struct elf_strtab_hash);
97d2201f2fSdrahn 
98*cf2f2c56Smiod   table = bfd_malloc (amt);
99d2201f2fSdrahn   if (table == NULL)
100d2201f2fSdrahn     return NULL;
101d2201f2fSdrahn 
102d2201f2fSdrahn   if (! bfd_hash_table_init (&table->table, elf_strtab_hash_newfunc))
103d2201f2fSdrahn     {
104d2201f2fSdrahn       free (table);
105d2201f2fSdrahn       return NULL;
106d2201f2fSdrahn     }
107d2201f2fSdrahn 
108d2201f2fSdrahn   table->sec_size = 0;
109d2201f2fSdrahn   table->size = 1;
110d2201f2fSdrahn   table->alloced = 64;
111d2201f2fSdrahn   amt = sizeof (struct elf_strtab_hasn_entry *);
112*cf2f2c56Smiod   table->array = bfd_malloc (table->alloced * amt);
113d2201f2fSdrahn   if (table->array == NULL)
114d2201f2fSdrahn     {
115d2201f2fSdrahn       free (table);
116d2201f2fSdrahn       return NULL;
117d2201f2fSdrahn     }
118d2201f2fSdrahn 
119d2201f2fSdrahn   table->array[0] = NULL;
120d2201f2fSdrahn 
121d2201f2fSdrahn   return table;
122d2201f2fSdrahn }
123d2201f2fSdrahn 
124d2201f2fSdrahn /* Free a strtab.  */
125d2201f2fSdrahn 
126d2201f2fSdrahn void
_bfd_elf_strtab_free(struct elf_strtab_hash * tab)127*cf2f2c56Smiod _bfd_elf_strtab_free (struct elf_strtab_hash *tab)
128d2201f2fSdrahn {
129d2201f2fSdrahn   bfd_hash_table_free (&tab->table);
130d2201f2fSdrahn   free (tab->array);
131d2201f2fSdrahn   free (tab);
132d2201f2fSdrahn }
133d2201f2fSdrahn 
134d2201f2fSdrahn /* Get the index of an entity in a hash table, adding it if it is not
135d2201f2fSdrahn    already present.  */
136d2201f2fSdrahn 
137d2201f2fSdrahn bfd_size_type
_bfd_elf_strtab_add(struct elf_strtab_hash * tab,const char * str,bfd_boolean copy)138*cf2f2c56Smiod _bfd_elf_strtab_add (struct elf_strtab_hash *tab,
139*cf2f2c56Smiod 		     const char *str,
140*cf2f2c56Smiod 		     bfd_boolean copy)
141d2201f2fSdrahn {
142d2201f2fSdrahn   register struct elf_strtab_hash_entry *entry;
143d2201f2fSdrahn 
144d2201f2fSdrahn   /* We handle this specially, since we don't want to do refcounting
145d2201f2fSdrahn      on it.  */
146d2201f2fSdrahn   if (*str == '\0')
147d2201f2fSdrahn     return 0;
148d2201f2fSdrahn 
149d2201f2fSdrahn   BFD_ASSERT (tab->sec_size == 0);
150d2201f2fSdrahn   entry = (struct elf_strtab_hash_entry *)
151d2201f2fSdrahn 	  bfd_hash_lookup (&tab->table, str, TRUE, copy);
152d2201f2fSdrahn 
153d2201f2fSdrahn   if (entry == NULL)
154d2201f2fSdrahn     return (bfd_size_type) -1;
155d2201f2fSdrahn 
156d2201f2fSdrahn   entry->refcount++;
157d2201f2fSdrahn   if (entry->len == 0)
158d2201f2fSdrahn     {
159d2201f2fSdrahn       entry->len = strlen (str) + 1;
160*cf2f2c56Smiod       /* 2G strings lose.  */
161*cf2f2c56Smiod       BFD_ASSERT (entry->len > 0);
162d2201f2fSdrahn       if (tab->size == tab->alloced)
163d2201f2fSdrahn 	{
164d2201f2fSdrahn 	  bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
165d2201f2fSdrahn 	  tab->alloced *= 2;
166*cf2f2c56Smiod 	  tab->array = bfd_realloc (tab->array, tab->alloced * amt);
167d2201f2fSdrahn 	  if (tab->array == NULL)
168d2201f2fSdrahn 	    return (bfd_size_type) -1;
169d2201f2fSdrahn 	}
170d2201f2fSdrahn 
171d2201f2fSdrahn       entry->u.index = tab->size++;
172d2201f2fSdrahn       tab->array[entry->u.index] = entry;
173d2201f2fSdrahn     }
174d2201f2fSdrahn   return entry->u.index;
175d2201f2fSdrahn }
176d2201f2fSdrahn 
177d2201f2fSdrahn void
_bfd_elf_strtab_addref(struct elf_strtab_hash * tab,bfd_size_type idx)178*cf2f2c56Smiod _bfd_elf_strtab_addref (struct elf_strtab_hash *tab, bfd_size_type idx)
179d2201f2fSdrahn {
180d2201f2fSdrahn   if (idx == 0 || idx == (bfd_size_type) -1)
181d2201f2fSdrahn     return;
182d2201f2fSdrahn   BFD_ASSERT (tab->sec_size == 0);
183d2201f2fSdrahn   BFD_ASSERT (idx < tab->size);
184d2201f2fSdrahn   ++tab->array[idx]->refcount;
185d2201f2fSdrahn }
186d2201f2fSdrahn 
187d2201f2fSdrahn void
_bfd_elf_strtab_delref(struct elf_strtab_hash * tab,bfd_size_type idx)188*cf2f2c56Smiod _bfd_elf_strtab_delref (struct elf_strtab_hash *tab, bfd_size_type idx)
189d2201f2fSdrahn {
190d2201f2fSdrahn   if (idx == 0 || idx == (bfd_size_type) -1)
191d2201f2fSdrahn     return;
192d2201f2fSdrahn   BFD_ASSERT (tab->sec_size == 0);
193d2201f2fSdrahn   BFD_ASSERT (idx < tab->size);
194d2201f2fSdrahn   BFD_ASSERT (tab->array[idx]->refcount > 0);
195d2201f2fSdrahn   --tab->array[idx]->refcount;
196d2201f2fSdrahn }
197d2201f2fSdrahn 
198d2201f2fSdrahn void
_bfd_elf_strtab_clear_all_refs(struct elf_strtab_hash * tab)199*cf2f2c56Smiod _bfd_elf_strtab_clear_all_refs (struct elf_strtab_hash *tab)
200d2201f2fSdrahn {
201d2201f2fSdrahn   bfd_size_type idx;
202d2201f2fSdrahn 
203d2201f2fSdrahn   for (idx = 1; idx < tab->size; ++idx)
204d2201f2fSdrahn     tab->array[idx]->refcount = 0;
205d2201f2fSdrahn }
206d2201f2fSdrahn 
207d2201f2fSdrahn bfd_size_type
_bfd_elf_strtab_size(struct elf_strtab_hash * tab)208*cf2f2c56Smiod _bfd_elf_strtab_size (struct elf_strtab_hash *tab)
209d2201f2fSdrahn {
210d2201f2fSdrahn   return tab->sec_size ? tab->sec_size : tab->size;
211d2201f2fSdrahn }
212d2201f2fSdrahn 
213d2201f2fSdrahn bfd_size_type
_bfd_elf_strtab_offset(struct elf_strtab_hash * tab,bfd_size_type idx)214*cf2f2c56Smiod _bfd_elf_strtab_offset (struct elf_strtab_hash *tab, bfd_size_type idx)
215d2201f2fSdrahn {
216d2201f2fSdrahn   struct elf_strtab_hash_entry *entry;
217d2201f2fSdrahn 
218d2201f2fSdrahn   if (idx == 0)
219d2201f2fSdrahn     return 0;
220d2201f2fSdrahn   BFD_ASSERT (idx < tab->size);
221d2201f2fSdrahn   BFD_ASSERT (tab->sec_size);
222d2201f2fSdrahn   entry = tab->array[idx];
223d2201f2fSdrahn   BFD_ASSERT (entry->refcount > 0);
224d2201f2fSdrahn   entry->refcount--;
225d2201f2fSdrahn   return tab->array[idx]->u.index;
226d2201f2fSdrahn }
227d2201f2fSdrahn 
228d2201f2fSdrahn bfd_boolean
_bfd_elf_strtab_emit(register bfd * abfd,struct elf_strtab_hash * tab)229*cf2f2c56Smiod _bfd_elf_strtab_emit (register bfd *abfd, struct elf_strtab_hash *tab)
230d2201f2fSdrahn {
231d2201f2fSdrahn   bfd_size_type off = 1, i;
232d2201f2fSdrahn 
233d2201f2fSdrahn   if (bfd_bwrite ("", 1, abfd) != 1)
234d2201f2fSdrahn     return FALSE;
235d2201f2fSdrahn 
236d2201f2fSdrahn   for (i = 1; i < tab->size; ++i)
237d2201f2fSdrahn     {
238d2201f2fSdrahn       register const char *str;
239*cf2f2c56Smiod       register unsigned int len;
240d2201f2fSdrahn 
241d2201f2fSdrahn       BFD_ASSERT (tab->array[i]->refcount == 0);
242*cf2f2c56Smiod       len = tab->array[i]->len;
243*cf2f2c56Smiod       if ((int) len < 0)
244d2201f2fSdrahn 	continue;
245d2201f2fSdrahn 
246*cf2f2c56Smiod       str = tab->array[i]->root.string;
247*cf2f2c56Smiod       if (bfd_bwrite (str, len, abfd) != len)
248d2201f2fSdrahn 	return FALSE;
249d2201f2fSdrahn 
250d2201f2fSdrahn       off += len;
251d2201f2fSdrahn     }
252d2201f2fSdrahn 
253d2201f2fSdrahn   BFD_ASSERT (off == tab->sec_size);
254d2201f2fSdrahn   return TRUE;
255d2201f2fSdrahn }
256d2201f2fSdrahn 
257*cf2f2c56Smiod /* Compare two elf_strtab_hash_entry structures.  Called via qsort.  */
258d2201f2fSdrahn 
259d2201f2fSdrahn static int
strrevcmp(const void * a,const void * b)260*cf2f2c56Smiod strrevcmp (const void *a, const void *b)
261d2201f2fSdrahn {
262d2201f2fSdrahn   struct elf_strtab_hash_entry *A = *(struct elf_strtab_hash_entry **) a;
263d2201f2fSdrahn   struct elf_strtab_hash_entry *B = *(struct elf_strtab_hash_entry **) b;
264*cf2f2c56Smiod   unsigned int lenA = A->len;
265*cf2f2c56Smiod   unsigned int lenB = B->len;
266*cf2f2c56Smiod   const unsigned char *s = A->root.string + lenA - 1;
267*cf2f2c56Smiod   const unsigned char *t = B->root.string + lenB - 1;
268*cf2f2c56Smiod   int l = lenA < lenB ? lenA : lenB;
269d2201f2fSdrahn 
270*cf2f2c56Smiod   while (l)
271*cf2f2c56Smiod     {
272*cf2f2c56Smiod       if (*s != *t)
273*cf2f2c56Smiod 	return (int) *s - (int) *t;
274*cf2f2c56Smiod       s--;
275*cf2f2c56Smiod       t--;
276*cf2f2c56Smiod       l--;
277*cf2f2c56Smiod     }
278*cf2f2c56Smiod   return lenA - lenB;
279d2201f2fSdrahn }
280d2201f2fSdrahn 
281*cf2f2c56Smiod static inline int
is_suffix(const struct elf_strtab_hash_entry * A,const struct elf_strtab_hash_entry * B)282*cf2f2c56Smiod is_suffix (const struct elf_strtab_hash_entry *A,
283*cf2f2c56Smiod 	   const struct elf_strtab_hash_entry *B)
284d2201f2fSdrahn {
285d2201f2fSdrahn   if (A->len <= B->len)
286d2201f2fSdrahn     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
287d2201f2fSdrahn        not to be equal by the hash table.  */
288d2201f2fSdrahn     return 0;
289d2201f2fSdrahn 
290d2201f2fSdrahn   return memcmp (A->root.string + (A->len - B->len),
291*cf2f2c56Smiod 		 B->root.string, B->len - 1) == 0;
292d2201f2fSdrahn }
293d2201f2fSdrahn 
294d2201f2fSdrahn /* This function assigns final string table offsets for used strings,
295d2201f2fSdrahn    merging strings matching suffixes of longer strings if possible.  */
296d2201f2fSdrahn 
297d2201f2fSdrahn void
_bfd_elf_strtab_finalize(struct elf_strtab_hash * tab)298*cf2f2c56Smiod _bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
299d2201f2fSdrahn {
300*cf2f2c56Smiod   struct elf_strtab_hash_entry **array, **a, *e;
301d2201f2fSdrahn   bfd_size_type size, amt;
302d2201f2fSdrahn 
303d2201f2fSdrahn   /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
304d2201f2fSdrahn      a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
305d2201f2fSdrahn      Besides, indexing with a long long wouldn't give anything but extra
306d2201f2fSdrahn      cycles.  */
307d2201f2fSdrahn   size_t i;
308d2201f2fSdrahn 
309*cf2f2c56Smiod   /* Sort the strings by suffix and length.  */
310d2201f2fSdrahn   amt = tab->size * sizeof (struct elf_strtab_hash_entry *);
311*cf2f2c56Smiod   array = bfd_malloc (amt);
312d2201f2fSdrahn   if (array == NULL)
313d2201f2fSdrahn     goto alloc_failure;
314d2201f2fSdrahn 
315d2201f2fSdrahn   for (i = 1, a = array; i < tab->size; ++i)
316*cf2f2c56Smiod     {
317*cf2f2c56Smiod       e = tab->array[i];
318*cf2f2c56Smiod       if (e->refcount)
319*cf2f2c56Smiod 	{
320*cf2f2c56Smiod 	  *a++ = e;
321*cf2f2c56Smiod 	  /* Adjust the length to not include the zero terminator.  */
322*cf2f2c56Smiod 	  e->len -= 1;
323*cf2f2c56Smiod 	}
324d2201f2fSdrahn       else
325*cf2f2c56Smiod 	e->len = 0;
326*cf2f2c56Smiod     }
327d2201f2fSdrahn 
328d2201f2fSdrahn   size = a - array;
329*cf2f2c56Smiod   if (size != 0)
330d2201f2fSdrahn     {
331*cf2f2c56Smiod       qsort (array, size, sizeof (struct elf_strtab_hash_entry *), strrevcmp);
332d2201f2fSdrahn 
333*cf2f2c56Smiod       /* Loop over the sorted array and merge suffixes.  Start from the
334*cf2f2c56Smiod 	 end because we want eg.
335d2201f2fSdrahn 
336*cf2f2c56Smiod 	 s1 -> "d"
337*cf2f2c56Smiod 	 s2 -> "bcd"
338*cf2f2c56Smiod 	 s3 -> "abcd"
339*cf2f2c56Smiod 
340*cf2f2c56Smiod 	 to end up as
341*cf2f2c56Smiod 
342*cf2f2c56Smiod 	 s3 -> "abcd"
343*cf2f2c56Smiod 	 s2 _____^
344*cf2f2c56Smiod 	 s1 _______^
345*cf2f2c56Smiod 
346*cf2f2c56Smiod 	 ie. we don't want s1 pointing into the old s2.  */
347*cf2f2c56Smiod       e = *--a;
348*cf2f2c56Smiod       e->len += 1;
349*cf2f2c56Smiod       while (--a >= array)
350*cf2f2c56Smiod 	{
351*cf2f2c56Smiod 	  struct elf_strtab_hash_entry *cmp = *a;
352*cf2f2c56Smiod 
353*cf2f2c56Smiod 	  cmp->len += 1;
354*cf2f2c56Smiod 	  if (is_suffix (e, cmp))
355*cf2f2c56Smiod 	    {
356*cf2f2c56Smiod 	      cmp->u.suffix = e;
357*cf2f2c56Smiod 	      cmp->len = -cmp->len;
358d2201f2fSdrahn 	    }
359d2201f2fSdrahn 	  else
360*cf2f2c56Smiod 	    e = cmp;
361d2201f2fSdrahn 	}
362d2201f2fSdrahn     }
363d2201f2fSdrahn 
364d2201f2fSdrahn alloc_failure:
365d2201f2fSdrahn   if (array)
366d2201f2fSdrahn     free (array);
367d2201f2fSdrahn 
368*cf2f2c56Smiod   /* Assign positions to the strings we want to keep.  */
369d2201f2fSdrahn   size = 1;
370d2201f2fSdrahn   for (i = 1; i < tab->size; ++i)
371d2201f2fSdrahn     {
372d2201f2fSdrahn       e = tab->array[i];
373*cf2f2c56Smiod       if (e->refcount && e->len > 0)
374d2201f2fSdrahn 	{
375d2201f2fSdrahn 	  e->u.index = size;
376d2201f2fSdrahn 	  size += e->len;
377d2201f2fSdrahn 	}
378d2201f2fSdrahn     }
379d2201f2fSdrahn 
380d2201f2fSdrahn   tab->sec_size = size;
381d2201f2fSdrahn 
382*cf2f2c56Smiod   /* Adjust the rest.  */
383d2201f2fSdrahn   for (i = 1; i < tab->size; ++i)
384d2201f2fSdrahn     {
385d2201f2fSdrahn       e = tab->array[i];
386*cf2f2c56Smiod       if (e->refcount && e->len < 0)
387*cf2f2c56Smiod 	e->u.index = e->u.suffix->u.index + (e->u.suffix->len + e->len);
388d2201f2fSdrahn     }
389d2201f2fSdrahn }
390