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