0001
0002 #include <elf.h>
0003 #include <inttypes.h>
0004 #include <stdio.h>
0005
0006 #include "dso.h"
0007 #include "map.h"
0008 #include "symbol.h"
0009
0010 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
0011 {
0012 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
0013 sym->start, sym->end,
0014 sym->binding == STB_GLOBAL ? 'g' :
0015 sym->binding == STB_LOCAL ? 'l' : 'w',
0016 sym->name);
0017 }
0018
0019 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
0020 const struct addr_location *al,
0021 bool unknown_as_addr,
0022 bool print_offsets, FILE *fp)
0023 {
0024 unsigned long offset;
0025 size_t length;
0026
0027 if (sym) {
0028 length = fprintf(fp, "%s", sym->name);
0029 if (al && print_offsets) {
0030 if (al->addr < sym->end)
0031 offset = al->addr - sym->start;
0032 else
0033 offset = al->addr - al->map->start - sym->start;
0034 length += fprintf(fp, "+0x%lx", offset);
0035 }
0036 return length;
0037 } else if (al && unknown_as_addr)
0038 return fprintf(fp, "[%#" PRIx64 "]", al->addr);
0039 else
0040 return fprintf(fp, "[unknown]");
0041 }
0042
0043 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
0044 const struct addr_location *al,
0045 FILE *fp)
0046 {
0047 return __symbol__fprintf_symname_offs(sym, al, false, true, fp);
0048 }
0049
0050 size_t __symbol__fprintf_symname(const struct symbol *sym,
0051 const struct addr_location *al,
0052 bool unknown_as_addr, FILE *fp)
0053 {
0054 return __symbol__fprintf_symname_offs(sym, al, unknown_as_addr, false, fp);
0055 }
0056
0057 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
0058 {
0059 return __symbol__fprintf_symname_offs(sym, NULL, false, false, fp);
0060 }
0061
0062 size_t dso__fprintf_symbols_by_name(struct dso *dso,
0063 FILE *fp)
0064 {
0065 size_t ret = 0;
0066 struct rb_node *nd;
0067 struct symbol_name_rb_node *pos;
0068
0069 for (nd = rb_first_cached(&dso->symbol_names); nd; nd = rb_next(nd)) {
0070 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
0071 ret += fprintf(fp, "%s\n", pos->sym.name);
0072 }
0073
0074 return ret;
0075 }