10 const char *sym_hist_filter
;
12 static struct symbol
*symbol__new(uint64_t start
, uint64_t len
,
13 const char *name
, unsigned int priv_size
,
14 uint64_t obj_start
, int verbose
)
16 size_t namelen
= strlen(name
) + 1;
17 struct symbol
*self
= calloc(1, priv_size
+ sizeof(*self
) + namelen
);
23 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
24 (__u64
)start
, (unsigned long)len
, name
, self
->hist
, (void *)(unsigned long)obj_start
);
26 self
->obj_start
= obj_start
;
30 if (sym_hist_filter
&& !strcmp(name
, sym_hist_filter
))
31 self
->hist
= calloc(sizeof(__u64
), len
);
34 memset(self
, 0, priv_size
);
35 self
= ((void *)self
) + priv_size
;
38 self
->end
= start
+ len
- 1;
39 memcpy(self
->name
, name
, namelen
);
44 static void symbol__delete(struct symbol
*self
, unsigned int priv_size
)
46 free(((void *)self
) - priv_size
);
49 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
51 return fprintf(fp
, " %llx-%llx %s\n",
52 self
->start
, self
->end
, self
->name
);
55 struct dso
*dso__new(const char *name
, unsigned int sym_priv_size
)
57 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
60 strcpy(self
->name
, name
);
62 self
->sym_priv_size
= sym_priv_size
;
63 self
->find_symbol
= dso__find_symbol
;
69 static void dso__delete_symbols(struct dso
*self
)
72 struct rb_node
*next
= rb_first(&self
->syms
);
75 pos
= rb_entry(next
, struct symbol
, rb_node
);
76 next
= rb_next(&pos
->rb_node
);
77 rb_erase(&pos
->rb_node
, &self
->syms
);
78 symbol__delete(pos
, self
->sym_priv_size
);
82 void dso__delete(struct dso
*self
)
84 dso__delete_symbols(self
);
88 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
90 struct rb_node
**p
= &self
->syms
.rb_node
;
91 struct rb_node
*parent
= NULL
;
92 const uint64_t ip
= sym
->start
;
97 s
= rb_entry(parent
, struct symbol
, rb_node
);
103 rb_link_node(&sym
->rb_node
, parent
, p
);
104 rb_insert_color(&sym
->rb_node
, &self
->syms
);
107 struct symbol
*dso__find_symbol(struct dso
*self
, uint64_t ip
)
114 n
= self
->syms
.rb_node
;
117 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
121 else if (ip
> s
->end
)
130 size_t dso__fprintf(struct dso
*self
, FILE *fp
)
132 size_t ret
= fprintf(fp
, "dso: %s\n", self
->name
);
135 for (nd
= rb_first(&self
->syms
); nd
; nd
= rb_next(nd
)) {
136 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
137 ret
+= symbol__fprintf(pos
, fp
);
143 static int dso__load_kallsyms(struct dso
*self
, symbol_filter_t filter
, int verbose
)
145 struct rb_node
*nd
, *prevnd
;
148 FILE *file
= fopen("/proc/kallsyms", "r");
153 while (!feof(file
)) {
159 line_len
= getline(&line
, &n
, file
);
166 line
[--line_len
] = '\0'; /* \n */
168 len
= hex2u64(line
, &start
);
171 if (len
+ 2 >= line_len
)
174 symbol_type
= toupper(line
[len
]);
176 * We're interested only in code ('T'ext)
178 if (symbol_type
!= 'T' && symbol_type
!= 'W')
181 * Well fix up the end later, when we have all sorted.
183 sym
= symbol__new(start
, 0xdead, line
+ len
+ 2,
184 self
->sym_priv_size
, 0, verbose
);
187 goto out_delete_line
;
189 if (filter
&& filter(self
, sym
))
190 symbol__delete(sym
, self
->sym_priv_size
);
192 dso__insert_symbol(self
, sym
);
196 * Now that we have all sorted out, just set the ->end of all
199 prevnd
= rb_first(&self
->syms
);
202 goto out_delete_line
;
204 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
205 struct symbol
*prev
= rb_entry(prevnd
, struct symbol
, rb_node
),
206 *curr
= rb_entry(nd
, struct symbol
, rb_node
);
208 prev
->end
= curr
->start
- 1;
224 * elf_symtab__for_each_symbol - iterate thru all the symbols
226 * @self: struct elf_symtab instance to iterate
227 * @index: uint32_t index
228 * @sym: GElf_Sym iterator
230 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
231 for (index = 0, gelf_getsym(syms, index, &sym);\
233 index++, gelf_getsym(syms, index, &sym))
235 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
237 return GELF_ST_TYPE(sym
->st_info
);
240 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
242 return elf_sym__type(sym
) == STT_FUNC
&&
244 sym
->st_shndx
!= SHN_UNDEF
&&
248 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
249 const Elf_Data
*symstrs
)
251 return symstrs
->d_buf
+ sym
->st_name
;
254 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
255 GElf_Shdr
*shp
, const char *name
,
261 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
264 gelf_getshdr(sec
, shp
);
265 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
266 if (!strcmp(name
, str
)) {
277 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
278 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
280 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
282 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
283 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
285 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
287 static int dso__synthesize_plt_symbols(struct dso
*self
, Elf
*elf
,
288 GElf_Ehdr
*ehdr
, Elf_Scn
*scn_dynsym
,
289 GElf_Shdr
*shdr_dynsym
,
290 size_t dynsym_idx
, int verbose
)
292 uint32_t nr_rel_entries
, idx
;
297 GElf_Shdr shdr_rel_plt
;
298 Elf_Data
*reldata
, *syms
, *symstrs
;
299 Elf_Scn
*scn_plt_rel
, *scn_symstrs
;
300 char sympltname
[1024];
303 scn_plt_rel
= elf_section_by_name(elf
, ehdr
, &shdr_rel_plt
,
305 if (scn_plt_rel
== NULL
) {
306 scn_plt_rel
= elf_section_by_name(elf
, ehdr
, &shdr_rel_plt
,
308 if (scn_plt_rel
== NULL
)
312 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
315 if (elf_section_by_name(elf
, ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
319 * Fetch the relocation section to find the indexes to the GOT
320 * and the symbols in the .dynsym they refer to.
322 reldata
= elf_getdata(scn_plt_rel
, NULL
);
326 syms
= elf_getdata(scn_dynsym
, NULL
);
330 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
->sh_link
);
331 if (scn_symstrs
== NULL
)
334 symstrs
= elf_getdata(scn_symstrs
, NULL
);
338 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
339 plt_offset
= shdr_plt
.sh_offset
;
341 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
342 GElf_Rela pos_mem
, *pos
;
344 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
346 symidx
= GELF_R_SYM(pos
->r_info
);
347 plt_offset
+= shdr_plt
.sh_entsize
;
348 gelf_getsym(syms
, symidx
, &sym
);
349 snprintf(sympltname
, sizeof(sympltname
),
350 "%s@plt", elf_sym__name(&sym
, symstrs
));
352 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
353 sympltname
, self
->sym_priv_size
, 0, verbose
);
357 dso__insert_symbol(self
, f
);
360 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
361 GElf_Rel pos_mem
, *pos
;
362 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
364 symidx
= GELF_R_SYM(pos
->r_info
);
365 plt_offset
+= shdr_plt
.sh_entsize
;
366 gelf_getsym(syms
, symidx
, &sym
);
367 snprintf(sympltname
, sizeof(sympltname
),
368 "%s@plt", elf_sym__name(&sym
, symstrs
));
370 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
371 sympltname
, self
->sym_priv_size
, 0, verbose
);
375 dso__insert_symbol(self
, f
);
380 * TODO: There are still one more shdr_rel_plt.sh_type
381 * I have to investigate, but probably should be ignored.
388 static int dso__load_sym(struct dso
*self
, int fd
, const char *name
,
389 symbol_filter_t filter
, int verbose
)
399 Elf_Scn
*sec
, *sec_dynsym
;
404 elf
= elf_begin(fd
, ELF_C_READ_MMAP
, NULL
);
407 fprintf(stderr
, "%s: cannot read %s ELF file.\n",
412 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
414 fprintf(stderr
, "%s: cannot get elf header.\n", __func__
);
419 * We need to check if we have a .dynsym, so that we can handle the
420 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
421 * .dynsym or .symtab)
423 sec_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr
,
424 ".dynsym", &dynsym_idx
);
425 if (sec_dynsym
!= NULL
) {
426 nr
= dso__synthesize_plt_symbols(self
, elf
, &ehdr
,
428 dynsym_idx
, verbose
);
434 * But if we have a full .symtab (that is a superset of .dynsym) we
435 * should add the symbols not in the .dynsyn
437 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
439 if (sec_dynsym
== NULL
)
443 gelf_getshdr(sec
, &shdr
);
446 syms
= elf_getdata(sec
, NULL
);
450 sec
= elf_getscn(elf
, shdr
.sh_link
);
454 symstrs
= elf_getdata(sec
, NULL
);
458 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
460 elf_symtab__for_each_symbol(syms
, nr_syms
, index
, sym
) {
464 if (!elf_sym__is_function(&sym
))
467 sec
= elf_getscn(elf
, sym
.st_shndx
);
471 gelf_getshdr(sec
, &shdr
);
472 obj_start
= sym
.st_value
;
474 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
476 f
= symbol__new(sym
.st_value
, sym
.st_size
,
477 elf_sym__name(&sym
, symstrs
),
478 self
->sym_priv_size
, obj_start
, verbose
);
482 if (filter
&& filter(self
, f
))
483 symbol__delete(f
, self
->sym_priv_size
);
485 dso__insert_symbol(self
, f
);
497 int dso__load(struct dso
*self
, symbol_filter_t filter
, int verbose
)
499 int size
= strlen(self
->name
) + sizeof("/usr/lib/debug%s.debug");
500 char *name
= malloc(size
);
512 snprintf(name
, size
, "/usr/lib/debug%s.debug", self
->name
);
515 snprintf(name
, size
, "/usr/lib/debug%s", self
->name
);
517 case 2: /* Sane people */
518 snprintf(name
, size
, "%s", self
->name
);
526 fd
= open(name
, O_RDONLY
);
529 ret
= dso__load_sym(self
, fd
, name
, filter
, verbose
);
533 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
543 static int dso__load_vmlinux(struct dso
*self
, const char *vmlinux
,
544 symbol_filter_t filter
, int verbose
)
546 int err
, fd
= open(vmlinux
, O_RDONLY
);
551 err
= dso__load_sym(self
, fd
, vmlinux
, filter
, verbose
);
557 int dso__load_kernel(struct dso
*self
, const char *vmlinux
,
558 symbol_filter_t filter
, int verbose
)
563 err
= dso__load_vmlinux(self
, vmlinux
, filter
, verbose
);
566 err
= dso__load_kallsyms(self
, filter
, verbose
);
571 void symbol__init(void)
573 elf_version(EV_CURRENT
);
This page took 0.050164 seconds and 6 git commands to generate.