-
Notifications
You must be signed in to change notification settings - Fork 5
/
maps.c
299 lines (252 loc) · 6.91 KB
/
maps.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
298
/*
* Copyright (c) 2007-2012 Message Systems, Inc. All rights reserved
* For licensing information, see:
* https://bitbucket.org/wez/gimli/src/tip/LICENSE
*/
#include "impl.h"
#include "gimli_dwarf.h"
static int sort_compare_mapping(const void *A, const void *B)
{
struct gimli_object_mapping *a = *(struct gimli_object_mapping**)A;
struct gimli_object_mapping *b = *(struct gimli_object_mapping**)B;
if (a->base < b->base) {
return -1;
}
if (a->base > b->base) {
return 1;
}
return a->len - b->len;
}
static int search_compare_mapping(const void *addrp, const void *L)
{
gimli_addr_t addr = *(gimli_addr_t*)addrp;
struct gimli_object_mapping *m = *(struct gimli_object_mapping**)L;
if (addr < m->base) {
return -1;
}
if (addr < m->base + m->len) {
return 0;
}
return 1;
}
void gimli_show_memory_map(gimli_proc_t proc)
{
int i;
/* print out the maps, coalesce adjacent maps for the same object */
printf("\nMEMORY MAP: (executable, shared objects and named mmaps)\n");
i = 0;
while (i < proc->nmaps) {
int j;
struct gimli_object_mapping *map = proc->mappings[i];
uint64_t upper = map->base + map->len;
for (j = i + 1; j < proc->nmaps; j++) {
struct gimli_object_mapping *om = proc->mappings[j];
if (om->objfile == map->objfile && om->base == upper) {
upper = om->base + om->len;
i = j;
continue;
}
break;
}
printf(PTRFMT " - " PTRFMT " %s\n",
map->base, upper, map->objfile->objname);
i++;
}
printf("\n\n");
}
struct gimli_object_mapping *gimli_mapping_for_addr(gimli_proc_t proc, gimli_addr_t addr)
{
struct gimli_object_mapping **mptr, *m;
if (proc->maps_changed) {
/* (re)sort the list of maps */
qsort(proc->mappings, proc->nmaps, sizeof(struct gimli_object_mapping*),
sort_compare_mapping);
proc->maps_changed = 0;
}
mptr = bsearch(&addr, proc->mappings, proc->nmaps, sizeof(struct gimli_object_mapping*),
search_compare_mapping);
if (mptr) {
m = *mptr;
if (addr < m->base + m->len) {
return m;
}
}
return NULL;
}
const char *gimli_data_sym_name(gimli_proc_t proc, gimli_addr_t addr, char *buf, int buflen)
{
struct gimli_object_mapping *m;
struct gimli_symbol *s;
m = gimli_mapping_for_addr(proc, (gimli_addr_t)addr);
if (m) {
s = find_symbol_for_addr(m->objfile, addr);
if (s) {
if (addr == s->addr) {
snprintf(buf, buflen-1, "%s`%s", m->objfile->objname, s->name);
} else {
snprintf(buf, buflen-1, "%s`%s+%lx",
m->objfile->objname, s->name, (uintmax_t)(addr - s->addr));
}
} else {
/* just identify the containing module; the caller will typically
* annotate with the address */
snprintf(buf, buflen-1, "%s", m->objfile->objname);
}
return buf;
}
return "";
}
const char *gimli_pc_sym_name(gimli_proc_t proc, gimli_addr_t addr, char *buf, int buflen)
{
struct gimli_object_mapping *m;
struct gimli_symbol *s;
m = gimli_mapping_for_addr(proc, (gimli_addr_t)addr);
if (m) {
s = find_symbol_for_addr(m->objfile, addr);
if (s) {
if (addr == s->addr) {
snprintf(buf, buflen-1, "%s`%s", m->objfile->objname, s->name);
} else {
snprintf(buf, buflen-1, "%s`%s+%lx",
m->objfile->objname, s->name, (uintmax_t)(addr - s->addr));
}
} else {
snprintf(buf, buflen-1, "%s`" PTRFMT, m->objfile->objname, addr);
}
return buf;
}
return "";
}
struct gimli_object_mapping *gimli_add_mapping(
gimli_proc_t proc,
const char *objname, gimli_addr_t base, unsigned long len,
unsigned long offset)
{
struct gimli_object_mapping *m = calloc(1, sizeof(*m));
m->proc = proc; // FIXME: refcnt
m->base = base;
m->len = len;
m->offset = offset;
if (debug) {
fprintf(stderr, "MAP: " PTRFMT " - " PTRFMT " [off=0x%" PRIx64 "] %s\n",
m->base, m->base + m->len, m->offset, objname);
}
m->objfile = gimli_find_object(proc, objname);
if (!m->objfile) {
m->objfile = gimli_add_object(proc, objname, base);
}
/* add to our collection */
proc->mappings = realloc(proc->mappings, (proc->nmaps + 1) * sizeof(m));
proc->mappings[proc->nmaps++] = m;
proc->maps_changed = 1;
return m;
}
gimli_mapped_object_t gimli_find_object(
gimli_proc_t proc,
const char *objname)
{
gimli_mapped_object_t f;
if (objname == NULL) {
return proc->first_file;
}
if (gimli_hash_find(proc->files, objname, (void**)&f)) {
return f;
}
return NULL;
}
void gimli_mapped_object_addref(gimli_mapped_object_t file)
{
file->refcnt++;
}
static void destroy_cu(struct gimli_dwarf_cu *cu)
{
if (cu->left) destroy_cu(cu->left);
if (cu->right) destroy_cu(cu->right);
free(cu);
}
void gimli_mapped_object_delete(gimli_mapped_object_t file)
{
if (--file->refcnt) return;
if (file->symhash) {
gimli_hash_destroy(file->symhash);
}
if (file->symtab) {
free(file->symtab);
}
if (file->sections) {
gimli_hash_destroy(file->sections);
}
if (file->elf) {
gimli_object_file_destroy(file->elf);
}
if (file->aux_elf) {
gimli_object_file_destroy(file->aux_elf);
}
if (file->lines) {
free(file->lines);
}
if (file->types) {
gimli_type_collection_delete(file->types);
}
if (file->die_to_type) {
gimli_hash_destroy(file->die_to_type);
}
if (file->abbr.map) {
gimli_hash_destroy(file->abbr.map);
}
if (file->debug_info.cus) {
destroy_cu(file->debug_info.cus);
}
free(file->arange);
gimli_dw_fde_destroy(file);
gimli_slab_destroy(&file->dieslab);
gimli_slab_destroy(&file->attrslab);
free(file->objname);
free(file);
}
void gimli_destroy_mapped_object_hash(void *item)
{
gimli_mapped_object_t file = item;
gimli_mapped_object_delete(file);
}
static void destroy_section(void *ptr)
{
struct gimli_section_data *data = ptr;
free(data->name);
free(data);
}
gimli_mapped_object_t gimli_add_object(
gimli_proc_t proc,
const char *objname, gimli_addr_t base)
{
gimli_mapped_object_t f = gimli_find_object(proc, objname);
struct gimli_symbol *sym;
char *name = NULL;
if (f) return f;
f = calloc(1, sizeof(*f));
f->refcnt = 1;
f->objname = strdup(objname);
f->sections = gimli_hash_new(destroy_section);
gimli_slab_init(&f->dieslab, sizeof(struct gimli_dwarf_die), "die");
gimli_slab_init(&f->attrslab, sizeof(struct gimli_dwarf_attr), "attr");
gimli_hash_insert(proc->files, f->objname, f);
if (proc->first_file == NULL) {
proc->first_file = f;
}
#ifndef __MACH__
f->elf = gimli_elf_open(f->objname);
if (f->elf) {
f->elf->gobject = f;
/* need to determine the base address offset for this object */
f->base_addr = (intptr_t)base - f->elf->vaddr;
if (debug) {
printf("ELF: %s %d base=" PTRFMT " vaddr=" PTRFMT " base_addr=" PTRFMT "\n",
f->objname, f->elf->e_type, base, f->elf->vaddr, f->base_addr);
}
gimli_process_elf(f);
}
#endif
return f;
}
/* vim:ts=2:sw=2:et:
*/