1 /* Return child of current DIE.
2 Copyright (C) 2003-2011, 2014, 2017 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2003.
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/>. */
37 /* Some arbitrary value not conflicting with any existing code. */
38 #define INVALID 0xffffe444
43 __libdw_find_attr (Dwarf_Die *die, unsigned int search_name,
44 unsigned int *codep, unsigned int *formp)
46 const unsigned char *readp = NULL;
48 /* Find the abbreviation entry. */
49 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &readp);
50 if (unlikely (abbrevp == DWARF_END_ABBREV))
52 __libdw_seterrno (DWARF_E_INVALID_DWARF);
56 /* Search the name attribute. Attribute has been checked when
57 Dwarf_Abbrev was created, we can read unchecked. */
58 const unsigned char *attrp = abbrevp->attrp;
61 /* Get attribute name and form. */
62 unsigned int attr_name;
63 get_uleb128_unchecked (attr_name, attrp);
65 unsigned int attr_form;
66 get_uleb128_unchecked (attr_form, attrp);
68 /* We can stop if we found the attribute with value zero. */
69 if (attr_name == 0 && attr_form == 0)
72 /* Is this the name attribute? */
73 if (attr_name == search_name && search_name != INVALID)
80 /* Normally the attribute data comes from the DIE/info,
81 except for implicit_form, where it comes from the abbrev. */
82 if (attr_form == DW_FORM_implicit_const)
83 return (unsigned char *) attrp;
85 return (unsigned char *) readp;
88 /* Skip over the rest of this attribute (if there is any). */
91 size_t len = __libdw_form_val_len (die->cu, attr_form, readp);
92 if (unlikely (len == (size_t) -1l))
98 // __libdw_form_val_len will have done a bounds check.
101 // If the value is in the abbrev data, skip it.
102 if (attr_form == DW_FORM_implicit_const)
104 int64_t attr_value __attribute__((__unused__));
105 get_sleb128_unchecked (attr_value, attrp);
110 // XXX Do we need other values?
116 return (unsigned char *) readp;
121 dwarf_child (Dwarf_Die *die, Dwarf_Die *result)
123 /* Ignore previous errors. */
127 /* Find the abbreviation entry. */
128 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, NULL);
129 if (unlikely (abbrevp == DWARF_END_ABBREV))
131 __libdw_seterrno (DWARF_E_INVALID_DWARF);
135 /* If there are no children, do not search. */
136 if (! abbrevp->has_children)
139 /* Skip past the last attribute. */
140 void *addr = __libdw_find_attr (die, INVALID, NULL, NULL);
145 /* RESULT can be the same as DIE. So preserve what we need. */
146 struct Dwarf_CU *cu = die->cu;
148 /* It's kosher (just suboptimal) to have a null entry first thing (7.5.3).
149 So if this starts with ULEB128 of 0 (even with silly encoding of 0),
150 it is a kosher null entry and we do not really have any children. */
151 const unsigned char *code = addr;
152 const unsigned char *endp = cu->endp;
155 if (unlikely (code >= endp)) /* Truncated section. */
157 if (unlikely (*code == 0x80))
162 if (unlikely (*code == '\0'))
165 /* Clear the entire DIE structure. This signals we have not yet
166 determined any of the information. */
167 memset (result, '\0', sizeof (Dwarf_Die));
169 /* We have the address. */
172 /* Same CU as the parent. */