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.
5
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
8
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
12
13 or
14
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
18
19 or both in parallel, as here.
20
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.
25
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/>. */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include "libdwP.h"
35 #include <string.h>
36
37 /* Some arbitrary value not conflicting with any existing code. */
38 #define INVALID 0xffffe444
39
40
41 unsigned char *
42 internal_function
__libdw_find_attr(Dwarf_Die * die,unsigned int search_name,unsigned int * codep,unsigned int * formp)43 __libdw_find_attr (Dwarf_Die *die, unsigned int search_name,
44 unsigned int *codep, unsigned int *formp)
45 {
46 Dwarf *dbg = die->cu->dbg;
47 const unsigned char *readp;
48
49 /* Find the abbreviation entry. */
50 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &readp);
51 if (unlikely (abbrevp == DWARF_END_ABBREV))
52 {
53 invalid_dwarf:
54 __libdw_seterrno (DWARF_E_INVALID_DWARF);
55 return NULL;
56 }
57
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);
62
63 const unsigned char *attrp = abbrevp->attrp;
64 while (1)
65 {
66 /* Get attribute name and form. */
67 if (unlikely (attrp >= endp))
68 goto invalid_dwarf;
69 unsigned int attr_name;
70 get_uleb128 (attr_name, attrp, endp);
71
72 if (unlikely (attrp >= endp))
73 goto invalid_dwarf;
74 unsigned int attr_form;
75 get_uleb128 (attr_form, attrp, endp);
76
77 /* We can stop if we found the attribute with value zero. */
78 if (attr_name == 0 && attr_form == 0)
79 break;
80
81 /* Is this the name attribute? */
82 if (attr_name == search_name && search_name != INVALID)
83 {
84 if (codep != NULL)
85 *codep = attr_name;
86 if (formp != NULL)
87 *formp = attr_form;
88
89 return (unsigned char *) readp;
90 }
91
92 /* Skip over the rest of this attribute (if there is any). */
93 if (attr_form != 0)
94 {
95 size_t len = __libdw_form_val_len (die->cu, attr_form, readp);
96 if (unlikely (len == (size_t) -1l))
97 {
98 readp = NULL;
99 break;
100 }
101
102 // __libdw_form_val_len will have done a bounds check.
103 readp += len;
104 }
105 }
106
107 // XXX Do we need other values?
108 if (codep != NULL)
109 *codep = INVALID;
110 if (formp != NULL)
111 *formp = INVALID;
112
113 return (unsigned char *) readp;
114 }
115
116
117 int
dwarf_child(die,result)118 dwarf_child (die, result)
119 Dwarf_Die *die;
120 Dwarf_Die *result;
121 {
122 /* Ignore previous errors. */
123 if (die == NULL)
124 return -1;
125
126 /* Find the abbreviation entry. */
127 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, NULL);
128 if (unlikely (abbrevp == DWARF_END_ABBREV))
129 {
130 __libdw_seterrno (DWARF_E_INVALID_DWARF);
131 return -1;
132 }
133
134 /* If there are no children, do not search. */
135 if (! abbrevp->has_children)
136 return 1;
137
138 /* Skip past the last attribute. */
139 void *addr = __libdw_find_attr (die, INVALID, NULL, NULL);
140
141 if (addr == NULL)
142 return -1;
143
144 /* RESULT can be the same as DIE. So preserve what we need. */
145 struct Dwarf_CU *cu = die->cu;
146
147 /* It's kosher (just suboptimal) to have a null entry first thing (7.5.3).
148 So if this starts with ULEB128 of 0 (even with silly encoding of 0),
149 it is a kosher null entry and we do not really have any children. */
150 const unsigned char *code = addr;
151 const unsigned char *endp = cu->endp;
152 while (1)
153 {
154 if (unlikely (code >= endp)) /* Truncated section. */
155 return 1;
156 if (unlikely (*code == 0x80))
157 ++code;
158 else
159 break;
160 }
161 if (unlikely (*code == '\0'))
162 return 1;
163
164 /* Clear the entire DIE structure. This signals we have not yet
165 determined any of the information. */
166 memset (result, '\0', sizeof (Dwarf_Die));
167
168 /* We have the address. */
169 result->addr = addr;
170
171 /* Same CU as the parent. */
172 result->cu = cu;
173
174 return 0;
175 }
176 INTDEF(dwarf_child)
177