1 /* Get attributes of the DIE.
2 Copyright (C) 2004, 2005, 2008, 2009, 2014, 2017 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2004.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
38 dwarf_getattrs (Dwarf_Die *die, int (*callback) (Dwarf_Attribute *, void *),
39 void *arg, ptrdiff_t offset)
44 if (unlikely (offset == 1))
47 const unsigned char *die_addr = NULL;
49 /* Find the abbreviation entry. */
50 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &die_addr);
52 if (unlikely (abbrevp == DWARF_END_ABBREV))
54 __libdw_seterrno (DWARF_E_INVALID_DWARF);
58 /* This is where the attributes start. */
59 const unsigned char *attrp = abbrevp->attrp;
60 const unsigned char *const offset_attrp = abbrevp->attrp + offset;
62 /* Go over the list of attributes. */
65 /* Get attribute name and form. Dwarf_Abbrev was checked when
66 created, so we can read unchecked. */
68 const unsigned char *remembered_attrp = attrp;
70 get_uleb128_unchecked (attr.code, attrp);
71 get_uleb128_unchecked (attr.form, attrp);
73 /* We can stop if we found the attribute with value zero. */
74 if (attr.code == 0 && attr.form == 0)
75 /* Do not return 0 here - there would be no way to
76 distinguish this value from the attribute at offset 0.
77 Instead we return +1 which would never be a valid
78 offset of an attribute. */
81 /* If we are not to OFFSET_ATTRP yet, we just have to skip
82 the values of the intervening attributes. */
83 if (remembered_attrp >= offset_attrp)
85 /* Fill in the rest. */
86 if (attr.form == DW_FORM_implicit_const)
87 attr.valp = (unsigned char *) attrp;
89 attr.valp = (unsigned char *) die_addr;
92 /* Now call the callback function. */
93 if (callback (&attr, arg) != DWARF_CB_OK)
94 /* Return the offset of the start of the attribute, so that
95 dwarf_getattrs() can be restarted from this point if the
97 return remembered_attrp - abbrevp->attrp;
100 /* Skip over the rest of this attribute (if there is any). */
103 size_t len = __libdw_form_val_len (die->cu, attr.form, die_addr);
104 if (unlikely (len == (size_t) -1l))
105 /* Something wrong with the file. */
108 // __libdw_form_val_len will have done a bounds check.
111 if (attr.form == DW_FORM_implicit_const)
113 int64_t attr_value __attribute__((__unused__));
114 get_sleb128_unchecked (attr_value, attrp);