xref: /netbsd/external/gpl3/gdb/dist/bfd/elf-strtab.c (revision 1424dfb3)
1377e23a2Schristos /* ELF strtab with GC and suffix merging support.
2*1424dfb3Schristos    Copyright (C) 2001-2020 Free Software Foundation, Inc.
3377e23a2Schristos    Written by Jakub Jelinek <jakub@redhat.com>.
4377e23a2Schristos 
5377e23a2Schristos    This file is part of BFD, the Binary File Descriptor library.
6377e23a2Schristos 
7377e23a2Schristos    This program is free software; you can redistribute it and/or modify
8377e23a2Schristos    it under the terms of the GNU General Public License as published by
9377e23a2Schristos    the Free Software Foundation; either version 3 of the License, or
10377e23a2Schristos    (at your option) any later version.
11377e23a2Schristos 
12377e23a2Schristos    This program is distributed in the hope that it will be useful,
13377e23a2Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14377e23a2Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15377e23a2Schristos    GNU General Public License for more details.
16377e23a2Schristos 
17377e23a2Schristos    You should have received a copy of the GNU General Public License
18377e23a2Schristos    along with this program; if not, write to the Free Software
19377e23a2Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20377e23a2Schristos    MA 02110-1301, USA.  */
21377e23a2Schristos 
22377e23a2Schristos #include "sysdep.h"
23377e23a2Schristos #include "bfd.h"
24377e23a2Schristos #include "libbfd.h"
25377e23a2Schristos #include "elf-bfd.h"
26377e23a2Schristos #include "hashtab.h"
27377e23a2Schristos #include "libiberty.h"
28377e23a2Schristos 
29377e23a2Schristos /* An entry in the strtab hash table.  */
30377e23a2Schristos 
31377e23a2Schristos struct elf_strtab_hash_entry
32377e23a2Schristos {
33377e23a2Schristos   struct bfd_hash_entry root;
34377e23a2Schristos   /* Length of this entry.  This includes the zero terminator.  */
35377e23a2Schristos   int len;
36377e23a2Schristos   unsigned int refcount;
37377e23a2Schristos   union {
38377e23a2Schristos     /* Index within the merged section.  */
39377e23a2Schristos     bfd_size_type index;
40377e23a2Schristos     /* Entry this is a suffix of (if len < 0).  */
41377e23a2Schristos     struct elf_strtab_hash_entry *suffix;
42377e23a2Schristos   } u;
43377e23a2Schristos };
44377e23a2Schristos 
45377e23a2Schristos /* The strtab hash table.  */
46377e23a2Schristos 
47377e23a2Schristos struct elf_strtab_hash
48377e23a2Schristos {
49377e23a2Schristos   struct bfd_hash_table table;
50377e23a2Schristos   /* Next available index.  */
51c03b94e9Schristos   size_t size;
52377e23a2Schristos   /* Number of array entries alloced.  */
53c03b94e9Schristos   size_t alloced;
54377e23a2Schristos   /* Final strtab size.  */
55377e23a2Schristos   bfd_size_type sec_size;
56377e23a2Schristos   /* Array of pointers to strtab entries.  */
57377e23a2Schristos   struct elf_strtab_hash_entry **array;
58377e23a2Schristos };
59377e23a2Schristos 
60377e23a2Schristos /* Routine to create an entry in a section merge hashtab.  */
61377e23a2Schristos 
62377e23a2Schristos static struct bfd_hash_entry *
elf_strtab_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)63377e23a2Schristos elf_strtab_hash_newfunc (struct bfd_hash_entry *entry,
64377e23a2Schristos 			 struct bfd_hash_table *table,
65377e23a2Schristos 			 const char *string)
66377e23a2Schristos {
67377e23a2Schristos   /* Allocate the structure if it has not already been allocated by a
68377e23a2Schristos      subclass.  */
69377e23a2Schristos   if (entry == NULL)
70377e23a2Schristos     entry = (struct bfd_hash_entry *)
71377e23a2Schristos 	bfd_hash_allocate (table, sizeof (struct elf_strtab_hash_entry));
72377e23a2Schristos   if (entry == NULL)
73377e23a2Schristos     return NULL;
74377e23a2Schristos 
75377e23a2Schristos   /* Call the allocation method of the superclass.  */
76377e23a2Schristos   entry = bfd_hash_newfunc (entry, table, string);
77377e23a2Schristos 
78377e23a2Schristos   if (entry)
79377e23a2Schristos     {
80377e23a2Schristos       /* Initialize the local fields.  */
81377e23a2Schristos       struct elf_strtab_hash_entry *ret;
82377e23a2Schristos 
83377e23a2Schristos       ret = (struct elf_strtab_hash_entry *) entry;
84377e23a2Schristos       ret->u.index = -1;
85377e23a2Schristos       ret->refcount = 0;
86377e23a2Schristos       ret->len = 0;
87377e23a2Schristos     }
88377e23a2Schristos 
89377e23a2Schristos   return entry;
90377e23a2Schristos }
91377e23a2Schristos 
92377e23a2Schristos /* Create a new hash table.  */
93377e23a2Schristos 
94377e23a2Schristos struct elf_strtab_hash *
_bfd_elf_strtab_init(void)95377e23a2Schristos _bfd_elf_strtab_init (void)
96377e23a2Schristos {
97377e23a2Schristos   struct elf_strtab_hash *table;
98*1424dfb3Schristos   size_t amt = sizeof (struct elf_strtab_hash);
99377e23a2Schristos 
100377e23a2Schristos   table = (struct elf_strtab_hash *) bfd_malloc (amt);
101377e23a2Schristos   if (table == NULL)
102377e23a2Schristos     return NULL;
103377e23a2Schristos 
104377e23a2Schristos   if (!bfd_hash_table_init (&table->table, elf_strtab_hash_newfunc,
105377e23a2Schristos 			    sizeof (struct elf_strtab_hash_entry)))
106377e23a2Schristos     {
107377e23a2Schristos       free (table);
108377e23a2Schristos       return NULL;
109377e23a2Schristos     }
110377e23a2Schristos 
111377e23a2Schristos   table->sec_size = 0;
112377e23a2Schristos   table->size = 1;
113377e23a2Schristos   table->alloced = 64;
114377e23a2Schristos   amt = sizeof (struct elf_strtab_hasn_entry *);
115c03b94e9Schristos   table->array = ((struct elf_strtab_hash_entry **)
116c03b94e9Schristos 		  bfd_malloc (table->alloced * amt));
117377e23a2Schristos   if (table->array == NULL)
118377e23a2Schristos     {
119377e23a2Schristos       free (table);
120377e23a2Schristos       return NULL;
121377e23a2Schristos     }
122377e23a2Schristos 
123377e23a2Schristos   table->array[0] = NULL;
124377e23a2Schristos 
125377e23a2Schristos   return table;
126377e23a2Schristos }
127377e23a2Schristos 
128377e23a2Schristos /* Free a strtab.  */
129377e23a2Schristos 
130377e23a2Schristos void
_bfd_elf_strtab_free(struct elf_strtab_hash * tab)131377e23a2Schristos _bfd_elf_strtab_free (struct elf_strtab_hash *tab)
132377e23a2Schristos {
133377e23a2Schristos   bfd_hash_table_free (&tab->table);
134377e23a2Schristos   free (tab->array);
135377e23a2Schristos   free (tab);
136377e23a2Schristos }
137377e23a2Schristos 
138377e23a2Schristos /* Get the index of an entity in a hash table, adding it if it is not
139377e23a2Schristos    already present.  */
140377e23a2Schristos 
141c03b94e9Schristos size_t
_bfd_elf_strtab_add(struct elf_strtab_hash * tab,const char * str,bfd_boolean copy)142377e23a2Schristos _bfd_elf_strtab_add (struct elf_strtab_hash *tab,
143377e23a2Schristos 		     const char *str,
144377e23a2Schristos 		     bfd_boolean copy)
145377e23a2Schristos {
146377e23a2Schristos   register struct elf_strtab_hash_entry *entry;
147377e23a2Schristos 
148377e23a2Schristos   /* We handle this specially, since we don't want to do refcounting
149377e23a2Schristos      on it.  */
150377e23a2Schristos   if (*str == '\0')
151377e23a2Schristos     return 0;
152377e23a2Schristos 
153377e23a2Schristos   BFD_ASSERT (tab->sec_size == 0);
154377e23a2Schristos   entry = (struct elf_strtab_hash_entry *)
155377e23a2Schristos 	  bfd_hash_lookup (&tab->table, str, TRUE, copy);
156377e23a2Schristos 
157377e23a2Schristos   if (entry == NULL)
158c03b94e9Schristos     return (size_t) -1;
159377e23a2Schristos 
160377e23a2Schristos   entry->refcount++;
161377e23a2Schristos   if (entry->len == 0)
162377e23a2Schristos     {
163377e23a2Schristos       entry->len = strlen (str) + 1;
164377e23a2Schristos       /* 2G strings lose.  */
165377e23a2Schristos       BFD_ASSERT (entry->len > 0);
166377e23a2Schristos       if (tab->size == tab->alloced)
167377e23a2Schristos 	{
168377e23a2Schristos 	  bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *);
169377e23a2Schristos 	  tab->alloced *= 2;
170377e23a2Schristos 	  tab->array = (struct elf_strtab_hash_entry **)
171377e23a2Schristos 	      bfd_realloc_or_free (tab->array, tab->alloced * amt);
172377e23a2Schristos 	  if (tab->array == NULL)
173c03b94e9Schristos 	    return (size_t) -1;
174377e23a2Schristos 	}
175377e23a2Schristos 
176377e23a2Schristos       entry->u.index = tab->size++;
177377e23a2Schristos       tab->array[entry->u.index] = entry;
178377e23a2Schristos     }
179377e23a2Schristos   return entry->u.index;
180377e23a2Schristos }
181377e23a2Schristos 
182377e23a2Schristos void
_bfd_elf_strtab_addref(struct elf_strtab_hash * tab,size_t idx)183c03b94e9Schristos _bfd_elf_strtab_addref (struct elf_strtab_hash *tab, size_t idx)
184377e23a2Schristos {
185c03b94e9Schristos   if (idx == 0 || idx == (size_t) -1)
186377e23a2Schristos     return;
187377e23a2Schristos   BFD_ASSERT (tab->sec_size == 0);
188377e23a2Schristos   BFD_ASSERT (idx < tab->size);
189377e23a2Schristos   ++tab->array[idx]->refcount;
190377e23a2Schristos }
191377e23a2Schristos 
192377e23a2Schristos void
_bfd_elf_strtab_delref(struct elf_strtab_hash * tab,size_t idx)193c03b94e9Schristos _bfd_elf_strtab_delref (struct elf_strtab_hash *tab, size_t idx)
194377e23a2Schristos {
195c03b94e9Schristos   if (idx == 0 || idx == (size_t) -1)
196377e23a2Schristos     return;
197377e23a2Schristos   BFD_ASSERT (tab->sec_size == 0);
198377e23a2Schristos   BFD_ASSERT (idx < tab->size);
199377e23a2Schristos   BFD_ASSERT (tab->array[idx]->refcount > 0);
200377e23a2Schristos   --tab->array[idx]->refcount;
201377e23a2Schristos }
202377e23a2Schristos 
20348596154Schristos unsigned int
_bfd_elf_strtab_refcount(struct elf_strtab_hash * tab,size_t idx)204c03b94e9Schristos _bfd_elf_strtab_refcount (struct elf_strtab_hash *tab, size_t idx)
20548596154Schristos {
20648596154Schristos   return tab->array[idx]->refcount;
20748596154Schristos }
20848596154Schristos 
209377e23a2Schristos void
_bfd_elf_strtab_clear_all_refs(struct elf_strtab_hash * tab)210377e23a2Schristos _bfd_elf_strtab_clear_all_refs (struct elf_strtab_hash *tab)
211377e23a2Schristos {
212c03b94e9Schristos   size_t idx;
213377e23a2Schristos 
21448596154Schristos   for (idx = 1; idx < tab->size; idx++)
215377e23a2Schristos     tab->array[idx]->refcount = 0;
216377e23a2Schristos }
217377e23a2Schristos 
218c03b94e9Schristos /* Save strtab refcounts prior to adding --as-needed library.  */
219c03b94e9Schristos 
220c03b94e9Schristos struct strtab_save
22148596154Schristos {
222c03b94e9Schristos   size_t size;
223c03b94e9Schristos   unsigned int refcount[1];
224c03b94e9Schristos };
225c03b94e9Schristos 
226c03b94e9Schristos void *
_bfd_elf_strtab_save(struct elf_strtab_hash * tab)227c03b94e9Schristos _bfd_elf_strtab_save (struct elf_strtab_hash *tab)
228c03b94e9Schristos {
229c03b94e9Schristos   struct strtab_save *save;
230c03b94e9Schristos   size_t idx, size;
231c03b94e9Schristos 
232c03b94e9Schristos   size = sizeof (*save) + (tab->size - 1) * sizeof (save->refcount[0]);
233c03b94e9Schristos   save = bfd_malloc (size);
234c03b94e9Schristos   if (save == NULL)
235c03b94e9Schristos     return save;
236c03b94e9Schristos 
237c03b94e9Schristos   save->size = tab->size;
238c03b94e9Schristos   for (idx = 1; idx < tab->size; idx++)
239c03b94e9Schristos     save->refcount[idx] = tab->array[idx]->refcount;
240c03b94e9Schristos   return save;
241c03b94e9Schristos }
242c03b94e9Schristos 
243c03b94e9Schristos /* Restore strtab refcounts on finding --as-needed library not needed.  */
244c03b94e9Schristos 
245c03b94e9Schristos void
_bfd_elf_strtab_restore(struct elf_strtab_hash * tab,void * buf)246c03b94e9Schristos _bfd_elf_strtab_restore (struct elf_strtab_hash *tab, void *buf)
247c03b94e9Schristos {
248*1424dfb3Schristos   size_t idx, curr_size = tab->size, save_size;
249c03b94e9Schristos   struct strtab_save *save = (struct strtab_save *) buf;
25048596154Schristos 
25148596154Schristos   BFD_ASSERT (tab->sec_size == 0);
252*1424dfb3Schristos   save_size = 1;
253*1424dfb3Schristos   if (save != NULL)
254*1424dfb3Schristos     save_size = save->size;
255*1424dfb3Schristos   BFD_ASSERT (save_size <= curr_size);
256*1424dfb3Schristos   tab->size = save_size;
257*1424dfb3Schristos   for (idx = 1; idx < save_size; ++idx)
258c03b94e9Schristos     tab->array[idx]->refcount = save->refcount[idx];
259c03b94e9Schristos 
26048596154Schristos   for (; idx < curr_size; ++idx)
26148596154Schristos     {
26248596154Schristos       /* We don't remove entries from the hash table, just set their
26348596154Schristos 	 REFCOUNT to zero.  Setting LEN zero will result in the size
26448596154Schristos 	 growing if the entry is added again.  See _bfd_elf_strtab_add.  */
26548596154Schristos       tab->array[idx]->refcount = 0;
26648596154Schristos       tab->array[idx]->len = 0;
26748596154Schristos     }
26848596154Schristos }
26948596154Schristos 
270377e23a2Schristos bfd_size_type
_bfd_elf_strtab_size(struct elf_strtab_hash * tab)271377e23a2Schristos _bfd_elf_strtab_size (struct elf_strtab_hash *tab)
272377e23a2Schristos {
273377e23a2Schristos   return tab->sec_size ? tab->sec_size : tab->size;
274377e23a2Schristos }
275377e23a2Schristos 
276377e23a2Schristos bfd_size_type
_bfd_elf_strtab_len(struct elf_strtab_hash * tab)277*1424dfb3Schristos _bfd_elf_strtab_len (struct elf_strtab_hash *tab)
278*1424dfb3Schristos {
279*1424dfb3Schristos   return tab->size;
280*1424dfb3Schristos }
281*1424dfb3Schristos 
282*1424dfb3Schristos bfd_size_type
_bfd_elf_strtab_offset(struct elf_strtab_hash * tab,size_t idx)283c03b94e9Schristos _bfd_elf_strtab_offset (struct elf_strtab_hash *tab, size_t idx)
284377e23a2Schristos {
285377e23a2Schristos   struct elf_strtab_hash_entry *entry;
286377e23a2Schristos 
287377e23a2Schristos   if (idx == 0)
288377e23a2Schristos     return 0;
289377e23a2Schristos   BFD_ASSERT (idx < tab->size);
290377e23a2Schristos   BFD_ASSERT (tab->sec_size);
291377e23a2Schristos   entry = tab->array[idx];
292377e23a2Schristos   BFD_ASSERT (entry->refcount > 0);
293377e23a2Schristos   entry->refcount--;
294377e23a2Schristos   return tab->array[idx]->u.index;
295377e23a2Schristos }
296377e23a2Schristos 
297*1424dfb3Schristos const char *
_bfd_elf_strtab_str(struct elf_strtab_hash * tab,size_t idx,bfd_size_type * offset)298*1424dfb3Schristos _bfd_elf_strtab_str (struct elf_strtab_hash *tab, size_t idx,
299*1424dfb3Schristos 		     bfd_size_type *offset)
300*1424dfb3Schristos {
301*1424dfb3Schristos   if (idx == 0)
302*1424dfb3Schristos     return 0;
303*1424dfb3Schristos   BFD_ASSERT (idx < tab->size);
304*1424dfb3Schristos   BFD_ASSERT (tab->sec_size);
305*1424dfb3Schristos   if (offset)
306*1424dfb3Schristos     *offset = tab->array[idx]->u.index;
307*1424dfb3Schristos   return tab->array[idx]->root.string;
308*1424dfb3Schristos }
309*1424dfb3Schristos 
310377e23a2Schristos bfd_boolean
_bfd_elf_strtab_emit(register bfd * abfd,struct elf_strtab_hash * tab)311377e23a2Schristos _bfd_elf_strtab_emit (register bfd *abfd, struct elf_strtab_hash *tab)
312377e23a2Schristos {
313c03b94e9Schristos   bfd_size_type off = 1;
314c03b94e9Schristos   size_t i;
315377e23a2Schristos 
316377e23a2Schristos   if (bfd_bwrite ("", 1, abfd) != 1)
317377e23a2Schristos     return FALSE;
318377e23a2Schristos 
319377e23a2Schristos   for (i = 1; i < tab->size; ++i)
320377e23a2Schristos     {
321377e23a2Schristos       register const char *str;
322377e23a2Schristos       register unsigned int len;
323377e23a2Schristos 
324377e23a2Schristos       BFD_ASSERT (tab->array[i]->refcount == 0);
325377e23a2Schristos       len = tab->array[i]->len;
326377e23a2Schristos       if ((int) len < 0)
327377e23a2Schristos 	continue;
328377e23a2Schristos 
329377e23a2Schristos       str = tab->array[i]->root.string;
330377e23a2Schristos       if (bfd_bwrite (str, len, abfd) != len)
331377e23a2Schristos 	return FALSE;
332377e23a2Schristos 
333377e23a2Schristos       off += len;
334377e23a2Schristos     }
335377e23a2Schristos 
336377e23a2Schristos   BFD_ASSERT (off == tab->sec_size);
337377e23a2Schristos   return TRUE;
338377e23a2Schristos }
339377e23a2Schristos 
340*1424dfb3Schristos /* Compare two elf_strtab_hash_entry structures.  Called via qsort.
341*1424dfb3Schristos    Won't ever return zero as all entries differ, so there is no issue
342*1424dfb3Schristos    with qsort stability here.  */
343377e23a2Schristos 
344377e23a2Schristos static int
strrevcmp(const void * a,const void * b)345377e23a2Schristos strrevcmp (const void *a, const void *b)
346377e23a2Schristos {
347377e23a2Schristos   struct elf_strtab_hash_entry *A = *(struct elf_strtab_hash_entry **) a;
348377e23a2Schristos   struct elf_strtab_hash_entry *B = *(struct elf_strtab_hash_entry **) b;
349377e23a2Schristos   unsigned int lenA = A->len;
350377e23a2Schristos   unsigned int lenB = B->len;
351377e23a2Schristos   const unsigned char *s = (const unsigned char *) A->root.string + lenA - 1;
352377e23a2Schristos   const unsigned char *t = (const unsigned char *) B->root.string + lenB - 1;
353377e23a2Schristos   int l = lenA < lenB ? lenA : lenB;
354377e23a2Schristos 
355377e23a2Schristos   while (l)
356377e23a2Schristos     {
357377e23a2Schristos       if (*s != *t)
358377e23a2Schristos 	return (int) *s - (int) *t;
359377e23a2Schristos       s--;
360377e23a2Schristos       t--;
361377e23a2Schristos       l--;
362377e23a2Schristos     }
363377e23a2Schristos   return lenA - lenB;
364377e23a2Schristos }
365377e23a2Schristos 
366377e23a2Schristos static inline int
is_suffix(const struct elf_strtab_hash_entry * A,const struct elf_strtab_hash_entry * B)367377e23a2Schristos is_suffix (const struct elf_strtab_hash_entry *A,
368377e23a2Schristos 	   const struct elf_strtab_hash_entry *B)
369377e23a2Schristos {
370377e23a2Schristos   if (A->len <= B->len)
371377e23a2Schristos     /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
372377e23a2Schristos        not to be equal by the hash table.  */
373377e23a2Schristos     return 0;
374377e23a2Schristos 
375377e23a2Schristos   return memcmp (A->root.string + (A->len - B->len),
376377e23a2Schristos 		 B->root.string, B->len - 1) == 0;
377377e23a2Schristos }
378377e23a2Schristos 
379377e23a2Schristos /* This function assigns final string table offsets for used strings,
380377e23a2Schristos    merging strings matching suffixes of longer strings if possible.  */
381377e23a2Schristos 
382377e23a2Schristos void
_bfd_elf_strtab_finalize(struct elf_strtab_hash * tab)383377e23a2Schristos _bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
384377e23a2Schristos {
385377e23a2Schristos   struct elf_strtab_hash_entry **array, **a, *e;
386c03b94e9Schristos   bfd_size_type amt, sec_size;
387c03b94e9Schristos   size_t size, i;
388377e23a2Schristos 
389377e23a2Schristos   /* Sort the strings by suffix and length.  */
390c03b94e9Schristos   amt = tab->size;
391c03b94e9Schristos   amt *= sizeof (struct elf_strtab_hash_entry *);
392377e23a2Schristos   array = (struct elf_strtab_hash_entry **) bfd_malloc (amt);
393377e23a2Schristos   if (array == NULL)
394377e23a2Schristos     goto alloc_failure;
395377e23a2Schristos 
396377e23a2Schristos   for (i = 1, a = array; i < tab->size; ++i)
397377e23a2Schristos     {
398377e23a2Schristos       e = tab->array[i];
399377e23a2Schristos       if (e->refcount)
400377e23a2Schristos 	{
401377e23a2Schristos 	  *a++ = e;
402377e23a2Schristos 	  /* Adjust the length to not include the zero terminator.  */
403377e23a2Schristos 	  e->len -= 1;
404377e23a2Schristos 	}
405377e23a2Schristos       else
406377e23a2Schristos 	e->len = 0;
407377e23a2Schristos     }
408377e23a2Schristos 
409377e23a2Schristos   size = a - array;
410377e23a2Schristos   if (size != 0)
411377e23a2Schristos     {
412377e23a2Schristos       qsort (array, size, sizeof (struct elf_strtab_hash_entry *), strrevcmp);
413377e23a2Schristos 
414377e23a2Schristos       /* Loop over the sorted array and merge suffixes.  Start from the
415377e23a2Schristos 	 end because we want eg.
416377e23a2Schristos 
417377e23a2Schristos 	 s1 -> "d"
418377e23a2Schristos 	 s2 -> "bcd"
419377e23a2Schristos 	 s3 -> "abcd"
420377e23a2Schristos 
421377e23a2Schristos 	 to end up as
422377e23a2Schristos 
423377e23a2Schristos 	 s3 -> "abcd"
424377e23a2Schristos 	 s2 _____^
425377e23a2Schristos 	 s1 _______^
426377e23a2Schristos 
427377e23a2Schristos 	 ie. we don't want s1 pointing into the old s2.  */
428377e23a2Schristos       e = *--a;
429377e23a2Schristos       e->len += 1;
430377e23a2Schristos       while (--a >= array)
431377e23a2Schristos 	{
432377e23a2Schristos 	  struct elf_strtab_hash_entry *cmp = *a;
433377e23a2Schristos 
434377e23a2Schristos 	  cmp->len += 1;
435377e23a2Schristos 	  if (is_suffix (e, cmp))
436377e23a2Schristos 	    {
437377e23a2Schristos 	      cmp->u.suffix = e;
438377e23a2Schristos 	      cmp->len = -cmp->len;
439377e23a2Schristos 	    }
440377e23a2Schristos 	  else
441377e23a2Schristos 	    e = cmp;
442377e23a2Schristos 	}
443377e23a2Schristos     }
444377e23a2Schristos 
445377e23a2Schristos  alloc_failure:
446377e23a2Schristos   free (array);
447377e23a2Schristos 
448377e23a2Schristos   /* Assign positions to the strings we want to keep.  */
449c03b94e9Schristos   sec_size = 1;
450377e23a2Schristos   for (i = 1; i < tab->size; ++i)
451377e23a2Schristos     {
452377e23a2Schristos       e = tab->array[i];
453377e23a2Schristos       if (e->refcount && e->len > 0)
454377e23a2Schristos 	{
455c03b94e9Schristos 	  e->u.index = sec_size;
456c03b94e9Schristos 	  sec_size += e->len;
457377e23a2Schristos 	}
458377e23a2Schristos     }
459377e23a2Schristos 
460c03b94e9Schristos   tab->sec_size = sec_size;
461377e23a2Schristos 
462377e23a2Schristos   /* Adjust the rest.  */
463377e23a2Schristos   for (i = 1; i < tab->size; ++i)
464377e23a2Schristos     {
465377e23a2Schristos       e = tab->array[i];
466377e23a2Schristos       if (e->refcount && e->len < 0)
467377e23a2Schristos 	e->u.index = e->u.suffix->u.index + (e->u.suffix->len + e->len);
468377e23a2Schristos     }
469377e23a2Schristos }
470