blob: 8efe7e41109a7dcd3cd5fc876c29ab50af1b8fec [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
Ingo Molnar0b73da32009-06-06 15:48:52 +020010const char *sym_hist_filter;
11
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100012static struct symbol *symbol__new(u64 start, u64 len,
Ingo Molnar0b73da32009-06-06 15:48:52 +020013 const char *name, unsigned int priv_size,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100014 u64 obj_start, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030015{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030016 size_t namelen = strlen(name) + 1;
Ingo Molnar0b73da32009-06-06 15:48:52 +020017 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030018
Ingo Molnar0b73da32009-06-06 15:48:52 +020019 if (!self)
20 return NULL;
21
22 if (verbose >= 2)
23 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100024 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
Ingo Molnar0b73da32009-06-06 15:48:52 +020025
26 self->obj_start= obj_start;
27 self->hist = NULL;
28 self->hist_sum = 0;
29
30 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100031 self->hist = calloc(sizeof(u64), len);
Ingo Molnar0b73da32009-06-06 15:48:52 +020032
33 if (priv_size) {
34 memset(self, 0, priv_size);
35 self = ((void *)self) + priv_size;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030036 }
Ingo Molnar0b73da32009-06-06 15:48:52 +020037 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +020038 self->end = len ? start + len - 1 : start;
Ingo Molnar0b73da32009-06-06 15:48:52 +020039 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030040
41 return self;
42}
43
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030044static void symbol__delete(struct symbol *self, unsigned int priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030045{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030046 free(((void *)self) - priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030047}
48
49static size_t symbol__fprintf(struct symbol *self, FILE *fp)
50{
Mike Galbraith6cfcc532009-07-02 08:08:36 +020051 if (!self->module)
52 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030053 self->start, self->end, self->name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +020054 else
55 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
56 self->start, self->end, self->name, self->module->name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030057}
58
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030059struct dso *dso__new(const char *name, unsigned int sym_priv_size)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030060{
61 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
62
63 if (self != NULL) {
64 strcpy(self->name, name);
65 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030066 self->sym_priv_size = sym_priv_size;
Peter Zijlstrafc54db52009-06-05 14:04:59 +020067 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030068 self->slen_calculated = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030069 }
70
71 return self;
72}
73
74static void dso__delete_symbols(struct dso *self)
75{
76 struct symbol *pos;
77 struct rb_node *next = rb_first(&self->syms);
78
79 while (next) {
80 pos = rb_entry(next, struct symbol, rb_node);
81 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloc8c96522009-06-01 17:50:57 -030082 rb_erase(&pos->rb_node, &self->syms);
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -030083 symbol__delete(pos, self->sym_priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030084 }
85}
86
87void dso__delete(struct dso *self)
88{
89 dso__delete_symbols(self);
90 free(self);
91}
92
93static void dso__insert_symbol(struct dso *self, struct symbol *sym)
94{
95 struct rb_node **p = &self->syms.rb_node;
96 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100097 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030098 struct symbol *s;
99
100 while (*p != NULL) {
101 parent = *p;
102 s = rb_entry(parent, struct symbol, rb_node);
103 if (ip < s->start)
104 p = &(*p)->rb_left;
105 else
106 p = &(*p)->rb_right;
107 }
108 rb_link_node(&sym->rb_node, parent, p);
109 rb_insert_color(&sym->rb_node, &self->syms);
110}
111
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000112struct symbol *dso__find_symbol(struct dso *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300113{
114 struct rb_node *n;
115
116 if (self == NULL)
117 return NULL;
118
119 n = self->syms.rb_node;
120
121 while (n) {
122 struct symbol *s = rb_entry(n, struct symbol, rb_node);
123
124 if (ip < s->start)
125 n = n->rb_left;
126 else if (ip > s->end)
127 n = n->rb_right;
128 else
129 return s;
130 }
131
132 return NULL;
133}
134
135size_t dso__fprintf(struct dso *self, FILE *fp)
136{
137 size_t ret = fprintf(fp, "dso: %s\n", self->name);
138
139 struct rb_node *nd;
140 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
141 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
142 ret += symbol__fprintf(pos, fp);
143 }
144
145 return ret;
146}
147
Ingo Molnarbd741372009-06-04 14:13:04 +0200148static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300149{
150 struct rb_node *nd, *prevnd;
151 char *line = NULL;
152 size_t n;
153 FILE *file = fopen("/proc/kallsyms", "r");
Mike Galbraith9974f492009-07-02 08:05:58 +0200154 int count = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300155
156 if (file == NULL)
157 goto out_failure;
158
159 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000160 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300161 struct symbol *sym;
162 int line_len, len;
163 char symbol_type;
164
165 line_len = getline(&line, &n, file);
166 if (line_len < 0)
167 break;
168
169 if (!line)
170 goto out_failure;
171
172 line[--line_len] = '\0'; /* \n */
173
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300174 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300175
176 len++;
177 if (len + 2 >= line_len)
178 continue;
179
180 symbol_type = toupper(line[len]);
181 /*
182 * We're interested only in code ('T'ext)
183 */
184 if (symbol_type != 'T' && symbol_type != 'W')
185 continue;
186 /*
187 * Well fix up the end later, when we have all sorted.
188 */
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300189 sym = symbol__new(start, 0xdead, line + len + 2,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200190 self->sym_priv_size, 0, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300191
192 if (sym == NULL)
193 goto out_delete_line;
194
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300195 if (filter && filter(self, sym))
196 symbol__delete(sym, self->sym_priv_size);
Mike Galbraith9974f492009-07-02 08:05:58 +0200197 else {
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300198 dso__insert_symbol(self, sym);
Mike Galbraith9974f492009-07-02 08:05:58 +0200199 count++;
200 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300201 }
202
203 /*
204 * Now that we have all sorted out, just set the ->end of all
205 * symbols
206 */
207 prevnd = rb_first(&self->syms);
208
209 if (prevnd == NULL)
210 goto out_delete_line;
211
212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
213 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
214 *curr = rb_entry(nd, struct symbol, rb_node);
215
216 prev->end = curr->start - 1;
217 prevnd = nd;
218 }
219
220 free(line);
221 fclose(file);
222
Mike Galbraith9974f492009-07-02 08:05:58 +0200223 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300224
225out_delete_line:
226 free(line);
227out_failure:
228 return -1;
229}
230
Pekka Enberg80d496b2009-06-08 21:12:48 +0300231static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
232{
233 char *line = NULL;
234 size_t n;
235 FILE *file;
236 int nr_syms = 0;
237
238 file = fopen(self->name, "r");
239 if (file == NULL)
240 goto out_failure;
241
242 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000243 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300244 struct symbol *sym;
245 int line_len, len;
246
247 line_len = getline(&line, &n, file);
248 if (line_len < 0)
249 break;
250
251 if (!line)
252 goto out_failure;
253
254 line[--line_len] = '\0'; /* \n */
255
256 len = hex2u64(line, &start);
257
258 len++;
259 if (len + 2 >= line_len)
260 continue;
261
262 len += hex2u64(line + len, &size);
263
264 len++;
265 if (len + 2 >= line_len)
266 continue;
267
268 sym = symbol__new(start, size, line + len,
269 self->sym_priv_size, start, verbose);
270
271 if (sym == NULL)
272 goto out_delete_line;
273
274 if (filter && filter(self, sym))
275 symbol__delete(sym, self->sym_priv_size);
276 else {
277 dso__insert_symbol(self, sym);
278 nr_syms++;
279 }
280 }
281
282 free(line);
283 fclose(file);
284
285 return nr_syms;
286
287out_delete_line:
288 free(line);
289out_failure:
290 return -1;
291}
292
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300293/**
294 * elf_symtab__for_each_symbol - iterate thru all the symbols
295 *
296 * @self: struct elf_symtab instance to iterate
297 * @index: uint32_t index
298 * @sym: GElf_Sym iterator
299 */
300#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
301 for (index = 0, gelf_getsym(syms, index, &sym);\
302 index < nr_syms; \
303 index++, gelf_getsym(syms, index, &sym))
304
305static inline uint8_t elf_sym__type(const GElf_Sym *sym)
306{
307 return GELF_ST_TYPE(sym->st_info);
308}
309
310static inline int elf_sym__is_function(const GElf_Sym *sym)
311{
312 return elf_sym__type(sym) == STT_FUNC &&
313 sym->st_name != 0 &&
314 sym->st_shndx != SHN_UNDEF &&
315 sym->st_size != 0;
316}
317
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200318static inline int elf_sym__is_label(const GElf_Sym *sym)
319{
320 return elf_sym__type(sym) == STT_NOTYPE &&
321 sym->st_name != 0 &&
322 sym->st_shndx != SHN_UNDEF &&
323 sym->st_shndx != SHN_ABS;
324}
325
326static inline const char *elf_sec__name(const GElf_Shdr *shdr,
327 const Elf_Data *secstrs)
328{
329 return secstrs->d_buf + shdr->sh_name;
330}
331
332static inline int elf_sec__is_text(const GElf_Shdr *shdr,
333 const Elf_Data *secstrs)
334{
335 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
336}
337
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300338static inline const char *elf_sym__name(const GElf_Sym *sym,
339 const Elf_Data *symstrs)
340{
341 return symstrs->d_buf + sym->st_name;
342}
343
344static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
345 GElf_Shdr *shp, const char *name,
346 size_t *index)
347{
348 Elf_Scn *sec = NULL;
349 size_t cnt = 1;
350
351 while ((sec = elf_nextscn(elf, sec)) != NULL) {
352 char *str;
353
354 gelf_getshdr(sec, shp);
355 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
356 if (!strcmp(name, str)) {
357 if (index)
358 *index = cnt;
359 break;
360 }
361 ++cnt;
362 }
363
364 return sec;
365}
366
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300367#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
368 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
369 idx < nr_entries; \
370 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
371
372#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
373 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
374 idx < nr_entries; \
375 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
376
377static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf,
378 GElf_Ehdr *ehdr, Elf_Scn *scn_dynsym,
379 GElf_Shdr *shdr_dynsym,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200380 size_t dynsym_idx, int verbose)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300381{
382 uint32_t nr_rel_entries, idx;
383 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000384 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300385 GElf_Shdr shdr_plt;
386 struct symbol *f;
387 GElf_Shdr shdr_rel_plt;
388 Elf_Data *reldata, *syms, *symstrs;
389 Elf_Scn *scn_plt_rel, *scn_symstrs;
390 char sympltname[1024];
391 int nr = 0, symidx;
392
393 scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
394 ".rela.plt", NULL);
395 if (scn_plt_rel == NULL) {
396 scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
397 ".rel.plt", NULL);
398 if (scn_plt_rel == NULL)
399 return 0;
400 }
401
402 if (shdr_rel_plt.sh_link != dynsym_idx)
403 return 0;
404
405 if (elf_section_by_name(elf, ehdr, &shdr_plt, ".plt", NULL) == NULL)
406 return 0;
407
408 /*
409 * Fetch the relocation section to find the indexes to the GOT
410 * and the symbols in the .dynsym they refer to.
411 */
412 reldata = elf_getdata(scn_plt_rel, NULL);
413 if (reldata == NULL)
414 return -1;
415
416 syms = elf_getdata(scn_dynsym, NULL);
417 if (syms == NULL)
418 return -1;
419
420 scn_symstrs = elf_getscn(elf, shdr_dynsym->sh_link);
421 if (scn_symstrs == NULL)
422 return -1;
423
424 symstrs = elf_getdata(scn_symstrs, NULL);
425 if (symstrs == NULL)
426 return -1;
427
428 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
429 plt_offset = shdr_plt.sh_offset;
430
431 if (shdr_rel_plt.sh_type == SHT_RELA) {
432 GElf_Rela pos_mem, *pos;
433
434 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
435 nr_rel_entries) {
436 symidx = GELF_R_SYM(pos->r_info);
437 plt_offset += shdr_plt.sh_entsize;
438 gelf_getsym(syms, symidx, &sym);
439 snprintf(sympltname, sizeof(sympltname),
440 "%s@plt", elf_sym__name(&sym, symstrs));
441
442 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200443 sympltname, self->sym_priv_size, 0, verbose);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300444 if (!f)
445 return -1;
446
447 dso__insert_symbol(self, f);
448 ++nr;
449 }
450 } else if (shdr_rel_plt.sh_type == SHT_REL) {
451 GElf_Rel pos_mem, *pos;
452 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
453 nr_rel_entries) {
454 symidx = GELF_R_SYM(pos->r_info);
455 plt_offset += shdr_plt.sh_entsize;
456 gelf_getsym(syms, symidx, &sym);
457 snprintf(sympltname, sizeof(sympltname),
458 "%s@plt", elf_sym__name(&sym, symstrs));
459
460 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200461 sympltname, self->sym_priv_size, 0, verbose);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300462 if (!f)
463 return -1;
464
465 dso__insert_symbol(self, f);
466 ++nr;
467 }
468 } else {
469 /*
470 * TODO: There are still one more shdr_rel_plt.sh_type
471 * I have to investigate, but probably should be ignored.
472 */
473 }
474
475 return nr;
476}
477
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300478static int dso__load_sym(struct dso *self, int fd, const char *name,
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200479 symbol_filter_t filter, int verbose, struct module *mod)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300480{
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200481 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300482 uint32_t nr_syms;
483 int err = -1;
484 uint32_t index;
485 GElf_Ehdr ehdr;
486 GElf_Shdr shdr;
487 Elf_Data *syms;
488 GElf_Sym sym;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200489 Elf_Scn *sec, *sec_dynsym, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300490 Elf *elf;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300491 size_t dynsym_idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300492 int nr = 0;
493
494 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
495 if (elf == NULL) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200496 if (verbose)
497 fprintf(stderr, "%s: cannot read %s ELF file.\n",
498 __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300499 goto out_close;
500 }
501
502 if (gelf_getehdr(elf, &ehdr) == NULL) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200503 if (verbose)
504 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300505 goto out_elf_end;
506 }
507
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300508 /*
509 * We need to check if we have a .dynsym, so that we can handle the
510 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
511 * .dynsym or .symtab)
512 */
513 sec_dynsym = elf_section_by_name(elf, &ehdr, &shdr,
514 ".dynsym", &dynsym_idx);
515 if (sec_dynsym != NULL) {
516 nr = dso__synthesize_plt_symbols(self, elf, &ehdr,
517 sec_dynsym, &shdr,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200518 dynsym_idx, verbose);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300519 if (nr < 0)
520 goto out_elf_end;
521 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300522
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300523 /*
524 * But if we have a full .symtab (that is a superset of .dynsym) we
525 * should add the symbols not in the .dynsyn
526 */
527 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
528 if (sec == NULL) {
529 if (sec_dynsym == NULL)
530 goto out_elf_end;
531
532 sec = sec_dynsym;
533 gelf_getshdr(sec, &shdr);
534 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300535
536 syms = elf_getdata(sec, NULL);
537 if (syms == NULL)
538 goto out_elf_end;
539
540 sec = elf_getscn(elf, shdr.sh_link);
541 if (sec == NULL)
542 goto out_elf_end;
543
544 symstrs = elf_getdata(sec, NULL);
545 if (symstrs == NULL)
546 goto out_elf_end;
547
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200548 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
549 if (sec_strndx == NULL)
550 goto out_elf_end;
551
552 secstrs = elf_getdata(sec_strndx, NULL);
553 if (symstrs == NULL)
554 goto out_elf_end;
555
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300556 nr_syms = shdr.sh_size / shdr.sh_entsize;
557
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200558 memset(&sym, 0, sizeof(sym));
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300559 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
560 elf_section_by_name(elf, &ehdr, &shdr,
561 ".gnu.prelink_undo",
562 NULL) != NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300563 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
564 struct symbol *f;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000565 u64 obj_start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200566 struct section *section = NULL;
567 int is_label = elf_sym__is_label(&sym);
568 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300569
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200570 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300571 continue;
572
573 sec = elf_getscn(elf, sym.st_shndx);
574 if (!sec)
575 goto out_elf_end;
576
577 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200578
579 if (is_label && !elf_sec__is_text(&shdr, secstrs))
580 continue;
581
582 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200583 obj_start = sym.st_value;
584
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300585 if (self->adjust_symbols) {
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300586 if (verbose >= 2)
587 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
588 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Peter Zijlstra520f2c32009-06-22 16:52:51 +0200589
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300590 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
591 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300592
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200593 if (mod) {
594 section = mod->sections->find_section(mod->sections, section_name);
595 if (section)
596 sym.st_value += section->vma;
597 else {
598 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
599 mod->name, section_name);
600 goto out_elf_end;
601 }
602 }
603
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300604 f = symbol__new(sym.st_value, sym.st_size,
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300605 elf_sym__name(&sym, symstrs),
Ingo Molnar0b73da32009-06-06 15:48:52 +0200606 self->sym_priv_size, obj_start, verbose);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300607 if (!f)
608 goto out_elf_end;
609
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300610 if (filter && filter(self, f))
611 symbol__delete(f, self->sym_priv_size);
612 else {
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200613 f->module = mod;
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300614 dso__insert_symbol(self, f);
615 nr++;
616 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300617 }
618
619 err = nr;
620out_elf_end:
621 elf_end(elf);
622out_close:
623 return err;
624}
625
Ingo Molnarbd741372009-06-04 14:13:04 +0200626int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300627{
628 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
629 char *name = malloc(size);
630 int variant = 0;
631 int ret = -1;
632 int fd;
633
634 if (!name)
635 return -1;
636
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300637 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300638
Pekka Enberg80d496b2009-06-08 21:12:48 +0300639 if (strncmp(self->name, "/tmp/perf-", 10) == 0)
640 return dso__load_perf_map(self, filter, verbose);
641
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300642more:
643 do {
644 switch (variant) {
645 case 0: /* Fedora */
646 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
647 break;
648 case 1: /* Ubuntu */
649 snprintf(name, size, "/usr/lib/debug%s", self->name);
650 break;
651 case 2: /* Sane people */
652 snprintf(name, size, "%s", self->name);
653 break;
654
655 default:
656 goto out;
657 }
658 variant++;
659
660 fd = open(name, O_RDONLY);
661 } while (fd < 0);
662
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200663 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300664 close(fd);
665
666 /*
667 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
668 */
669 if (!ret)
670 goto more;
671
672out:
673 free(name);
674 return ret;
675}
676
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200677static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
678 symbol_filter_t filter, int verbose)
679{
680 struct module *mod = mod_dso__find_module(mods, name);
681 int err = 0, fd;
682
683 if (mod == NULL || !mod->active)
684 return err;
685
686 fd = open(mod->path, O_RDONLY);
687
688 if (fd < 0)
689 return err;
690
691 err = dso__load_sym(self, fd, name, filter, verbose, mod);
692 close(fd);
693
694 return err;
695}
696
697int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
698{
699 struct mod_dso *mods = mod_dso__new_dso("modules");
700 struct module *pos;
701 struct rb_node *next;
702 int err;
703
704 err = mod_dso__load_modules(mods);
705
706 if (err <= 0)
707 return err;
708
709 /*
710 * Iterate over modules, and load active symbols.
711 */
712 next = rb_first(&mods->mods);
713 while (next) {
714 pos = rb_entry(next, struct module, rb_node);
715 err = dso__load_module(self, mods, pos->name, filter, verbose);
716
717 if (err < 0)
718 break;
719
720 next = rb_next(&pos->rb_node);
721 }
722
723 if (err < 0) {
724 mod_dso__delete_modules(mods);
725 mod_dso__delete_self(mods);
726 }
727
728 return err;
729}
730
731static inline void dso__fill_symbol_holes(struct dso *self)
732{
733 struct symbol *prev = NULL;
734 struct rb_node *nd;
735
736 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
737 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
738
739 if (prev) {
740 u64 hole = 0;
741 int alias = pos->start == prev->start;
742
743 if (!alias)
744 hole = prev->start - pos->end - 1;
745
746 if (hole || alias) {
747 if (alias)
748 pos->end = prev->end;
749 else if (hole)
750 pos->end = prev->start - 1;
751 }
752 }
753 prev = pos;
754 }
755}
756
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300757static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
Ingo Molnarbd741372009-06-04 14:13:04 +0200758 symbol_filter_t filter, int verbose)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300759{
760 int err, fd = open(vmlinux, O_RDONLY);
761
762 if (fd < 0)
763 return -1;
764
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200765 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
766
767 if (err > 0)
768 dso__fill_symbol_holes(self);
769
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300770 close(fd);
771
772 return err;
773}
774
Ingo Molnarbd741372009-06-04 14:13:04 +0200775int dso__load_kernel(struct dso *self, const char *vmlinux,
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200776 symbol_filter_t filter, int verbose, int modules)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300777{
778 int err = -1;
779
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200780 if (vmlinux) {
Ingo Molnarbd741372009-06-04 14:13:04 +0200781 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200782 if (err > 0 && modules)
783 err = dso__load_modules(self, filter, verbose);
784 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300785
Mike Galbraith9974f492009-07-02 08:05:58 +0200786 if (err <= 0)
Ingo Molnarbd741372009-06-04 14:13:04 +0200787 err = dso__load_kallsyms(self, filter, verbose);
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -0300788
789 return err;
790}
791
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300792void symbol__init(void)
793{
794 elf_version(EV_CURRENT);
795}