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