blob: 0bfa569474ec59e40a7e586eef4a862862cbed88 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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 <algorithm>
21#include <cstdint>
22#include <memory>
23
24namespace llvm {
25
26class MCContext;
27class MCInst;
28class raw_ostream;
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// Symbolize and annotate disassembled instructions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031///
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.
39class MCSymbolizer {
40protected:
41 MCContext &Ctx;
42 std::unique_ptr<MCRelocationInfo> RelInfo;
43
44public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010045 /// Construct an MCSymbolizer, taking ownership of \p RelInfo.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 MCSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> RelInfo)
47 : Ctx(Ctx), RelInfo(std::move(RelInfo)) {
48 }
49
50 MCSymbolizer(const MCSymbolizer &) = delete;
51 MCSymbolizer &operator=(const MCSymbolizer &) = delete;
52 virtual ~MCSymbolizer();
53
Andrew Scullcdfcccc2018-10-05 20:58:37 +010054 /// Try to add a symbolic operand instead of \p Value to the MCInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055 ///
56 /// Instead of having a difficult to read immediate, a symbolic operand would
57 /// represent this immediate in a more understandable way, for instance as a
58 /// symbol or an offset from a symbol. Relocations can also be used to enrich
59 /// the symbolic expression.
60 /// \param Inst - The MCInst where to insert the symbolic operand.
61 /// \param cStream - Stream to print comments and annotations on.
62 /// \param Value - Operand value, pc-adjusted by the caller if necessary.
63 /// \param Address - Load address of the instruction.
64 /// \param IsBranch - Is the instruction a branch?
65 /// \param Offset - Byte offset of the operand inside the inst.
66 /// \param InstSize - Size of the instruction in bytes.
67 /// \return Whether a symbolic operand was added.
68 virtual bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
69 int64_t Value, uint64_t Address,
70 bool IsBranch, uint64_t Offset,
71 uint64_t InstSize) = 0;
72
Andrew Scullcdfcccc2018-10-05 20:58:37 +010073 /// Try to add a comment on the PC-relative load.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 /// For instance, in Mach-O, this is used to add annotations to instructions
75 /// that use C string literals, as found in __cstring.
76 virtual void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
77 int64_t Value,
78 uint64_t Address) = 0;
79};
80
81} // end namespace llvm
82
83#endif // LLVM_MC_MCDISASSEMBLER_MCSYMBOLIZER_H