]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/util/symbol.c
perf tools: Fix /etc config related installation
[mirror_ubuntu-zesty-kernel.git] / tools / perf / util / symbol.c
CommitLineData
5aab621b
ACM
1#include <dirent.h>
2#include <errno.h>
5aab621b
ACM
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/param.h>
9#include <fcntl.h>
10#include <unistd.h>
9486aa38 11#include <inttypes.h>
b36f19d5 12#include "build-id.h"
e334c726 13#include "util.h"
8a6c5b26 14#include "debug.h"
a2928c42 15#include "symbol.h"
5aab621b 16#include "strlist.h"
a2928c42
ACM
17
18#include <libelf.h>
19#include <gelf.h>
20#include <elf.h>
f1617b40 21#include <limits.h>
439d473b 22#include <sys/utsname.h>
2cdbc46d 23
3b01a413 24#ifndef KSYM_NAME_LEN
c752d040 25#define KSYM_NAME_LEN 256
3b01a413
ACM
26#endif
27
c12e15e7
ACM
28#ifndef NT_GNU_BUILD_ID
29#define NT_GNU_BUILD_ID 3
30#endif
31
4dff624a 32static void dso_cache__free(struct rb_root *root);
aeafcbaf 33static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
21916c38 34static int elf_read_build_id(Elf *elf, void *bf, size_t size);
b0da954a 35static void dsos__add(struct list_head *head, struct dso *dso);
3610583c 36static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
aeafcbaf 37static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 38 symbol_filter_t filter);
aeafcbaf 39static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
a1645ce1 40 symbol_filter_t filter);
cc612d81
ACM
41static int vmlinux_path__nr_entries;
42static char **vmlinux_path;
439d473b 43
75be6cf4 44struct symbol_conf symbol_conf = {
d599db3f 45 .exclude_other = true,
b32d133a
ACM
46 .use_modules = true,
47 .try_vmlinux_path = true,
3e6a2a7f 48 .annotate_src = true,
ec5761ea 49 .symfs = "",
b32d133a
ACM
50};
51
44f24cb3
JO
52static enum dso_binary_type binary_type_symtab[] = {
53 DSO_BINARY_TYPE__KALLSYMS,
54 DSO_BINARY_TYPE__GUEST_KALLSYMS,
55 DSO_BINARY_TYPE__JAVA_JIT,
56 DSO_BINARY_TYPE__DEBUGLINK,
57 DSO_BINARY_TYPE__BUILD_ID_CACHE,
58 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__GUEST_KMODULE,
63 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
64 DSO_BINARY_TYPE__NOT_FOUND,
65};
66
028df767 67#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
44f24cb3 68
949d160b
JO
69static enum dso_binary_type binary_type_data[] = {
70 DSO_BINARY_TYPE__BUILD_ID_CACHE,
71 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
72 DSO_BINARY_TYPE__NOT_FOUND,
73};
74
028df767 75#define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
949d160b 76
aeafcbaf 77int dso__name_len(const struct dso *dso)
8a6c5b26 78{
1e2dd2f7
DM
79 if (!dso)
80 return strlen("[unknown]");
8a6c5b26 81 if (verbose)
aeafcbaf 82 return dso->long_name_len;
8a6c5b26 83
aeafcbaf 84 return dso->short_name_len;
8a6c5b26
ACM
85}
86
aeafcbaf 87bool dso__loaded(const struct dso *dso, enum map_type type)
3610583c 88{
aeafcbaf 89 return dso->loaded & (1 << type);
3610583c
ACM
90}
91
aeafcbaf 92bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
79406cd7 93{
aeafcbaf 94 return dso->sorted_by_name & (1 << type);
79406cd7
ACM
95}
96
aeafcbaf 97static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
79406cd7 98{
aeafcbaf 99 dso->sorted_by_name |= (1 << type);
79406cd7
ACM
100}
101
36a3e646 102bool symbol_type__is_a(char symbol_type, enum map_type map_type)
6893d4ee 103{
31877908
AB
104 symbol_type = toupper(symbol_type);
105
6893d4ee
ACM
106 switch (map_type) {
107 case MAP__FUNCTION:
108 return symbol_type == 'T' || symbol_type == 'W';
f1dfa0b1 109 case MAP__VARIABLE:
31877908 110 return symbol_type == 'D';
6893d4ee
ACM
111 default:
112 return false;
113 }
114}
115
694bf407
AB
116static int prefix_underscores_count(const char *str)
117{
118 const char *tail = str;
119
120 while (*tail == '_')
121 tail++;
122
123 return tail - str;
124}
125
126#define SYMBOL_A 0
127#define SYMBOL_B 1
128
129static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
130{
131 s64 a;
132 s64 b;
133
134 /* Prefer a symbol with non zero length */
135 a = syma->end - syma->start;
136 b = symb->end - symb->start;
137 if ((b == 0) && (a > 0))
138 return SYMBOL_A;
139 else if ((a == 0) && (b > 0))
140 return SYMBOL_B;
141
142 /* Prefer a non weak symbol over a weak one */
143 a = syma->binding == STB_WEAK;
144 b = symb->binding == STB_WEAK;
145 if (b && !a)
146 return SYMBOL_A;
147 if (a && !b)
148 return SYMBOL_B;
149
150 /* Prefer a global symbol over a non global one */
151 a = syma->binding == STB_GLOBAL;
152 b = symb->binding == STB_GLOBAL;
153 if (a && !b)
154 return SYMBOL_A;
155 if (b && !a)
156 return SYMBOL_B;
157
158 /* Prefer a symbol with less underscores */
159 a = prefix_underscores_count(syma->name);
160 b = prefix_underscores_count(symb->name);
161 if (b > a)
162 return SYMBOL_A;
163 else if (a > b)
164 return SYMBOL_B;
165
166 /* If all else fails, choose the symbol with the longest name */
167 if (strlen(syma->name) >= strlen(symb->name))
168 return SYMBOL_A;
169 else
170 return SYMBOL_B;
171}
172
173static void symbols__fixup_duplicate(struct rb_root *symbols)
174{
175 struct rb_node *nd;
176 struct symbol *curr, *next;
177
178 nd = rb_first(symbols);
179
180 while (nd) {
181 curr = rb_entry(nd, struct symbol, rb_node);
182again:
183 nd = rb_next(&curr->rb_node);
184 next = rb_entry(nd, struct symbol, rb_node);
185
186 if (!nd)
187 break;
188
189 if (curr->start != next->start)
190 continue;
191
192 if (choose_best_symbol(curr, next) == SYMBOL_A) {
193 rb_erase(&next->rb_node, symbols);
194 goto again;
195 } else {
196 nd = rb_next(&curr->rb_node);
197 rb_erase(&curr->rb_node, symbols);
198 }
199 }
200}
201
aeafcbaf 202static void symbols__fixup_end(struct rb_root *symbols)
af427bf5 203{
aeafcbaf 204 struct rb_node *nd, *prevnd = rb_first(symbols);
2e538c4a 205 struct symbol *curr, *prev;
af427bf5
ACM
206
207 if (prevnd == NULL)
208 return;
209
2e538c4a
ACM
210 curr = rb_entry(prevnd, struct symbol, rb_node);
211
af427bf5 212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
213 prev = curr;
214 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5 215
3b01a413 216 if (prev->end == prev->start && prev->end != curr->start)
af427bf5 217 prev->end = curr->start - 1;
af427bf5 218 }
2e538c4a
ACM
219
220 /* Last entry */
221 if (curr->end == curr->start)
222 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
223}
224
aeafcbaf 225static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
af427bf5
ACM
226{
227 struct map *prev, *curr;
aeafcbaf 228 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
af427bf5
ACM
229
230 if (prevnd == NULL)
231 return;
232
233 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
234
235 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
236 prev = curr;
237 curr = rb_entry(nd, struct map, rb_node);
238 prev->end = curr->start - 1;
2e538c4a 239 }
90c83218
ACM
240
241 /*
242 * We still haven't the actual symbols, so guess the
243 * last map final address.
244 */
9d1faba5 245 curr->end = ~0ULL;
af427bf5
ACM
246}
247
aeafcbaf 248static void map_groups__fixup_end(struct map_groups *mg)
23ea4a3f
ACM
249{
250 int i;
251 for (i = 0; i < MAP__NR_TYPES; ++i)
aeafcbaf 252 __map_groups__fixup_end(mg, i);
23ea4a3f
ACM
253}
254
c408fedf
ACM
255static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
256 const char *name)
a2928c42 257{
0085c954 258 size_t namelen = strlen(name) + 1;
aeafcbaf
ACM
259 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
260 sizeof(*sym) + namelen));
261 if (sym == NULL)
0b73da3f
IM
262 return NULL;
263
75be6cf4 264 if (symbol_conf.priv_size)
aeafcbaf 265 sym = ((void *)sym) + symbol_conf.priv_size;
e4204992 266
aeafcbaf
ACM
267 sym->start = start;
268 sym->end = len ? start + len - 1 : start;
269 sym->binding = binding;
270 sym->namelen = namelen - 1;
e4204992 271
aeafcbaf
ACM
272 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
273 __func__, name, start, sym->end);
274 memcpy(sym->name, name, namelen);
a2928c42 275
aeafcbaf 276 return sym;
a2928c42
ACM
277}
278
aeafcbaf 279void symbol__delete(struct symbol *sym)
a2928c42 280{
aeafcbaf 281 free(((void *)sym) - symbol_conf.priv_size);
a2928c42
ACM
282}
283
aeafcbaf 284static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
a2928c42 285{
9486aa38 286 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
aeafcbaf
ACM
287 sym->start, sym->end,
288 sym->binding == STB_GLOBAL ? 'g' :
289 sym->binding == STB_LOCAL ? 'l' : 'w',
290 sym->name);
a2928c42
ACM
291}
292
a978f2ab
AN
293size_t symbol__fprintf_symname_offs(const struct symbol *sym,
294 const struct addr_location *al, FILE *fp)
547a92e0 295{
a978f2ab
AN
296 unsigned long offset;
297 size_t length;
298
299 if (sym && sym->name) {
300 length = fprintf(fp, "%s", sym->name);
301 if (al) {
302 offset = al->addr - sym->start;
303 length += fprintf(fp, "+0x%lx", offset);
304 }
305 return length;
306 } else
307 return fprintf(fp, "[unknown]");
308}
547a92e0 309
a978f2ab
AN
310size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
311{
312 return symbol__fprintf_symname_offs(sym, NULL, fp);
547a92e0
AN
313}
314
aeafcbaf 315void dso__set_long_name(struct dso *dso, char *name)
cfc10d3b 316{
ef6ae724
ACM
317 if (name == NULL)
318 return;
aeafcbaf
ACM
319 dso->long_name = name;
320 dso->long_name_len = strlen(name);
cfc10d3b
ACM
321}
322
aeafcbaf 323static void dso__set_short_name(struct dso *dso, const char *name)
b63be8d7
ACM
324{
325 if (name == NULL)
326 return;
aeafcbaf
ACM
327 dso->short_name = name;
328 dso->short_name_len = strlen(name);
b63be8d7
ACM
329}
330
aeafcbaf 331static void dso__set_basename(struct dso *dso)
cfc10d3b 332{
aeafcbaf 333 dso__set_short_name(dso, basename(dso->long_name));
cfc10d3b
ACM
334}
335
00a192b3 336struct dso *dso__new(const char *name)
a2928c42 337{
aeafcbaf 338 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
a2928c42 339
aeafcbaf 340 if (dso != NULL) {
6a4694a4 341 int i;
aeafcbaf
ACM
342 strcpy(dso->name, name);
343 dso__set_long_name(dso, dso->name);
344 dso__set_short_name(dso, dso->name);
6a4694a4 345 for (i = 0; i < MAP__NR_TYPES; ++i)
aeafcbaf 346 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
4dff624a 347 dso->cache = RB_ROOT;
44f24cb3 348 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
949d160b 349 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
aeafcbaf
ACM
350 dso->loaded = 0;
351 dso->sorted_by_name = 0;
352 dso->has_build_id = 0;
353 dso->kernel = DSO_TYPE_USER;
8db4841f 354 dso->needs_swap = DSO_SWAP__UNSET;
aeafcbaf 355 INIT_LIST_HEAD(&dso->node);
a2928c42
ACM
356 }
357
aeafcbaf 358 return dso;
a2928c42
ACM
359}
360
aeafcbaf 361static void symbols__delete(struct rb_root *symbols)
a2928c42
ACM
362{
363 struct symbol *pos;
aeafcbaf 364 struct rb_node *next = rb_first(symbols);
a2928c42
ACM
365
366 while (next) {
367 pos = rb_entry(next, struct symbol, rb_node);
368 next = rb_next(&pos->rb_node);
aeafcbaf 369 rb_erase(&pos->rb_node, symbols);
00a192b3 370 symbol__delete(pos);
a2928c42
ACM
371 }
372}
373
aeafcbaf 374void dso__delete(struct dso *dso)
a2928c42 375{
6a4694a4
ACM
376 int i;
377 for (i = 0; i < MAP__NR_TYPES; ++i)
aeafcbaf
ACM
378 symbols__delete(&dso->symbols[i]);
379 if (dso->sname_alloc)
380 free((char *)dso->short_name);
381 if (dso->lname_alloc)
382 free(dso->long_name);
4dff624a 383 dso_cache__free(&dso->cache);
aeafcbaf 384 free(dso);
a2928c42
ACM
385}
386
aeafcbaf 387void dso__set_build_id(struct dso *dso, void *build_id)
8d06367f 388{
aeafcbaf
ACM
389 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
390 dso->has_build_id = 1;
8d06367f
ACM
391}
392
aeafcbaf 393static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
a2928c42 394{
aeafcbaf 395 struct rb_node **p = &symbols->rb_node;
a2928c42 396 struct rb_node *parent = NULL;
9cffa8d5 397 const u64 ip = sym->start;
a2928c42
ACM
398 struct symbol *s;
399
400 while (*p != NULL) {
401 parent = *p;
402 s = rb_entry(parent, struct symbol, rb_node);
403 if (ip < s->start)
404 p = &(*p)->rb_left;
405 else
406 p = &(*p)->rb_right;
407 }
408 rb_link_node(&sym->rb_node, parent, p);
aeafcbaf 409 rb_insert_color(&sym->rb_node, symbols);
a2928c42
ACM
410}
411
aeafcbaf 412static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
a2928c42
ACM
413{
414 struct rb_node *n;
415
aeafcbaf 416 if (symbols == NULL)
a2928c42
ACM
417 return NULL;
418
aeafcbaf 419 n = symbols->rb_node;
a2928c42
ACM
420
421 while (n) {
422 struct symbol *s = rb_entry(n, struct symbol, rb_node);
423
424 if (ip < s->start)
425 n = n->rb_left;
426 else if (ip > s->end)
427 n = n->rb_right;
428 else
429 return s;
430 }
431
432 return NULL;
433}
434
79406cd7
ACM
435struct symbol_name_rb_node {
436 struct rb_node rb_node;
437 struct symbol sym;
438};
439
aeafcbaf 440static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
79406cd7 441{
aeafcbaf 442 struct rb_node **p = &symbols->rb_node;
79406cd7 443 struct rb_node *parent = NULL;
02a9d037
RV
444 struct symbol_name_rb_node *symn, *s;
445
446 symn = container_of(sym, struct symbol_name_rb_node, sym);
79406cd7
ACM
447
448 while (*p != NULL) {
449 parent = *p;
450 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
451 if (strcmp(sym->name, s->sym.name) < 0)
452 p = &(*p)->rb_left;
453 else
454 p = &(*p)->rb_right;
455 }
456 rb_link_node(&symn->rb_node, parent, p);
aeafcbaf 457 rb_insert_color(&symn->rb_node, symbols);
79406cd7
ACM
458}
459
aeafcbaf
ACM
460static void symbols__sort_by_name(struct rb_root *symbols,
461 struct rb_root *source)
79406cd7
ACM
462{
463 struct rb_node *nd;
464
465 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
466 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
aeafcbaf 467 symbols__insert_by_name(symbols, pos);
79406cd7
ACM
468 }
469}
470
aeafcbaf
ACM
471static struct symbol *symbols__find_by_name(struct rb_root *symbols,
472 const char *name)
79406cd7
ACM
473{
474 struct rb_node *n;
475
aeafcbaf 476 if (symbols == NULL)
79406cd7
ACM
477 return NULL;
478
aeafcbaf 479 n = symbols->rb_node;
79406cd7
ACM
480
481 while (n) {
482 struct symbol_name_rb_node *s;
483 int cmp;
484
485 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
486 cmp = strcmp(name, s->sym.name);
487
488 if (cmp < 0)
489 n = n->rb_left;
490 else if (cmp > 0)
491 n = n->rb_right;
492 else
493 return &s->sym;
494 }
495
496 return NULL;
497}
498
aeafcbaf 499struct symbol *dso__find_symbol(struct dso *dso,
79406cd7 500 enum map_type type, u64 addr)
fcf1203a 501{
aeafcbaf 502 return symbols__find(&dso->symbols[type], addr);
fcf1203a
ACM
503}
504
aeafcbaf 505struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
79406cd7
ACM
506 const char *name)
507{
aeafcbaf 508 return symbols__find_by_name(&dso->symbol_names[type], name);
79406cd7
ACM
509}
510
aeafcbaf 511void dso__sort_by_name(struct dso *dso, enum map_type type)
79406cd7 512{
aeafcbaf
ACM
513 dso__set_sorted_by_name(dso, type);
514 return symbols__sort_by_name(&dso->symbol_names[type],
515 &dso->symbols[type]);
79406cd7
ACM
516}
517
aeafcbaf 518int build_id__sprintf(const u8 *build_id, int len, char *bf)
a2928c42 519{
8d06367f 520 char *bid = bf;
aeafcbaf 521 const u8 *raw = build_id;
8d06367f 522 int i;
a2928c42 523
8d06367f
ACM
524 for (i = 0; i < len; ++i) {
525 sprintf(bid, "%02x", *raw);
526 ++raw;
527 bid += 2;
528 }
529
aeafcbaf 530 return raw - build_id;
8d06367f
ACM
531}
532
aeafcbaf 533size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
8d06367f
ACM
534{
535 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
8d06367f 536
aeafcbaf 537 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
9e03eb2d
ACM
538 return fprintf(fp, "%s", sbuild_id);
539}
540
aeafcbaf
ACM
541size_t dso__fprintf_symbols_by_name(struct dso *dso,
542 enum map_type type, FILE *fp)
90f18e63
SD
543{
544 size_t ret = 0;
545 struct rb_node *nd;
546 struct symbol_name_rb_node *pos;
547
aeafcbaf 548 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
90f18e63
SD
549 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
550 fprintf(fp, "%s\n", pos->sym.name);
551 }
552
553 return ret;
554}
555
aeafcbaf 556size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
9e03eb2d
ACM
557{
558 struct rb_node *nd;
aeafcbaf 559 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
9e03eb2d 560
aeafcbaf
ACM
561 if (dso->short_name != dso->long_name)
562 ret += fprintf(fp, "%s, ", dso->long_name);
3846df2e 563 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
aeafcbaf
ACM
564 dso->loaded ? "" : "NOT ");
565 ret += dso__fprintf_buildid(dso, fp);
6a4694a4 566 ret += fprintf(fp, ")\n");
aeafcbaf 567 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
95011c60
ACM
568 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
569 ret += symbol__fprintf(pos, fp);
a2928c42
ACM
570 }
571
572 return ret;
573}
574
9e201442
ACM
575int kallsyms__parse(const char *filename, void *arg,
576 int (*process_symbol)(void *arg, const char *name,
3b01a413 577 char type, u64 start, u64 end))
a2928c42 578{
a2928c42
ACM
579 char *line = NULL;
580 size_t n;
3b01a413 581 int err = -1;
9e201442 582 FILE *file = fopen(filename, "r");
a2928c42
ACM
583
584 if (file == NULL)
585 goto out_failure;
586
3b01a413
ACM
587 err = 0;
588
a2928c42 589 while (!feof(file)) {
9cffa8d5 590 u64 start;
a2928c42
ACM
591 int line_len, len;
592 char symbol_type;
2e538c4a 593 char *symbol_name;
a2928c42
ACM
594
595 line_len = getline(&line, &n, file);
a1645ce1 596 if (line_len < 0 || !line)
a2928c42
ACM
597 break;
598
a2928c42
ACM
599 line[--line_len] = '\0'; /* \n */
600
a0055ae2 601 len = hex2u64(line, &start);
a2928c42
ACM
602
603 len++;
604 if (len + 2 >= line_len)
605 continue;
606
31877908 607 symbol_type = line[len];
3b01a413
ACM
608 len += 2;
609 symbol_name = line + len;
610 len = line_len - len;
682b335a 611
3b01a413
ACM
612 if (len >= KSYM_NAME_LEN) {
613 err = -1;
682b335a 614 break;
3b01a413
ACM
615 }
616
3f5a4272
AB
617 /*
618 * module symbols are not sorted so we add all
619 * symbols with zero length and rely on
620 * symbols__fixup_end() to fix it up.
621 */
622 err = process_symbol(arg, symbol_name,
623 symbol_type, start, start);
624 if (err)
625 break;
2e538c4a
ACM
626 }
627
628 free(line);
629 fclose(file);
682b335a 630 return err;
2e538c4a 631
2e538c4a
ACM
632out_failure:
633 return -1;
634}
635
682b335a
ACM
636struct process_kallsyms_args {
637 struct map *map;
638 struct dso *dso;
639};
640
c408fedf
ACM
641static u8 kallsyms2elf_type(char type)
642{
643 if (type == 'W')
644 return STB_WEAK;
645
646 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
647}
648
682b335a 649static int map__process_kallsym_symbol(void *arg, const char *name,
3b01a413 650 char type, u64 start, u64 end)
682b335a
ACM
651{
652 struct symbol *sym;
653 struct process_kallsyms_args *a = arg;
654 struct rb_root *root = &a->dso->symbols[a->map->type];
655
656 if (!symbol_type__is_a(type, a->map->type))
657 return 0;
658
3b01a413
ACM
659 sym = symbol__new(start, end - start + 1,
660 kallsyms2elf_type(type), name);
682b335a
ACM
661 if (sym == NULL)
662 return -ENOMEM;
663 /*
664 * We will pass the symbols to the filter later, in
665 * map__split_kallsyms, when we have split the maps per module
666 */
667 symbols__insert(root, sym);
a1645ce1 668
682b335a
ACM
669 return 0;
670}
671
672/*
673 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
674 * so that we can in the next step set the symbol ->end address and then
675 * call kernel_maps__split_kallsyms.
676 */
aeafcbaf 677static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
9e201442 678 struct map *map)
682b335a 679{
aeafcbaf 680 struct process_kallsyms_args args = { .map = map, .dso = dso, };
9e201442 681 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
682b335a
ACM
682}
683
2e538c4a
ACM
684/*
685 * Split the symbols into maps, making sure there are no overlaps, i.e. the
686 * kernel range is broken in several maps, named [kernel].N, as we don't have
687 * the original ELF section names vmlinux have.
688 */
aeafcbaf 689static int dso__split_kallsyms(struct dso *dso, struct map *map,
9de89fe7 690 symbol_filter_t filter)
2e538c4a 691{
9de89fe7 692 struct map_groups *kmaps = map__kmap(map)->kmaps;
23346f21 693 struct machine *machine = kmaps->machine;
4e06255f 694 struct map *curr_map = map;
2e538c4a 695 struct symbol *pos;
8a953312 696 int count = 0, moved = 0;
aeafcbaf 697 struct rb_root *root = &dso->symbols[map->type];
4e06255f 698 struct rb_node *next = rb_first(root);
2e538c4a
ACM
699 int kernel_range = 0;
700
701 while (next) {
702 char *module;
703
704 pos = rb_entry(next, struct symbol, rb_node);
705 next = rb_next(&pos->rb_node);
706
707 module = strchr(pos->name, '\t');
708 if (module) {
75be6cf4 709 if (!symbol_conf.use_modules)
1de8e245
ACM
710 goto discard_symbol;
711
2e538c4a
ACM
712 *module++ = '\0';
713
b7cece76 714 if (strcmp(curr_map->dso->short_name, module)) {
a1645ce1 715 if (curr_map != map &&
aeafcbaf 716 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 717 machine__is_default_guest(machine)) {
a1645ce1
ZY
718 /*
719 * We assume all symbols of a module are
720 * continuous in * kallsyms, so curr_map
721 * points to a module and all its
722 * symbols are in its kmap. Mark it as
723 * loaded.
724 */
725 dso__set_loaded(curr_map->dso,
726 curr_map->type);
727 }
728
729 curr_map = map_groups__find_by_name(kmaps,
730 map->type, module);
4e06255f 731 if (curr_map == NULL) {
2f51903b 732 pr_debug("%s/proc/{kallsyms,modules} "
b7cece76 733 "inconsistency while looking "
a1645ce1 734 "for \"%s\" module!\n",
23346f21 735 machine->root_dir, module);
a1645ce1
ZY
736 curr_map = map;
737 goto discard_symbol;
af427bf5 738 }
b7cece76 739
a1645ce1 740 if (curr_map->dso->loaded &&
23346f21 741 !machine__is_default_guest(machine))
b7cece76 742 goto discard_symbol;
af427bf5 743 }
2e538c4a
ACM
744 /*
745 * So that we look just like we get from .ko files,
746 * i.e. not prelinked, relative to map->start.
747 */
4e06255f
ACM
748 pos->start = curr_map->map_ip(curr_map, pos->start);
749 pos->end = curr_map->map_ip(curr_map, pos->end);
750 } else if (curr_map != map) {
2e538c4a 751 char dso_name[PATH_MAX];
aeafcbaf 752 struct dso *ndso;
2e538c4a 753
8a953312
ACM
754 if (count == 0) {
755 curr_map = map;
756 goto filter_symbol;
757 }
758
aeafcbaf 759 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
a1645ce1
ZY
760 snprintf(dso_name, sizeof(dso_name),
761 "[guest.kernel].%d",
762 kernel_range++);
763 else
764 snprintf(dso_name, sizeof(dso_name),
765 "[kernel].%d",
766 kernel_range++);
2e538c4a 767
aeafcbaf
ACM
768 ndso = dso__new(dso_name);
769 if (ndso == NULL)
2e538c4a
ACM
770 return -1;
771
aeafcbaf 772 ndso->kernel = dso->kernel;
a1645ce1 773
aeafcbaf 774 curr_map = map__new2(pos->start, ndso, map->type);
37fe5fcb 775 if (curr_map == NULL) {
aeafcbaf 776 dso__delete(ndso);
2e538c4a
ACM
777 return -1;
778 }
a2928c42 779
4e06255f 780 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
9de89fe7 781 map_groups__insert(kmaps, curr_map);
2e538c4a
ACM
782 ++kernel_range;
783 }
8a953312 784filter_symbol:
4e06255f 785 if (filter && filter(curr_map, pos)) {
1de8e245 786discard_symbol: rb_erase(&pos->rb_node, root);
00a192b3 787 symbol__delete(pos);
2e538c4a 788 } else {
4e06255f
ACM
789 if (curr_map != map) {
790 rb_erase(&pos->rb_node, root);
791 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
8a953312
ACM
792 ++moved;
793 } else
794 ++count;
9974f496 795 }
a2928c42
ACM
796 }
797
a1645ce1 798 if (curr_map != map &&
aeafcbaf 799 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 800 machine__is_default_guest(kmaps->machine)) {
a1645ce1
ZY
801 dso__set_loaded(curr_map->dso, curr_map->type);
802 }
803
8a953312 804 return count + moved;
2e538c4a 805}
a2928c42 806
ec80fde7
ACM
807static bool symbol__restricted_filename(const char *filename,
808 const char *restricted_filename)
809{
810 bool restricted = false;
811
812 if (symbol_conf.kptr_restrict) {
813 char *r = realpath(filename, NULL);
814
815 if (r != NULL) {
816 restricted = strcmp(r, restricted_filename) == 0;
817 free(r);
818 return restricted;
819 }
820 }
821
822 return restricted;
823}
824
aeafcbaf 825int dso__load_kallsyms(struct dso *dso, const char *filename,
9de89fe7 826 struct map *map, symbol_filter_t filter)
2e538c4a 827{
ec80fde7
ACM
828 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
829 return -1;
830
aeafcbaf 831 if (dso__load_all_kallsyms(dso, filename, map) < 0)
2e538c4a
ACM
832 return -1;
833
694bf407 834 symbols__fixup_duplicate(&dso->symbols[map->type]);
3f5a4272
AB
835 symbols__fixup_end(&dso->symbols[map->type]);
836
aeafcbaf 837 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
44f24cb3 838 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
a1645ce1 839 else
44f24cb3 840 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
2e538c4a 841
aeafcbaf 842 return dso__split_kallsyms(dso, map, filter);
af427bf5
ACM
843}
844
aeafcbaf 845static int dso__load_perf_map(struct dso *dso, struct map *map,
6beba7ad 846 symbol_filter_t filter)
80d496be
PE
847{
848 char *line = NULL;
849 size_t n;
850 FILE *file;
851 int nr_syms = 0;
852
aeafcbaf 853 file = fopen(dso->long_name, "r");
80d496be
PE
854 if (file == NULL)
855 goto out_failure;
856
857 while (!feof(file)) {
9cffa8d5 858 u64 start, size;
80d496be
PE
859 struct symbol *sym;
860 int line_len, len;
861
862 line_len = getline(&line, &n, file);
863 if (line_len < 0)
864 break;
865
866 if (!line)
867 goto out_failure;
868
869 line[--line_len] = '\0'; /* \n */
870
871 len = hex2u64(line, &start);
872
873 len++;
874 if (len + 2 >= line_len)
875 continue;
876
877 len += hex2u64(line + len, &size);
878
879 len++;
880 if (len + 2 >= line_len)
881 continue;
882
c408fedf 883 sym = symbol__new(start, size, STB_GLOBAL, line + len);
80d496be
PE
884
885 if (sym == NULL)
886 goto out_delete_line;
887
439d473b 888 if (filter && filter(map, sym))
00a192b3 889 symbol__delete(sym);
80d496be 890 else {
aeafcbaf 891 symbols__insert(&dso->symbols[map->type], sym);
80d496be
PE
892 nr_syms++;
893 }
894 }
895
896 free(line);
897 fclose(file);
898
899 return nr_syms;
900
901out_delete_line:
902 free(line);
903out_failure:
904 return -1;
905}
906
a2928c42
ACM
907/**
908 * elf_symtab__for_each_symbol - iterate thru all the symbols
909 *
aeafcbaf 910 * @syms: struct elf_symtab instance to iterate
83a0944f 911 * @idx: uint32_t idx
a2928c42
ACM
912 * @sym: GElf_Sym iterator
913 */
83a0944f
IM
914#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
915 for (idx = 0, gelf_getsym(syms, idx, &sym);\
916 idx < nr_syms; \
917 idx++, gelf_getsym(syms, idx, &sym))
a2928c42
ACM
918
919static inline uint8_t elf_sym__type(const GElf_Sym *sym)
920{
921 return GELF_ST_TYPE(sym->st_info);
922}
923
924static inline int elf_sym__is_function(const GElf_Sym *sym)
925{
926 return elf_sym__type(sym) == STT_FUNC &&
927 sym->st_name != 0 &&
81833130 928 sym->st_shndx != SHN_UNDEF;
a2928c42
ACM
929}
930
f1dfa0b1
ACM
931static inline bool elf_sym__is_object(const GElf_Sym *sym)
932{
933 return elf_sym__type(sym) == STT_OBJECT &&
934 sym->st_name != 0 &&
935 sym->st_shndx != SHN_UNDEF;
936}
937
6cfcc53e
MG
938static inline int elf_sym__is_label(const GElf_Sym *sym)
939{
940 return elf_sym__type(sym) == STT_NOTYPE &&
941 sym->st_name != 0 &&
942 sym->st_shndx != SHN_UNDEF &&
943 sym->st_shndx != SHN_ABS;
944}
945
946static inline const char *elf_sec__name(const GElf_Shdr *shdr,
947 const Elf_Data *secstrs)
948{
949 return secstrs->d_buf + shdr->sh_name;
950}
951
952static inline int elf_sec__is_text(const GElf_Shdr *shdr,
953 const Elf_Data *secstrs)
954{
955 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
956}
957
f1dfa0b1
ACM
958static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
959 const Elf_Data *secstrs)
960{
961 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
962}
963
a2928c42
ACM
964static inline const char *elf_sym__name(const GElf_Sym *sym,
965 const Elf_Data *symstrs)
966{
967 return symstrs->d_buf + sym->st_name;
968}
969
970static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
971 GElf_Shdr *shp, const char *name,
83a0944f 972 size_t *idx)
a2928c42
ACM
973{
974 Elf_Scn *sec = NULL;
975 size_t cnt = 1;
976
977 while ((sec = elf_nextscn(elf, sec)) != NULL) {
978 char *str;
979
980 gelf_getshdr(sec, shp);
981 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
982 if (!strcmp(name, str)) {
83a0944f
IM
983 if (idx)
984 *idx = cnt;
a2928c42
ACM
985 break;
986 }
987 ++cnt;
988 }
989
990 return sec;
991}
992
8ce998d6
ACM
993#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
994 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
995 idx < nr_entries; \
996 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
997
998#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
999 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
1000 idx < nr_entries; \
1001 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
1002
a25e46c4
ACM
1003/*
1004 * We need to check if we have a .dynsym, so that we can handle the
1005 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
1006 * .dynsym or .symtab).
1007 * And always look at the original dso, not at debuginfo packages, that
1008 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
1009 */
33ff581e
JO
1010static int
1011dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
1012 symbol_filter_t filter)
8ce998d6
ACM
1013{
1014 uint32_t nr_rel_entries, idx;
1015 GElf_Sym sym;
9cffa8d5 1016 u64 plt_offset;
8ce998d6
ACM
1017 GElf_Shdr shdr_plt;
1018 struct symbol *f;
a25e46c4 1019 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 1020 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
1021 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
1022 size_t dynsym_idx;
1023 GElf_Ehdr ehdr;
8ce998d6 1024 char sympltname[1024];
a25e46c4
ACM
1025 Elf *elf;
1026 int nr = 0, symidx, fd, err = 0;
1027
ec5761ea 1028 fd = open(name, O_RDONLY);
a25e46c4
ACM
1029 if (fd < 0)
1030 goto out;
1031
84087126 1032 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a25e46c4
ACM
1033 if (elf == NULL)
1034 goto out_close;
1035
1036 if (gelf_getehdr(elf, &ehdr) == NULL)
1037 goto out_elf_end;
1038
1039 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
1040 ".dynsym", &dynsym_idx);
1041 if (scn_dynsym == NULL)
1042 goto out_elf_end;
8ce998d6 1043
a25e46c4 1044 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
1045 ".rela.plt", NULL);
1046 if (scn_plt_rel == NULL) {
a25e46c4 1047 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
1048 ".rel.plt", NULL);
1049 if (scn_plt_rel == NULL)
a25e46c4 1050 goto out_elf_end;
8ce998d6
ACM
1051 }
1052
a25e46c4
ACM
1053 err = -1;
1054
8ce998d6 1055 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 1056 goto out_elf_end;
8ce998d6 1057
a25e46c4
ACM
1058 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
1059 goto out_elf_end;
8ce998d6
ACM
1060
1061 /*
83a0944f 1062 * Fetch the relocation section to find the idxes to the GOT
8ce998d6
ACM
1063 * and the symbols in the .dynsym they refer to.
1064 */
1065 reldata = elf_getdata(scn_plt_rel, NULL);
1066 if (reldata == NULL)
a25e46c4 1067 goto out_elf_end;
8ce998d6
ACM
1068
1069 syms = elf_getdata(scn_dynsym, NULL);
1070 if (syms == NULL)
a25e46c4 1071 goto out_elf_end;
8ce998d6 1072
a25e46c4 1073 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 1074 if (scn_symstrs == NULL)
a25e46c4 1075 goto out_elf_end;
8ce998d6
ACM
1076
1077 symstrs = elf_getdata(scn_symstrs, NULL);
1078 if (symstrs == NULL)
a25e46c4 1079 goto out_elf_end;
8ce998d6
ACM
1080
1081 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
1082 plt_offset = shdr_plt.sh_offset;
1083
1084 if (shdr_rel_plt.sh_type == SHT_RELA) {
1085 GElf_Rela pos_mem, *pos;
1086
1087 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
1088 nr_rel_entries) {
1089 symidx = GELF_R_SYM(pos->r_info);
1090 plt_offset += shdr_plt.sh_entsize;
1091 gelf_getsym(syms, symidx, &sym);
1092 snprintf(sympltname, sizeof(sympltname),
1093 "%s@plt", elf_sym__name(&sym, symstrs));
1094
1095 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
c408fedf 1096 STB_GLOBAL, sympltname);
8ce998d6 1097 if (!f)
a25e46c4 1098 goto out_elf_end;
8ce998d6 1099
82164161
ACM
1100 if (filter && filter(map, f))
1101 symbol__delete(f);
1102 else {
aeafcbaf 1103 symbols__insert(&dso->symbols[map->type], f);
82164161
ACM
1104 ++nr;
1105 }
8ce998d6
ACM
1106 }
1107 } else if (shdr_rel_plt.sh_type == SHT_REL) {
1108 GElf_Rel pos_mem, *pos;
1109 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
1110 nr_rel_entries) {
1111 symidx = GELF_R_SYM(pos->r_info);
1112 plt_offset += shdr_plt.sh_entsize;
1113 gelf_getsym(syms, symidx, &sym);
1114 snprintf(sympltname, sizeof(sympltname),
1115 "%s@plt", elf_sym__name(&sym, symstrs));
1116
1117 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
c408fedf 1118 STB_GLOBAL, sympltname);
8ce998d6 1119 if (!f)
a25e46c4 1120 goto out_elf_end;
8ce998d6 1121
82164161
ACM
1122 if (filter && filter(map, f))
1123 symbol__delete(f);
1124 else {
aeafcbaf 1125 symbols__insert(&dso->symbols[map->type], f);
82164161
ACM
1126 ++nr;
1127 }
8ce998d6 1128 }
8ce998d6
ACM
1129 }
1130
a25e46c4
ACM
1131 err = 0;
1132out_elf_end:
1133 elf_end(elf);
1134out_close:
1135 close(fd);
1136
1137 if (err == 0)
1138 return nr;
1139out:
fe2197b8 1140 pr_debug("%s: problems reading %s PLT info.\n",
aeafcbaf 1141 __func__, dso->long_name);
a25e46c4 1142 return 0;
8ce998d6
ACM
1143}
1144
aeafcbaf 1145static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
d45868d3
ACM
1146{
1147 switch (type) {
1148 case MAP__FUNCTION:
aeafcbaf 1149 return elf_sym__is_function(sym);
f1dfa0b1 1150 case MAP__VARIABLE:
aeafcbaf 1151 return elf_sym__is_object(sym);
d45868d3
ACM
1152 default:
1153 return false;
1154 }
1155}
1156
aeafcbaf
ACM
1157static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
1158 enum map_type type)
d45868d3
ACM
1159{
1160 switch (type) {
1161 case MAP__FUNCTION:
aeafcbaf 1162 return elf_sec__is_text(shdr, secstrs);
f1dfa0b1 1163 case MAP__VARIABLE:
aeafcbaf 1164 return elf_sec__is_data(shdr, secstrs);
d45868d3
ACM
1165 default:
1166 return false;
1167 }
1168}
1169
70c3856b
EM
1170static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1171{
1172 Elf_Scn *sec = NULL;
1173 GElf_Shdr shdr;
1174 size_t cnt = 1;
1175
1176 while ((sec = elf_nextscn(elf, sec)) != NULL) {
1177 gelf_getshdr(sec, &shdr);
1178
1179 if ((addr >= shdr.sh_addr) &&
1180 (addr < (shdr.sh_addr + shdr.sh_size)))
1181 return cnt;
1182
1183 ++cnt;
1184 }
1185
1186 return -1;
1187}
1188
8db4841f
JO
1189static int dso__swap_init(struct dso *dso, unsigned char eidata)
1190{
1191 static unsigned int const endian = 1;
1192
1193 dso->needs_swap = DSO_SWAP__NO;
1194
1195 switch (eidata) {
1196 case ELFDATA2LSB:
1197 /* We are big endian, DSO is little endian. */
1198 if (*(unsigned char const *)&endian != 1)
1199 dso->needs_swap = DSO_SWAP__YES;
1200 break;
1201
1202 case ELFDATA2MSB:
1203 /* We are little endian, DSO is big endian. */
1204 if (*(unsigned char const *)&endian != 0)
1205 dso->needs_swap = DSO_SWAP__YES;
1206 break;
1207
1208 default:
1209 pr_err("unrecognized DSO data encoding %d\n", eidata);
1210 return -EINVAL;
1211 }
1212
1213 return 0;
1214}
1215
aeafcbaf 1216static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
6da80ce8
DM
1217 int fd, symbol_filter_t filter, int kmodule,
1218 int want_symtab)
a2928c42 1219{
aeafcbaf 1220 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
2e538c4a 1221 struct map *curr_map = map;
aeafcbaf 1222 struct dso *curr_dso = dso;
6cfcc53e 1223 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
1224 uint32_t nr_syms;
1225 int err = -1;
83a0944f 1226 uint32_t idx;
a2928c42 1227 GElf_Ehdr ehdr;
70c3856b
EM
1228 GElf_Shdr shdr, opdshdr;
1229 Elf_Data *syms, *opddata = NULL;
a2928c42 1230 GElf_Sym sym;
70c3856b 1231 Elf_Scn *sec, *sec_strndx, *opdsec;
a2928c42 1232 Elf *elf;
439d473b 1233 int nr = 0;
70c3856b 1234 size_t opdidx = 0;
a2928c42 1235
84087126 1236 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a2928c42 1237 if (elf == NULL) {
8b1389ef 1238 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
a2928c42
ACM
1239 goto out_close;
1240 }
1241
1242 if (gelf_getehdr(elf, &ehdr) == NULL) {
8b1389ef 1243 pr_debug("%s: cannot get elf header.\n", __func__);
a2928c42
ACM
1244 goto out_elf_end;
1245 }
1246
8db4841f
JO
1247 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
1248 goto out_elf_end;
1249
6da80ce8 1250 /* Always reject images with a mismatched build-id: */
aeafcbaf 1251 if (dso->has_build_id) {
21916c38
DM
1252 u8 build_id[BUILD_ID_SIZE];
1253
be96ea8f 1254 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
21916c38
DM
1255 goto out_elf_end;
1256
aeafcbaf 1257 if (!dso__build_id_equal(dso, build_id))
21916c38
DM
1258 goto out_elf_end;
1259 }
1260
a2928c42 1261 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 1262 if (sec == NULL) {
6da80ce8
DM
1263 if (want_symtab)
1264 goto out_elf_end;
1265
a25e46c4
ACM
1266 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1267 if (sec == NULL)
8ce998d6 1268 goto out_elf_end;
8ce998d6 1269 }
a2928c42 1270
70c3856b 1271 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
adb09184
AB
1272 if (opdshdr.sh_type != SHT_PROGBITS)
1273 opdsec = NULL;
70c3856b
EM
1274 if (opdsec)
1275 opddata = elf_rawdata(opdsec, NULL);
1276
a2928c42
ACM
1277 syms = elf_getdata(sec, NULL);
1278 if (syms == NULL)
1279 goto out_elf_end;
1280
1281 sec = elf_getscn(elf, shdr.sh_link);
1282 if (sec == NULL)
1283 goto out_elf_end;
1284
1285 symstrs = elf_getdata(sec, NULL);
1286 if (symstrs == NULL)
1287 goto out_elf_end;
1288
6cfcc53e
MG
1289 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1290 if (sec_strndx == NULL)
1291 goto out_elf_end;
1292
1293 secstrs = elf_getdata(sec_strndx, NULL);
9b30a26b 1294 if (secstrs == NULL)
6cfcc53e
MG
1295 goto out_elf_end;
1296
a2928c42
ACM
1297 nr_syms = shdr.sh_size / shdr.sh_entsize;
1298
e9fbc9dc 1299 memset(&sym, 0, sizeof(sym));
aeafcbaf
ACM
1300 if (dso->kernel == DSO_TYPE_USER) {
1301 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
1302 elf_section_by_name(elf, &ehdr, &shdr,
1303 ".gnu.prelink_undo",
1304 NULL) != NULL);
aeafcbaf
ACM
1305 } else {
1306 dso->adjust_symbols = 0;
1307 }
83a0944f 1308 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
a2928c42 1309 struct symbol *f;
56b03f3c 1310 const char *elf_name = elf_sym__name(&sym, symstrs);
2e538c4a 1311 char *demangled = NULL;
6cfcc53e
MG
1312 int is_label = elf_sym__is_label(&sym);
1313 const char *section_name;
a2928c42 1314
9de89fe7
ACM
1315 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1316 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1317 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
56b03f3c 1318
d45868d3 1319 if (!is_label && !elf_sym__is_a(&sym, map->type))
a2928c42
ACM
1320 continue;
1321
696b97a5
DM
1322 /* Reject ARM ELF "mapping symbols": these aren't unique and
1323 * don't identify functions, so will confuse the profile
1324 * output: */
1325 if (ehdr.e_machine == EM_ARM) {
1326 if (!strcmp(elf_name, "$a") ||
1327 !strcmp(elf_name, "$d") ||
1328 !strcmp(elf_name, "$t"))
1329 continue;
1330 }
1331
70c3856b
EM
1332 if (opdsec && sym.st_shndx == opdidx) {
1333 u32 offset = sym.st_value - opdshdr.sh_addr;
1334 u64 *opd = opddata->d_buf + offset;
8db4841f 1335 sym.st_value = DSO__SWAP(dso, u64, *opd);
70c3856b
EM
1336 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1337 }
1338
a2928c42
ACM
1339 sec = elf_getscn(elf, sym.st_shndx);
1340 if (!sec)
1341 goto out_elf_end;
1342
1343 gelf_getshdr(sec, &shdr);
6cfcc53e 1344
d45868d3 1345 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
6cfcc53e
MG
1346 continue;
1347
1348 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f 1349
b2f8fb23
DDAG
1350 /* On ARM, symbols for thumb functions have 1 added to
1351 * the symbol address as a flag - remove it */
1352 if ((ehdr.e_machine == EM_ARM) &&
1353 (map->type == MAP__FUNCTION) &&
1354 (sym.st_value & 1))
1355 --sym.st_value;
1356
aeafcbaf 1357 if (dso->kernel != DSO_TYPE_USER || kmodule) {
2e538c4a
ACM
1358 char dso_name[PATH_MAX];
1359
1360 if (strcmp(section_name,
b63be8d7 1361 (curr_dso->short_name +
aeafcbaf 1362 dso->short_name_len)) == 0)
2e538c4a
ACM
1363 goto new_symbol;
1364
1365 if (strcmp(section_name, ".text") == 0) {
1366 curr_map = map;
aeafcbaf 1367 curr_dso = dso;
2e538c4a
ACM
1368 goto new_symbol;
1369 }
1370
1371 snprintf(dso_name, sizeof(dso_name),
aeafcbaf 1372 "%s%s", dso->short_name, section_name);
2e538c4a 1373
9de89fe7 1374 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
2e538c4a
ACM
1375 if (curr_map == NULL) {
1376 u64 start = sym.st_value;
1377
1378 if (kmodule)
1379 start += map->start + shdr.sh_offset;
1380
00a192b3 1381 curr_dso = dso__new(dso_name);
2e538c4a
ACM
1382 if (curr_dso == NULL)
1383 goto out_elf_end;
aeafcbaf
ACM
1384 curr_dso->kernel = dso->kernel;
1385 curr_dso->long_name = dso->long_name;
1386 curr_dso->long_name_len = dso->long_name_len;
3610583c 1387 curr_map = map__new2(start, curr_dso,
6275ce2d 1388 map->type);
2e538c4a
ACM
1389 if (curr_map == NULL) {
1390 dso__delete(curr_dso);
1391 goto out_elf_end;
1392 }
ed52ce2e
ACM
1393 curr_map->map_ip = identity__map_ip;
1394 curr_map->unmap_ip = identity__map_ip;
aeafcbaf 1395 curr_dso->symtab_type = dso->symtab_type;
9de89fe7 1396 map_groups__insert(kmap->kmaps, curr_map);
aeafcbaf 1397 dsos__add(&dso->node, curr_dso);
6275ce2d 1398 dso__set_loaded(curr_dso, map->type);
2e538c4a
ACM
1399 } else
1400 curr_dso = curr_map->dso;
1401
1402 goto new_symbol;
af427bf5
ACM
1403 }
1404
2e538c4a 1405 if (curr_dso->adjust_symbols) {
9486aa38
ACM
1406 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1407 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
29a9f66d
ACM
1408 (u64)sym.st_value, (u64)shdr.sh_addr,
1409 (u64)shdr.sh_offset);
f5812a7a 1410 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 1411 }
28ac909b
ACM
1412 /*
1413 * We need to figure out if the object was created from C++ sources
1414 * DWARF DW_compile_unit has this, but we don't always have access
1415 * to it...
1416 */
83a0944f 1417 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 1418 if (demangled != NULL)
83a0944f 1419 elf_name = demangled;
2e538c4a 1420new_symbol:
c408fedf
ACM
1421 f = symbol__new(sym.st_value, sym.st_size,
1422 GELF_ST_BIND(sym.st_info), elf_name);
28ac909b 1423 free(demangled);
a2928c42
ACM
1424 if (!f)
1425 goto out_elf_end;
1426
2e538c4a 1427 if (filter && filter(curr_map, f))
00a192b3 1428 symbol__delete(f);
69ee69f6 1429 else {
6a4694a4 1430 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
1431 nr++;
1432 }
a2928c42
ACM
1433 }
1434
2e538c4a
ACM
1435 /*
1436 * For misannotated, zeroed, ASM function sizes.
1437 */
6275ce2d 1438 if (nr > 0) {
694bf407 1439 symbols__fixup_duplicate(&dso->symbols[map->type]);
aeafcbaf 1440 symbols__fixup_end(&dso->symbols[map->type]);
6275ce2d
ACM
1441 if (kmap) {
1442 /*
1443 * We need to fixup this here too because we create new
1444 * maps here, for things like vsyscall sections.
1445 */
1446 __map_groups__fixup_end(kmap->kmaps, map->type);
1447 }
1448 }
a2928c42
ACM
1449 err = nr;
1450out_elf_end:
1451 elf_end(elf);
1452out_close:
1453 return err;
1454}
1455
aeafcbaf 1456static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
78075caa 1457{
aeafcbaf 1458 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
78075caa
ACM
1459}
1460
a1645ce1 1461bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
57f395a7 1462{
e30a3d12 1463 bool have_build_id = false;
57f395a7
FW
1464 struct dso *pos;
1465
6122e4e4
ACM
1466 list_for_each_entry(pos, head, node) {
1467 if (with_hits && !pos->hit)
1468 continue;
f6e1467d
ACM
1469 if (pos->has_build_id) {
1470 have_build_id = true;
1471 continue;
1472 }
e30a3d12
ACM
1473 if (filename__read_build_id(pos->long_name, pos->build_id,
1474 sizeof(pos->build_id)) > 0) {
1475 have_build_id = true;
1476 pos->has_build_id = true;
1477 }
6122e4e4 1478 }
57f395a7 1479
e30a3d12 1480 return have_build_id;
57f395a7
FW
1481}
1482
fd7a346e
ACM
1483/*
1484 * Align offset to 4 bytes as needed for note name and descriptor data.
1485 */
1486#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1487
21916c38 1488static int elf_read_build_id(Elf *elf, void *bf, size_t size)
4d1e00a8 1489{
21916c38 1490 int err = -1;
4d1e00a8
ACM
1491 GElf_Ehdr ehdr;
1492 GElf_Shdr shdr;
fd7a346e 1493 Elf_Data *data;
4d1e00a8 1494 Elf_Scn *sec;
e57cfcda 1495 Elf_Kind ek;
fd7a346e 1496 void *ptr;
4d1e00a8 1497
2643ce11
ACM
1498 if (size < BUILD_ID_SIZE)
1499 goto out;
1500
e57cfcda
PE
1501 ek = elf_kind(elf);
1502 if (ek != ELF_K_ELF)
21916c38 1503 goto out;
e57cfcda 1504
4d1e00a8 1505 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 1506 pr_err("%s: cannot get elf header.\n", __func__);
21916c38 1507 goto out;
4d1e00a8
ACM
1508 }
1509
1388d715
JO
1510 /*
1511 * Check following sections for notes:
1512 * '.note.gnu.build-id'
1513 * '.notes'
1514 * '.note' (VDSO specific)
1515 */
1516 do {
1517 sec = elf_section_by_name(elf, &ehdr, &shdr,
1518 ".note.gnu.build-id", NULL);
1519 if (sec)
1520 break;
1521
fd7a346e
ACM
1522 sec = elf_section_by_name(elf, &ehdr, &shdr,
1523 ".notes", NULL);
1388d715
JO
1524 if (sec)
1525 break;
1526
1527 sec = elf_section_by_name(elf, &ehdr, &shdr,
1528 ".note", NULL);
1529 if (sec)
1530 break;
1531
1532 return err;
1533
1534 } while (0);
4d1e00a8 1535
fd7a346e
ACM
1536 data = elf_getdata(sec, NULL);
1537 if (data == NULL)
21916c38 1538 goto out;
fd7a346e
ACM
1539
1540 ptr = data->d_buf;
1541 while (ptr < (data->d_buf + data->d_size)) {
1542 GElf_Nhdr *nhdr = ptr;
be96ea8f
SE
1543 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
1544 descsz = NOTE_ALIGN(nhdr->n_descsz);
fd7a346e
ACM
1545 const char *name;
1546
1547 ptr += sizeof(*nhdr);
1548 name = ptr;
1549 ptr += namesz;
1550 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1551 nhdr->n_namesz == sizeof("GNU")) {
1552 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
be96ea8f
SE
1553 size_t sz = min(size, descsz);
1554 memcpy(bf, ptr, sz);
1555 memset(bf + sz, 0, size - sz);
1556 err = descsz;
fd7a346e
ACM
1557 break;
1558 }
1559 }
1560 ptr += descsz;
1561 }
21916c38
DM
1562
1563out:
1564 return err;
1565}
1566
1567int filename__read_build_id(const char *filename, void *bf, size_t size)
1568{
1569 int fd, err = -1;
1570 Elf *elf;
1571
1572 if (size < BUILD_ID_SIZE)
1573 goto out;
1574
1575 fd = open(filename, O_RDONLY);
1576 if (fd < 0)
1577 goto out;
1578
1579 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1580 if (elf == NULL) {
1581 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1582 goto out_close;
1583 }
1584
1585 err = elf_read_build_id(elf, bf, size);
1586
2643ce11
ACM
1587 elf_end(elf);
1588out_close:
1589 close(fd);
1590out:
1591 return err;
1592}
1593
f1617b40
ACM
1594int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1595{
1596 int fd, err = -1;
1597
1598 if (size < BUILD_ID_SIZE)
1599 goto out;
1600
1601 fd = open(filename, O_RDONLY);
1602 if (fd < 0)
1603 goto out;
1604
1605 while (1) {
1606 char bf[BUFSIZ];
1607 GElf_Nhdr nhdr;
be96ea8f 1608 size_t namesz, descsz;
f1617b40
ACM
1609
1610 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1611 break;
1612
fd7a346e
ACM
1613 namesz = NOTE_ALIGN(nhdr.n_namesz);
1614 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1615 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1616 nhdr.n_namesz == sizeof("GNU")) {
be96ea8f 1617 if (read(fd, bf, namesz) != (ssize_t)namesz)
f1617b40
ACM
1618 break;
1619 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
be96ea8f
SE
1620 size_t sz = min(descsz, size);
1621 if (read(fd, build_id, sz) == (ssize_t)sz) {
1622 memset(build_id + sz, 0, size - sz);
f1617b40
ACM
1623 err = 0;
1624 break;
1625 }
be96ea8f 1626 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
f1617b40
ACM
1627 break;
1628 } else {
1629 int n = namesz + descsz;
1630 if (read(fd, bf, n) != n)
1631 break;
1632 }
1633 }
1634 close(fd);
1635out:
1636 return err;
1637}
1638
209bd9e3
PLG
1639static int filename__read_debuglink(const char *filename,
1640 char *debuglink, size_t size)
1641{
1642 int fd, err = -1;
1643 Elf *elf;
1644 GElf_Ehdr ehdr;
1645 GElf_Shdr shdr;
1646 Elf_Data *data;
1647 Elf_Scn *sec;
1648 Elf_Kind ek;
1649
1650 fd = open(filename, O_RDONLY);
1651 if (fd < 0)
1652 goto out;
1653
1654 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1655 if (elf == NULL) {
1656 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1657 goto out_close;
1658 }
1659
1660 ek = elf_kind(elf);
1661 if (ek != ELF_K_ELF)
1662 goto out_close;
1663
1664 if (gelf_getehdr(elf, &ehdr) == NULL) {
1665 pr_err("%s: cannot get elf header.\n", __func__);
1666 goto out_close;
1667 }
1668
1669 sec = elf_section_by_name(elf, &ehdr, &shdr,
1670 ".gnu_debuglink", NULL);
1671 if (sec == NULL)
1672 goto out_close;
1673
1674 data = elf_getdata(sec, NULL);
1675 if (data == NULL)
1676 goto out_close;
1677
1678 /* the start of this section is a zero-terminated string */
1679 strncpy(debuglink, data->d_buf, size);
1680
1681 elf_end(elf);
1682
1683out_close:
1684 close(fd);
1685out:
1686 return err;
1687}
1688
aeafcbaf 1689char dso__symtab_origin(const struct dso *dso)
94cb9e38
ACM
1690{
1691 static const char origin[] = {
44f24cb3
JO
1692 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
1693 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
1694 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
1695 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
1696 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
1697 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
1698 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
1699 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
1700 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
1701 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
1702 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
94cb9e38
ACM
1703 };
1704
44f24cb3 1705 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
94cb9e38 1706 return '!';
aeafcbaf 1707 return origin[dso->symtab_type];
94cb9e38
ACM
1708}
1709
44f24cb3
JO
1710int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
1711 char *root_dir, char *file, size_t size)
1712{
1713 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1714 int ret = 0;
1715
1716 switch (type) {
1717 case DSO_BINARY_TYPE__DEBUGLINK: {
1718 char *debuglink;
1719
1720 strncpy(file, dso->long_name, size);
1721 debuglink = file + dso->long_name_len;
1722 while (debuglink != file && *debuglink != '/')
1723 debuglink--;
1724 if (*debuglink == '/')
1725 debuglink++;
1726 filename__read_debuglink(dso->long_name, debuglink,
1727 size - (debuglink - file));
1728 }
1729 break;
1730 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1731 /* skip the locally configured cache if a symfs is given */
1732 if (symbol_conf.symfs[0] ||
1733 (dso__build_id_filename(dso, file, size) == NULL))
1734 ret = -1;
1735 break;
1736
1737 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1738 snprintf(file, size, "%s/usr/lib/debug%s.debug",
1739 symbol_conf.symfs, dso->long_name);
1740 break;
1741
1742 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1743 snprintf(file, size, "%s/usr/lib/debug%s",
1744 symbol_conf.symfs, dso->long_name);
1745 break;
1746
1747 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1748 if (!dso->has_build_id) {
1749 ret = -1;
1750 break;
1751 }
1752
1753 build_id__sprintf(dso->build_id,
1754 sizeof(dso->build_id),
1755 build_id_hex);
1756 snprintf(file, size,
1757 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1758 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
1759 break;
1760
1761 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1762 snprintf(file, size, "%s%s",
1763 symbol_conf.symfs, dso->long_name);
1764 break;
1765
1766 case DSO_BINARY_TYPE__GUEST_KMODULE:
1767 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1768 root_dir, dso->long_name);
1769 break;
1770
1771 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1772 snprintf(file, size, "%s%s", symbol_conf.symfs,
1773 dso->long_name);
1774 break;
1775
1776 default:
1777 case DSO_BINARY_TYPE__KALLSYMS:
1778 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1779 case DSO_BINARY_TYPE__JAVA_JIT:
1780 case DSO_BINARY_TYPE__NOT_FOUND:
1781 ret = -1;
1782 break;
1783 }
1784
1785 return ret;
1786}
1787
aeafcbaf 1788int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
a2928c42 1789{
c338aee8 1790 char *name;
a2928c42
ACM
1791 int ret = -1;
1792 int fd;
44f24cb3 1793 u_int i;
23346f21 1794 struct machine *machine;
44f24cb3 1795 char *root_dir = (char *) "";
6da80ce8 1796 int want_symtab;
a2928c42 1797
aeafcbaf 1798 dso__set_loaded(dso, map->type);
66bd8424 1799
aeafcbaf
ACM
1800 if (dso->kernel == DSO_TYPE_KERNEL)
1801 return dso__load_kernel_sym(dso, map, filter);
1802 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1803 return dso__load_guest_kernel_sym(dso, map, filter);
a1645ce1 1804
23346f21
ACM
1805 if (map->groups && map->groups->machine)
1806 machine = map->groups->machine;
a1645ce1 1807 else
23346f21 1808 machine = NULL;
c338aee8 1809
44f24cb3 1810 name = malloc(PATH_MAX);
a2928c42
ACM
1811 if (!name)
1812 return -1;
1813
aeafcbaf 1814 dso->adjust_symbols = 0;
f5812a7a 1815
aeafcbaf 1816 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
981c1252
PE
1817 struct stat st;
1818
e9b52ef2 1819 if (lstat(dso->name, &st) < 0)
981c1252
PE
1820 return -1;
1821
1822 if (st.st_uid && (st.st_uid != geteuid())) {
1823 pr_warning("File %s not owned by current user or root, "
1824 "ignoring it.\n", dso->name);
1825 return -1;
1826 }
1827
aeafcbaf 1828 ret = dso__load_perf_map(dso, map, filter);
44f24cb3
JO
1829 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1830 DSO_BINARY_TYPE__NOT_FOUND;
94cb9e38
ACM
1831 return ret;
1832 }
1833
44f24cb3
JO
1834 if (machine)
1835 root_dir = machine->root_dir;
1836
6da80ce8
DM
1837 /* Iterate over candidate debug images.
1838 * On the first pass, only load images if they have a full symtab.
1839 * Failing that, do a second pass where we accept .dynsym also
1840 */
60e4b10c
ACM
1841 want_symtab = 1;
1842restart:
44f24cb3 1843 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
6da80ce8 1844
44f24cb3 1845 dso->symtab_type = binary_type_symtab[i];
ec5761ea 1846
44f24cb3
JO
1847 if (dso__binary_type_file(dso, dso->symtab_type,
1848 root_dir, name, PATH_MAX))
1849 continue;
6da80ce8
DM
1850
1851 /* Name is now the name of the next image to try */
a2928c42 1852 fd = open(name, O_RDONLY);
6da80ce8
DM
1853 if (fd < 0)
1854 continue;
a2928c42 1855
aeafcbaf 1856 ret = dso__load_sym(dso, map, name, fd, filter, 0,
6da80ce8
DM
1857 want_symtab);
1858 close(fd);
a2928c42 1859
6da80ce8
DM
1860 /*
1861 * Some people seem to have debuginfo files _WITHOUT_ debug
1862 * info!?!?
1863 */
1864 if (!ret)
1865 continue;
a2928c42 1866
6da80ce8 1867 if (ret > 0) {
33ff581e
JO
1868 int nr_plt;
1869
1870 nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
6da80ce8
DM
1871 if (nr_plt > 0)
1872 ret += nr_plt;
1873 break;
1874 }
a25e46c4 1875 }
6da80ce8 1876
60e4b10c
ACM
1877 /*
1878 * If we wanted a full symtab but no image had one,
1879 * relax our requirements and repeat the search.
1880 */
1881 if (ret <= 0 && want_symtab) {
1882 want_symtab = 0;
1883 goto restart;
1884 }
1885
a2928c42 1886 free(name);
aeafcbaf 1887 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1340e6bb 1888 return 0;
a2928c42
ACM
1889 return ret;
1890}
1891
aeafcbaf 1892struct map *map_groups__find_by_name(struct map_groups *mg,
79406cd7 1893 enum map_type type, const char *name)
439d473b
ACM
1894{
1895 struct rb_node *nd;
1896
aeafcbaf 1897 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1898 struct map *map = rb_entry(nd, struct map, rb_node);
1899
b7cece76 1900 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1901 return map;
1902 }
1903
1904 return NULL;
1905}
1906
aeafcbaf
ACM
1907static int dso__kernel_module_get_build_id(struct dso *dso,
1908 const char *root_dir)
b7cece76
ACM
1909{
1910 char filename[PATH_MAX];
1911 /*
1912 * kernel module short names are of the form "[module]" and
1913 * we need just "module" here.
1914 */
aeafcbaf 1915 const char *name = dso->short_name + 1;
b7cece76
ACM
1916
1917 snprintf(filename, sizeof(filename),
a1645ce1
ZY
1918 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1919 root_dir, (int)strlen(name) - 1, name);
b7cece76 1920
aeafcbaf
ACM
1921 if (sysfs__read_build_id(filename, dso->build_id,
1922 sizeof(dso->build_id)) == 0)
1923 dso->has_build_id = true;
b7cece76
ACM
1924
1925 return 0;
1926}
1927
aeafcbaf 1928static int map_groups__set_modules_path_dir(struct map_groups *mg,
a1645ce1 1929 const char *dir_name)
6cfcc53e 1930{
439d473b 1931 struct dirent *dent;
5aab621b 1932 DIR *dir = opendir(dir_name);
74534341 1933 int ret = 0;
6cfcc53e 1934
439d473b 1935 if (!dir) {
5aab621b 1936 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
439d473b
ACM
1937 return -1;
1938 }
6cfcc53e 1939
439d473b
ACM
1940 while ((dent = readdir(dir)) != NULL) {
1941 char path[PATH_MAX];
a1645ce1
ZY
1942 struct stat st;
1943
1944 /*sshfs might return bad dent->d_type, so we have to stat*/
2b600f95 1945 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
a1645ce1
ZY
1946 if (stat(path, &st))
1947 continue;
439d473b 1948
a1645ce1 1949 if (S_ISDIR(st.st_mode)) {
439d473b
ACM
1950 if (!strcmp(dent->d_name, ".") ||
1951 !strcmp(dent->d_name, ".."))
1952 continue;
1953
aeafcbaf 1954 ret = map_groups__set_modules_path_dir(mg, path);
74534341
GJ
1955 if (ret < 0)
1956 goto out;
439d473b
ACM
1957 } else {
1958 char *dot = strrchr(dent->d_name, '.'),
1959 dso_name[PATH_MAX];
1960 struct map *map;
cfc10d3b 1961 char *long_name;
439d473b
ACM
1962
1963 if (dot == NULL || strcmp(dot, ".ko"))
1964 continue;
1965 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1966 (int)(dot - dent->d_name), dent->d_name);
1967
a2a99e8e 1968 strxfrchar(dso_name, '-', '_');
aeafcbaf
ACM
1969 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1970 dso_name);
439d473b
ACM
1971 if (map == NULL)
1972 continue;
1973
cfc10d3b 1974 long_name = strdup(path);
74534341
GJ
1975 if (long_name == NULL) {
1976 ret = -1;
1977 goto out;
1978 }
cfc10d3b 1979 dso__set_long_name(map->dso, long_name);
6e406257 1980 map->dso->lname_alloc = 1;
a1645ce1 1981 dso__kernel_module_get_build_id(map->dso, "");
439d473b 1982 }
439d473b 1983 }
6cfcc53e 1984
74534341 1985out:
439d473b 1986 closedir(dir);
74534341 1987 return ret;
439d473b 1988}
6cfcc53e 1989
a1645ce1 1990static char *get_kernel_version(const char *root_dir)
439d473b 1991{
a1645ce1
ZY
1992 char version[PATH_MAX];
1993 FILE *file;
1994 char *name, *tmp;
1995 const char *prefix = "Linux version ";
1996
1997 sprintf(version, "%s/proc/version", root_dir);
1998 file = fopen(version, "r");
1999 if (!file)
2000 return NULL;
2001
2002 version[0] = '\0';
2003 tmp = fgets(version, sizeof(version), file);
2004 fclose(file);
2005
2006 name = strstr(version, prefix);
2007 if (!name)
2008 return NULL;
2009 name += strlen(prefix);
2010 tmp = strchr(name, ' ');
2011 if (tmp)
2012 *tmp = '\0';
2013
2014 return strdup(name);
2015}
2016
aeafcbaf 2017static int machine__set_modules_path(struct machine *machine)
a1645ce1
ZY
2018{
2019 char *version;
439d473b 2020 char modules_path[PATH_MAX];
6cfcc53e 2021
aeafcbaf 2022 version = get_kernel_version(machine->root_dir);
a1645ce1 2023 if (!version)
439d473b 2024 return -1;
6cfcc53e 2025
a1645ce1 2026 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
aeafcbaf 2027 machine->root_dir, version);
a1645ce1 2028 free(version);
6cfcc53e 2029
aeafcbaf 2030 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
6cfcc53e
MG
2031}
2032
439d473b
ACM
2033/*
2034 * Constructor variant for modules (where we know from /proc/modules where
2035 * they are loaded) and for vmlinux, where only after we load all the
2036 * symbols we'll know where it starts and ends.
2037 */
3610583c 2038static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 2039{
aeafcbaf
ACM
2040 struct map *map = calloc(1, (sizeof(*map) +
2041 (dso->kernel ? sizeof(struct kmap) : 0)));
2042 if (map != NULL) {
439d473b 2043 /*
afb7b4f0 2044 * ->end will be filled after we load all the symbols
439d473b 2045 */
aeafcbaf 2046 map__init(map, type, start, 0, 0, dso);
439d473b 2047 }
afb7b4f0 2048
aeafcbaf 2049 return map;
439d473b
ACM
2050}
2051
aeafcbaf 2052struct map *machine__new_module(struct machine *machine, u64 start,
d28c6223 2053 const char *filename)
b7cece76
ACM
2054{
2055 struct map *map;
aeafcbaf 2056 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
b7cece76
ACM
2057
2058 if (dso == NULL)
2059 return NULL;
2060
2061 map = map__new2(start, dso, MAP__FUNCTION);
2062 if (map == NULL)
2063 return NULL;
2064
aeafcbaf 2065 if (machine__is_host(machine))
44f24cb3 2066 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
a1645ce1 2067 else
44f24cb3 2068 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
aeafcbaf 2069 map_groups__insert(&machine->kmaps, map);
b7cece76
ACM
2070 return map;
2071}
2072
aeafcbaf 2073static int machine__create_modules(struct machine *machine)
439d473b
ACM
2074{
2075 char *line = NULL;
2076 size_t n;
a1645ce1 2077 FILE *file;
439d473b 2078 struct map *map;
a1645ce1
ZY
2079 const char *modules;
2080 char path[PATH_MAX];
2081
aeafcbaf 2082 if (machine__is_default_guest(machine))
a1645ce1
ZY
2083 modules = symbol_conf.default_guest_modules;
2084 else {
aeafcbaf 2085 sprintf(path, "%s/proc/modules", machine->root_dir);
a1645ce1
ZY
2086 modules = path;
2087 }
6cfcc53e 2088
ec80fde7
ACM
2089 if (symbol__restricted_filename(path, "/proc/modules"))
2090 return -1;
2091
a1645ce1 2092 file = fopen(modules, "r");
439d473b
ACM
2093 if (file == NULL)
2094 return -1;
6cfcc53e 2095
439d473b
ACM
2096 while (!feof(file)) {
2097 char name[PATH_MAX];
2098 u64 start;
439d473b
ACM
2099 char *sep;
2100 int line_len;
6cfcc53e 2101
439d473b
ACM
2102 line_len = getline(&line, &n, file);
2103 if (line_len < 0)
2104 break;
2105
2106 if (!line)
2107 goto out_failure;
2108
2109 line[--line_len] = '\0'; /* \n */
2110
2111 sep = strrchr(line, 'x');
2112 if (sep == NULL)
2113 continue;
2114
2115 hex2u64(sep + 1, &start);
2116
2117 sep = strchr(line, ' ');
2118 if (sep == NULL)
2119 continue;
2120
2121 *sep = '\0';
2122
2123 snprintf(name, sizeof(name), "[%s]", line);
aeafcbaf 2124 map = machine__new_module(machine, start, name);
b7cece76 2125 if (map == NULL)
439d473b 2126 goto out_delete_line;
aeafcbaf 2127 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
6cfcc53e 2128 }
439d473b
ACM
2129
2130 free(line);
2131 fclose(file);
2132
aeafcbaf 2133 return machine__set_modules_path(machine);
439d473b
ACM
2134
2135out_delete_line:
2136 free(line);
2137out_failure:
2138 return -1;
6cfcc53e
MG
2139}
2140
aeafcbaf 2141int dso__load_vmlinux(struct dso *dso, struct map *map,
fd930ff9 2142 const char *vmlinux, symbol_filter_t filter)
a2928c42 2143{
fbd733b8 2144 int err = -1, fd;
ec5761ea 2145 char symfs_vmlinux[PATH_MAX];
a2928c42 2146
a639dc64 2147 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
ec5761ea
DA
2148 symbol_conf.symfs, vmlinux);
2149 fd = open(symfs_vmlinux, O_RDONLY);
a2928c42
ACM
2150 if (fd < 0)
2151 return -1;
2152
aeafcbaf
ACM
2153 dso__set_long_name(dso, (char *)vmlinux);
2154 dso__set_loaded(dso, map->type);
2155 err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
a2928c42
ACM
2156 close(fd);
2157
3846df2e 2158 if (err > 0)
ec5761ea 2159 pr_debug("Using %s for symbols\n", symfs_vmlinux);
3846df2e 2160
a2928c42
ACM
2161 return err;
2162}
2163
aeafcbaf 2164int dso__load_vmlinux_path(struct dso *dso, struct map *map,
9de89fe7 2165 symbol_filter_t filter)
a19afe46
ACM
2166{
2167 int i, err = 0;
5ad90e4e 2168 char *filename;
a19afe46
ACM
2169
2170 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
5ad90e4e
ACM
2171 vmlinux_path__nr_entries + 1);
2172
aeafcbaf 2173 filename = dso__build_id_filename(dso, NULL, 0);
5ad90e4e 2174 if (filename != NULL) {
aeafcbaf 2175 err = dso__load_vmlinux(dso, map, filename, filter);
5ad90e4e 2176 if (err > 0) {
aeafcbaf 2177 dso__set_long_name(dso, filename);
5ad90e4e
ACM
2178 goto out;
2179 }
2180 free(filename);
2181 }
a19afe46
ACM
2182
2183 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
aeafcbaf 2184 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
a19afe46 2185 if (err > 0) {
aeafcbaf 2186 dso__set_long_name(dso, strdup(vmlinux_path[i]));
a19afe46
ACM
2187 break;
2188 }
2189 }
5ad90e4e 2190out:
a19afe46
ACM
2191 return err;
2192}
2193
aeafcbaf 2194static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 2195 symbol_filter_t filter)
a827c875 2196{
cc612d81 2197 int err;
9e201442
ACM
2198 const char *kallsyms_filename = NULL;
2199 char *kallsyms_allocated_filename = NULL;
dc8d6ab2 2200 /*
b226a5a7
DA
2201 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2202 * it and only it, reporting errors to the user if it cannot be used.
dc8d6ab2
ACM
2203 *
2204 * For instance, try to analyse an ARM perf.data file _without_ a
2205 * build-id, or if the user specifies the wrong path to the right
2206 * vmlinux file, obviously we can't fallback to another vmlinux (a
2207 * x86_86 one, on the machine where analysis is being performed, say),
2208 * or worse, /proc/kallsyms.
2209 *
2210 * If the specified file _has_ a build-id and there is a build-id
2211 * section in the perf.data file, we will still do the expected
2212 * validation in dso__load_vmlinux and will bail out if they don't
2213 * match.
2214 */
b226a5a7
DA
2215 if (symbol_conf.kallsyms_name != NULL) {
2216 kallsyms_filename = symbol_conf.kallsyms_name;
2217 goto do_kallsyms;
2218 }
2219
dc8d6ab2 2220 if (symbol_conf.vmlinux_name != NULL) {
aeafcbaf 2221 err = dso__load_vmlinux(dso, map,
dc8d6ab2 2222 symbol_conf.vmlinux_name, filter);
e7dadc00 2223 if (err > 0) {
aeafcbaf 2224 dso__set_long_name(dso,
e7dadc00
ACM
2225 strdup(symbol_conf.vmlinux_name));
2226 goto out_fixup;
2227 }
2228 return err;
dc8d6ab2 2229 }
cc612d81
ACM
2230
2231 if (vmlinux_path != NULL) {
aeafcbaf 2232 err = dso__load_vmlinux_path(dso, map, filter);
a19afe46
ACM
2233 if (err > 0)
2234 goto out_fixup;
cc612d81
ACM
2235 }
2236
ec5761ea
DA
2237 /* do not try local files if a symfs was given */
2238 if (symbol_conf.symfs[0] != 0)
2239 return -1;
2240
b7cece76
ACM
2241 /*
2242 * Say the kernel DSO was created when processing the build-id header table,
2243 * we have a build-id, so check if it is the same as the running kernel,
2244 * using it if it is.
2245 */
aeafcbaf 2246 if (dso->has_build_id) {
b7cece76 2247 u8 kallsyms_build_id[BUILD_ID_SIZE];
9e201442 2248 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
b7cece76
ACM
2249
2250 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
8d0591f6 2251 sizeof(kallsyms_build_id)) == 0) {
aeafcbaf 2252 if (dso__build_id_equal(dso, kallsyms_build_id)) {
9e201442 2253 kallsyms_filename = "/proc/kallsyms";
8d0591f6 2254 goto do_kallsyms;
9e201442 2255 }
8d0591f6 2256 }
dc8d6ab2
ACM
2257 /*
2258 * Now look if we have it on the build-id cache in
2259 * $HOME/.debug/[kernel.kallsyms].
2260 */
aeafcbaf 2261 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
9e201442
ACM
2262 sbuild_id);
2263
2264 if (asprintf(&kallsyms_allocated_filename,
2265 "%s/.debug/[kernel.kallsyms]/%s",
3846df2e
ACM
2266 getenv("HOME"), sbuild_id) == -1) {
2267 pr_err("Not enough memory for kallsyms file lookup\n");
dc8d6ab2 2268 return -1;
3846df2e 2269 }
dc8d6ab2 2270
19fc2ded
ACM
2271 kallsyms_filename = kallsyms_allocated_filename;
2272
dc8d6ab2 2273 if (access(kallsyms_filename, F_OK)) {
3846df2e
ACM
2274 pr_err("No kallsyms or vmlinux with build-id %s "
2275 "was found\n", sbuild_id);
9e201442 2276 free(kallsyms_allocated_filename);
dc8d6ab2 2277 return -1;
9e201442 2278 }
dc8d6ab2
ACM
2279 } else {
2280 /*
2281 * Last resort, if we don't have a build-id and couldn't find
2282 * any vmlinux file, try the running kernel kallsyms table.
2283 */
9e201442 2284 kallsyms_filename = "/proc/kallsyms";
9e201442 2285 }
439d473b 2286
cc612d81 2287do_kallsyms:
aeafcbaf 2288 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
3846df2e
ACM
2289 if (err > 0)
2290 pr_debug("Using %s for symbols\n", kallsyms_filename);
dc8d6ab2 2291 free(kallsyms_allocated_filename);
439d473b
ACM
2292
2293 if (err > 0) {
cc612d81 2294out_fixup:
e1c7c6a4 2295 if (kallsyms_filename != NULL)
aeafcbaf 2296 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
6a4694a4
ACM
2297 map__fixup_start(map);
2298 map__fixup_end(map);
439d473b 2299 }
94cb9e38 2300
a827c875
ACM
2301 return err;
2302}
2303
aeafcbaf
ACM
2304static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
2305 symbol_filter_t filter)
a1645ce1
ZY
2306{
2307 int err;
2308 const char *kallsyms_filename = NULL;
23346f21 2309 struct machine *machine;
a1645ce1
ZY
2310 char path[PATH_MAX];
2311
2312 if (!map->groups) {
2313 pr_debug("Guest kernel map hasn't the point to groups\n");
2314 return -1;
2315 }
23346f21 2316 machine = map->groups->machine;
a1645ce1 2317
23346f21 2318 if (machine__is_default_guest(machine)) {
a1645ce1
ZY
2319 /*
2320 * if the user specified a vmlinux filename, use it and only
2321 * it, reporting errors to the user if it cannot be used.
2322 * Or use file guest_kallsyms inputted by user on commandline
2323 */
2324 if (symbol_conf.default_guest_vmlinux_name != NULL) {
aeafcbaf 2325 err = dso__load_vmlinux(dso, map,
a1645ce1
ZY
2326 symbol_conf.default_guest_vmlinux_name, filter);
2327 goto out_try_fixup;
2328 }
2329
2330 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2331 if (!kallsyms_filename)
2332 return -1;
2333 } else {
23346f21 2334 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
a1645ce1
ZY
2335 kallsyms_filename = path;
2336 }
2337
aeafcbaf 2338 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
a1645ce1
ZY
2339 if (err > 0)
2340 pr_debug("Using %s for symbols\n", kallsyms_filename);
2341
2342out_try_fixup:
2343 if (err > 0) {
2344 if (kallsyms_filename != NULL) {
48ea8f54 2345 machine__mmap_name(machine, path, sizeof(path));
aeafcbaf 2346 dso__set_long_name(dso, strdup(path));
a1645ce1
ZY
2347 }
2348 map__fixup_start(map);
2349 map__fixup_end(map);
2350 }
2351
2352 return err;
2353}
cd84c2ac 2354
b0da954a 2355static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 2356{
b0da954a 2357 list_add_tail(&dso->node, head);
cd84c2ac
FW
2358}
2359
b0da954a 2360static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
2361{
2362 struct dso *pos;
2363
b0da954a 2364 list_for_each_entry(pos, head, node)
cf4e5b08 2365 if (strcmp(pos->long_name, name) == 0)
cd84c2ac
FW
2366 return pos;
2367 return NULL;
2368}
2369
a89e5abe 2370struct dso *__dsos__findnew(struct list_head *head, const char *name)
cd84c2ac 2371{
a89e5abe 2372 struct dso *dso = dsos__find(head, name);
cd84c2ac 2373
e4204992 2374 if (!dso) {
00a192b3 2375 dso = dso__new(name);
cfc10d3b 2376 if (dso != NULL) {
a89e5abe 2377 dsos__add(head, dso);
cfc10d3b
ACM
2378 dso__set_basename(dso);
2379 }
66bd8424 2380 }
cd84c2ac
FW
2381
2382 return dso;
cd84c2ac
FW
2383}
2384
1f626bc3 2385size_t __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
2386{
2387 struct dso *pos;
cbf69680 2388 size_t ret = 0;
cd84c2ac 2389
95011c60
ACM
2390 list_for_each_entry(pos, head, node) {
2391 int i;
2392 for (i = 0; i < MAP__NR_TYPES; ++i)
cbf69680 2393 ret += dso__fprintf(pos, i, fp);
95011c60 2394 }
cbf69680
ACM
2395
2396 return ret;
cd84c2ac
FW
2397}
2398
aeafcbaf 2399size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
b0da954a 2400{
a1645ce1 2401 struct rb_node *nd;
cbf69680 2402 size_t ret = 0;
a1645ce1 2403
aeafcbaf 2404 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
23346f21 2405 struct machine *pos = rb_entry(nd, struct machine, rb_node);
cbf69680
ACM
2406 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2407 ret += __dsos__fprintf(&pos->user_dsos, fp);
a1645ce1 2408 }
cbf69680
ACM
2409
2410 return ret;
b0da954a
ACM
2411}
2412
88d3d9b7
ACM
2413static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2414 bool with_hits)
9e03eb2d
ACM
2415{
2416 struct dso *pos;
2417 size_t ret = 0;
2418
b0da954a 2419 list_for_each_entry(pos, head, node) {
88d3d9b7
ACM
2420 if (with_hits && !pos->hit)
2421 continue;
9e03eb2d 2422 ret += dso__fprintf_buildid(pos, fp);
1124ba73 2423 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
2424 }
2425 return ret;
2426}
2427
aeafcbaf
ACM
2428size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
2429 bool with_hits)
f869097e 2430{
aeafcbaf
ACM
2431 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
2432 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
f869097e
ACM
2433}
2434
aeafcbaf
ACM
2435size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
2436 FILE *fp, bool with_hits)
b0da954a 2437{
a1645ce1
ZY
2438 struct rb_node *nd;
2439 size_t ret = 0;
2440
aeafcbaf 2441 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
23346f21 2442 struct machine *pos = rb_entry(nd, struct machine, rb_node);
f869097e 2443 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
a1645ce1
ZY
2444 }
2445 return ret;
b0da954a
ACM
2446}
2447
f57b05ed
JO
2448static struct dso*
2449dso__kernel_findnew(struct machine *machine, const char *name,
2450 const char *short_name, int dso_type)
fd1d908c 2451{
f57b05ed
JO
2452 /*
2453 * The kernel dso could be created by build_id processing.
2454 */
2455 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
a1645ce1 2456
f57b05ed
JO
2457 /*
2458 * We need to run this in all cases, since during the build_id
2459 * processing we had no idea this was the kernel dso.
2460 */
aeafcbaf 2461 if (dso != NULL) {
f57b05ed
JO
2462 dso__set_short_name(dso, short_name);
2463 dso->kernel = dso_type;
fd1d908c
ACM
2464 }
2465
aeafcbaf 2466 return dso;
fd1d908c
ACM
2467}
2468
aeafcbaf 2469void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
fd1d908c 2470{
a1645ce1
ZY
2471 char path[PATH_MAX];
2472
23346f21 2473 if (machine__is_default_guest(machine))
a1645ce1 2474 return;
23346f21 2475 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
aeafcbaf
ACM
2476 if (sysfs__read_build_id(path, dso->build_id,
2477 sizeof(dso->build_id)) == 0)
2478 dso->has_build_id = true;
fd1d908c
ACM
2479}
2480
f57b05ed 2481static struct dso *machine__get_kernel(struct machine *machine)
cd84c2ac 2482{
a1645ce1
ZY
2483 const char *vmlinux_name = NULL;
2484 struct dso *kernel;
cd84c2ac 2485
aeafcbaf 2486 if (machine__is_host(machine)) {
a1645ce1 2487 vmlinux_name = symbol_conf.vmlinux_name;
f57b05ed
JO
2488 if (!vmlinux_name)
2489 vmlinux_name = "[kernel.kallsyms]";
2490
2491 kernel = dso__kernel_findnew(machine, vmlinux_name,
2492 "[kernel]",
2493 DSO_TYPE_KERNEL);
a1645ce1 2494 } else {
f57b05ed
JO
2495 char bf[PATH_MAX];
2496
aeafcbaf 2497 if (machine__is_default_guest(machine))
a1645ce1 2498 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
f57b05ed
JO
2499 if (!vmlinux_name)
2500 vmlinux_name = machine__mmap_name(machine, bf,
2501 sizeof(bf));
2502
2503 kernel = dso__kernel_findnew(machine, vmlinux_name,
2504 "[guest.kernel]",
2505 DSO_TYPE_GUEST_KERNEL);
8d92c02a 2506 }
cd84c2ac 2507
f57b05ed 2508 if (kernel != NULL && (!kernel->has_build_id))
aeafcbaf 2509 dso__read_running_kernel_build_id(kernel, machine);
f57b05ed 2510
f1dfa0b1 2511 return kernel;
f1dfa0b1
ACM
2512}
2513
d214afbd
ML
2514struct process_args {
2515 u64 start;
2516};
2517
2518static int symbol__in_kernel(void *arg, const char *name,
3b01a413 2519 char type __used, u64 start, u64 end __used)
d214afbd
ML
2520{
2521 struct process_args *args = arg;
2522
2523 if (strchr(name, '['))
2524 return 0;
2525
2526 args->start = start;
2527 return 1;
2528}
2529
2530/* Figure out the start address of kernel map from /proc/kallsyms */
2531static u64 machine__get_kernel_start_addr(struct machine *machine)
2532{
2533 const char *filename;
2534 char path[PATH_MAX];
2535 struct process_args args;
2536
2537 if (machine__is_host(machine)) {
2538 filename = "/proc/kallsyms";
2539 } else {
2540 if (machine__is_default_guest(machine))
2541 filename = (char *)symbol_conf.default_guest_kallsyms;
2542 else {
2543 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2544 filename = path;
2545 }
2546 }
2547
ec80fde7
ACM
2548 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
2549 return 0;
2550
d214afbd
ML
2551 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2552 return 0;
2553
2554 return args.start;
2555}
2556
aeafcbaf 2557int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
f1dfa0b1 2558{
de176489 2559 enum map_type type;
aeafcbaf 2560 u64 start = machine__get_kernel_start_addr(machine);
f1dfa0b1 2561
de176489 2562 for (type = 0; type < MAP__NR_TYPES; ++type) {
9de89fe7
ACM
2563 struct kmap *kmap;
2564
aeafcbaf
ACM
2565 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
2566 if (machine->vmlinux_maps[type] == NULL)
de176489 2567 return -1;
f1dfa0b1 2568
aeafcbaf
ACM
2569 machine->vmlinux_maps[type]->map_ip =
2570 machine->vmlinux_maps[type]->unmap_ip =
2571 identity__map_ip;
2572 kmap = map__kmap(machine->vmlinux_maps[type]);
2573 kmap->kmaps = &machine->kmaps;
2574 map_groups__insert(&machine->kmaps,
2575 machine->vmlinux_maps[type]);
f1dfa0b1
ACM
2576 }
2577
f1dfa0b1 2578 return 0;
2446042c
ACM
2579}
2580
aeafcbaf 2581void machine__destroy_kernel_maps(struct machine *machine)
076c6e45
ACM
2582{
2583 enum map_type type;
2584
2585 for (type = 0; type < MAP__NR_TYPES; ++type) {
2586 struct kmap *kmap;
2587
aeafcbaf 2588 if (machine->vmlinux_maps[type] == NULL)
076c6e45
ACM
2589 continue;
2590
aeafcbaf
ACM
2591 kmap = map__kmap(machine->vmlinux_maps[type]);
2592 map_groups__remove(&machine->kmaps,
2593 machine->vmlinux_maps[type]);
076c6e45
ACM
2594 if (kmap->ref_reloc_sym) {
2595 /*
2596 * ref_reloc_sym is shared among all maps, so free just
2597 * on one of them.
2598 */
2599 if (type == MAP__FUNCTION) {
2600 free((char *)kmap->ref_reloc_sym->name);
2601 kmap->ref_reloc_sym->name = NULL;
2602 free(kmap->ref_reloc_sym);
2603 }
2604 kmap->ref_reloc_sym = NULL;
2605 }
2606
aeafcbaf
ACM
2607 map__delete(machine->vmlinux_maps[type]);
2608 machine->vmlinux_maps[type] = NULL;
076c6e45
ACM
2609 }
2610}
2611
aeafcbaf 2612int machine__create_kernel_maps(struct machine *machine)
5c0541d5 2613{
f57b05ed 2614 struct dso *kernel = machine__get_kernel(machine);
5c0541d5
ACM
2615
2616 if (kernel == NULL ||
aeafcbaf 2617 __machine__create_kernel_maps(machine, kernel) < 0)
5c0541d5
ACM
2618 return -1;
2619
f51304d3
DA
2620 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
2621 if (machine__is_host(machine))
2622 pr_debug("Problems creating module maps, "
2623 "continuing anyway...\n");
2624 else
2625 pr_debug("Problems creating module maps for guest %d, "
2626 "continuing anyway...\n", machine->pid);
2627 }
2628
5c0541d5
ACM
2629 /*
2630 * Now that we have all the maps created, just set the ->end of them:
2631 */
aeafcbaf 2632 map_groups__fixup_end(&machine->kmaps);
5c0541d5
ACM
2633 return 0;
2634}
2635
cc612d81
ACM
2636static void vmlinux_path__exit(void)
2637{
2638 while (--vmlinux_path__nr_entries >= 0) {
2639 free(vmlinux_path[vmlinux_path__nr_entries]);
2640 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2641 }
2642
2643 free(vmlinux_path);
2644 vmlinux_path = NULL;
2645}
2646
2647static int vmlinux_path__init(void)
2648{
2649 struct utsname uts;
2650 char bf[PATH_MAX];
2651
cc612d81
ACM
2652 vmlinux_path = malloc(sizeof(char *) * 5);
2653 if (vmlinux_path == NULL)
2654 return -1;
2655
2656 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2657 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2658 goto out_fail;
2659 ++vmlinux_path__nr_entries;
2660 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2661 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2662 goto out_fail;
2663 ++vmlinux_path__nr_entries;
ec5761ea
DA
2664
2665 /* only try running kernel version if no symfs was given */
2666 if (symbol_conf.symfs[0] != 0)
2667 return 0;
2668
2669 if (uname(&uts) < 0)
2670 return -1;
2671
cc612d81
ACM
2672 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2673 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2674 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2675 goto out_fail;
2676 ++vmlinux_path__nr_entries;
2677 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2678 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2679 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2680 goto out_fail;
2681 ++vmlinux_path__nr_entries;
2682 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2683 uts.release);
2684 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2685 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2686 goto out_fail;
2687 ++vmlinux_path__nr_entries;
2688
2689 return 0;
2690
2691out_fail:
2692 vmlinux_path__exit();
2693 return -1;
2694}
2695
aeafcbaf 2696size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
b0a9ab62
ACM
2697{
2698 int i;
2699 size_t printed = 0;
aeafcbaf 2700 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
5ad90e4e
ACM
2701
2702 if (kdso->has_build_id) {
2703 char filename[PATH_MAX];
2704 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2705 printed += fprintf(fp, "[0] %s\n", filename);
2706 }
b0a9ab62
ACM
2707
2708 for (i = 0; i < vmlinux_path__nr_entries; ++i)
5ad90e4e
ACM
2709 printed += fprintf(fp, "[%d] %s\n",
2710 i + kdso->has_build_id, vmlinux_path[i]);
b0a9ab62
ACM
2711
2712 return printed;
2713}
2714
655000e7
ACM
2715static int setup_list(struct strlist **list, const char *list_str,
2716 const char *list_name)
2717{
2718 if (list_str == NULL)
2719 return 0;
2720
2721 *list = strlist__new(true, list_str);
2722 if (!*list) {
2723 pr_err("problems parsing %s list\n", list_name);
2724 return -1;
2725 }
2726 return 0;
2727}
2728
ec80fde7
ACM
2729static bool symbol__read_kptr_restrict(void)
2730{
2731 bool value = false;
2732
2733 if (geteuid() != 0) {
2734 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2735 if (fp != NULL) {
2736 char line[8];
2737
2738 if (fgets(line, sizeof(line), fp) != NULL)
2739 value = atoi(line) != 0;
2740
2741 fclose(fp);
2742 }
2743 }
2744
2745 return value;
2746}
2747
75be6cf4 2748int symbol__init(void)
2446042c 2749{
ec5761ea
DA
2750 const char *symfs;
2751
85e00b55
JZ
2752 if (symbol_conf.initialized)
2753 return 0;
2754
4d439517
DM
2755 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2756
95011c60 2757 elf_version(EV_CURRENT);
75be6cf4
ACM
2758 if (symbol_conf.sort_by_name)
2759 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2760 sizeof(struct symbol));
b32d133a 2761
75be6cf4 2762 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
2763 return -1;
2764
c410a338
ACM
2765 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2766 pr_err("'.' is the only non valid --field-separator argument\n");
2767 return -1;
2768 }
2769
655000e7
ACM
2770 if (setup_list(&symbol_conf.dso_list,
2771 symbol_conf.dso_list_str, "dso") < 0)
2772 return -1;
2773
2774 if (setup_list(&symbol_conf.comm_list,
2775 symbol_conf.comm_list_str, "comm") < 0)
2776 goto out_free_dso_list;
2777
2778 if (setup_list(&symbol_conf.sym_list,
2779 symbol_conf.sym_list_str, "symbol") < 0)
2780 goto out_free_comm_list;
2781
ec5761ea
DA
2782 /*
2783 * A path to symbols of "/" is identical to ""
2784 * reset here for simplicity.
2785 */
2786 symfs = realpath(symbol_conf.symfs, NULL);
2787 if (symfs == NULL)
2788 symfs = symbol_conf.symfs;
2789 if (strcmp(symfs, "/") == 0)
2790 symbol_conf.symfs = "";
2791 if (symfs != symbol_conf.symfs)
2792 free((void *)symfs);
2793
ec80fde7
ACM
2794 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2795
85e00b55 2796 symbol_conf.initialized = true;
4aa65636 2797 return 0;
655000e7 2798
655000e7
ACM
2799out_free_comm_list:
2800 strlist__delete(symbol_conf.comm_list);
d74c896b
NK
2801out_free_dso_list:
2802 strlist__delete(symbol_conf.dso_list);
655000e7 2803 return -1;
4aa65636
ACM
2804}
2805
d65a458b
ACM
2806void symbol__exit(void)
2807{
85e00b55
JZ
2808 if (!symbol_conf.initialized)
2809 return;
d65a458b
ACM
2810 strlist__delete(symbol_conf.sym_list);
2811 strlist__delete(symbol_conf.dso_list);
2812 strlist__delete(symbol_conf.comm_list);
2813 vmlinux_path__exit();
2814 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
85e00b55 2815 symbol_conf.initialized = false;
d65a458b
ACM
2816}
2817
aeafcbaf 2818int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
4aa65636 2819{
aeafcbaf 2820 struct machine *machine = machines__findnew(machines, pid);
9de89fe7 2821
23346f21 2822 if (machine == NULL)
a1645ce1 2823 return -1;
cc612d81 2824
5c0541d5 2825 return machine__create_kernel_maps(machine);
cd84c2ac 2826}
5aab621b
ACM
2827
2828static int hex(char ch)
2829{
2830 if ((ch >= '0') && (ch <= '9'))
2831 return ch - '0';
2832 if ((ch >= 'a') && (ch <= 'f'))
2833 return ch - 'a' + 10;
2834 if ((ch >= 'A') && (ch <= 'F'))
2835 return ch - 'A' + 10;
2836 return -1;
2837}
2838
2839/*
2840 * While we find nice hex chars, build a long_val.
2841 * Return number of chars processed.
2842 */
2843int hex2u64(const char *ptr, u64 *long_val)
2844{
2845 const char *p = ptr;
2846 *long_val = 0;
2847
2848 while (*p) {
2849 const int hex_val = hex(*p);
2850
2851 if (hex_val < 0)
2852 break;
2853
2854 *long_val = (*long_val << 4) | hex_val;
2855 p++;
2856 }
2857
2858 return p - ptr;
2859}
2860
2861char *strxfrchar(char *s, char from, char to)
2862{
2863 char *p = s;
2864
2865 while ((p = strchr(p, from)) != NULL)
2866 *p++ = to;
2867
2868 return s;
2869}
a1645ce1 2870
aeafcbaf 2871int machines__create_guest_kernel_maps(struct rb_root *machines)
a1645ce1
ZY
2872{
2873 int ret = 0;
2874 struct dirent **namelist = NULL;
2875 int i, items = 0;
2876 char path[PATH_MAX];
2877 pid_t pid;
347ed990 2878 char *endp;
a1645ce1
ZY
2879
2880 if (symbol_conf.default_guest_vmlinux_name ||
2881 symbol_conf.default_guest_modules ||
2882 symbol_conf.default_guest_kallsyms) {
aeafcbaf 2883 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
a1645ce1
ZY
2884 }
2885
2886 if (symbol_conf.guestmount) {
2887 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2888 if (items <= 0)
2889 return -ENOENT;
2890 for (i = 0; i < items; i++) {
2891 if (!isdigit(namelist[i]->d_name[0])) {
2892 /* Filter out . and .. */
2893 continue;
2894 }
347ed990
DA
2895 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2896 if ((*endp != '\0') ||
2897 (endp == namelist[i]->d_name) ||
2898 (errno == ERANGE)) {
2899 pr_debug("invalid directory (%s). Skipping.\n",
2900 namelist[i]->d_name);
2901 continue;
2902 }
a1645ce1
ZY
2903 sprintf(path, "%s/%s/proc/kallsyms",
2904 symbol_conf.guestmount,
2905 namelist[i]->d_name);
2906 ret = access(path, R_OK);
2907 if (ret) {
2908 pr_debug("Can't access file %s\n", path);
2909 goto failure;
2910 }
aeafcbaf 2911 machines__create_kernel_maps(machines, pid);
a1645ce1
ZY
2912 }
2913failure:
2914 free(namelist);
2915 }
2916
2917 return ret;
2918}
5c0541d5 2919
aeafcbaf 2920void machines__destroy_guest_kernel_maps(struct rb_root *machines)
076c6e45 2921{
aeafcbaf 2922 struct rb_node *next = rb_first(machines);
076c6e45
ACM
2923
2924 while (next) {
2925 struct machine *pos = rb_entry(next, struct machine, rb_node);
2926
2927 next = rb_next(&pos->rb_node);
aeafcbaf 2928 rb_erase(&pos->rb_node, machines);
076c6e45
ACM
2929 machine__delete(pos);
2930 }
2931}
2932
aeafcbaf 2933int machine__load_kallsyms(struct machine *machine, const char *filename,
5c0541d5
ACM
2934 enum map_type type, symbol_filter_t filter)
2935{
aeafcbaf 2936 struct map *map = machine->vmlinux_maps[type];
5c0541d5
ACM
2937 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2938
2939 if (ret > 0) {
2940 dso__set_loaded(map->dso, type);
2941 /*
2942 * Since /proc/kallsyms will have multiple sessions for the
2943 * kernel, with modules between them, fixup the end of all
2944 * sections.
2945 */
aeafcbaf 2946 __map_groups__fixup_end(&machine->kmaps, type);
5c0541d5
ACM
2947 }
2948
2949 return ret;
2950}
2951
aeafcbaf 2952int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
5c0541d5
ACM
2953 symbol_filter_t filter)
2954{
aeafcbaf 2955 struct map *map = machine->vmlinux_maps[type];
5c0541d5
ACM
2956 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2957
2958 if (ret > 0) {
2959 dso__set_loaded(map->dso, type);
2960 map__reloc_vmlinux(map);
2961 }
2962
2963 return ret;
2964}
225466f1
SD
2965
2966struct map *dso__new_map(const char *name)
2967{
378474e4 2968 struct map *map = NULL;
225466f1 2969 struct dso *dso = dso__new(name);
378474e4
SD
2970
2971 if (dso)
2972 map = map__new2(0, dso, MAP__FUNCTION);
225466f1
SD
2973
2974 return map;
2975}
949d160b
JO
2976
2977static int open_dso(struct dso *dso, struct machine *machine)
2978{
2979 char *root_dir = (char *) "";
2980 char *name;
2981 int fd;
2982
2983 name = malloc(PATH_MAX);
2984 if (!name)
2985 return -ENOMEM;
2986
2987 if (machine)
2988 root_dir = machine->root_dir;
2989
2990 if (dso__binary_type_file(dso, dso->data_type,
2991 root_dir, name, PATH_MAX)) {
2992 free(name);
2993 return -EINVAL;
2994 }
2995
2996 fd = open(name, O_RDONLY);
2997 free(name);
2998 return fd;
2999}
3000
3001int dso__data_fd(struct dso *dso, struct machine *machine)
3002{
3003 int i = 0;
3004
3005 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
3006 return open_dso(dso, machine);
3007
3008 do {
3009 int fd;
3010
3011 dso->data_type = binary_type_data[i++];
3012
3013 fd = open_dso(dso, machine);
3014 if (fd >= 0)
3015 return fd;
3016
3017 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
3018
3019 return -EINVAL;
3020}
3021
4dff624a
JO
3022static void
3023dso_cache__free(struct rb_root *root)
949d160b 3024{
4dff624a
JO
3025 struct rb_node *next = rb_first(root);
3026
3027 while (next) {
3028 struct dso_cache *cache;
3029
3030 cache = rb_entry(next, struct dso_cache, rb_node);
3031 next = rb_next(&cache->rb_node);
3032 rb_erase(&cache->rb_node, root);
3033 free(cache);
3034 }
949d160b
JO
3035}
3036
4dff624a
JO
3037static struct dso_cache*
3038dso_cache__find(struct rb_root *root, u64 offset)
949d160b 3039{
4dff624a
JO
3040 struct rb_node **p = &root->rb_node;
3041 struct rb_node *parent = NULL;
3042 struct dso_cache *cache;
3043
3044 while (*p != NULL) {
3045 u64 end;
3046
3047 parent = *p;
3048 cache = rb_entry(parent, struct dso_cache, rb_node);
3049 end = cache->offset + DSO__DATA_CACHE_SIZE;
3050
3051 if (offset < cache->offset)
3052 p = &(*p)->rb_left;
3053 else if (offset >= end)
3054 p = &(*p)->rb_right;
3055 else
3056 return cache;
3057 }
3058 return NULL;
3059}
3060
3061static void
3062dso_cache__insert(struct rb_root *root, struct dso_cache *new)
3063{
3064 struct rb_node **p = &root->rb_node;
3065 struct rb_node *parent = NULL;
3066 struct dso_cache *cache;
3067 u64 offset = new->offset;
3068
3069 while (*p != NULL) {
3070 u64 end;
3071
3072 parent = *p;
3073 cache = rb_entry(parent, struct dso_cache, rb_node);
3074 end = cache->offset + DSO__DATA_CACHE_SIZE;
3075
3076 if (offset < cache->offset)
3077 p = &(*p)->rb_left;
3078 else if (offset >= end)
3079 p = &(*p)->rb_right;
3080 }
3081
3082 rb_link_node(&new->rb_node, parent, p);
3083 rb_insert_color(&new->rb_node, root);
3084}
3085
3086static ssize_t
3087dso_cache__memcpy(struct dso_cache *cache, u64 offset,
3088 u8 *data, u64 size)
3089{
3090 u64 cache_offset = offset - cache->offset;
3091 u64 cache_size = min(cache->size - cache_offset, size);
3092
3093 memcpy(data, cache->data + cache_offset, cache_size);
3094 return cache_size;
949d160b
JO
3095}
3096
4dff624a
JO
3097static ssize_t
3098dso_cache__read(struct dso *dso, struct machine *machine,
3099 u64 offset, u8 *data, ssize_t size)
949d160b 3100{
4dff624a
JO
3101 struct dso_cache *cache;
3102 ssize_t ret;
949d160b
JO
3103 int fd;
3104
3105 fd = dso__data_fd(dso, machine);
3106 if (fd < 0)
3107 return -1;
3108
3109 do {
4dff624a
JO
3110 u64 cache_offset;
3111
3112 ret = -ENOMEM;
3113
3114 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
3115 if (!cache)
949d160b
JO
3116 break;
3117
4dff624a
JO
3118 cache_offset = offset & DSO__DATA_CACHE_MASK;
3119 ret = -EINVAL;
3120
3121 if (-1 == lseek(fd, cache_offset, SEEK_SET))
949d160b
JO
3122 break;
3123
4dff624a
JO
3124 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
3125 if (ret <= 0)
3126 break;
3127
3128 cache->offset = cache_offset;
3129 cache->size = ret;
3130 dso_cache__insert(&dso->cache, cache);
3131
3132 ret = dso_cache__memcpy(cache, offset, data, size);
949d160b
JO
3133
3134 } while (0);
3135
4dff624a
JO
3136 if (ret <= 0)
3137 free(cache);
3138
949d160b 3139 close(fd);
4dff624a
JO
3140 return ret;
3141}
3142
3143static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
3144 u64 offset, u8 *data, ssize_t size)
3145{
3146 struct dso_cache *cache;
3147
3148 cache = dso_cache__find(&dso->cache, offset);
3149 if (cache)
3150 return dso_cache__memcpy(cache, offset, data, size);
3151 else
3152 return dso_cache__read(dso, machine, offset, data, size);
949d160b
JO
3153}
3154
3155ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
3156 u64 offset, u8 *data, ssize_t size)
3157{
4dff624a
JO
3158 ssize_t r = 0;
3159 u8 *p = data;
3160
3161 do {
3162 ssize_t ret;
3163
3164 ret = dso_cache_read(dso, machine, offset, p, size);
3165 if (ret < 0)
3166 return ret;
3167
3168 /* Reached EOF, return what we have. */
3169 if (!ret)
3170 break;
3171
3172 BUG_ON(ret > size);
3173
3174 r += ret;
3175 p += ret;
3176 offset += ret;
3177 size -= ret;
3178
3179 } while (size);
3180
3181 return r;
949d160b
JO
3182}
3183
3184ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
3185 struct machine *machine, u64 addr,
3186 u8 *data, ssize_t size)
3187{
3188 u64 offset = map->map_ip(map, addr);
3189 return dso__data_read_offset(dso, machine, offset, data, size);
3190}