Home | History | Annotate | Download | only in libebl
      1 /* ELF string table handling.
      2    Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2000.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    In addition, as a special exception, Red Hat, Inc. gives You the
     20    additional right to link the code of Red Hat elfutils with code licensed
     21    under any Open Source Initiative certified open source license
     22    (http://www.opensource.org/licenses/index.php) which requires the
     23    distribution of source code with any binary distribution and to
     24    distribute linked combinations of the two.  Non-GPL Code permitted under
     25    this exception must only link to the code of Red Hat elfutils through
     26    those well defined interfaces identified in the file named EXCEPTION
     27    found in the source code files (the "Approved Interfaces").  The files
     28    of Non-GPL Code may instantiate templates or use macros or inline
     29    functions from the Approved Interfaces without causing the resulting
     30    work to be covered by the GNU General Public License.  Only Red Hat,
     31    Inc. may make changes or additions to the list of Approved Interfaces.
     32    Red Hat's grant of this exception is conditioned upon your not adding
     33    any new exceptions.  If you wish to add a new Approved Interface or
     34    exception, please contact Red Hat.  You must obey the GNU General Public
     35    License in all respects for all of the Red Hat elfutils code and other
     36    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     37    covered by this exception.  If you modify this file, you may extend this
     38    exception to your version of the file, but you are not obligated to do
     39    so.  If you do not wish to provide this exception without modification,
     40    you must delete this exception statement from your version and license
     41    this file solely under the GPL without exception.
     42 
     43    Red Hat elfutils is an included package of the Open Invention Network.
     44    An included package of the Open Invention Network is a package for which
     45    Open Invention Network licensees cross-license their patents.  No patent
     46    license is granted, either expressly or impliedly, by designation as an
     47    included package.  Should you wish to participate in the Open Invention
     48    Network licensing program, please visit www.openinventionnetwork.com
     49    <http://www.openinventionnetwork.com>.  */
     50 
     51 #ifdef HAVE_CONFIG_H
     52 # include <config.h>
     53 #endif
     54 
     55 #include <assert.h>
     56 #include <inttypes.h>
     57 #include <libelf.h>
     58 #include <stddef.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 #include <sys/param.h>
     63 
     64 #include "libebl.h"
     65 #include <system.h>
     66 
     67 #ifndef MIN
     68 # define MIN(a, b) ((a) < (b) ? (a) : (b))
     69 #endif
     70 
     71 
     72 struct Ebl_Strent
     73 {
     74   const char *string;
     75   size_t len;
     76   struct Ebl_Strent *next;
     77   struct Ebl_Strent *left;
     78   struct Ebl_Strent *right;
     79   size_t offset;
     80   char reverse[0];
     81 };
     82 
     83 
     84 struct memoryblock
     85 {
     86   struct memoryblock *next;
     87   char memory[0];
     88 };
     89 
     90 
     91 struct Ebl_Strtab
     92 {
     93   struct Ebl_Strent *root;
     94   struct memoryblock *memory;
     95   char *backp;
     96   size_t left;
     97   size_t total;
     98   bool nullstr;
     99 
    100   struct Ebl_Strent null;
    101 };
    102 
    103 
    104 /* Cache for the pagesize.  */
    105 static size_t ps;
    106 /* We correct this value a bit so that `malloc' is not allocating more
    107    than a page. */
    108 #define MALLOC_OVERHEAD (2 * sizeof (void *))
    109 
    110 
    111 struct Ebl_Strtab *
    112 ebl_strtabinit (bool nullstr)
    113 {
    114   if (ps == 0)
    115     {
    116       ps = sysconf (_SC_PAGESIZE);
    117       assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
    118     }
    119 
    120   struct Ebl_Strtab *ret
    121     = (struct Ebl_Strtab *) calloc (1, sizeof (struct Ebl_Strtab));
    122   if (ret != NULL)
    123     {
    124       ret->nullstr = nullstr;
    125 
    126       if (nullstr)
    127 	{
    128 	  ret->null.len = 1;
    129 	  ret->null.string = "";
    130 	}
    131     }
    132 
    133   return ret;
    134 }
    135 
    136 
    137 static int
    138 morememory (struct Ebl_Strtab *st, size_t len)
    139 {
    140   size_t overhead = offsetof (struct memoryblock, memory);
    141   len += overhead + MALLOC_OVERHEAD;
    142 
    143   /* Allocate nearest multiple of pagesize >= len.  */
    144   len = ((len / ps) + (len % ps != 0)) * ps - MALLOC_OVERHEAD;
    145 
    146   struct memoryblock *newmem = (struct memoryblock *) malloc (len);
    147   if (newmem == NULL)
    148     return 1;
    149 
    150   newmem->next = st->memory;
    151   st->memory = newmem;
    152   st->backp = newmem->memory;
    153   st->left = len - overhead;
    154 
    155   return 0;
    156 }
    157 
    158 
    159 void
    160 ebl_strtabfree (struct Ebl_Strtab *st)
    161 {
    162   struct memoryblock *mb = st->memory;
    163 
    164   while (mb != NULL)
    165     {
    166       void *old = mb;
    167       mb = mb->next;
    168       free (old);
    169     }
    170 
    171   free (st);
    172 }
    173 
    174 
    175 static struct Ebl_Strent *
    176 newstring (struct Ebl_Strtab *st, const char *str, size_t len)
    177 {
    178   /* Compute the amount of padding needed to make the structure aligned.  */
    179   size_t align = ((__alignof__ (struct Ebl_Strent)
    180 		   - (((uintptr_t) st->backp)
    181 		      & (__alignof__ (struct Ebl_Strent) - 1)))
    182 		  & (__alignof__ (struct Ebl_Strent) - 1));
    183 
    184   /* Make sure there is enough room in the memory block.  */
    185   if (st->left < align + sizeof (struct Ebl_Strent) + len)
    186     {
    187       if (morememory (st, sizeof (struct Ebl_Strent) + len))
    188 	return NULL;
    189 
    190       align = 0;
    191     }
    192 
    193   /* Create the reserved string.  */
    194   struct Ebl_Strent *newstr = (struct Ebl_Strent *) (st->backp + align);
    195   newstr->string = str;
    196   newstr->len = len;
    197   newstr->next = NULL;
    198   newstr->left = NULL;
    199   newstr->right = NULL;
    200   newstr->offset = 0;
    201   for (int i = len - 2; i >= 0; --i)
    202     newstr->reverse[i] = str[len - 2 - i];
    203   newstr->reverse[len - 1] = '\0';
    204   st->backp += align + sizeof (struct Ebl_Strent) + len;
    205   st->left -= align + sizeof (struct Ebl_Strent) + len;
    206 
    207   return newstr;
    208 }
    209 
    210 
    211 /* XXX This function should definitely be rewritten to use a balancing
    212    tree algorith (AVL, red-black trees).  For now a simple, correct
    213    implementation is enough.  */
    214 static struct Ebl_Strent **
    215 searchstring (struct Ebl_Strent **sep, struct Ebl_Strent *newstr)
    216 {
    217   /* More strings?  */
    218   if (*sep == NULL)
    219     {
    220       *sep = newstr;
    221       return sep;
    222     }
    223 
    224   /* Compare the strings.  */
    225   int cmpres = memcmp ((*sep)->reverse, newstr->reverse,
    226 		       MIN ((*sep)->len, newstr->len) - 1);
    227   if (cmpres == 0)
    228     /* We found a matching string.  */
    229     return sep;
    230   else if (cmpres > 0)
    231     return searchstring (&(*sep)->left, newstr);
    232   else
    233     return searchstring (&(*sep)->right, newstr);
    234 }
    235 
    236 
    237 /* Add new string.  The actual string is assumed to be permanent.  */
    238 struct Ebl_Strent *
    239 ebl_strtabadd (struct Ebl_Strtab *st, const char *str, size_t len)
    240 {
    241   /* Compute the string length if the caller doesn't know it.  */
    242   if (len == 0)
    243     len = strlen (str) + 1;
    244 
    245   /* Make sure all "" strings get offset 0 but only if the table was
    246      created with a special null entry in mind.  */
    247   if (len == 1 && st->null.string != NULL)
    248     return &st->null;
    249 
    250   /* Allocate memory for the new string and its associated information.  */
    251   struct Ebl_Strent *newstr = newstring (st, str, len);
    252   if (newstr == NULL)
    253     return NULL;
    254 
    255   /* Search in the array for the place to insert the string.  If there
    256      is no string with matching prefix and no string with matching
    257      leading substring, create a new entry.  */
    258   struct Ebl_Strent **sep = searchstring (&st->root, newstr);
    259   if (*sep != newstr)
    260     {
    261       /* This is not the same entry.  This means we have a prefix match.  */
    262       if ((*sep)->len > newstr->len)
    263 	{
    264 	  /* Check whether we already know this string.  */
    265 	  for (struct Ebl_Strent *subs = (*sep)->next; subs != NULL;
    266 	       subs = subs->next)
    267 	    if (subs->len == newstr->len)
    268 	      {
    269 		/* We have an exact match with a substring.  Free the memory
    270 		   we allocated.  */
    271 		st->left += st->backp - (char *) newstr;
    272 		st->backp = (char *) newstr;
    273 
    274 		return subs;
    275 	      }
    276 
    277 	  /* We have a new substring.  This means we don't need the reverse
    278 	     string of this entry anymore.  */
    279 	  st->backp -= newstr->len;
    280 	  st->left += newstr->len;
    281 
    282 	  newstr->next = (*sep)->next;
    283 	  (*sep)->next = newstr;
    284 	}
    285       else if ((*sep)->len != newstr->len)
    286 	{
    287 	  /* When we get here it means that the string we are about to
    288 	     add has a common prefix with a string we already have but
    289 	     it is longer.  In this case we have to put it first.  */
    290 	  st->total += newstr->len - (*sep)->len;
    291 	  newstr->next = *sep;
    292 	  newstr->left = (*sep)->left;
    293 	  newstr->right = (*sep)->right;
    294 	  *sep = newstr;
    295 	}
    296       else
    297 	{
    298 	  /* We have an exact match.  Free the memory we allocated.  */
    299 	  st->left += st->backp - (char *) newstr;
    300 	  st->backp = (char *) newstr;
    301 
    302 	  newstr = *sep;
    303 	}
    304     }
    305   else
    306     st->total += newstr->len;
    307 
    308   return newstr;
    309 }
    310 
    311 
    312 static void
    313 copystrings (struct Ebl_Strent *nodep, char **freep, size_t *offsetp)
    314 {
    315   if (nodep->left != NULL)
    316     copystrings (nodep->left, freep, offsetp);
    317 
    318   /* Process the current node.  */
    319   nodep->offset = *offsetp;
    320   *freep = (char *) mempcpy (*freep, nodep->string, nodep->len);
    321   *offsetp += nodep->len;
    322 
    323   for (struct Ebl_Strent *subs = nodep->next; subs != NULL; subs = subs->next)
    324     {
    325       assert (subs->len < nodep->len);
    326       subs->offset = nodep->offset + nodep->len - subs->len;
    327       assert (subs->offset != 0 || subs->string[0] == '\0');
    328     }
    329 
    330   if (nodep->right != NULL)
    331     copystrings (nodep->right, freep, offsetp);
    332 }
    333 
    334 
    335 void
    336 ebl_strtabfinalize (struct Ebl_Strtab *st, Elf_Data *data)
    337 {
    338   size_t nulllen = st->nullstr ? 1 : 0;
    339 
    340   /* Fill in the information.  */
    341   data->d_buf = malloc (st->total + nulllen);
    342   if (data->d_buf == NULL)
    343     abort ();
    344 
    345   /* The first byte must always be zero if we created the table with a
    346      null string.  */
    347   if (st->nullstr)
    348     *((char *) data->d_buf) = '\0';
    349 
    350   data->d_type = ELF_T_BYTE;
    351   data->d_size = st->total + nulllen;
    352   data->d_off = 0;
    353   data->d_align = 1;
    354   data->d_version = EV_CURRENT;
    355 
    356   /* Now run through the tree and add all the string while also updating
    357      the offset members of the elfstrent records.  */
    358   char *endp = (char *) data->d_buf + nulllen;
    359   size_t copylen = nulllen;
    360   if (st->root)
    361     copystrings (st->root, &endp, &copylen);
    362   assert (copylen == st->total + nulllen);
    363 }
    364 
    365 
    366 size_t
    367 ebl_strtaboffset (struct Ebl_Strent *se)
    368 {
    369   return se->offset;
    370 }
    371 
    372 
    373 const char *
    374 ebl_string (struct Ebl_Strent *se)
    375 {
    376   assert (se->string != NULL);
    377 
    378   return se->string;
    379 }
    380