Blame libdw/dwarf_getpubnames.c

Packit 032894
/* Get public symbol information.
Packit 032894
   Copyright (C) 2002, 2003, 2004, 2005, 2008 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
Packit 032894
#include <libdwP.h>
Packit 032894
#include <dwarf.h>
Packit 032894
#include <system.h>
Packit 032894
Packit 032894
Packit 032894
static int
Packit 032894
get_offsets (Dwarf *dbg)
Packit 032894
{
Packit 032894
  size_t allocated = 0;
Packit 032894
  size_t cnt = 0;
Packit 032894
  struct pubnames_s *mem = NULL;
Packit 032894
  const size_t entsize = sizeof (struct pubnames_s);
Packit 032894
  unsigned char *const startp = dbg->sectiondata[IDX_debug_pubnames]->d_buf;
Packit 032894
  unsigned char *readp = startp;
Packit 032894
  unsigned char *endp = readp + dbg->sectiondata[IDX_debug_pubnames]->d_size;
Packit 032894
Packit 032894
  while (readp + 14 < endp)
Packit 032894
    {
Packit 032894
      /* If necessary, allocate more entries.  */
Packit 032894
      if (cnt >= allocated)
Packit 032894
	{
Packit 032894
	  allocated = MAX (10, 2 * allocated);
Packit 032894
	  struct pubnames_s *newmem
Packit 032894
	    = (struct pubnames_s *) realloc (mem, allocated * entsize);
Packit 032894
	  if (newmem == NULL)
Packit 032894
	    {
Packit 032894
	      __libdw_seterrno (DWARF_E_NOMEM);
Packit 032894
	    err_return:
Packit 032894
	      free (mem);
Packit 032894
	      return -1;
Packit 032894
	    }
Packit 032894
Packit 032894
	  mem = newmem;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Read the set header.  */
Packit 032894
      int len_bytes = 4;
Packit 032894
      Dwarf_Off len = read_4ubyte_unaligned_inc (dbg, readp);
Packit 032894
      if (len == DWARF3_LENGTH_64_BIT)
Packit 032894
	{
Packit 032894
	  len = read_8ubyte_unaligned_inc (dbg, readp);
Packit 032894
	  len_bytes = 8;
Packit 032894
	}
Packit 032894
      else if (unlikely (len >= DWARF3_LENGTH_MIN_ESCAPE_CODE
Packit 032894
			 && len <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
Packit 032894
	{
Packit 032894
	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
Packit 032894
	  goto err_return;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Now we know the offset of the first offset/name pair.  */
Packit 032894
      mem[cnt].set_start = readp + 2 + 2 * len_bytes - startp;
Packit 032894
      mem[cnt].address_len = len_bytes;
Packit 032894
      size_t max_size = dbg->sectiondata[IDX_debug_pubnames]->d_size;
Packit 032894
      if (mem[cnt].set_start >= max_size
Packit 032894
	  || len - (2 + 2 * len_bytes) > max_size - mem[cnt].set_start)
Packit 032894
	/* Something wrong, the first entry is beyond the end of
Packit 032894
	   the section.  Or the length of the whole unit is too big.  */
Packit 032894
	break;
Packit 032894
Packit 032894
      /* Read the version.  It better be two for now.  */
Packit 032894
      uint16_t version = read_2ubyte_unaligned (dbg, readp);
Packit 032894
      if (unlikely (version != 2))
Packit 032894
	{
Packit 032894
	  __libdw_seterrno (DWARF_E_INVALID_VERSION);
Packit 032894
	  goto err_return;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Get the CU offset.  */
Packit 032894
      if (__libdw_read_offset (dbg, dbg, IDX_debug_pubnames,
Packit 032894
			       readp + 2, len_bytes,
Packit 032894
			       &mem[cnt].cu_offset, IDX_debug_info, 3))
Packit 032894
	/* Error has been already set in reader.  */
Packit 032894
	goto err_return;
Packit 032894
Packit 032894
      /* Determine the size of the CU header.  */
Packit 032894
      unsigned char *infop
Packit 032894
	= ((unsigned char *) dbg->sectiondata[IDX_debug_info]->d_buf
Packit 032894
	   + mem[cnt].cu_offset);
Packit 032894
      if (read_4ubyte_unaligned_noncvt (infop) == DWARF3_LENGTH_64_BIT)
Packit 032894
	mem[cnt].cu_header_size = 23;
Packit 032894
      else
Packit 032894
	mem[cnt].cu_header_size = 11;
Packit 032894
Packit 032894
      ++cnt;
Packit 032894
Packit 032894
      /* Advance to the next set.  */
Packit 032894
      readp += len;
Packit 032894
    }
Packit 032894
Packit 032894
  if (mem == NULL || cnt == 0)
Packit 032894
    {
Packit 032894
      free (mem);
Packit 032894
      __libdw_seterrno (DWARF_E_NO_ENTRY);
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  dbg->pubnames_sets = (struct pubnames_s *) realloc (mem, cnt * entsize);
Packit 032894
  dbg->pubnames_nsets = cnt;
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
ptrdiff_t
Packit 032894
dwarf_getpubnames (Dwarf *dbg,
Packit 032894
		   int (*callback) (Dwarf *, Dwarf_Global *, void *),
Packit 032894
		   void *arg, ptrdiff_t offset)
Packit 032894
{
Packit 032894
  if (dbg == NULL)
Packit 032894
    return -1l;
Packit 032894
Packit 032894
  if (unlikely (offset < 0))
Packit 032894
    {
Packit 032894
      __libdw_seterrno (DWARF_E_INVALID_OFFSET);
Packit 032894
      return -1l;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Make sure it is a valid offset.  */
Packit 032894
  if (unlikely (dbg->sectiondata[IDX_debug_pubnames] == NULL
Packit 032894
		|| ((size_t) offset
Packit 032894
		    >= dbg->sectiondata[IDX_debug_pubnames]->d_size)))
Packit 032894
    /* No (more) entry.  */
Packit 032894
    return 0;
Packit 032894
Packit 032894
  /* If necessary read the set information.  */
Packit 032894
  if (dbg->pubnames_nsets == 0 && unlikely (get_offsets (dbg) != 0))
Packit 032894
    return -1l;
Packit 032894
Packit 032894
  /* Find the place where to start.  */
Packit 032894
  size_t cnt;
Packit 032894
  if (offset == 0)
Packit 032894
    {
Packit 032894
      cnt = 0;
Packit 032894
      offset = dbg->pubnames_sets[0].set_start;
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      for (cnt = 0; cnt + 1 < dbg->pubnames_nsets; ++cnt)
Packit 032894
	if ((Dwarf_Off) offset >= dbg->pubnames_sets[cnt].set_start)
Packit 032894
	  {
Packit 032894
	    assert ((Dwarf_Off) offset
Packit 032894
		    < dbg->pubnames_sets[cnt + 1].set_start);
Packit 032894
	    break;
Packit 032894
	  }
Packit 032894
      assert (cnt + 1 < dbg->pubnames_nsets);
Packit 032894
    }
Packit 032894
Packit 032894
  unsigned char *startp
Packit 032894
    = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
Packit 032894
  unsigned char *endp
Packit 032894
    = startp + dbg->sectiondata[IDX_debug_pubnames]->d_size;
Packit 032894
  unsigned char *readp = startp + offset;
Packit 032894
  while (1)
Packit 032894
    {
Packit 032894
      Dwarf_Global gl;
Packit 032894
Packit 032894
      gl.cu_offset = (dbg->pubnames_sets[cnt].cu_offset
Packit 032894
		      + dbg->pubnames_sets[cnt].cu_header_size);
Packit 032894
Packit 032894
      while (1)
Packit 032894
	{
Packit 032894
	  /* READP points to the next offset/name pair.  */
Packit 032894
	  if (readp + dbg->pubnames_sets[cnt].address_len > endp)
Packit 032894
	    goto invalid_dwarf;
Packit 032894
	  if (dbg->pubnames_sets[cnt].address_len == 4)
Packit 032894
	    gl.die_offset = read_4ubyte_unaligned_inc (dbg, readp);
Packit 032894
	  else
Packit 032894
	    gl.die_offset = read_8ubyte_unaligned_inc (dbg, readp);
Packit 032894
Packit 032894
	  /* If the offset is zero we reached the end of the set.  */
Packit 032894
	  if (gl.die_offset == 0)
Packit 032894
	    break;
Packit 032894
Packit 032894
	  /* Add the CU offset.  */
Packit 032894
	  gl.die_offset += dbg->pubnames_sets[cnt].cu_offset;
Packit 032894
Packit 032894
	  gl.name = (char *) readp;
Packit 032894
	  readp = (unsigned char *) memchr (gl.name, '\0', endp - readp);
Packit 032894
	  if (unlikely (readp == NULL))
Packit 032894
	    {
Packit 032894
	    invalid_dwarf:
Packit 032894
	      __libdw_seterrno (DWARF_E_INVALID_DWARF);
Packit 032894
	      return -1l;
Packit 032894
	    }
Packit 032894
	  readp++;
Packit 032894
Packit 032894
	  /* We found name and DIE offset.  Report it.  */
Packit 032894
	  if (callback (dbg, &gl, arg) != DWARF_CB_OK)
Packit 032894
	    {
Packit 032894
	      /* The user wants us to stop.  Return the offset of the
Packit 032894
		 next entry.  */
Packit 032894
	      return readp - startp;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
Packit 032894
      if (++cnt == dbg->pubnames_nsets)
Packit 032894
	/* This was the last set.  */
Packit 032894
	break;
Packit 032894
Packit 032894
      startp = (unsigned char *) dbg->sectiondata[IDX_debug_pubnames]->d_buf;
Packit 032894
      readp = startp + dbg->pubnames_sets[cnt].set_start;
Packit 032894
    }
Packit 032894
Packit 032894
  /* We are done.  No more entries.  */
Packit 032894
  return 0;
Packit 032894
}