]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/symbol.c
perf tools: Move the build-id storage operations to headers
[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>
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
2643ce11 854int filename__read_build_id(const char *filename, void *bf, size_t size)
4d1e00a8 855{
2643ce11 856 int fd, err = -1;
4d1e00a8
ACM
857 GElf_Ehdr ehdr;
858 GElf_Shdr shdr;
859 Elf_Data *build_id_data;
860 Elf_Scn *sec;
4d1e00a8 861 Elf *elf;
4d1e00a8 862
2643ce11
ACM
863 if (size < BUILD_ID_SIZE)
864 goto out;
865
866 fd = open(filename, O_RDONLY);
4d1e00a8
ACM
867 if (fd < 0)
868 goto out;
869
84087126 870 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
4d1e00a8 871 if (elf == NULL) {
8d06367f 872 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
4d1e00a8
ACM
873 goto out_close;
874 }
875
876 if (gelf_getehdr(elf, &ehdr) == NULL) {
6beba7ad 877 pr_err("%s: cannot get elf header.\n", __func__);
4d1e00a8
ACM
878 goto out_elf_end;
879 }
880
2643ce11
ACM
881 sec = elf_section_by_name(elf, &ehdr, &shdr,
882 ".note.gnu.build-id", NULL);
4d1e00a8
ACM
883 if (sec == NULL)
884 goto out_elf_end;
885
886 build_id_data = elf_getdata(sec, NULL);
887 if (build_id_data == NULL)
888 goto out_elf_end;
2643ce11
ACM
889 memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE);
890 err = BUILD_ID_SIZE;
891out_elf_end:
892 elf_end(elf);
893out_close:
894 close(fd);
895out:
896 return err;
897}
898
899static char *dso__read_build_id(struct dso *self)
900{
8d06367f
ACM
901 int len;
902 char *build_id = NULL;
903 unsigned char rawbf[BUILD_ID_SIZE];
2643ce11
ACM
904
905 len = filename__read_build_id(self->long_name, rawbf, sizeof(rawbf));
906 if (len < 0)
907 goto out;
908
909 build_id = malloc(len * 2 + 1);
4d1e00a8 910 if (build_id == NULL)
2643ce11 911 goto out;
4d1e00a8 912
8d06367f 913 build_id__sprintf(rawbf, len, build_id);
4d1e00a8
ACM
914out:
915 return build_id;
916}
917
94cb9e38
ACM
918char dso__symtab_origin(const struct dso *self)
919{
920 static const char origin[] = {
921 [DSO__ORIG_KERNEL] = 'k',
922 [DSO__ORIG_JAVA_JIT] = 'j',
923 [DSO__ORIG_FEDORA] = 'f',
924 [DSO__ORIG_UBUNTU] = 'u',
925 [DSO__ORIG_BUILDID] = 'b',
926 [DSO__ORIG_DSO] = 'd',
439d473b 927 [DSO__ORIG_KMODULE] = 'K',
94cb9e38
ACM
928 };
929
930 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
931 return '!';
932 return origin[self->origin];
933}
934
6beba7ad 935int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
a2928c42 936{
4d1e00a8
ACM
937 int size = PATH_MAX;
938 char *name = malloc(size), *build_id = NULL;
a2928c42
ACM
939 int ret = -1;
940 int fd;
941
8d06367f 942 self->loaded = 1;
66bd8424 943
a2928c42
ACM
944 if (!name)
945 return -1;
946
30d7a77d 947 self->adjust_symbols = 0;
f5812a7a 948
94cb9e38 949 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
6beba7ad 950 ret = dso__load_perf_map(self, map, filter);
94cb9e38
ACM
951 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
952 DSO__ORIG_NOT_FOUND;
953 return ret;
954 }
955
956 self->origin = DSO__ORIG_FEDORA - 1;
80d496be 957
a2928c42
ACM
958more:
959 do {
8d06367f
ACM
960 int berr = 0;
961
94cb9e38
ACM
962 self->origin++;
963 switch (self->origin) {
964 case DSO__ORIG_FEDORA:
439d473b
ACM
965 snprintf(name, size, "/usr/lib/debug%s.debug",
966 self->long_name);
a2928c42 967 break;
94cb9e38 968 case DSO__ORIG_UBUNTU:
439d473b
ACM
969 snprintf(name, size, "/usr/lib/debug%s",
970 self->long_name);
a2928c42 971 break;
94cb9e38 972 case DSO__ORIG_BUILDID:
6beba7ad 973 build_id = dso__read_build_id(self);
4d1e00a8
ACM
974 if (build_id != NULL) {
975 snprintf(name, size,
976 "/usr/lib/debug/.build-id/%.2s/%s.debug",
977 build_id, build_id + 2);
8d06367f 978 goto compare_build_id;
4d1e00a8 979 }
94cb9e38 980 self->origin++;
4d1e00a8 981 /* Fall thru */
94cb9e38 982 case DSO__ORIG_DSO:
439d473b 983 snprintf(name, size, "%s", self->long_name);
a2928c42
ACM
984 break;
985
986 default:
987 goto out;
988 }
a2928c42 989
8d06367f
ACM
990 if (self->has_build_id) {
991 bool match;
992 build_id = malloc(BUILD_ID_SIZE);
993 if (build_id == NULL)
994 goto more;
995 berr = filename__read_build_id(name, build_id,
996 BUILD_ID_SIZE);
997compare_build_id:
998 match = berr > 0 && memcmp(build_id, self->build_id,
999 sizeof(self->build_id)) == 0;
1000 free(build_id);
1001 build_id = NULL;
1002 if (!match)
1003 goto more;
1004 }
1005
a2928c42
ACM
1006 fd = open(name, O_RDONLY);
1007 } while (fd < 0);
1008
6beba7ad 1009 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
a2928c42
ACM
1010 close(fd);
1011
1012 /*
1013 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1014 */
1015 if (!ret)
1016 goto more;
1017
a25e46c4 1018 if (ret > 0) {
6beba7ad 1019 int nr_plt = dso__synthesize_plt_symbols(self);
a25e46c4
ACM
1020 if (nr_plt > 0)
1021 ret += nr_plt;
1022 }
a2928c42
ACM
1023out:
1024 free(name);
1340e6bb
ACM
1025 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1026 return 0;
a2928c42
ACM
1027 return ret;
1028}
1029
439d473b
ACM
1030struct map *kernel_map;
1031
1032static void kernel_maps__insert(struct map *map)
6cfcc53e 1033{
439d473b
ACM
1034 maps__insert(&kernel_maps, map);
1035}
6cfcc53e 1036
439d473b
ACM
1037struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1038{
439d473b 1039 struct map *map = maps__find(&kernel_maps, ip);
2e538c4a
ACM
1040
1041 if (mapp)
1042 *mapp = map;
439d473b
ACM
1043
1044 if (map) {
1045 ip = map->map_ip(map, ip);
2e538c4a 1046 return map->dso->find_symbol(map->dso, ip);
439d473b 1047 }
6cfcc53e 1048
2e538c4a 1049 return NULL;
439d473b
ACM
1050}
1051
1052struct map *kernel_maps__find_by_dso_name(const char *name)
1053{
1054 struct rb_node *nd;
1055
1056 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1057 struct map *map = rb_entry(nd, struct map, rb_node);
1058
1059 if (map->dso && strcmp(map->dso->name, name) == 0)
1060 return map;
1061 }
1062
1063 return NULL;
1064}
1065
1066static int dso__load_module_sym(struct dso *self, struct map *map,
6beba7ad 1067 symbol_filter_t filter)
439d473b
ACM
1068{
1069 int err = 0, fd = open(self->long_name, O_RDONLY);
1070
8d06367f 1071 self->loaded = 1;
66bd8424 1072
439d473b 1073 if (fd < 0) {
6beba7ad 1074 pr_err("%s: cannot open %s\n", __func__, self->long_name);
6cfcc53e 1075 return err;
439d473b 1076 }
6cfcc53e 1077
6beba7ad 1078 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
6cfcc53e
MG
1079 close(fd);
1080
1081 return err;
1082}
1083
6beba7ad 1084static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
6cfcc53e 1085{
439d473b
ACM
1086 struct dirent *dent;
1087 int nr_symbols = 0, err;
1088 DIR *dir = opendir(dirname);
6cfcc53e 1089
439d473b 1090 if (!dir) {
6beba7ad 1091 pr_err("%s: cannot open %s dir\n", __func__, dirname);
439d473b
ACM
1092 return -1;
1093 }
6cfcc53e 1094
439d473b
ACM
1095 while ((dent = readdir(dir)) != NULL) {
1096 char path[PATH_MAX];
1097
1098 if (dent->d_type == DT_DIR) {
1099 if (!strcmp(dent->d_name, ".") ||
1100 !strcmp(dent->d_name, ".."))
1101 continue;
1102
1103 snprintf(path, sizeof(path), "%s/%s",
1104 dirname, dent->d_name);
6beba7ad 1105 err = dsos__load_modules_sym_dir(path, filter);
439d473b
ACM
1106 if (err < 0)
1107 goto failure;
1108 } else {
1109 char *dot = strrchr(dent->d_name, '.'),
1110 dso_name[PATH_MAX];
1111 struct map *map;
1112 struct rb_node *last;
1113
1114 if (dot == NULL || strcmp(dot, ".ko"))
1115 continue;
1116 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1117 (int)(dot - dent->d_name), dent->d_name);
1118
a2a99e8e 1119 strxfrchar(dso_name, '-', '_');
439d473b
ACM
1120 map = kernel_maps__find_by_dso_name(dso_name);
1121 if (map == NULL)
1122 continue;
1123
1124 snprintf(path, sizeof(path), "%s/%s",
1125 dirname, dent->d_name);
1126
1127 map->dso->long_name = strdup(path);
1128 if (map->dso->long_name == NULL)
1129 goto failure;
1130
6beba7ad 1131 err = dso__load_module_sym(map->dso, map, filter);
439d473b
ACM
1132 if (err < 0)
1133 goto failure;
1134 last = rb_last(&map->dso->syms);
1135 if (last) {
1136 struct symbol *sym;
2e538c4a
ACM
1137 /*
1138 * We do this here as well, even having the
1139 * symbol size found in the symtab because
1140 * misannotated ASM symbols may have the size
1141 * set to zero.
1142 */
1143 dso__fixup_sym_end(map->dso);
1144
439d473b
ACM
1145 sym = rb_entry(last, struct symbol, rb_node);
1146 map->end = map->start + sym->end;
1147 }
1148 }
1149 nr_symbols += err;
1150 }
6cfcc53e 1151
439d473b
ACM
1152 return nr_symbols;
1153failure:
1154 closedir(dir);
1155 return -1;
1156}
6cfcc53e 1157
6beba7ad 1158static int dsos__load_modules_sym(symbol_filter_t filter)
439d473b
ACM
1159{
1160 struct utsname uts;
1161 char modules_path[PATH_MAX];
6cfcc53e 1162
439d473b
ACM
1163 if (uname(&uts) < 0)
1164 return -1;
6cfcc53e 1165
439d473b
ACM
1166 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1167 uts.release);
6cfcc53e 1168
6beba7ad 1169 return dsos__load_modules_sym_dir(modules_path, filter);
6cfcc53e
MG
1170}
1171
439d473b
ACM
1172/*
1173 * Constructor variant for modules (where we know from /proc/modules where
1174 * they are loaded) and for vmlinux, where only after we load all the
1175 * symbols we'll know where it starts and ends.
1176 */
1177static struct map *map__new2(u64 start, struct dso *dso)
6cfcc53e 1178{
439d473b 1179 struct map *self = malloc(sizeof(*self));
6cfcc53e 1180
439d473b 1181 if (self != NULL) {
439d473b 1182 /*
afb7b4f0 1183 * ->end will be filled after we load all the symbols
439d473b 1184 */
afb7b4f0 1185 map__init(self, start, 0, 0, dso);
439d473b 1186 }
afb7b4f0 1187
439d473b
ACM
1188 return self;
1189}
1190
00a192b3 1191static int dsos__load_modules(void)
439d473b
ACM
1192{
1193 char *line = NULL;
1194 size_t n;
1195 FILE *file = fopen("/proc/modules", "r");
1196 struct map *map;
6cfcc53e 1197
439d473b
ACM
1198 if (file == NULL)
1199 return -1;
6cfcc53e 1200
439d473b
ACM
1201 while (!feof(file)) {
1202 char name[PATH_MAX];
1203 u64 start;
1204 struct dso *dso;
1205 char *sep;
1206 int line_len;
6cfcc53e 1207
439d473b
ACM
1208 line_len = getline(&line, &n, file);
1209 if (line_len < 0)
1210 break;
1211
1212 if (!line)
1213 goto out_failure;
1214
1215 line[--line_len] = '\0'; /* \n */
1216
1217 sep = strrchr(line, 'x');
1218 if (sep == NULL)
1219 continue;
1220
1221 hex2u64(sep + 1, &start);
1222
1223 sep = strchr(line, ' ');
1224 if (sep == NULL)
1225 continue;
1226
1227 *sep = '\0';
1228
1229 snprintf(name, sizeof(name), "[%s]", line);
00a192b3 1230 dso = dso__new(name);
439d473b
ACM
1231
1232 if (dso == NULL)
1233 goto out_delete_line;
1234
1235 map = map__new2(start, dso);
1236 if (map == NULL) {
1237 dso__delete(dso);
1238 goto out_delete_line;
6cfcc53e 1239 }
439d473b
ACM
1240
1241 dso->origin = DSO__ORIG_KMODULE;
1242 kernel_maps__insert(map);
1243 dsos__add(dso);
6cfcc53e 1244 }
439d473b
ACM
1245
1246 free(line);
1247 fclose(file);
1248
af427bf5 1249 return 0;
439d473b
ACM
1250
1251out_delete_line:
1252 free(line);
1253out_failure:
1254 return -1;
6cfcc53e
MG
1255}
1256
439d473b 1257static int dso__load_vmlinux(struct dso *self, struct map *map,
6beba7ad 1258 const char *vmlinux, symbol_filter_t filter)
a2928c42
ACM
1259{
1260 int err, fd = open(vmlinux, O_RDONLY);
1261
8d06367f 1262 self->loaded = 1;
66bd8424 1263
a2928c42
ACM
1264 if (fd < 0)
1265 return -1;
1266
6beba7ad 1267 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
6cfcc53e 1268
a2928c42
ACM
1269 close(fd);
1270
1271 return err;
1272}
1273
00a192b3
ACM
1274int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter,
1275 int use_modules)
a827c875
ACM
1276{
1277 int err = -1;
00a192b3 1278 struct dso *dso = dso__new(vmlinux);
439d473b
ACM
1279
1280 if (dso == NULL)
1281 return -1;
1282
1283 dso->short_name = "[kernel]";
1284 kernel_map = map__new2(0, dso);
1285 if (kernel_map == NULL)
1286 goto out_delete_dso;
1287
ed52ce2e 1288 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
a827c875 1289
00a192b3 1290 if (use_modules && dsos__load_modules() < 0) {
6beba7ad
ACM
1291 pr_warning("Failed to load list of modules in use! "
1292 "Continuing...\n");
af427bf5
ACM
1293 use_modules = 0;
1294 }
1295
6cfcc53e 1296 if (vmlinux) {
6beba7ad 1297 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
508c4d08 1298 if (err > 0 && use_modules) {
6beba7ad 1299 int syms = dsos__load_modules_sym(filter);
508c4d08 1300
af427bf5 1301 if (syms < 0)
6beba7ad
ACM
1302 pr_warning("Failed to read module symbols!"
1303 " Continuing...\n");
af427bf5
ACM
1304 else
1305 err += syms;
508c4d08 1306 }
6cfcc53e 1307 }
a827c875 1308
9974f496 1309 if (err <= 0)
6beba7ad 1310 err = kernel_maps__load_kallsyms(filter, use_modules);
439d473b
ACM
1311
1312 if (err > 0) {
1313 struct rb_node *node = rb_first(&dso->syms);
1314 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
a827c875 1315
439d473b
ACM
1316 kernel_map->start = sym->start;
1317 node = rb_last(&dso->syms);
1318 sym = rb_entry(node, struct symbol, rb_node);
1319 kernel_map->end = sym->end;
1320
1321 dso->origin = DSO__ORIG_KERNEL;
2e538c4a 1322 kernel_maps__insert(kernel_map);
439d473b 1323 /*
2e538c4a
ACM
1324 * Now that we have all sorted out, just set the ->end of all
1325 * maps:
439d473b 1326 */
2e538c4a 1327 kernel_maps__fixup_end();
439d473b 1328 dsos__add(dso);
af427bf5 1329
6beba7ad
ACM
1330 if (verbose)
1331 kernel_maps__fprintf(stderr);
439d473b 1332 }
94cb9e38 1333
a827c875 1334 return err;
439d473b
ACM
1335
1336out_delete_dso:
1337 dso__delete(dso);
1338 return -1;
a827c875
ACM
1339}
1340
cd84c2ac 1341LIST_HEAD(dsos);
cd84c2ac 1342struct dso *vdso;
cd84c2ac 1343
83a0944f 1344const char *vmlinux_name = "vmlinux";
cd84c2ac
FW
1345int modules;
1346
1347static void dsos__add(struct dso *dso)
1348{
1349 list_add_tail(&dso->node, &dsos);
1350}
1351
1352static struct dso *dsos__find(const char *name)
1353{
1354 struct dso *pos;
1355
1356 list_for_each_entry(pos, &dsos, node)
1357 if (strcmp(pos->name, name) == 0)
1358 return pos;
1359 return NULL;
1360}
1361
00a192b3 1362struct dso *dsos__findnew(const char *name)
cd84c2ac
FW
1363{
1364 struct dso *dso = dsos__find(name);
cd84c2ac 1365
e4204992 1366 if (!dso) {
00a192b3 1367 dso = dso__new(name);
66bd8424 1368 if (dso != NULL)
e4204992 1369 dsos__add(dso);
66bd8424 1370 }
cd84c2ac
FW
1371
1372 return dso;
cd84c2ac
FW
1373}
1374
1375void dsos__fprintf(FILE *fp)
1376{
1377 struct dso *pos;
1378
1379 list_for_each_entry(pos, &dsos, node)
1380 dso__fprintf(pos, fp);
1381}
1382
00a192b3 1383int load_kernel(symbol_filter_t filter)
cd84c2ac 1384{
00a192b3 1385 if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0)
cd84c2ac
FW
1386 return -1;
1387
00a192b3 1388 vdso = dso__new("[vdso]");
cd84c2ac
FW
1389 if (!vdso)
1390 return -1;
1391
cd84c2ac
FW
1392 dsos__add(vdso);
1393
439d473b 1394 return 0;
cd84c2ac
FW
1395}
1396
00a192b3 1397void symbol__init(unsigned int priv_size)
a2928c42
ACM
1398{
1399 elf_version(EV_CURRENT);
00a192b3 1400 symbol__priv_size = priv_size;
a2928c42 1401}