1 /* ELF strtab with GC and suffix merging support.
2 Copyright 2001, 2002, 2003, 2005, 2006, 2007, 2008
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 3 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,
21 MA 02110-1301, USA. */
28 #include "libiberty.h"
30 /* An entry in the strtab hash table. */
32 struct elf_strtab_hash_entry
34 struct bfd_hash_entry root
;
35 /* Length of this entry. This includes the zero terminator. */
37 unsigned int refcount
;
39 /* Index within the merged section. */
41 /* Entry this is a suffix of (if len < 0). */
42 struct elf_strtab_hash_entry
*suffix
;
46 /* The strtab hash table. */
48 struct elf_strtab_hash
50 struct bfd_hash_table table
;
51 /* Next available index. */
53 /* Number of array entries alloced. */
54 bfd_size_type alloced
;
55 /* Final strtab size. */
56 bfd_size_type sec_size
;
57 /* Array of pointers to strtab entries. */
58 struct elf_strtab_hash_entry
**array
;
61 /* Routine to create an entry in a section merge hashtab. */
63 static struct bfd_hash_entry
*
64 elf_strtab_hash_newfunc (struct bfd_hash_entry
*entry
,
65 struct bfd_hash_table
*table
,
68 /* Allocate the structure if it has not already been allocated by a
71 entry
= 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 bfd_size_type amt
= sizeof (struct elf_strtab_hash
);
100 table
= 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
= bfd_malloc (table
->alloced
* amt
);
116 if (table
->array
== NULL
)
122 table
->array
[0] = NULL
;
130 _bfd_elf_strtab_free (struct elf_strtab_hash
*tab
)
132 bfd_hash_table_free (&tab
->table
);
137 /* Get the index of an entity in a hash table, adding it if it is not
141 _bfd_elf_strtab_add (struct elf_strtab_hash
*tab
,
145 register struct elf_strtab_hash_entry
*entry
;
147 /* We handle this specially, since we don't want to do refcounting
152 BFD_ASSERT (tab
->sec_size
== 0);
153 entry
= (struct elf_strtab_hash_entry
*)
154 bfd_hash_lookup (&tab
->table
, str
, TRUE
, copy
);
157 return (bfd_size_type
) -1;
162 entry
->len
= strlen (str
) + 1;
163 /* 2G strings lose. */
164 BFD_ASSERT (entry
->len
> 0);
165 if (tab
->size
== tab
->alloced
)
167 bfd_size_type amt
= sizeof (struct elf_strtab_hash_entry
*);
169 tab
->array
= bfd_realloc_or_free (tab
->array
, tab
->alloced
* amt
);
170 if (tab
->array
== NULL
)
171 return (bfd_size_type
) -1;
174 entry
->u
.index
= tab
->size
++;
175 tab
->array
[entry
->u
.index
] = entry
;
177 return entry
->u
.index
;
181 _bfd_elf_strtab_addref (struct elf_strtab_hash
*tab
, bfd_size_type idx
)
183 if (idx
== 0 || idx
== (bfd_size_type
) -1)
185 BFD_ASSERT (tab
->sec_size
== 0);
186 BFD_ASSERT (idx
< tab
->size
);
187 ++tab
->array
[idx
]->refcount
;
191 _bfd_elf_strtab_delref (struct elf_strtab_hash
*tab
, bfd_size_type idx
)
193 if (idx
== 0 || idx
== (bfd_size_type
) -1)
195 BFD_ASSERT (tab
->sec_size
== 0);
196 BFD_ASSERT (idx
< tab
->size
);
197 BFD_ASSERT (tab
->array
[idx
]->refcount
> 0);
198 --tab
->array
[idx
]->refcount
;
202 _bfd_elf_strtab_clear_all_refs (struct elf_strtab_hash
*tab
)
206 for (idx
= 1; idx
< tab
->size
; ++idx
)
207 tab
->array
[idx
]->refcount
= 0;
211 _bfd_elf_strtab_size (struct elf_strtab_hash
*tab
)
213 return tab
->sec_size
? tab
->sec_size
: tab
->size
;
217 _bfd_elf_strtab_offset (struct elf_strtab_hash
*tab
, bfd_size_type idx
)
219 struct elf_strtab_hash_entry
*entry
;
223 BFD_ASSERT (idx
< tab
->size
);
224 BFD_ASSERT (tab
->sec_size
);
225 entry
= tab
->array
[idx
];
226 BFD_ASSERT (entry
->refcount
> 0);
228 return tab
->array
[idx
]->u
.index
;
232 _bfd_elf_strtab_emit (register bfd
*abfd
, struct elf_strtab_hash
*tab
)
234 bfd_size_type off
= 1, i
;
236 if (bfd_bwrite ("", 1, abfd
) != 1)
239 for (i
= 1; i
< tab
->size
; ++i
)
241 register const char *str
;
242 register unsigned int len
;
244 BFD_ASSERT (tab
->array
[i
]->refcount
== 0);
245 len
= tab
->array
[i
]->len
;
249 str
= tab
->array
[i
]->root
.string
;
250 if (bfd_bwrite (str
, len
, abfd
) != len
)
256 BFD_ASSERT (off
== tab
->sec_size
);
260 /* Compare two elf_strtab_hash_entry structures. Called via qsort. */
263 strrevcmp (const void *a
, const void *b
)
265 struct elf_strtab_hash_entry
*A
= *(struct elf_strtab_hash_entry
**) a
;
266 struct elf_strtab_hash_entry
*B
= *(struct elf_strtab_hash_entry
**) b
;
267 unsigned int lenA
= A
->len
;
268 unsigned int lenB
= B
->len
;
269 const unsigned char *s
= (const unsigned char *) A
->root
.string
+ lenA
- 1;
270 const unsigned char *t
= (const unsigned char *) B
->root
.string
+ lenB
- 1;
271 int l
= lenA
< lenB
? lenA
: lenB
;
276 return (int) *s
- (int) *t
;
285 is_suffix (const struct elf_strtab_hash_entry
*A
,
286 const struct elf_strtab_hash_entry
*B
)
288 if (A
->len
<= B
->len
)
289 /* B cannot be a suffix of A unless A is equal to B, which is guaranteed
290 not to be equal by the hash table. */
293 return memcmp (A
->root
.string
+ (A
->len
- B
->len
),
294 B
->root
.string
, B
->len
- 1) == 0;
297 /* This function assigns final string table offsets for used strings,
298 merging strings matching suffixes of longer strings if possible. */
301 _bfd_elf_strtab_finalize (struct elf_strtab_hash
*tab
)
303 struct elf_strtab_hash_entry
**array
, **a
, *e
;
304 bfd_size_type size
, amt
;
306 /* GCC 2.91.66 (egcs-1.1.2) on i386 miscompiles this function when i is
307 a 64-bit bfd_size_type: a 64-bit target or --enable-64-bit-bfd.
308 Besides, indexing with a long long wouldn't give anything but extra
312 /* Sort the strings by suffix and length. */
313 amt
= tab
->size
* sizeof (struct elf_strtab_hash_entry
*);
314 array
= bfd_malloc (amt
);
318 for (i
= 1, a
= array
; i
< tab
->size
; ++i
)
324 /* Adjust the length to not include the zero terminator. */
334 qsort (array
, size
, sizeof (struct elf_strtab_hash_entry
*), strrevcmp
);
336 /* Loop over the sorted array and merge suffixes. Start from the
337 end because we want eg.
349 ie. we don't want s1 pointing into the old s2. */
354 struct elf_strtab_hash_entry
*cmp
= *a
;
357 if (is_suffix (e
, cmp
))
360 cmp
->len
= -cmp
->len
;
371 /* Assign positions to the strings we want to keep. */
373 for (i
= 1; i
< tab
->size
; ++i
)
376 if (e
->refcount
&& e
->len
> 0)
383 tab
->sec_size
= size
;
385 /* Adjust the rest. */
386 for (i
= 1; i
< tab
->size
; ++i
)
389 if (e
->refcount
&& e
->len
< 0)
390 e
->u
.index
= e
->u
.suffix
->u
.index
+ (e
->u
.suffix
->len
+ e
->len
);
This page took 0.036388 seconds and 4 git commands to generate.