-
Notifications
You must be signed in to change notification settings - Fork 2
/
refl-die.c
213 lines (183 loc) · 4.73 KB
/
refl-die.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (C) 2013 Petr Machata <[email protected]>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <string.h>
#include <dwarf.h>
#include "reflP.h"
int
__refl_die_tree (Dwarf_Die *root, Dwarf_Die *ret, bool recurse,
enum refl_cb_status (*callback) (Dwarf_Die *die,
void *data),
void *data)
{
switch (callback (root, data))
{
case refl_cb_stop:
if (ret != NULL)
*ret = *root;
return 0;
case refl_cb_fail:
return -1;
case refl_cb_next:
;
};
if (recurse)
return __refl_die_children (root, ret, recurse, callback, data);
return 1;
}
int
__refl_die_children (Dwarf_Die *root, Dwarf_Die *ret, bool recurse,
enum refl_cb_status (*callback) (Dwarf_Die *die,
void *data),
void *data)
{
if (!dwarf_haschildren (root))
return 1;
Dwarf_Die die_mem;
if (dwarf_child (root, &die_mem))
{
__refl_seterr (REFL_E_DWARF);
return -1;
}
while (1)
{
int result = __refl_die_tree (&die_mem, ret, recurse,
callback, data);
if (result <= 0)
return result;
switch (dwarf_siblingof (&die_mem, &die_mem))
{
case 0: continue;
case -1:
{
__refl_seterr (REFL_E_DWARF);
return -1;
}
}
break;
}
return 1;
}
int
__refl_each_die (Dwfl_Module *module, Dwarf_Die *root, Dwarf_Die *ret,
enum refl_cb_status (*callback) (Dwarf_Die *die,
void *data),
void *data)
{
bool iterate_cus = false;
Dwarf_Addr bias;
if (root == NULL)
{
root = dwfl_module_nextcu (module, NULL, &bias);
iterate_cus = true;
}
else
{
uint8_t address_size;
uint8_t offset_size;
Dwarf_Die cudie_mem, *cudie
= dwarf_diecu (root, &cudie_mem, &address_size, &offset_size);
if (cudie == NULL)
{
__refl_seterr (REFL_E_DWARF);
return -1;
}
/* If ROOT isn't a CU DIE, don't iterate other CU's. */
iterate_cus = dwarf_dieoffset (cudie) != dwarf_dieoffset (root);
}
while (root != NULL)
{
Dwarf_Die found;
Dwarf_Die tmp = *root;
int result = __refl_die_tree (&tmp, &found, true, callback, data);
if (result == 0 && ret != NULL)
*ret = found;
if (result <= 0)
return result;
if (!iterate_cus)
break;
root = dwfl_module_nextcu (module, root, &bias);
}
return 1;
}
enum refl_cb_status
__refl_die_attr_pred (Dwarf_Die *die, void *data)
{
struct __refl_die_attr_pred *pred_data = data;
Dwarf_Attribute attr_mem, *attr
= dwarf_attr (die, pred_data->at_name, &attr_mem);
if (attr == NULL)
return DWARF_CB_OK;
/* fprintf (stderr, "%#lx\n", dwarf_dieoffset (die)); */
return pred_data->callback (attr, pred_data->data);
}
enum refl_cb_status
__refl_attr_match_string (Dwarf_Attribute *attr, void *data)
{
char const *name = data;
assert (name != NULL);
char const *value = dwarf_formstring (attr);
if (value == NULL)
{
__refl_seterr (REFL_E_DWARF);
return refl_cb_fail;
}
return strcmp (name, value) != 0 ? refl_cb_next : refl_cb_stop;
}
Dwarf_Attribute *
__refl_attr_integrate (Dwarf_Die *die, int name, Dwarf_Attribute *mem)
{
Dwarf_Attribute *ret = dwarf_attr_integrate (die, name, mem);
if (ret == NULL)
__refl_seterr (REFL_E_DWARF);
return ret;
}
char const *
__refl_die_name (Dwarf_Die *die)
{
Dwarf_Attribute attr_mem, *attr
= __refl_attr_integrate (die, DW_AT_name, &attr_mem);
if (attr == NULL)
return NULL;
const char *str = dwarf_formstring (attr);
if (str == NULL)
__refl_seterr (REFL_E_DWARF);
return str;
}
Dwarf_Die *
__refl_die_type (Dwarf_Die *die, Dwarf_Die *ret_mem)
{
Dwarf_Attribute type_att_mem, *type_att
= __refl_attr_integrate (die, DW_AT_type, &type_att_mem);
Dwarf_Die *ret = NULL;
if (type_att == NULL
|| (ret = dwarf_formref_die (type_att, ret_mem)) == NULL)
{
__refl_seterr (REFL_E_DWARF);
return NULL;
}
return ret;
}
Dwarf_Die *
__refl_die_strip_cvq (Dwarf_Die *die, Dwarf_Die *ret_mem)
{
while (dwarf_tag (die) == DW_TAG_const_type
|| dwarf_tag (die) == DW_TAG_volatile_type)
if (__refl_die_type (die, ret_mem) == NULL)
return NULL;
return ret_mem;
}