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