1 /* ELF strtab with GC and suffix merging support.
2 Copyright 2001, 2002, 2003, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Jakub Jelinek <jakub@redhat.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 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. */
53 bfd_size_type 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
= bfd_hash_allocate (table
, sizeof (struct elf_strtab_hash_entry
));
74 /* Call the allocation method of the superclass. */
75 entry
= bfd_hash_newfunc (entry
, table
, string
);
79 /* Initialize the local fields. */
80 struct elf_strtab_hash_entry
*ret
;
82 ret
= (struct elf_strtab_hash_entry
*) entry
;
91 /* Create a new hash table. */
93 struct elf_strtab_hash
*
94 _bfd_elf_strtab_init (void)
96 struct elf_strtab_hash
*table
;
97 bfd_size_type amt
= sizeof (struct elf_strtab_hash
);
99 table
= bfd_malloc (amt
);
103 if (!bfd_hash_table_init (&table
->table
, elf_strtab_hash_newfunc
,
104 sizeof (struct elf_strtab_hash_entry
)))
113 amt
= sizeof (struct elf_strtab_hasn_entry
*);
114 table
->array
= bfd_malloc (table
->alloced
* amt
);
115 if (table
->array
== NULL
)
121 table
->array
[0] = NULL
;
129 _bfd_elf_strtab_free (struct elf_strtab_hash
*tab
)
131 bfd_hash_table_free (&tab
->table
);
136 /* Get the index of an entity in a hash table, adding it if it is not
140 _bfd_elf_strtab_add (struct elf_strtab_hash
*tab
,
144 register struct elf_strtab_hash_entry
*entry
;
146 /* We handle this specially, since we don't want to do refcounting
151 BFD_ASSERT (tab
->sec_size
== 0);
152 entry
= (struct elf_strtab_hash_entry
*)
153 bfd_hash_lookup (&tab
->table
, str
, TRUE
, copy
);
156 return (bfd_size_type
) -1;
161 entry
->len
= strlen (str
) + 1;
162 /* 2G strings lose. */
163 BFD_ASSERT (entry
->len
> 0);
164 if (tab
->size
== tab
->alloced
)
166 bfd_size_type amt
= sizeof (struct elf_strtab_hash_entry
*);
168 tab
->array
= bfd_realloc (tab
->array
, tab
->alloced
* amt
);
169 if (tab
->array
== NULL
)
170 return (bfd_size_type
) -1;
173 entry
->u
.index
= tab
->size
++;
174 tab
->array
[entry
->u
.index
] = entry
;
176 return entry
->u
.index
;
180 _bfd_elf_strtab_addref (struct elf_strtab_hash
*tab
, bfd_size_type idx
)
182 if (idx
== 0 || idx
== (bfd_size_type
) -1)
184 BFD_ASSERT (tab
->sec_size
== 0);
185 BFD_ASSERT (idx
< tab
->size
);
186 ++tab
->array
[idx
]->refcount
;
190 _bfd_elf_strtab_delref (struct elf_strtab_hash
*tab
, bfd_size_type idx
)
192 if (idx
== 0 || idx
== (bfd_size_type
) -1)
194 BFD_ASSERT (tab
->sec_size
== 0);
195 BFD_ASSERT (idx
< tab
->size
);
196 BFD_ASSERT (tab
->array
[idx
]->refcount
> 0);
197 --tab
->array
[idx
]->refcount
;
201 _bfd_elf_strtab_clear_all_refs (struct elf_strtab_hash
*tab
)
205 for (idx
= 1; idx
< tab
->size
; ++idx
)
206 tab
->array
[idx
]->refcount
= 0;
210 _bfd_elf_strtab_size (struct elf_strtab_hash
*tab
)
212 return tab
->sec_size
? tab
->sec_size
: tab
->size
;
216 _bfd_elf_strtab_offset (struct elf_strtab_hash
*tab
, bfd_size_type idx
)
218 struct elf_strtab_hash_entry
*entry
;
222 BFD_ASSERT (idx
< tab
->size
);
223 BFD_ASSERT (tab
->sec_size
);
224 entry
= tab
->array
[idx
];
225 BFD_ASSERT (entry
->refcount
> 0);
227 return tab
->array
[idx
]->u
.index
;
231 _bfd_elf_strtab_emit (register bfd
*abfd
, struct elf_strtab_hash
*tab
)
233 bfd_size_type off
= 1, i
;
235 if (bfd_bwrite ("", 1, abfd
) != 1)
238 for (i
= 1; i
< tab
->size
; ++i
)
240 register const char *str
;
241 register unsigned int len
;
243 BFD_ASSERT (tab
->array
[i
]->refcount
== 0);
244 len
= tab
->array
[i
]->len
;
248 str
= tab
->array
[i
]->root
.string
;
249 if (bfd_bwrite (str
, len
, abfd
) != len
)
255 BFD_ASSERT (off
== tab
->sec_size
);
259 /* Compare two elf_strtab_hash_entry structures. Called via qsort. */
262 strrevcmp (const void *a
, const void *b
)
264 struct elf_strtab_hash_entry
*A
= *(struct elf_strtab_hash_entry
**) a
;
265 struct elf_strtab_hash_entry
*B
= *(struct elf_strtab_hash_entry
**) b
;
266 unsigned int lenA
= A
->len
;
267 unsigned int lenB
= B
->len
;
268 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
269 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
270 int l
= lenA
< lenB
? lenA
: lenB
;
275 return (int) *s
- (int) *t
;
284 is_suffix (const struct elf_strtab_hash_entry
*A
,
285 const struct elf_strtab_hash_entry
*B
)
287 if (A
->len
<= B
->len
)
288 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
289 not to be equal by the hash table. */
292 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
293 B
->root
.string
, B
->len
- 1) == 0;
296 /* This function assigns final string table offsets for used strings,
297 merging strings matching suffixes of longer strings if possible. */
300 _bfd_elf_strtab_finalize (struct elf_strtab_hash
*tab
)
302 struct elf_strtab_hash_entry
**array
, **a
, *e
;
303 bfd_size_type size
, amt
;
305 /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
306 a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
307 Besides, indexing with a long long wouldn't give anything but extra
311 /* Sort the strings by suffix and length. */
312 amt
= tab
->size
* sizeof (struct elf_strtab_hash_entry
*);
313 array
= bfd_malloc (amt
);
317 for (i
= 1, a
= array
; i
< tab
->size
; ++i
)
323 /* Adjust the length to not include the zero terminator. */
333 qsort (array
, size
, sizeof (struct elf_strtab_hash_entry
*), strrevcmp
);
335 /* Loop over the sorted array and merge suffixes. Start from the
336 end because we want eg.
348 ie. we don't want s1 pointing into the old s2. */
353 struct elf_strtab_hash_entry
*cmp
= *a
;
356 if (is_suffix (e
, cmp
))
359 cmp
->len
= -cmp
->len
;
370 /* Assign positions to the strings we want to keep. */
372 for (i
= 1; i
< tab
->size
; ++i
)
375 if (e
->refcount
&& e
->len
> 0)
382 tab
->sec_size
= size
;
384 /* Adjust the rest. */
385 for (i
= 1; i
< tab
->size
; ++i
)
388 if (e
->refcount
&& e
->len
< 0)
389 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
+ e
->len
);
This page took 0.039239 seconds and 4 git commands to generate.