-
Notifications
You must be signed in to change notification settings - Fork 2
/
refl-method.c
297 lines (258 loc) · 6.46 KB
/
refl-method.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (C) 2011, 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 <stdlib.h>
#include <string.h>
#include <dwarf.h>
#include <ffi.h>
#include <assert.h>
#include <elfutils/libdw.h>
#include "reflP.h"
struct refl_method *
__refl_method_begin (Dwarf_Die *die)
{
struct refl_method *result = malloc (sizeof (*result));
if (result == NULL)
{
__refl_seterr (REFL_E_SYSTEM);
return NULL;
}
result->die = *die;
return result;
}
struct refl_method *
refl_method_at (struct refl *refl, void *ptr)
{
Dwarf_Addr addr = (Dwarf_Addr)ptr;
Dwarf_Addr bias;
Dwarf *dwarf = dwfl_addrdwarf (refl->dwfl, addr, &bias);
if (dwarf == NULL)
{
__refl_seterr (REFL_E_DWFL);
return NULL;
}
Dwarf_Die die_mem, *die = dwarf_addrdie (dwarf, addr, &die_mem);
if (die == NULL)
{
__refl_seterr (REFL_E_DWARF);
return NULL;
}
int tag = dwarf_tag (die);
if (tag != DW_TAG_compile_unit)
{
__refl_seterr (REFL_E_DWARF);
return NULL;
}
if (dwarf_child (die, &die_mem))
{
__refl_seterr (REFL_E_DWARF);
return NULL;
}
do
{
tag = dwarf_tag (die);
if (tag == DW_TAG_subprogram)
{
Dwarf_Addr low_pc, high_pc;
if (dwarf_lowpc (die, &low_pc) != 0
|| dwarf_highpc (die, &high_pc) != 0)
continue;
if (low_pc <= addr && addr < high_pc)
return __refl_method_begin (die);
}
}
while (dwarf_siblingof (die, &die_mem) == 0);
return NULL;
}
struct refl_method *
refl_method_cur (struct refl *refl)
{
return refl_method_at (refl, __builtin_return_address (0));
}
char const *
refl_method_name (struct refl_method *method)
{
if (method->name == NULL)
method->name = __refl_die_name (&method->die);
return method->name;
}
static int
dw_base_type_to_ffi (Dwarf_Die *die, ffi_type **ret)
{
assert (dwarf_tag (die) == DW_TAG_base_type);
Dwarf_Attribute size_att_mem, *size_att
= __refl_attr_integrate (die, DW_AT_byte_size, &size_att_mem);
if (size_att == NULL)
return -1;
Dwarf_Attribute enc_att_mem, *enc_att
= __refl_attr_integrate (die, DW_AT_encoding, &enc_att_mem);
if (enc_att == NULL)
return -1;
Dwarf_Word size;
Dwarf_Word encoding;
if (dwarf_formudata (size_att, &size) < 0
|| dwarf_formudata (enc_att, &encoding) < 0)
{
__refl_seterr (REFL_E_DWARF);
return -1;
}
bool sign;
switch (encoding)
{
case DW_ATE_signed:
sign = true;
break;
case DW_ATE_unsigned:
sign = false;
break;
default:
assert (!"unhandled encoding");
abort ();
}
switch (size)
{
case 1:
*ret = sign ? &ffi_type_sint8 : &ffi_type_uint8;
return 0;
case 2:
*ret = sign ? &ffi_type_sint16 : &ffi_type_uint16;
return 0;
case 4:
*ret = sign ? &ffi_type_sint32 : &ffi_type_uint32;
return 0;
case 8:
*ret = sign ? &ffi_type_sint64 : &ffi_type_uint64;
return 0;
default:
assert (!"unhandled size");
abort ();
};
}
static int
dw_type_to_ffi (Dwarf_Die *die, ffi_type **ret)
{
switch (dwarf_tag (die))
{
case DW_TAG_base_type:
return dw_base_type_to_ffi (die, ret);
default:
assert (!"unhandled type in refl/ffi translation");
abort ();
}
}
struct build_ffi_arglist
{
size_t given;
struct refl_object **args;
void **ptrs;
ffi_type **ffi_types;
size_t found;
};
static enum refl_cb_status
build_ffi_arglist (Dwarf_Die *die, void *d)
{
struct build_ffi_arglist *data = d;
if (dwarf_tag (die) == DW_TAG_formal_parameter)
{
size_t i = data->found++;
if (data->found > data->given)
{
__refl_seterr_2 (REFL_E_REFL, REFL_ME_MISMATCH);
return refl_cb_fail;
}
Dwarf_Die type_die_mem, *type_die = __refl_die_type (die, &type_die_mem);
if (type_die == NULL)
return refl_cb_fail;
ffi_type *ffit;
if (dw_type_to_ffi (type_die, &ffit) < 0)
return -1;
data->ffi_types[i] = ffit;
data->ptrs[i] = refl_object_cdata (data->args[i]);
}
return refl_cb_next;
}
int
refl_method_call (struct refl *refl, struct refl_method *method,
struct refl_object *args[], size_t nargs,
struct refl_object **ret)
{
Dwarf_Attribute lowpc_att_mem, *lowpc_att
= __refl_attr_integrate (&method->die, DW_AT_low_pc, &lowpc_att_mem);
if (lowpc_att == NULL)
return -1;
Dwarf_Addr low_pc;
if (dwarf_formaddr (lowpc_att, &low_pc) < 0)
{
__refl_seterr (REFL_E_DWARF);
return -1;
}
void *ptrs[nargs];
ffi_type *ffi_types[nargs];
struct build_ffi_arglist data = { nargs, args, ptrs, ffi_types };
if (__refl_die_children (&method->die, NULL, false,
build_ffi_arglist, &data) < 0)
return -1;
ffi_type *rettype;
struct refl_type *type;
if (dwarf_hasattr (&method->die, DW_AT_type))
{
Dwarf_Die type_die_mem, *type_die
= __refl_die_type (&method->die, &type_die_mem);
if (type_die == NULL
|| dw_type_to_ffi (type_die, &rettype) < 0)
return -1;
type = __refl_type_begin (type_die);
if (type == NULL)
return -1;
}
else
{
rettype = &ffi_type_void;
type = NULL;
}
ffi_cif cif;
ffi_status stat
= ffi_prep_cif (&cif, FFI_DEFAULT_ABI, nargs, rettype, ffi_types);
/* We should be able to avoid making any errors that FFI would
complain about. */
assert (stat == FFI_OK);
struct refl_object *tmp = NULL;
if (ret == NULL)
ret = &tmp;
void *ret_buf;
if (type != NULL)
{
size_t sz = refl_type_sizeof (refl, type);
if (sz < 8)
sz = 8;
*ret = __refl_object_begin_inline (type, sz);
if (*ret == NULL)
{
__refl_type_free (type);
return -1;
}
ret_buf = refl_object_cdata (*ret);
}
else
ret_buf = NULL;
ffi_call (&cif, (void (*)())low_pc, ret_buf, ptrs);
if (tmp != NULL)
{
__refl_type_free (type);
free (tmp);
}
return 0;
}