MCSymbolizer.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===-- llvm/MC/MCSymbolizer.h - MCSymbolizer class -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the declaration of the MCSymbolizer class, which is used
  11. // to symbolize instructions decoded from an object, that is, transform their
  12. // immediate operands to MCExprs.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_MC_MCSYMBOLIZER_H
  16. #define LLVM_MC_MCSYMBOLIZER_H
  17. #include "llvm/MC/MCRelocationInfo.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include "llvm/Support/DataTypes.h"
  20. #include <cassert>
  21. #include <memory>
  22. namespace llvm {
  23. class MCContext;
  24. class MCInst;
  25. class raw_ostream;
  26. /// \brief Symbolize and annotate disassembled instructions.
  27. ///
  28. /// For now this mimics the old symbolization logic (from both ARM and x86), that
  29. /// relied on user-provided (C API) callbacks to do the actual symbol lookup in
  30. /// the object file. This was moved to MCExternalSymbolizer.
  31. /// A better API would not rely on actually calling the two methods here from
  32. /// inside each disassembler, but would use the instr info to determine what
  33. /// operands are actually symbolizable, and in what way. I don't think this
  34. /// information exists right now.
  35. class MCSymbolizer {
  36. MCSymbolizer(const MCSymbolizer &) = delete;
  37. void operator=(const MCSymbolizer &) = delete;
  38. protected:
  39. MCContext &Ctx;
  40. std::unique_ptr<MCRelocationInfo> RelInfo;
  41. public:
  42. /// \brief Construct an MCSymbolizer, taking ownership of \p RelInfo.
  43. MCSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> RelInfo)
  44. : Ctx(Ctx), RelInfo(std::move(RelInfo)) {
  45. }
  46. virtual ~MCSymbolizer();
  47. /// \brief Try to add a symbolic operand instead of \p Value to the MCInst.
  48. ///
  49. /// Instead of having a difficult to read immediate, a symbolic operand would
  50. /// represent this immediate in a more understandable way, for instance as a
  51. /// symbol or an offset from a symbol. Relocations can also be used to enrich
  52. /// the symbolic expression.
  53. /// \param Inst - The MCInst where to insert the symbolic operand.
  54. /// \param cStream - Stream to print comments and annotations on.
  55. /// \param Value - Operand value, pc-adjusted by the caller if necessary.
  56. /// \param Address - Load address of the instruction.
  57. /// \param IsBranch - Is the instruction a branch?
  58. /// \param Offset - Byte offset of the operand inside the inst.
  59. /// \param InstSize - Size of the instruction in bytes.
  60. /// \return Whether a symbolic operand was added.
  61. virtual bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
  62. int64_t Value, uint64_t Address,
  63. bool IsBranch, uint64_t Offset,
  64. uint64_t InstSize) = 0;
  65. /// \brief Try to add a comment on the PC-relative load.
  66. /// For instance, in Mach-O, this is used to add annotations to instructions
  67. /// that use C string literals, as found in __cstring.
  68. virtual void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
  69. int64_t Value,
  70. uint64_t Address) = 0;
  71. };
  72. }
  73. #endif