symbol_fprintf.c 1.9 KB

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