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
|
#include "rust_internal.h"
rust_crate_cache::lib::lib(rust_dom *dom, char const *name)
: handle(0),
dom(dom)
{
#if defined(__WIN32__)
handle = (uintptr_t)LoadLibrary(_T(name));
#else
handle = (uintptr_t)dlopen(name, RTLD_GLOBAL|RTLD_LAZY);
#endif
DLOG(dom, cache, "loaded library '%s' as 0x%" PRIxPTR,
name, handle);
}
rust_crate_cache::lib::~lib() {
DLOG(dom, cache, "~rust_crate_cache::lib(0x%" PRIxPTR ")",
handle);
if (handle) {
#if defined(__WIN32__)
FreeLibrary((HMODULE)handle);
#else
dlclose((void*)handle);
#endif
}
}
uintptr_t
rust_crate_cache::lib::get_handle() {
return handle;
}
rust_crate_cache::c_sym::c_sym(rust_dom *dom, lib *library, char const *name)
: val(0),
library(library),
dom(dom)
{
library->ref();
uintptr_t handle = library->get_handle();
if (handle) {
#if defined(__WIN32__)
val = (uintptr_t)GetProcAddress((HMODULE)handle, _T(name));
#else
val = (uintptr_t)dlsym((void*)handle, name);
#endif
DLOG(dom, cache, "resolved symbol '%s' to 0x%" PRIxPTR,
name, val);
} else {
DLOG_ERR(dom, cache, "unresolved symbol '%s', null lib handle\n"
"(did you omit a -L flag?)", name);
}
}
rust_crate_cache::c_sym::~c_sym() {
DLOG(dom, cache,
"~rust_crate_cache::c_sym(0x%" PRIxPTR ")", val);
library->deref();
}
uintptr_t
rust_crate_cache::c_sym::get_val() {
return val;
}
rust_crate_cache::rust_sym::rust_sym(rust_dom *dom,
rust_crate const *curr_crate,
c_sym *crate_sym,
char const **path)
: val(0),
crate_sym(crate_sym),
dom(dom)
{
crate_sym->ref();
typedef rust_crate_reader::die die;
rust_crate const *crate = (rust_crate*)crate_sym->get_val();
if (!crate) {
DLOG_ERR(dom, cache, "failed to resolve symbol, null crate symbol");
return;
}
rust_crate_reader rdr(dom, crate);
bool found_root = false;
bool found_leaf = false;
for (die d = rdr.dies.first_die();
!(found_root || d.is_null());
d = d.next_sibling()) {
die t1 = d;
die t2 = d;
for (char const **c = crate_rel(curr_crate, path);
(*c
&& !t1.is_null()
&& t1.find_child_by_name(crate_rel(curr_crate, *c), t2));
++c, t1=t2) {
DLOG(dom, dwarf, "matched die <0x%" PRIxPTR
">, child '%s' = die<0x%" PRIxPTR ">",
t1.off, crate_rel(curr_crate, *c), t2.off);
found_root = found_root || true;
if (!*(c+1) && t2.find_num_attr(DW_AT_low_pc, val)) {
DLOG(dom, dwarf, "found relative address: 0x%" PRIxPTR, val);
DLOG(dom, dwarf, "plus image-base 0x%" PRIxPTR,
crate->get_image_base());
val += crate->get_image_base();
found_leaf = true;
break;
}
}
if (found_root || found_leaf)
break;
}
if (found_leaf) {
DLOG(dom, cache, "resolved symbol to 0x%" PRIxPTR, val);
} else {
DLOG_ERR(dom, cache, "failed to resolve symbol");
}
}
rust_crate_cache::rust_sym::~rust_sym() {
DLOG(dom, cache,
"~rust_crate_cache::rust_sym(0x%" PRIxPTR ")", val);
crate_sym->deref();
}
uintptr_t
rust_crate_cache::rust_sym::get_val() {
return val;
}
static inline void
adjust_disp(uintptr_t &disp, const void *oldp, const void *newp)
{
if (disp) {
disp += (uintptr_t)oldp;
disp -= (uintptr_t)newp;
}
}
type_desc *
rust_crate_cache::get_type_desc(size_t size,
size_t align,
size_t n_descs,
type_desc const **descs)
{
I(dom, n_descs > 1);
type_desc *td = NULL;
size_t keysz = n_descs * sizeof(type_desc*);
HASH_FIND(hh, this->type_descs, descs, keysz, td);
if (td) {
DLOG(dom, cache, "rust_crate_cache::get_type_desc hit");
return td;
}
DLOG(dom, cache, "rust_crate_cache::get_type_desc miss");
td = (type_desc*) dom->malloc(sizeof(type_desc) + keysz);
if (!td)
return NULL;
// By convention, desc 0 is the root descriptor.
// but we ignore the size and alignment of it and use the
// passed-in, computed values.
memcpy(td, descs[0], sizeof(type_desc));
td->first_param = &td->descs[1];
td->size = size;
td->align = align;
for (size_t i = 0; i < n_descs; ++i) {
DLOG(dom, cache,
"rust_crate_cache::descs[%" PRIdPTR "] = 0x%" PRIxPTR,
i, descs[i]);
td->descs[i] = descs[i];
// FIXME (issue #136): Below is a miscalculation.
td->is_stateful |= descs[i]->is_stateful;
}
HASH_ADD(hh, this->type_descs, descs, keysz, td);
return td;
}
rust_crate_cache::rust_crate_cache(rust_dom *dom,
rust_crate const *crate)
: rust_syms((rust_sym**)
dom->calloc(sizeof(rust_sym*) * crate->n_rust_syms)),
c_syms((c_sym**) dom->calloc(sizeof(c_sym*) * crate->n_c_syms)),
libs((lib**) dom->calloc(sizeof(lib*) * crate->n_libs)),
type_descs(NULL),
crate(crate),
dom(dom),
idx(0)
{
I(dom, rust_syms);
I(dom, c_syms);
I(dom, libs);
}
void
rust_crate_cache::flush() {
DLOG(dom, cache, "rust_crate_cache::flush()");
for (size_t i = 0; i < crate->n_rust_syms; ++i) {
rust_sym *s = rust_syms[i];
if (s) {
DLOG(dom, cache,
"rust_crate_cache::flush() deref rust_sym %"
PRIdPTR " (rc=%" PRIdPTR ")", i, s->ref_count);
s->deref();
}
rust_syms[i] = NULL;
}
for (size_t i = 0; i < crate->n_c_syms; ++i) {
c_sym *s = c_syms[i];
if (s) {
DLOG(dom, cache,
"rust_crate_cache::flush() deref c_sym %"
PRIdPTR " (rc=%" PRIdPTR ")", i, s->ref_count);
s->deref();
}
c_syms[i] = NULL;
}
for (size_t i = 0; i < crate->n_libs; ++i) {
lib *l = libs[i];
if (l) {
DLOG(dom, cache, "rust_crate_cache::flush() deref lib %"
PRIdPTR " (rc=%" PRIdPTR ")", i, l->ref_count);
l->deref();
}
libs[i] = NULL;
}
while (type_descs) {
type_desc *d = type_descs;
HASH_DEL(type_descs, d);
DLOG(dom, mem, "rust_crate_cache::flush() tydesc %" PRIxPTR, d);
dom->free(d);
}
}
rust_crate_cache::~rust_crate_cache()
{
flush();
dom->free(rust_syms);
dom->free(c_syms);
dom->free(libs);
}
//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//
|