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