perf symbols: Use the buildids if present
[safe/jmp/linux-2.6] / tools / perf / util / symbol.c
1 #include "util.h"
2 #include "../perf.h"
3 #include "string.h"
4 #include "symbol.h"
5 #include "thread.h"
6
7 #include "debug.h"
8
9 #include <libelf.h>
10 #include <gelf.h>
11 #include <elf.h>
12 #include <sys/utsname.h>
13
14 enum 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,
21         DSO__ORIG_KMODULE,
22         DSO__ORIG_NOT_FOUND,
23 };
24
25 static void dsos__add(struct dso *dso);
26 static struct dso *dsos__find(const char *name);
27 static struct map *map__new2(u64 start, struct dso *dso);
28 static void kernel_maps__insert(struct map *map);
29 unsigned int symbol__priv_size;
30
31 static struct rb_root kernel_maps;
32
33 static void dso__fixup_sym_end(struct dso *self)
34 {
35         struct rb_node *nd, *prevnd = rb_first(&self->syms);
36         struct symbol *curr, *prev;
37
38         if (prevnd == NULL)
39                 return;
40
41         curr = rb_entry(prevnd, struct symbol, rb_node);
42
43         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
44                 prev = curr;
45                 curr = rb_entry(nd, struct symbol, rb_node);
46
47                 if (prev->end == prev->start)
48                         prev->end = curr->start - 1;
49         }
50
51         /* Last entry */
52         if (curr->end == curr->start)
53                 curr->end = roundup(curr->start, 4096);
54 }
55
56 static void kernel_maps__fixup_end(void)
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);
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;
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;
76         }
77 }
78
79 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
80 {
81         size_t namelen = strlen(name) + 1;
82         struct symbol *self = calloc(1, (symbol__priv_size +
83                                          sizeof(*self) + namelen));
84         if (!self)
85                 return NULL;
86
87         if (symbol__priv_size) {
88                 memset(self, 0, symbol__priv_size);
89                 self = ((void *)self) + symbol__priv_size;
90         }
91         self->start = start;
92         self->end   = len ? start + len - 1 : start;
93
94         pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
95
96         memcpy(self->name, name, namelen);
97
98         return self;
99 }
100
101 static void symbol__delete(struct symbol *self)
102 {
103         free(((void *)self) - symbol__priv_size);
104 }
105
106 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
107 {
108         return fprintf(fp, " %llx-%llx %s\n",
109                        self->start, self->end, self->name);
110 }
111
112 struct dso *dso__new(const char *name)
113 {
114         struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
115
116         if (self != NULL) {
117                 strcpy(self->name, name);
118                 self->long_name = self->name;
119                 self->short_name = self->name;
120                 self->syms = RB_ROOT;
121                 self->find_symbol = dso__find_symbol;
122                 self->slen_calculated = 0;
123                 self->origin = DSO__ORIG_NOT_FOUND;
124                 self->loaded = 0;
125                 self->has_build_id = 0;
126         }
127
128         return self;
129 }
130
131 static 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);
139                 rb_erase(&pos->rb_node, &self->syms);
140                 symbol__delete(pos);
141         }
142 }
143
144 void dso__delete(struct dso *self)
145 {
146         dso__delete_symbols(self);
147         if (self->long_name != self->name)
148                 free(self->long_name);
149         free(self);
150 }
151
152 void 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
158 static 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;
162         const u64 ip = sym->start;
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
177 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
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
200 int build_id__sprintf(u8 *self, int len, char *bf)
201 {
202         char *bid = bf;
203         u8 *raw = self;
204         int i;
205
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
215 size_t dso__fprintf(struct dso *self, FILE *fp)
216 {
217         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
218         struct rb_node *nd;
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
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
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  */
237 static int kernel_maps__load_all_kallsyms(void)
238 {
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)) {
247                 u64 start;
248                 struct symbol *sym;
249                 int line_len, len;
250                 char symbol_type;
251                 char *symbol_name;
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
262                 len = hex2u64(line, &start);
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;
274
275                 symbol_name = line + len + 2;
276                 /*
277                  * Will fix up the end later, when we have all symbols sorted.
278                  */
279                 sym = symbol__new(start, 0, symbol_name);
280
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
292 out_delete_line:
293         free(line);
294 out_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  */
303 static 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) {
319                         if (!use_modules)
320                                 goto delete_symbol;
321
322                         *module++ = '\0';
323
324                         if (strcmp(map->dso->name, module)) {
325                                 map = kernel_maps__find_by_dso_name(module);
326                                 if (!map) {
327                                         pr_err("/proc/{kallsyms,modules} "
328                                                "inconsistency!\n");
329                                         return -1;
330                                 }
331                         }
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
345                         dso = dso__new(dso_name);
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                         }
354
355                         map->map_ip = map->unmap_ip = identity__map_ip;
356                         kernel_maps__insert(map);
357                         ++kernel_range;
358                 }
359
360                 if (filter && filter(map, pos)) {
361 delete_symbol:
362                         rb_erase(&pos->rb_node, &kernel_map->dso->syms);
363                         symbol__delete(pos);
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                         }
369                         count++;
370                 }
371         }
372
373         return count;
374 }
375
376
377 static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
378 {
379         if (kernel_maps__load_all_kallsyms())
380                 return -1;
381
382         dso__fixup_sym_end(kernel_map->dso);
383
384         return kernel_maps__split_kallsyms(filter, use_modules);
385 }
386
387 static size_t kernel_maps__fprintf(FILE *fp)
388 {
389         size_t printed = fprintf(fp, "Kernel maps:\n");
390         struct rb_node *nd;
391
392         for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
393                 struct map *pos = rb_entry(nd, struct map, rb_node);
394
395                 printed += fprintf(fp, "Map:");
396                 printed += map__fprintf(pos, fp);
397                 if (verbose > 1) {
398                         printed += dso__fprintf(pos->dso, fp);
399                         printed += fprintf(fp, "--\n");
400                 }
401         }
402
403         return printed + fprintf(fp, "END kernel maps\n");
404 }
405
406 static int dso__load_perf_map(struct dso *self, struct map *map,
407                               symbol_filter_t filter)
408 {
409         char *line = NULL;
410         size_t n;
411         FILE *file;
412         int nr_syms = 0;
413
414         file = fopen(self->long_name, "r");
415         if (file == NULL)
416                 goto out_failure;
417
418         while (!feof(file)) {
419                 u64 start, size;
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
444                 sym = symbol__new(start, size, line + len);
445
446                 if (sym == NULL)
447                         goto out_delete_line;
448
449                 if (filter && filter(map, sym))
450                         symbol__delete(sym);
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
462 out_delete_line:
463         free(line);
464 out_failure:
465         return -1;
466 }
467
468 /**
469  * elf_symtab__for_each_symbol - iterate thru all the symbols
470  *
471  * @self: struct elf_symtab instance to iterate
472  * @idx: uint32_t idx
473  * @sym: GElf_Sym iterator
474  */
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))
479
480 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
481 {
482         return GELF_ST_TYPE(sym->st_info);
483 }
484
485 static inline int elf_sym__is_function(const GElf_Sym *sym)
486 {
487         return elf_sym__type(sym) == STT_FUNC &&
488                sym->st_name != 0 &&
489                sym->st_shndx != SHN_UNDEF;
490 }
491
492 static 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
500 static 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
506 static 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
512 static 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
518 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
519                                     GElf_Shdr *shp, const char *name,
520                                     size_t *idx)
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)) {
531                         if (idx)
532                                 *idx = cnt;
533                         break;
534                 }
535                 ++cnt;
536         }
537
538         return sec;
539 }
540
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
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  */
558 static int dso__synthesize_plt_symbols(struct  dso *self)
559 {
560         uint32_t nr_rel_entries, idx;
561         GElf_Sym sym;
562         u64 plt_offset;
563         GElf_Shdr shdr_plt;
564         struct symbol *f;
565         GElf_Shdr shdr_rel_plt, shdr_dynsym;
566         Elf_Data *reldata, *syms, *symstrs;
567         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
568         size_t dynsym_idx;
569         GElf_Ehdr ehdr;
570         char sympltname[1024];
571         Elf *elf;
572         int nr = 0, symidx, fd, err = 0;
573
574         fd = open(self->long_name, O_RDONLY);
575         if (fd < 0)
576                 goto out;
577
578         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
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;
589
590         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
591                                           ".rela.plt", NULL);
592         if (scn_plt_rel == NULL) {
593                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
594                                                   ".rel.plt", NULL);
595                 if (scn_plt_rel == NULL)
596                         goto out_elf_end;
597         }
598
599         err = -1;
600
601         if (shdr_rel_plt.sh_link != dynsym_idx)
602                 goto out_elf_end;
603
604         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
605                 goto out_elf_end;
606
607         /*
608          * Fetch the relocation section to find the idxes to the GOT
609          * and the symbols in the .dynsym they refer to.
610          */
611         reldata = elf_getdata(scn_plt_rel, NULL);
612         if (reldata == NULL)
613                 goto out_elf_end;
614
615         syms = elf_getdata(scn_dynsym, NULL);
616         if (syms == NULL)
617                 goto out_elf_end;
618
619         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
620         if (scn_symstrs == NULL)
621                 goto out_elf_end;
622
623         symstrs = elf_getdata(scn_symstrs, NULL);
624         if (symstrs == NULL)
625                 goto out_elf_end;
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,
642                                         sympltname);
643                         if (!f)
644                                 goto out_elf_end;
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,
660                                         sympltname);
661                         if (!f)
662                                 goto out_elf_end;
663
664                         dso__insert_symbol(self, f);
665                         ++nr;
666                 }
667         }
668
669         err = 0;
670 out_elf_end:
671         elf_end(elf);
672 out_close:
673         close(fd);
674
675         if (err == 0)
676                 return nr;
677 out:
678         pr_warning("%s: problems reading %s PLT info.\n",
679                    __func__, self->long_name);
680         return 0;
681 }
682
683 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
684                          int fd, symbol_filter_t filter, int kernel,
685                          int kmodule)
686 {
687         struct map *curr_map = map;
688         struct dso *curr_dso = self;
689         size_t dso_name_len = strlen(self->short_name);
690         Elf_Data *symstrs, *secstrs;
691         uint32_t nr_syms;
692         int err = -1;
693         uint32_t idx;
694         GElf_Ehdr ehdr;
695         GElf_Shdr shdr;
696         Elf_Data *syms;
697         GElf_Sym sym;
698         Elf_Scn *sec, *sec_strndx;
699         Elf *elf;
700         int nr = 0;
701
702         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
703         if (elf == NULL) {
704                 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
705                 goto out_close;
706         }
707
708         if (gelf_getehdr(elf, &ehdr) == NULL) {
709                 pr_err("%s: cannot get elf header.\n", __func__);
710                 goto out_elf_end;
711         }
712
713         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
714         if (sec == NULL) {
715                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
716                 if (sec == NULL)
717                         goto out_elf_end;
718         }
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
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);
737         if (secstrs == NULL)
738                 goto out_elf_end;
739
740         nr_syms = shdr.sh_size / shdr.sh_entsize;
741
742         memset(&sym, 0, sizeof(sym));
743         if (!kernel) {
744                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
745                                 elf_section_by_name(elf, &ehdr, &shdr,
746                                                      ".gnu.prelink_undo",
747                                                      NULL) != NULL);
748         } else self->adjust_symbols = 0;
749
750         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
751                 struct symbol *f;
752                 const char *elf_name;
753                 char *demangled = NULL;
754                 int is_label = elf_sym__is_label(&sym);
755                 const char *section_name;
756
757                 if (!is_label && !elf_sym__is_function(&sym))
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);
765
766                 if (is_label && !elf_sec__is_text(&shdr, secstrs))
767                         continue;
768
769                 elf_name = elf_sym__name(&sym, symstrs);
770                 section_name = elf_sec__name(&shdr, secstrs);
771
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
795                                 curr_dso = dso__new(dso_name);
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                                 }
803                                 curr_map->map_ip = identity__map_ip;
804                                 curr_map->unmap_ip = identity__map_ip;
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;
812                 }
813
814                 if (curr_dso->adjust_symbols) {
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);
818                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
819                 }
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                  */
825                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
826                 if (demangled != NULL)
827                         elf_name = demangled;
828 new_symbol:
829                 f = symbol__new(sym.st_value, sym.st_size, elf_name);
830                 free(demangled);
831                 if (!f)
832                         goto out_elf_end;
833
834                 if (filter && filter(curr_map, f))
835                         symbol__delete(f);
836                 else {
837                         dso__insert_symbol(curr_dso, f);
838                         nr++;
839                 }
840         }
841
842         /*
843          * For misannotated, zeroed, ASM function sizes.
844          */
845         if (nr > 0)
846                 dso__fixup_sym_end(self);
847         err = nr;
848 out_elf_end:
849         elf_end(elf);
850 out_close:
851         return err;
852 }
853
854 int filename__read_build_id(const char *filename, void *bf, size_t size)
855 {
856         int fd, err = -1;
857         GElf_Ehdr ehdr;
858         GElf_Shdr shdr;
859         Elf_Data *build_id_data;
860         Elf_Scn *sec;
861         Elf *elf;
862
863         if (size < BUILD_ID_SIZE)
864                 goto out;
865
866         fd = open(filename, O_RDONLY);
867         if (fd < 0)
868                 goto out;
869
870         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
871         if (elf == NULL) {
872                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
873                 goto out_close;
874         }
875
876         if (gelf_getehdr(elf, &ehdr) == NULL) {
877                 pr_err("%s: cannot get elf header.\n", __func__);
878                 goto out_elf_end;
879         }
880
881         sec = elf_section_by_name(elf, &ehdr, &shdr,
882                                   ".note.gnu.build-id", NULL);
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;
889         memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE);
890         err = BUILD_ID_SIZE;
891 out_elf_end:
892         elf_end(elf);
893 out_close:
894         close(fd);
895 out:
896         return err;
897 }
898
899 static char *dso__read_build_id(struct dso *self)
900 {
901         int len;
902         char *build_id = NULL;
903         unsigned char rawbf[BUILD_ID_SIZE];
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);
910         if (build_id == NULL)
911                 goto out;
912
913         build_id__sprintf(rawbf, len, build_id);
914 out:
915         return build_id;
916 }
917
918 char 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',
927                 [DSO__ORIG_KMODULE] =  'K',
928         };
929
930         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
931                 return '!';
932         return origin[self->origin];
933 }
934
935 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
936 {
937         int size = PATH_MAX;
938         char *name = malloc(size), *build_id = NULL;
939         int ret = -1;
940         int fd;
941
942         self->loaded = 1;
943
944         if (!name)
945                 return -1;
946
947         self->adjust_symbols = 0;
948
949         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
950                 ret = dso__load_perf_map(self, map, filter);
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;
957
958 more:
959         do {
960                 int berr = 0;
961
962                 self->origin++;
963                 switch (self->origin) {
964                 case DSO__ORIG_FEDORA:
965                         snprintf(name, size, "/usr/lib/debug%s.debug",
966                                  self->long_name);
967                         break;
968                 case DSO__ORIG_UBUNTU:
969                         snprintf(name, size, "/usr/lib/debug%s",
970                                  self->long_name);
971                         break;
972                 case DSO__ORIG_BUILDID:
973                         build_id = dso__read_build_id(self);
974                         if (build_id != NULL) {
975                                 snprintf(name, size,
976                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
977                                         build_id, build_id + 2);
978                                 goto compare_build_id;
979                         }
980                         self->origin++;
981                         /* Fall thru */
982                 case DSO__ORIG_DSO:
983                         snprintf(name, size, "%s", self->long_name);
984                         break;
985
986                 default:
987                         goto out;
988                 }
989
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);
997 compare_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
1006                 fd = open(name, O_RDONLY);
1007         } while (fd < 0);
1008
1009         ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1010         close(fd);
1011
1012         /*
1013          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1014          */
1015         if (!ret)
1016                 goto more;
1017
1018         if (ret > 0) {
1019                 int nr_plt = dso__synthesize_plt_symbols(self);
1020                 if (nr_plt > 0)
1021                         ret += nr_plt;
1022         }
1023 out:
1024         free(name);
1025         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1026                 return 0;
1027         return ret;
1028 }
1029
1030 struct map *kernel_map;
1031
1032 static void kernel_maps__insert(struct map *map)
1033 {
1034         maps__insert(&kernel_maps, map);
1035 }
1036
1037 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1038 {
1039         struct map *map = maps__find(&kernel_maps, ip);
1040
1041         if (mapp)
1042                 *mapp = map;
1043
1044         if (map) {
1045                 ip = map->map_ip(map, ip);
1046                 return map->dso->find_symbol(map->dso, ip);
1047         }
1048
1049         return NULL;
1050 }
1051
1052 struct 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
1066 static int dso__load_module_sym(struct dso *self, struct map *map,
1067                                 symbol_filter_t filter)
1068 {
1069         int err = 0, fd = open(self->long_name, O_RDONLY);
1070
1071         self->loaded = 1;
1072
1073         if (fd < 0) {
1074                 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1075                 return err;
1076         }
1077
1078         err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1079         close(fd);
1080
1081         return err;
1082 }
1083
1084 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1085 {
1086         struct dirent *dent;
1087         int nr_symbols = 0, err;
1088         DIR *dir = opendir(dirname);
1089
1090         if (!dir) {
1091                 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1092                 return -1;
1093         }
1094
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);
1105                         err = dsos__load_modules_sym_dir(path, filter);
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
1119                         strxfrchar(dso_name, '-', '_');
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
1131                         err = dso__load_module_sym(map->dso, map, filter);
1132                         if (err < 0)
1133                                 goto failure;
1134                         last = rb_last(&map->dso->syms);
1135                         if (last) {
1136                                 struct symbol *sym;
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
1145                                 sym = rb_entry(last, struct symbol, rb_node);
1146                                 map->end = map->start + sym->end;
1147                         }
1148                 }
1149                 nr_symbols += err;
1150         }
1151
1152         return nr_symbols;
1153 failure:
1154         closedir(dir);
1155         return -1;
1156 }
1157
1158 static int dsos__load_modules_sym(symbol_filter_t filter)
1159 {
1160         struct utsname uts;
1161         char modules_path[PATH_MAX];
1162
1163         if (uname(&uts) < 0)
1164                 return -1;
1165
1166         snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1167                  uts.release);
1168
1169         return dsos__load_modules_sym_dir(modules_path, filter);
1170 }
1171
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  */
1177 static struct map *map__new2(u64 start, struct dso *dso)
1178 {
1179         struct map *self = malloc(sizeof(*self));
1180
1181         if (self != NULL) {
1182                 /*
1183                  * ->end will be filled after we load all the symbols
1184                  */
1185                 map__init(self, start, 0, 0, dso);
1186         }
1187
1188         return self;
1189 }
1190
1191 static int dsos__load_modules(void)
1192 {
1193         char *line = NULL;
1194         size_t n;
1195         FILE *file = fopen("/proc/modules", "r");
1196         struct map *map;
1197
1198         if (file == NULL)
1199                 return -1;
1200
1201         while (!feof(file)) {
1202                 char name[PATH_MAX];
1203                 u64 start;
1204                 struct dso *dso;
1205                 char *sep;
1206                 int line_len;
1207
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);
1230                 dso = dso__new(name);
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;
1239                 }
1240
1241                 dso->origin = DSO__ORIG_KMODULE;
1242                 kernel_maps__insert(map);
1243                 dsos__add(dso);
1244         }
1245
1246         free(line);
1247         fclose(file);
1248
1249         return 0;
1250
1251 out_delete_line:
1252         free(line);
1253 out_failure:
1254         return -1;
1255 }
1256
1257 static int dso__load_vmlinux(struct dso *self, struct map *map,
1258                              const char *vmlinux, symbol_filter_t filter)
1259 {
1260         int err, fd = open(vmlinux, O_RDONLY);
1261
1262         self->loaded = 1;
1263
1264         if (fd < 0)
1265                 return -1;
1266
1267         err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1268
1269         close(fd);
1270
1271         return err;
1272 }
1273
1274 int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter,
1275                       int use_modules)
1276 {
1277         int err = -1;
1278         struct dso *dso = dso__new(vmlinux);
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
1288         kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1289
1290         if (use_modules && dsos__load_modules() < 0) {
1291                 pr_warning("Failed to load list of modules in use! "
1292                            "Continuing...\n");
1293                 use_modules = 0;
1294         }
1295
1296         if (vmlinux) {
1297                 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
1298                 if (err > 0 && use_modules) {
1299                         int syms = dsos__load_modules_sym(filter);
1300
1301                         if (syms < 0)
1302                                 pr_warning("Failed to read module symbols!"
1303                                            " Continuing...\n");
1304                         else
1305                                 err += syms;
1306                 }
1307         }
1308
1309         if (err <= 0)
1310                 err = kernel_maps__load_kallsyms(filter, use_modules);
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);
1315
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;
1322                 kernel_maps__insert(kernel_map);
1323                 /*
1324                  * Now that we have all sorted out, just set the ->end of all
1325                  * maps:
1326                  */
1327                 kernel_maps__fixup_end();
1328                 dsos__add(dso);
1329
1330                 if (verbose)
1331                         kernel_maps__fprintf(stderr);
1332         }
1333
1334         return err;
1335
1336 out_delete_dso:
1337         dso__delete(dso);
1338         return -1;
1339 }
1340
1341 LIST_HEAD(dsos);
1342 struct dso      *vdso;
1343
1344 const char      *vmlinux_name = "vmlinux";
1345 int             modules;
1346
1347 static void dsos__add(struct dso *dso)
1348 {
1349         list_add_tail(&dso->node, &dsos);
1350 }
1351
1352 static 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
1362 struct dso *dsos__findnew(const char *name)
1363 {
1364         struct dso *dso = dsos__find(name);
1365
1366         if (!dso) {
1367                 dso = dso__new(name);
1368                 if (dso != NULL)
1369                         dsos__add(dso);
1370         }
1371
1372         return dso;
1373 }
1374
1375 void 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
1383 int load_kernel(symbol_filter_t filter)
1384 {
1385         if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0)
1386                 return -1;
1387
1388         vdso = dso__new("[vdso]");
1389         if (!vdso)
1390                 return -1;
1391
1392         dsos__add(vdso);
1393
1394         return 0;
1395 }
1396
1397 void symbol__init(unsigned int priv_size)
1398 {
1399         elf_version(EV_CURRENT);
1400         symbol__priv_size = priv_size;
1401 }