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