]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/symbol.c
perf: 'perf kvm' tool for monitoring guest performance from host
[mirror_ubuntu-bionic-kernel.git] / tools / perf / util / symbol.c
CommitLineData
5aab621b
ACM
1#define _GNU_SOURCE
2#include <ctype.h>
3#include <dirent.h>
4#include <errno.h>
5#include <libgen.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/param.h>
12#include <fcntl.h>
13#include <unistd.h>
a2928c42 14#include "symbol.h"
5aab621b 15#include "strlist.h"
a2928c42
ACM
16
17#include <libelf.h>
18#include <gelf.h>
19#include <elf.h>
f1617b40 20#include <limits.h>
439d473b 21#include <sys/utsname.h>
2cdbc46d 22
c12e15e7
ACM
23#ifndef NT_GNU_BUILD_ID
24#define NT_GNU_BUILD_ID 3
25#endif
26
b0da954a 27static void dsos__add(struct list_head *head, struct dso *dso);
3610583c 28static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
c338aee8 29static int dso__load_kernel_sym(struct dso *self, struct map *map,
9de89fe7 30 symbol_filter_t filter);
a1645ce1
ZY
31static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
32 symbol_filter_t filter);
cc612d81
ACM
33static int vmlinux_path__nr_entries;
34static char **vmlinux_path;
439d473b 35
75be6cf4 36struct symbol_conf symbol_conf = {
d599db3f 37 .exclude_other = true,
b32d133a
ACM
38 .use_modules = true,
39 .try_vmlinux_path = true,
40};
41
3610583c
ACM
42bool dso__loaded(const struct dso *self, enum map_type type)
43{
44 return self->loaded & (1 << type);
45}
46
79406cd7
ACM
47bool dso__sorted_by_name(const struct dso *self, enum map_type type)
48{
49 return self->sorted_by_name & (1 << type);
50}
51
79406cd7
ACM
52static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
53{
54 self->sorted_by_name |= (1 << type);
55}
56
36a3e646 57bool symbol_type__is_a(char symbol_type, enum map_type map_type)
6893d4ee
ACM
58{
59 switch (map_type) {
60 case MAP__FUNCTION:
61 return symbol_type == 'T' || symbol_type == 'W';
f1dfa0b1
ACM
62 case MAP__VARIABLE:
63 return symbol_type == 'D' || symbol_type == 'd';
6893d4ee
ACM
64 default:
65 return false;
66 }
67}
68
fcf1203a 69static void symbols__fixup_end(struct rb_root *self)
af427bf5 70{
fcf1203a 71 struct rb_node *nd, *prevnd = rb_first(self);
2e538c4a 72 struct symbol *curr, *prev;
af427bf5
ACM
73
74 if (prevnd == NULL)
75 return;
76
2e538c4a
ACM
77 curr = rb_entry(prevnd, struct symbol, rb_node);
78
af427bf5 79 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
80 prev = curr;
81 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5
ACM
82
83 if (prev->end == prev->start)
84 prev->end = curr->start - 1;
af427bf5 85 }
2e538c4a
ACM
86
87 /* Last entry */
88 if (curr->end == curr->start)
89 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
90}
91
9958e1f0 92static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
af427bf5
ACM
93{
94 struct map *prev, *curr;
95011c60 95 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
af427bf5
ACM
96
97 if (prevnd == NULL)
98 return;
99
100 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
101
102 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
103 prev = curr;
104 curr = rb_entry(nd, struct map, rb_node);
105 prev->end = curr->start - 1;
2e538c4a 106 }
90c83218
ACM
107
108 /*
109 * We still haven't the actual symbols, so guess the
110 * last map final address.
111 */
112 curr->end = ~0UL;
af427bf5
ACM
113}
114
9958e1f0 115static void map_groups__fixup_end(struct map_groups *self)
23ea4a3f
ACM
116{
117 int i;
118 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 119 __map_groups__fixup_end(self, i);
23ea4a3f
ACM
120}
121
00a192b3 122static struct symbol *symbol__new(u64 start, u64 len, const char *name)
a2928c42 123{
0085c954 124 size_t namelen = strlen(name) + 1;
5aab621b
ACM
125 struct symbol *self = calloc(1, (symbol_conf.priv_size +
126 sizeof(*self) + namelen));
36479484 127 if (self == NULL)
0b73da3f
IM
128 return NULL;
129
75be6cf4
ACM
130 if (symbol_conf.priv_size)
131 self = ((void *)self) + symbol_conf.priv_size;
36479484 132
0b73da3f 133 self->start = start;
6cfcc53e 134 self->end = len ? start + len - 1 : start;
e4204992 135
29a9f66d 136 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
e4204992 137
0b73da3f 138 memcpy(self->name, name, namelen);
a2928c42
ACM
139
140 return self;
141}
142
628ada0c 143void symbol__delete(struct symbol *self)
a2928c42 144{
75be6cf4 145 free(((void *)self) - symbol_conf.priv_size);
a2928c42
ACM
146}
147
148static size_t symbol__fprintf(struct symbol *self, FILE *fp)
149{
439d473b 150 return fprintf(fp, " %llx-%llx %s\n",
a2928c42
ACM
151 self->start, self->end, self->name);
152}
153
b7cece76 154void dso__set_long_name(struct dso *self, char *name)
cfc10d3b 155{
ef6ae724
ACM
156 if (name == NULL)
157 return;
cfc10d3b
ACM
158 self->long_name = name;
159 self->long_name_len = strlen(name);
160}
161
b63be8d7
ACM
162static void dso__set_short_name(struct dso *self, const char *name)
163{
164 if (name == NULL)
165 return;
166 self->short_name = name;
167 self->short_name_len = strlen(name);
168}
169
cfc10d3b
ACM
170static void dso__set_basename(struct dso *self)
171{
b63be8d7 172 dso__set_short_name(self, basename(self->long_name));
cfc10d3b
ACM
173}
174
00a192b3 175struct dso *dso__new(const char *name)
a2928c42 176{
5aab621b 177 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
a2928c42
ACM
178
179 if (self != NULL) {
6a4694a4 180 int i;
a2928c42 181 strcpy(self->name, name);
cfc10d3b 182 dso__set_long_name(self, self->name);
b63be8d7 183 dso__set_short_name(self, self->name);
6a4694a4 184 for (i = 0; i < MAP__NR_TYPES; ++i)
79406cd7 185 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
52d422de 186 self->slen_calculated = 0;
94cb9e38 187 self->origin = DSO__ORIG_NOT_FOUND;
8d06367f 188 self->loaded = 0;
79406cd7 189 self->sorted_by_name = 0;
8d06367f 190 self->has_build_id = 0;
a1645ce1 191 self->kernel = DSO_TYPE_USER;
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
3846df2e
ACM
375 if (self->short_name != self->long_name)
376 ret += fprintf(fp, "%s, ", self->long_name);
377 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
378 self->loaded ? "" : "NOT ");
9e03eb2d 379 ret += dso__fprintf_buildid(self, fp);
6a4694a4 380 ret += fprintf(fp, ")\n");
95011c60
ACM
381 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
382 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
383 ret += symbol__fprintf(pos, fp);
a2928c42
ACM
384 }
385
386 return ret;
387}
388
9e201442
ACM
389int kallsyms__parse(const char *filename, void *arg,
390 int (*process_symbol)(void *arg, const char *name,
682b335a 391 char type, u64 start))
a2928c42 392{
a2928c42
ACM
393 char *line = NULL;
394 size_t n;
682b335a 395 int err = 0;
9e201442 396 FILE *file = fopen(filename, "r");
a2928c42
ACM
397
398 if (file == NULL)
399 goto out_failure;
400
401 while (!feof(file)) {
9cffa8d5 402 u64 start;
a2928c42
ACM
403 int line_len, len;
404 char symbol_type;
2e538c4a 405 char *symbol_name;
a2928c42
ACM
406
407 line_len = getline(&line, &n, file);
a1645ce1 408 if (line_len < 0 || !line)
a2928c42
ACM
409 break;
410
a2928c42
ACM
411 line[--line_len] = '\0'; /* \n */
412
a0055ae2 413 len = hex2u64(line, &start);
a2928c42
ACM
414
415 len++;
416 if (len + 2 >= line_len)
417 continue;
418
419 symbol_type = toupper(line[len]);
af427bf5 420 symbol_name = line + len + 2;
682b335a
ACM
421
422 err = process_symbol(arg, symbol_name, symbol_type, start);
423 if (err)
424 break;
2e538c4a
ACM
425 }
426
427 free(line);
428 fclose(file);
682b335a 429 return err;
2e538c4a 430
2e538c4a
ACM
431out_failure:
432 return -1;
433}
434
682b335a
ACM
435struct process_kallsyms_args {
436 struct map *map;
437 struct dso *dso;
438};
439
440static int map__process_kallsym_symbol(void *arg, const char *name,
441 char type, u64 start)
442{
443 struct symbol *sym;
444 struct process_kallsyms_args *a = arg;
445 struct rb_root *root = &a->dso->symbols[a->map->type];
446
447 if (!symbol_type__is_a(type, a->map->type))
448 return 0;
449
450 /*
451 * Will fix up the end later, when we have all symbols sorted.
452 */
453 sym = symbol__new(start, 0, name);
454
455 if (sym == NULL)
456 return -ENOMEM;
457 /*
458 * We will pass the symbols to the filter later, in
459 * map__split_kallsyms, when we have split the maps per module
460 */
461 symbols__insert(root, sym);
a1645ce1 462
682b335a
ACM
463 return 0;
464}
465
466/*
467 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
468 * so that we can in the next step set the symbol ->end address and then
469 * call kernel_maps__split_kallsyms.
470 */
9e201442
ACM
471static int dso__load_all_kallsyms(struct dso *self, const char *filename,
472 struct map *map)
682b335a
ACM
473{
474 struct process_kallsyms_args args = { .map = map, .dso = self, };
9e201442 475 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
682b335a
ACM
476}
477
2e538c4a
ACM
478/*
479 * Split the symbols into maps, making sure there are no overlaps, i.e. the
480 * kernel range is broken in several maps, named [kernel].N, as we don't have
481 * the original ELF section names vmlinux have.
482 */
9958e1f0 483static int dso__split_kallsyms(struct dso *self, struct map *map,
9de89fe7 484 symbol_filter_t filter)
2e538c4a 485{
9de89fe7 486 struct map_groups *kmaps = map__kmap(map)->kmaps;
a1645ce1 487 struct kernel_info *kerninfo = kmaps->this_kerninfo;
4e06255f 488 struct map *curr_map = map;
2e538c4a
ACM
489 struct symbol *pos;
490 int count = 0;
4e06255f
ACM
491 struct rb_root *root = &self->symbols[map->type];
492 struct rb_node *next = rb_first(root);
2e538c4a
ACM
493 int kernel_range = 0;
494
495 while (next) {
496 char *module;
497
498 pos = rb_entry(next, struct symbol, rb_node);
499 next = rb_next(&pos->rb_node);
500
501 module = strchr(pos->name, '\t');
502 if (module) {
75be6cf4 503 if (!symbol_conf.use_modules)
1de8e245
ACM
504 goto discard_symbol;
505
2e538c4a
ACM
506 *module++ = '\0';
507
b7cece76 508 if (strcmp(curr_map->dso->short_name, module)) {
a1645ce1
ZY
509 if (curr_map != map &&
510 self->kernel == DSO_TYPE_GUEST_KERNEL &&
511 is_default_guest(kerninfo)) {
512 /*
513 * We assume all symbols of a module are
514 * continuous in * kallsyms, so curr_map
515 * points to a module and all its
516 * symbols are in its kmap. Mark it as
517 * loaded.
518 */
519 dso__set_loaded(curr_map->dso,
520 curr_map->type);
521 }
522
523 curr_map = map_groups__find_by_name(kmaps,
524 map->type, module);
4e06255f 525 if (curr_map == NULL) {
a1645ce1 526 pr_err("%s/proc/{kallsyms,modules} "
b7cece76 527 "inconsistency while looking "
a1645ce1
ZY
528 "for \"%s\" module!\n",
529 kerninfo->root_dir, module);
530 curr_map = map;
531 goto discard_symbol;
af427bf5 532 }
b7cece76 533
a1645ce1
ZY
534 if (curr_map->dso->loaded &&
535 !is_default_guest(kmaps->this_kerninfo))
b7cece76 536 goto discard_symbol;
af427bf5 537 }
2e538c4a
ACM
538 /*
539 * So that we look just like we get from .ko files,
540 * i.e. not prelinked, relative to map->start.
541 */
4e06255f
ACM
542 pos->start = curr_map->map_ip(curr_map, pos->start);
543 pos->end = curr_map->map_ip(curr_map, pos->end);
544 } else if (curr_map != map) {
2e538c4a
ACM
545 char dso_name[PATH_MAX];
546 struct dso *dso;
547
a1645ce1
ZY
548 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
549 snprintf(dso_name, sizeof(dso_name),
550 "[guest.kernel].%d",
551 kernel_range++);
552 else
553 snprintf(dso_name, sizeof(dso_name),
554 "[kernel].%d",
555 kernel_range++);
2e538c4a 556
00a192b3 557 dso = dso__new(dso_name);
2e538c4a
ACM
558 if (dso == NULL)
559 return -1;
560
a1645ce1
ZY
561 dso->kernel = self->kernel;
562
4e06255f 563 curr_map = map__new2(pos->start, dso, map->type);
37fe5fcb 564 if (curr_map == NULL) {
2e538c4a
ACM
565 dso__delete(dso);
566 return -1;
567 }
a2928c42 568
4e06255f 569 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
9de89fe7 570 map_groups__insert(kmaps, curr_map);
2e538c4a
ACM
571 ++kernel_range;
572 }
a2928c42 573
4e06255f 574 if (filter && filter(curr_map, pos)) {
1de8e245 575discard_symbol: rb_erase(&pos->rb_node, root);
00a192b3 576 symbol__delete(pos);
2e538c4a 577 } else {
4e06255f
ACM
578 if (curr_map != map) {
579 rb_erase(&pos->rb_node, root);
580 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
2e538c4a 581 }
9974f496
MG
582 count++;
583 }
a2928c42
ACM
584 }
585
a1645ce1
ZY
586 if (curr_map != map &&
587 self->kernel == DSO_TYPE_GUEST_KERNEL &&
588 is_default_guest(kmaps->this_kerninfo)) {
589 dso__set_loaded(curr_map->dso, curr_map->type);
590 }
591
9974f496 592 return count;
2e538c4a 593}
a2928c42 594
9de89fe7
ACM
595int dso__load_kallsyms(struct dso *self, const char *filename,
596 struct map *map, symbol_filter_t filter)
2e538c4a 597{
9e201442 598 if (dso__load_all_kallsyms(self, filename, map) < 0)
2e538c4a
ACM
599 return -1;
600
4e06255f 601 symbols__fixup_end(&self->symbols[map->type]);
a1645ce1
ZY
602 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
603 self->origin = DSO__ORIG_GUEST_KERNEL;
604 else
605 self->origin = DSO__ORIG_KERNEL;
2e538c4a 606
9de89fe7 607 return dso__split_kallsyms(self, map, filter);
af427bf5
ACM
608}
609
439d473b 610static int dso__load_perf_map(struct dso *self, struct map *map,
6beba7ad 611 symbol_filter_t filter)
80d496be
PE
612{
613 char *line = NULL;
614 size_t n;
615 FILE *file;
616 int nr_syms = 0;
617
439d473b 618 file = fopen(self->long_name, "r");
80d496be
PE
619 if (file == NULL)
620 goto out_failure;
621
622 while (!feof(file)) {
9cffa8d5 623 u64 start, size;
80d496be
PE
624 struct symbol *sym;
625 int line_len, len;
626
627 line_len = getline(&line, &n, file);
628 if (line_len < 0)
629 break;
630
631 if (!line)
632 goto out_failure;
633
634 line[--line_len] = '\0'; /* \n */
635
636 len = hex2u64(line, &start);
637
638 len++;
639 if (len + 2 >= line_len)
640 continue;
641
642 len += hex2u64(line + len, &size);
643
644 len++;
645 if (len + 2 >= line_len)
646 continue;
647
00a192b3 648 sym = symbol__new(start, size, line + len);
80d496be
PE
649
650 if (sym == NULL)
651 goto out_delete_line;
652
439d473b 653 if (filter && filter(map, sym))
00a192b3 654 symbol__delete(sym);
80d496be 655 else {
6a4694a4 656 symbols__insert(&self->symbols[map->type], sym);
80d496be
PE
657 nr_syms++;
658 }
659 }
660
661 free(line);
662 fclose(file);
663
664 return nr_syms;
665
666out_delete_line:
667 free(line);
668out_failure:
669 return -1;
670}
671
a2928c42
ACM
672/**
673 * elf_symtab__for_each_symbol - iterate thru all the symbols
674 *
675 * @self: struct elf_symtab instance to iterate
83a0944f 676 * @idx: uint32_t idx
a2928c42
ACM
677 * @sym: GElf_Sym iterator
678 */
83a0944f
IM
679#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
680 for (idx = 0, gelf_getsym(syms, idx, &sym);\
681 idx < nr_syms; \
682 idx++, gelf_getsym(syms, idx, &sym))
a2928c42
ACM
683
684static inline uint8_t elf_sym__type(const GElf_Sym *sym)
685{
686 return GELF_ST_TYPE(sym->st_info);
687}
688
689static inline int elf_sym__is_function(const GElf_Sym *sym)
690{
691 return elf_sym__type(sym) == STT_FUNC &&
692 sym->st_name != 0 &&
81833130 693 sym->st_shndx != SHN_UNDEF;
a2928c42
ACM
694}
695
f1dfa0b1
ACM
696static inline bool elf_sym__is_object(const GElf_Sym *sym)
697{
698 return elf_sym__type(sym) == STT_OBJECT &&
699 sym->st_name != 0 &&
700 sym->st_shndx != SHN_UNDEF;
701}
702
6cfcc53e
MG
703static inline int elf_sym__is_label(const GElf_Sym *sym)
704{
705 return elf_sym__type(sym) == STT_NOTYPE &&
706 sym->st_name != 0 &&
707 sym->st_shndx != SHN_UNDEF &&
708 sym->st_shndx != SHN_ABS;
709}
710
711static inline const char *elf_sec__name(const GElf_Shdr *shdr,
712 const Elf_Data *secstrs)
713{
714 return secstrs->d_buf + shdr->sh_name;
715}
716
717static inline int elf_sec__is_text(const GElf_Shdr *shdr,
718 const Elf_Data *secstrs)
719{
720 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
721}
722
f1dfa0b1
ACM
723static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
724 const Elf_Data *secstrs)
725{
726 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
727}
728
a2928c42
ACM
729static inline const char *elf_sym__name(const GElf_Sym *sym,
730 const Elf_Data *symstrs)
731{
732 return symstrs->d_buf + sym->st_name;
733}
734
735static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
736 GElf_Shdr *shp, const char *name,
83a0944f 737 size_t *idx)
a2928c42
ACM
738{
739 Elf_Scn *sec = NULL;
740 size_t cnt = 1;
741
742 while ((sec = elf_nextscn(elf, sec)) != NULL) {
743 char *str;
744
745 gelf_getshdr(sec, shp);
746 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
747 if (!strcmp(name, str)) {
83a0944f
IM
748 if (idx)
749 *idx = cnt;
a2928c42
ACM
750 break;
751 }
752 ++cnt;
753 }
754
755 return sec;
756}
757
8ce998d6
ACM
758#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
759 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
760 idx < nr_entries; \
761 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
762
763#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
764 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
765 idx < nr_entries; \
766 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
767
a25e46c4
ACM
768/*
769 * We need to check if we have a .dynsym, so that we can handle the
770 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
771 * .dynsym or .symtab).
772 * And always look at the original dso, not at debuginfo packages, that
773 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
774 */
82164161
ACM
775static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
776 symbol_filter_t filter)
8ce998d6
ACM
777{
778 uint32_t nr_rel_entries, idx;
779 GElf_Sym sym;
9cffa8d5 780 u64 plt_offset;
8ce998d6
ACM
781 GElf_Shdr shdr_plt;
782 struct symbol *f;
a25e46c4 783 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 784 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
785 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
786 size_t dynsym_idx;
787 GElf_Ehdr ehdr;
8ce998d6 788 char sympltname[1024];
a25e46c4
ACM
789 Elf *elf;
790 int nr = 0, symidx, fd, err = 0;
791
439d473b 792 fd = open(self->long_name, O_RDONLY);
a25e46c4
ACM
793 if (fd < 0)
794 goto out;
795
84087126 796 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a25e46c4
ACM
797 if (elf == NULL)
798 goto out_close;
799
800 if (gelf_getehdr(elf, &ehdr) == NULL)
801 goto out_elf_end;
802
803 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
804 ".dynsym", &dynsym_idx);
805 if (scn_dynsym == NULL)
806 goto out_elf_end;
8ce998d6 807
a25e46c4 808 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
809 ".rela.plt", NULL);
810 if (scn_plt_rel == NULL) {
a25e46c4 811 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
812 ".rel.plt", NULL);
813 if (scn_plt_rel == NULL)
a25e46c4 814 goto out_elf_end;
8ce998d6
ACM
815 }
816
a25e46c4
ACM
817 err = -1;
818
8ce998d6 819 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 820 goto out_elf_end;
8ce998d6 821
a25e46c4
ACM
822 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
823 goto out_elf_end;
8ce998d6
ACM
824
825 /*
83a0944f 826 * Fetch the relocation section to find the idxes to the GOT
8ce998d6
ACM
827 * and the symbols in the .dynsym they refer to.
828 */
829 reldata = elf_getdata(scn_plt_rel, NULL);
830 if (reldata == NULL)
a25e46c4 831 goto out_elf_end;
8ce998d6
ACM
832
833 syms = elf_getdata(scn_dynsym, NULL);
834 if (syms == NULL)
a25e46c4 835 goto out_elf_end;
8ce998d6 836
a25e46c4 837 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 838 if (scn_symstrs == NULL)
a25e46c4 839 goto out_elf_end;
8ce998d6
ACM
840
841 symstrs = elf_getdata(scn_symstrs, NULL);
842 if (symstrs == NULL)
a25e46c4 843 goto out_elf_end;
8ce998d6
ACM
844
845 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
846 plt_offset = shdr_plt.sh_offset;
847
848 if (shdr_rel_plt.sh_type == SHT_RELA) {
849 GElf_Rela pos_mem, *pos;
850
851 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
852 nr_rel_entries) {
853 symidx = GELF_R_SYM(pos->r_info);
854 plt_offset += shdr_plt.sh_entsize;
855 gelf_getsym(syms, symidx, &sym);
856 snprintf(sympltname, sizeof(sympltname),
857 "%s@plt", elf_sym__name(&sym, symstrs));
858
859 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
00a192b3 860 sympltname);
8ce998d6 861 if (!f)
a25e46c4 862 goto out_elf_end;
8ce998d6 863
82164161
ACM
864 if (filter && filter(map, f))
865 symbol__delete(f);
866 else {
6a4694a4 867 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
868 ++nr;
869 }
8ce998d6
ACM
870 }
871 } else if (shdr_rel_plt.sh_type == SHT_REL) {
872 GElf_Rel pos_mem, *pos;
873 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
874 nr_rel_entries) {
875 symidx = GELF_R_SYM(pos->r_info);
876 plt_offset += shdr_plt.sh_entsize;
877 gelf_getsym(syms, symidx, &sym);
878 snprintf(sympltname, sizeof(sympltname),
879 "%s@plt", elf_sym__name(&sym, symstrs));
880
881 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
00a192b3 882 sympltname);
8ce998d6 883 if (!f)
a25e46c4 884 goto out_elf_end;
8ce998d6 885
82164161
ACM
886 if (filter && filter(map, f))
887 symbol__delete(f);
888 else {
6a4694a4 889 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
890 ++nr;
891 }
8ce998d6 892 }
8ce998d6
ACM
893 }
894
a25e46c4
ACM
895 err = 0;
896out_elf_end:
897 elf_end(elf);
898out_close:
899 close(fd);
900
901 if (err == 0)
902 return nr;
903out:
fe2197b8
ACM
904 pr_debug("%s: problems reading %s PLT info.\n",
905 __func__, self->long_name);
a25e46c4 906 return 0;
8ce998d6
ACM
907}
908
d45868d3
ACM
909static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
910{
911 switch (type) {
912 case MAP__FUNCTION:
913 return elf_sym__is_function(self);
f1dfa0b1
ACM
914 case MAP__VARIABLE:
915 return elf_sym__is_object(self);
d45868d3
ACM
916 default:
917 return false;
918 }
919}
920
921static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
922{
923 switch (type) {
924 case MAP__FUNCTION:
925 return elf_sec__is_text(self, secstrs);
f1dfa0b1
ACM
926 case MAP__VARIABLE:
927 return elf_sec__is_data(self, secstrs);
d45868d3
ACM
928 default:
929 return false;
930 }
931}
932
9de89fe7
ACM
933static int dso__load_sym(struct dso *self, struct map *map, const char *name,
934 int fd, symbol_filter_t filter, int kmodule)
a2928c42 935{
9de89fe7 936 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
2e538c4a
ACM
937 struct map *curr_map = map;
938 struct dso *curr_dso = self;
6cfcc53e 939 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
940 uint32_t nr_syms;
941 int err = -1;
83a0944f 942 uint32_t idx;
a2928c42
ACM
943 GElf_Ehdr ehdr;
944 GElf_Shdr shdr;
945 Elf_Data *syms;
946 GElf_Sym sym;
a25e46c4 947 Elf_Scn *sec, *sec_strndx;
a2928c42 948 Elf *elf;
439d473b 949 int nr = 0;
a2928c42 950
84087126 951 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a2928c42 952 if (elf == NULL) {
6beba7ad 953 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
a2928c42
ACM
954 goto out_close;
955 }
956
957 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 958 pr_err("%s: cannot get elf header.\n", __func__);
a2928c42
ACM
959 goto out_elf_end;
960 }
961
962 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 963 if (sec == NULL) {
a25e46c4
ACM
964 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
965 if (sec == NULL)
8ce998d6 966 goto out_elf_end;
8ce998d6 967 }
a2928c42
ACM
968
969 syms = elf_getdata(sec, NULL);
970 if (syms == NULL)
971 goto out_elf_end;
972
973 sec = elf_getscn(elf, shdr.sh_link);
974 if (sec == NULL)
975 goto out_elf_end;
976
977 symstrs = elf_getdata(sec, NULL);
978 if (symstrs == NULL)
979 goto out_elf_end;
980
6cfcc53e
MG
981 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
982 if (sec_strndx == NULL)
983 goto out_elf_end;
984
985 secstrs = elf_getdata(sec_strndx, NULL);
9b30a26b 986 if (secstrs == NULL)
6cfcc53e
MG
987 goto out_elf_end;
988
a2928c42
ACM
989 nr_syms = shdr.sh_size / shdr.sh_entsize;
990
e9fbc9dc 991 memset(&sym, 0, sizeof(sym));
a1645ce1 992 if (self->kernel == DSO_TYPE_USER) {
d20ff6bd 993 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
994 elf_section_by_name(elf, &ehdr, &shdr,
995 ".gnu.prelink_undo",
996 NULL) != NULL);
d20ff6bd
MG
997 } else self->adjust_symbols = 0;
998
83a0944f 999 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
a2928c42 1000 struct symbol *f;
56b03f3c 1001 const char *elf_name = elf_sym__name(&sym, symstrs);
2e538c4a 1002 char *demangled = NULL;
6cfcc53e
MG
1003 int is_label = elf_sym__is_label(&sym);
1004 const char *section_name;
a2928c42 1005
9de89fe7
ACM
1006 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1007 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1008 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
56b03f3c 1009
d45868d3 1010 if (!is_label && !elf_sym__is_a(&sym, map->type))
a2928c42
ACM
1011 continue;
1012
1013 sec = elf_getscn(elf, sym.st_shndx);
1014 if (!sec)
1015 goto out_elf_end;
1016
1017 gelf_getshdr(sec, &shdr);
6cfcc53e 1018
d45868d3 1019 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
6cfcc53e
MG
1020 continue;
1021
1022 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f 1023
a1645ce1 1024 if (self->kernel != DSO_TYPE_USER || kmodule) {
2e538c4a
ACM
1025 char dso_name[PATH_MAX];
1026
1027 if (strcmp(section_name,
b63be8d7
ACM
1028 (curr_dso->short_name +
1029 self->short_name_len)) == 0)
2e538c4a
ACM
1030 goto new_symbol;
1031
1032 if (strcmp(section_name, ".text") == 0) {
1033 curr_map = map;
1034 curr_dso = self;
1035 goto new_symbol;
1036 }
1037
1038 snprintf(dso_name, sizeof(dso_name),
1039 "%s%s", self->short_name, section_name);
1040
9de89fe7 1041 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
2e538c4a
ACM
1042 if (curr_map == NULL) {
1043 u64 start = sym.st_value;
1044
1045 if (kmodule)
1046 start += map->start + shdr.sh_offset;
1047
00a192b3 1048 curr_dso = dso__new(dso_name);
2e538c4a
ACM
1049 if (curr_dso == NULL)
1050 goto out_elf_end;
a1645ce1 1051 curr_dso->kernel = self->kernel;
3610583c 1052 curr_map = map__new2(start, curr_dso,
6275ce2d 1053 map->type);
2e538c4a
ACM
1054 if (curr_map == NULL) {
1055 dso__delete(curr_dso);
1056 goto out_elf_end;
1057 }
ed52ce2e
ACM
1058 curr_map->map_ip = identity__map_ip;
1059 curr_map->unmap_ip = identity__map_ip;
b0a9ab62 1060 curr_dso->origin = self->origin;
9de89fe7 1061 map_groups__insert(kmap->kmaps, curr_map);
a1645ce1 1062 dsos__add(&self->node, curr_dso);
6275ce2d 1063 dso__set_loaded(curr_dso, map->type);
2e538c4a
ACM
1064 } else
1065 curr_dso = curr_map->dso;
1066
1067 goto new_symbol;
af427bf5
ACM
1068 }
1069
2e538c4a 1070 if (curr_dso->adjust_symbols) {
29a9f66d
ACM
1071 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1072 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1073 (u64)sym.st_value, (u64)shdr.sh_addr,
1074 (u64)shdr.sh_offset);
f5812a7a 1075 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 1076 }
28ac909b
ACM
1077 /*
1078 * We need to figure out if the object was created from C++ sources
1079 * DWARF DW_compile_unit has this, but we don't always have access
1080 * to it...
1081 */
83a0944f 1082 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 1083 if (demangled != NULL)
83a0944f 1084 elf_name = demangled;
2e538c4a 1085new_symbol:
00a192b3 1086 f = symbol__new(sym.st_value, sym.st_size, elf_name);
28ac909b 1087 free(demangled);
a2928c42
ACM
1088 if (!f)
1089 goto out_elf_end;
1090
2e538c4a 1091 if (filter && filter(curr_map, f))
00a192b3 1092 symbol__delete(f);
69ee69f6 1093 else {
6a4694a4 1094 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
1095 nr++;
1096 }
a2928c42
ACM
1097 }
1098
2e538c4a
ACM
1099 /*
1100 * For misannotated, zeroed, ASM function sizes.
1101 */
6275ce2d 1102 if (nr > 0) {
6a4694a4 1103 symbols__fixup_end(&self->symbols[map->type]);
6275ce2d
ACM
1104 if (kmap) {
1105 /*
1106 * We need to fixup this here too because we create new
1107 * maps here, for things like vsyscall sections.
1108 */
1109 __map_groups__fixup_end(kmap->kmaps, map->type);
1110 }
1111 }
a2928c42
ACM
1112 err = nr;
1113out_elf_end:
1114 elf_end(elf);
1115out_close:
1116 return err;
1117}
1118
78075caa
ACM
1119static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1120{
1121 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1122}
1123
a1645ce1 1124bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
57f395a7 1125{
e30a3d12 1126 bool have_build_id = false;
57f395a7
FW
1127 struct dso *pos;
1128
6122e4e4
ACM
1129 list_for_each_entry(pos, head, node) {
1130 if (with_hits && !pos->hit)
1131 continue;
e30a3d12
ACM
1132 if (filename__read_build_id(pos->long_name, pos->build_id,
1133 sizeof(pos->build_id)) > 0) {
1134 have_build_id = true;
1135 pos->has_build_id = true;
1136 }
6122e4e4 1137 }
57f395a7 1138
e30a3d12 1139 return have_build_id;
57f395a7
FW
1140}
1141
fd7a346e
ACM
1142/*
1143 * Align offset to 4 bytes as needed for note name and descriptor data.
1144 */
1145#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1146
2643ce11 1147int filename__read_build_id(const char *filename, void *bf, size_t size)
4d1e00a8 1148{
2643ce11 1149 int fd, err = -1;
4d1e00a8
ACM
1150 GElf_Ehdr ehdr;
1151 GElf_Shdr shdr;
fd7a346e 1152 Elf_Data *data;
4d1e00a8 1153 Elf_Scn *sec;
e57cfcda 1154 Elf_Kind ek;
fd7a346e 1155 void *ptr;
4d1e00a8 1156 Elf *elf;
4d1e00a8 1157
2643ce11
ACM
1158 if (size < BUILD_ID_SIZE)
1159 goto out;
1160
1161 fd = open(filename, O_RDONLY);
4d1e00a8
ACM
1162 if (fd < 0)
1163 goto out;
1164
84087126 1165 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
4d1e00a8 1166 if (elf == NULL) {
8d06367f 1167 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
4d1e00a8
ACM
1168 goto out_close;
1169 }
1170
e57cfcda
PE
1171 ek = elf_kind(elf);
1172 if (ek != ELF_K_ELF)
1173 goto out_elf_end;
1174
4d1e00a8 1175 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 1176 pr_err("%s: cannot get elf header.\n", __func__);
4d1e00a8
ACM
1177 goto out_elf_end;
1178 }
1179
2643ce11
ACM
1180 sec = elf_section_by_name(elf, &ehdr, &shdr,
1181 ".note.gnu.build-id", NULL);
fd7a346e
ACM
1182 if (sec == NULL) {
1183 sec = elf_section_by_name(elf, &ehdr, &shdr,
1184 ".notes", NULL);
1185 if (sec == NULL)
1186 goto out_elf_end;
1187 }
4d1e00a8 1188
fd7a346e
ACM
1189 data = elf_getdata(sec, NULL);
1190 if (data == NULL)
4d1e00a8 1191 goto out_elf_end;
fd7a346e
ACM
1192
1193 ptr = data->d_buf;
1194 while (ptr < (data->d_buf + data->d_size)) {
1195 GElf_Nhdr *nhdr = ptr;
1196 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1197 descsz = NOTE_ALIGN(nhdr->n_descsz);
1198 const char *name;
1199
1200 ptr += sizeof(*nhdr);
1201 name = ptr;
1202 ptr += namesz;
1203 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1204 nhdr->n_namesz == sizeof("GNU")) {
1205 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1206 memcpy(bf, ptr, BUILD_ID_SIZE);
1207 err = BUILD_ID_SIZE;
1208 break;
1209 }
1210 }
1211 ptr += descsz;
1212 }
2643ce11
ACM
1213out_elf_end:
1214 elf_end(elf);
1215out_close:
1216 close(fd);
1217out:
1218 return err;
1219}
1220
f1617b40
ACM
1221int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1222{
1223 int fd, err = -1;
1224
1225 if (size < BUILD_ID_SIZE)
1226 goto out;
1227
1228 fd = open(filename, O_RDONLY);
1229 if (fd < 0)
1230 goto out;
1231
1232 while (1) {
1233 char bf[BUFSIZ];
1234 GElf_Nhdr nhdr;
1235 int namesz, descsz;
1236
1237 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1238 break;
1239
fd7a346e
ACM
1240 namesz = NOTE_ALIGN(nhdr.n_namesz);
1241 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1242 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1243 nhdr.n_namesz == sizeof("GNU")) {
1244 if (read(fd, bf, namesz) != namesz)
1245 break;
1246 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1247 if (read(fd, build_id,
1248 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1249 err = 0;
1250 break;
1251 }
1252 } else if (read(fd, bf, descsz) != descsz)
1253 break;
1254 } else {
1255 int n = namesz + descsz;
1256 if (read(fd, bf, n) != n)
1257 break;
1258 }
1259 }
1260 close(fd);
1261out:
1262 return err;
1263}
1264
94cb9e38
ACM
1265char dso__symtab_origin(const struct dso *self)
1266{
1267 static const char origin[] = {
1268 [DSO__ORIG_KERNEL] = 'k',
1269 [DSO__ORIG_JAVA_JIT] = 'j',
4cf40131 1270 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
94cb9e38
ACM
1271 [DSO__ORIG_FEDORA] = 'f',
1272 [DSO__ORIG_UBUNTU] = 'u',
1273 [DSO__ORIG_BUILDID] = 'b',
1274 [DSO__ORIG_DSO] = 'd',
439d473b 1275 [DSO__ORIG_KMODULE] = 'K',
a1645ce1
ZY
1276 [DSO__ORIG_GUEST_KERNEL] = 'g',
1277 [DSO__ORIG_GUEST_KMODULE] = 'G',
94cb9e38
ACM
1278 };
1279
1280 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1281 return '!';
1282 return origin[self->origin];
1283}
1284
9de89fe7 1285int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
a2928c42 1286{
4d1e00a8 1287 int size = PATH_MAX;
c338aee8 1288 char *name;
d3379ab9 1289 u8 build_id[BUILD_ID_SIZE];
4cf40131 1290 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
a2928c42
ACM
1291 int ret = -1;
1292 int fd;
a1645ce1
ZY
1293 struct kernel_info *kerninfo;
1294 const char *root_dir;
a2928c42 1295
3610583c 1296 dso__set_loaded(self, map->type);
66bd8424 1297
a1645ce1 1298 if (self->kernel == DSO_TYPE_KERNEL)
9de89fe7 1299 return dso__load_kernel_sym(self, map, filter);
a1645ce1
ZY
1300 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1301 return dso__load_guest_kernel_sym(self, map, filter);
1302
1303 if (map->groups && map->groups->this_kerninfo)
1304 kerninfo = map->groups->this_kerninfo;
1305 else
1306 kerninfo = NULL;
c338aee8
ACM
1307
1308 name = malloc(size);
a2928c42
ACM
1309 if (!name)
1310 return -1;
1311
30d7a77d 1312 self->adjust_symbols = 0;
f5812a7a 1313
94cb9e38 1314 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
6beba7ad 1315 ret = dso__load_perf_map(self, map, filter);
94cb9e38
ACM
1316 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1317 DSO__ORIG_NOT_FOUND;
1318 return ret;
1319 }
1320
4cf40131 1321 self->origin = DSO__ORIG_BUILD_ID_CACHE;
80d496be 1322
4cf40131
ACM
1323 if (self->has_build_id) {
1324 build_id__sprintf(self->build_id, sizeof(self->build_id),
1325 build_id_hex);
1326 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1327 getenv("HOME"), DEBUG_CACHE_DIR,
1328 build_id_hex, build_id_hex + 2);
1329 goto open_file;
1330 }
a2928c42
ACM
1331more:
1332 do {
94cb9e38
ACM
1333 self->origin++;
1334 switch (self->origin) {
1335 case DSO__ORIG_FEDORA:
439d473b
ACM
1336 snprintf(name, size, "/usr/lib/debug%s.debug",
1337 self->long_name);
a2928c42 1338 break;
94cb9e38 1339 case DSO__ORIG_UBUNTU:
439d473b
ACM
1340 snprintf(name, size, "/usr/lib/debug%s",
1341 self->long_name);
a2928c42 1342 break;
94cb9e38 1343 case DSO__ORIG_BUILDID:
d3379ab9
ACM
1344 if (filename__read_build_id(self->long_name, build_id,
1345 sizeof(build_id))) {
d3379ab9
ACM
1346 build_id__sprintf(build_id, sizeof(build_id),
1347 build_id_hex);
4d1e00a8
ACM
1348 snprintf(name, size,
1349 "/usr/lib/debug/.build-id/%.2s/%s.debug",
d3379ab9
ACM
1350 build_id_hex, build_id_hex + 2);
1351 if (self->has_build_id)
1352 goto compare_build_id;
1353 break;
4d1e00a8 1354 }
94cb9e38 1355 self->origin++;
4d1e00a8 1356 /* Fall thru */
94cb9e38 1357 case DSO__ORIG_DSO:
439d473b 1358 snprintf(name, size, "%s", self->long_name);
a2928c42 1359 break;
a1645ce1
ZY
1360 case DSO__ORIG_GUEST_KMODULE:
1361 if (map->groups && map->groups->this_kerninfo)
1362 root_dir = map->groups->this_kerninfo->root_dir;
1363 else
1364 root_dir = "";
1365 snprintf(name, size, "%s%s", root_dir, self->long_name);
1366 break;
a2928c42
ACM
1367
1368 default:
1369 goto out;
1370 }
a2928c42 1371
8d06367f 1372 if (self->has_build_id) {
d3379ab9
ACM
1373 if (filename__read_build_id(name, build_id,
1374 sizeof(build_id)) < 0)
8d06367f 1375 goto more;
8d06367f 1376compare_build_id:
78075caa 1377 if (!dso__build_id_equal(self, build_id))
8d06367f
ACM
1378 goto more;
1379 }
4cf40131 1380open_file:
a2928c42
ACM
1381 fd = open(name, O_RDONLY);
1382 } while (fd < 0);
1383
9de89fe7 1384 ret = dso__load_sym(self, map, name, fd, filter, 0);
a2928c42
ACM
1385 close(fd);
1386
1387 /*
1388 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1389 */
1390 if (!ret)
1391 goto more;
1392
a25e46c4 1393 if (ret > 0) {
82164161 1394 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
a25e46c4
ACM
1395 if (nr_plt > 0)
1396 ret += nr_plt;
1397 }
a2928c42
ACM
1398out:
1399 free(name);
1340e6bb
ACM
1400 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1401 return 0;
a2928c42
ACM
1402 return ret;
1403}
1404
79406cd7
ACM
1405struct map *map_groups__find_by_name(struct map_groups *self,
1406 enum map_type type, const char *name)
439d473b
ACM
1407{
1408 struct rb_node *nd;
1409
79406cd7 1410 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1411 struct map *map = rb_entry(nd, struct map, rb_node);
1412
b7cece76 1413 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1414 return map;
1415 }
1416
1417 return NULL;
1418}
1419
a1645ce1
ZY
1420static int dso__kernel_module_get_build_id(struct dso *self,
1421 const char *root_dir)
b7cece76
ACM
1422{
1423 char filename[PATH_MAX];
1424 /*
1425 * kernel module short names are of the form "[module]" and
1426 * we need just "module" here.
1427 */
1428 const char *name = self->short_name + 1;
1429
1430 snprintf(filename, sizeof(filename),
a1645ce1
ZY
1431 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1432 root_dir, (int)strlen(name) - 1, name);
b7cece76
ACM
1433
1434 if (sysfs__read_build_id(filename, self->build_id,
1435 sizeof(self->build_id)) == 0)
1436 self->has_build_id = true;
1437
1438 return 0;
1439}
1440
a1645ce1
ZY
1441static int map_groups__set_modules_path_dir(struct map_groups *self,
1442 const char *dir_name)
6cfcc53e 1443{
439d473b 1444 struct dirent *dent;
5aab621b 1445 DIR *dir = opendir(dir_name);
6cfcc53e 1446
439d473b 1447 if (!dir) {
5aab621b 1448 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
439d473b
ACM
1449 return -1;
1450 }
6cfcc53e 1451
439d473b
ACM
1452 while ((dent = readdir(dir)) != NULL) {
1453 char path[PATH_MAX];
a1645ce1
ZY
1454 struct stat st;
1455
1456 /*sshfs might return bad dent->d_type, so we have to stat*/
1457 sprintf(path, "%s/%s", dir_name, dent->d_name);
1458 if (stat(path, &st))
1459 continue;
439d473b 1460
a1645ce1 1461 if (S_ISDIR(st.st_mode)) {
439d473b
ACM
1462 if (!strcmp(dent->d_name, ".") ||
1463 !strcmp(dent->d_name, ".."))
1464 continue;
1465
1466 snprintf(path, sizeof(path), "%s/%s",
5aab621b 1467 dir_name, dent->d_name);
9de89fe7 1468 if (map_groups__set_modules_path_dir(self, path) < 0)
439d473b
ACM
1469 goto failure;
1470 } else {
1471 char *dot = strrchr(dent->d_name, '.'),
1472 dso_name[PATH_MAX];
1473 struct map *map;
cfc10d3b 1474 char *long_name;
439d473b
ACM
1475
1476 if (dot == NULL || strcmp(dot, ".ko"))
1477 continue;
1478 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1479 (int)(dot - dent->d_name), dent->d_name);
1480
a2a99e8e 1481 strxfrchar(dso_name, '-', '_');
9de89fe7 1482 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
439d473b
ACM
1483 if (map == NULL)
1484 continue;
1485
1486 snprintf(path, sizeof(path), "%s/%s",
5aab621b 1487 dir_name, dent->d_name);
439d473b 1488
cfc10d3b
ACM
1489 long_name = strdup(path);
1490 if (long_name == NULL)
439d473b 1491 goto failure;
cfc10d3b 1492 dso__set_long_name(map->dso, long_name);
a1645ce1 1493 dso__kernel_module_get_build_id(map->dso, "");
439d473b 1494 }
439d473b 1495 }
6cfcc53e 1496
c338aee8 1497 return 0;
439d473b
ACM
1498failure:
1499 closedir(dir);
1500 return -1;
1501}
6cfcc53e 1502
a1645ce1 1503static char *get_kernel_version(const char *root_dir)
439d473b 1504{
a1645ce1
ZY
1505 char version[PATH_MAX];
1506 FILE *file;
1507 char *name, *tmp;
1508 const char *prefix = "Linux version ";
1509
1510 sprintf(version, "%s/proc/version", root_dir);
1511 file = fopen(version, "r");
1512 if (!file)
1513 return NULL;
1514
1515 version[0] = '\0';
1516 tmp = fgets(version, sizeof(version), file);
1517 fclose(file);
1518
1519 name = strstr(version, prefix);
1520 if (!name)
1521 return NULL;
1522 name += strlen(prefix);
1523 tmp = strchr(name, ' ');
1524 if (tmp)
1525 *tmp = '\0';
1526
1527 return strdup(name);
1528}
1529
1530static int map_groups__set_modules_path(struct map_groups *self,
1531 const char *root_dir)
1532{
1533 char *version;
439d473b 1534 char modules_path[PATH_MAX];
6cfcc53e 1535
a1645ce1
ZY
1536 version = get_kernel_version(root_dir);
1537 if (!version)
439d473b 1538 return -1;
6cfcc53e 1539
a1645ce1
ZY
1540 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1541 root_dir, version);
1542 free(version);
6cfcc53e 1543
9de89fe7 1544 return map_groups__set_modules_path_dir(self, modules_path);
6cfcc53e
MG
1545}
1546
439d473b
ACM
1547/*
1548 * Constructor variant for modules (where we know from /proc/modules where
1549 * they are loaded) and for vmlinux, where only after we load all the
1550 * symbols we'll know where it starts and ends.
1551 */
3610583c 1552static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 1553{
5aab621b
ACM
1554 struct map *self = calloc(1, (sizeof(*self) +
1555 (dso->kernel ? sizeof(struct kmap) : 0)));
439d473b 1556 if (self != NULL) {
439d473b 1557 /*
afb7b4f0 1558 * ->end will be filled after we load all the symbols
439d473b 1559 */
3610583c 1560 map__init(self, type, start, 0, 0, dso);
439d473b 1561 }
afb7b4f0 1562
439d473b
ACM
1563 return self;
1564}
1565
9de89fe7 1566struct map *map_groups__new_module(struct map_groups *self, u64 start,
a1645ce1
ZY
1567 const char *filename,
1568 struct kernel_info *kerninfo)
b7cece76
ACM
1569{
1570 struct map *map;
a1645ce1 1571 struct dso *dso;
b7cece76 1572
a1645ce1 1573 dso = __dsos__findnew(&kerninfo->dsos__kernel, filename);
b7cece76
ACM
1574 if (dso == NULL)
1575 return NULL;
1576
1577 map = map__new2(start, dso, MAP__FUNCTION);
1578 if (map == NULL)
1579 return NULL;
1580
a1645ce1
ZY
1581 if (is_host_kernel(kerninfo))
1582 dso->origin = DSO__ORIG_KMODULE;
1583 else
1584 dso->origin = DSO__ORIG_GUEST_KMODULE;
9de89fe7 1585 map_groups__insert(self, map);
b7cece76
ACM
1586 return map;
1587}
1588
a1645ce1 1589static int map_groups__create_modules(struct kernel_info *kerninfo)
439d473b
ACM
1590{
1591 char *line = NULL;
1592 size_t n;
a1645ce1 1593 FILE *file;
439d473b 1594 struct map *map;
a1645ce1
ZY
1595 const char *root_dir;
1596 const char *modules;
1597 char path[PATH_MAX];
1598
1599 if (is_default_guest(kerninfo))
1600 modules = symbol_conf.default_guest_modules;
1601 else {
1602 sprintf(path, "%s/proc/modules", kerninfo->root_dir);
1603 modules = path;
1604 }
6cfcc53e 1605
a1645ce1 1606 file = fopen(modules, "r");
439d473b
ACM
1607 if (file == NULL)
1608 return -1;
6cfcc53e 1609
a1645ce1
ZY
1610 root_dir = kerninfo->root_dir;
1611
439d473b
ACM
1612 while (!feof(file)) {
1613 char name[PATH_MAX];
1614 u64 start;
439d473b
ACM
1615 char *sep;
1616 int line_len;
6cfcc53e 1617
439d473b
ACM
1618 line_len = getline(&line, &n, file);
1619 if (line_len < 0)
1620 break;
1621
1622 if (!line)
1623 goto out_failure;
1624
1625 line[--line_len] = '\0'; /* \n */
1626
1627 sep = strrchr(line, 'x');
1628 if (sep == NULL)
1629 continue;
1630
1631 hex2u64(sep + 1, &start);
1632
1633 sep = strchr(line, ' ');
1634 if (sep == NULL)
1635 continue;
1636
1637 *sep = '\0';
1638
1639 snprintf(name, sizeof(name), "[%s]", line);
a1645ce1
ZY
1640 map = map_groups__new_module(&kerninfo->kmaps,
1641 start, name, kerninfo);
b7cece76 1642 if (map == NULL)
439d473b 1643 goto out_delete_line;
a1645ce1 1644 dso__kernel_module_get_build_id(map->dso, root_dir);
6cfcc53e 1645 }
439d473b
ACM
1646
1647 free(line);
1648 fclose(file);
1649
a1645ce1 1650 return map_groups__set_modules_path(&kerninfo->kmaps, root_dir);
439d473b
ACM
1651
1652out_delete_line:
1653 free(line);
1654out_failure:
1655 return -1;
6cfcc53e
MG
1656}
1657
9958e1f0 1658static int dso__load_vmlinux(struct dso *self, struct map *map,
6beba7ad 1659 const char *vmlinux, symbol_filter_t filter)
a2928c42 1660{
fbd733b8 1661 int err = -1, fd;
a2928c42 1662
fbd733b8
ACM
1663 if (self->has_build_id) {
1664 u8 build_id[BUILD_ID_SIZE];
1665
1666 if (filename__read_build_id(vmlinux, build_id,
1667 sizeof(build_id)) < 0) {
1668 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1669 return -1;
1670 }
1671 if (!dso__build_id_equal(self, build_id)) {
1672 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1673 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1674
1675 build_id__sprintf(self->build_id,
1676 sizeof(self->build_id),
1677 expected_build_id);
1678 build_id__sprintf(build_id, sizeof(build_id),
1679 vmlinux_build_id);
1680 pr_debug("build_id in %s is %s while expected is %s, "
1681 "ignoring it\n", vmlinux, vmlinux_build_id,
1682 expected_build_id);
1683 return -1;
1684 }
1685 }
66bd8424 1686
fbd733b8 1687 fd = open(vmlinux, O_RDONLY);
a2928c42
ACM
1688 if (fd < 0)
1689 return -1;
1690
3610583c 1691 dso__set_loaded(self, map->type);
9de89fe7 1692 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
a2928c42
ACM
1693 close(fd);
1694
3846df2e
ACM
1695 if (err > 0)
1696 pr_debug("Using %s for symbols\n", vmlinux);
1697
a2928c42
ACM
1698 return err;
1699}
1700
a19afe46 1701int dso__load_vmlinux_path(struct dso *self, struct map *map,
9de89fe7 1702 symbol_filter_t filter)
a19afe46
ACM
1703{
1704 int i, err = 0;
1705
1706 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1707 vmlinux_path__nr_entries);
1708
1709 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
9de89fe7 1710 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
a19afe46 1711 if (err > 0) {
a19afe46
ACM
1712 dso__set_long_name(self, strdup(vmlinux_path[i]));
1713 break;
1714 }
1715 }
1716
1717 return err;
1718}
1719
c338aee8 1720static int dso__load_kernel_sym(struct dso *self, struct map *map,
9de89fe7 1721 symbol_filter_t filter)
a827c875 1722{
cc612d81 1723 int err;
9e201442
ACM
1724 const char *kallsyms_filename = NULL;
1725 char *kallsyms_allocated_filename = NULL;
dc8d6ab2
ACM
1726 /*
1727 * Step 1: if the user specified a vmlinux filename, use it and only
1728 * it, reporting errors to the user if it cannot be used.
1729 *
1730 * For instance, try to analyse an ARM perf.data file _without_ a
1731 * build-id, or if the user specifies the wrong path to the right
1732 * vmlinux file, obviously we can't fallback to another vmlinux (a
1733 * x86_86 one, on the machine where analysis is being performed, say),
1734 * or worse, /proc/kallsyms.
1735 *
1736 * If the specified file _has_ a build-id and there is a build-id
1737 * section in the perf.data file, we will still do the expected
1738 * validation in dso__load_vmlinux and will bail out if they don't
1739 * match.
1740 */
1741 if (symbol_conf.vmlinux_name != NULL) {
9de89fe7 1742 err = dso__load_vmlinux(self, map,
dc8d6ab2
ACM
1743 symbol_conf.vmlinux_name, filter);
1744 goto out_try_fixup;
1745 }
cc612d81
ACM
1746
1747 if (vmlinux_path != NULL) {
9de89fe7 1748 err = dso__load_vmlinux_path(self, map, filter);
a19afe46
ACM
1749 if (err > 0)
1750 goto out_fixup;
cc612d81
ACM
1751 }
1752
b7cece76
ACM
1753 /*
1754 * Say the kernel DSO was created when processing the build-id header table,
1755 * we have a build-id, so check if it is the same as the running kernel,
1756 * using it if it is.
1757 */
1758 if (self->has_build_id) {
1759 u8 kallsyms_build_id[BUILD_ID_SIZE];
9e201442 1760 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
b7cece76
ACM
1761
1762 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
8d0591f6 1763 sizeof(kallsyms_build_id)) == 0) {
9e201442
ACM
1764 if (dso__build_id_equal(self, kallsyms_build_id)) {
1765 kallsyms_filename = "/proc/kallsyms";
8d0591f6 1766 goto do_kallsyms;
9e201442 1767 }
8d0591f6 1768 }
dc8d6ab2
ACM
1769 /*
1770 * Now look if we have it on the build-id cache in
1771 * $HOME/.debug/[kernel.kallsyms].
1772 */
9e201442
ACM
1773 build_id__sprintf(self->build_id, sizeof(self->build_id),
1774 sbuild_id);
1775
1776 if (asprintf(&kallsyms_allocated_filename,
1777 "%s/.debug/[kernel.kallsyms]/%s",
3846df2e
ACM
1778 getenv("HOME"), sbuild_id) == -1) {
1779 pr_err("Not enough memory for kallsyms file lookup\n");
dc8d6ab2 1780 return -1;
3846df2e 1781 }
dc8d6ab2 1782
19fc2ded
ACM
1783 kallsyms_filename = kallsyms_allocated_filename;
1784
dc8d6ab2 1785 if (access(kallsyms_filename, F_OK)) {
3846df2e
ACM
1786 pr_err("No kallsyms or vmlinux with build-id %s "
1787 "was found\n", sbuild_id);
9e201442 1788 free(kallsyms_allocated_filename);
dc8d6ab2 1789 return -1;
9e201442 1790 }
dc8d6ab2
ACM
1791 } else {
1792 /*
1793 * Last resort, if we don't have a build-id and couldn't find
1794 * any vmlinux file, try the running kernel kallsyms table.
1795 */
9e201442 1796 kallsyms_filename = "/proc/kallsyms";
9e201442 1797 }
439d473b 1798
cc612d81 1799do_kallsyms:
9de89fe7 1800 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
3846df2e
ACM
1801 if (err > 0)
1802 pr_debug("Using %s for symbols\n", kallsyms_filename);
dc8d6ab2 1803 free(kallsyms_allocated_filename);
439d473b 1804
dc8d6ab2 1805out_try_fixup:
439d473b 1806 if (err > 0) {
cc612d81 1807out_fixup:
e1c7c6a4 1808 if (kallsyms_filename != NULL)
dc8d6ab2 1809 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
6a4694a4
ACM
1810 map__fixup_start(map);
1811 map__fixup_end(map);
439d473b 1812 }
94cb9e38 1813
a827c875
ACM
1814 return err;
1815}
1816
a1645ce1
ZY
1817static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1818 symbol_filter_t filter)
1819{
1820 int err;
1821 const char *kallsyms_filename = NULL;
1822 struct kernel_info *kerninfo;
1823 char path[PATH_MAX];
1824
1825 if (!map->groups) {
1826 pr_debug("Guest kernel map hasn't the point to groups\n");
1827 return -1;
1828 }
1829 kerninfo = map->groups->this_kerninfo;
1830
1831 if (is_default_guest(kerninfo)) {
1832 /*
1833 * if the user specified a vmlinux filename, use it and only
1834 * it, reporting errors to the user if it cannot be used.
1835 * Or use file guest_kallsyms inputted by user on commandline
1836 */
1837 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1838 err = dso__load_vmlinux(self, map,
1839 symbol_conf.default_guest_vmlinux_name, filter);
1840 goto out_try_fixup;
1841 }
1842
1843 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1844 if (!kallsyms_filename)
1845 return -1;
1846 } else {
1847 sprintf(path, "%s/proc/kallsyms", kerninfo->root_dir);
1848 kallsyms_filename = path;
1849 }
1850
1851 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1852 if (err > 0)
1853 pr_debug("Using %s for symbols\n", kallsyms_filename);
1854
1855out_try_fixup:
1856 if (err > 0) {
1857 if (kallsyms_filename != NULL) {
1858 kern_mmap_name(kerninfo, path);
1859 dso__set_long_name(self,
1860 strdup(path));
1861 }
1862 map__fixup_start(map);
1863 map__fixup_end(map);
1864 }
1865
1866 return err;
1867}
cd84c2ac 1868
b0da954a 1869static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 1870{
b0da954a 1871 list_add_tail(&dso->node, head);
cd84c2ac
FW
1872}
1873
b0da954a 1874static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
1875{
1876 struct dso *pos;
1877
b0da954a 1878 list_for_each_entry(pos, head, node)
cf4e5b08 1879 if (strcmp(pos->long_name, name) == 0)
cd84c2ac
FW
1880 return pos;
1881 return NULL;
1882}
1883
a89e5abe 1884struct dso *__dsos__findnew(struct list_head *head, const char *name)
cd84c2ac 1885{
a89e5abe 1886 struct dso *dso = dsos__find(head, name);
cd84c2ac 1887
e4204992 1888 if (!dso) {
00a192b3 1889 dso = dso__new(name);
cfc10d3b 1890 if (dso != NULL) {
a89e5abe 1891 dsos__add(head, dso);
cfc10d3b
ACM
1892 dso__set_basename(dso);
1893 }
66bd8424 1894 }
cd84c2ac
FW
1895
1896 return dso;
cd84c2ac
FW
1897}
1898
b0da954a 1899static void __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
1900{
1901 struct dso *pos;
1902
95011c60
ACM
1903 list_for_each_entry(pos, head, node) {
1904 int i;
1905 for (i = 0; i < MAP__NR_TYPES; ++i)
1906 dso__fprintf(pos, i, fp);
1907 }
cd84c2ac
FW
1908}
1909
a1645ce1 1910void dsos__fprintf(struct rb_root *kerninfo_root, FILE *fp)
b0da954a 1911{
a1645ce1
ZY
1912 struct rb_node *nd;
1913
1914 for (nd = rb_first(kerninfo_root); nd; nd = rb_next(nd)) {
1915 struct kernel_info *pos = rb_entry(nd, struct kernel_info,
1916 rb_node);
1917 __dsos__fprintf(&pos->dsos__kernel, fp);
1918 __dsos__fprintf(&pos->dsos__user, fp);
1919 }
b0da954a
ACM
1920}
1921
88d3d9b7
ACM
1922static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1923 bool with_hits)
9e03eb2d
ACM
1924{
1925 struct dso *pos;
1926 size_t ret = 0;
1927
b0da954a 1928 list_for_each_entry(pos, head, node) {
88d3d9b7
ACM
1929 if (with_hits && !pos->hit)
1930 continue;
9e03eb2d 1931 ret += dso__fprintf_buildid(pos, fp);
1124ba73 1932 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
1933 }
1934 return ret;
1935}
1936
a1645ce1
ZY
1937size_t dsos__fprintf_buildid(struct rb_root *kerninfo_root,
1938 FILE *fp, bool with_hits)
b0da954a 1939{
a1645ce1
ZY
1940 struct rb_node *nd;
1941 size_t ret = 0;
1942
1943 for (nd = rb_first(kerninfo_root); nd; nd = rb_next(nd)) {
1944 struct kernel_info *pos = rb_entry(nd, struct kernel_info,
1945 rb_node);
1946 ret += __dsos__fprintf_buildid(&pos->dsos__kernel,
1947 fp, with_hits);
1948 ret += __dsos__fprintf_buildid(&pos->dsos__user,
1949 fp, with_hits);
1950 }
1951 return ret;
b0da954a
ACM
1952}
1953
fd1d908c
ACM
1954struct dso *dso__new_kernel(const char *name)
1955{
1956 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1957
1958 if (self != NULL) {
b63be8d7 1959 dso__set_short_name(self, "[kernel]");
a1645ce1
ZY
1960 self->kernel = DSO_TYPE_KERNEL;
1961 }
1962
1963 return self;
1964}
1965
1966static struct dso *dso__new_guest_kernel(struct kernel_info *kerninfo,
1967 const char *name)
1968{
1969 char buff[PATH_MAX];
1970 struct dso *self;
1971
1972 kern_mmap_name(kerninfo, buff);
1973 self = dso__new(name ?: buff);
1974 if (self != NULL) {
1975 dso__set_short_name(self, "[guest.kernel]");
1976 self->kernel = DSO_TYPE_GUEST_KERNEL;
fd1d908c
ACM
1977 }
1978
1979 return self;
1980}
1981
a1645ce1
ZY
1982void dso__read_running_kernel_build_id(struct dso *self,
1983 struct kernel_info *kerninfo)
fd1d908c 1984{
a1645ce1
ZY
1985 char path[PATH_MAX];
1986
1987 if (is_default_guest(kerninfo))
1988 return;
1989 sprintf(path, "%s/sys/kernel/notes", kerninfo->root_dir);
1990 if (sysfs__read_build_id(path, self->build_id,
fd1d908c
ACM
1991 sizeof(self->build_id)) == 0)
1992 self->has_build_id = true;
1993}
1994
a1645ce1 1995static struct dso *dsos__create_kernel(struct kernel_info *kerninfo)
cd84c2ac 1996{
a1645ce1
ZY
1997 const char *vmlinux_name = NULL;
1998 struct dso *kernel;
cd84c2ac 1999
a1645ce1
ZY
2000 if (is_host_kernel(kerninfo)) {
2001 vmlinux_name = symbol_conf.vmlinux_name;
2002 kernel = dso__new_kernel(vmlinux_name);
2003 } else {
2004 if (is_default_guest(kerninfo))
2005 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2006 kernel = dso__new_guest_kernel(kerninfo, vmlinux_name);
8d92c02a 2007 }
cd84c2ac 2008
a1645ce1
ZY
2009 if (kernel != NULL) {
2010 dso__read_running_kernel_build_id(kernel, kerninfo);
2011 dsos__add(&kerninfo->dsos__kernel, kernel);
2012 }
f1dfa0b1 2013 return kernel;
f1dfa0b1
ACM
2014}
2015
b7cece76
ACM
2016int __map_groups__create_kernel_maps(struct map_groups *self,
2017 struct map *vmlinux_maps[MAP__NR_TYPES],
2018 struct dso *kernel)
f1dfa0b1 2019{
de176489 2020 enum map_type type;
f1dfa0b1 2021
de176489 2022 for (type = 0; type < MAP__NR_TYPES; ++type) {
9de89fe7
ACM
2023 struct kmap *kmap;
2024
de176489
ACM
2025 vmlinux_maps[type] = map__new2(0, kernel, type);
2026 if (vmlinux_maps[type] == NULL)
2027 return -1;
f1dfa0b1 2028
de176489
ACM
2029 vmlinux_maps[type]->map_ip =
2030 vmlinux_maps[type]->unmap_ip = identity__map_ip;
9de89fe7
ACM
2031
2032 kmap = map__kmap(vmlinux_maps[type]);
2033 kmap->kmaps = self;
de176489 2034 map_groups__insert(self, vmlinux_maps[type]);
f1dfa0b1
ACM
2035 }
2036
f1dfa0b1 2037 return 0;
2446042c
ACM
2038}
2039
cc612d81
ACM
2040static void vmlinux_path__exit(void)
2041{
2042 while (--vmlinux_path__nr_entries >= 0) {
2043 free(vmlinux_path[vmlinux_path__nr_entries]);
2044 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2045 }
2046
2047 free(vmlinux_path);
2048 vmlinux_path = NULL;
2049}
2050
2051static int vmlinux_path__init(void)
2052{
2053 struct utsname uts;
2054 char bf[PATH_MAX];
2055
2056 if (uname(&uts) < 0)
2057 return -1;
2058
2059 vmlinux_path = malloc(sizeof(char *) * 5);
2060 if (vmlinux_path == NULL)
2061 return -1;
2062
2063 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2064 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2065 goto out_fail;
2066 ++vmlinux_path__nr_entries;
2067 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2068 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2069 goto out_fail;
2070 ++vmlinux_path__nr_entries;
2071 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2072 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2073 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2074 goto out_fail;
2075 ++vmlinux_path__nr_entries;
2076 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2077 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2078 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2079 goto out_fail;
2080 ++vmlinux_path__nr_entries;
2081 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2082 uts.release);
2083 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2084 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2085 goto out_fail;
2086 ++vmlinux_path__nr_entries;
2087
2088 return 0;
2089
2090out_fail:
2091 vmlinux_path__exit();
2092 return -1;
2093}
2094
b0a9ab62
ACM
2095size_t vmlinux_path__fprintf(FILE *fp)
2096{
2097 int i;
2098 size_t printed = 0;
2099
2100 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2101 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
2102
2103 return printed;
2104}
2105
655000e7
ACM
2106static int setup_list(struct strlist **list, const char *list_str,
2107 const char *list_name)
2108{
2109 if (list_str == NULL)
2110 return 0;
2111
2112 *list = strlist__new(true, list_str);
2113 if (!*list) {
2114 pr_err("problems parsing %s list\n", list_name);
2115 return -1;
2116 }
2117 return 0;
2118}
2119
75be6cf4 2120int symbol__init(void)
2446042c 2121{
95011c60 2122 elf_version(EV_CURRENT);
75be6cf4
ACM
2123 if (symbol_conf.sort_by_name)
2124 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2125 sizeof(struct symbol));
b32d133a 2126
75be6cf4 2127 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
2128 return -1;
2129
c410a338
ACM
2130 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2131 pr_err("'.' is the only non valid --field-separator argument\n");
2132 return -1;
2133 }
2134
655000e7
ACM
2135 if (setup_list(&symbol_conf.dso_list,
2136 symbol_conf.dso_list_str, "dso") < 0)
2137 return -1;
2138
2139 if (setup_list(&symbol_conf.comm_list,
2140 symbol_conf.comm_list_str, "comm") < 0)
2141 goto out_free_dso_list;
2142
2143 if (setup_list(&symbol_conf.sym_list,
2144 symbol_conf.sym_list_str, "symbol") < 0)
2145 goto out_free_comm_list;
2146
4aa65636 2147 return 0;
655000e7
ACM
2148
2149out_free_dso_list:
2150 strlist__delete(symbol_conf.dso_list);
2151out_free_comm_list:
2152 strlist__delete(symbol_conf.comm_list);
2153 return -1;
4aa65636
ACM
2154}
2155
a1645ce1 2156int map_groups__create_kernel_maps(struct rb_root *kerninfo_root, pid_t pid)
4aa65636 2157{
a1645ce1
ZY
2158 struct kernel_info *kerninfo;
2159 struct dso *kernel;
9de89fe7 2160
a1645ce1
ZY
2161 kerninfo = kerninfo__findnew(kerninfo_root, pid);
2162 if (kerninfo == NULL)
2163 return -1;
2164 kernel = dsos__create_kernel(kerninfo);
9de89fe7
ACM
2165 if (kernel == NULL)
2166 return -1;
2167
a1645ce1
ZY
2168 if (__map_groups__create_kernel_maps(&kerninfo->kmaps,
2169 kerninfo->vmlinux_maps, kernel) < 0)
cc612d81 2170 return -1;
cc612d81 2171
a1645ce1
ZY
2172 if (symbol_conf.use_modules &&
2173 map_groups__create_modules(kerninfo) < 0)
10fe12ef 2174 pr_debug("Problems creating module maps, continuing anyway...\n");
90c83218
ACM
2175 /*
2176 * Now that we have all the maps created, just set the ->end of them:
2177 */
a1645ce1 2178 map_groups__fixup_end(&kerninfo->kmaps);
6671cb16 2179 return 0;
cd84c2ac 2180}
5aab621b
ACM
2181
2182static int hex(char ch)
2183{
2184 if ((ch >= '0') && (ch <= '9'))
2185 return ch - '0';
2186 if ((ch >= 'a') && (ch <= 'f'))
2187 return ch - 'a' + 10;
2188 if ((ch >= 'A') && (ch <= 'F'))
2189 return ch - 'A' + 10;
2190 return -1;
2191}
2192
2193/*
2194 * While we find nice hex chars, build a long_val.
2195 * Return number of chars processed.
2196 */
2197int hex2u64(const char *ptr, u64 *long_val)
2198{
2199 const char *p = ptr;
2200 *long_val = 0;
2201
2202 while (*p) {
2203 const int hex_val = hex(*p);
2204
2205 if (hex_val < 0)
2206 break;
2207
2208 *long_val = (*long_val << 4) | hex_val;
2209 p++;
2210 }
2211
2212 return p - ptr;
2213}
2214
2215char *strxfrchar(char *s, char from, char to)
2216{
2217 char *p = s;
2218
2219 while ((p = strchr(p, from)) != NULL)
2220 *p++ = to;
2221
2222 return s;
2223}
a1645ce1
ZY
2224
2225int map_groups__create_guest_kernel_maps(struct rb_root *kerninfo_root)
2226{
2227 int ret = 0;
2228 struct dirent **namelist = NULL;
2229 int i, items = 0;
2230 char path[PATH_MAX];
2231 pid_t pid;
2232
2233 if (symbol_conf.default_guest_vmlinux_name ||
2234 symbol_conf.default_guest_modules ||
2235 symbol_conf.default_guest_kallsyms) {
2236 map_groups__create_kernel_maps(kerninfo_root,
2237 DEFAULT_GUEST_KERNEL_ID);
2238 }
2239
2240 if (symbol_conf.guestmount) {
2241 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2242 if (items <= 0)
2243 return -ENOENT;
2244 for (i = 0; i < items; i++) {
2245 if (!isdigit(namelist[i]->d_name[0])) {
2246 /* Filter out . and .. */
2247 continue;
2248 }
2249 pid = atoi(namelist[i]->d_name);
2250 sprintf(path, "%s/%s/proc/kallsyms",
2251 symbol_conf.guestmount,
2252 namelist[i]->d_name);
2253 ret = access(path, R_OK);
2254 if (ret) {
2255 pr_debug("Can't access file %s\n", path);
2256 goto failure;
2257 }
2258 map_groups__create_kernel_maps(kerninfo_root,
2259 pid);
2260 }
2261failure:
2262 free(namelist);
2263 }
2264
2265 return ret;
2266}