MCSymbolizer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCSymbolizer.h - MCSymbolizer class --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains the declaration of the MCSymbolizer class, which is used
  15. // to symbolize instructions decoded from an object, that is, transform their
  16. // immediate operands to MCExprs.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_MC_MCDISASSEMBLER_MCSYMBOLIZER_H
  20. #define LLVM_MC_MCDISASSEMBLER_MCSYMBOLIZER_H
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
  23. #include <algorithm>
  24. #include <cstdint>
  25. #include <memory>
  26. namespace llvm {
  27. class MCContext;
  28. class MCInst;
  29. class raw_ostream;
  30. /// Symbolize and annotate disassembled instructions.
  31. ///
  32. /// For now this mimics the old symbolization logic (from both ARM and x86), that
  33. /// relied on user-provided (C API) callbacks to do the actual symbol lookup in
  34. /// the object file. This was moved to MCExternalSymbolizer.
  35. /// A better API would not rely on actually calling the two methods here from
  36. /// inside each disassembler, but would use the instr info to determine what
  37. /// operands are actually symbolizable, and in what way. I don't think this
  38. /// information exists right now.
  39. class MCSymbolizer {
  40. protected:
  41. MCContext &Ctx;
  42. std::unique_ptr<MCRelocationInfo> RelInfo;
  43. public:
  44. /// Construct an MCSymbolizer, taking ownership of \p RelInfo.
  45. MCSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> RelInfo)
  46. : Ctx(Ctx), RelInfo(std::move(RelInfo)) {
  47. }
  48. MCSymbolizer(const MCSymbolizer &) = delete;
  49. MCSymbolizer &operator=(const MCSymbolizer &) = delete;
  50. virtual ~MCSymbolizer();
  51. /// Try to add a symbolic operand instead of \p Value to the MCInst.
  52. ///
  53. /// Instead of having a difficult to read immediate, a symbolic operand would
  54. /// represent this immediate in a more understandable way, for instance as a
  55. /// symbol or an offset from a symbol. Relocations can also be used to enrich
  56. /// the symbolic expression.
  57. /// \param Inst - The MCInst where to insert the symbolic operand.
  58. /// \param cStream - Stream to print comments and annotations on.
  59. /// \param Value - Operand value, pc-adjusted by the caller if necessary.
  60. /// \param Address - Load address of the instruction.
  61. /// \param IsBranch - Is the instruction a branch?
  62. /// \param Offset - Byte offset of the operand inside the inst.
  63. /// \param InstSize - Size of the instruction in bytes.
  64. /// \return Whether a symbolic operand was added.
  65. virtual bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
  66. int64_t Value, uint64_t Address,
  67. bool IsBranch, uint64_t Offset,
  68. uint64_t InstSize) = 0;
  69. /// Try to add a comment on the PC-relative load.
  70. /// For instance, in Mach-O, this is used to add annotations to instructions
  71. /// that use C string literals, as found in __cstring.
  72. virtual void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
  73. int64_t Value,
  74. uint64_t Address) = 0;
  75. /// Get the MCSymbolizer's list of addresses that were referenced by
  76. /// symbolizable operands but not resolved to a symbol. The caller (some
  77. /// code that is disassembling a section or other chunk of code) would
  78. /// typically create a synthetic label at each address and add them to its
  79. /// list of symbols in the section, before creating a new MCSymbolizer with
  80. /// the enhanced symbol list and retrying disassembling the section.
  81. /// The returned array is unordered and may have duplicates.
  82. /// The returned ArrayRef stops being valid on any call to or destruction of
  83. /// the MCSymbolizer object.
  84. virtual ArrayRef<uint64_t> getReferencedAddresses() const { return {}; }
  85. };
  86. } // end namespace llvm
  87. #endif // LLVM_MC_MCDISASSEMBLER_MCSYMBOLIZER_H
  88. #ifdef __GNUC__
  89. #pragma GCC diagnostic pop
  90. #endif