2 * Copyright (c) 2013, Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/param.h>
28 #include <sys/queue.h>
37 #include "_libelftc.h"
39 ELFTC_VCSID("$Id: elftc_string_table.c 2869 2013-01-06 13:29:18Z jkoshy $");
41 #define ELFTC_STRING_TABLE_DEFAULT_SIZE (4*1024)
42 #define ELFTC_STRING_TABLE_EXPECTED_STRING_SIZE 16
43 #define ELFTC_STRING_TABLE_EXPECTED_CHAIN_LENGTH 8
44 #define ELFTC_STRING_TABLE_POOL_SIZE_INCREMENT (4*1024)
46 struct _Elftc_String_Table_Entry {
48 SLIST_ENTRY(_Elftc_String_Table_Entry) ste_next;
51 #define ELFTC_STRING_TABLE_COMPACTION_FLAG 0x1
52 #define ELFTC_STRING_TABLE_LENGTH(st) ((st)->st_len >> 1)
53 #define ELFTC_STRING_TABLE_CLEAR_COMPACTION_FLAG(st) do { \
54 (st)->st_len &= ~ELFTC_STRING_TABLE_COMPACTION_FLAG; \
56 #define ELFTC_STRING_TABLE_SET_COMPACTION_FLAG(st) do { \
57 (st)->st_len |= ELFTC_STRING_TABLE_COMPACTION_FLAG; \
59 #define ELFTC_STRING_TABLE_UPDATE_LENGTH(st, len) do { \
62 ELFTC_STRING_TABLE_COMPACTION_FLAG) | \
66 struct _Elftc_String_Table {
67 unsigned int st_len; /* length and flags */
69 int st_string_pool_size;
71 SLIST_HEAD(_Elftc_String_Table_Bucket,
72 _Elftc_String_Table_Entry) st_buckets[];
75 static struct _Elftc_String_Table_Entry *
76 elftc_string_table_find_hash_entry(Elftc_String_Table *st, const char *string,
79 struct _Elftc_String_Table_Entry *ste;
83 hashindex = libelftc_hash_string(string) % st->st_nbuckets;
86 *rhashindex = hashindex;
88 SLIST_FOREACH(ste, &st->st_buckets[hashindex], ste_next) {
89 s = st->st_string_pool + abs(ste->ste_idx);
91 assert(s > st->st_string_pool &&
92 s < st->st_string_pool + st->st_string_pool_size);
94 if (strcmp(s, string) == 0)
102 elftc_string_table_add_to_pool(Elftc_String_Table *st, const char *string)
105 int len, newsize, stlen;
107 len = strlen(string) + 1; /* length, including the trailing NUL */
108 stlen = ELFTC_STRING_TABLE_LENGTH(st);
110 /* Resize the pool, if needed. */
111 if (stlen + len >= st->st_string_pool_size) {
112 newsize = roundup(st->st_string_pool_size +
113 ELFTC_STRING_TABLE_POOL_SIZE_INCREMENT,
114 ELFTC_STRING_TABLE_POOL_SIZE_INCREMENT);
115 if ((newpool = realloc(st->st_string_pool, newsize)) ==
118 st->st_string_pool = newpool;
119 st->st_string_pool_size = newsize;
122 strcpy(st->st_string_pool + stlen, string);
123 ELFTC_STRING_TABLE_UPDATE_LENGTH(st, stlen + len);
129 elftc_string_table_create(int sizehint)
131 int n, nbuckets, tablesize;
132 struct _Elftc_String_Table *st;
134 if (sizehint < ELFTC_STRING_TABLE_DEFAULT_SIZE)
135 sizehint = ELFTC_STRING_TABLE_DEFAULT_SIZE;
137 nbuckets = sizehint / (ELFTC_STRING_TABLE_EXPECTED_CHAIN_LENGTH *
138 ELFTC_STRING_TABLE_EXPECTED_STRING_SIZE);
140 tablesize = sizeof(struct _Elftc_String_Table) +
141 nbuckets * sizeof(struct _Elftc_String_Table_Bucket);
143 if ((st = malloc(tablesize)) == NULL)
145 if ((st->st_string_pool = malloc(sizehint)) == NULL) {
150 for (n = 0; n < nbuckets; n++)
151 SLIST_INIT(&st->st_buckets[n]);
154 st->st_nbuckets = nbuckets;
155 st->st_string_pool_size = sizehint;
156 *st->st_string_pool = '\0';
157 ELFTC_STRING_TABLE_UPDATE_LENGTH(st, 1);
163 elftc_string_table_destroy(Elftc_String_Table *st)
166 struct _Elftc_String_Table_Entry *s, *t;
168 for (n = 0; n < st->st_nbuckets; n++)
169 SLIST_FOREACH_SAFE(s, &st->st_buckets[n], ste_next, t)
171 free(st->st_string_pool);
178 elftc_string_table_from_section(Elf_Scn *scn, int sizehint)
184 Elftc_String_Table *st;
186 /* Verify the type of the section passed in. */
187 if (gelf_getshdr(scn, &sh) == NULL ||
188 sh.sh_type != SHT_STRTAB) {
193 if ((d = elf_getdata(scn, NULL)) == NULL ||
199 if ((st = elftc_string_table_create(sizehint)) == NULL)
205 * Verify that the first byte of the data buffer is '\0'.
215 * Skip the first '\0' and insert the strings in the buffer,
218 for (s += 1; s < end; s += len) {
219 if (elftc_string_table_insert(st, s) == 0)
222 len = strlen(s) + 1; /* Include space for the trailing NUL. */
229 (void) elftc_string_table_destroy(st);
235 elftc_string_table_image(Elftc_String_Table *st, size_t *size)
238 struct _Elftc_String_Table_Entry *ste;
239 struct _Elftc_String_Table_Bucket *head;
240 int copied, hashindex, offset, length, newsize;
243 * For the common case of a string table has not seen
244 * a string deletion, we can just export the current
247 if ((st->st_len & ELFTC_STRING_TABLE_COMPACTION_FLAG) == 0) {
249 *size = ELFTC_STRING_TABLE_LENGTH(st);
250 return (st->st_string_pool);
254 * Otherwise, compact the string table in-place.
256 assert(*st->st_string_pool == '\0');
259 end = st->st_string_pool + ELFTC_STRING_TABLE_LENGTH(st);
261 for (r = s = st->st_string_pool + 1;
263 s += length, r += copied) {
266 length = strlen(s) + 1;
268 ste = elftc_string_table_find_hash_entry(st, s,
270 head = &st->st_buckets[hashindex];
274 /* Ignore deleted strings. */
275 if (ste->ste_idx < 0) {
276 SLIST_REMOVE(head, ste, _Elftc_String_Table_Entry,
282 /* Move 'live' strings up. */
287 if (r == s) /* Nothing removed yet. */
290 memmove(r, s, copied);
292 /* Update the index for this entry. */
293 ste->ste_idx = offset;
296 ELFTC_STRING_TABLE_CLEAR_COMPACTION_FLAG(st);
297 ELFTC_STRING_TABLE_UPDATE_LENGTH(st, newsize);
302 return (st->st_string_pool);
306 elftc_string_table_insert(Elftc_String_Table *st, const char *string)
309 struct _Elftc_String_Table_Entry *ste;
313 ste = elftc_string_table_find_hash_entry(st, string, &hashindex);
315 assert(hashindex >= 0 && hashindex < st->st_nbuckets);
318 if ((ste = malloc(sizeof(*ste))) == NULL)
320 if ((ste->ste_idx = elftc_string_table_add_to_pool(st,
326 SLIST_INSERT_HEAD(&st->st_buckets[hashindex], ste, ste_next);
330 if (idx < 0) /* Undelete. */
331 ste->ste_idx = idx = (- idx);
337 elftc_string_table_lookup(Elftc_String_Table *st, const char *string)
340 struct _Elftc_String_Table_Entry *ste;
342 ste = elftc_string_table_find_hash_entry(st, string, &hashindex);
344 assert(hashindex >= 0 && hashindex < st->st_nbuckets);
346 if (ste == NULL || (idx = ste->ste_idx) < 0)
353 elftc_string_table_remove(Elftc_String_Table *st, const char *string)
356 struct _Elftc_String_Table_Entry *ste;
358 ste = elftc_string_table_find_hash_entry(st, string, NULL);
360 if (ste == NULL || (idx = ste->ste_idx) < 0)
361 return (ELFTC_FAILURE);
363 assert(idx > 0 && idx < (int) ELFTC_STRING_TABLE_LENGTH(st));
365 ste->ste_idx = (- idx);
367 ELFTC_STRING_TABLE_SET_COMPACTION_FLAG(st);
369 return (ELFTC_SUCCESS);
373 elftc_string_table_to_string(Elftc_String_Table *st, size_t offset)
377 s = st->st_string_pool + offset;
381 * - An offset value within pool bounds.
382 * - A non-NUL byte at the specified offset.
383 * - The end of the prior string at offset - 1.
385 if (offset == 0 || offset >= ELFTC_STRING_TABLE_LENGTH(st) ||
386 *s == '\0' || *(s - 1) != '\0') {