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