blob: 15d5cf9abfac6d15d0a0cf555adafa8d6883a5ab [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03003#include "string.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03004#include "symbol.h"
5
6#include <libelf.h>
7#include <gelf.h>
8#include <elf.h>
9
10static struct symbol *symbol__new(uint64_t start, uint64_t len,
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030011 const char *name, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030012{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030013 size_t namelen = strlen(name) + 1;
14 struct symbol *self = malloc(priv_size + sizeof(*self) + namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030015
16 if (self != NULL) {
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030017 if (priv_size) {
18 memset(self, 0, priv_size);
19 self = ((void *)self) + priv_size;
20 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030021 self->start = start;
Arnaldo Carvalho de Melo18374ab2009-06-03 14:49:21 -030022 self->end = start + len - 1;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030023 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030024 }
25
26 return self;
27}
28
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030029static void symbol__delete(struct symbol *self, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030030{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030031 free(((void *)self) - priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030032}
33
34static size_t symbol__fprintf(struct symbol *self, FILE *fp)
35{
36 return fprintf(fp, " %llx-%llx %s\n",
37 self->start, self->end, self->name);
38}
39
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030040struct dso *dso__new(const char *name, unsigned int sym_priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030041{
42 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
43
44 if (self != NULL) {
45 strcpy(self->name, name);
46 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030047 self->sym_priv_size = sym_priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030048 }
49
50 return self;
51}
52
53static void dso__delete_symbols(struct dso *self)
54{
55 struct symbol *pos;
56 struct rb_node *next = rb_first(&self->syms);
57
58 while (next) {
59 pos = rb_entry(next, struct symbol, rb_node);
60 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloc8c96522009-06-01 17:50:57 -030061 rb_erase(&pos->rb_node, &self->syms);
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030062 symbol__delete(pos, self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030063 }
64}
65
66void dso__delete(struct dso *self)
67{
68 dso__delete_symbols(self);
69 free(self);
70}
71
72static void dso__insert_symbol(struct dso *self, struct symbol *sym)
73{
74 struct rb_node **p = &self->syms.rb_node;
75 struct rb_node *parent = NULL;
76 const uint64_t ip = sym->start;
77 struct symbol *s;
78
79 while (*p != NULL) {
80 parent = *p;
81 s = rb_entry(parent, struct symbol, rb_node);
82 if (ip < s->start)
83 p = &(*p)->rb_left;
84 else
85 p = &(*p)->rb_right;
86 }
87 rb_link_node(&sym->rb_node, parent, p);
88 rb_insert_color(&sym->rb_node, &self->syms);
89}
90
91struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
92{
93 struct rb_node *n;
94
95 if (self == NULL)
96 return NULL;
97
98 n = self->syms.rb_node;
99
100 while (n) {
101 struct symbol *s = rb_entry(n, struct symbol, rb_node);
102
103 if (ip < s->start)
104 n = n->rb_left;
105 else if (ip > s->end)
106 n = n->rb_right;
107 else
108 return s;
109 }
110
111 return NULL;
112}
113
114size_t dso__fprintf(struct dso *self, FILE *fp)
115{
116 size_t ret = fprintf(fp, "dso: %s\n", self->name);
117
118 struct rb_node *nd;
119 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
120 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
121 ret += symbol__fprintf(pos, fp);
122 }
123
124 return ret;
125}
126
Ingo Molnarbd741372009-06-04 14:13:04 +0200127static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300128{
129 struct rb_node *nd, *prevnd;
130 char *line = NULL;
131 size_t n;
132 FILE *file = fopen("/proc/kallsyms", "r");
133
134 if (file == NULL)
135 goto out_failure;
136
137 while (!feof(file)) {
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300138 __u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300139 struct symbol *sym;
140 int line_len, len;
141 char symbol_type;
142
143 line_len = getline(&line, &n, file);
144 if (line_len < 0)
145 break;
146
147 if (!line)
148 goto out_failure;
149
150 line[--line_len] = '\0'; /* \n */
151
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300152 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300153
154 len++;
155 if (len + 2 >= line_len)
156 continue;
157
158 symbol_type = toupper(line[len]);
159 /*
160 * We're interested only in code ('T'ext)
161 */
162 if (symbol_type != 'T' && symbol_type != 'W')
163 continue;
164 /*
165 * Well fix up the end later, when we have all sorted.
166 */
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300167 sym = symbol__new(start, 0xdead, line + len + 2,
168 self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300169
170 if (sym == NULL)
171 goto out_delete_line;
172
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300173 if (filter && filter(self, sym))
174 symbol__delete(sym, self->sym_priv_size);
175 else
176 dso__insert_symbol(self, sym);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300177 }
178
179 /*
180 * Now that we have all sorted out, just set the ->end of all
181 * symbols
182 */
183 prevnd = rb_first(&self->syms);
184
185 if (prevnd == NULL)
186 goto out_delete_line;
187
188 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
189 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
190 *curr = rb_entry(nd, struct symbol, rb_node);
191
192 prev->end = curr->start - 1;
193 prevnd = nd;
194 }
195
196 free(line);
197 fclose(file);
198
199 return 0;
200
201out_delete_line:
202 free(line);
203out_failure:
204 return -1;
205}
206
207/**
208 * elf_symtab__for_each_symbol - iterate thru all the symbols
209 *
210 * @self: struct elf_symtab instance to iterate
211 * @index: uint32_t index
212 * @sym: GElf_Sym iterator
213 */
214#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
215 for (index = 0, gelf_getsym(syms, index, &sym);\
216 index < nr_syms; \
217 index++, gelf_getsym(syms, index, &sym))
218
219static inline uint8_t elf_sym__type(const GElf_Sym *sym)
220{
221 return GELF_ST_TYPE(sym->st_info);
222}
223
224static inline int elf_sym__is_function(const GElf_Sym *sym)
225{
226 return elf_sym__type(sym) == STT_FUNC &&
227 sym->st_name != 0 &&
228 sym->st_shndx != SHN_UNDEF &&
229 sym->st_size != 0;
230}
231
232static inline const char *elf_sym__name(const GElf_Sym *sym,
233 const Elf_Data *symstrs)
234{
235 return symstrs->d_buf + sym->st_name;
236}
237
238static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
239 GElf_Shdr *shp, const char *name,
240 size_t *index)
241{
242 Elf_Scn *sec = NULL;
243 size_t cnt = 1;
244
245 while ((sec = elf_nextscn(elf, sec)) != NULL) {
246 char *str;
247
248 gelf_getshdr(sec, shp);
249 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
250 if (!strcmp(name, str)) {
251 if (index)
252 *index = cnt;
253 break;
254 }
255 ++cnt;
256 }
257
258 return sec;
259}
260
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300261#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
262 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
263 idx < nr_entries; \
264 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
265
266#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
267 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
268 idx < nr_entries; \
269 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
270
271static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf,
272 GElf_Ehdr *ehdr, Elf_Scn *scn_dynsym,
273 GElf_Shdr *shdr_dynsym,
274 size_t dynsym_idx)
275{
276 uint32_t nr_rel_entries, idx;
277 GElf_Sym sym;
278 __u64 plt_offset;
279 GElf_Shdr shdr_plt;
280 struct symbol *f;
281 GElf_Shdr shdr_rel_plt;
282 Elf_Data *reldata, *syms, *symstrs;
283 Elf_Scn *scn_plt_rel, *scn_symstrs;
284 char sympltname[1024];
285 int nr = 0, symidx;
286
287 scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
288 ".rela.plt", NULL);
289 if (scn_plt_rel == NULL) {
290 scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
291 ".rel.plt", NULL);
292 if (scn_plt_rel == NULL)
293 return 0;
294 }
295
296 if (shdr_rel_plt.sh_link != dynsym_idx)
297 return 0;
298
299 if (elf_section_by_name(elf, ehdr, &shdr_plt, ".plt", NULL) == NULL)
300 return 0;
301
302 /*
303 * Fetch the relocation section to find the indexes to the GOT
304 * and the symbols in the .dynsym they refer to.
305 */
306 reldata = elf_getdata(scn_plt_rel, NULL);
307 if (reldata == NULL)
308 return -1;
309
310 syms = elf_getdata(scn_dynsym, NULL);
311 if (syms == NULL)
312 return -1;
313
314 scn_symstrs = elf_getscn(elf, shdr_dynsym->sh_link);
315 if (scn_symstrs == NULL)
316 return -1;
317
318 symstrs = elf_getdata(scn_symstrs, NULL);
319 if (symstrs == NULL)
320 return -1;
321
322 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
323 plt_offset = shdr_plt.sh_offset;
324
325 if (shdr_rel_plt.sh_type == SHT_RELA) {
326 GElf_Rela pos_mem, *pos;
327
328 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
329 nr_rel_entries) {
330 symidx = GELF_R_SYM(pos->r_info);
331 plt_offset += shdr_plt.sh_entsize;
332 gelf_getsym(syms, symidx, &sym);
333 snprintf(sympltname, sizeof(sympltname),
334 "%s@plt", elf_sym__name(&sym, symstrs));
335
336 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
337 sympltname, self->sym_priv_size);
338 if (!f)
339 return -1;
340
341 dso__insert_symbol(self, f);
342 ++nr;
343 }
344 } else if (shdr_rel_plt.sh_type == SHT_REL) {
345 GElf_Rel pos_mem, *pos;
346 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
347 nr_rel_entries) {
348 symidx = GELF_R_SYM(pos->r_info);
349 plt_offset += shdr_plt.sh_entsize;
350 gelf_getsym(syms, symidx, &sym);
351 snprintf(sympltname, sizeof(sympltname),
352 "%s@plt", elf_sym__name(&sym, symstrs));
353
354 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
355 sympltname, self->sym_priv_size);
356 if (!f)
357 return -1;
358
359 dso__insert_symbol(self, f);
360 ++nr;
361 }
362 } else {
363 /*
364 * TODO: There are still one more shdr_rel_plt.sh_type
365 * I have to investigate, but probably should be ignored.
366 */
367 }
368
369 return nr;
370}
371
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300372static int dso__load_sym(struct dso *self, int fd, const char *name,
Ingo Molnarbd741372009-06-04 14:13:04 +0200373 symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300374{
375 Elf_Data *symstrs;
376 uint32_t nr_syms;
377 int err = -1;
378 uint32_t index;
379 GElf_Ehdr ehdr;
380 GElf_Shdr shdr;
381 Elf_Data *syms;
382 GElf_Sym sym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300383 Elf_Scn *sec, *sec_dynsym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300384 Elf *elf;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300385 size_t dynsym_idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300386 int nr = 0;
387
388 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
389 if (elf == NULL) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200390 if (verbose)
391 fprintf(stderr, "%s: cannot read %s ELF file.\n",
392 __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300393 goto out_close;
394 }
395
396 if (gelf_getehdr(elf, &ehdr) == NULL) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200397 if (verbose)
398 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300399 goto out_elf_end;
400 }
401
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300402 /*
403 * We need to check if we have a .dynsym, so that we can handle the
404 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
405 * .dynsym or .symtab)
406 */
407 sec_dynsym = elf_section_by_name(elf, &ehdr, &shdr,
408 ".dynsym", &dynsym_idx);
409 if (sec_dynsym != NULL) {
410 nr = dso__synthesize_plt_symbols(self, elf, &ehdr,
411 sec_dynsym, &shdr,
412 dynsym_idx);
413 if (nr < 0)
414 goto out_elf_end;
415 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300416
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300417 /*
418 * But if we have a full .symtab (that is a superset of .dynsym) we
419 * should add the symbols not in the .dynsyn
420 */
421 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
422 if (sec == NULL) {
423 if (sec_dynsym == NULL)
424 goto out_elf_end;
425
426 sec = sec_dynsym;
427 gelf_getshdr(sec, &shdr);
428 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300429
430 syms = elf_getdata(sec, NULL);
431 if (syms == NULL)
432 goto out_elf_end;
433
434 sec = elf_getscn(elf, shdr.sh_link);
435 if (sec == NULL)
436 goto out_elf_end;
437
438 symstrs = elf_getdata(sec, NULL);
439 if (symstrs == NULL)
440 goto out_elf_end;
441
442 nr_syms = shdr.sh_size / shdr.sh_entsize;
443
444 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
445 struct symbol *f;
446
447 if (!elf_sym__is_function(&sym))
448 continue;
449
450 sec = elf_getscn(elf, sym.st_shndx);
451 if (!sec)
452 goto out_elf_end;
453
454 gelf_getshdr(sec, &shdr);
455 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
456
457 f = symbol__new(sym.st_value, sym.st_size,
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300458 elf_sym__name(&sym, symstrs),
459 self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300460 if (!f)
461 goto out_elf_end;
462
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300463 if (filter && filter(self, f))
464 symbol__delete(f, self->sym_priv_size);
465 else {
466 dso__insert_symbol(self, f);
467 nr++;
468 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300469 }
470
471 err = nr;
472out_elf_end:
473 elf_end(elf);
474out_close:
475 return err;
476}
477
Ingo Molnarbd741372009-06-04 14:13:04 +0200478int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300479{
480 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
481 char *name = malloc(size);
482 int variant = 0;
483 int ret = -1;
484 int fd;
485
486 if (!name)
487 return -1;
488
489more:
490 do {
491 switch (variant) {
492 case 0: /* Fedora */
493 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
494 break;
495 case 1: /* Ubuntu */
496 snprintf(name, size, "/usr/lib/debug%s", self->name);
497 break;
498 case 2: /* Sane people */
499 snprintf(name, size, "%s", self->name);
500 break;
501
502 default:
503 goto out;
504 }
505 variant++;
506
507 fd = open(name, O_RDONLY);
508 } while (fd < 0);
509
Ingo Molnarbd741372009-06-04 14:13:04 +0200510 ret = dso__load_sym(self, fd, name, filter, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300511 close(fd);
512
513 /*
514 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
515 */
516 if (!ret)
517 goto more;
518
519out:
520 free(name);
521 return ret;
522}
523
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300524static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
Ingo Molnarbd741372009-06-04 14:13:04 +0200525 symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300526{
527 int err, fd = open(vmlinux, O_RDONLY);
528
529 if (fd < 0)
530 return -1;
531
Ingo Molnarbd741372009-06-04 14:13:04 +0200532 err = dso__load_sym(self, fd, vmlinux, filter, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300533 close(fd);
534
535 return err;
536}
537
Ingo Molnarbd741372009-06-04 14:13:04 +0200538int dso__load_kernel(struct dso *self, const char *vmlinux,
539 symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300540{
541 int err = -1;
542
543 if (vmlinux)
Ingo Molnarbd741372009-06-04 14:13:04 +0200544 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300545
546 if (err)
Ingo Molnarbd741372009-06-04 14:13:04 +0200547 err = dso__load_kallsyms(self, filter, verbose);
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300548
549 return err;
550}
551
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300552void symbol__init(void)
553{
554 elf_version(EV_CURRENT);
555}