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