blob: 35c936ce33efa6ea202cd7374d86a7eaeaf5f818 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -03002#include <elf.h>
3#include <inttypes.h>
4#include <stdio.h>
5
Arnaldo Carvalho de Melob1d1b092019-08-30 10:26:37 -03006#include "dso.h"
Arnaldo Carvalho de Melod328e302019-01-22 11:10:59 -02007#include "map.h"
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -03008#include "symbol.h"
9
10size_t symbol__fprintf(struct symbol *sym, FILE *fp)
11{
12 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
13 sym->start, sym->end,
14 sym->binding == STB_GLOBAL ? 'g' :
15 sym->binding == STB_LOCAL ? 'l' : 'w',
16 sym->name);
17}
18
19size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
20 const struct addr_location *al,
Namhyung Kima8763442016-11-16 15:06:27 +090021 bool unknown_as_addr,
22 bool print_offsets, FILE *fp)
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030023{
24 unsigned long offset;
25 size_t length;
26
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -030027 if (sym) {
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030028 length = fprintf(fp, "%s", sym->name);
Namhyung Kima8763442016-11-16 15:06:27 +090029 if (al && print_offsets) {
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030030 if (al->addr < sym->end)
31 offset = al->addr - sym->start;
32 else
33 offset = al->addr - al->map->start - sym->start;
34 length += fprintf(fp, "+0x%lx", offset);
35 }
36 return length;
37 } else if (al && unknown_as_addr)
38 return fprintf(fp, "[%#" PRIx64 "]", al->addr);
39 else
40 return fprintf(fp, "[unknown]");
41}
42
43size_t symbol__fprintf_symname_offs(const struct symbol *sym,
44 const struct addr_location *al,
45 FILE *fp)
46{
Namhyung Kima8763442016-11-16 15:06:27 +090047 return __symbol__fprintf_symname_offs(sym, al, false, true, fp);
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030048}
49
50size_t __symbol__fprintf_symname(const struct symbol *sym,
51 const struct addr_location *al,
52 bool unknown_as_addr, FILE *fp)
53{
Namhyung Kima8763442016-11-16 15:06:27 +090054 return __symbol__fprintf_symname_offs(sym, al, unknown_as_addr, false, fp);
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030055}
56
57size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
58{
Namhyung Kima8763442016-11-16 15:06:27 +090059 return __symbol__fprintf_symname_offs(sym, NULL, false, false, fp);
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030060}
61
62size_t dso__fprintf_symbols_by_name(struct dso *dso,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030063 FILE *fp)
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030064{
65 size_t ret = 0;
66 struct rb_node *nd;
67 struct symbol_name_rb_node *pos;
68
Davidlohr Bueso7137ff52018-12-06 11:18:17 -080069 for (nd = rb_first_cached(&dso->symbol_names); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -030070 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
71 fprintf(fp, "%s\n", pos->sym.name);
72 }
73
74 return ret;
75}