Merge branch 'perf/urgent' into perf/core
[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 }
401
402 static inline int elf_sym__is_label(const GElf_Sym *sym)
403 {
404         return elf_sym__type(sym) == STT_NOTYPE &&
405                 sym->st_name != 0 &&
406                 sym->st_shndx != SHN_UNDEF &&
407                 sym->st_shndx != SHN_ABS;
408 }
409
410 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
411                                         const Elf_Data *secstrs)
412 {
413         return secstrs->d_buf + shdr->sh_name;
414 }
415
416 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
417                                         const Elf_Data *secstrs)
418 {
419         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
420 }
421
422 static inline const char *elf_sym__name(const GElf_Sym *sym,
423                                         const Elf_Data *symstrs)
424 {
425         return symstrs->d_buf + sym->st_name;
426 }
427
428 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
429                                     GElf_Shdr *shp, const char *name,
430                                     size_t *idx)
431 {
432         Elf_Scn *sec = NULL;
433         size_t cnt = 1;
434
435         while ((sec = elf_nextscn(elf, sec)) != NULL) {
436                 char *str;
437
438                 gelf_getshdr(sec, shp);
439                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
440                 if (!strcmp(name, str)) {
441                         if (idx)
442                                 *idx = cnt;
443                         break;
444                 }
445                 ++cnt;
446         }
447
448         return sec;
449 }
450
451 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
452         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
453              idx < nr_entries; \
454              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
455
456 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
457         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
458              idx < nr_entries; \
459              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
460
461 /*
462  * We need to check if we have a .dynsym, so that we can handle the
463  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
464  * .dynsym or .symtab).
465  * And always look at the original dso, not at debuginfo packages, that
466  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
467  */
468 static int dso__synthesize_plt_symbols(struct  dso *self, int v)
469 {
470         uint32_t nr_rel_entries, idx;
471         GElf_Sym sym;
472         u64 plt_offset;
473         GElf_Shdr shdr_plt;
474         struct symbol *f;
475         GElf_Shdr shdr_rel_plt, shdr_dynsym;
476         Elf_Data *reldata, *syms, *symstrs;
477         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
478         size_t dynsym_idx;
479         GElf_Ehdr ehdr;
480         char sympltname[1024];
481         Elf *elf;
482         int nr = 0, symidx, fd, err = 0;
483
484         fd = open(self->long_name, O_RDONLY);
485         if (fd < 0)
486                 goto out;
487
488         elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
489         if (elf == NULL)
490                 goto out_close;
491
492         if (gelf_getehdr(elf, &ehdr) == NULL)
493                 goto out_elf_end;
494
495         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
496                                          ".dynsym", &dynsym_idx);
497         if (scn_dynsym == NULL)
498                 goto out_elf_end;
499
500         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
501                                           ".rela.plt", NULL);
502         if (scn_plt_rel == NULL) {
503                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
504                                                   ".rel.plt", NULL);
505                 if (scn_plt_rel == NULL)
506                         goto out_elf_end;
507         }
508
509         err = -1;
510
511         if (shdr_rel_plt.sh_link != dynsym_idx)
512                 goto out_elf_end;
513
514         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
515                 goto out_elf_end;
516
517         /*
518          * Fetch the relocation section to find the idxes to the GOT
519          * and the symbols in the .dynsym they refer to.
520          */
521         reldata = elf_getdata(scn_plt_rel, NULL);
522         if (reldata == NULL)
523                 goto out_elf_end;
524
525         syms = elf_getdata(scn_dynsym, NULL);
526         if (syms == NULL)
527                 goto out_elf_end;
528
529         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
530         if (scn_symstrs == NULL)
531                 goto out_elf_end;
532
533         symstrs = elf_getdata(scn_symstrs, NULL);
534         if (symstrs == NULL)
535                 goto out_elf_end;
536
537         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
538         plt_offset = shdr_plt.sh_offset;
539
540         if (shdr_rel_plt.sh_type == SHT_RELA) {
541                 GElf_Rela pos_mem, *pos;
542
543                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
544                                            nr_rel_entries) {
545                         symidx = GELF_R_SYM(pos->r_info);
546                         plt_offset += shdr_plt.sh_entsize;
547                         gelf_getsym(syms, symidx, &sym);
548                         snprintf(sympltname, sizeof(sympltname),
549                                  "%s@plt", elf_sym__name(&sym, symstrs));
550
551                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
552                                         sympltname, self->sym_priv_size, v);
553                         if (!f)
554                                 goto out_elf_end;
555
556                         dso__insert_symbol(self, f);
557                         ++nr;
558                 }
559         } else if (shdr_rel_plt.sh_type == SHT_REL) {
560                 GElf_Rel pos_mem, *pos;
561                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
562                                           nr_rel_entries) {
563                         symidx = GELF_R_SYM(pos->r_info);
564                         plt_offset += shdr_plt.sh_entsize;
565                         gelf_getsym(syms, symidx, &sym);
566                         snprintf(sympltname, sizeof(sympltname),
567                                  "%s@plt", elf_sym__name(&sym, symstrs));
568
569                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
570                                         sympltname, self->sym_priv_size, v);
571                         if (!f)
572                                 goto out_elf_end;
573
574                         dso__insert_symbol(self, f);
575                         ++nr;
576                 }
577         }
578
579         err = 0;
580 out_elf_end:
581         elf_end(elf);
582 out_close:
583         close(fd);
584
585         if (err == 0)
586                 return nr;
587 out:
588         fprintf(stderr, "%s: problems reading %s PLT info.\n",
589                 __func__, self->long_name);
590         return 0;
591 }
592
593 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
594                          int fd, symbol_filter_t filter, int kernel,
595                          int kmodule, int v)
596 {
597         Elf_Data *symstrs, *secstrs;
598         uint32_t nr_syms;
599         int err = -1;
600         uint32_t idx;
601         GElf_Ehdr ehdr;
602         GElf_Shdr shdr;
603         Elf_Data *syms;
604         GElf_Sym sym;
605         Elf_Scn *sec, *sec_strndx;
606         Elf *elf;
607         int nr = 0;
608
609         elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
610         if (elf == NULL) {
611                 if (v)
612                         fprintf(stderr, "%s: cannot read %s ELF file.\n",
613                                 __func__, name);
614                 goto out_close;
615         }
616
617         if (gelf_getehdr(elf, &ehdr) == NULL) {
618                 if (v)
619                         fprintf(stderr, "%s: cannot get elf header.\n", __func__);
620                 goto out_elf_end;
621         }
622
623         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
624         if (sec == NULL) {
625                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
626                 if (sec == NULL)
627                         goto out_elf_end;
628         }
629
630         syms = elf_getdata(sec, NULL);
631         if (syms == NULL)
632                 goto out_elf_end;
633
634         sec = elf_getscn(elf, shdr.sh_link);
635         if (sec == NULL)
636                 goto out_elf_end;
637
638         symstrs = elf_getdata(sec, NULL);
639         if (symstrs == NULL)
640                 goto out_elf_end;
641
642         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
643         if (sec_strndx == NULL)
644                 goto out_elf_end;
645
646         secstrs = elf_getdata(sec_strndx, NULL);
647         if (secstrs == NULL)
648                 goto out_elf_end;
649
650         nr_syms = shdr.sh_size / shdr.sh_entsize;
651
652         memset(&sym, 0, sizeof(sym));
653         if (!kernel) {
654                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
655                                 elf_section_by_name(elf, &ehdr, &shdr,
656                                                      ".gnu.prelink_undo",
657                                                      NULL) != NULL);
658         } else self->adjust_symbols = 0;
659
660         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
661                 struct symbol *f;
662                 const char *elf_name;
663                 char *demangled;
664                 int is_label = elf_sym__is_label(&sym);
665                 const char *section_name;
666                 u64 sh_offset = 0;
667
668                 if (!is_label && !elf_sym__is_function(&sym))
669                         continue;
670
671                 sec = elf_getscn(elf, sym.st_shndx);
672                 if (!sec)
673                         goto out_elf_end;
674
675                 gelf_getshdr(sec, &shdr);
676
677                 if (is_label && !elf_sec__is_text(&shdr, secstrs))
678                         continue;
679
680                 section_name = elf_sec__name(&shdr, secstrs);
681
682                 if ((kernel || kmodule)) {
683                         if (strstr(section_name, ".init"))
684                                 sh_offset = shdr.sh_offset;
685                 }
686
687                 if (self->adjust_symbols) {
688                         if (v >= 2)
689                                 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
690                                         (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
691
692                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
693                 }
694                 /*
695                  * We need to figure out if the object was created from C++ sources
696                  * DWARF DW_compile_unit has this, but we don't always have access
697                  * to it...
698                  */
699                 elf_name = elf_sym__name(&sym, symstrs);
700                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
701                 if (demangled != NULL)
702                         elf_name = demangled;
703
704                 f = symbol__new(sym.st_value + sh_offset, sym.st_size, elf_name,
705                                 self->sym_priv_size, v);
706                 free(demangled);
707                 if (!f)
708                         goto out_elf_end;
709
710                 if (filter && filter(map, f))
711                         symbol__delete(f, self->sym_priv_size);
712                 else {
713                         dso__insert_symbol(self, f);
714                         nr++;
715                 }
716         }
717
718         err = nr;
719 out_elf_end:
720         elf_end(elf);
721 out_close:
722         return err;
723 }
724
725 #define BUILD_ID_SIZE 128
726
727 static char *dso__read_build_id(struct dso *self, int v)
728 {
729         int i;
730         GElf_Ehdr ehdr;
731         GElf_Shdr shdr;
732         Elf_Data *build_id_data;
733         Elf_Scn *sec;
734         char *build_id = NULL, *bid;
735         unsigned char *raw;
736         Elf *elf;
737         int fd = open(self->long_name, O_RDONLY);
738
739         if (fd < 0)
740                 goto out;
741
742         elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
743         if (elf == NULL) {
744                 if (v)
745                         fprintf(stderr, "%s: cannot read %s ELF file.\n",
746                                 __func__, self->long_name);
747                 goto out_close;
748         }
749
750         if (gelf_getehdr(elf, &ehdr) == NULL) {
751                 if (v)
752                         fprintf(stderr, "%s: cannot get elf header.\n", __func__);
753                 goto out_elf_end;
754         }
755
756         sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
757         if (sec == NULL)
758                 goto out_elf_end;
759
760         build_id_data = elf_getdata(sec, NULL);
761         if (build_id_data == NULL)
762                 goto out_elf_end;
763         build_id = malloc(BUILD_ID_SIZE);
764         if (build_id == NULL)
765                 goto out_elf_end;
766         raw = build_id_data->d_buf + 16;
767         bid = build_id;
768
769         for (i = 0; i < 20; ++i) {
770                 sprintf(bid, "%02x", *raw);
771                 ++raw;
772                 bid += 2;
773         }
774         if (v >= 2)
775                 printf("%s(%s): %s\n", __func__, self->long_name, build_id);
776 out_elf_end:
777         elf_end(elf);
778 out_close:
779         close(fd);
780 out:
781         return build_id;
782 }
783
784 char dso__symtab_origin(const struct dso *self)
785 {
786         static const char origin[] = {
787                 [DSO__ORIG_KERNEL] =   'k',
788                 [DSO__ORIG_JAVA_JIT] = 'j',
789                 [DSO__ORIG_FEDORA] =   'f',
790                 [DSO__ORIG_UBUNTU] =   'u',
791                 [DSO__ORIG_BUILDID] =  'b',
792                 [DSO__ORIG_DSO] =      'd',
793                 [DSO__ORIG_KMODULE] =  'K',
794         };
795
796         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
797                 return '!';
798         return origin[self->origin];
799 }
800
801 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter, int v)
802 {
803         int size = PATH_MAX;
804         char *name = malloc(size), *build_id = NULL;
805         int ret = -1;
806         int fd;
807
808         if (!name)
809                 return -1;
810
811         self->adjust_symbols = 0;
812
813         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
814                 ret = dso__load_perf_map(self, map, filter, v);
815                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
816                                          DSO__ORIG_NOT_FOUND;
817                 return ret;
818         }
819
820         self->origin = DSO__ORIG_FEDORA - 1;
821
822 more:
823         do {
824                 self->origin++;
825                 switch (self->origin) {
826                 case DSO__ORIG_FEDORA:
827                         snprintf(name, size, "/usr/lib/debug%s.debug",
828                                  self->long_name);
829                         break;
830                 case DSO__ORIG_UBUNTU:
831                         snprintf(name, size, "/usr/lib/debug%s",
832                                  self->long_name);
833                         break;
834                 case DSO__ORIG_BUILDID:
835                         build_id = dso__read_build_id(self, v);
836                         if (build_id != NULL) {
837                                 snprintf(name, size,
838                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
839                                         build_id, build_id + 2);
840                                 free(build_id);
841                                 break;
842                         }
843                         self->origin++;
844                         /* Fall thru */
845                 case DSO__ORIG_DSO:
846                         snprintf(name, size, "%s", self->long_name);
847                         break;
848
849                 default:
850                         goto out;
851                 }
852
853                 fd = open(name, O_RDONLY);
854         } while (fd < 0);
855
856         ret = dso__load_sym(self, map, name, fd, filter, 0, 0, v);
857         close(fd);
858
859         /*
860          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
861          */
862         if (!ret)
863                 goto more;
864
865         if (ret > 0) {
866                 int nr_plt = dso__synthesize_plt_symbols(self, v);
867                 if (nr_plt > 0)
868                         ret += nr_plt;
869         }
870 out:
871         free(name);
872         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
873                 return 0;
874         return ret;
875 }
876
877 struct map *kernel_map;
878
879 static void kernel_maps__insert(struct map *map)
880 {
881         maps__insert(&kernel_maps, map);
882 }
883
884 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
885 {
886         /*
887          * We can't have kernel_map in kernel_maps because it spans an address
888          * space that includes the modules. The right way to fix this is to
889          * create several maps, so that we don't have overlapping ranges with
890          * modules. For now lets look first on the kernel dso.
891          */
892         struct map *map = maps__find(&kernel_maps, ip);
893         struct symbol *sym;
894
895         if (map) {
896                 ip = map->map_ip(map, ip);
897                 sym = map->dso->find_symbol(map->dso, ip);
898         } else {
899                 map = kernel_map;
900                 sym = map->dso->find_symbol(map->dso, ip);
901         }
902
903         if (mapp)
904                 *mapp = map;
905
906         return sym;
907 }
908
909 struct map *kernel_maps__find_by_dso_name(const char *name)
910 {
911         struct rb_node *nd;
912
913         for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
914                 struct map *map = rb_entry(nd, struct map, rb_node);
915
916                 if (map->dso && strcmp(map->dso->name, name) == 0)
917                         return map;
918         }
919
920         return NULL;
921 }
922
923 static int dso__load_module_sym(struct dso *self, struct map *map,
924                                 symbol_filter_t filter, int v)
925 {
926         int err = 0, fd = open(self->long_name, O_RDONLY);
927
928         if (fd < 0) {
929                 if (v)
930                         fprintf(stderr, "%s: cannot open %s\n",
931                                 __func__, self->long_name);
932                 return err;
933         }
934
935         err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1, v);
936         close(fd);
937
938         return err;
939 }
940
941 static int dsos__load_modules_sym_dir(char *dirname,
942                                       symbol_filter_t filter, int v)
943 {
944         struct dirent *dent;
945         int nr_symbols = 0, err;
946         DIR *dir = opendir(dirname);
947
948         if (!dir) {
949                 if (v)
950                         fprintf(stderr, "%s: cannot open %s dir\n", __func__,
951                                 dirname);
952                 return -1;
953         }
954
955         while ((dent = readdir(dir)) != NULL) {
956                 char path[PATH_MAX];
957
958                 if (dent->d_type == DT_DIR) {
959                         if (!strcmp(dent->d_name, ".") ||
960                             !strcmp(dent->d_name, ".."))
961                                 continue;
962
963                         snprintf(path, sizeof(path), "%s/%s",
964                                  dirname, dent->d_name);
965                         err = dsos__load_modules_sym_dir(path, filter, v);
966                         if (err < 0)
967                                 goto failure;
968                 } else {
969                         char *dot = strrchr(dent->d_name, '.'),
970                              dso_name[PATH_MAX];
971                         struct map *map;
972                         struct rb_node *last;
973
974                         if (dot == NULL || strcmp(dot, ".ko"))
975                                 continue;
976                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
977                                  (int)(dot - dent->d_name), dent->d_name);
978
979                         strxfrchar(dso_name, '-', '_');
980                         map = kernel_maps__find_by_dso_name(dso_name);
981                         if (map == NULL)
982                                 continue;
983
984                         snprintf(path, sizeof(path), "%s/%s",
985                                  dirname, dent->d_name);
986
987                         map->dso->long_name = strdup(path);
988                         if (map->dso->long_name == NULL)
989                                 goto failure;
990
991                         err = dso__load_module_sym(map->dso, map, filter, v);
992                         if (err < 0)
993                                 goto failure;
994                         last = rb_last(&map->dso->syms);
995                         if (last) {
996                                 struct symbol *sym;
997                                 sym = rb_entry(last, struct symbol, rb_node);
998                                 map->end = map->start + sym->end;
999                         }
1000                 }
1001                 nr_symbols += err;
1002         }
1003
1004         return nr_symbols;
1005 failure:
1006         closedir(dir);
1007         return -1;
1008 }
1009
1010 static int dsos__load_modules_sym(symbol_filter_t filter, int v)
1011 {
1012         struct utsname uts;
1013         char modules_path[PATH_MAX];
1014
1015         if (uname(&uts) < 0)
1016                 return -1;
1017
1018         snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1019                  uts.release);
1020
1021         return dsos__load_modules_sym_dir(modules_path, filter, v);
1022 }
1023
1024 /*
1025  * Constructor variant for modules (where we know from /proc/modules where
1026  * they are loaded) and for vmlinux, where only after we load all the
1027  * symbols we'll know where it starts and ends.
1028  */
1029 static struct map *map__new2(u64 start, struct dso *dso)
1030 {
1031         struct map *self = malloc(sizeof(*self));
1032
1033         if (self != NULL) {
1034                 self->start = start;
1035                 /*
1036                  * Will be filled after we load all the symbols
1037                  */
1038                 self->end = 0;
1039
1040                 self->pgoff = 0;
1041                 self->dso = dso;
1042                 self->map_ip = map__map_ip;
1043                 RB_CLEAR_NODE(&self->rb_node);
1044         }
1045         return self;
1046 }
1047
1048 static int dsos__load_modules(unsigned int sym_priv_size)
1049 {
1050         char *line = NULL;
1051         size_t n;
1052         FILE *file = fopen("/proc/modules", "r");
1053         struct map *map;
1054
1055         if (file == NULL)
1056                 return -1;
1057
1058         while (!feof(file)) {
1059                 char name[PATH_MAX];
1060                 u64 start;
1061                 struct dso *dso;
1062                 char *sep;
1063                 int line_len;
1064
1065                 line_len = getline(&line, &n, file);
1066                 if (line_len < 0)
1067                         break;
1068
1069                 if (!line)
1070                         goto out_failure;
1071
1072                 line[--line_len] = '\0'; /* \n */
1073
1074                 sep = strrchr(line, 'x');
1075                 if (sep == NULL)
1076                         continue;
1077
1078                 hex2u64(sep + 1, &start);
1079
1080                 sep = strchr(line, ' ');
1081                 if (sep == NULL)
1082                         continue;
1083
1084                 *sep = '\0';
1085
1086                 snprintf(name, sizeof(name), "[%s]", line);
1087                 dso = dso__new(name, sym_priv_size);
1088
1089                 if (dso == NULL)
1090                         goto out_delete_line;
1091
1092                 map = map__new2(start, dso);
1093                 if (map == NULL) {
1094                         dso__delete(dso);
1095                         goto out_delete_line;
1096                 }
1097
1098                 dso->origin = DSO__ORIG_KMODULE;
1099                 kernel_maps__insert(map);
1100                 dsos__add(dso);
1101         }
1102
1103         free(line);
1104         fclose(file);
1105
1106         return 0;
1107
1108 out_delete_line:
1109         free(line);
1110 out_failure:
1111         return -1;
1112 }
1113
1114 static int dso__load_vmlinux(struct dso *self, struct map *map,
1115                              const char *vmlinux,
1116                              symbol_filter_t filter, int v)
1117 {
1118         int err, fd = open(vmlinux, O_RDONLY);
1119
1120         if (fd < 0)
1121                 return -1;
1122
1123         err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0, v);
1124
1125         close(fd);
1126
1127         return err;
1128 }
1129
1130 int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
1131                       symbol_filter_t filter, int v, int use_modules)
1132 {
1133         int err = -1;
1134         struct dso *dso = dso__new(vmlinux, sym_priv_size);
1135
1136         if (dso == NULL)
1137                 return -1;
1138
1139         dso->short_name = "[kernel]";
1140         kernel_map = map__new2(0, dso);
1141         if (kernel_map == NULL)
1142                 goto out_delete_dso;
1143
1144         kernel_map->map_ip = vdso__map_ip;
1145
1146         if (use_modules && dsos__load_modules(sym_priv_size) < 0) {
1147                 fprintf(stderr, "Failed to load list of modules in use! "
1148                                 "Continuing...\n");
1149                 use_modules = 0;
1150         }
1151
1152         if (vmlinux) {
1153                 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter, v);
1154                 if (err > 0 && use_modules) {
1155                         int syms = dsos__load_modules_sym(filter, v);
1156
1157                         if (syms < 0)
1158                                 fprintf(stderr, "Failed to read module symbols!"
1159                                         " Continuing...\n");
1160                         else
1161                                 err += syms;
1162                 }
1163         }
1164
1165         if (err <= 0)
1166                 err = maps__load_kallsyms(filter, use_modules, v);
1167
1168         if (err > 0) {
1169                 struct rb_node *node = rb_first(&dso->syms);
1170                 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1171                 /*
1172                  * Now that we have all sorted out, just set the ->end of all
1173                  * symbols that still don't have it.
1174                  */
1175                 dso__set_symbols_end(dso);
1176                 kernel_maps__fixup_sym_end();
1177
1178                 kernel_map->start = sym->start;
1179                 node = rb_last(&dso->syms);
1180                 sym = rb_entry(node, struct symbol, rb_node);
1181                 kernel_map->end = sym->end;
1182
1183                 dso->origin = DSO__ORIG_KERNEL;
1184                 /*
1185                  * XXX See kernel_maps__find_symbol comment
1186                  * kernel_maps__insert(kernel_map)
1187                  */
1188                 dsos__add(dso);
1189
1190                 if (v > 0)
1191                         kernel_maps__fprintf(stderr);
1192         }
1193
1194         return err;
1195
1196 out_delete_dso:
1197         dso__delete(dso);
1198         return -1;
1199 }
1200
1201 LIST_HEAD(dsos);
1202 struct dso      *vdso;
1203
1204 const char      *vmlinux_name = "vmlinux";
1205 int             modules;
1206
1207 static void dsos__add(struct dso *dso)
1208 {
1209         list_add_tail(&dso->node, &dsos);
1210 }
1211
1212 static struct dso *dsos__find(const char *name)
1213 {
1214         struct dso *pos;
1215
1216         list_for_each_entry(pos, &dsos, node)
1217                 if (strcmp(pos->name, name) == 0)
1218                         return pos;
1219         return NULL;
1220 }
1221
1222 struct dso *dsos__findnew(const char *name)
1223 {
1224         struct dso *dso = dsos__find(name);
1225         int nr;
1226
1227         if (dso)
1228                 return dso;
1229
1230         dso = dso__new(name, 0);
1231         if (!dso)
1232                 goto out_delete_dso;
1233
1234         nr = dso__load(dso, NULL, NULL, verbose);
1235         if (nr < 0) {
1236                 eprintf("Failed to open: %s\n", name);
1237                 goto out_delete_dso;
1238         }
1239         if (!nr)
1240                 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
1241
1242         dsos__add(dso);
1243
1244         return dso;
1245
1246 out_delete_dso:
1247         dso__delete(dso);
1248         return NULL;
1249 }
1250
1251 void dsos__fprintf(FILE *fp)
1252 {
1253         struct dso *pos;
1254
1255         list_for_each_entry(pos, &dsos, node)
1256                 dso__fprintf(pos, fp);
1257 }
1258
1259 int load_kernel(void)
1260 {
1261         if (dsos__load_kernel(vmlinux_name, 0, NULL, verbose, modules) <= 0)
1262                 return -1;
1263
1264         vdso = dso__new("[vdso]", 0);
1265         if (!vdso)
1266                 return -1;
1267
1268         dsos__add(vdso);
1269
1270         return 0;
1271 }
1272
1273 void symbol__init(void)
1274 {
1275         elf_version(EV_CURRENT);
1276 }