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