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