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