Blame libdwelf/dwelf_strtab.c

Packit 032894
/* ELF/DWARF string table handling.
Packit 032894
   Copyright (C) 2000, 2001, 2002, 2005, 2016 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2000.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <assert.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include <libelf.h>
Packit 032894
#include <stddef.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include "libdwelfP.h"
Packit 032894
#include <system.h>
Packit 032894
Packit 032894
Packit 032894
struct Dwelf_Strent
Packit 032894
{
Packit 032894
  const char *string;
Packit 032894
  size_t len;
Packit 032894
  struct Dwelf_Strent *next;
Packit 032894
  struct Dwelf_Strent *left;
Packit 032894
  struct Dwelf_Strent *right;
Packit 032894
  size_t offset;
Packit 032894
  char reverse[0];
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
struct memoryblock
Packit 032894
{
Packit 032894
  struct memoryblock *next;
Packit 032894
  char memory[0];
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
struct Dwelf_Strtab
Packit 032894
{
Packit 032894
  struct Dwelf_Strent *root;
Packit 032894
  struct memoryblock *memory;
Packit 032894
  char *backp;
Packit 032894
  size_t left;
Packit 032894
  size_t total;
Packit 032894
  bool nullstr;
Packit 032894
Packit 032894
  struct Dwelf_Strent null;
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
/* Cache for the pagesize.  */
Packit 032894
static size_t ps;
Packit 032894
/* We correct this value a bit so that `malloc' is not allocating more
Packit 032894
   than a page. */
Packit 032894
#define MALLOC_OVERHEAD (2 * sizeof (void *))
Packit 032894
Packit 032894
Packit 032894
Dwelf_Strtab *
Packit 032894
dwelf_strtab_init (bool nullstr)
Packit 032894
{
Packit 032894
  if (ps == 0)
Packit 032894
    {
Packit 032894
      ps = sysconf (_SC_PAGESIZE);
Packit 032894
      assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
Packit 032894
    }
Packit 032894
Packit 032894
  Dwelf_Strtab *ret
Packit 032894
    = (Dwelf_Strtab *) calloc (1, sizeof (struct Dwelf_Strtab));
Packit 032894
  if (ret != NULL)
Packit 032894
    {
Packit 032894
      ret->nullstr = nullstr;
Packit 032894
Packit 032894
      if (nullstr)
Packit 032894
	{
Packit 032894
	  ret->null.len = 1;
Packit 032894
	  ret->null.string = "";
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  return ret;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
static int
Packit 032894
morememory (Dwelf_Strtab *st, size_t len)
Packit 032894
{
Packit 032894
  size_t overhead = offsetof (struct memoryblock, memory);
Packit 032894
  len += overhead + MALLOC_OVERHEAD;
Packit 032894
Packit 032894
  /* Allocate nearest multiple of pagesize >= len.  */
Packit 032894
  len = ((len / ps) + (len % ps != 0)) * ps - MALLOC_OVERHEAD;
Packit 032894
Packit 032894
  struct memoryblock *newmem = (struct memoryblock *) malloc (len);
Packit 032894
  if (newmem == NULL)
Packit 032894
    return 1;
Packit 032894
Packit 032894
  newmem->next = st->memory;
Packit 032894
  st->memory = newmem;
Packit 032894
  st->backp = newmem->memory;
Packit 032894
  st->left = len - overhead;
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
void
Packit 032894
dwelf_strtab_free (Dwelf_Strtab *st)
Packit 032894
{
Packit 032894
  struct memoryblock *mb = st->memory;
Packit 032894
Packit 032894
  while (mb != NULL)
Packit 032894
    {
Packit 032894
      void *old = mb;
Packit 032894
      mb = mb->next;
Packit 032894
      free (old);
Packit 032894
    }
Packit 032894
Packit 032894
  free (st);
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
static Dwelf_Strent *
Packit 032894
newstring (Dwelf_Strtab *st, const char *str, size_t len)
Packit 032894
{
Packit 032894
  /* Compute the amount of padding needed to make the structure aligned.  */
Packit 032894
  size_t align = ((__alignof__ (struct Dwelf_Strent)
Packit 032894
		   - (((uintptr_t) st->backp)
Packit 032894
		      & (__alignof__ (struct Dwelf_Strent) - 1)))
Packit 032894
		  & (__alignof__ (struct Dwelf_Strent) - 1));
Packit 032894
Packit 032894
  /* Make sure there is enough room in the memory block.  */
Packit 032894
  if (st->left < align + sizeof (struct Dwelf_Strent) + len)
Packit 032894
    {
Packit 032894
      if (morememory (st, sizeof (struct Dwelf_Strent) + len))
Packit 032894
	return NULL;
Packit 032894
Packit 032894
      align = 0;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Create the reserved string.  */
Packit 032894
  Dwelf_Strent *newstr = (Dwelf_Strent *) (st->backp + align);
Packit 032894
  newstr->string = str;
Packit 032894
  newstr->len = len;
Packit 032894
  newstr->next = NULL;
Packit 032894
  newstr->left = NULL;
Packit 032894
  newstr->right = NULL;
Packit 032894
  newstr->offset = 0;
Packit 032894
  for (int i = len - 2; i >= 0; --i)
Packit 032894
    newstr->reverse[i] = str[len - 2 - i];
Packit 032894
  newstr->reverse[len - 1] = '\0';
Packit 032894
  st->backp += align + sizeof (struct Dwelf_Strent) + len;
Packit 032894
  st->left -= align + sizeof (struct Dwelf_Strent) + len;
Packit 032894
Packit 032894
  return newstr;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
/* XXX This function should definitely be rewritten to use a balancing
Packit 032894
   tree algorith (AVL, red-black trees).  For now a simple, correct
Packit 032894
   implementation is enough.  */
Packit 032894
static Dwelf_Strent **
Packit 032894
searchstring (Dwelf_Strent **sep, Dwelf_Strent *newstr)
Packit 032894
{
Packit 032894
  /* More strings?  */
Packit 032894
  if (*sep == NULL)
Packit 032894
    {
Packit 032894
      *sep = newstr;
Packit 032894
      return sep;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Compare the strings.  */
Packit 032894
  int cmpres = memcmp ((*sep)->reverse, newstr->reverse,
Packit 032894
		       MIN ((*sep)->len, newstr->len) - 1);
Packit 032894
  if (cmpres == 0)
Packit 032894
    /* We found a matching string.  */
Packit 032894
    return sep;
Packit 032894
  else if (cmpres > 0)
Packit 032894
    return searchstring (&(*sep)->left, newstr);
Packit 032894
  else
Packit 032894
    return searchstring (&(*sep)->right, newstr);
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
/* Add new string.  The actual string is assumed to be permanent.  */
Packit 032894
static Dwelf_Strent *
Packit 032894
strtab_add (Dwelf_Strtab *st, const char *str, size_t len)
Packit 032894
{
Packit 032894
  /* Make sure all "" strings get offset 0 but only if the table was
Packit 032894
     created with a special null entry in mind.  */
Packit 032894
  if (len == 1 && st->null.string != NULL)
Packit 032894
    return &st->null;
Packit 032894
Packit 032894
  /* Allocate memory for the new string and its associated information.  */
Packit 032894
  Dwelf_Strent *newstr = newstring (st, str, len);
Packit 032894
  if (newstr == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  /* Search in the array for the place to insert the string.  If there
Packit 032894
     is no string with matching prefix and no string with matching
Packit 032894
     leading substring, create a new entry.  */
Packit 032894
  Dwelf_Strent **sep = searchstring (&st->root, newstr);
Packit 032894
  if (*sep != newstr)
Packit 032894
    {
Packit 032894
      /* This is not the same entry.  This means we have a prefix match.  */
Packit 032894
      if ((*sep)->len > newstr->len)
Packit 032894
	{
Packit 032894
	  /* Check whether we already know this string.  */
Packit 032894
	  for (Dwelf_Strent *subs = (*sep)->next; subs != NULL;
Packit 032894
	       subs = subs->next)
Packit 032894
	    if (subs->len == newstr->len)
Packit 032894
	      {
Packit 032894
		/* We have an exact match with a substring.  Free the memory
Packit 032894
		   we allocated.  */
Packit 032894
		st->left += st->backp - (char *) newstr;
Packit 032894
		st->backp = (char *) newstr;
Packit 032894
Packit 032894
		return subs;
Packit 032894
	      }
Packit 032894
Packit 032894
	  /* We have a new substring.  This means we don't need the reverse
Packit 032894
	     string of this entry anymore.  */
Packit 032894
	  st->backp -= newstr->len;
Packit 032894
	  st->left += newstr->len;
Packit 032894
Packit 032894
	  newstr->next = (*sep)->next;
Packit 032894
	  (*sep)->next = newstr;
Packit 032894
	}
Packit 032894
      else if ((*sep)->len != newstr->len)
Packit 032894
	{
Packit 032894
	  /* When we get here it means that the string we are about to
Packit 032894
	     add has a common prefix with a string we already have but
Packit 032894
	     it is longer.  In this case we have to put it first.  */
Packit 032894
	  st->total += newstr->len - (*sep)->len;
Packit 032894
	  newstr->next = *sep;
Packit 032894
	  newstr->left = (*sep)->left;
Packit 032894
	  newstr->right = (*sep)->right;
Packit 032894
	  *sep = newstr;
Packit 032894
	}
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  /* We have an exact match.  Free the memory we allocated.  */
Packit 032894
	  st->left += st->backp - (char *) newstr;
Packit 032894
	  st->backp = (char *) newstr;
Packit 032894
Packit 032894
	  newstr = *sep;
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else
Packit 032894
    st->total += newstr->len;
Packit 032894
Packit 032894
  return newstr;
Packit 032894
}
Packit 032894
Packit 032894
Dwelf_Strent *
Packit 032894
dwelf_strtab_add (Dwelf_Strtab *st, const char *str)
Packit 032894
{
Packit 032894
  return strtab_add (st, str, strlen (str) + 1);
Packit 032894
}
Packit 032894
Packit 032894
Dwelf_Strent *
Packit 032894
dwelf_strtab_add_len (Dwelf_Strtab *st, const char *str, size_t len)
Packit 032894
{
Packit 032894
  return strtab_add (st, str, len);
Packit 032894
}
Packit 032894
Packit 032894
static void
Packit 032894
copystrings (Dwelf_Strent *nodep, char **freep, size_t *offsetp)
Packit 032894
{
Packit 032894
  if (nodep->left != NULL)
Packit 032894
    copystrings (nodep->left, freep, offsetp);
Packit 032894
Packit 032894
  /* Process the current node.  */
Packit 032894
  nodep->offset = *offsetp;
Packit 032894
  *freep = (char *) mempcpy (*freep, nodep->string, nodep->len);
Packit 032894
  *offsetp += nodep->len;
Packit 032894
Packit 032894
  for (Dwelf_Strent *subs = nodep->next; subs != NULL; subs = subs->next)
Packit 032894
    {
Packit 032894
      assert (subs->len < nodep->len);
Packit 032894
      subs->offset = nodep->offset + nodep->len - subs->len;
Packit 032894
      assert (subs->offset != 0 || subs->string[0] == '\0');
Packit 032894
    }
Packit 032894
Packit 032894
  if (nodep->right != NULL)
Packit 032894
    copystrings (nodep->right, freep, offsetp);
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
Elf_Data *
Packit 032894
dwelf_strtab_finalize (Dwelf_Strtab *st, Elf_Data *data)
Packit 032894
{
Packit 032894
  size_t nulllen = st->nullstr ? 1 : 0;
Packit 032894
Packit 032894
  /* Fill in the information.  */
Packit 032894
  data->d_buf = malloc (st->total + nulllen);
Packit 032894
  if (data->d_buf == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  /* The first byte must always be zero if we created the table with a
Packit 032894
     null string.  */
Packit 032894
  if (st->nullstr)
Packit 032894
    *((char *) data->d_buf) = '\0';
Packit 032894
Packit 032894
  data->d_type = ELF_T_BYTE;
Packit 032894
  data->d_size = st->total + nulllen;
Packit 032894
  data->d_off = 0;
Packit 032894
  data->d_align = 1;
Packit 032894
  data->d_version = EV_CURRENT;
Packit 032894
Packit 032894
  /* Now run through the tree and add all the string while also updating
Packit 032894
     the offset members of the elfstrent records.  */
Packit 032894
  char *endp = (char *) data->d_buf + nulllen;
Packit 032894
  size_t copylen = nulllen;
Packit 032894
  if (st->root)
Packit 032894
    copystrings (st->root, &endp, &copylen);
Packit 032894
  assert (copylen == st->total + nulllen);
Packit 032894
Packit 032894
  return data;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
size_t
Packit 032894
dwelf_strent_off (Dwelf_Strent *se)
Packit 032894
{
Packit 032894
  return se->offset;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
const char *
Packit 032894
dwelf_strent_str (Dwelf_Strent *se)
Packit 032894
{
Packit 032894
  return se->string;
Packit 032894
}