1 /* Return child of current DIE.
2 Copyright (C) 2003-2011, 2014 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 Dwarf *dbg = die->cu->dbg;
47 const unsigned char *readp;
49 /* Find the abbreviation entry. */
50 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &readp);
51 if (unlikely (abbrevp == DWARF_END_ABBREV))
54 __libdw_seterrno (DWARF_E_INVALID_DWARF);
58 /* Search the name attribute. */
59 unsigned char *const endp
60 = ((unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf
61 + dbg->sectiondata[IDX_debug_abbrev]->d_size);
63 const unsigned char *attrp = abbrevp->attrp;
66 /* Get attribute name and form. */
67 if (unlikely (attrp >= endp))
69 unsigned int attr_name;
70 get_uleb128 (attr_name, attrp, endp);
72 if (unlikely (attrp >= endp))
74 unsigned int attr_form;
75 get_uleb128 (attr_form, attrp, endp);
77 /* We can stop if we found the attribute with value zero. */
78 if (attr_name == 0 && attr_form == 0)
81 /* Is this the name attribute? */
82 if (attr_name == search_name && search_name != INVALID)
89 return (unsigned char *) readp;
92 /* Skip over the rest of this attribute (if there is any). */
95 size_t len = __libdw_form_val_len (die->cu, attr_form, readp);
96 if (unlikely (len == (size_t) -1l))
102 // __libdw_form_val_len will have done a bounds check.
107 // XXX Do we need other values?
113 return (unsigned char *) readp;
118 dwarf_child (Dwarf_Die *die, Dwarf_Die *result)
120 /* Ignore previous errors. */
124 /* Find the abbreviation entry. */
125 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, NULL);
126 if (unlikely (abbrevp == DWARF_END_ABBREV))
128 __libdw_seterrno (DWARF_E_INVALID_DWARF);
132 /* If there are no children, do not search. */
133 if (! abbrevp->has_children)
136 /* Skip past the last attribute. */
137 void *addr = __libdw_find_attr (die, INVALID, NULL, NULL);
142 /* RESULT can be the same as DIE. So preserve what we need. */
143 struct Dwarf_CU *cu = die->cu;
145 /* It's kosher (just suboptimal) to have a null entry first thing (7.5.3).
146 So if this starts with ULEB128 of 0 (even with silly encoding of 0),
147 it is a kosher null entry and we do not really have any children. */
148 const unsigned char *code = addr;
149 const unsigned char *endp = cu->endp;
152 if (unlikely (code >= endp)) /* Truncated section. */
154 if (unlikely (*code == 0x80))
159 if (unlikely (*code == '\0'))
162 /* Clear the entire DIE structure. This signals we have not yet
163 determined any of the information. */
164 memset (result, '\0', sizeof (Dwarf_Die));
166 /* We have the address. */
169 /* Same CU as the parent. */