]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/symbol.c
perf symbols: Generalise the kallsyms parsing routine
[net-next-2.6.git] / tools / perf / util / symbol.c
CommitLineData
a2928c42
ACM
1#include "util.h"
2#include "../perf.h"
4aa65636 3#include "session.h"
655000e7 4#include "sort.h"
a0055ae2 5#include "string.h"
a2928c42 6#include "symbol.h"
439d473b 7#include "thread.h"
a2928c42 8
8f28827a
FW
9#include "debug.h"
10
b32d133a 11#include <asm/bug.h>
a2928c42
ACM
12#include <libelf.h>
13#include <gelf.h>
14#include <elf.h>
f1617b40 15#include <limits.h>
439d473b 16#include <sys/utsname.h>
2cdbc46d 17
c12e15e7
ACM
18#ifndef NT_GNU_BUILD_ID
19#define NT_GNU_BUILD_ID 3
20#endif
21
94cb9e38
ACM
22enum dso_origin {
23 DSO__ORIG_KERNEL = 0,
24 DSO__ORIG_JAVA_JIT,
4cf40131 25 DSO__ORIG_BUILD_ID_CACHE,
94cb9e38
ACM
26 DSO__ORIG_FEDORA,
27 DSO__ORIG_UBUNTU,
28 DSO__ORIG_BUILDID,
29 DSO__ORIG_DSO,
439d473b 30 DSO__ORIG_KMODULE,
94cb9e38
ACM
31 DSO__ORIG_NOT_FOUND,
32};
33
b0da954a 34static void dsos__add(struct list_head *head, struct dso *dso);
3610583c 35static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
c338aee8 36static int dso__load_kernel_sym(struct dso *self, struct map *map,
4aa65636 37 struct perf_session *session, symbol_filter_t filter);
cc612d81
ACM
38static int vmlinux_path__nr_entries;
39static char **vmlinux_path;
439d473b 40
75be6cf4 41struct symbol_conf symbol_conf = {
d599db3f 42 .exclude_other = true,
b32d133a
ACM
43 .use_modules = true,
44 .try_vmlinux_path = true,
45};
46
3610583c
ACM
47bool dso__loaded(const struct dso *self, enum map_type type)
48{
49 return self->loaded & (1 << type);
50}
51
79406cd7
ACM
52bool dso__sorted_by_name(const struct dso *self, enum map_type type)
53{
54 return self->sorted_by_name & (1 << type);
55}
56
3610583c
ACM
57static void dso__set_loaded(struct dso *self, enum map_type type)
58{
59 self->loaded |= (1 << type);
60}
61
79406cd7
ACM
62static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
63{
64 self->sorted_by_name |= (1 << type);
65}
66
6893d4ee
ACM
67static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
68{
69 switch (map_type) {
70 case MAP__FUNCTION:
71 return symbol_type == 'T' || symbol_type == 'W';
f1dfa0b1
ACM
72 case MAP__VARIABLE:
73 return symbol_type == 'D' || symbol_type == 'd';
6893d4ee
ACM
74 default:
75 return false;
76 }
77}
78
fcf1203a 79static void symbols__fixup_end(struct rb_root *self)
af427bf5 80{
fcf1203a 81 struct rb_node *nd, *prevnd = rb_first(self);
2e538c4a 82 struct symbol *curr, *prev;
af427bf5
ACM
83
84 if (prevnd == NULL)
85 return;
86
2e538c4a
ACM
87 curr = rb_entry(prevnd, struct symbol, rb_node);
88
af427bf5 89 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
90 prev = curr;
91 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5
ACM
92
93 if (prev->end == prev->start)
94 prev->end = curr->start - 1;
af427bf5 95 }
2e538c4a
ACM
96
97 /* Last entry */
98 if (curr->end == curr->start)
99 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
100}
101
9958e1f0 102static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
af427bf5
ACM
103{
104 struct map *prev, *curr;
95011c60 105 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
af427bf5
ACM
106
107 if (prevnd == NULL)
108 return;
109
110 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
111
112 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
113 prev = curr;
114 curr = rb_entry(nd, struct map, rb_node);
115 prev->end = curr->start - 1;
2e538c4a 116 }
90c83218
ACM
117
118 /*
119 * We still haven't the actual symbols, so guess the
120 * last map final address.
121 */
122 curr->end = ~0UL;
af427bf5
ACM
123}
124
9958e1f0 125static void map_groups__fixup_end(struct map_groups *self)
23ea4a3f
ACM
126{
127 int i;
128 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 129 __map_groups__fixup_end(self, i);
23ea4a3f
ACM
130}
131
00a192b3 132static struct symbol *symbol__new(u64 start, u64 len, const char *name)
a2928c42 133{
0085c954 134 size_t namelen = strlen(name) + 1;
75be6cf4 135 struct symbol *self = zalloc(symbol_conf.priv_size +
36479484
ACM
136 sizeof(*self) + namelen);
137 if (self == NULL)
0b73da3f
IM
138 return NULL;
139
75be6cf4
ACM
140 if (symbol_conf.priv_size)
141 self = ((void *)self) + symbol_conf.priv_size;
36479484 142
0b73da3f 143 self->start = start;
6cfcc53e 144 self->end = len ? start + len - 1 : start;
e4204992 145
6beba7ad 146 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
e4204992 147
0b73da3f 148 memcpy(self->name, name, namelen);
a2928c42
ACM
149
150 return self;
151}
152
00a192b3 153static void symbol__delete(struct symbol *self)
a2928c42 154{
75be6cf4 155 free(((void *)self) - symbol_conf.priv_size);
a2928c42
ACM
156}
157
158static size_t symbol__fprintf(struct symbol *self, FILE *fp)
159{
439d473b 160 return fprintf(fp, " %llx-%llx %s\n",
a2928c42
ACM
161 self->start, self->end, self->name);
162}
163
cfc10d3b
ACM
164static void dso__set_long_name(struct dso *self, char *name)
165{
ef6ae724
ACM
166 if (name == NULL)
167 return;
cfc10d3b
ACM
168 self->long_name = name;
169 self->long_name_len = strlen(name);
170}
171
172static void dso__set_basename(struct dso *self)
173{
174 self->short_name = basename(self->long_name);
175}
176
00a192b3 177struct dso *dso__new(const char *name)
a2928c42
ACM
178{
179 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
180
181 if (self != NULL) {
6a4694a4 182 int i;
a2928c42 183 strcpy(self->name, name);
cfc10d3b 184 dso__set_long_name(self, self->name);
439d473b 185 self->short_name = self->name;
6a4694a4 186 for (i = 0; i < MAP__NR_TYPES; ++i)
79406cd7 187 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
52d422de 188 self->slen_calculated = 0;
94cb9e38 189 self->origin = DSO__ORIG_NOT_FOUND;
8d06367f 190 self->loaded = 0;
79406cd7 191 self->sorted_by_name = 0;
8d06367f 192 self->has_build_id = 0;
a2928c42
ACM
193 }
194
195 return self;
196}
197
fcf1203a 198static void symbols__delete(struct rb_root *self)
a2928c42
ACM
199{
200 struct symbol *pos;
fcf1203a 201 struct rb_node *next = rb_first(self);
a2928c42
ACM
202
203 while (next) {
204 pos = rb_entry(next, struct symbol, rb_node);
205 next = rb_next(&pos->rb_node);
fcf1203a 206 rb_erase(&pos->rb_node, self);
00a192b3 207 symbol__delete(pos);
a2928c42
ACM
208 }
209}
210
211void dso__delete(struct dso *self)
212{
6a4694a4
ACM
213 int i;
214 for (i = 0; i < MAP__NR_TYPES; ++i)
215 symbols__delete(&self->symbols[i]);
439d473b
ACM
216 if (self->long_name != self->name)
217 free(self->long_name);
a2928c42
ACM
218 free(self);
219}
220
8d06367f
ACM
221void dso__set_build_id(struct dso *self, void *build_id)
222{
223 memcpy(self->build_id, build_id, sizeof(self->build_id));
224 self->has_build_id = 1;
225}
226
fcf1203a 227static void symbols__insert(struct rb_root *self, struct symbol *sym)
a2928c42 228{
fcf1203a 229 struct rb_node **p = &self->rb_node;
a2928c42 230 struct rb_node *parent = NULL;
9cffa8d5 231 const u64 ip = sym->start;
a2928c42
ACM
232 struct symbol *s;
233
234 while (*p != NULL) {
235 parent = *p;
236 s = rb_entry(parent, struct symbol, rb_node);
237 if (ip < s->start)
238 p = &(*p)->rb_left;
239 else
240 p = &(*p)->rb_right;
241 }
242 rb_link_node(&sym->rb_node, parent, p);
fcf1203a 243 rb_insert_color(&sym->rb_node, self);
a2928c42
ACM
244}
245
fcf1203a 246static struct symbol *symbols__find(struct rb_root *self, u64 ip)
a2928c42
ACM
247{
248 struct rb_node *n;
249
250 if (self == NULL)
251 return NULL;
252
fcf1203a 253 n = self->rb_node;
a2928c42
ACM
254
255 while (n) {
256 struct symbol *s = rb_entry(n, struct symbol, rb_node);
257
258 if (ip < s->start)
259 n = n->rb_left;
260 else if (ip > s->end)
261 n = n->rb_right;
262 else
263 return s;
264 }
265
266 return NULL;
267}
268
79406cd7
ACM
269struct symbol_name_rb_node {
270 struct rb_node rb_node;
271 struct symbol sym;
272};
273
274static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
275{
276 struct rb_node **p = &self->rb_node;
277 struct rb_node *parent = NULL;
278 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
279
280 while (*p != NULL) {
281 parent = *p;
282 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283 if (strcmp(sym->name, s->sym.name) < 0)
284 p = &(*p)->rb_left;
285 else
286 p = &(*p)->rb_right;
287 }
288 rb_link_node(&symn->rb_node, parent, p);
289 rb_insert_color(&symn->rb_node, self);
290}
291
292static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
293{
294 struct rb_node *nd;
295
296 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 symbols__insert_by_name(self, pos);
299 }
300}
301
302static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
303{
304 struct rb_node *n;
305
306 if (self == NULL)
307 return NULL;
308
309 n = self->rb_node;
310
311 while (n) {
312 struct symbol_name_rb_node *s;
313 int cmp;
314
315 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316 cmp = strcmp(name, s->sym.name);
317
318 if (cmp < 0)
319 n = n->rb_left;
320 else if (cmp > 0)
321 n = n->rb_right;
322 else
323 return &s->sym;
324 }
325
326 return NULL;
327}
328
329struct symbol *dso__find_symbol(struct dso *self,
330 enum map_type type, u64 addr)
fcf1203a 331{
6a4694a4 332 return symbols__find(&self->symbols[type], addr);
fcf1203a
ACM
333}
334
79406cd7
ACM
335struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
336 const char *name)
337{
338 return symbols__find_by_name(&self->symbol_names[type], name);
339}
340
341void dso__sort_by_name(struct dso *self, enum map_type type)
342{
343 dso__set_sorted_by_name(self, type);
344 return symbols__sort_by_name(&self->symbol_names[type],
345 &self->symbols[type]);
346}
347
8d06367f 348int build_id__sprintf(u8 *self, int len, char *bf)
a2928c42 349{
8d06367f
ACM
350 char *bid = bf;
351 u8 *raw = self;
352 int i;
a2928c42 353
8d06367f
ACM
354 for (i = 0; i < len; ++i) {
355 sprintf(bid, "%02x", *raw);
356 ++raw;
357 bid += 2;
358 }
359
360 return raw - self;
361}
362
9e03eb2d 363size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
8d06367f
ACM
364{
365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
8d06367f
ACM
366
367 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
9e03eb2d
ACM
368 return fprintf(fp, "%s", sbuild_id);
369}
370
95011c60 371size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
9e03eb2d
ACM
372{
373 struct rb_node *nd;
374 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
375
376 ret += dso__fprintf_buildid(self, fp);
6a4694a4 377 ret += fprintf(fp, ")\n");
95011c60
ACM
378 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
379 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
380 ret += symbol__fprintf(pos, fp);
a2928c42
ACM
381 }
382
383 return ret;
384}
385
682b335a
ACM
386int kallsyms__parse(void *arg, int (*process_symbol)(void *arg, const char *name,
387 char type, u64 start))
a2928c42 388{
a2928c42
ACM
389 char *line = NULL;
390 size_t n;
682b335a 391 int err = 0;
a2928c42
ACM
392 FILE *file = fopen("/proc/kallsyms", "r");
393
394 if (file == NULL)
395 goto out_failure;
396
397 while (!feof(file)) {
9cffa8d5 398 u64 start;
a2928c42
ACM
399 int line_len, len;
400 char symbol_type;
2e538c4a 401 char *symbol_name;
a2928c42
ACM
402
403 line_len = getline(&line, &n, file);
404 if (line_len < 0)
405 break;
406
407 if (!line)
408 goto out_failure;
409
410 line[--line_len] = '\0'; /* \n */
411
a0055ae2 412 len = hex2u64(line, &start);
a2928c42
ACM
413
414 len++;
415 if (len + 2 >= line_len)
416 continue;
417
418 symbol_type = toupper(line[len]);
af427bf5 419 symbol_name = line + len + 2;
682b335a
ACM
420
421 err = process_symbol(arg, symbol_name, symbol_type, start);
422 if (err)
423 break;
2e538c4a
ACM
424 }
425
426 free(line);
427 fclose(file);
682b335a 428 return err;
2e538c4a 429
2e538c4a
ACM
430out_failure:
431 return -1;
432}
433
682b335a
ACM
434struct process_kallsyms_args {
435 struct map *map;
436 struct dso *dso;
437};
438
439static int map__process_kallsym_symbol(void *arg, const char *name,
440 char type, u64 start)
441{
442 struct symbol *sym;
443 struct process_kallsyms_args *a = arg;
444 struct rb_root *root = &a->dso->symbols[a->map->type];
445
446 if (!symbol_type__is_a(type, a->map->type))
447 return 0;
448
449 /*
450 * Will fix up the end later, when we have all symbols sorted.
451 */
452 sym = symbol__new(start, 0, name);
453
454 if (sym == NULL)
455 return -ENOMEM;
456 /*
457 * We will pass the symbols to the filter later, in
458 * map__split_kallsyms, when we have split the maps per module
459 */
460 symbols__insert(root, sym);
461 return 0;
462}
463
464/*
465 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
466 * so that we can in the next step set the symbol ->end address and then
467 * call kernel_maps__split_kallsyms.
468 */
469static int dso__load_all_kallsyms(struct dso *self, struct map *map)
470{
471 struct process_kallsyms_args args = { .map = map, .dso = self, };
472 return kallsyms__parse(&args, map__process_kallsym_symbol);
473}
474
2e538c4a
ACM
475/*
476 * Split the symbols into maps, making sure there are no overlaps, i.e. the
477 * kernel range is broken in several maps, named [kernel].N, as we don't have
478 * the original ELF section names vmlinux have.
479 */
9958e1f0 480static int dso__split_kallsyms(struct dso *self, struct map *map,
4aa65636 481 struct perf_session *session, symbol_filter_t filter)
2e538c4a 482{
4e06255f 483 struct map *curr_map = map;
2e538c4a
ACM
484 struct symbol *pos;
485 int count = 0;
4e06255f
ACM
486 struct rb_root *root = &self->symbols[map->type];
487 struct rb_node *next = rb_first(root);
2e538c4a
ACM
488 int kernel_range = 0;
489
490 while (next) {
491 char *module;
492
493 pos = rb_entry(next, struct symbol, rb_node);
494 next = rb_next(&pos->rb_node);
495
496 module = strchr(pos->name, '\t');
497 if (module) {
75be6cf4 498 if (!symbol_conf.use_modules)
1de8e245
ACM
499 goto discard_symbol;
500
2e538c4a
ACM
501 *module++ = '\0';
502
4e06255f 503 if (strcmp(self->name, module)) {
4aa65636 504 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
4e06255f 505 if (curr_map == NULL) {
95011c60
ACM
506 pr_debug("/proc/{kallsyms,modules} "
507 "inconsistency!\n");
af427bf5
ACM
508 return -1;
509 }
510 }
2e538c4a
ACM
511 /*
512 * So that we look just like we get from .ko files,
513 * i.e. not prelinked, relative to map->start.
514 */
4e06255f
ACM
515 pos->start = curr_map->map_ip(curr_map, pos->start);
516 pos->end = curr_map->map_ip(curr_map, pos->end);
517 } else if (curr_map != map) {
2e538c4a
ACM
518 char dso_name[PATH_MAX];
519 struct dso *dso;
520
521 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
522 kernel_range++);
523
00a192b3 524 dso = dso__new(dso_name);
2e538c4a
ACM
525 if (dso == NULL)
526 return -1;
527
4e06255f 528 curr_map = map__new2(pos->start, dso, map->type);
2e538c4a
ACM
529 if (map == NULL) {
530 dso__delete(dso);
531 return -1;
532 }
a2928c42 533
4e06255f 534 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
4aa65636 535 map_groups__insert(&session->kmaps, curr_map);
2e538c4a
ACM
536 ++kernel_range;
537 }
a2928c42 538
4e06255f 539 if (filter && filter(curr_map, pos)) {
1de8e245 540discard_symbol: rb_erase(&pos->rb_node, root);
00a192b3 541 symbol__delete(pos);
2e538c4a 542 } else {
4e06255f
ACM
543 if (curr_map != map) {
544 rb_erase(&pos->rb_node, root);
545 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
2e538c4a 546 }
9974f496
MG
547 count++;
548 }
a2928c42
ACM
549 }
550
9974f496 551 return count;
2e538c4a 552}
a2928c42 553
2e538c4a 554
4e06255f 555static int dso__load_kallsyms(struct dso *self, struct map *map,
4aa65636 556 struct perf_session *session, symbol_filter_t filter)
2e538c4a 557{
4e06255f 558 if (dso__load_all_kallsyms(self, map) < 0)
2e538c4a
ACM
559 return -1;
560
4e06255f
ACM
561 symbols__fixup_end(&self->symbols[map->type]);
562 self->origin = DSO__ORIG_KERNEL;
2e538c4a 563
4aa65636 564 return dso__split_kallsyms(self, map, session, filter);
af427bf5
ACM
565}
566
439d473b 567static int dso__load_perf_map(struct dso *self, struct map *map,
6beba7ad 568 symbol_filter_t filter)
80d496be
PE
569{
570 char *line = NULL;
571 size_t n;
572 FILE *file;
573 int nr_syms = 0;
574
439d473b 575 file = fopen(self->long_name, "r");
80d496be
PE
576 if (file == NULL)
577 goto out_failure;
578
579 while (!feof(file)) {
9cffa8d5 580 u64 start, size;
80d496be
PE
581 struct symbol *sym;
582 int line_len, len;
583
584 line_len = getline(&line, &n, file);
585 if (line_len < 0)
586 break;
587
588 if (!line)
589 goto out_failure;
590
591 line[--line_len] = '\0'; /* \n */
592
593 len = hex2u64(line, &start);
594
595 len++;
596 if (len + 2 >= line_len)
597 continue;
598
599 len += hex2u64(line + len, &size);
600
601 len++;
602 if (len + 2 >= line_len)
603 continue;
604
00a192b3 605 sym = symbol__new(start, size, line + len);
80d496be
PE
606
607 if (sym == NULL)
608 goto out_delete_line;
609
439d473b 610 if (filter && filter(map, sym))
00a192b3 611 symbol__delete(sym);
80d496be 612 else {
6a4694a4 613 symbols__insert(&self->symbols[map->type], sym);
80d496be
PE
614 nr_syms++;
615 }
616 }
617
618 free(line);
619 fclose(file);
620
621 return nr_syms;
622
623out_delete_line:
624 free(line);
625out_failure:
626 return -1;
627}
628
a2928c42
ACM
629/**
630 * elf_symtab__for_each_symbol - iterate thru all the symbols
631 *
632 * @self: struct elf_symtab instance to iterate
83a0944f 633 * @idx: uint32_t idx
a2928c42
ACM
634 * @sym: GElf_Sym iterator
635 */
83a0944f
IM
636#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
637 for (idx = 0, gelf_getsym(syms, idx, &sym);\
638 idx < nr_syms; \
639 idx++, gelf_getsym(syms, idx, &sym))
a2928c42
ACM
640
641static inline uint8_t elf_sym__type(const GElf_Sym *sym)
642{
643 return GELF_ST_TYPE(sym->st_info);
644}
645
646static inline int elf_sym__is_function(const GElf_Sym *sym)
647{
648 return elf_sym__type(sym) == STT_FUNC &&
649 sym->st_name != 0 &&
81833130 650 sym->st_shndx != SHN_UNDEF;
a2928c42
ACM
651}
652
f1dfa0b1
ACM
653static inline bool elf_sym__is_object(const GElf_Sym *sym)
654{
655 return elf_sym__type(sym) == STT_OBJECT &&
656 sym->st_name != 0 &&
657 sym->st_shndx != SHN_UNDEF;
658}
659
6cfcc53e
MG
660static inline int elf_sym__is_label(const GElf_Sym *sym)
661{
662 return elf_sym__type(sym) == STT_NOTYPE &&
663 sym->st_name != 0 &&
664 sym->st_shndx != SHN_UNDEF &&
665 sym->st_shndx != SHN_ABS;
666}
667
668static inline const char *elf_sec__name(const GElf_Shdr *shdr,
669 const Elf_Data *secstrs)
670{
671 return secstrs->d_buf + shdr->sh_name;
672}
673
674static inline int elf_sec__is_text(const GElf_Shdr *shdr,
675 const Elf_Data *secstrs)
676{
677 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
678}
679
f1dfa0b1
ACM
680static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
681 const Elf_Data *secstrs)
682{
683 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
684}
685
a2928c42
ACM
686static inline const char *elf_sym__name(const GElf_Sym *sym,
687 const Elf_Data *symstrs)
688{
689 return symstrs->d_buf + sym->st_name;
690}
691
692static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
693 GElf_Shdr *shp, const char *name,
83a0944f 694 size_t *idx)
a2928c42
ACM
695{
696 Elf_Scn *sec = NULL;
697 size_t cnt = 1;
698
699 while ((sec = elf_nextscn(elf, sec)) != NULL) {
700 char *str;
701
702 gelf_getshdr(sec, shp);
703 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
704 if (!strcmp(name, str)) {
83a0944f
IM
705 if (idx)
706 *idx = cnt;
a2928c42
ACM
707 break;
708 }
709 ++cnt;
710 }
711
712 return sec;
713}
714
8ce998d6
ACM
715#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
716 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
717 idx < nr_entries; \
718 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
719
720#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
721 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
722 idx < nr_entries; \
723 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
724
a25e46c4
ACM
725/*
726 * We need to check if we have a .dynsym, so that we can handle the
727 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
728 * .dynsym or .symtab).
729 * And always look at the original dso, not at debuginfo packages, that
730 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
731 */
82164161
ACM
732static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
733 symbol_filter_t filter)
8ce998d6
ACM
734{
735 uint32_t nr_rel_entries, idx;
736 GElf_Sym sym;
9cffa8d5 737 u64 plt_offset;
8ce998d6
ACM
738 GElf_Shdr shdr_plt;
739 struct symbol *f;
a25e46c4 740 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 741 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
742 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
743 size_t dynsym_idx;
744 GElf_Ehdr ehdr;
8ce998d6 745 char sympltname[1024];
a25e46c4
ACM
746 Elf *elf;
747 int nr = 0, symidx, fd, err = 0;
748
439d473b 749 fd = open(self->long_name, O_RDONLY);
a25e46c4
ACM
750 if (fd < 0)
751 goto out;
752
84087126 753 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a25e46c4
ACM
754 if (elf == NULL)
755 goto out_close;
756
757 if (gelf_getehdr(elf, &ehdr) == NULL)
758 goto out_elf_end;
759
760 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
761 ".dynsym", &dynsym_idx);
762 if (scn_dynsym == NULL)
763 goto out_elf_end;
8ce998d6 764
a25e46c4 765 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
766 ".rela.plt", NULL);
767 if (scn_plt_rel == NULL) {
a25e46c4 768 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
769 ".rel.plt", NULL);
770 if (scn_plt_rel == NULL)
a25e46c4 771 goto out_elf_end;
8ce998d6
ACM
772 }
773
a25e46c4
ACM
774 err = -1;
775
8ce998d6 776 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 777 goto out_elf_end;
8ce998d6 778
a25e46c4
ACM
779 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
780 goto out_elf_end;
8ce998d6
ACM
781
782 /*
83a0944f 783 * Fetch the relocation section to find the idxes to the GOT
8ce998d6
ACM
784 * and the symbols in the .dynsym they refer to.
785 */
786 reldata = elf_getdata(scn_plt_rel, NULL);
787 if (reldata == NULL)
a25e46c4 788 goto out_elf_end;
8ce998d6
ACM
789
790 syms = elf_getdata(scn_dynsym, NULL);
791 if (syms == NULL)
a25e46c4 792 goto out_elf_end;
8ce998d6 793
a25e46c4 794 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 795 if (scn_symstrs == NULL)
a25e46c4 796 goto out_elf_end;
8ce998d6
ACM
797
798 symstrs = elf_getdata(scn_symstrs, NULL);
799 if (symstrs == NULL)
a25e46c4 800 goto out_elf_end;
8ce998d6
ACM
801
802 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
803 plt_offset = shdr_plt.sh_offset;
804
805 if (shdr_rel_plt.sh_type == SHT_RELA) {
806 GElf_Rela pos_mem, *pos;
807
808 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
809 nr_rel_entries) {
810 symidx = GELF_R_SYM(pos->r_info);
811 plt_offset += shdr_plt.sh_entsize;
812 gelf_getsym(syms, symidx, &sym);
813 snprintf(sympltname, sizeof(sympltname),
814 "%s@plt", elf_sym__name(&sym, symstrs));
815
816 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
00a192b3 817 sympltname);
8ce998d6 818 if (!f)
a25e46c4 819 goto out_elf_end;
8ce998d6 820
82164161
ACM
821 if (filter && filter(map, f))
822 symbol__delete(f);
823 else {
6a4694a4 824 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
825 ++nr;
826 }
8ce998d6
ACM
827 }
828 } else if (shdr_rel_plt.sh_type == SHT_REL) {
829 GElf_Rel pos_mem, *pos;
830 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
831 nr_rel_entries) {
832 symidx = GELF_R_SYM(pos->r_info);
833 plt_offset += shdr_plt.sh_entsize;
834 gelf_getsym(syms, symidx, &sym);
835 snprintf(sympltname, sizeof(sympltname),
836 "%s@plt", elf_sym__name(&sym, symstrs));
837
838 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
00a192b3 839 sympltname);
8ce998d6 840 if (!f)
a25e46c4 841 goto out_elf_end;
8ce998d6 842
82164161
ACM
843 if (filter && filter(map, f))
844 symbol__delete(f);
845 else {
6a4694a4 846 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
847 ++nr;
848 }
8ce998d6 849 }
8ce998d6
ACM
850 }
851
a25e46c4
ACM
852 err = 0;
853out_elf_end:
854 elf_end(elf);
855out_close:
856 close(fd);
857
858 if (err == 0)
859 return nr;
860out:
6beba7ad
ACM
861 pr_warning("%s: problems reading %s PLT info.\n",
862 __func__, self->long_name);
a25e46c4 863 return 0;
8ce998d6
ACM
864}
865
d45868d3
ACM
866static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
867{
868 switch (type) {
869 case MAP__FUNCTION:
870 return elf_sym__is_function(self);
f1dfa0b1
ACM
871 case MAP__VARIABLE:
872 return elf_sym__is_object(self);
d45868d3
ACM
873 default:
874 return false;
875 }
876}
877
878static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
879{
880 switch (type) {
881 case MAP__FUNCTION:
882 return elf_sec__is_text(self, secstrs);
f1dfa0b1
ACM
883 case MAP__VARIABLE:
884 return elf_sec__is_data(self, secstrs);
d45868d3
ACM
885 default:
886 return false;
887 }
888}
889
95011c60 890static int dso__load_sym(struct dso *self, struct map *map,
4aa65636 891 struct perf_session *session, const char *name, int fd,
95011c60 892 symbol_filter_t filter, int kernel, int kmodule)
a2928c42 893{
2e538c4a
ACM
894 struct map *curr_map = map;
895 struct dso *curr_dso = self;
896 size_t dso_name_len = strlen(self->short_name);
6cfcc53e 897 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
898 uint32_t nr_syms;
899 int err = -1;
83a0944f 900 uint32_t idx;
a2928c42
ACM
901 GElf_Ehdr ehdr;
902 GElf_Shdr shdr;
903 Elf_Data *syms;
904 GElf_Sym sym;
a25e46c4 905 Elf_Scn *sec, *sec_strndx;
a2928c42 906 Elf *elf;
439d473b 907 int nr = 0;
a2928c42 908
84087126 909 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a2928c42 910 if (elf == NULL) {
6beba7ad 911 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
a2928c42
ACM
912 goto out_close;
913 }
914
915 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 916 pr_err("%s: cannot get elf header.\n", __func__);
a2928c42
ACM
917 goto out_elf_end;
918 }
919
920 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 921 if (sec == NULL) {
a25e46c4
ACM
922 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
923 if (sec == NULL)
8ce998d6 924 goto out_elf_end;
8ce998d6 925 }
a2928c42
ACM
926
927 syms = elf_getdata(sec, NULL);
928 if (syms == NULL)
929 goto out_elf_end;
930
931 sec = elf_getscn(elf, shdr.sh_link);
932 if (sec == NULL)
933 goto out_elf_end;
934
935 symstrs = elf_getdata(sec, NULL);
936 if (symstrs == NULL)
937 goto out_elf_end;
938
6cfcc53e
MG
939 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
940 if (sec_strndx == NULL)
941 goto out_elf_end;
942
943 secstrs = elf_getdata(sec_strndx, NULL);
9b30a26b 944 if (secstrs == NULL)
6cfcc53e
MG
945 goto out_elf_end;
946
a2928c42
ACM
947 nr_syms = shdr.sh_size / shdr.sh_entsize;
948
e9fbc9dc 949 memset(&sym, 0, sizeof(sym));
d20ff6bd
MG
950 if (!kernel) {
951 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
952 elf_section_by_name(elf, &ehdr, &shdr,
953 ".gnu.prelink_undo",
954 NULL) != NULL);
d20ff6bd
MG
955 } else self->adjust_symbols = 0;
956
83a0944f 957 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
a2928c42 958 struct symbol *f;
83a0944f 959 const char *elf_name;
2e538c4a 960 char *demangled = NULL;
6cfcc53e
MG
961 int is_label = elf_sym__is_label(&sym);
962 const char *section_name;
a2928c42 963
d45868d3 964 if (!is_label && !elf_sym__is_a(&sym, map->type))
a2928c42
ACM
965 continue;
966
967 sec = elf_getscn(elf, sym.st_shndx);
968 if (!sec)
969 goto out_elf_end;
970
971 gelf_getshdr(sec, &shdr);
6cfcc53e 972
d45868d3 973 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
6cfcc53e
MG
974 continue;
975
2e538c4a 976 elf_name = elf_sym__name(&sym, symstrs);
6cfcc53e 977 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f 978
2e538c4a
ACM
979 if (kernel || kmodule) {
980 char dso_name[PATH_MAX];
981
982 if (strcmp(section_name,
983 curr_dso->short_name + dso_name_len) == 0)
984 goto new_symbol;
985
986 if (strcmp(section_name, ".text") == 0) {
987 curr_map = map;
988 curr_dso = self;
989 goto new_symbol;
990 }
991
992 snprintf(dso_name, sizeof(dso_name),
993 "%s%s", self->short_name, section_name);
994
4aa65636 995 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
2e538c4a
ACM
996 if (curr_map == NULL) {
997 u64 start = sym.st_value;
998
999 if (kmodule)
1000 start += map->start + shdr.sh_offset;
1001
00a192b3 1002 curr_dso = dso__new(dso_name);
2e538c4a
ACM
1003 if (curr_dso == NULL)
1004 goto out_elf_end;
3610583c
ACM
1005 curr_map = map__new2(start, curr_dso,
1006 MAP__FUNCTION);
2e538c4a
ACM
1007 if (curr_map == NULL) {
1008 dso__delete(curr_dso);
1009 goto out_elf_end;
1010 }
ed52ce2e
ACM
1011 curr_map->map_ip = identity__map_ip;
1012 curr_map->unmap_ip = identity__map_ip;
2e538c4a 1013 curr_dso->origin = DSO__ORIG_KERNEL;
4aa65636 1014 map_groups__insert(&session->kmaps, curr_map);
b0da954a 1015 dsos__add(&dsos__kernel, curr_dso);
2e538c4a
ACM
1016 } else
1017 curr_dso = curr_map->dso;
1018
1019 goto new_symbol;
af427bf5
ACM
1020 }
1021
2e538c4a 1022 if (curr_dso->adjust_symbols) {
6beba7ad
ACM
1023 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1024 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1025 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
f5812a7a 1026 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 1027 }
28ac909b
ACM
1028 /*
1029 * We need to figure out if the object was created from C++ sources
1030 * DWARF DW_compile_unit has this, but we don't always have access
1031 * to it...
1032 */
83a0944f 1033 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 1034 if (demangled != NULL)
83a0944f 1035 elf_name = demangled;
2e538c4a 1036new_symbol:
00a192b3 1037 f = symbol__new(sym.st_value, sym.st_size, elf_name);
28ac909b 1038 free(demangled);
a2928c42
ACM
1039 if (!f)
1040 goto out_elf_end;
1041
2e538c4a 1042 if (filter && filter(curr_map, f))
00a192b3 1043 symbol__delete(f);
69ee69f6 1044 else {
6a4694a4 1045 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
1046 nr++;
1047 }
a2928c42
ACM
1048 }
1049
2e538c4a
ACM
1050 /*
1051 * For misannotated, zeroed, ASM function sizes.
1052 */
1053 if (nr > 0)
6a4694a4 1054 symbols__fixup_end(&self->symbols[map->type]);
a2928c42
ACM
1055 err = nr;
1056out_elf_end:
1057 elf_end(elf);
1058out_close:
1059 return err;
1060}
1061
78075caa
ACM
1062static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1063{
1064 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1065}
1066
b0da954a 1067static bool __dsos__read_build_ids(struct list_head *head)
57f395a7 1068{
e30a3d12 1069 bool have_build_id = false;
57f395a7
FW
1070 struct dso *pos;
1071
b0da954a 1072 list_for_each_entry(pos, head, node)
e30a3d12
ACM
1073 if (filename__read_build_id(pos->long_name, pos->build_id,
1074 sizeof(pos->build_id)) > 0) {
1075 have_build_id = true;
1076 pos->has_build_id = true;
1077 }
57f395a7 1078
e30a3d12 1079 return have_build_id;
57f395a7
FW
1080}
1081
b0da954a
ACM
1082bool dsos__read_build_ids(void)
1083{
8b4825bf
ACM
1084 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1085 ubuildids = __dsos__read_build_ids(&dsos__user);
1086 return kbuildids || ubuildids;
b0da954a
ACM
1087}
1088
fd7a346e
ACM
1089/*
1090 * Align offset to 4 bytes as needed for note name and descriptor data.
1091 */
1092#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1093
2643ce11 1094int filename__read_build_id(const char *filename, void *bf, size_t size)
4d1e00a8 1095{
2643ce11 1096 int fd, err = -1;
4d1e00a8
ACM
1097 GElf_Ehdr ehdr;
1098 GElf_Shdr shdr;
fd7a346e 1099 Elf_Data *data;
4d1e00a8 1100 Elf_Scn *sec;
e57cfcda 1101 Elf_Kind ek;
fd7a346e 1102 void *ptr;
4d1e00a8 1103 Elf *elf;
4d1e00a8 1104
2643ce11
ACM
1105 if (size < BUILD_ID_SIZE)
1106 goto out;
1107
1108 fd = open(filename, O_RDONLY);
4d1e00a8
ACM
1109 if (fd < 0)
1110 goto out;
1111
84087126 1112 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
4d1e00a8 1113 if (elf == NULL) {
8d06367f 1114 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
4d1e00a8
ACM
1115 goto out_close;
1116 }
1117
e57cfcda
PE
1118 ek = elf_kind(elf);
1119 if (ek != ELF_K_ELF)
1120 goto out_elf_end;
1121
4d1e00a8 1122 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 1123 pr_err("%s: cannot get elf header.\n", __func__);
4d1e00a8
ACM
1124 goto out_elf_end;
1125 }
1126
2643ce11
ACM
1127 sec = elf_section_by_name(elf, &ehdr, &shdr,
1128 ".note.gnu.build-id", NULL);
fd7a346e
ACM
1129 if (sec == NULL) {
1130 sec = elf_section_by_name(elf, &ehdr, &shdr,
1131 ".notes", NULL);
1132 if (sec == NULL)
1133 goto out_elf_end;
1134 }
4d1e00a8 1135
fd7a346e
ACM
1136 data = elf_getdata(sec, NULL);
1137 if (data == NULL)
4d1e00a8 1138 goto out_elf_end;
fd7a346e
ACM
1139
1140 ptr = data->d_buf;
1141 while (ptr < (data->d_buf + data->d_size)) {
1142 GElf_Nhdr *nhdr = ptr;
1143 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1144 descsz = NOTE_ALIGN(nhdr->n_descsz);
1145 const char *name;
1146
1147 ptr += sizeof(*nhdr);
1148 name = ptr;
1149 ptr += namesz;
1150 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1151 nhdr->n_namesz == sizeof("GNU")) {
1152 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1153 memcpy(bf, ptr, BUILD_ID_SIZE);
1154 err = BUILD_ID_SIZE;
1155 break;
1156 }
1157 }
1158 ptr += descsz;
1159 }
2643ce11
ACM
1160out_elf_end:
1161 elf_end(elf);
1162out_close:
1163 close(fd);
1164out:
1165 return err;
1166}
1167
f1617b40
ACM
1168int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1169{
1170 int fd, err = -1;
1171
1172 if (size < BUILD_ID_SIZE)
1173 goto out;
1174
1175 fd = open(filename, O_RDONLY);
1176 if (fd < 0)
1177 goto out;
1178
1179 while (1) {
1180 char bf[BUFSIZ];
1181 GElf_Nhdr nhdr;
1182 int namesz, descsz;
1183
1184 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1185 break;
1186
fd7a346e
ACM
1187 namesz = NOTE_ALIGN(nhdr.n_namesz);
1188 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1189 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1190 nhdr.n_namesz == sizeof("GNU")) {
1191 if (read(fd, bf, namesz) != namesz)
1192 break;
1193 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1194 if (read(fd, build_id,
1195 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1196 err = 0;
1197 break;
1198 }
1199 } else if (read(fd, bf, descsz) != descsz)
1200 break;
1201 } else {
1202 int n = namesz + descsz;
1203 if (read(fd, bf, n) != n)
1204 break;
1205 }
1206 }
1207 close(fd);
1208out:
1209 return err;
1210}
1211
94cb9e38
ACM
1212char dso__symtab_origin(const struct dso *self)
1213{
1214 static const char origin[] = {
1215 [DSO__ORIG_KERNEL] = 'k',
1216 [DSO__ORIG_JAVA_JIT] = 'j',
4cf40131 1217 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
94cb9e38
ACM
1218 [DSO__ORIG_FEDORA] = 'f',
1219 [DSO__ORIG_UBUNTU] = 'u',
1220 [DSO__ORIG_BUILDID] = 'b',
1221 [DSO__ORIG_DSO] = 'd',
439d473b 1222 [DSO__ORIG_KMODULE] = 'K',
94cb9e38
ACM
1223 };
1224
1225 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1226 return '!';
1227 return origin[self->origin];
1228}
1229
4aa65636
ACM
1230int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1231 symbol_filter_t filter)
a2928c42 1232{
4d1e00a8 1233 int size = PATH_MAX;
c338aee8 1234 char *name;
d3379ab9 1235 u8 build_id[BUILD_ID_SIZE];
4cf40131 1236 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
a2928c42
ACM
1237 int ret = -1;
1238 int fd;
1239
3610583c 1240 dso__set_loaded(self, map->type);
66bd8424 1241
c338aee8 1242 if (self->kernel)
4aa65636 1243 return dso__load_kernel_sym(self, map, session, filter);
c338aee8
ACM
1244
1245 name = malloc(size);
a2928c42
ACM
1246 if (!name)
1247 return -1;
1248
30d7a77d 1249 self->adjust_symbols = 0;
f5812a7a 1250
94cb9e38 1251 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
6beba7ad 1252 ret = dso__load_perf_map(self, map, filter);
94cb9e38
ACM
1253 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1254 DSO__ORIG_NOT_FOUND;
1255 return ret;
1256 }
1257
4cf40131 1258 self->origin = DSO__ORIG_BUILD_ID_CACHE;
80d496be 1259
4cf40131
ACM
1260 if (self->has_build_id) {
1261 build_id__sprintf(self->build_id, sizeof(self->build_id),
1262 build_id_hex);
1263 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1264 getenv("HOME"), DEBUG_CACHE_DIR,
1265 build_id_hex, build_id_hex + 2);
1266 goto open_file;
1267 }
a2928c42
ACM
1268more:
1269 do {
94cb9e38
ACM
1270 self->origin++;
1271 switch (self->origin) {
1272 case DSO__ORIG_FEDORA:
439d473b
ACM
1273 snprintf(name, size, "/usr/lib/debug%s.debug",
1274 self->long_name);
a2928c42 1275 break;
94cb9e38 1276 case DSO__ORIG_UBUNTU:
439d473b
ACM
1277 snprintf(name, size, "/usr/lib/debug%s",
1278 self->long_name);
a2928c42 1279 break;
94cb9e38 1280 case DSO__ORIG_BUILDID:
d3379ab9
ACM
1281 if (filename__read_build_id(self->long_name, build_id,
1282 sizeof(build_id))) {
d3379ab9
ACM
1283 build_id__sprintf(build_id, sizeof(build_id),
1284 build_id_hex);
4d1e00a8
ACM
1285 snprintf(name, size,
1286 "/usr/lib/debug/.build-id/%.2s/%s.debug",
d3379ab9
ACM
1287 build_id_hex, build_id_hex + 2);
1288 if (self->has_build_id)
1289 goto compare_build_id;
1290 break;
4d1e00a8 1291 }
94cb9e38 1292 self->origin++;
4d1e00a8 1293 /* Fall thru */
94cb9e38 1294 case DSO__ORIG_DSO:
439d473b 1295 snprintf(name, size, "%s", self->long_name);
a2928c42
ACM
1296 break;
1297
1298 default:
1299 goto out;
1300 }
a2928c42 1301
8d06367f 1302 if (self->has_build_id) {
d3379ab9
ACM
1303 if (filename__read_build_id(name, build_id,
1304 sizeof(build_id)) < 0)
8d06367f 1305 goto more;
8d06367f 1306compare_build_id:
78075caa 1307 if (!dso__build_id_equal(self, build_id))
8d06367f
ACM
1308 goto more;
1309 }
4cf40131 1310open_file:
a2928c42
ACM
1311 fd = open(name, O_RDONLY);
1312 } while (fd < 0);
1313
95011c60 1314 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
a2928c42
ACM
1315 close(fd);
1316
1317 /*
1318 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1319 */
1320 if (!ret)
1321 goto more;
1322
a25e46c4 1323 if (ret > 0) {
82164161 1324 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
a25e46c4
ACM
1325 if (nr_plt > 0)
1326 ret += nr_plt;
1327 }
a2928c42
ACM
1328out:
1329 free(name);
1340e6bb
ACM
1330 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1331 return 0;
a2928c42
ACM
1332 return ret;
1333}
1334
79406cd7
ACM
1335struct map *map_groups__find_by_name(struct map_groups *self,
1336 enum map_type type, const char *name)
439d473b
ACM
1337{
1338 struct rb_node *nd;
1339
79406cd7 1340 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1341 struct map *map = rb_entry(nd, struct map, rb_node);
1342
1343 if (map->dso && strcmp(map->dso->name, name) == 0)
1344 return map;
1345 }
1346
1347 return NULL;
1348}
1349
4aa65636 1350static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
6cfcc53e 1351{
439d473b 1352 struct dirent *dent;
439d473b 1353 DIR *dir = opendir(dirname);
6cfcc53e 1354
439d473b 1355 if (!dir) {
87f8ea4c 1356 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
439d473b
ACM
1357 return -1;
1358 }
6cfcc53e 1359
439d473b
ACM
1360 while ((dent = readdir(dir)) != NULL) {
1361 char path[PATH_MAX];
1362
1363 if (dent->d_type == DT_DIR) {
1364 if (!strcmp(dent->d_name, ".") ||
1365 !strcmp(dent->d_name, ".."))
1366 continue;
1367
1368 snprintf(path, sizeof(path), "%s/%s",
1369 dirname, dent->d_name);
4aa65636 1370 if (perf_session__set_modules_path_dir(self, path) < 0)
439d473b
ACM
1371 goto failure;
1372 } else {
1373 char *dot = strrchr(dent->d_name, '.'),
1374 dso_name[PATH_MAX];
1375 struct map *map;
cfc10d3b 1376 char *long_name;
439d473b
ACM
1377
1378 if (dot == NULL || strcmp(dot, ".ko"))
1379 continue;
1380 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1381 (int)(dot - dent->d_name), dent->d_name);
1382
a2a99e8e 1383 strxfrchar(dso_name, '-', '_');
4aa65636 1384 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
439d473b
ACM
1385 if (map == NULL)
1386 continue;
1387
1388 snprintf(path, sizeof(path), "%s/%s",
1389 dirname, dent->d_name);
1390
cfc10d3b
ACM
1391 long_name = strdup(path);
1392 if (long_name == NULL)
439d473b 1393 goto failure;
cfc10d3b 1394 dso__set_long_name(map->dso, long_name);
439d473b 1395 }
439d473b 1396 }
6cfcc53e 1397
c338aee8 1398 return 0;
439d473b
ACM
1399failure:
1400 closedir(dir);
1401 return -1;
1402}
6cfcc53e 1403
4aa65636 1404static int perf_session__set_modules_path(struct perf_session *self)
439d473b
ACM
1405{
1406 struct utsname uts;
1407 char modules_path[PATH_MAX];
6cfcc53e 1408
439d473b
ACM
1409 if (uname(&uts) < 0)
1410 return -1;
6cfcc53e 1411
439d473b
ACM
1412 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1413 uts.release);
6cfcc53e 1414
4aa65636 1415 return perf_session__set_modules_path_dir(self, modules_path);
6cfcc53e
MG
1416}
1417
439d473b
ACM
1418/*
1419 * Constructor variant for modules (where we know from /proc/modules where
1420 * they are loaded) and for vmlinux, where only after we load all the
1421 * symbols we'll know where it starts and ends.
1422 */
3610583c 1423static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 1424{
439d473b 1425 struct map *self = malloc(sizeof(*self));
6cfcc53e 1426
439d473b 1427 if (self != NULL) {
439d473b 1428 /*
afb7b4f0 1429 * ->end will be filled after we load all the symbols
439d473b 1430 */
3610583c 1431 map__init(self, type, start, 0, 0, dso);
439d473b 1432 }
afb7b4f0 1433
439d473b
ACM
1434 return self;
1435}
1436
4aa65636 1437static int perf_session__create_module_maps(struct perf_session *self)
439d473b
ACM
1438{
1439 char *line = NULL;
1440 size_t n;
1441 FILE *file = fopen("/proc/modules", "r");
1442 struct map *map;
6cfcc53e 1443
439d473b
ACM
1444 if (file == NULL)
1445 return -1;
6cfcc53e 1446
439d473b
ACM
1447 while (!feof(file)) {
1448 char name[PATH_MAX];
1449 u64 start;
1450 struct dso *dso;
1451 char *sep;
1452 int line_len;
6cfcc53e 1453
439d473b
ACM
1454 line_len = getline(&line, &n, file);
1455 if (line_len < 0)
1456 break;
1457
1458 if (!line)
1459 goto out_failure;
1460
1461 line[--line_len] = '\0'; /* \n */
1462
1463 sep = strrchr(line, 'x');
1464 if (sep == NULL)
1465 continue;
1466
1467 hex2u64(sep + 1, &start);
1468
1469 sep = strchr(line, ' ');
1470 if (sep == NULL)
1471 continue;
1472
1473 *sep = '\0';
1474
1475 snprintf(name, sizeof(name), "[%s]", line);
00a192b3 1476 dso = dso__new(name);
439d473b
ACM
1477
1478 if (dso == NULL)
1479 goto out_delete_line;
1480
3610583c 1481 map = map__new2(start, dso, MAP__FUNCTION);
439d473b
ACM
1482 if (map == NULL) {
1483 dso__delete(dso);
1484 goto out_delete_line;
6cfcc53e 1485 }
439d473b 1486
f1617b40
ACM
1487 snprintf(name, sizeof(name),
1488 "/sys/module/%s/notes/.note.gnu.build-id", line);
1489 if (sysfs__read_build_id(name, dso->build_id,
1490 sizeof(dso->build_id)) == 0)
1491 dso->has_build_id = true;
1492
439d473b 1493 dso->origin = DSO__ORIG_KMODULE;
4aa65636 1494 map_groups__insert(&self->kmaps, map);
b0da954a 1495 dsos__add(&dsos__kernel, dso);
6cfcc53e 1496 }
439d473b
ACM
1497
1498 free(line);
1499 fclose(file);
1500
4aa65636 1501 return perf_session__set_modules_path(self);
439d473b
ACM
1502
1503out_delete_line:
1504 free(line);
1505out_failure:
1506 return -1;
6cfcc53e
MG
1507}
1508
9958e1f0 1509static int dso__load_vmlinux(struct dso *self, struct map *map,
4aa65636 1510 struct perf_session *session,
6beba7ad 1511 const char *vmlinux, symbol_filter_t filter)
a2928c42 1512{
fbd733b8 1513 int err = -1, fd;
a2928c42 1514
fbd733b8
ACM
1515 if (self->has_build_id) {
1516 u8 build_id[BUILD_ID_SIZE];
1517
1518 if (filename__read_build_id(vmlinux, build_id,
1519 sizeof(build_id)) < 0) {
1520 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1521 return -1;
1522 }
1523 if (!dso__build_id_equal(self, build_id)) {
1524 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1525 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1526
1527 build_id__sprintf(self->build_id,
1528 sizeof(self->build_id),
1529 expected_build_id);
1530 build_id__sprintf(build_id, sizeof(build_id),
1531 vmlinux_build_id);
1532 pr_debug("build_id in %s is %s while expected is %s, "
1533 "ignoring it\n", vmlinux, vmlinux_build_id,
1534 expected_build_id);
1535 return -1;
1536 }
1537 }
66bd8424 1538
fbd733b8 1539 fd = open(vmlinux, O_RDONLY);
a2928c42
ACM
1540 if (fd < 0)
1541 return -1;
1542
3610583c 1543 dso__set_loaded(self, map->type);
4aa65636 1544 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
a2928c42
ACM
1545 close(fd);
1546
1547 return err;
1548}
1549
c338aee8 1550static int dso__load_kernel_sym(struct dso *self, struct map *map,
4aa65636 1551 struct perf_session *session, symbol_filter_t filter)
a827c875 1552{
cc612d81
ACM
1553 int err;
1554 bool is_kallsyms;
1555
1556 if (vmlinux_path != NULL) {
1557 int i;
1558 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1559 vmlinux_path__nr_entries);
1560 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
4aa65636 1561 err = dso__load_vmlinux(self, map, session,
95011c60 1562 vmlinux_path[i], filter);
cc612d81
ACM
1563 if (err > 0) {
1564 pr_debug("Using %s for symbols\n",
1565 vmlinux_path[i]);
1566 dso__set_long_name(self,
1567 strdup(vmlinux_path[i]));
1568 goto out_fixup;
1569 }
1570 }
1571 }
1572
1573 is_kallsyms = self->long_name[0] == '[';
1574 if (is_kallsyms)
1575 goto do_kallsyms;
439d473b 1576
4aa65636 1577 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
ef6ae724 1578 if (err <= 0) {
cc612d81
ACM
1579 pr_info("The file %s cannot be used, "
1580 "trying to use /proc/kallsyms...", self->long_name);
cc612d81 1581do_kallsyms:
4aa65636 1582 err = dso__load_kallsyms(self, map, session, filter);
cc612d81 1583 if (err > 0 && !is_kallsyms)
ef6ae724
ACM
1584 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1585 }
439d473b
ACM
1586
1587 if (err > 0) {
cc612d81 1588out_fixup:
6a4694a4
ACM
1589 map__fixup_start(map);
1590 map__fixup_end(map);
439d473b 1591 }
94cb9e38 1592
a827c875
ACM
1593 return err;
1594}
1595
b0da954a
ACM
1596LIST_HEAD(dsos__user);
1597LIST_HEAD(dsos__kernel);
cc612d81 1598struct dso *vdso;
cd84c2ac 1599
b0da954a 1600static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 1601{
b0da954a 1602 list_add_tail(&dso->node, head);
cd84c2ac
FW
1603}
1604
b0da954a 1605static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
1606{
1607 struct dso *pos;
1608
b0da954a 1609 list_for_each_entry(pos, head, node)
cd84c2ac
FW
1610 if (strcmp(pos->name, name) == 0)
1611 return pos;
1612 return NULL;
1613}
1614
00a192b3 1615struct dso *dsos__findnew(const char *name)
cd84c2ac 1616{
b0da954a 1617 struct dso *dso = dsos__find(&dsos__user, name);
cd84c2ac 1618
e4204992 1619 if (!dso) {
00a192b3 1620 dso = dso__new(name);
cfc10d3b 1621 if (dso != NULL) {
b0da954a 1622 dsos__add(&dsos__user, dso);
cfc10d3b
ACM
1623 dso__set_basename(dso);
1624 }
66bd8424 1625 }
cd84c2ac
FW
1626
1627 return dso;
cd84c2ac
FW
1628}
1629
b0da954a 1630static void __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
1631{
1632 struct dso *pos;
1633
95011c60
ACM
1634 list_for_each_entry(pos, head, node) {
1635 int i;
1636 for (i = 0; i < MAP__NR_TYPES; ++i)
1637 dso__fprintf(pos, i, fp);
1638 }
cd84c2ac
FW
1639}
1640
b0da954a
ACM
1641void dsos__fprintf(FILE *fp)
1642{
1643 __dsos__fprintf(&dsos__kernel, fp);
1644 __dsos__fprintf(&dsos__user, fp);
1645}
1646
1647static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
9e03eb2d
ACM
1648{
1649 struct dso *pos;
1650 size_t ret = 0;
1651
b0da954a 1652 list_for_each_entry(pos, head, node) {
9e03eb2d 1653 ret += dso__fprintf_buildid(pos, fp);
1124ba73 1654 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
1655 }
1656 return ret;
1657}
1658
b0da954a
ACM
1659size_t dsos__fprintf_buildid(FILE *fp)
1660{
1661 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1662 __dsos__fprintf_buildid(&dsos__user, fp));
1663}
1664
f1dfa0b1 1665static struct dso *dsos__create_kernel( const char *vmlinux)
cd84c2ac 1666{
95011c60 1667 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
cd84c2ac 1668
2446042c 1669 if (kernel == NULL)
f1dfa0b1 1670 return NULL;
c338aee8 1671
4e06255f
ACM
1672 kernel->short_name = "[kernel]";
1673 kernel->kernel = 1;
2446042c 1674
00a192b3 1675 vdso = dso__new("[vdso]");
c338aee8 1676 if (vdso == NULL)
f1dfa0b1 1677 goto out_delete_kernel_dso;
3610583c 1678 dso__set_loaded(vdso, MAP__FUNCTION);
2446042c
ACM
1679
1680 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1681 sizeof(kernel->build_id)) == 0)
1682 kernel->has_build_id = true;
cd84c2ac 1683
b0da954a
ACM
1684 dsos__add(&dsos__kernel, kernel);
1685 dsos__add(&dsos__user, vdso);
cd84c2ac 1686
f1dfa0b1 1687 return kernel;
c338aee8 1688
c338aee8
ACM
1689out_delete_kernel_dso:
1690 dso__delete(kernel);
f1dfa0b1
ACM
1691 return NULL;
1692}
1693
1694static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1695{
1696 struct map *functions, *variables;
1697 struct dso *kernel = dsos__create_kernel(vmlinux);
1698
1699 if (kernel == NULL)
1700 return -1;
1701
1702 functions = map__new2(0, kernel, MAP__FUNCTION);
1703 if (functions == NULL)
1704 return -1;
1705
1706 variables = map__new2(0, kernel, MAP__VARIABLE);
1707 if (variables == NULL) {
1708 map__delete(functions);
1709 return -1;
1710 }
1711
1712 functions->map_ip = functions->unmap_ip =
1713 variables->map_ip = variables->unmap_ip = identity__map_ip;
1714 map_groups__insert(self, functions);
1715 map_groups__insert(self, variables);
1716
1717 return 0;
2446042c
ACM
1718}
1719
cc612d81
ACM
1720static void vmlinux_path__exit(void)
1721{
1722 while (--vmlinux_path__nr_entries >= 0) {
1723 free(vmlinux_path[vmlinux_path__nr_entries]);
1724 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1725 }
1726
1727 free(vmlinux_path);
1728 vmlinux_path = NULL;
1729}
1730
1731static int vmlinux_path__init(void)
1732{
1733 struct utsname uts;
1734 char bf[PATH_MAX];
1735
1736 if (uname(&uts) < 0)
1737 return -1;
1738
1739 vmlinux_path = malloc(sizeof(char *) * 5);
1740 if (vmlinux_path == NULL)
1741 return -1;
1742
1743 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1744 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1745 goto out_fail;
1746 ++vmlinux_path__nr_entries;
1747 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1748 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1749 goto out_fail;
1750 ++vmlinux_path__nr_entries;
1751 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1752 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1753 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1754 goto out_fail;
1755 ++vmlinux_path__nr_entries;
1756 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1757 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1758 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1759 goto out_fail;
1760 ++vmlinux_path__nr_entries;
1761 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1762 uts.release);
1763 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1764 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1765 goto out_fail;
1766 ++vmlinux_path__nr_entries;
1767
1768 return 0;
1769
1770out_fail:
1771 vmlinux_path__exit();
1772 return -1;
1773}
1774
655000e7
ACM
1775static int setup_list(struct strlist **list, const char *list_str,
1776 const char *list_name)
1777{
1778 if (list_str == NULL)
1779 return 0;
1780
1781 *list = strlist__new(true, list_str);
1782 if (!*list) {
1783 pr_err("problems parsing %s list\n", list_name);
1784 return -1;
1785 }
1786 return 0;
1787}
1788
75be6cf4 1789int symbol__init(void)
2446042c 1790{
95011c60 1791 elf_version(EV_CURRENT);
75be6cf4
ACM
1792 if (symbol_conf.sort_by_name)
1793 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1794 sizeof(struct symbol));
b32d133a 1795
75be6cf4 1796 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
1797 return -1;
1798
c410a338
ACM
1799 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1800 pr_err("'.' is the only non valid --field-separator argument\n");
1801 return -1;
1802 }
1803
655000e7
ACM
1804 if (setup_list(&symbol_conf.dso_list,
1805 symbol_conf.dso_list_str, "dso") < 0)
1806 return -1;
1807
1808 if (setup_list(&symbol_conf.comm_list,
1809 symbol_conf.comm_list_str, "comm") < 0)
1810 goto out_free_dso_list;
1811
1812 if (setup_list(&symbol_conf.sym_list,
1813 symbol_conf.sym_list_str, "symbol") < 0)
1814 goto out_free_comm_list;
1815
4aa65636 1816 return 0;
655000e7
ACM
1817
1818out_free_dso_list:
1819 strlist__delete(symbol_conf.dso_list);
1820out_free_comm_list:
1821 strlist__delete(symbol_conf.comm_list);
1822 return -1;
4aa65636
ACM
1823}
1824
75be6cf4 1825int perf_session__create_kernel_maps(struct perf_session *self)
4aa65636 1826{
4aa65636 1827 if (map_groups__create_kernel_maps(&self->kmaps,
75be6cf4 1828 symbol_conf.vmlinux_name) < 0)
cc612d81 1829 return -1;
cc612d81 1830
75be6cf4
ACM
1831 if (symbol_conf.use_modules &&
1832 perf_session__create_module_maps(self) < 0)
4aa65636
ACM
1833 pr_debug("Failed to load list of modules for session %s, "
1834 "continuing...\n", self->filename);
90c83218
ACM
1835 /*
1836 * Now that we have all the maps created, just set the ->end of them:
1837 */
4aa65636 1838 map_groups__fixup_end(&self->kmaps);
6671cb16 1839 return 0;
cd84c2ac 1840}