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