Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 2 | #include <elf.h> |
| 3 | #include <inttypes.h> |
| 4 | #include <stdio.h> |
| 5 | |
Arnaldo Carvalho de Melo | b1d1b09 | 2019-08-30 10:26:37 -0300 | [diff] [blame] | 6 | #include "dso.h" |
Arnaldo Carvalho de Melo | d328e30 | 2019-01-22 11:10:59 -0200 | [diff] [blame] | 7 | #include "map.h" |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 8 | #include "symbol.h" |
| 9 | |
| 10 | size_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 | |
| 19 | size_t __symbol__fprintf_symname_offs(const struct symbol *sym, |
| 20 | const struct addr_location *al, |
Namhyung Kim | a876344 | 2016-11-16 15:06:27 +0900 | [diff] [blame] | 21 | bool unknown_as_addr, |
| 22 | bool print_offsets, FILE *fp) |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 23 | { |
| 24 | unsigned long offset; |
| 25 | size_t length; |
| 26 | |
Arnaldo Carvalho de Melo | a7c3899 | 2017-02-13 16:52:15 -0300 | [diff] [blame] | 27 | if (sym) { |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 28 | length = fprintf(fp, "%s", sym->name); |
Namhyung Kim | a876344 | 2016-11-16 15:06:27 +0900 | [diff] [blame] | 29 | if (al && print_offsets) { |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 30 | 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 | |
| 43 | size_t symbol__fprintf_symname_offs(const struct symbol *sym, |
| 44 | const struct addr_location *al, |
| 45 | FILE *fp) |
| 46 | { |
Namhyung Kim | a876344 | 2016-11-16 15:06:27 +0900 | [diff] [blame] | 47 | return __symbol__fprintf_symname_offs(sym, al, false, true, fp); |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | size_t __symbol__fprintf_symname(const struct symbol *sym, |
| 51 | const struct addr_location *al, |
| 52 | bool unknown_as_addr, FILE *fp) |
| 53 | { |
Namhyung Kim | a876344 | 2016-11-16 15:06:27 +0900 | [diff] [blame] | 54 | return __symbol__fprintf_symname_offs(sym, al, unknown_as_addr, false, fp); |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp) |
| 58 | { |
Namhyung Kim | a876344 | 2016-11-16 15:06:27 +0900 | [diff] [blame] | 59 | return __symbol__fprintf_symname_offs(sym, NULL, false, false, fp); |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | size_t dso__fprintf_symbols_by_name(struct dso *dso, |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 63 | FILE *fp) |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 64 | { |
| 65 | size_t ret = 0; |
| 66 | struct rb_node *nd; |
| 67 | struct symbol_name_rb_node *pos; |
| 68 | |
Davidlohr Bueso | 7137ff5 | 2018-12-06 11:18:17 -0800 | [diff] [blame] | 69 | for (nd = rb_first_cached(&dso->symbol_names); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | bfbba18 | 2016-04-14 15:54:36 -0300 | [diff] [blame] | 70 | 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 | } |