Blame libdw/dwarf_getattrs.c

Packit Service 97d2fb
/* Get attributes of the DIE.
Packit Service 97d2fb
   Copyright (C) 2004, 2005, 2008, 2009, 2014, 2017 Red Hat, Inc.
Packit Service 97d2fb
   This file is part of elfutils.
Packit Service 97d2fb
   Written by Ulrich Drepper <drepper@redhat.com>, 2004.
Packit Service 97d2fb
Packit Service 97d2fb
   This file is free software; you can redistribute it and/or modify
Packit Service 97d2fb
   it under the terms of either
Packit Service 97d2fb
Packit Service 97d2fb
     * the GNU Lesser General Public License as published by the Free
Packit Service 97d2fb
       Software Foundation; either version 3 of the License, or (at
Packit Service 97d2fb
       your option) any later version
Packit Service 97d2fb
Packit Service 97d2fb
   or
Packit Service 97d2fb
Packit Service 97d2fb
     * the GNU General Public License as published by the Free
Packit Service 97d2fb
       Software Foundation; either version 2 of the License, or (at
Packit Service 97d2fb
       your option) any later version
Packit Service 97d2fb
Packit Service 97d2fb
   or both in parallel, as here.
Packit Service 97d2fb
Packit Service 97d2fb
   elfutils is distributed in the hope that it will be useful, but
Packit Service 97d2fb
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 97d2fb
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 97d2fb
   General Public License for more details.
Packit Service 97d2fb
Packit Service 97d2fb
   You should have received copies of the GNU General Public License and
Packit Service 97d2fb
   the GNU Lesser General Public License along with this program.  If
Packit Service 97d2fb
   not, see <http://www.gnu.org/licenses/>.  */
Packit Service 97d2fb
Packit Service 97d2fb
#ifdef HAVE_CONFIG_H
Packit Service 97d2fb
# include <config.h>
Packit Service 97d2fb
#endif
Packit Service 97d2fb
Packit Service 97d2fb
#include "libdwP.h"
Packit Service 97d2fb
Packit Service 97d2fb
Packit Service 97d2fb
ptrdiff_t
Packit Service 97d2fb
dwarf_getattrs (Dwarf_Die *die, int (*callback) (Dwarf_Attribute *, void *),
Packit Service 97d2fb
		void *arg, ptrdiff_t offset)
Packit Service 97d2fb
{
Packit Service 97d2fb
  if (die == NULL)
Packit Service 97d2fb
    return -1l;
Packit Service 97d2fb
Packit Service 97d2fb
  if (unlikely (offset == 1))
Packit Service 97d2fb
    return 1;
Packit Service 97d2fb
Packit Service 97d2fb
  const unsigned char *die_addr = NULL;
Packit Service 97d2fb
Packit Service 97d2fb
  /* Find the abbreviation entry.  */
Packit Service 97d2fb
  Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &die_addr);
Packit Service 97d2fb
Packit Service 97d2fb
  if (unlikely (abbrevp == DWARF_END_ABBREV))
Packit Service 97d2fb
    {
Packit Service 97d2fb
      __libdw_seterrno (DWARF_E_INVALID_DWARF);
Packit Service 97d2fb
      return -1l;
Packit Service 97d2fb
    }
Packit Service 97d2fb
Packit Service 97d2fb
  /* This is where the attributes start.  */
Packit Service 97d2fb
  const unsigned char *attrp = abbrevp->attrp;
Packit Service 97d2fb
  const unsigned char *const offset_attrp = abbrevp->attrp + offset;
Packit Service 97d2fb
Packit Service 97d2fb
  /* Go over the list of attributes.  */
Packit Service 97d2fb
  while (1)
Packit Service 97d2fb
    {
Packit Service 97d2fb
      /* Get attribute name and form.  Dwarf_Abbrev was checked when
Packit Service 97d2fb
	 created, so we can read unchecked.  */
Packit Service 97d2fb
      Dwarf_Attribute attr;
Packit Service 97d2fb
      const unsigned char *remembered_attrp = attrp;
Packit Service 97d2fb
Packit Service 97d2fb
      get_uleb128_unchecked (attr.code, attrp);
Packit Service 97d2fb
      get_uleb128_unchecked (attr.form, attrp);
Packit Service 97d2fb
Packit Service 97d2fb
      /* We can stop if we found the attribute with value zero.  */
Packit Service 97d2fb
      if (attr.code == 0 && attr.form == 0)
Packit Service 97d2fb
	/* Do not return 0 here - there would be no way to
Packit Service 97d2fb
	   distinguish this value from the attribute at offset 0.
Packit Service 97d2fb
	   Instead we return +1 which would never be a valid
Packit Service 97d2fb
	   offset of an attribute.  */
Packit Service 97d2fb
        return 1l;
Packit Service 97d2fb
Packit Service 97d2fb
      /* If we are not to OFFSET_ATTRP yet, we just have to skip
Packit Service 97d2fb
	 the values of the intervening attributes.  */
Packit Service 97d2fb
      if (remembered_attrp >= offset_attrp)
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  /* Fill in the rest.  */
Packit Service 97d2fb
	  if (attr.form == DW_FORM_implicit_const)
Packit Service 97d2fb
	    attr.valp = (unsigned char *) attrp;
Packit Service 97d2fb
	  else
Packit Service 97d2fb
	    attr.valp = (unsigned char *) die_addr;
Packit Service 97d2fb
	  attr.cu = die->cu;
Packit Service 97d2fb
Packit Service 97d2fb
	  /* Now call the callback function.  */
Packit Service 97d2fb
	  if (callback (&attr, arg) != DWARF_CB_OK)
Packit Service 97d2fb
	    /* Return the offset of the start of the attribute, so that
Packit Service 97d2fb
	       dwarf_getattrs() can be restarted from this point if the
Packit Service 97d2fb
	       caller so desires.  */
Packit Service 97d2fb
	    return remembered_attrp - abbrevp->attrp;
Packit Service 97d2fb
	}
Packit Service 97d2fb
Packit Service 97d2fb
      /* Skip over the rest of this attribute (if there is any).  */
Packit Service 97d2fb
      if (attr.form != 0)
Packit Service 97d2fb
	{
Packit Service 97d2fb
	  size_t len = __libdw_form_val_len (die->cu, attr.form, die_addr);
Packit Service 97d2fb
	  if (unlikely (len == (size_t) -1l))
Packit Service 97d2fb
	    /* Something wrong with the file.  */
Packit Service 97d2fb
	    return -1l;
Packit Service 97d2fb
Packit Service 97d2fb
	  // __libdw_form_val_len will have done a bounds check.
Packit Service 97d2fb
	  die_addr += len;
Packit Service 97d2fb
Packit Service 97d2fb
	  if (attr.form == DW_FORM_implicit_const)
Packit Service 97d2fb
	    {
Packit Service 97d2fb
	      int64_t attr_value __attribute__((__unused__));
Packit Service 97d2fb
	      get_sleb128_unchecked (attr_value, attrp);
Packit Service 97d2fb
	    }
Packit Service 97d2fb
	}
Packit Service 97d2fb
    }
Packit Service 97d2fb
  /* NOTREACHED */
Packit Service 97d2fb
}