blob: 5c0f42e6b33b90ee8960ccb91845ae74da6b725e [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>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +02009
Ingo Molnar0b73da32009-06-06 15:48:52 +020010const char *sym_hist_filter;
11
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030012enum dso_origin {
13 DSO__ORIG_KERNEL = 0,
14 DSO__ORIG_JAVA_JIT,
15 DSO__ORIG_FEDORA,
16 DSO__ORIG_UBUNTU,
17 DSO__ORIG_BUILDID,
18 DSO__ORIG_DSO,
19 DSO__ORIG_NOT_FOUND,
20};
21
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100022static struct symbol *symbol__new(u64 start, u64 len,
Ingo Molnar0b73da32009-06-06 15:48:52 +020023 const char *name, unsigned int priv_size,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100024 u64 obj_start, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030025{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030026 size_t namelen = strlen(name) + 1;
Ingo Molnar0b73da32009-06-06 15:48:52 +020027 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030028
Ingo Molnar0b73da32009-06-06 15:48:52 +020029 if (!self)
30 return NULL;
31
32 if (verbose >= 2)
33 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100034 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
Ingo Molnar0b73da32009-06-06 15:48:52 +020035
36 self->obj_start= obj_start;
37 self->hist = NULL;
38 self->hist_sum = 0;
39
40 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100041 self->hist = calloc(sizeof(u64), len);
Ingo Molnar0b73da32009-06-06 15:48:52 +020042
43 if (priv_size) {
44 memset(self, 0, priv_size);
45 self = ((void *)self) + priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030046 }
Ingo Molnar0b73da32009-06-06 15:48:52 +020047 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +020048 self->end = len ? start + len - 1 : start;
Ingo Molnar0b73da32009-06-06 15:48:52 +020049 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030050
51 return self;
52}
53
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030054static void symbol__delete(struct symbol *self, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030055{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030056 free(((void *)self) - priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030057}
58
59static size_t symbol__fprintf(struct symbol *self, FILE *fp)
60{
Mike Galbraith6cfcc532009-07-02 08:08:36 +020061 if (!self->module)
62 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030063 self->start, self->end, self->name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +020064 else
65 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
66 self->start, self->end, self->name, self->module->name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030067}
68
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030069struct dso *dso__new(const char *name, unsigned int sym_priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030070{
71 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
72
73 if (self != NULL) {
74 strcpy(self->name, name);
75 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030076 self->sym_priv_size = sym_priv_size;
Peter Zijlstrafc54db52009-06-05 14:04:59 +020077 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030078 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030079 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030080 }
81
82 return self;
83}
84
85static void dso__delete_symbols(struct dso *self)
86{
87 struct symbol *pos;
88 struct rb_node *next = rb_first(&self->syms);
89
90 while (next) {
91 pos = rb_entry(next, struct symbol, rb_node);
92 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloc8c96522009-06-01 17:50:57 -030093 rb_erase(&pos->rb_node, &self->syms);
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030094 symbol__delete(pos, self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030095 }
96}
97
98void dso__delete(struct dso *self)
99{
100 dso__delete_symbols(self);
101 free(self);
102}
103
104static void dso__insert_symbol(struct dso *self, struct symbol *sym)
105{
106 struct rb_node **p = &self->syms.rb_node;
107 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000108 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300109 struct symbol *s;
110
111 while (*p != NULL) {
112 parent = *p;
113 s = rb_entry(parent, struct symbol, rb_node);
114 if (ip < s->start)
115 p = &(*p)->rb_left;
116 else
117 p = &(*p)->rb_right;
118 }
119 rb_link_node(&sym->rb_node, parent, p);
120 rb_insert_color(&sym->rb_node, &self->syms);
121}
122
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000123struct symbol *dso__find_symbol(struct dso *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300124{
125 struct rb_node *n;
126
127 if (self == NULL)
128 return NULL;
129
130 n = self->syms.rb_node;
131
132 while (n) {
133 struct symbol *s = rb_entry(n, struct symbol, rb_node);
134
135 if (ip < s->start)
136 n = n->rb_left;
137 else if (ip > s->end)
138 n = n->rb_right;
139 else
140 return s;
141 }
142
143 return NULL;
144}
145
146size_t dso__fprintf(struct dso *self, FILE *fp)
147{
148 size_t ret = fprintf(fp, "dso: %s\n", self->name);
149
150 struct rb_node *nd;
151 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
152 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
153 ret += symbol__fprintf(pos, fp);
154 }
155
156 return ret;
157}
158
Ingo Molnarbd741372009-06-04 14:13:04 +0200159static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300160{
161 struct rb_node *nd, *prevnd;
162 char *line = NULL;
163 size_t n;
164 FILE *file = fopen("/proc/kallsyms", "r");
Mike Galbraith9974f492009-07-02 08:05:58 +0200165 int count = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300166
167 if (file == NULL)
168 goto out_failure;
169
170 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000171 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300172 struct symbol *sym;
173 int line_len, len;
174 char symbol_type;
175
176 line_len = getline(&line, &n, file);
177 if (line_len < 0)
178 break;
179
180 if (!line)
181 goto out_failure;
182
183 line[--line_len] = '\0'; /* \n */
184
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300185 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300186
187 len++;
188 if (len + 2 >= line_len)
189 continue;
190
191 symbol_type = toupper(line[len]);
192 /*
193 * We're interested only in code ('T'ext)
194 */
195 if (symbol_type != 'T' && symbol_type != 'W')
196 continue;
197 /*
198 * Well fix up the end later, when we have all sorted.
199 */
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300200 sym = symbol__new(start, 0xdead, line + len + 2,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200201 self->sym_priv_size, 0, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300202
203 if (sym == NULL)
204 goto out_delete_line;
205
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300206 if (filter && filter(self, sym))
207 symbol__delete(sym, self->sym_priv_size);
Mike Galbraith9974f492009-07-02 08:05:58 +0200208 else {
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300209 dso__insert_symbol(self, sym);
Mike Galbraith9974f492009-07-02 08:05:58 +0200210 count++;
211 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300212 }
213
214 /*
215 * Now that we have all sorted out, just set the ->end of all
216 * symbols
217 */
218 prevnd = rb_first(&self->syms);
219
220 if (prevnd == NULL)
221 goto out_delete_line;
222
223 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
224 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
225 *curr = rb_entry(nd, struct symbol, rb_node);
226
227 prev->end = curr->start - 1;
228 prevnd = nd;
229 }
230
231 free(line);
232 fclose(file);
233
Mike Galbraith9974f492009-07-02 08:05:58 +0200234 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300235
236out_delete_line:
237 free(line);
238out_failure:
239 return -1;
240}
241
Pekka Enberg80d496b2009-06-08 21:12:48 +0300242static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
243{
244 char *line = NULL;
245 size_t n;
246 FILE *file;
247 int nr_syms = 0;
248
249 file = fopen(self->name, "r");
250 if (file == NULL)
251 goto out_failure;
252
253 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000254 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300255 struct symbol *sym;
256 int line_len, len;
257
258 line_len = getline(&line, &n, file);
259 if (line_len < 0)
260 break;
261
262 if (!line)
263 goto out_failure;
264
265 line[--line_len] = '\0'; /* \n */
266
267 len = hex2u64(line, &start);
268
269 len++;
270 if (len + 2 >= line_len)
271 continue;
272
273 len += hex2u64(line + len, &size);
274
275 len++;
276 if (len + 2 >= line_len)
277 continue;
278
279 sym = symbol__new(start, size, line + len,
280 self->sym_priv_size, start, verbose);
281
282 if (sym == NULL)
283 goto out_delete_line;
284
285 if (filter && filter(self, sym))
286 symbol__delete(sym, self->sym_priv_size);
287 else {
288 dso__insert_symbol(self, sym);
289 nr_syms++;
290 }
291 }
292
293 free(line);
294 fclose(file);
295
296 return nr_syms;
297
298out_delete_line:
299 free(line);
300out_failure:
301 return -1;
302}
303
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300304/**
305 * elf_symtab__for_each_symbol - iterate thru all the symbols
306 *
307 * @self: struct elf_symtab instance to iterate
308 * @index: uint32_t index
309 * @sym: GElf_Sym iterator
310 */
311#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
312 for (index = 0, gelf_getsym(syms, index, &sym);\
313 index < nr_syms; \
314 index++, gelf_getsym(syms, index, &sym))
315
316static inline uint8_t elf_sym__type(const GElf_Sym *sym)
317{
318 return GELF_ST_TYPE(sym->st_info);
319}
320
321static inline int elf_sym__is_function(const GElf_Sym *sym)
322{
323 return elf_sym__type(sym) == STT_FUNC &&
324 sym->st_name != 0 &&
325 sym->st_shndx != SHN_UNDEF &&
326 sym->st_size != 0;
327}
328
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200329static inline int elf_sym__is_label(const GElf_Sym *sym)
330{
331 return elf_sym__type(sym) == STT_NOTYPE &&
332 sym->st_name != 0 &&
333 sym->st_shndx != SHN_UNDEF &&
334 sym->st_shndx != SHN_ABS;
335}
336
337static inline const char *elf_sec__name(const GElf_Shdr *shdr,
338 const Elf_Data *secstrs)
339{
340 return secstrs->d_buf + shdr->sh_name;
341}
342
343static inline int elf_sec__is_text(const GElf_Shdr *shdr,
344 const Elf_Data *secstrs)
345{
346 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
347}
348
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300349static inline const char *elf_sym__name(const GElf_Sym *sym,
350 const Elf_Data *symstrs)
351{
352 return symstrs->d_buf + sym->st_name;
353}
354
355static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
356 GElf_Shdr *shp, const char *name,
357 size_t *index)
358{
359 Elf_Scn *sec = NULL;
360 size_t cnt = 1;
361
362 while ((sec = elf_nextscn(elf, sec)) != NULL) {
363 char *str;
364
365 gelf_getshdr(sec, shp);
366 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
367 if (!strcmp(name, str)) {
368 if (index)
369 *index = cnt;
370 break;
371 }
372 ++cnt;
373 }
374
375 return sec;
376}
377
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300378#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
379 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
380 idx < nr_entries; \
381 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
382
383#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
384 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
385 idx < nr_entries; \
386 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
387
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300388/*
389 * We need to check if we have a .dynsym, so that we can handle the
390 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
391 * .dynsym or .symtab).
392 * And always look at the original dso, not at debuginfo packages, that
393 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
394 */
395static int dso__synthesize_plt_symbols(struct dso *self, int verbose)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300396{
397 uint32_t nr_rel_entries, idx;
398 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000399 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300400 GElf_Shdr shdr_plt;
401 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300402 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300403 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300404 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
405 size_t dynsym_idx;
406 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300407 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300408 Elf *elf;
409 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300410
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300411 fd = open(self->name, O_RDONLY);
412 if (fd < 0)
413 goto out;
414
415 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
416 if (elf == NULL)
417 goto out_close;
418
419 if (gelf_getehdr(elf, &ehdr) == NULL)
420 goto out_elf_end;
421
422 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
423 ".dynsym", &dynsym_idx);
424 if (scn_dynsym == NULL)
425 goto out_elf_end;
426
427 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300428 ".rela.plt", NULL);
429 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300430 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300431 ".rel.plt", NULL);
432 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300433 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300434 }
435
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300436 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300437
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300438 if (shdr_rel_plt.sh_link != dynsym_idx)
439 goto out_elf_end;
440
441 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
442 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300443
444 /*
445 * Fetch the relocation section to find the indexes to the GOT
446 * and the symbols in the .dynsym they refer to.
447 */
448 reldata = elf_getdata(scn_plt_rel, NULL);
449 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300450 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300451
452 syms = elf_getdata(scn_dynsym, NULL);
453 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300454 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300455
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300456 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300457 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300458 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300459
460 symstrs = elf_getdata(scn_symstrs, NULL);
461 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300462 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300463
464 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
465 plt_offset = shdr_plt.sh_offset;
466
467 if (shdr_rel_plt.sh_type == SHT_RELA) {
468 GElf_Rela pos_mem, *pos;
469
470 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
471 nr_rel_entries) {
472 symidx = GELF_R_SYM(pos->r_info);
473 plt_offset += shdr_plt.sh_entsize;
474 gelf_getsym(syms, symidx, &sym);
475 snprintf(sympltname, sizeof(sympltname),
476 "%s@plt", elf_sym__name(&sym, symstrs));
477
478 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200479 sympltname, self->sym_priv_size, 0, verbose);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300480 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300481 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300482
483 dso__insert_symbol(self, f);
484 ++nr;
485 }
486 } else if (shdr_rel_plt.sh_type == SHT_REL) {
487 GElf_Rel pos_mem, *pos;
488 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
489 nr_rel_entries) {
490 symidx = GELF_R_SYM(pos->r_info);
491 plt_offset += shdr_plt.sh_entsize;
492 gelf_getsym(syms, symidx, &sym);
493 snprintf(sympltname, sizeof(sympltname),
494 "%s@plt", elf_sym__name(&sym, symstrs));
495
496 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200497 sympltname, self->sym_priv_size, 0, verbose);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300498 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300499 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300500
501 dso__insert_symbol(self, f);
502 ++nr;
503 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300504 }
505
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300506 err = 0;
507out_elf_end:
508 elf_end(elf);
509out_close:
510 close(fd);
511
512 if (err == 0)
513 return nr;
514out:
515 fprintf(stderr, "%s: problems reading %s PLT info.\n",
516 __func__, self->name);
517 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300518}
519
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300520static int dso__load_sym(struct dso *self, int fd, const char *name,
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200521 symbol_filter_t filter, int verbose, struct module *mod)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300522{
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200523 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300524 uint32_t nr_syms;
525 int err = -1;
526 uint32_t index;
527 GElf_Ehdr ehdr;
528 GElf_Shdr shdr;
529 Elf_Data *syms;
530 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300531 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300532 Elf *elf;
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200533 int nr = 0, kernel = !strcmp("[kernel]", self->name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300534
535 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
536 if (elf == NULL) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200537 if (verbose)
538 fprintf(stderr, "%s: cannot read %s ELF file.\n",
539 __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300540 goto out_close;
541 }
542
543 if (gelf_getehdr(elf, &ehdr) == NULL) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200544 if (verbose)
545 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300546 goto out_elf_end;
547 }
548
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300549 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
550 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300551 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
552 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300553 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300554 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300555
556 syms = elf_getdata(sec, NULL);
557 if (syms == NULL)
558 goto out_elf_end;
559
560 sec = elf_getscn(elf, shdr.sh_link);
561 if (sec == NULL)
562 goto out_elf_end;
563
564 symstrs = elf_getdata(sec, NULL);
565 if (symstrs == NULL)
566 goto out_elf_end;
567
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200568 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
569 if (sec_strndx == NULL)
570 goto out_elf_end;
571
572 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500573 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200574 goto out_elf_end;
575
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300576 nr_syms = shdr.sh_size / shdr.sh_entsize;
577
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200578 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200579 if (!kernel) {
580 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300581 elf_section_by_name(elf, &ehdr, &shdr,
582 ".gnu.prelink_undo",
583 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200584 } else self->adjust_symbols = 0;
585
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300586 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
587 struct symbol *f;
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300588 const char *name;
589 char *demangled;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000590 u64 obj_start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200591 struct section *section = NULL;
592 int is_label = elf_sym__is_label(&sym);
593 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300594
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200595 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300596 continue;
597
598 sec = elf_getscn(elf, sym.st_shndx);
599 if (!sec)
600 goto out_elf_end;
601
602 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200603
604 if (is_label && !elf_sec__is_text(&shdr, secstrs))
605 continue;
606
607 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200608 obj_start = sym.st_value;
609
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300610 if (self->adjust_symbols) {
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300611 if (verbose >= 2)
612 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
613 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200614
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300615 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
616 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300617
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200618 if (mod) {
619 section = mod->sections->find_section(mod->sections, section_name);
620 if (section)
621 sym.st_value += section->vma;
622 else {
623 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
624 mod->name, section_name);
625 goto out_elf_end;
626 }
627 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300628 /*
629 * We need to figure out if the object was created from C++ sources
630 * DWARF DW_compile_unit has this, but we don't always have access
631 * to it...
632 */
633 name = elf_sym__name(&sym, symstrs);
634 demangled = bfd_demangle(NULL, name, DMGL_PARAMS | DMGL_ANSI);
635 if (demangled != NULL)
636 name = demangled;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200637
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300638 f = symbol__new(sym.st_value, sym.st_size, name,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200639 self->sym_priv_size, obj_start, verbose);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300640 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300641 if (!f)
642 goto out_elf_end;
643
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300644 if (filter && filter(self, f))
645 symbol__delete(f, self->sym_priv_size);
646 else {
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200647 f->module = mod;
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300648 dso__insert_symbol(self, f);
649 nr++;
650 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300651 }
652
653 err = nr;
654out_elf_end:
655 elf_end(elf);
656out_close:
657 return err;
658}
659
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300660#define BUILD_ID_SIZE 128
661
662static char *dso__read_build_id(struct dso *self, int verbose)
663{
664 int i;
665 GElf_Ehdr ehdr;
666 GElf_Shdr shdr;
667 Elf_Data *build_id_data;
668 Elf_Scn *sec;
669 char *build_id = NULL, *bid;
670 unsigned char *raw;
671 Elf *elf;
672 int fd = open(self->name, O_RDONLY);
673
674 if (fd < 0)
675 goto out;
676
677 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
678 if (elf == NULL) {
679 if (verbose)
680 fprintf(stderr, "%s: cannot read %s ELF file.\n",
681 __func__, self->name);
682 goto out_close;
683 }
684
685 if (gelf_getehdr(elf, &ehdr) == NULL) {
686 if (verbose)
687 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
688 goto out_elf_end;
689 }
690
691 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
692 if (sec == NULL)
693 goto out_elf_end;
694
695 build_id_data = elf_getdata(sec, NULL);
696 if (build_id_data == NULL)
697 goto out_elf_end;
698 build_id = malloc(BUILD_ID_SIZE);
699 if (build_id == NULL)
700 goto out_elf_end;
701 raw = build_id_data->d_buf + 16;
702 bid = build_id;
703
704 for (i = 0; i < 20; ++i) {
705 sprintf(bid, "%02x", *raw);
706 ++raw;
707 bid += 2;
708 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300709 if (verbose >= 2)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300710 printf("%s(%s): %s\n", __func__, self->name, build_id);
711out_elf_end:
712 elf_end(elf);
713out_close:
714 close(fd);
715out:
716 return build_id;
717}
718
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300719char dso__symtab_origin(const struct dso *self)
720{
721 static const char origin[] = {
722 [DSO__ORIG_KERNEL] = 'k',
723 [DSO__ORIG_JAVA_JIT] = 'j',
724 [DSO__ORIG_FEDORA] = 'f',
725 [DSO__ORIG_UBUNTU] = 'u',
726 [DSO__ORIG_BUILDID] = 'b',
727 [DSO__ORIG_DSO] = 'd',
728 };
729
730 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
731 return '!';
732 return origin[self->origin];
733}
734
Ingo Molnarbd741372009-06-04 14:13:04 +0200735int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300736{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300737 int size = PATH_MAX;
738 char *name = malloc(size), *build_id = NULL;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300739 int ret = -1;
740 int fd;
741
742 if (!name)
743 return -1;
744
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300745 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300746
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300747 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
748 ret = dso__load_perf_map(self, filter, verbose);
749 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
750 DSO__ORIG_NOT_FOUND;
751 return ret;
752 }
753
754 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300755
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300756more:
757 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300758 self->origin++;
759 switch (self->origin) {
760 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300761 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
762 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300763 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300764 snprintf(name, size, "/usr/lib/debug%s", self->name);
765 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300766 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300767 build_id = dso__read_build_id(self, verbose);
768 if (build_id != NULL) {
769 snprintf(name, size,
770 "/usr/lib/debug/.build-id/%.2s/%s.debug",
771 build_id, build_id + 2);
772 free(build_id);
773 break;
774 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300775 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300776 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300777 case DSO__ORIG_DSO:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300778 snprintf(name, size, "%s", self->name);
779 break;
780
781 default:
782 goto out;
783 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300784
785 fd = open(name, O_RDONLY);
786 } while (fd < 0);
787
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200788 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300789 close(fd);
790
791 /*
792 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
793 */
794 if (!ret)
795 goto more;
796
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300797 if (ret > 0) {
798 int nr_plt = dso__synthesize_plt_symbols(self, verbose);
799 if (nr_plt > 0)
800 ret += nr_plt;
801 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300802out:
803 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -0300804 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
805 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300806 return ret;
807}
808
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200809static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
810 symbol_filter_t filter, int verbose)
811{
812 struct module *mod = mod_dso__find_module(mods, name);
813 int err = 0, fd;
814
815 if (mod == NULL || !mod->active)
816 return err;
817
818 fd = open(mod->path, O_RDONLY);
819
820 if (fd < 0)
821 return err;
822
823 err = dso__load_sym(self, fd, name, filter, verbose, mod);
824 close(fd);
825
826 return err;
827}
828
829int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
830{
831 struct mod_dso *mods = mod_dso__new_dso("modules");
832 struct module *pos;
833 struct rb_node *next;
834 int err;
835
836 err = mod_dso__load_modules(mods);
837
838 if (err <= 0)
839 return err;
840
841 /*
842 * Iterate over modules, and load active symbols.
843 */
844 next = rb_first(&mods->mods);
845 while (next) {
846 pos = rb_entry(next, struct module, rb_node);
847 err = dso__load_module(self, mods, pos->name, filter, verbose);
848
849 if (err < 0)
850 break;
851
852 next = rb_next(&pos->rb_node);
853 }
854
855 if (err < 0) {
856 mod_dso__delete_modules(mods);
857 mod_dso__delete_self(mods);
858 }
859
860 return err;
861}
862
863static inline void dso__fill_symbol_holes(struct dso *self)
864{
865 struct symbol *prev = NULL;
866 struct rb_node *nd;
867
868 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
869 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
870
871 if (prev) {
872 u64 hole = 0;
873 int alias = pos->start == prev->start;
874
875 if (!alias)
876 hole = prev->start - pos->end - 1;
877
878 if (hole || alias) {
879 if (alias)
880 pos->end = prev->end;
881 else if (hole)
882 pos->end = prev->start - 1;
883 }
884 }
885 prev = pos;
886 }
887}
888
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300889static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
Ingo Molnarbd741372009-06-04 14:13:04 +0200890 symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300891{
892 int err, fd = open(vmlinux, O_RDONLY);
893
894 if (fd < 0)
895 return -1;
896
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200897 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
898
899 if (err > 0)
900 dso__fill_symbol_holes(self);
901
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300902 close(fd);
903
904 return err;
905}
906
Ingo Molnarbd741372009-06-04 14:13:04 +0200907int dso__load_kernel(struct dso *self, const char *vmlinux,
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200908 symbol_filter_t filter, int verbose, int modules)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300909{
910 int err = -1;
911
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200912 if (vmlinux) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200913 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200914 if (err > 0 && modules)
915 err = dso__load_modules(self, filter, verbose);
916 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300917
Mike Galbraith9974f492009-07-02 08:05:58 +0200918 if (err <= 0)
Ingo Molnarbd741372009-06-04 14:13:04 +0200919 err = dso__load_kallsyms(self, filter, verbose);
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300920
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300921 if (err > 0)
922 self->origin = DSO__ORIG_KERNEL;
923
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300924 return err;
925}
926
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300927void symbol__init(void)
928{
929 elf_version(EV_CURRENT);
930}