]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/symbol.c
perf symbols: Remove perf_session usage in symbols layer
[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
ACM
1013 curr_map = map__new2(start, curr_dso,
1014 MAP__FUNCTION);
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);
2e538c4a
ACM
1024 } else
1025 curr_dso = curr_map->dso;
1026
1027 goto new_symbol;
af427bf5
ACM
1028 }
1029
2e538c4a 1030 if (curr_dso->adjust_symbols) {
6beba7ad
ACM
1031 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1032 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1033 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
f5812a7a 1034 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 1035 }
28ac909b
ACM
1036 /*
1037 * We need to figure out if the object was created from C++ sources
1038 * DWARF DW_compile_unit has this, but we don't always have access
1039 * to it...
1040 */
83a0944f 1041 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 1042 if (demangled != NULL)
83a0944f 1043 elf_name = demangled;
2e538c4a 1044new_symbol:
00a192b3 1045 f = symbol__new(sym.st_value, sym.st_size, elf_name);
28ac909b 1046 free(demangled);
a2928c42
ACM
1047 if (!f)
1048 goto out_elf_end;
1049
2e538c4a 1050 if (filter && filter(curr_map, f))
00a192b3 1051 symbol__delete(f);
69ee69f6 1052 else {
6a4694a4 1053 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
1054 nr++;
1055 }
a2928c42
ACM
1056 }
1057
2e538c4a
ACM
1058 /*
1059 * For misannotated, zeroed, ASM function sizes.
1060 */
1061 if (nr > 0)
6a4694a4 1062 symbols__fixup_end(&self->symbols[map->type]);
a2928c42
ACM
1063 err = nr;
1064out_elf_end:
1065 elf_end(elf);
1066out_close:
1067 return err;
1068}
1069
78075caa
ACM
1070static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1071{
1072 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1073}
1074
b0da954a 1075static bool __dsos__read_build_ids(struct list_head *head)
57f395a7 1076{
e30a3d12 1077 bool have_build_id = false;
57f395a7
FW
1078 struct dso *pos;
1079
b0da954a 1080 list_for_each_entry(pos, head, node)
e30a3d12
ACM
1081 if (filename__read_build_id(pos->long_name, pos->build_id,
1082 sizeof(pos->build_id)) > 0) {
1083 have_build_id = true;
1084 pos->has_build_id = true;
1085 }
57f395a7 1086
e30a3d12 1087 return have_build_id;
57f395a7
FW
1088}
1089
b0da954a
ACM
1090bool dsos__read_build_ids(void)
1091{
8b4825bf
ACM
1092 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1093 ubuildids = __dsos__read_build_ids(&dsos__user);
1094 return kbuildids || ubuildids;
b0da954a
ACM
1095}
1096
fd7a346e
ACM
1097/*
1098 * Align offset to 4 bytes as needed for note name and descriptor data.
1099 */
1100#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1101
2643ce11 1102int filename__read_build_id(const char *filename, void *bf, size_t size)
4d1e00a8 1103{
2643ce11 1104 int fd, err = -1;
4d1e00a8
ACM
1105 GElf_Ehdr ehdr;
1106 GElf_Shdr shdr;
fd7a346e 1107 Elf_Data *data;
4d1e00a8 1108 Elf_Scn *sec;
e57cfcda 1109 Elf_Kind ek;
fd7a346e 1110 void *ptr;
4d1e00a8 1111 Elf *elf;
4d1e00a8 1112
2643ce11
ACM
1113 if (size < BUILD_ID_SIZE)
1114 goto out;
1115
1116 fd = open(filename, O_RDONLY);
4d1e00a8
ACM
1117 if (fd < 0)
1118 goto out;
1119
84087126 1120 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
4d1e00a8 1121 if (elf == NULL) {
8d06367f 1122 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
4d1e00a8
ACM
1123 goto out_close;
1124 }
1125
e57cfcda
PE
1126 ek = elf_kind(elf);
1127 if (ek != ELF_K_ELF)
1128 goto out_elf_end;
1129
4d1e00a8 1130 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 1131 pr_err("%s: cannot get elf header.\n", __func__);
4d1e00a8
ACM
1132 goto out_elf_end;
1133 }
1134
2643ce11
ACM
1135 sec = elf_section_by_name(elf, &ehdr, &shdr,
1136 ".note.gnu.build-id", NULL);
fd7a346e
ACM
1137 if (sec == NULL) {
1138 sec = elf_section_by_name(elf, &ehdr, &shdr,
1139 ".notes", NULL);
1140 if (sec == NULL)
1141 goto out_elf_end;
1142 }
4d1e00a8 1143
fd7a346e
ACM
1144 data = elf_getdata(sec, NULL);
1145 if (data == NULL)
4d1e00a8 1146 goto out_elf_end;
fd7a346e
ACM
1147
1148 ptr = data->d_buf;
1149 while (ptr < (data->d_buf + data->d_size)) {
1150 GElf_Nhdr *nhdr = ptr;
1151 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1152 descsz = NOTE_ALIGN(nhdr->n_descsz);
1153 const char *name;
1154
1155 ptr += sizeof(*nhdr);
1156 name = ptr;
1157 ptr += namesz;
1158 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1159 nhdr->n_namesz == sizeof("GNU")) {
1160 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1161 memcpy(bf, ptr, BUILD_ID_SIZE);
1162 err = BUILD_ID_SIZE;
1163 break;
1164 }
1165 }
1166 ptr += descsz;
1167 }
2643ce11
ACM
1168out_elf_end:
1169 elf_end(elf);
1170out_close:
1171 close(fd);
1172out:
1173 return err;
1174}
1175
f1617b40
ACM
1176int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1177{
1178 int fd, err = -1;
1179
1180 if (size < BUILD_ID_SIZE)
1181 goto out;
1182
1183 fd = open(filename, O_RDONLY);
1184 if (fd < 0)
1185 goto out;
1186
1187 while (1) {
1188 char bf[BUFSIZ];
1189 GElf_Nhdr nhdr;
1190 int namesz, descsz;
1191
1192 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1193 break;
1194
fd7a346e
ACM
1195 namesz = NOTE_ALIGN(nhdr.n_namesz);
1196 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1197 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1198 nhdr.n_namesz == sizeof("GNU")) {
1199 if (read(fd, bf, namesz) != namesz)
1200 break;
1201 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1202 if (read(fd, build_id,
1203 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1204 err = 0;
1205 break;
1206 }
1207 } else if (read(fd, bf, descsz) != descsz)
1208 break;
1209 } else {
1210 int n = namesz + descsz;
1211 if (read(fd, bf, n) != n)
1212 break;
1213 }
1214 }
1215 close(fd);
1216out:
1217 return err;
1218}
1219
94cb9e38
ACM
1220char dso__symtab_origin(const struct dso *self)
1221{
1222 static const char origin[] = {
1223 [DSO__ORIG_KERNEL] = 'k',
1224 [DSO__ORIG_JAVA_JIT] = 'j',
4cf40131 1225 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
94cb9e38
ACM
1226 [DSO__ORIG_FEDORA] = 'f',
1227 [DSO__ORIG_UBUNTU] = 'u',
1228 [DSO__ORIG_BUILDID] = 'b',
1229 [DSO__ORIG_DSO] = 'd',
439d473b 1230 [DSO__ORIG_KMODULE] = 'K',
94cb9e38
ACM
1231 };
1232
1233 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1234 return '!';
1235 return origin[self->origin];
1236}
1237
9de89fe7 1238int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
a2928c42 1239{
4d1e00a8 1240 int size = PATH_MAX;
c338aee8 1241 char *name;
d3379ab9 1242 u8 build_id[BUILD_ID_SIZE];
4cf40131 1243 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
a2928c42
ACM
1244 int ret = -1;
1245 int fd;
1246
3610583c 1247 dso__set_loaded(self, map->type);
66bd8424 1248
c338aee8 1249 if (self->kernel)
9de89fe7 1250 return dso__load_kernel_sym(self, map, filter);
c338aee8
ACM
1251
1252 name = malloc(size);
a2928c42
ACM
1253 if (!name)
1254 return -1;
1255
30d7a77d 1256 self->adjust_symbols = 0;
f5812a7a 1257
94cb9e38 1258 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
6beba7ad 1259 ret = dso__load_perf_map(self, map, filter);
94cb9e38
ACM
1260 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1261 DSO__ORIG_NOT_FOUND;
1262 return ret;
1263 }
1264
4cf40131 1265 self->origin = DSO__ORIG_BUILD_ID_CACHE;
80d496be 1266
4cf40131
ACM
1267 if (self->has_build_id) {
1268 build_id__sprintf(self->build_id, sizeof(self->build_id),
1269 build_id_hex);
1270 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1271 getenv("HOME"), DEBUG_CACHE_DIR,
1272 build_id_hex, build_id_hex + 2);
1273 goto open_file;
1274 }
a2928c42
ACM
1275more:
1276 do {
94cb9e38
ACM
1277 self->origin++;
1278 switch (self->origin) {
1279 case DSO__ORIG_FEDORA:
439d473b
ACM
1280 snprintf(name, size, "/usr/lib/debug%s.debug",
1281 self->long_name);
a2928c42 1282 break;
94cb9e38 1283 case DSO__ORIG_UBUNTU:
439d473b
ACM
1284 snprintf(name, size, "/usr/lib/debug%s",
1285 self->long_name);
a2928c42 1286 break;
94cb9e38 1287 case DSO__ORIG_BUILDID:
d3379ab9
ACM
1288 if (filename__read_build_id(self->long_name, build_id,
1289 sizeof(build_id))) {
d3379ab9
ACM
1290 build_id__sprintf(build_id, sizeof(build_id),
1291 build_id_hex);
4d1e00a8
ACM
1292 snprintf(name, size,
1293 "/usr/lib/debug/.build-id/%.2s/%s.debug",
d3379ab9
ACM
1294 build_id_hex, build_id_hex + 2);
1295 if (self->has_build_id)
1296 goto compare_build_id;
1297 break;
4d1e00a8 1298 }
94cb9e38 1299 self->origin++;
4d1e00a8 1300 /* Fall thru */
94cb9e38 1301 case DSO__ORIG_DSO:
439d473b 1302 snprintf(name, size, "%s", self->long_name);
a2928c42
ACM
1303 break;
1304
1305 default:
1306 goto out;
1307 }
a2928c42 1308
8d06367f 1309 if (self->has_build_id) {
d3379ab9
ACM
1310 if (filename__read_build_id(name, build_id,
1311 sizeof(build_id)) < 0)
8d06367f 1312 goto more;
8d06367f 1313compare_build_id:
78075caa 1314 if (!dso__build_id_equal(self, build_id))
8d06367f
ACM
1315 goto more;
1316 }
4cf40131 1317open_file:
a2928c42
ACM
1318 fd = open(name, O_RDONLY);
1319 } while (fd < 0);
1320
9de89fe7 1321 ret = dso__load_sym(self, map, name, fd, filter, 0);
a2928c42
ACM
1322 close(fd);
1323
1324 /*
1325 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1326 */
1327 if (!ret)
1328 goto more;
1329
a25e46c4 1330 if (ret > 0) {
82164161 1331 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
a25e46c4
ACM
1332 if (nr_plt > 0)
1333 ret += nr_plt;
1334 }
a2928c42
ACM
1335out:
1336 free(name);
1340e6bb
ACM
1337 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1338 return 0;
a2928c42
ACM
1339 return ret;
1340}
1341
79406cd7
ACM
1342struct map *map_groups__find_by_name(struct map_groups *self,
1343 enum map_type type, const char *name)
439d473b
ACM
1344{
1345 struct rb_node *nd;
1346
79406cd7 1347 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1348 struct map *map = rb_entry(nd, struct map, rb_node);
1349
b7cece76 1350 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1351 return map;
1352 }
1353
1354 return NULL;
1355}
1356
b7cece76
ACM
1357static int dso__kernel_module_get_build_id(struct dso *self)
1358{
1359 char filename[PATH_MAX];
1360 /*
1361 * kernel module short names are of the form "[module]" and
1362 * we need just "module" here.
1363 */
1364 const char *name = self->short_name + 1;
1365
1366 snprintf(filename, sizeof(filename),
1367 "/sys/module/%.*s/notes/.note.gnu.build-id",
1368 (int)strlen(name - 1), name);
1369
1370 if (sysfs__read_build_id(filename, self->build_id,
1371 sizeof(self->build_id)) == 0)
1372 self->has_build_id = true;
1373
1374 return 0;
1375}
1376
9de89fe7 1377static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
6cfcc53e 1378{
439d473b 1379 struct dirent *dent;
439d473b 1380 DIR *dir = opendir(dirname);
6cfcc53e 1381
439d473b 1382 if (!dir) {
87f8ea4c 1383 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
439d473b
ACM
1384 return -1;
1385 }
6cfcc53e 1386
439d473b
ACM
1387 while ((dent = readdir(dir)) != NULL) {
1388 char path[PATH_MAX];
1389
1390 if (dent->d_type == DT_DIR) {
1391 if (!strcmp(dent->d_name, ".") ||
1392 !strcmp(dent->d_name, ".."))
1393 continue;
1394
1395 snprintf(path, sizeof(path), "%s/%s",
1396 dirname, dent->d_name);
9de89fe7 1397 if (map_groups__set_modules_path_dir(self, path) < 0)
439d473b
ACM
1398 goto failure;
1399 } else {
1400 char *dot = strrchr(dent->d_name, '.'),
1401 dso_name[PATH_MAX];
1402 struct map *map;
cfc10d3b 1403 char *long_name;
439d473b
ACM
1404
1405 if (dot == NULL || strcmp(dot, ".ko"))
1406 continue;
1407 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1408 (int)(dot - dent->d_name), dent->d_name);
1409
a2a99e8e 1410 strxfrchar(dso_name, '-', '_');
9de89fe7 1411 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
439d473b
ACM
1412 if (map == NULL)
1413 continue;
1414
1415 snprintf(path, sizeof(path), "%s/%s",
1416 dirname, dent->d_name);
1417
cfc10d3b
ACM
1418 long_name = strdup(path);
1419 if (long_name == NULL)
439d473b 1420 goto failure;
cfc10d3b 1421 dso__set_long_name(map->dso, long_name);
b7cece76 1422 dso__kernel_module_get_build_id(map->dso);
439d473b 1423 }
439d473b 1424 }
6cfcc53e 1425
c338aee8 1426 return 0;
439d473b
ACM
1427failure:
1428 closedir(dir);
1429 return -1;
1430}
6cfcc53e 1431
9de89fe7 1432static int map_groups__set_modules_path(struct map_groups *self)
439d473b
ACM
1433{
1434 struct utsname uts;
1435 char modules_path[PATH_MAX];
6cfcc53e 1436
439d473b
ACM
1437 if (uname(&uts) < 0)
1438 return -1;
6cfcc53e 1439
439d473b
ACM
1440 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1441 uts.release);
6cfcc53e 1442
9de89fe7 1443 return map_groups__set_modules_path_dir(self, modules_path);
6cfcc53e
MG
1444}
1445
439d473b
ACM
1446/*
1447 * Constructor variant for modules (where we know from /proc/modules where
1448 * they are loaded) and for vmlinux, where only after we load all the
1449 * symbols we'll know where it starts and ends.
1450 */
3610583c 1451static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 1452{
9de89fe7
ACM
1453 struct map *self = zalloc(sizeof(*self) +
1454 (dso->kernel ? sizeof(struct kmap) : 0));
439d473b 1455 if (self != NULL) {
439d473b 1456 /*
afb7b4f0 1457 * ->end will be filled after we load all the symbols
439d473b 1458 */
3610583c 1459 map__init(self, type, start, 0, 0, dso);
439d473b 1460 }
afb7b4f0 1461
439d473b
ACM
1462 return self;
1463}
1464
9de89fe7
ACM
1465struct map *map_groups__new_module(struct map_groups *self, u64 start,
1466 const char *filename)
b7cece76
ACM
1467{
1468 struct map *map;
1469 struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1470
1471 if (dso == NULL)
1472 return NULL;
1473
1474 map = map__new2(start, dso, MAP__FUNCTION);
1475 if (map == NULL)
1476 return NULL;
1477
1478 dso->origin = DSO__ORIG_KMODULE;
9de89fe7 1479 map_groups__insert(self, map);
b7cece76
ACM
1480 return map;
1481}
1482
9de89fe7 1483static int map_groups__create_modules(struct map_groups *self)
439d473b
ACM
1484{
1485 char *line = NULL;
1486 size_t n;
1487 FILE *file = fopen("/proc/modules", "r");
1488 struct map *map;
6cfcc53e 1489
439d473b
ACM
1490 if (file == NULL)
1491 return -1;
6cfcc53e 1492
439d473b
ACM
1493 while (!feof(file)) {
1494 char name[PATH_MAX];
1495 u64 start;
439d473b
ACM
1496 char *sep;
1497 int line_len;
6cfcc53e 1498
439d473b
ACM
1499 line_len = getline(&line, &n, file);
1500 if (line_len < 0)
1501 break;
1502
1503 if (!line)
1504 goto out_failure;
1505
1506 line[--line_len] = '\0'; /* \n */
1507
1508 sep = strrchr(line, 'x');
1509 if (sep == NULL)
1510 continue;
1511
1512 hex2u64(sep + 1, &start);
1513
1514 sep = strchr(line, ' ');
1515 if (sep == NULL)
1516 continue;
1517
1518 *sep = '\0';
1519
1520 snprintf(name, sizeof(name), "[%s]", line);
9de89fe7 1521 map = map_groups__new_module(self, start, name);
b7cece76 1522 if (map == NULL)
439d473b 1523 goto out_delete_line;
b7cece76 1524 dso__kernel_module_get_build_id(map->dso);
6cfcc53e 1525 }
439d473b
ACM
1526
1527 free(line);
1528 fclose(file);
1529
9de89fe7 1530 return map_groups__set_modules_path(self);
439d473b
ACM
1531
1532out_delete_line:
1533 free(line);
1534out_failure:
1535 return -1;
6cfcc53e
MG
1536}
1537
9958e1f0 1538static int dso__load_vmlinux(struct dso *self, struct map *map,
6beba7ad 1539 const char *vmlinux, symbol_filter_t filter)
a2928c42 1540{
fbd733b8 1541 int err = -1, fd;
a2928c42 1542
fbd733b8
ACM
1543 if (self->has_build_id) {
1544 u8 build_id[BUILD_ID_SIZE];
1545
1546 if (filename__read_build_id(vmlinux, build_id,
1547 sizeof(build_id)) < 0) {
1548 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1549 return -1;
1550 }
1551 if (!dso__build_id_equal(self, build_id)) {
1552 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1553 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1554
1555 build_id__sprintf(self->build_id,
1556 sizeof(self->build_id),
1557 expected_build_id);
1558 build_id__sprintf(build_id, sizeof(build_id),
1559 vmlinux_build_id);
1560 pr_debug("build_id in %s is %s while expected is %s, "
1561 "ignoring it\n", vmlinux, vmlinux_build_id,
1562 expected_build_id);
1563 return -1;
1564 }
1565 }
66bd8424 1566
fbd733b8 1567 fd = open(vmlinux, O_RDONLY);
a2928c42
ACM
1568 if (fd < 0)
1569 return -1;
1570
3610583c 1571 dso__set_loaded(self, map->type);
9de89fe7 1572 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
a2928c42
ACM
1573 close(fd);
1574
1575 return err;
1576}
1577
a19afe46 1578int dso__load_vmlinux_path(struct dso *self, struct map *map,
9de89fe7 1579 symbol_filter_t filter)
a19afe46
ACM
1580{
1581 int i, err = 0;
1582
1583 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1584 vmlinux_path__nr_entries);
1585
1586 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
9de89fe7 1587 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
a19afe46
ACM
1588 if (err > 0) {
1589 pr_debug("Using %s for symbols\n", vmlinux_path[i]);
1590 dso__set_long_name(self, strdup(vmlinux_path[i]));
1591 break;
1592 }
1593 }
1594
1595 return err;
1596}
1597
c338aee8 1598static int dso__load_kernel_sym(struct dso *self, struct map *map,
9de89fe7 1599 symbol_filter_t filter)
a827c875 1600{
cc612d81 1601 int err;
9e201442
ACM
1602 const char *kallsyms_filename = NULL;
1603 char *kallsyms_allocated_filename = NULL;
dc8d6ab2
ACM
1604 /*
1605 * Step 1: if the user specified a vmlinux filename, use it and only
1606 * it, reporting errors to the user if it cannot be used.
1607 *
1608 * For instance, try to analyse an ARM perf.data file _without_ a
1609 * build-id, or if the user specifies the wrong path to the right
1610 * vmlinux file, obviously we can't fallback to another vmlinux (a
1611 * x86_86 one, on the machine where analysis is being performed, say),
1612 * or worse, /proc/kallsyms.
1613 *
1614 * If the specified file _has_ a build-id and there is a build-id
1615 * section in the perf.data file, we will still do the expected
1616 * validation in dso__load_vmlinux and will bail out if they don't
1617 * match.
1618 */
1619 if (symbol_conf.vmlinux_name != NULL) {
9de89fe7 1620 err = dso__load_vmlinux(self, map,
dc8d6ab2
ACM
1621 symbol_conf.vmlinux_name, filter);
1622 goto out_try_fixup;
1623 }
cc612d81
ACM
1624
1625 if (vmlinux_path != NULL) {
9de89fe7 1626 err = dso__load_vmlinux_path(self, map, filter);
a19afe46
ACM
1627 if (err > 0)
1628 goto out_fixup;
cc612d81
ACM
1629 }
1630
b7cece76
ACM
1631 /*
1632 * Say the kernel DSO was created when processing the build-id header table,
1633 * we have a build-id, so check if it is the same as the running kernel,
1634 * using it if it is.
1635 */
1636 if (self->has_build_id) {
1637 u8 kallsyms_build_id[BUILD_ID_SIZE];
9e201442 1638 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
b7cece76
ACM
1639
1640 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
8d0591f6 1641 sizeof(kallsyms_build_id)) == 0) {
9e201442
ACM
1642 if (dso__build_id_equal(self, kallsyms_build_id)) {
1643 kallsyms_filename = "/proc/kallsyms";
8d0591f6 1644 goto do_kallsyms;
9e201442 1645 }
8d0591f6 1646 }
dc8d6ab2
ACM
1647 /*
1648 * Now look if we have it on the build-id cache in
1649 * $HOME/.debug/[kernel.kallsyms].
1650 */
9e201442
ACM
1651 build_id__sprintf(self->build_id, sizeof(self->build_id),
1652 sbuild_id);
1653
1654 if (asprintf(&kallsyms_allocated_filename,
1655 "%s/.debug/[kernel.kallsyms]/%s",
dc8d6ab2
ACM
1656 getenv("HOME"), sbuild_id) == -1)
1657 return -1;
1658
19fc2ded
ACM
1659 kallsyms_filename = kallsyms_allocated_filename;
1660
dc8d6ab2 1661 if (access(kallsyms_filename, F_OK)) {
9e201442 1662 free(kallsyms_allocated_filename);
dc8d6ab2 1663 return -1;
9e201442 1664 }
dc8d6ab2
ACM
1665 } else {
1666 /*
1667 * Last resort, if we don't have a build-id and couldn't find
1668 * any vmlinux file, try the running kernel kallsyms table.
1669 */
9e201442 1670 kallsyms_filename = "/proc/kallsyms";
9e201442 1671 }
439d473b 1672
cc612d81 1673do_kallsyms:
9de89fe7 1674 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
dc8d6ab2 1675 free(kallsyms_allocated_filename);
439d473b 1676
dc8d6ab2 1677out_try_fixup:
439d473b 1678 if (err > 0) {
cc612d81 1679out_fixup:
e1c7c6a4 1680 if (kallsyms_filename != NULL)
dc8d6ab2 1681 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
6a4694a4
ACM
1682 map__fixup_start(map);
1683 map__fixup_end(map);
439d473b 1684 }
94cb9e38 1685
a827c875
ACM
1686 return err;
1687}
1688
b0da954a
ACM
1689LIST_HEAD(dsos__user);
1690LIST_HEAD(dsos__kernel);
cc612d81 1691struct dso *vdso;
cd84c2ac 1692
b0da954a 1693static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 1694{
b0da954a 1695 list_add_tail(&dso->node, head);
cd84c2ac
FW
1696}
1697
b0da954a 1698static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
1699{
1700 struct dso *pos;
1701
b0da954a 1702 list_for_each_entry(pos, head, node)
cf4e5b08 1703 if (strcmp(pos->long_name, name) == 0)
cd84c2ac
FW
1704 return pos;
1705 return NULL;
1706}
1707
a89e5abe 1708struct dso *__dsos__findnew(struct list_head *head, const char *name)
cd84c2ac 1709{
a89e5abe 1710 struct dso *dso = dsos__find(head, name);
cd84c2ac 1711
e4204992 1712 if (!dso) {
00a192b3 1713 dso = dso__new(name);
cfc10d3b 1714 if (dso != NULL) {
a89e5abe 1715 dsos__add(head, dso);
cfc10d3b
ACM
1716 dso__set_basename(dso);
1717 }
66bd8424 1718 }
cd84c2ac
FW
1719
1720 return dso;
cd84c2ac
FW
1721}
1722
b0da954a 1723static void __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
1724{
1725 struct dso *pos;
1726
95011c60
ACM
1727 list_for_each_entry(pos, head, node) {
1728 int i;
1729 for (i = 0; i < MAP__NR_TYPES; ++i)
1730 dso__fprintf(pos, i, fp);
1731 }
cd84c2ac
FW
1732}
1733
b0da954a
ACM
1734void dsos__fprintf(FILE *fp)
1735{
1736 __dsos__fprintf(&dsos__kernel, fp);
1737 __dsos__fprintf(&dsos__user, fp);
1738}
1739
88d3d9b7
ACM
1740static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1741 bool with_hits)
9e03eb2d
ACM
1742{
1743 struct dso *pos;
1744 size_t ret = 0;
1745
b0da954a 1746 list_for_each_entry(pos, head, node) {
88d3d9b7
ACM
1747 if (with_hits && !pos->hit)
1748 continue;
9e03eb2d 1749 ret += dso__fprintf_buildid(pos, fp);
1124ba73 1750 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
1751 }
1752 return ret;
1753}
1754
88d3d9b7 1755size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
b0da954a 1756{
88d3d9b7
ACM
1757 return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
1758 __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
b0da954a
ACM
1759}
1760
fd1d908c
ACM
1761struct dso *dso__new_kernel(const char *name)
1762{
1763 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1764
1765 if (self != NULL) {
1766 self->short_name = "[kernel]";
1767 self->kernel = 1;
1768 }
1769
1770 return self;
1771}
1772
1773void dso__read_running_kernel_build_id(struct dso *self)
1774{
1775 if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
1776 sizeof(self->build_id)) == 0)
1777 self->has_build_id = true;
1778}
1779
de176489 1780static struct dso *dsos__create_kernel(const char *vmlinux)
cd84c2ac 1781{
fd1d908c 1782 struct dso *kernel = dso__new_kernel(vmlinux);
cd84c2ac 1783
2446042c 1784 if (kernel == NULL)
f1dfa0b1 1785 return NULL;
c338aee8 1786
00a192b3 1787 vdso = dso__new("[vdso]");
c338aee8 1788 if (vdso == NULL)
f1dfa0b1 1789 goto out_delete_kernel_dso;
3610583c 1790 dso__set_loaded(vdso, MAP__FUNCTION);
2446042c 1791
fd1d908c 1792 dso__read_running_kernel_build_id(kernel);
cd84c2ac 1793
b0da954a
ACM
1794 dsos__add(&dsos__kernel, kernel);
1795 dsos__add(&dsos__user, vdso);
cd84c2ac 1796
f1dfa0b1 1797 return kernel;
c338aee8 1798
c338aee8
ACM
1799out_delete_kernel_dso:
1800 dso__delete(kernel);
f1dfa0b1
ACM
1801 return NULL;
1802}
1803
b7cece76
ACM
1804int __map_groups__create_kernel_maps(struct map_groups *self,
1805 struct map *vmlinux_maps[MAP__NR_TYPES],
1806 struct dso *kernel)
f1dfa0b1 1807{
de176489 1808 enum map_type type;
f1dfa0b1 1809
de176489 1810 for (type = 0; type < MAP__NR_TYPES; ++type) {
9de89fe7
ACM
1811 struct kmap *kmap;
1812
de176489
ACM
1813 vmlinux_maps[type] = map__new2(0, kernel, type);
1814 if (vmlinux_maps[type] == NULL)
1815 return -1;
f1dfa0b1 1816
de176489
ACM
1817 vmlinux_maps[type]->map_ip =
1818 vmlinux_maps[type]->unmap_ip = identity__map_ip;
9de89fe7
ACM
1819
1820 kmap = map__kmap(vmlinux_maps[type]);
1821 kmap->kmaps = self;
de176489 1822 map_groups__insert(self, vmlinux_maps[type]);
f1dfa0b1
ACM
1823 }
1824
f1dfa0b1 1825 return 0;
2446042c
ACM
1826}
1827
cc612d81
ACM
1828static void vmlinux_path__exit(void)
1829{
1830 while (--vmlinux_path__nr_entries >= 0) {
1831 free(vmlinux_path[vmlinux_path__nr_entries]);
1832 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1833 }
1834
1835 free(vmlinux_path);
1836 vmlinux_path = NULL;
1837}
1838
1839static int vmlinux_path__init(void)
1840{
1841 struct utsname uts;
1842 char bf[PATH_MAX];
1843
1844 if (uname(&uts) < 0)
1845 return -1;
1846
1847 vmlinux_path = malloc(sizeof(char *) * 5);
1848 if (vmlinux_path == NULL)
1849 return -1;
1850
1851 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1852 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1853 goto out_fail;
1854 ++vmlinux_path__nr_entries;
1855 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1856 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1857 goto out_fail;
1858 ++vmlinux_path__nr_entries;
1859 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1860 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1861 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1862 goto out_fail;
1863 ++vmlinux_path__nr_entries;
1864 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1865 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1866 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1867 goto out_fail;
1868 ++vmlinux_path__nr_entries;
1869 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1870 uts.release);
1871 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1872 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1873 goto out_fail;
1874 ++vmlinux_path__nr_entries;
1875
1876 return 0;
1877
1878out_fail:
1879 vmlinux_path__exit();
1880 return -1;
1881}
1882
655000e7
ACM
1883static int setup_list(struct strlist **list, const char *list_str,
1884 const char *list_name)
1885{
1886 if (list_str == NULL)
1887 return 0;
1888
1889 *list = strlist__new(true, list_str);
1890 if (!*list) {
1891 pr_err("problems parsing %s list\n", list_name);
1892 return -1;
1893 }
1894 return 0;
1895}
1896
75be6cf4 1897int symbol__init(void)
2446042c 1898{
95011c60 1899 elf_version(EV_CURRENT);
75be6cf4
ACM
1900 if (symbol_conf.sort_by_name)
1901 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1902 sizeof(struct symbol));
b32d133a 1903
75be6cf4 1904 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
1905 return -1;
1906
c410a338
ACM
1907 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1908 pr_err("'.' is the only non valid --field-separator argument\n");
1909 return -1;
1910 }
1911
655000e7
ACM
1912 if (setup_list(&symbol_conf.dso_list,
1913 symbol_conf.dso_list_str, "dso") < 0)
1914 return -1;
1915
1916 if (setup_list(&symbol_conf.comm_list,
1917 symbol_conf.comm_list_str, "comm") < 0)
1918 goto out_free_dso_list;
1919
1920 if (setup_list(&symbol_conf.sym_list,
1921 symbol_conf.sym_list_str, "symbol") < 0)
1922 goto out_free_comm_list;
1923
4aa65636 1924 return 0;
655000e7
ACM
1925
1926out_free_dso_list:
1927 strlist__delete(symbol_conf.dso_list);
1928out_free_comm_list:
1929 strlist__delete(symbol_conf.comm_list);
1930 return -1;
4aa65636
ACM
1931}
1932
9de89fe7
ACM
1933int map_groups__create_kernel_maps(struct map_groups *self,
1934 struct map *vmlinux_maps[MAP__NR_TYPES])
4aa65636 1935{
9de89fe7
ACM
1936 struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
1937
1938 if (kernel == NULL)
1939 return -1;
1940
1941 if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
cc612d81 1942 return -1;
cc612d81 1943
9de89fe7
ACM
1944 if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
1945 return -1;
90c83218
ACM
1946 /*
1947 * Now that we have all the maps created, just set the ->end of them:
1948 */
9de89fe7 1949 map_groups__fixup_end(self);
6671cb16 1950 return 0;
cd84c2ac 1951}