symbol_fprintf.c 1.8 KB

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