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