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