1 // SPDX-License-Identifier: GPL-2.0 2 #include <elf.h> 3 #include <inttypes.h> 4 #include <stdio.h> 5 6 #include "map.h" 7 #include "symbol.h" 8 9 size_t symbol__fprintf(struct symbol *sym, FILE *fp) 10 { 11 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n", 12 sym->start, sym->end, 13 sym->binding == STB_GLOBAL ? 'g' : 14 sym->binding == STB_LOCAL ? 'l' : 'w', 15 sym->name); 16 } 17 18 size_t __symbol__fprintf_symname_offs(const struct symbol *sym, 19 const struct addr_location *al, 20 bool unknown_as_addr, 21 bool print_offsets, FILE *fp) 22 { 23 unsigned long offset; 24 size_t length; 25 26 if (sym) { 27 length = fprintf(fp, "%s", sym->name); 28 if (al && print_offsets) { 29 if (al->addr < sym->end) 30 offset = al->addr - sym->start; 31 else 32 offset = al->addr - al->map->start - sym->start; 33 length += fprintf(fp, "+0x%lx", offset); 34 } 35 return length; 36 } else if (al && unknown_as_addr) 37 return fprintf(fp, "[%#" PRIx64 "]", al->addr); 38 else 39 return fprintf(fp, "[unknown]"); 40 } 41 42 size_t symbol__fprintf_symname_offs(const struct symbol *sym, 43 const struct addr_location *al, 44 FILE *fp) 45 { 46 return __symbol__fprintf_symname_offs(sym, al, false, true, fp); 47 } 48 49 size_t __symbol__fprintf_symname(const struct symbol *sym, 50 const struct addr_location *al, 51 bool unknown_as_addr, FILE *fp) 52 { 53 return __symbol__fprintf_symname_offs(sym, al, unknown_as_addr, false, fp); 54 } 55 56 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp) 57 { 58 return __symbol__fprintf_symname_offs(sym, NULL, false, false, fp); 59 } 60 61 size_t dso__fprintf_symbols_by_name(struct dso *dso, 62 FILE *fp) 63 { 64 size_t ret = 0; 65 struct rb_node *nd; 66 struct symbol_name_rb_node *pos; 67 68 for (nd = rb_first_cached(&dso->symbol_names); nd; nd = rb_next(nd)) { 69 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); 70 fprintf(fp, "%s\n", pos->sym.name); 71 } 72 73 return ret; 74 } 75