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