symbol_fprintf.c 1.8 KB

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