Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame^] | 1 | /* ELF string table handling. |
| 2 | Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc. |
| 3 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 4 | |
| 5 | This program is Open Source software; you can redistribute it and/or |
| 6 | modify it under the terms of the Open Software License version 1.0 as |
| 7 | published by the Open Source Initiative. |
| 8 | |
| 9 | You should have received a copy of the Open Software License along |
| 10 | with this program; if not, you may obtain a copy of the Open Software |
| 11 | License version 1.0 from http://www.opensource.org/licenses/osl.php or |
| 12 | by writing the Open Source Initiative c/o Lawrence Rosen, Esq., |
| 13 | 3001 King Ranch Road, Ukiah, CA 95482. */ |
| 14 | |
| 15 | #ifdef HAVE_CONFIG_H |
| 16 | # include <config.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <libelf.h> |
| 22 | #include <stddef.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/param.h> |
| 27 | |
| 28 | #include "libebl.h" |
| 29 | #include <system.h> |
| 30 | |
| 31 | #ifndef MIN |
| 32 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 33 | #endif |
| 34 | |
| 35 | |
| 36 | struct Ebl_Strent |
| 37 | { |
| 38 | const char *string; |
| 39 | size_t len; |
| 40 | struct Ebl_Strent *next; |
| 41 | struct Ebl_Strent *left; |
| 42 | struct Ebl_Strent *right; |
| 43 | size_t offset; |
| 44 | char reverse[0]; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | struct memoryblock |
| 49 | { |
| 50 | struct memoryblock *next; |
| 51 | char memory[0]; |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | struct Ebl_Strtab |
| 56 | { |
| 57 | struct Ebl_Strent *root; |
| 58 | struct memoryblock *memory; |
| 59 | char *backp; |
| 60 | size_t left; |
| 61 | size_t total; |
| 62 | bool nullstr; |
| 63 | |
| 64 | struct Ebl_Strent null; |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | /* Cache for the pagesize. We correct this value a bit so that `malloc' |
| 69 | is not allocating more than a page. */ |
| 70 | static size_t ps; |
| 71 | |
| 72 | |
| 73 | struct Ebl_Strtab * |
| 74 | ebl_strtabinit (bool nullstr) |
| 75 | { |
| 76 | if (ps == 0) |
| 77 | { |
| 78 | ps = sysconf (_SC_PAGESIZE) - 2 * sizeof (void *); |
| 79 | assert (sizeof (struct memoryblock) < ps); |
| 80 | } |
| 81 | |
| 82 | struct Ebl_Strtab *ret |
| 83 | = (struct Ebl_Strtab *) calloc (1, sizeof (struct Ebl_Strtab)); |
| 84 | if (ret != NULL) |
| 85 | { |
| 86 | ret->nullstr = nullstr; |
| 87 | |
| 88 | if (nullstr) |
| 89 | { |
| 90 | ret->null.len = 1; |
| 91 | ret->null.string = ""; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static int |
| 100 | morememory (struct Ebl_Strtab *st, size_t len) |
| 101 | { |
| 102 | if (len < ps) |
| 103 | len = ps; |
| 104 | |
| 105 | struct memoryblock *newmem = (struct memoryblock *) malloc (len); |
| 106 | if (newmem == NULL) |
| 107 | return 1; |
| 108 | |
| 109 | newmem->next = st->memory; |
| 110 | st->memory = newmem; |
| 111 | st->backp = newmem->memory; |
| 112 | st->left = len - offsetof (struct memoryblock, memory); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void |
| 119 | ebl_strtabfree (struct Ebl_Strtab *st) |
| 120 | { |
| 121 | struct memoryblock *mb = st->memory; |
| 122 | |
| 123 | while (mb != NULL) |
| 124 | { |
| 125 | void *old = mb; |
| 126 | mb = mb->next; |
| 127 | free (old); |
| 128 | } |
| 129 | |
| 130 | free (st); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static struct Ebl_Strent * |
| 135 | newstring (struct Ebl_Strtab *st, const char *str, size_t len) |
| 136 | { |
| 137 | /* Compute the amount of padding needed to make the structure aligned. */ |
| 138 | size_t align = ((__alignof__ (struct Ebl_Strent) |
| 139 | - (((uintptr_t) st->backp) |
| 140 | & (__alignof__ (struct Ebl_Strent) - 1))) |
| 141 | & (__alignof__ (struct Ebl_Strent) - 1)); |
| 142 | |
| 143 | /* Make sure there is enough room in the memory block. */ |
| 144 | if (st->left < align + sizeof (struct Ebl_Strent) + len) |
| 145 | { |
| 146 | if (morememory (st, sizeof (struct Ebl_Strent) + len)) |
| 147 | return NULL; |
| 148 | |
| 149 | align = 0; |
| 150 | } |
| 151 | |
| 152 | /* Create the reserved string. */ |
| 153 | struct Ebl_Strent *newstr = (struct Ebl_Strent *) (st->backp + align); |
| 154 | newstr->string = str; |
| 155 | newstr->len = len; |
| 156 | newstr->next = NULL; |
| 157 | newstr->left = NULL; |
| 158 | newstr->right = NULL; |
| 159 | newstr->offset = 0; |
| 160 | for (int i = len - 2; i >= 0; --i) |
| 161 | newstr->reverse[i] = str[len - 2 - i]; |
| 162 | newstr->reverse[len - 1] = '\0'; |
| 163 | st->backp += align + sizeof (struct Ebl_Strent) + len; |
| 164 | st->left -= align + sizeof (struct Ebl_Strent) + len; |
| 165 | |
| 166 | return newstr; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* XXX This function should definitely be rewritten to use a balancing |
| 171 | tree algorith (AVL, red-black trees). For now a simple, correct |
| 172 | implementation is enough. */ |
| 173 | static struct Ebl_Strent ** |
| 174 | searchstring (struct Ebl_Strent **sep, struct Ebl_Strent *newstr) |
| 175 | { |
| 176 | /* More strings? */ |
| 177 | if (*sep == NULL) |
| 178 | { |
| 179 | *sep = newstr; |
| 180 | return sep; |
| 181 | } |
| 182 | |
| 183 | /* Compare the strings. */ |
| 184 | int cmpres = memcmp ((*sep)->reverse, newstr->reverse, |
| 185 | MIN ((*sep)->len, newstr->len) - 1); |
| 186 | if (cmpres == 0) |
| 187 | /* We found a matching string. */ |
| 188 | return sep; |
| 189 | else if (cmpres > 0) |
| 190 | return searchstring (&(*sep)->left, newstr); |
| 191 | else |
| 192 | return searchstring (&(*sep)->right, newstr); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* Add new string. The actual string is assumed to be permanent. */ |
| 197 | struct Ebl_Strent * |
| 198 | ebl_strtabadd (struct Ebl_Strtab *st, const char *str, size_t len) |
| 199 | { |
| 200 | /* Compute the string length if the caller doesn't know it. */ |
| 201 | if (len == 0) |
| 202 | len = strlen (str) + 1; |
| 203 | |
| 204 | /* Make sure all "" strings get offset 0 but only if the table was |
| 205 | created with a special null entry in mind. */ |
| 206 | if (len == 1 && st->null.string != NULL) |
| 207 | return &st->null; |
| 208 | |
| 209 | /* Allocate memory for the new string and its associated information. */ |
| 210 | struct Ebl_Strent *newstr = newstring (st, str, len); |
| 211 | if (newstr == NULL) |
| 212 | return NULL; |
| 213 | |
| 214 | /* Search in the array for the place to insert the string. If there |
| 215 | is no string with matching prefix and no string with matching |
| 216 | leading substring, create a new entry. */ |
| 217 | struct Ebl_Strent **sep = searchstring (&st->root, newstr); |
| 218 | if (*sep != newstr) |
| 219 | { |
| 220 | /* This is not the same entry. This means we have a prefix match. */ |
| 221 | if ((*sep)->len > newstr->len) |
| 222 | { |
| 223 | /* Check whether we already know this string. */ |
| 224 | for (struct Ebl_Strent *subs = (*sep)->next; subs != NULL; |
| 225 | subs = subs->next) |
| 226 | if (subs->len == newstr->len) |
| 227 | { |
| 228 | /* We have an exact match with a substring. Free the memory |
| 229 | we allocated. */ |
| 230 | st->left += st->backp - (char *) newstr; |
| 231 | st->backp = (char *) newstr; |
| 232 | |
| 233 | return subs; |
| 234 | } |
| 235 | |
| 236 | /* We have a new substring. This means we don't need the reverse |
| 237 | string of this entry anymore. */ |
| 238 | st->backp -= newstr->len; |
| 239 | st->left += newstr->len; |
| 240 | |
| 241 | newstr->next = (*sep)->next; |
| 242 | (*sep)->next = newstr; |
| 243 | } |
| 244 | else if ((*sep)->len != newstr->len) |
| 245 | { |
| 246 | /* When we get here it means that the string we are about to |
| 247 | add has a common prefix with a string we already have but |
| 248 | it is longer. In this case we have to put it first. */ |
| 249 | st->total += newstr->len - (*sep)->len; |
| 250 | newstr->next = *sep; |
| 251 | newstr->left = (*sep)->left; |
| 252 | newstr->right = (*sep)->right; |
| 253 | *sep = newstr; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | /* We have an exact match. Free the memory we allocated. */ |
| 258 | st->left += st->backp - (char *) newstr; |
| 259 | st->backp = (char *) newstr; |
| 260 | |
| 261 | newstr = *sep; |
| 262 | } |
| 263 | } |
| 264 | else |
| 265 | st->total += newstr->len; |
| 266 | |
| 267 | return newstr; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static void |
| 272 | copystrings (struct Ebl_Strent *nodep, char **freep, size_t *offsetp) |
| 273 | { |
| 274 | if (nodep->left != NULL) |
| 275 | copystrings (nodep->left, freep, offsetp); |
| 276 | |
| 277 | /* Process the current node. */ |
| 278 | nodep->offset = *offsetp; |
| 279 | *freep = (char *) mempcpy (*freep, nodep->string, nodep->len); |
| 280 | *offsetp += nodep->len; |
| 281 | |
| 282 | for (struct Ebl_Strent *subs = nodep->next; subs != NULL; subs = subs->next) |
| 283 | { |
| 284 | assert (subs->len < nodep->len); |
| 285 | subs->offset = nodep->offset + nodep->len - subs->len; |
| 286 | assert (subs->offset != 0 || subs->string[0] == '\0'); |
| 287 | } |
| 288 | |
| 289 | if (nodep->right != NULL) |
| 290 | copystrings (nodep->right, freep, offsetp); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void |
| 295 | ebl_strtabfinalize (struct Ebl_Strtab *st, Elf_Data *data) |
| 296 | { |
| 297 | size_t nulllen = st->nullstr ? 1 : 0; |
| 298 | |
| 299 | /* Fill in the information. */ |
| 300 | data->d_buf = malloc (st->total + nulllen); |
| 301 | if (data->d_buf == NULL) |
| 302 | abort (); |
| 303 | |
| 304 | /* The first byte must always be zero if we created the table with a |
| 305 | null string. */ |
| 306 | if (st->nullstr) |
| 307 | *((char *) data->d_buf) = '\0'; |
| 308 | |
| 309 | data->d_type = ELF_T_BYTE; |
| 310 | data->d_size = st->total + nulllen; |
| 311 | data->d_off = 0; |
| 312 | data->d_align = 1; |
| 313 | data->d_version = EV_CURRENT; |
| 314 | |
| 315 | /* Now run through the tree and add all the string while also updating |
| 316 | the offset members of the elfstrent records. */ |
| 317 | char *endp = (char *) data->d_buf + nulllen; |
| 318 | size_t copylen = nulllen; |
| 319 | copystrings (st->root, &endp, ©len); |
| 320 | assert (copylen == st->total + nulllen); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | size_t |
| 325 | ebl_strtaboffset (struct Ebl_Strent *se) |
| 326 | { |
| 327 | return se->offset; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | const char * |
| 332 | ebl_string (struct Ebl_Strent *se) |
| 333 | { |
| 334 | assert (se->string != NULL); |
| 335 | |
| 336 | return se->string; |
| 337 | } |