1 /* ELF string table handling.
2 Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
41 #include <sys/param.h>
47 # define MIN(a, b) ((a) < (b) ? (a) : (b))
55 struct Ebl_Strent *next;
56 struct Ebl_Strent *left;
57 struct Ebl_Strent *right;
65 struct memoryblock *next;
72 struct Ebl_Strent *root;
73 struct memoryblock *memory;
79 struct Ebl_Strent null;
83 /* Cache for the pagesize. */
85 /* We correct this value a bit so that `malloc' is not allocating more
87 #define MALLOC_OVERHEAD (2 * sizeof (void *))
91 ebl_strtabinit (bool nullstr)
95 ps = sysconf (_SC_PAGESIZE);
96 assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
99 struct Ebl_Strtab *ret
100 = (struct Ebl_Strtab *) calloc (1, sizeof (struct Ebl_Strtab));
103 ret->nullstr = nullstr;
108 ret->null.string = "";
117 morememory (struct Ebl_Strtab *st, size_t len)
119 size_t overhead = offsetof (struct memoryblock, memory);
120 len += overhead + MALLOC_OVERHEAD;
122 /* Allocate nearest multiple of pagesize >= len. */
123 len = ((len / ps) + (len % ps != 0)) * ps - MALLOC_OVERHEAD;
125 struct memoryblock *newmem = (struct memoryblock *) malloc (len);
129 newmem->next = st->memory;
131 st->backp = newmem->memory;
132 st->left = len - overhead;
139 ebl_strtabfree (struct Ebl_Strtab *st)
141 struct memoryblock *mb = st->memory;
154 static struct Ebl_Strent *
155 newstring (struct Ebl_Strtab *st, const char *str, size_t len)
157 /* Compute the amount of padding needed to make the structure aligned. */
158 size_t align = ((__alignof__ (struct Ebl_Strent)
159 - (((uintptr_t) st->backp)
160 & (__alignof__ (struct Ebl_Strent) - 1)))
161 & (__alignof__ (struct Ebl_Strent) - 1));
163 /* Make sure there is enough room in the memory block. */
164 if (st->left < align + sizeof (struct Ebl_Strent) + len)
166 if (morememory (st, sizeof (struct Ebl_Strent) + len))
172 /* Create the reserved string. */
173 struct Ebl_Strent *newstr = (struct Ebl_Strent *) (st->backp + align);
174 newstr->string = str;
178 newstr->right = NULL;
180 for (int i = len - 2; i >= 0; --i)
181 newstr->reverse[i] = str[len - 2 - i];
182 newstr->reverse[len - 1] = '\0';
183 st->backp += align + sizeof (struct Ebl_Strent) + len;
184 st->left -= align + sizeof (struct Ebl_Strent) + len;
190 /* XXX This function should definitely be rewritten to use a balancing
191 tree algorith (AVL, red-black trees). For now a simple, correct
192 implementation is enough. */
193 static struct Ebl_Strent **
194 searchstring (struct Ebl_Strent **sep, struct Ebl_Strent *newstr)
203 /* Compare the strings. */
204 int cmpres = memcmp ((*sep)->reverse, newstr->reverse,
205 MIN ((*sep)->len, newstr->len) - 1);
207 /* We found a matching string. */
210 return searchstring (&(*sep)->left, newstr);
212 return searchstring (&(*sep)->right, newstr);
216 /* Add new string. The actual string is assumed to be permanent. */
218 ebl_strtabadd (struct Ebl_Strtab *st, const char *str, size_t len)
220 /* Compute the string length if the caller doesn't know it. */
222 len = strlen (str) + 1;
224 /* Make sure all "" strings get offset 0 but only if the table was
225 created with a special null entry in mind. */
226 if (len == 1 && st->null.string != NULL)
229 /* Allocate memory for the new string and its associated information. */
230 struct Ebl_Strent *newstr = newstring (st, str, len);
234 /* Search in the array for the place to insert the string. If there
235 is no string with matching prefix and no string with matching
236 leading substring, create a new entry. */
237 struct Ebl_Strent **sep = searchstring (&st->root, newstr);
240 /* This is not the same entry. This means we have a prefix match. */
241 if ((*sep)->len > newstr->len)
243 /* Check whether we already know this string. */
244 for (struct Ebl_Strent *subs = (*sep)->next; subs != NULL;
246 if (subs->len == newstr->len)
248 /* We have an exact match with a substring. Free the memory
250 st->left += st->backp - (char *) newstr;
251 st->backp = (char *) newstr;
256 /* We have a new substring. This means we don't need the reverse
257 string of this entry anymore. */
258 st->backp -= newstr->len;
259 st->left += newstr->len;
261 newstr->next = (*sep)->next;
262 (*sep)->next = newstr;
264 else if ((*sep)->len != newstr->len)
266 /* When we get here it means that the string we are about to
267 add has a common prefix with a string we already have but
268 it is longer. In this case we have to put it first. */
269 st->total += newstr->len - (*sep)->len;
271 newstr->left = (*sep)->left;
272 newstr->right = (*sep)->right;
277 /* We have an exact match. Free the memory we allocated. */
278 st->left += st->backp - (char *) newstr;
279 st->backp = (char *) newstr;
285 st->total += newstr->len;
292 copystrings (struct Ebl_Strent *nodep, char **freep, size_t *offsetp)
294 if (nodep->left != NULL)
295 copystrings (nodep->left, freep, offsetp);
297 /* Process the current node. */
298 nodep->offset = *offsetp;
299 *freep = (char *) mempcpy (*freep, nodep->string, nodep->len);
300 *offsetp += nodep->len;
302 for (struct Ebl_Strent *subs = nodep->next; subs != NULL; subs = subs->next)
304 assert (subs->len < nodep->len);
305 subs->offset = nodep->offset + nodep->len - subs->len;
306 assert (subs->offset != 0 || subs->string[0] == '\0');
309 if (nodep->right != NULL)
310 copystrings (nodep->right, freep, offsetp);
315 ebl_strtabfinalize (struct Ebl_Strtab *st, Elf_Data *data)
317 size_t nulllen = st->nullstr ? 1 : 0;
319 /* Fill in the information. */
320 data->d_buf = malloc (st->total + nulllen);
321 if (data->d_buf == NULL)
324 /* The first byte must always be zero if we created the table with a
327 *((char *) data->d_buf) = '\0';
329 data->d_type = ELF_T_BYTE;
330 data->d_size = st->total + nulllen;
333 data->d_version = EV_CURRENT;
335 /* Now run through the tree and add all the string while also updating
336 the offset members of the elfstrent records. */
337 char *endp = (char *) data->d_buf + nulllen;
338 size_t copylen = nulllen;
340 copystrings (st->root, &endp, ©len);
341 assert (copylen == st->total + nulllen);
346 ebl_strtaboffset (struct Ebl_Strent *se)
353 ebl_string (struct Ebl_Strent *se)
355 assert (se->string != NULL);