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