-
Notifications
You must be signed in to change notification settings - Fork 2
/
refl-obj.c
195 lines (161 loc) · 4.45 KB
/
refl-obj.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
/*
* 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 <string.h>
#include <dwarf.h>
#include <assert.h>
#include "reflP.h"
static void
init_object (struct refl_object *obj, struct refl_type *type, void *data)
{
obj->data = data;
obj->type = type;
}
struct refl_object *
__refl_object_begin (struct refl_type *type, void *data)
{
struct refl_object *result = __refl_malloc (sizeof (*result));
if (result == NULL)
return NULL;
init_object (result, type, data);
return result;
}
struct refl_object *
__refl_object_begin_inline (struct refl_type *type, size_t size)
{
struct refl_object *result = __refl_malloc (sizeof (*result) + size);
if (result == NULL)
return NULL;
init_object (result, type, (unsigned char *)(result + 1));
return result;
}
static void
checked_siblingof (Dwarf_Die **diep, Dwarf_Die *die_memp)
{
switch (dwarf_siblingof (*diep, die_memp))
{
case -1:
__refl_seterr (REFL_E_DWARF);
case 1:
*diep = NULL;
case 0:
break;
}
}
struct refl_object *
refl_access (struct refl *refl, struct refl_object *obj, char const *lookfor)
{
assert (refl != NULL);
assert (obj != NULL);
assert (lookfor != NULL);
struct refl_type *type = obj->type;
assert (type != NULL);
Dwarf_Die die_mem, *die = &die_mem;
if (dwarf_child (&type->die, &die_mem))
{
err:
__refl_seterr (REFL_E_DWARF);
return NULL;
}
for (; die != NULL; checked_siblingof (&die, &die_mem))
{
if (dwarf_tag (die) != DW_TAG_member)
continue;
Dwarf_Attribute attr_mem, *attr = dwarf_attr (die, DW_AT_name, &attr_mem);
if (attr == NULL)
goto err;
char const *name = dwarf_formstring (attr);
if (name == NULL)
{
err2:
__refl_seterr (REFL_E_DWARF);
return NULL;
}
if (strcmp (name, lookfor) != 0)
continue;
attr = dwarf_attr (die, DW_AT_data_member_location, &attr_mem);
if (attr == NULL)
goto err;
Dwarf_Word location;
if (dwarf_formudata (attr, &location) != 0)
goto err2;
attr = dwarf_attr (die, DW_AT_type, &attr_mem);
if (attr == NULL)
goto err;
Dwarf_Die tdie_mem, *tdie = dwarf_formref_die (attr, &tdie_mem);
if (tdie == NULL)
goto err2;
struct refl_type *field_type = __refl_type_begin (tdie);
if (field_type == NULL)
return NULL;
struct refl_object *result
= __refl_object_begin (field_type, (char *)obj->data + location);
if (result == NULL)
__refl_type_free (field_type);
return result;
}
return NULL;
}
struct refl_object *
refl_deref (struct refl *refl, struct refl_object *obj)
{
struct refl_type *type = refl_object_type (obj);
Dwarf_Die die = type->die;
if (__refl_die_strip_cvq (&die, &die) == NULL)
return NULL;
/* Cannot dereference non-pointer type. */
if (dwarf_tag (&die) != DW_TAG_pointer_type)
{
__refl_seterr_2 (REFL_E_REFL, REFL_ME_MISMATCH);
return NULL;
}
if (__refl_die_type (&die, &die) == NULL)
return NULL;
/* Cannot dereference function pointer. */
if (dwarf_tag (&die) == DW_TAG_subroutine_type)
{
__refl_seterr_2 (REFL_E_REFL, REFL_ME_MISMATCH);
return NULL;
}
struct refl_type *ntype = __refl_type_begin (&die);
if (ntype == NULL)
return NULL;
void *ndata = *(void **)refl_object_cdata (obj);
return __refl_object_begin (ntype, ndata);
}
void *
refl_object_cdata (struct refl_object *obj)
{
return obj->data;
}
struct refl_type *
refl_object_type (struct refl_object *obj)
{
return obj->type;
}
void
refl_assign_int (struct refl_object *obj, int i)
{
memcpy (refl_object_cdata (obj), &i, sizeof (i));
}
struct refl_object *
refl_new (struct refl *refl, struct refl_type *type)
{
size_t size = refl_type_sizeof (refl, type);
if (size == (size_t)-1)
return NULL;
return __refl_object_begin_inline (type, size);
}