1 /* ELF strtab with GC and suffix merging support.
2 Copyright (C) 2001-2024 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
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. */
36 unsigned int refcount
;
38 /* Index within the merged section. */
40 /* Entry this is a suffix of (if len < 0). */
41 struct elf_strtab_hash_entry
*suffix
;
45 /* The strtab hash table. */
47 struct elf_strtab_hash
49 struct bfd_hash_table table
;
50 /* Next available index. */
52 /* Number of array entries 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
,
67 /* Allocate the structure if it has not already been allocated by a
70 entry
= (struct bfd_hash_entry
*)
71 bfd_hash_allocate (table
, sizeof (struct elf_strtab_hash_entry
));
75 /* Call the allocation method of the superclass. */
76 entry
= bfd_hash_newfunc (entry
, table
, string
);
80 /* Initialize the local fields. */
81 struct elf_strtab_hash_entry
*ret
;
83 ret
= (struct elf_strtab_hash_entry
*) entry
;
92 /* Create a new hash table. */
94 struct elf_strtab_hash
*
95 _bfd_elf_strtab_init (void)
97 struct elf_strtab_hash
*table
;
98 size_t amt
= sizeof (struct elf_strtab_hash
);
100 table
= (struct elf_strtab_hash
*) bfd_malloc (amt
);
104 if (!bfd_hash_table_init (&table
->table
, elf_strtab_hash_newfunc
,
105 sizeof (struct elf_strtab_hash_entry
)))
114 amt
= sizeof (struct elf_strtab_hasn_entry
*);
115 table
->array
= ((struct elf_strtab_hash_entry
**)
116 bfd_malloc (table
->alloced
* amt
));
117 if (table
->array
== NULL
)
119 bfd_hash_table_free (&table
->table
);
124 table
->array
[0] = NULL
;
132 _bfd_elf_strtab_free (struct elf_strtab_hash
*tab
)
134 bfd_hash_table_free (&tab
->table
);
139 /* Get the index of an entity in a hash table, adding it if it is not
143 _bfd_elf_strtab_add (struct elf_strtab_hash
*tab
,
147 register struct elf_strtab_hash_entry
*entry
;
149 /* We handle this specially, since we don't want to do refcounting
154 BFD_ASSERT (tab
->sec_size
== 0);
155 entry
= (struct elf_strtab_hash_entry
*)
156 bfd_hash_lookup (&tab
->table
, str
, true, copy
);
164 entry
->len
= strlen (str
) + 1;
165 /* 2G strings lose. */
166 BFD_ASSERT (entry
->len
> 0);
167 if (tab
->size
== tab
->alloced
)
169 bfd_size_type amt
= sizeof (struct elf_strtab_hash_entry
*);
171 tab
->array
= (struct elf_strtab_hash_entry
**)
172 bfd_realloc_or_free (tab
->array
, tab
->alloced
* amt
);
173 if (tab
->array
== NULL
)
177 entry
->u
.index
= tab
->size
++;
178 tab
->array
[entry
->u
.index
] = entry
;
180 return entry
->u
.index
;
184 _bfd_elf_strtab_addref (struct elf_strtab_hash
*tab
, size_t idx
)
186 if (idx
== 0 || idx
== (size_t) -1)
188 BFD_ASSERT (tab
->sec_size
== 0);
189 BFD_ASSERT (idx
< tab
->size
);
190 ++tab
->array
[idx
]->refcount
;
194 _bfd_elf_strtab_delref (struct elf_strtab_hash
*tab
, size_t idx
)
196 if (idx
== 0 || idx
== (size_t) -1)
198 BFD_ASSERT (tab
->sec_size
== 0);
199 BFD_ASSERT (idx
< tab
->size
);
200 BFD_ASSERT (tab
->array
[idx
]->refcount
> 0);
201 --tab
->array
[idx
]->refcount
;
205 _bfd_elf_strtab_refcount (struct elf_strtab_hash
*tab
, size_t idx
)
207 return tab
->array
[idx
]->refcount
;
211 _bfd_elf_strtab_clear_all_refs (struct elf_strtab_hash
*tab
)
215 for (idx
= 1; idx
< tab
->size
; idx
++)
216 tab
->array
[idx
]->refcount
= 0;
219 /* Save strtab refcounts prior to adding --as-needed library. */
224 unsigned int refcount
[1];
228 _bfd_elf_strtab_save (struct elf_strtab_hash
*tab
)
230 struct strtab_save
*save
;
233 size
= sizeof (*save
) + (tab
->size
- 1) * sizeof (save
->refcount
[0]);
234 save
= bfd_malloc (size
);
238 save
->size
= tab
->size
;
239 for (idx
= 1; idx
< tab
->size
; idx
++)
240 save
->refcount
[idx
] = tab
->array
[idx
]->refcount
;
244 /* Restore strtab refcounts on finding --as-needed library not needed. */
247 _bfd_elf_strtab_restore (struct elf_strtab_hash
*tab
, void *buf
)
249 size_t idx
, curr_size
= tab
->size
, save_size
;
250 struct strtab_save
*save
= (struct strtab_save
*) buf
;
252 BFD_ASSERT (tab
->sec_size
== 0);
255 save_size
= save
->size
;
256 BFD_ASSERT (save_size
<= curr_size
);
257 tab
->size
= save_size
;
258 for (idx
= 1; idx
< save_size
; ++idx
)
259 tab
->array
[idx
]->refcount
= save
->refcount
[idx
];
261 for (; idx
< curr_size
; ++idx
)
263 /* We don't remove entries from the hash table, just set their
264 REFCOUNT to zero. Setting LEN zero will result in the size
265 growing if the entry is added again. See _bfd_elf_strtab_add. */
266 tab
->array
[idx
]->refcount
= 0;
267 tab
->array
[idx
]->len
= 0;
272 _bfd_elf_strtab_size (struct elf_strtab_hash
*tab
)
274 return tab
->sec_size
? tab
->sec_size
: tab
->size
;
278 _bfd_elf_strtab_len (struct elf_strtab_hash
*tab
)
284 _bfd_elf_strtab_offset (struct elf_strtab_hash
*tab
, size_t idx
)
286 struct elf_strtab_hash_entry
*entry
;
290 BFD_ASSERT (idx
< tab
->size
);
291 BFD_ASSERT (tab
->sec_size
);
292 entry
= tab
->array
[idx
];
293 BFD_ASSERT (entry
->refcount
> 0);
295 return tab
->array
[idx
]->u
.index
;
299 _bfd_elf_strtab_str (struct elf_strtab_hash
*tab
, size_t idx
,
300 bfd_size_type
*offset
)
304 BFD_ASSERT (idx
< tab
->size
);
305 BFD_ASSERT (tab
->sec_size
);
306 if (tab
->array
[idx
]->refcount
== 0)
309 *offset
= tab
->array
[idx
]->u
.index
;
310 return tab
->array
[idx
]->root
.string
;
314 _bfd_elf_strtab_emit (register bfd
*abfd
, struct elf_strtab_hash
*tab
)
316 bfd_size_type off
= 1;
319 if (bfd_write ("", 1, abfd
) != 1)
322 for (i
= 1; i
< tab
->size
; ++i
)
324 register const char *str
;
325 register unsigned int len
;
327 BFD_ASSERT (tab
->array
[i
]->refcount
== 0);
328 len
= tab
->array
[i
]->len
;
332 str
= tab
->array
[i
]->root
.string
;
333 if (bfd_write (str
, len
, abfd
) != len
)
339 BFD_ASSERT (off
== tab
->sec_size
);
343 /* Compare two elf_strtab_hash_entry structures. Called via qsort.
344 Won't ever return zero as all entries differ, so there is no issue
345 with qsort stability here. */
348 strrevcmp (const void *a
, const void *b
)
350 struct elf_strtab_hash_entry
*A
= *(struct elf_strtab_hash_entry
**) a
;
351 struct elf_strtab_hash_entry
*B
= *(struct elf_strtab_hash_entry
**) b
;
352 unsigned int lenA
= A
->len
;
353 unsigned int lenB
= B
->len
;
354 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
355 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
356 int l
= lenA
< lenB
? lenA
: lenB
;
361 return (int) *s
- (int) *t
;
370 is_suffix (const struct elf_strtab_hash_entry
*A
,
371 const struct elf_strtab_hash_entry
*B
)
373 if (A
->len
<= B
->len
)
374 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
375 not to be equal by the hash table. */
378 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
379 B
->root
.string
, B
->len
- 1) == 0;
382 /* This function assigns final string table offsets for used strings,
383 merging strings matching suffixes of longer strings if possible. */
386 _bfd_elf_strtab_finalize (struct elf_strtab_hash
*tab
)
388 struct elf_strtab_hash_entry
**array
, **a
, *e
;
389 bfd_size_type amt
, sec_size
;
392 /* Sort the strings by suffix and length. */
394 amt
*= sizeof (struct elf_strtab_hash_entry
*);
395 array
= (struct elf_strtab_hash_entry
**) bfd_malloc (amt
);
399 for (i
= 1, a
= array
; i
< tab
->size
; ++i
)
405 /* Adjust the length to not include the zero terminator. */
415 qsort (array
, size
, sizeof (struct elf_strtab_hash_entry
*), strrevcmp
);
417 /* Loop over the sorted array and merge suffixes. Start from the
418 end because we want eg.
430 ie. we don't want s1 pointing into the old s2. */
435 struct elf_strtab_hash_entry
*cmp
= *a
;
438 if (is_suffix (e
, cmp
))
441 cmp
->len
= -cmp
->len
;
451 /* Assign positions to the strings we want to keep. */
453 for (i
= 1; i
< tab
->size
; ++i
)
456 if (e
->refcount
&& e
->len
> 0)
458 e
->u
.index
= sec_size
;
463 tab
->sec_size
= sec_size
;
465 /* Adjust the rest. */
466 for (i
= 1; i
< tab
->size
; ++i
)
469 if (e
->refcount
&& e
->len
< 0)
470 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
+ e
->len
);