]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/symbol.c
perf symbols: Better support for multiple symbol tables per dso
[net-next-2.6.git] / tools / perf / util / symbol.c
CommitLineData
a2928c42
ACM
1#include "util.h"
2#include "../perf.h"
a0055ae2 3#include "string.h"
a2928c42 4#include "symbol.h"
439d473b 5#include "thread.h"
a2928c42 6
8f28827a
FW
7#include "debug.h"
8
b32d133a 9#include <asm/bug.h>
a2928c42
ACM
10#include <libelf.h>
11#include <gelf.h>
12#include <elf.h>
f1617b40 13#include <limits.h>
439d473b 14#include <sys/utsname.h>
2cdbc46d 15
c12e15e7
ACM
16#ifndef NT_GNU_BUILD_ID
17#define NT_GNU_BUILD_ID 3
18#endif
19
94cb9e38
ACM
20enum dso_origin {
21 DSO__ORIG_KERNEL = 0,
22 DSO__ORIG_JAVA_JIT,
23 DSO__ORIG_FEDORA,
24 DSO__ORIG_UBUNTU,
25 DSO__ORIG_BUILDID,
26 DSO__ORIG_DSO,
439d473b 27 DSO__ORIG_KMODULE,
94cb9e38
ACM
28 DSO__ORIG_NOT_FOUND,
29};
30
b0da954a 31static void dsos__add(struct list_head *head, struct dso *dso);
3610583c 32static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
2e538c4a 33static void kernel_maps__insert(struct map *map);
6a4694a4 34struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr);
c338aee8
ACM
35static int dso__load_kernel_sym(struct dso *self, struct map *map,
36 symbol_filter_t filter);
00a192b3 37unsigned int symbol__priv_size;
cc612d81
ACM
38static int vmlinux_path__nr_entries;
39static char **vmlinux_path;
605ca4ba 40static struct map *kernel_map__functions;
439d473b 41
b32d133a
ACM
42static struct symbol_conf symbol_conf__defaults = {
43 .use_modules = true,
44 .try_vmlinux_path = true,
45};
46
61f37a82 47static struct rb_root kernel_maps__functions;
af427bf5 48
3610583c
ACM
49bool dso__loaded(const struct dso *self, enum map_type type)
50{
51 return self->loaded & (1 << type);
52}
53
54static void dso__set_loaded(struct dso *self, enum map_type type)
55{
56 self->loaded |= (1 << type);
57}
58
fcf1203a 59static void symbols__fixup_end(struct rb_root *self)
af427bf5 60{
fcf1203a 61 struct rb_node *nd, *prevnd = rb_first(self);
2e538c4a 62 struct symbol *curr, *prev;
af427bf5
ACM
63
64 if (prevnd == NULL)
65 return;
66
2e538c4a
ACM
67 curr = rb_entry(prevnd, struct symbol, rb_node);
68
af427bf5 69 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
70 prev = curr;
71 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5
ACM
72
73 if (prev->end == prev->start)
74 prev->end = curr->start - 1;
af427bf5 75 }
2e538c4a
ACM
76
77 /* Last entry */
78 if (curr->end == curr->start)
79 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
80}
81
2e538c4a 82static void kernel_maps__fixup_end(void)
af427bf5
ACM
83{
84 struct map *prev, *curr;
61f37a82 85 struct rb_node *nd, *prevnd = rb_first(&kernel_maps__functions);
af427bf5
ACM
86
87 if (prevnd == NULL)
88 return;
89
90 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
91
92 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
93 prev = curr;
94 curr = rb_entry(nd, struct map, rb_node);
95 prev->end = curr->start - 1;
2e538c4a 96 }
90c83218
ACM
97
98 /*
99 * We still haven't the actual symbols, so guess the
100 * last map final address.
101 */
102 curr->end = ~0UL;
af427bf5
ACM
103}
104
00a192b3 105static struct symbol *symbol__new(u64 start, u64 len, const char *name)
a2928c42 106{
0085c954 107 size_t namelen = strlen(name) + 1;
36479484
ACM
108 struct symbol *self = zalloc(symbol__priv_size +
109 sizeof(*self) + namelen);
110 if (self == NULL)
0b73da3f
IM
111 return NULL;
112
36479484 113 if (symbol__priv_size)
00a192b3 114 self = ((void *)self) + symbol__priv_size;
36479484 115
0b73da3f 116 self->start = start;
6cfcc53e 117 self->end = len ? start + len - 1 : start;
e4204992 118
6beba7ad 119 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
e4204992 120
0b73da3f 121 memcpy(self->name, name, namelen);
a2928c42
ACM
122
123 return self;
124}
125
00a192b3 126static void symbol__delete(struct symbol *self)
a2928c42 127{
00a192b3 128 free(((void *)self) - symbol__priv_size);
a2928c42
ACM
129}
130
131static size_t symbol__fprintf(struct symbol *self, FILE *fp)
132{
439d473b 133 return fprintf(fp, " %llx-%llx %s\n",
a2928c42
ACM
134 self->start, self->end, self->name);
135}
136
cfc10d3b
ACM
137static void dso__set_long_name(struct dso *self, char *name)
138{
ef6ae724
ACM
139 if (name == NULL)
140 return;
cfc10d3b
ACM
141 self->long_name = name;
142 self->long_name_len = strlen(name);
143}
144
145static void dso__set_basename(struct dso *self)
146{
147 self->short_name = basename(self->long_name);
148}
149
00a192b3 150struct dso *dso__new(const char *name)
a2928c42
ACM
151{
152 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
153
154 if (self != NULL) {
6a4694a4 155 int i;
a2928c42 156 strcpy(self->name, name);
cfc10d3b 157 dso__set_long_name(self, self->name);
439d473b 158 self->short_name = self->name;
6a4694a4
ACM
159 for (i = 0; i < MAP__NR_TYPES; ++i)
160 self->symbols[i] = RB_ROOT;
161 self->find_symbol = dso__find_symbol;
52d422de 162 self->slen_calculated = 0;
94cb9e38 163 self->origin = DSO__ORIG_NOT_FOUND;
8d06367f
ACM
164 self->loaded = 0;
165 self->has_build_id = 0;
a2928c42
ACM
166 }
167
168 return self;
169}
170
fcf1203a 171static void symbols__delete(struct rb_root *self)
a2928c42
ACM
172{
173 struct symbol *pos;
fcf1203a 174 struct rb_node *next = rb_first(self);
a2928c42
ACM
175
176 while (next) {
177 pos = rb_entry(next, struct symbol, rb_node);
178 next = rb_next(&pos->rb_node);
fcf1203a 179 rb_erase(&pos->rb_node, self);
00a192b3 180 symbol__delete(pos);
a2928c42
ACM
181 }
182}
183
184void dso__delete(struct dso *self)
185{
6a4694a4
ACM
186 int i;
187 for (i = 0; i < MAP__NR_TYPES; ++i)
188 symbols__delete(&self->symbols[i]);
439d473b
ACM
189 if (self->long_name != self->name)
190 free(self->long_name);
a2928c42
ACM
191 free(self);
192}
193
8d06367f
ACM
194void dso__set_build_id(struct dso *self, void *build_id)
195{
196 memcpy(self->build_id, build_id, sizeof(self->build_id));
197 self->has_build_id = 1;
198}
199
fcf1203a 200static void symbols__insert(struct rb_root *self, struct symbol *sym)
a2928c42 201{
fcf1203a 202 struct rb_node **p = &self->rb_node;
a2928c42 203 struct rb_node *parent = NULL;
9cffa8d5 204 const u64 ip = sym->start;
a2928c42
ACM
205 struct symbol *s;
206
207 while (*p != NULL) {
208 parent = *p;
209 s = rb_entry(parent, struct symbol, rb_node);
210 if (ip < s->start)
211 p = &(*p)->rb_left;
212 else
213 p = &(*p)->rb_right;
214 }
215 rb_link_node(&sym->rb_node, parent, p);
fcf1203a 216 rb_insert_color(&sym->rb_node, self);
a2928c42
ACM
217}
218
fcf1203a 219static struct symbol *symbols__find(struct rb_root *self, u64 ip)
a2928c42
ACM
220{
221 struct rb_node *n;
222
223 if (self == NULL)
224 return NULL;
225
fcf1203a 226 n = self->rb_node;
a2928c42
ACM
227
228 while (n) {
229 struct symbol *s = rb_entry(n, struct symbol, rb_node);
230
231 if (ip < s->start)
232 n = n->rb_left;
233 else if (ip > s->end)
234 n = n->rb_right;
235 else
236 return s;
237 }
238
239 return NULL;
240}
241
6a4694a4 242struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr)
fcf1203a 243{
6a4694a4 244 return symbols__find(&self->symbols[type], addr);
fcf1203a
ACM
245}
246
8d06367f 247int build_id__sprintf(u8 *self, int len, char *bf)
a2928c42 248{
8d06367f
ACM
249 char *bid = bf;
250 u8 *raw = self;
251 int i;
a2928c42 252
8d06367f
ACM
253 for (i = 0; i < len; ++i) {
254 sprintf(bid, "%02x", *raw);
255 ++raw;
256 bid += 2;
257 }
258
259 return raw - self;
260}
261
9e03eb2d 262size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
8d06367f
ACM
263{
264 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
8d06367f
ACM
265
266 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
9e03eb2d
ACM
267 return fprintf(fp, "%s", sbuild_id);
268}
269
6a4694a4
ACM
270static const char * map_type__name[MAP__NR_TYPES] = {
271 [MAP__FUNCTION] = "Functions",
272};
273
9e03eb2d
ACM
274size_t dso__fprintf(struct dso *self, FILE *fp)
275{
6a4694a4 276 int i;
9e03eb2d
ACM
277 struct rb_node *nd;
278 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
279
280 ret += dso__fprintf_buildid(self, fp);
6a4694a4
ACM
281 ret += fprintf(fp, ")\n");
282 for (i = 0; i < MAP__NR_TYPES; ++i) {
283 ret += fprintf(fp, "%s:\n", map_type__name[i]);
8d06367f 284
6a4694a4
ACM
285 for (nd = rb_first(&self->symbols[i]); nd; nd = rb_next(nd)) {
286 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
287 ret += symbol__fprintf(pos, fp);
288 }
a2928c42
ACM
289 }
290
291 return ret;
292}
293
2e538c4a
ACM
294/*
295 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
296 * so that we can in the next step set the symbol ->end address and then
297 * call kernel_maps__split_kallsyms.
298 */
6beba7ad 299static int kernel_maps__load_all_kallsyms(void)
a2928c42 300{
a2928c42
ACM
301 char *line = NULL;
302 size_t n;
303 FILE *file = fopen("/proc/kallsyms", "r");
304
305 if (file == NULL)
306 goto out_failure;
307
308 while (!feof(file)) {
9cffa8d5 309 u64 start;
a2928c42
ACM
310 struct symbol *sym;
311 int line_len, len;
312 char symbol_type;
2e538c4a 313 char *symbol_name;
a2928c42
ACM
314
315 line_len = getline(&line, &n, file);
316 if (line_len < 0)
317 break;
318
319 if (!line)
320 goto out_failure;
321
322 line[--line_len] = '\0'; /* \n */
323
a0055ae2 324 len = hex2u64(line, &start);
a2928c42
ACM
325
326 len++;
327 if (len + 2 >= line_len)
328 continue;
329
330 symbol_type = toupper(line[len]);
331 /*
332 * We're interested only in code ('T'ext)
333 */
334 if (symbol_type != 'T' && symbol_type != 'W')
335 continue;
af427bf5
ACM
336
337 symbol_name = line + len + 2;
2e538c4a
ACM
338 /*
339 * Will fix up the end later, when we have all symbols sorted.
340 */
00a192b3 341 sym = symbol__new(start, 0, symbol_name);
af427bf5 342
2e538c4a
ACM
343 if (sym == NULL)
344 goto out_delete_line;
345
82164161
ACM
346 /*
347 * We will pass the symbols to the filter later, in
348 * kernel_maps__split_kallsyms, when we have split the
349 * maps per module
350 */
6a4694a4 351 symbols__insert(&kernel_map__functions->dso->symbols[MAP__FUNCTION], sym);
2e538c4a
ACM
352 }
353
354 free(line);
355 fclose(file);
356
357 return 0;
358
359out_delete_line:
360 free(line);
361out_failure:
362 return -1;
363}
364
365/*
366 * Split the symbols into maps, making sure there are no overlaps, i.e. the
367 * kernel range is broken in several maps, named [kernel].N, as we don't have
368 * the original ELF section names vmlinux have.
369 */
c338aee8 370static int kernel_maps__split_kallsyms(symbol_filter_t filter)
2e538c4a 371{
61f37a82 372 struct map *map = kernel_map__functions;
2e538c4a
ACM
373 struct symbol *pos;
374 int count = 0;
6a4694a4 375 struct rb_node *next = rb_first(&kernel_map__functions->dso->symbols[map->type]);
2e538c4a
ACM
376 int kernel_range = 0;
377
378 while (next) {
379 char *module;
380
381 pos = rb_entry(next, struct symbol, rb_node);
382 next = rb_next(&pos->rb_node);
383
384 module = strchr(pos->name, '\t');
385 if (module) {
2e538c4a
ACM
386 *module++ = '\0';
387
af427bf5
ACM
388 if (strcmp(map->dso->name, module)) {
389 map = kernel_maps__find_by_dso_name(module);
390 if (!map) {
6beba7ad
ACM
391 pr_err("/proc/{kallsyms,modules} "
392 "inconsistency!\n");
af427bf5
ACM
393 return -1;
394 }
395 }
2e538c4a
ACM
396 /*
397 * So that we look just like we get from .ko files,
398 * i.e. not prelinked, relative to map->start.
399 */
400 pos->start = map->map_ip(map, pos->start);
401 pos->end = map->map_ip(map, pos->end);
61f37a82 402 } else if (map != kernel_map__functions) {
2e538c4a
ACM
403 char dso_name[PATH_MAX];
404 struct dso *dso;
405
406 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
407 kernel_range++);
408
00a192b3 409 dso = dso__new(dso_name);
2e538c4a
ACM
410 if (dso == NULL)
411 return -1;
412
3610583c 413 map = map__new2(pos->start, dso, MAP__FUNCTION);
2e538c4a
ACM
414 if (map == NULL) {
415 dso__delete(dso);
416 return -1;
417 }
a2928c42 418
ed52ce2e 419 map->map_ip = map->unmap_ip = identity__map_ip;
2e538c4a
ACM
420 kernel_maps__insert(map);
421 ++kernel_range;
422 }
a2928c42 423
2e538c4a 424 if (filter && filter(map, pos)) {
6a4694a4 425 rb_erase(&pos->rb_node, &kernel_map__functions->dso->symbols[map->type]);
00a192b3 426 symbol__delete(pos);
2e538c4a 427 } else {
61f37a82 428 if (map != kernel_map__functions) {
fcf1203a 429 rb_erase(&pos->rb_node,
6a4694a4
ACM
430 &kernel_map__functions->dso->symbols[map->type]);
431 symbols__insert(&map->dso->symbols[map->type], pos);
2e538c4a 432 }
9974f496
MG
433 count++;
434 }
a2928c42
ACM
435 }
436
9974f496 437 return count;
2e538c4a 438}
a2928c42 439
2e538c4a 440
c338aee8 441static int kernel_maps__load_kallsyms(symbol_filter_t filter)
2e538c4a 442{
6beba7ad 443 if (kernel_maps__load_all_kallsyms())
2e538c4a
ACM
444 return -1;
445
6a4694a4 446 symbols__fixup_end(&kernel_map__functions->dso->symbols[MAP__FUNCTION]);
61f37a82 447 kernel_map__functions->dso->origin = DSO__ORIG_KERNEL;
2e538c4a 448
c338aee8 449 return kernel_maps__split_kallsyms(filter);
a2928c42
ACM
450}
451
c338aee8 452size_t kernel_maps__fprintf(FILE *fp)
af427bf5 453{
6beba7ad 454 size_t printed = fprintf(fp, "Kernel maps:\n");
af427bf5
ACM
455 struct rb_node *nd;
456
61f37a82 457 for (nd = rb_first(&kernel_maps__functions); nd; nd = rb_next(nd)) {
af427bf5
ACM
458 struct map *pos = rb_entry(nd, struct map, rb_node);
459
2e538c4a 460 printed += fprintf(fp, "Map:");
af427bf5 461 printed += map__fprintf(pos, fp);
6beba7ad 462 if (verbose > 1) {
2e538c4a
ACM
463 printed += dso__fprintf(pos->dso, fp);
464 printed += fprintf(fp, "--\n");
465 }
af427bf5
ACM
466 }
467
6beba7ad 468 return printed + fprintf(fp, "END kernel maps\n");
af427bf5
ACM
469}
470
439d473b 471static int dso__load_perf_map(struct dso *self, struct map *map,
6beba7ad 472 symbol_filter_t filter)
80d496be
PE
473{
474 char *line = NULL;
475 size_t n;
476 FILE *file;
477 int nr_syms = 0;
478
439d473b 479 file = fopen(self->long_name, "r");
80d496be
PE
480 if (file == NULL)
481 goto out_failure;
482
483 while (!feof(file)) {
9cffa8d5 484 u64 start, size;
80d496be
PE
485 struct symbol *sym;
486 int line_len, len;
487
488 line_len = getline(&line, &n, file);
489 if (line_len < 0)
490 break;
491
492 if (!line)
493 goto out_failure;
494
495 line[--line_len] = '\0'; /* \n */
496
497 len = hex2u64(line, &start);
498
499 len++;
500 if (len + 2 >= line_len)
501 continue;
502
503 len += hex2u64(line + len, &size);
504
505 len++;
506 if (len + 2 >= line_len)
507 continue;
508
00a192b3 509 sym = symbol__new(start, size, line + len);
80d496be
PE
510
511 if (sym == NULL)
512 goto out_delete_line;
513
439d473b 514 if (filter && filter(map, sym))
00a192b3 515 symbol__delete(sym);
80d496be 516 else {
6a4694a4 517 symbols__insert(&self->symbols[map->type], sym);
80d496be
PE
518 nr_syms++;
519 }
520 }
521
522 free(line);
523 fclose(file);
524
525 return nr_syms;
526
527out_delete_line:
528 free(line);
529out_failure:
530 return -1;
531}
532
a2928c42
ACM
533/**
534 * elf_symtab__for_each_symbol - iterate thru all the symbols
535 *
536 * @self: struct elf_symtab instance to iterate
83a0944f 537 * @idx: uint32_t idx
a2928c42
ACM
538 * @sym: GElf_Sym iterator
539 */
83a0944f
IM
540#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
541 for (idx = 0, gelf_getsym(syms, idx, &sym);\
542 idx < nr_syms; \
543 idx++, gelf_getsym(syms, idx, &sym))
a2928c42
ACM
544
545static inline uint8_t elf_sym__type(const GElf_Sym *sym)
546{
547 return GELF_ST_TYPE(sym->st_info);
548}
549
550static inline int elf_sym__is_function(const GElf_Sym *sym)
551{
552 return elf_sym__type(sym) == STT_FUNC &&
553 sym->st_name != 0 &&
81833130 554 sym->st_shndx != SHN_UNDEF;
a2928c42
ACM
555}
556
6cfcc53e
MG
557static inline int elf_sym__is_label(const GElf_Sym *sym)
558{
559 return elf_sym__type(sym) == STT_NOTYPE &&
560 sym->st_name != 0 &&
561 sym->st_shndx != SHN_UNDEF &&
562 sym->st_shndx != SHN_ABS;
563}
564
565static inline const char *elf_sec__name(const GElf_Shdr *shdr,
566 const Elf_Data *secstrs)
567{
568 return secstrs->d_buf + shdr->sh_name;
569}
570
571static inline int elf_sec__is_text(const GElf_Shdr *shdr,
572 const Elf_Data *secstrs)
573{
574 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
575}
576
a2928c42
ACM
577static inline const char *elf_sym__name(const GElf_Sym *sym,
578 const Elf_Data *symstrs)
579{
580 return symstrs->d_buf + sym->st_name;
581}
582
583static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
584 GElf_Shdr *shp, const char *name,
83a0944f 585 size_t *idx)
a2928c42
ACM
586{
587 Elf_Scn *sec = NULL;
588 size_t cnt = 1;
589
590 while ((sec = elf_nextscn(elf, sec)) != NULL) {
591 char *str;
592
593 gelf_getshdr(sec, shp);
594 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
595 if (!strcmp(name, str)) {
83a0944f
IM
596 if (idx)
597 *idx = cnt;
a2928c42
ACM
598 break;
599 }
600 ++cnt;
601 }
602
603 return sec;
604}
605
8ce998d6
ACM
606#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
607 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
608 idx < nr_entries; \
609 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
610
611#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
612 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
613 idx < nr_entries; \
614 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
615
a25e46c4
ACM
616/*
617 * We need to check if we have a .dynsym, so that we can handle the
618 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
619 * .dynsym or .symtab).
620 * And always look at the original dso, not at debuginfo packages, that
621 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
622 */
82164161
ACM
623static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
624 symbol_filter_t filter)
8ce998d6
ACM
625{
626 uint32_t nr_rel_entries, idx;
627 GElf_Sym sym;
9cffa8d5 628 u64 plt_offset;
8ce998d6
ACM
629 GElf_Shdr shdr_plt;
630 struct symbol *f;
a25e46c4 631 GElf_Shdr shdr_rel_plt, shdr_dynsym;
8ce998d6 632 Elf_Data *reldata, *syms, *symstrs;
a25e46c4
ACM
633 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
634 size_t dynsym_idx;
635 GElf_Ehdr ehdr;
8ce998d6 636 char sympltname[1024];
a25e46c4
ACM
637 Elf *elf;
638 int nr = 0, symidx, fd, err = 0;
639
439d473b 640 fd = open(self->long_name, O_RDONLY);
a25e46c4
ACM
641 if (fd < 0)
642 goto out;
643
84087126 644 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a25e46c4
ACM
645 if (elf == NULL)
646 goto out_close;
647
648 if (gelf_getehdr(elf, &ehdr) == NULL)
649 goto out_elf_end;
650
651 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
652 ".dynsym", &dynsym_idx);
653 if (scn_dynsym == NULL)
654 goto out_elf_end;
8ce998d6 655
a25e46c4 656 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
657 ".rela.plt", NULL);
658 if (scn_plt_rel == NULL) {
a25e46c4 659 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
8ce998d6
ACM
660 ".rel.plt", NULL);
661 if (scn_plt_rel == NULL)
a25e46c4 662 goto out_elf_end;
8ce998d6
ACM
663 }
664
a25e46c4
ACM
665 err = -1;
666
8ce998d6 667 if (shdr_rel_plt.sh_link != dynsym_idx)
a25e46c4 668 goto out_elf_end;
8ce998d6 669
a25e46c4
ACM
670 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
671 goto out_elf_end;
8ce998d6
ACM
672
673 /*
83a0944f 674 * Fetch the relocation section to find the idxes to the GOT
8ce998d6
ACM
675 * and the symbols in the .dynsym they refer to.
676 */
677 reldata = elf_getdata(scn_plt_rel, NULL);
678 if (reldata == NULL)
a25e46c4 679 goto out_elf_end;
8ce998d6
ACM
680
681 syms = elf_getdata(scn_dynsym, NULL);
682 if (syms == NULL)
a25e46c4 683 goto out_elf_end;
8ce998d6 684
a25e46c4 685 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
8ce998d6 686 if (scn_symstrs == NULL)
a25e46c4 687 goto out_elf_end;
8ce998d6
ACM
688
689 symstrs = elf_getdata(scn_symstrs, NULL);
690 if (symstrs == NULL)
a25e46c4 691 goto out_elf_end;
8ce998d6
ACM
692
693 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
694 plt_offset = shdr_plt.sh_offset;
695
696 if (shdr_rel_plt.sh_type == SHT_RELA) {
697 GElf_Rela pos_mem, *pos;
698
699 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
700 nr_rel_entries) {
701 symidx = GELF_R_SYM(pos->r_info);
702 plt_offset += shdr_plt.sh_entsize;
703 gelf_getsym(syms, symidx, &sym);
704 snprintf(sympltname, sizeof(sympltname),
705 "%s@plt", elf_sym__name(&sym, symstrs));
706
707 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
00a192b3 708 sympltname);
8ce998d6 709 if (!f)
a25e46c4 710 goto out_elf_end;
8ce998d6 711
82164161
ACM
712 if (filter && filter(map, f))
713 symbol__delete(f);
714 else {
6a4694a4 715 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
716 ++nr;
717 }
8ce998d6
ACM
718 }
719 } else if (shdr_rel_plt.sh_type == SHT_REL) {
720 GElf_Rel pos_mem, *pos;
721 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
722 nr_rel_entries) {
723 symidx = GELF_R_SYM(pos->r_info);
724 plt_offset += shdr_plt.sh_entsize;
725 gelf_getsym(syms, symidx, &sym);
726 snprintf(sympltname, sizeof(sympltname),
727 "%s@plt", elf_sym__name(&sym, symstrs));
728
729 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
00a192b3 730 sympltname);
8ce998d6 731 if (!f)
a25e46c4 732 goto out_elf_end;
8ce998d6 733
82164161
ACM
734 if (filter && filter(map, f))
735 symbol__delete(f);
736 else {
6a4694a4 737 symbols__insert(&self->symbols[map->type], f);
82164161
ACM
738 ++nr;
739 }
8ce998d6 740 }
8ce998d6
ACM
741 }
742
a25e46c4
ACM
743 err = 0;
744out_elf_end:
745 elf_end(elf);
746out_close:
747 close(fd);
748
749 if (err == 0)
750 return nr;
751out:
6beba7ad
ACM
752 pr_warning("%s: problems reading %s PLT info.\n",
753 __func__, self->long_name);
a25e46c4 754 return 0;
8ce998d6
ACM
755}
756
439d473b
ACM
757static int dso__load_sym(struct dso *self, struct map *map, const char *name,
758 int fd, symbol_filter_t filter, int kernel,
6beba7ad 759 int kmodule)
a2928c42 760{
2e538c4a
ACM
761 struct map *curr_map = map;
762 struct dso *curr_dso = self;
763 size_t dso_name_len = strlen(self->short_name);
6cfcc53e 764 Elf_Data *symstrs, *secstrs;
a2928c42
ACM
765 uint32_t nr_syms;
766 int err = -1;
83a0944f 767 uint32_t idx;
a2928c42
ACM
768 GElf_Ehdr ehdr;
769 GElf_Shdr shdr;
770 Elf_Data *syms;
771 GElf_Sym sym;
a25e46c4 772 Elf_Scn *sec, *sec_strndx;
a2928c42 773 Elf *elf;
439d473b 774 int nr = 0;
a2928c42 775
84087126 776 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
a2928c42 777 if (elf == NULL) {
6beba7ad 778 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
a2928c42
ACM
779 goto out_close;
780 }
781
782 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 783 pr_err("%s: cannot get elf header.\n", __func__);
a2928c42
ACM
784 goto out_elf_end;
785 }
786
787 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
8ce998d6 788 if (sec == NULL) {
a25e46c4
ACM
789 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
790 if (sec == NULL)
8ce998d6 791 goto out_elf_end;
8ce998d6 792 }
a2928c42
ACM
793
794 syms = elf_getdata(sec, NULL);
795 if (syms == NULL)
796 goto out_elf_end;
797
798 sec = elf_getscn(elf, shdr.sh_link);
799 if (sec == NULL)
800 goto out_elf_end;
801
802 symstrs = elf_getdata(sec, NULL);
803 if (symstrs == NULL)
804 goto out_elf_end;
805
6cfcc53e
MG
806 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
807 if (sec_strndx == NULL)
808 goto out_elf_end;
809
810 secstrs = elf_getdata(sec_strndx, NULL);
9b30a26b 811 if (secstrs == NULL)
6cfcc53e
MG
812 goto out_elf_end;
813
a2928c42
ACM
814 nr_syms = shdr.sh_size / shdr.sh_entsize;
815
e9fbc9dc 816 memset(&sym, 0, sizeof(sym));
d20ff6bd
MG
817 if (!kernel) {
818 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
30d7a77d
ACM
819 elf_section_by_name(elf, &ehdr, &shdr,
820 ".gnu.prelink_undo",
821 NULL) != NULL);
d20ff6bd
MG
822 } else self->adjust_symbols = 0;
823
83a0944f 824 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
a2928c42 825 struct symbol *f;
83a0944f 826 const char *elf_name;
2e538c4a 827 char *demangled = NULL;
6cfcc53e
MG
828 int is_label = elf_sym__is_label(&sym);
829 const char *section_name;
a2928c42 830
6cfcc53e 831 if (!is_label && !elf_sym__is_function(&sym))
a2928c42
ACM
832 continue;
833
834 sec = elf_getscn(elf, sym.st_shndx);
835 if (!sec)
836 goto out_elf_end;
837
838 gelf_getshdr(sec, &shdr);
6cfcc53e
MG
839
840 if (is_label && !elf_sec__is_text(&shdr, secstrs))
841 continue;
842
2e538c4a 843 elf_name = elf_sym__name(&sym, symstrs);
6cfcc53e 844 section_name = elf_sec__name(&shdr, secstrs);
0b73da3f 845
2e538c4a
ACM
846 if (kernel || kmodule) {
847 char dso_name[PATH_MAX];
848
849 if (strcmp(section_name,
850 curr_dso->short_name + dso_name_len) == 0)
851 goto new_symbol;
852
853 if (strcmp(section_name, ".text") == 0) {
854 curr_map = map;
855 curr_dso = self;
856 goto new_symbol;
857 }
858
859 snprintf(dso_name, sizeof(dso_name),
860 "%s%s", self->short_name, section_name);
861
862 curr_map = kernel_maps__find_by_dso_name(dso_name);
863 if (curr_map == NULL) {
864 u64 start = sym.st_value;
865
866 if (kmodule)
867 start += map->start + shdr.sh_offset;
868
00a192b3 869 curr_dso = dso__new(dso_name);
2e538c4a
ACM
870 if (curr_dso == NULL)
871 goto out_elf_end;
3610583c
ACM
872 curr_map = map__new2(start, curr_dso,
873 MAP__FUNCTION);
2e538c4a
ACM
874 if (curr_map == NULL) {
875 dso__delete(curr_dso);
876 goto out_elf_end;
877 }
ed52ce2e
ACM
878 curr_map->map_ip = identity__map_ip;
879 curr_map->unmap_ip = identity__map_ip;
2e538c4a
ACM
880 curr_dso->origin = DSO__ORIG_KERNEL;
881 kernel_maps__insert(curr_map);
b0da954a 882 dsos__add(&dsos__kernel, curr_dso);
2e538c4a
ACM
883 } else
884 curr_dso = curr_map->dso;
885
886 goto new_symbol;
af427bf5
ACM
887 }
888
2e538c4a 889 if (curr_dso->adjust_symbols) {
6beba7ad
ACM
890 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
891 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
892 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
f5812a7a 893 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
af427bf5 894 }
28ac909b
ACM
895 /*
896 * We need to figure out if the object was created from C++ sources
897 * DWARF DW_compile_unit has this, but we don't always have access
898 * to it...
899 */
83a0944f 900 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
28ac909b 901 if (demangled != NULL)
83a0944f 902 elf_name = demangled;
2e538c4a 903new_symbol:
00a192b3 904 f = symbol__new(sym.st_value, sym.st_size, elf_name);
28ac909b 905 free(demangled);
a2928c42
ACM
906 if (!f)
907 goto out_elf_end;
908
2e538c4a 909 if (filter && filter(curr_map, f))
00a192b3 910 symbol__delete(f);
69ee69f6 911 else {
6a4694a4 912 symbols__insert(&curr_dso->symbols[curr_map->type], f);
69ee69f6
ACM
913 nr++;
914 }
a2928c42
ACM
915 }
916
2e538c4a
ACM
917 /*
918 * For misannotated, zeroed, ASM function sizes.
919 */
920 if (nr > 0)
6a4694a4 921 symbols__fixup_end(&self->symbols[map->type]);
a2928c42
ACM
922 err = nr;
923out_elf_end:
924 elf_end(elf);
925out_close:
926 return err;
927}
928
78075caa
ACM
929static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
930{
931 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
932}
933
b0da954a 934static bool __dsos__read_build_ids(struct list_head *head)
57f395a7 935{
e30a3d12 936 bool have_build_id = false;
57f395a7
FW
937 struct dso *pos;
938
b0da954a 939 list_for_each_entry(pos, head, node)
e30a3d12
ACM
940 if (filename__read_build_id(pos->long_name, pos->build_id,
941 sizeof(pos->build_id)) > 0) {
942 have_build_id = true;
943 pos->has_build_id = true;
944 }
57f395a7 945
e30a3d12 946 return have_build_id;
57f395a7
FW
947}
948
b0da954a
ACM
949bool dsos__read_build_ids(void)
950{
951 return __dsos__read_build_ids(&dsos__kernel) ||
952 __dsos__read_build_ids(&dsos__user);
953}
954
fd7a346e
ACM
955/*
956 * Align offset to 4 bytes as needed for note name and descriptor data.
957 */
958#define NOTE_ALIGN(n) (((n) + 3) & -4U)
959
2643ce11 960int filename__read_build_id(const char *filename, void *bf, size_t size)
4d1e00a8 961{
2643ce11 962 int fd, err = -1;
4d1e00a8
ACM
963 GElf_Ehdr ehdr;
964 GElf_Shdr shdr;
fd7a346e 965 Elf_Data *data;
4d1e00a8 966 Elf_Scn *sec;
e57cfcda 967 Elf_Kind ek;
fd7a346e 968 void *ptr;
4d1e00a8 969 Elf *elf;
4d1e00a8 970
2643ce11
ACM
971 if (size < BUILD_ID_SIZE)
972 goto out;
973
974 fd = open(filename, O_RDONLY);
4d1e00a8
ACM
975 if (fd < 0)
976 goto out;
977
84087126 978 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
4d1e00a8 979 if (elf == NULL) {
8d06367f 980 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
4d1e00a8
ACM
981 goto out_close;
982 }
983
e57cfcda
PE
984 ek = elf_kind(elf);
985 if (ek != ELF_K_ELF)
986 goto out_elf_end;
987
4d1e00a8 988 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 989 pr_err("%s: cannot get elf header.\n", __func__);
4d1e00a8
ACM
990 goto out_elf_end;
991 }
992
2643ce11
ACM
993 sec = elf_section_by_name(elf, &ehdr, &shdr,
994 ".note.gnu.build-id", NULL);
fd7a346e
ACM
995 if (sec == NULL) {
996 sec = elf_section_by_name(elf, &ehdr, &shdr,
997 ".notes", NULL);
998 if (sec == NULL)
999 goto out_elf_end;
1000 }
4d1e00a8 1001
fd7a346e
ACM
1002 data = elf_getdata(sec, NULL);
1003 if (data == NULL)
4d1e00a8 1004 goto out_elf_end;
fd7a346e
ACM
1005
1006 ptr = data->d_buf;
1007 while (ptr < (data->d_buf + data->d_size)) {
1008 GElf_Nhdr *nhdr = ptr;
1009 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1010 descsz = NOTE_ALIGN(nhdr->n_descsz);
1011 const char *name;
1012
1013 ptr += sizeof(*nhdr);
1014 name = ptr;
1015 ptr += namesz;
1016 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1017 nhdr->n_namesz == sizeof("GNU")) {
1018 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1019 memcpy(bf, ptr, BUILD_ID_SIZE);
1020 err = BUILD_ID_SIZE;
1021 break;
1022 }
1023 }
1024 ptr += descsz;
1025 }
2643ce11
ACM
1026out_elf_end:
1027 elf_end(elf);
1028out_close:
1029 close(fd);
1030out:
1031 return err;
1032}
1033
f1617b40
ACM
1034int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1035{
1036 int fd, err = -1;
1037
1038 if (size < BUILD_ID_SIZE)
1039 goto out;
1040
1041 fd = open(filename, O_RDONLY);
1042 if (fd < 0)
1043 goto out;
1044
1045 while (1) {
1046 char bf[BUFSIZ];
1047 GElf_Nhdr nhdr;
1048 int namesz, descsz;
1049
1050 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1051 break;
1052
fd7a346e
ACM
1053 namesz = NOTE_ALIGN(nhdr.n_namesz);
1054 descsz = NOTE_ALIGN(nhdr.n_descsz);
f1617b40
ACM
1055 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1056 nhdr.n_namesz == sizeof("GNU")) {
1057 if (read(fd, bf, namesz) != namesz)
1058 break;
1059 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1060 if (read(fd, build_id,
1061 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1062 err = 0;
1063 break;
1064 }
1065 } else if (read(fd, bf, descsz) != descsz)
1066 break;
1067 } else {
1068 int n = namesz + descsz;
1069 if (read(fd, bf, n) != n)
1070 break;
1071 }
1072 }
1073 close(fd);
1074out:
1075 return err;
1076}
1077
94cb9e38
ACM
1078char dso__symtab_origin(const struct dso *self)
1079{
1080 static const char origin[] = {
1081 [DSO__ORIG_KERNEL] = 'k',
1082 [DSO__ORIG_JAVA_JIT] = 'j',
1083 [DSO__ORIG_FEDORA] = 'f',
1084 [DSO__ORIG_UBUNTU] = 'u',
1085 [DSO__ORIG_BUILDID] = 'b',
1086 [DSO__ORIG_DSO] = 'd',
439d473b 1087 [DSO__ORIG_KMODULE] = 'K',
94cb9e38
ACM
1088 };
1089
1090 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1091 return '!';
1092 return origin[self->origin];
1093}
1094
6beba7ad 1095int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
a2928c42 1096{
4d1e00a8 1097 int size = PATH_MAX;
c338aee8 1098 char *name;
d3379ab9 1099 u8 build_id[BUILD_ID_SIZE];
a2928c42
ACM
1100 int ret = -1;
1101 int fd;
1102
3610583c 1103 dso__set_loaded(self, map->type);
66bd8424 1104
c338aee8
ACM
1105 if (self->kernel)
1106 return dso__load_kernel_sym(self, map, filter);
1107
1108 name = malloc(size);
a2928c42
ACM
1109 if (!name)
1110 return -1;
1111
30d7a77d 1112 self->adjust_symbols = 0;
f5812a7a 1113
94cb9e38 1114 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
6beba7ad 1115 ret = dso__load_perf_map(self, map, filter);
94cb9e38
ACM
1116 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1117 DSO__ORIG_NOT_FOUND;
1118 return ret;
1119 }
1120
1121 self->origin = DSO__ORIG_FEDORA - 1;
80d496be 1122
a2928c42
ACM
1123more:
1124 do {
94cb9e38
ACM
1125 self->origin++;
1126 switch (self->origin) {
1127 case DSO__ORIG_FEDORA:
439d473b
ACM
1128 snprintf(name, size, "/usr/lib/debug%s.debug",
1129 self->long_name);
a2928c42 1130 break;
94cb9e38 1131 case DSO__ORIG_UBUNTU:
439d473b
ACM
1132 snprintf(name, size, "/usr/lib/debug%s",
1133 self->long_name);
a2928c42 1134 break;
94cb9e38 1135 case DSO__ORIG_BUILDID:
d3379ab9
ACM
1136 if (filename__read_build_id(self->long_name, build_id,
1137 sizeof(build_id))) {
1138 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1139
1140 build_id__sprintf(build_id, sizeof(build_id),
1141 build_id_hex);
4d1e00a8
ACM
1142 snprintf(name, size,
1143 "/usr/lib/debug/.build-id/%.2s/%s.debug",
d3379ab9
ACM
1144 build_id_hex, build_id_hex + 2);
1145 if (self->has_build_id)
1146 goto compare_build_id;
1147 break;
4d1e00a8 1148 }
94cb9e38 1149 self->origin++;
4d1e00a8 1150 /* Fall thru */
94cb9e38 1151 case DSO__ORIG_DSO:
439d473b 1152 snprintf(name, size, "%s", self->long_name);
a2928c42
ACM
1153 break;
1154
1155 default:
1156 goto out;
1157 }
a2928c42 1158
8d06367f 1159 if (self->has_build_id) {
d3379ab9
ACM
1160 if (filename__read_build_id(name, build_id,
1161 sizeof(build_id)) < 0)
8d06367f 1162 goto more;
8d06367f 1163compare_build_id:
78075caa 1164 if (!dso__build_id_equal(self, build_id))
8d06367f
ACM
1165 goto more;
1166 }
1167
a2928c42
ACM
1168 fd = open(name, O_RDONLY);
1169 } while (fd < 0);
1170
6beba7ad 1171 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
a2928c42
ACM
1172 close(fd);
1173
1174 /*
1175 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1176 */
1177 if (!ret)
1178 goto more;
1179
a25e46c4 1180 if (ret > 0) {
82164161 1181 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
a25e46c4
ACM
1182 if (nr_plt > 0)
1183 ret += nr_plt;
1184 }
a2928c42
ACM
1185out:
1186 free(name);
1340e6bb
ACM
1187 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1188 return 0;
a2928c42
ACM
1189 return ret;
1190}
1191
439d473b 1192static void kernel_maps__insert(struct map *map)
6cfcc53e 1193{
61f37a82 1194 maps__insert(&kernel_maps__functions, map);
439d473b 1195}
6cfcc53e 1196
fcf1203a
ACM
1197struct symbol *kernel_maps__find_function(u64 ip, struct map **mapp,
1198 symbol_filter_t filter)
439d473b 1199{
61f37a82 1200 struct map *map = maps__find(&kernel_maps__functions, ip);
2e538c4a
ACM
1201
1202 if (mapp)
1203 *mapp = map;
439d473b
ACM
1204
1205 if (map) {
1206 ip = map->map_ip(map, ip);
6a4694a4 1207 return map__find_symbol(map, ip, filter);
b32d133a 1208 } else
61f37a82 1209 WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps__functions),
b32d133a 1210 "Empty kernel_maps, was symbol__init() called?\n");
6cfcc53e 1211
2e538c4a 1212 return NULL;
439d473b
ACM
1213}
1214
1215struct map *kernel_maps__find_by_dso_name(const char *name)
1216{
1217 struct rb_node *nd;
1218
61f37a82 1219 for (nd = rb_first(&kernel_maps__functions); nd; nd = rb_next(nd)) {
439d473b
ACM
1220 struct map *map = rb_entry(nd, struct map, rb_node);
1221
1222 if (map->dso && strcmp(map->dso->name, name) == 0)
1223 return map;
1224 }
1225
1226 return NULL;
1227}
1228
c338aee8 1229static int dsos__set_modules_path_dir(char *dirname)
6cfcc53e 1230{
439d473b 1231 struct dirent *dent;
439d473b 1232 DIR *dir = opendir(dirname);
6cfcc53e 1233
439d473b 1234 if (!dir) {
87f8ea4c 1235 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
439d473b
ACM
1236 return -1;
1237 }
6cfcc53e 1238
439d473b
ACM
1239 while ((dent = readdir(dir)) != NULL) {
1240 char path[PATH_MAX];
1241
1242 if (dent->d_type == DT_DIR) {
1243 if (!strcmp(dent->d_name, ".") ||
1244 !strcmp(dent->d_name, ".."))
1245 continue;
1246
1247 snprintf(path, sizeof(path), "%s/%s",
1248 dirname, dent->d_name);
c338aee8 1249 if (dsos__set_modules_path_dir(path) < 0)
439d473b
ACM
1250 goto failure;
1251 } else {
1252 char *dot = strrchr(dent->d_name, '.'),
1253 dso_name[PATH_MAX];
1254 struct map *map;
cfc10d3b 1255 char *long_name;
439d473b
ACM
1256
1257 if (dot == NULL || strcmp(dot, ".ko"))
1258 continue;
1259 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1260 (int)(dot - dent->d_name), dent->d_name);
1261
a2a99e8e 1262 strxfrchar(dso_name, '-', '_');
439d473b
ACM
1263 map = kernel_maps__find_by_dso_name(dso_name);
1264 if (map == NULL)
1265 continue;
1266
1267 snprintf(path, sizeof(path), "%s/%s",
1268 dirname, dent->d_name);
1269
cfc10d3b
ACM
1270 long_name = strdup(path);
1271 if (long_name == NULL)
439d473b 1272 goto failure;
cfc10d3b 1273 dso__set_long_name(map->dso, long_name);
439d473b 1274 }
439d473b 1275 }
6cfcc53e 1276
c338aee8 1277 return 0;
439d473b
ACM
1278failure:
1279 closedir(dir);
1280 return -1;
1281}
6cfcc53e 1282
c338aee8 1283static int dsos__set_modules_path(void)
439d473b
ACM
1284{
1285 struct utsname uts;
1286 char modules_path[PATH_MAX];
6cfcc53e 1287
439d473b
ACM
1288 if (uname(&uts) < 0)
1289 return -1;
6cfcc53e 1290
439d473b
ACM
1291 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1292 uts.release);
6cfcc53e 1293
c338aee8 1294 return dsos__set_modules_path_dir(modules_path);
6cfcc53e
MG
1295}
1296
439d473b
ACM
1297/*
1298 * Constructor variant for modules (where we know from /proc/modules where
1299 * they are loaded) and for vmlinux, where only after we load all the
1300 * symbols we'll know where it starts and ends.
1301 */
3610583c 1302static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
6cfcc53e 1303{
439d473b 1304 struct map *self = malloc(sizeof(*self));
6cfcc53e 1305
439d473b 1306 if (self != NULL) {
439d473b 1307 /*
afb7b4f0 1308 * ->end will be filled after we load all the symbols
439d473b 1309 */
3610583c 1310 map__init(self, type, start, 0, 0, dso);
439d473b 1311 }
afb7b4f0 1312
439d473b
ACM
1313 return self;
1314}
1315
c338aee8 1316static int kernel_maps__create_module_maps(void)
439d473b
ACM
1317{
1318 char *line = NULL;
1319 size_t n;
1320 FILE *file = fopen("/proc/modules", "r");
1321 struct map *map;
6cfcc53e 1322
439d473b
ACM
1323 if (file == NULL)
1324 return -1;
6cfcc53e 1325
439d473b
ACM
1326 while (!feof(file)) {
1327 char name[PATH_MAX];
1328 u64 start;
1329 struct dso *dso;
1330 char *sep;
1331 int line_len;
6cfcc53e 1332
439d473b
ACM
1333 line_len = getline(&line, &n, file);
1334 if (line_len < 0)
1335 break;
1336
1337 if (!line)
1338 goto out_failure;
1339
1340 line[--line_len] = '\0'; /* \n */
1341
1342 sep = strrchr(line, 'x');
1343 if (sep == NULL)
1344 continue;
1345
1346 hex2u64(sep + 1, &start);
1347
1348 sep = strchr(line, ' ');
1349 if (sep == NULL)
1350 continue;
1351
1352 *sep = '\0';
1353
1354 snprintf(name, sizeof(name), "[%s]", line);
00a192b3 1355 dso = dso__new(name);
439d473b
ACM
1356
1357 if (dso == NULL)
1358 goto out_delete_line;
1359
3610583c 1360 map = map__new2(start, dso, MAP__FUNCTION);
439d473b
ACM
1361 if (map == NULL) {
1362 dso__delete(dso);
1363 goto out_delete_line;
6cfcc53e 1364 }
439d473b 1365
f1617b40
ACM
1366 snprintf(name, sizeof(name),
1367 "/sys/module/%s/notes/.note.gnu.build-id", line);
1368 if (sysfs__read_build_id(name, dso->build_id,
1369 sizeof(dso->build_id)) == 0)
1370 dso->has_build_id = true;
1371
439d473b
ACM
1372 dso->origin = DSO__ORIG_KMODULE;
1373 kernel_maps__insert(map);
b0da954a 1374 dsos__add(&dsos__kernel, dso);
6cfcc53e 1375 }
439d473b
ACM
1376
1377 free(line);
1378 fclose(file);
1379
c338aee8 1380 return dsos__set_modules_path();
439d473b
ACM
1381
1382out_delete_line:
1383 free(line);
1384out_failure:
1385 return -1;
6cfcc53e
MG
1386}
1387
439d473b 1388static int dso__load_vmlinux(struct dso *self, struct map *map,
6beba7ad 1389 const char *vmlinux, symbol_filter_t filter)
a2928c42 1390{
fbd733b8 1391 int err = -1, fd;
a2928c42 1392
fbd733b8
ACM
1393 if (self->has_build_id) {
1394 u8 build_id[BUILD_ID_SIZE];
1395
1396 if (filename__read_build_id(vmlinux, build_id,
1397 sizeof(build_id)) < 0) {
1398 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1399 return -1;
1400 }
1401 if (!dso__build_id_equal(self, build_id)) {
1402 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1403 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1404
1405 build_id__sprintf(self->build_id,
1406 sizeof(self->build_id),
1407 expected_build_id);
1408 build_id__sprintf(build_id, sizeof(build_id),
1409 vmlinux_build_id);
1410 pr_debug("build_id in %s is %s while expected is %s, "
1411 "ignoring it\n", vmlinux, vmlinux_build_id,
1412 expected_build_id);
1413 return -1;
1414 }
1415 }
66bd8424 1416
fbd733b8 1417 fd = open(vmlinux, O_RDONLY);
a2928c42
ACM
1418 if (fd < 0)
1419 return -1;
1420
3610583c 1421 dso__set_loaded(self, map->type);
6beba7ad 1422 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
6cfcc53e 1423
a2928c42
ACM
1424 close(fd);
1425
1426 return err;
1427}
1428
c338aee8
ACM
1429static int dso__load_kernel_sym(struct dso *self, struct map *map,
1430 symbol_filter_t filter)
a827c875 1431{
cc612d81
ACM
1432 int err;
1433 bool is_kallsyms;
1434
1435 if (vmlinux_path != NULL) {
1436 int i;
1437 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1438 vmlinux_path__nr_entries);
1439 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1440 err = dso__load_vmlinux(self, map, vmlinux_path[i],
1441 filter);
1442 if (err > 0) {
1443 pr_debug("Using %s for symbols\n",
1444 vmlinux_path[i]);
1445 dso__set_long_name(self,
1446 strdup(vmlinux_path[i]));
1447 goto out_fixup;
1448 }
1449 }
1450 }
1451
1452 is_kallsyms = self->long_name[0] == '[';
1453 if (is_kallsyms)
1454 goto do_kallsyms;
439d473b 1455
cc612d81 1456 err = dso__load_vmlinux(self, map, self->long_name, filter);
ef6ae724 1457 if (err <= 0) {
cc612d81
ACM
1458 pr_info("The file %s cannot be used, "
1459 "trying to use /proc/kallsyms...", self->long_name);
1460 sleep(2);
1461do_kallsyms:
c338aee8 1462 err = kernel_maps__load_kallsyms(filter);
cc612d81 1463 if (err > 0 && !is_kallsyms)
ef6ae724
ACM
1464 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1465 }
439d473b
ACM
1466
1467 if (err > 0) {
cc612d81 1468out_fixup:
6a4694a4
ACM
1469 map__fixup_start(map);
1470 map__fixup_end(map);
439d473b 1471 }
94cb9e38 1472
a827c875
ACM
1473 return err;
1474}
1475
b0da954a
ACM
1476LIST_HEAD(dsos__user);
1477LIST_HEAD(dsos__kernel);
cc612d81 1478struct dso *vdso;
cd84c2ac 1479
b0da954a 1480static void dsos__add(struct list_head *head, struct dso *dso)
cd84c2ac 1481{
b0da954a 1482 list_add_tail(&dso->node, head);
cd84c2ac
FW
1483}
1484
b0da954a 1485static struct dso *dsos__find(struct list_head *head, const char *name)
cd84c2ac
FW
1486{
1487 struct dso *pos;
1488
b0da954a 1489 list_for_each_entry(pos, head, node)
cd84c2ac
FW
1490 if (strcmp(pos->name, name) == 0)
1491 return pos;
1492 return NULL;
1493}
1494
00a192b3 1495struct dso *dsos__findnew(const char *name)
cd84c2ac 1496{
b0da954a 1497 struct dso *dso = dsos__find(&dsos__user, name);
cd84c2ac 1498
e4204992 1499 if (!dso) {
00a192b3 1500 dso = dso__new(name);
cfc10d3b 1501 if (dso != NULL) {
b0da954a 1502 dsos__add(&dsos__user, dso);
cfc10d3b
ACM
1503 dso__set_basename(dso);
1504 }
66bd8424 1505 }
cd84c2ac
FW
1506
1507 return dso;
cd84c2ac
FW
1508}
1509
b0da954a 1510static void __dsos__fprintf(struct list_head *head, FILE *fp)
cd84c2ac
FW
1511{
1512 struct dso *pos;
1513
b0da954a 1514 list_for_each_entry(pos, head, node)
cd84c2ac
FW
1515 dso__fprintf(pos, fp);
1516}
1517
b0da954a
ACM
1518void dsos__fprintf(FILE *fp)
1519{
1520 __dsos__fprintf(&dsos__kernel, fp);
1521 __dsos__fprintf(&dsos__user, fp);
1522}
1523
1524static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
9e03eb2d
ACM
1525{
1526 struct dso *pos;
1527 size_t ret = 0;
1528
b0da954a 1529 list_for_each_entry(pos, head, node) {
9e03eb2d 1530 ret += dso__fprintf_buildid(pos, fp);
1124ba73 1531 ret += fprintf(fp, " %s\n", pos->long_name);
9e03eb2d
ACM
1532 }
1533 return ret;
1534}
1535
b0da954a
ACM
1536size_t dsos__fprintf_buildid(FILE *fp)
1537{
1538 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1539 __dsos__fprintf_buildid(&dsos__user, fp));
1540}
1541
b32d133a 1542static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
cd84c2ac 1543{
b32d133a 1544 struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");
cd84c2ac 1545
2446042c 1546 if (kernel == NULL)
c338aee8
ACM
1547 return -1;
1548
3610583c 1549 kernel_map__functions = map__new2(0, kernel, MAP__FUNCTION);
61f37a82 1550 if (kernel_map__functions == NULL)
c338aee8
ACM
1551 goto out_delete_kernel_dso;
1552
61f37a82 1553 kernel_map__functions->map_ip = kernel_map__functions->unmap_ip = identity__map_ip;
cc612d81
ACM
1554 kernel->short_name = "[kernel]";
1555 kernel->kernel = 1;
2446042c 1556
00a192b3 1557 vdso = dso__new("[vdso]");
c338aee8
ACM
1558 if (vdso == NULL)
1559 goto out_delete_kernel_map;
3610583c 1560 dso__set_loaded(vdso, MAP__FUNCTION);
2446042c
ACM
1561
1562 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1563 sizeof(kernel->build_id)) == 0)
1564 kernel->has_build_id = true;
cd84c2ac 1565
61f37a82 1566 kernel_maps__insert(kernel_map__functions);
b0da954a
ACM
1567 dsos__add(&dsos__kernel, kernel);
1568 dsos__add(&dsos__user, vdso);
cd84c2ac 1569
c338aee8
ACM
1570 return 0;
1571
1572out_delete_kernel_map:
61f37a82
ACM
1573 map__delete(kernel_map__functions);
1574 kernel_map__functions = NULL;
c338aee8
ACM
1575out_delete_kernel_dso:
1576 dso__delete(kernel);
1577 return -1;
2446042c
ACM
1578}
1579
cc612d81
ACM
1580static void vmlinux_path__exit(void)
1581{
1582 while (--vmlinux_path__nr_entries >= 0) {
1583 free(vmlinux_path[vmlinux_path__nr_entries]);
1584 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1585 }
1586
1587 free(vmlinux_path);
1588 vmlinux_path = NULL;
1589}
1590
1591static int vmlinux_path__init(void)
1592{
1593 struct utsname uts;
1594 char bf[PATH_MAX];
1595
1596 if (uname(&uts) < 0)
1597 return -1;
1598
1599 vmlinux_path = malloc(sizeof(char *) * 5);
1600 if (vmlinux_path == NULL)
1601 return -1;
1602
1603 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1604 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1605 goto out_fail;
1606 ++vmlinux_path__nr_entries;
1607 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1608 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1609 goto out_fail;
1610 ++vmlinux_path__nr_entries;
1611 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1612 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1613 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1614 goto out_fail;
1615 ++vmlinux_path__nr_entries;
1616 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1617 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1618 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1619 goto out_fail;
1620 ++vmlinux_path__nr_entries;
1621 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1622 uts.release);
1623 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1624 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1625 goto out_fail;
1626 ++vmlinux_path__nr_entries;
1627
1628 return 0;
1629
1630out_fail:
1631 vmlinux_path__exit();
1632 return -1;
1633}
1634
b32d133a 1635static int kernel_maps__init(const struct symbol_conf *conf)
2446042c 1636{
b32d133a
ACM
1637 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1638
1639 symbol__priv_size = pconf->priv_size;
1640
1641 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
1642 return -1;
1643
b32d133a 1644 if (kernel_maps__create_kernel_map(pconf) < 0) {
cc612d81
ACM
1645 vmlinux_path__exit();
1646 return -1;
1647 }
1648
b32d133a 1649 if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
87f8ea4c
ACM
1650 pr_debug("Failed to load list of modules in use, "
1651 "continuing...\n");
90c83218
ACM
1652 /*
1653 * Now that we have all the maps created, just set the ->end of them:
1654 */
1655 kernel_maps__fixup_end();
6671cb16 1656 return 0;
cd84c2ac
FW
1657}
1658
b32d133a 1659int symbol__init(struct symbol_conf *conf)
a2928c42
ACM
1660{
1661 elf_version(EV_CURRENT);
b32d133a 1662 return kernel_maps__init(conf);
a2928c42 1663}