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