/
variable.c
271 lines (230 loc) · 7.14 KB
/
variable.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Written by Martin Milata in 2012.
* Published under WTFPL, see LICENSE.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <elfutils/libdw.h>
#include <dwarf.h>
#include "seecore-internal.h"
static void analyze_type(Dwarf_Die *die, struct type *ty)
{
int ret;
Dwarf_Attribute at;
/* find out the values of name, byte_size and type attributes
* even though not all of them make sense for all tags
*/
char *name = NULL;
if (dwarf_attr(die, DW_AT_name, &at) != NULL)
{
name = xstrdup(dwarf_formstring(&at));
}
struct type sub_type = { .name = NULL, .width = 0 };
if (dwarf_attr(die, DW_AT_type, &at) != NULL)
{
Dwarf_Die sub_die;
if (dwarf_formref_die(&at, &sub_die) != NULL)
analyze_type(&sub_die, &sub_type);
}
Dwarf_Word width = 0;
if (dwarf_attr(die, DW_AT_byte_size, &at) != NULL)
{
ret = dwarf_formudata(&at, &width);
fail_if(ret == -1, "dwarf_formudata");
}
switch (dwarf_tag(die))
{
case DW_TAG_base_type:
ty->name = name;
name = NULL;
ty->width = (unsigned)width;
/* TODO: what about encoding? */
break;
/* type modifiers */
case DW_TAG_const_type:
ty->name = xsprintf("const %s", sub_type.name ?: "void");
ty->width = sub_type.width;
break;
case DW_TAG_pointer_type:
ty->width = (unsigned)width;
ty->name = xsprintf("%s*", sub_type.name ?: "void");
break;
case DW_TAG_restrict_type:
ty->name = xsprintf("%s restrict", sub_type.name ?: "void");
ty->width = sub_type.width;
break;
case DW_TAG_volatile_type:
ty->name = xsprintf("volatile %s", sub_type.name ?: "void");
ty->width = sub_type.width;
break;
case DW_TAG_typedef:
ty->name = name;
name = NULL;
ty->width = sub_type.width;
break;
case DW_TAG_array_type:
ty->name = xsprintf("%s[]", sub_type.name);
ty->width = POINTER_SIZE;
break;
case DW_TAG_structure_type:
if (name)
ty->name = xsprintf("struct %s", name);
else
ty->name = xstrdup("struct");
ty->width = (unsigned)width;
break;
case DW_TAG_union_type:
if (name)
ty->name = xsprintf("union %s", name);
else
ty->name = xstrdup("union");
ty->width = (unsigned)width;
break;
case DW_TAG_class_type:
ty->name = xsprintf("class %s", name);
ty->width = (unsigned)width;
break;
case DW_TAG_enumeration_type:
ty->name = xsprintf("enum %s", name);
ty->width = (unsigned)width;
break;
case DW_TAG_subroutine_type:
ty->name = xstrdup("FUNCTION");
ty->width = 0; /* TODO */
break;
default:
warn("Unknown type 0x%x named %s with width %u", dwarf_tag(die), name, (unsigned)width);
break;
}
free(sub_type.name);
free(name);
}
void analyze_name_location(Dwarf_Die *die, Dwarf_Files *files,
char **name, struct location* loc)
{
int ret;
Dwarf_Attribute at;
Dwarf_Word w;
if (dwarf_attr_integrate(die, DW_AT_name, &at) != NULL)
{
*name = xstrdup(dwarf_formstring(&at));
}
if (dwarf_attr_integrate(die, DW_AT_decl_file, &at) != NULL)
{
ret = dwarf_formudata(&at, &w);
fail_if(ret == -1, "dwarf_formudata");
loc->file = xstrdup(dwarf_filesrc(files, (size_t)w, NULL, NULL));
}
if (dwarf_attr_integrate(die, DW_AT_decl_line, &at) != NULL)
{
ret = dwarf_formudata(&at, &w);
fail_if(ret == -1, "dwarf_formudata");
loc->line = (unsigned)w;
}
}
static struct variable* analyze_variable(Dwarf_Die *die, Dwarf_Files *files,
struct expr_context *ctx)
{
int ret;
Dwarf_Attribute at;
struct variable* var;
/* ignore declarations */
if (dwarf_attr_integrate(die, DW_AT_declaration, &at) != NULL)
{
bool flag;
ret = dwarf_formflag(&at, &flag);
fail_if(ret == -1, "dwarf_formflag");
if (flag)
return NULL;
}
var = xalloc(sizeof(struct variable));
analyze_name_location(die, files, &var->name, &var->loc);
if (dwarf_attr_integrate(die, DW_AT_type, &at) != NULL)
{
Dwarf_Die type_die;
if (dwarf_formref_die(&at, &type_die) == NULL)
fail("dwarf_formref_die");
analyze_type(&type_die, &(var->type));
}
if (dwarf_attr_integrate(die, DW_AT_const_value, &at) != NULL)
{
Dwarf_Word w;
Dwarf_Block bl;
unsigned int form = dwarf_whatform(&at);
debug("variable %s has constant value of form %x", var->name, form);
if (dwarf_formudata(&at, &w) == 0)
{
fail_if(sizeof(w) < var->type.width, "constant value too small");
var->value = xalloc(var->type.width);
memcpy(var->value, &w, var->type.width);
}
else if (dwarf_formblock(&at, &bl) == 0)
{
fail_if(bl.length < var->type.width, "constant value too small");
var->value = xalloc(var->type.width);
memcpy(var->value, bl.data, var->type.width);
}
else
{
warn("unable to get constant value of variable %x (form %x)", var->name, form);
}
}
else if (dwarf_attr_integrate(die, DW_AT_location, &at) != NULL)
{
size_t exprlen;
Dwarf_Op *expr;
ret = dwarf_getlocation_addr(&at, ctx->ip, &expr, &exprlen, 1);
if (ret != 1)
{
if (ret == -1)
/* it seems that elfutils have some kind of problem with
* DW_OP_GNU_entry_value but that operation is useless for us
* anyway */
warn("cannot get location for variable %s (ip: %lx), %s", var->name, ctx->ip, dwarf_errmsg(-1));
else if (ret == 0)
debug("no location available for variable %s (ip: %lx)", var->name, ctx->ip);
else
fail("unreachable reached");
return var;
}
var->value = evaluate_loc_expr(expr, exprlen, ctx, var->type.width);
}
return var;
}
void free_variables(struct variable *v)
{
struct variable *vx;
for (; v != NULL; v = vx)
{
vx = v->next;
free(v->loc.file);
free(v->type.name);
free(v->name);
free(v->value);
free(v);
}
}
struct variable* child_variables(Dwarf_Die *parent, Dwarf_Files *files,
struct expr_context *ctx, bool params)
{
int ret;
Dwarf_Die die;
struct variable *var, *head = NULL, *tail = NULL;
int desired_tag = params ? DW_TAG_formal_parameter : DW_TAG_variable;
ret = dwarf_child(parent, &die);
if (ret != 0)
return NULL;
do
{
if (dwarf_tag(&die) == desired_tag)
{
var = analyze_variable(&die, files, ctx);
if (!var)
continue;
list_append(head, tail, var);
}
} while (dwarf_siblingof(&die, &die) == 0);
return head;
}