1 /* Return child of current DIE.
2 Copyright (C) 2003-2011 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 = (unsigned char *) die->addr;
49 /* First we have to get the abbreviation code so that we can decode
50 the data in the DIE. */
51 unsigned int abbrev_code;
52 get_uleb128 (abbrev_code, readp);
54 /* Find the abbreviation entry. */
55 Dwarf_Abbrev *abbrevp = die->abbrev;
58 abbrevp = __libdw_findabbrev (die->cu, abbrev_code);
59 die->abbrev = abbrevp ?: DWARF_END_ABBREV;
61 if (unlikely (die->abbrev == DWARF_END_ABBREV))
64 __libdw_seterrno (DWARF_E_INVALID_DWARF);
68 /* Search the name attribute. */
69 unsigned char *const endp
70 = ((unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf
71 + dbg->sectiondata[IDX_debug_abbrev]->d_size);
73 const unsigned char *attrp = die->abbrev->attrp;
76 /* Are we still in bounds? This test needs to be refined. */
77 if (unlikely (attrp + 1 >= endp))
80 /* Get attribute name and form.
82 XXX We don't check whether this reads beyond the end of the
84 unsigned int attr_name;
85 get_uleb128 (attr_name, attrp);
86 unsigned int attr_form;
87 get_uleb128 (attr_form, attrp);
89 /* We can stop if we found the attribute with value zero. */
90 if (attr_name == 0 && attr_form == 0)
93 /* Is this the name attribute? */
94 if (attr_name == search_name && search_name != INVALID)
101 return (unsigned char *) readp;
104 /* Skip over the rest of this attribute (if there is any). */
107 size_t len = __libdw_form_val_len (dbg, die->cu, attr_form, readp);
109 if (unlikely (len == (size_t) -1l))
115 // XXX We need better boundary checks.
120 // XXX Do we need other values?
126 return (unsigned char *) readp;
131 dwarf_child (die, result)
135 /* Ignore previous errors. */
139 /* Skip past the last attribute. */
142 /* If we already know there are no children do not search. */
143 if (die->abbrev != DWARF_END_ABBREV
144 && (die->abbrev == NULL || die->abbrev->has_children))
145 addr = __libdw_find_attr (die, INVALID, NULL, NULL);
146 if (unlikely (die->abbrev == (Dwarf_Abbrev *) -1l))
149 /* Make sure the DIE really has children. */
150 if (! die->abbrev->has_children)
151 /* There cannot be any children. */
157 /* RESULT can be the same as DIE. So preserve what we need. */
158 struct Dwarf_CU *cu = die->cu;
159 Elf_Data *cu_sec = cu_data (cu);
161 /* It's kosher (just suboptimal) to have a null entry first thing (7.5.3).
162 So if this starts with ULEB128 of 0 (even with silly encoding of 0),
163 it is a kosher null entry and we do not really have any children. */
164 const unsigned char *code = addr;
165 const unsigned char *endp = (cu_sec->d_buf + cu_sec->d_size);
168 if (unlikely (code >= endp)) /* Truncated section. */
170 if (unlikely (*code == 0x80))
175 if (unlikely (*code == '\0'))
178 /* Clear the entire DIE structure. This signals we have not yet
179 determined any of the information. */
180 memset (result, '\0', sizeof (Dwarf_Die));
182 /* We have the address. */
185 /* Same CU as the parent. */