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