LLVM API Documentation

MCSymbolizer.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCSymbolizer.h - MCSymbolizer class -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the MCSymbolizer class, which is used
00011 // to symbolize instructions decoded from an object, that is, transform their
00012 // immediate operands to MCExprs.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_MC_MCSYMBOLIZER_H
00017 #define LLVM_MC_MCSYMBOLIZER_H
00018 
00019 #include "llvm/MC/MCRelocationInfo.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm/Support/DataTypes.h"
00022 #include <cassert>
00023 #include <memory>
00024 
00025 namespace llvm {
00026 
00027 class MCContext;
00028 class MCInst;
00029 class raw_ostream;
00030 
00031 /// \brief Symbolize and annotate disassembled instructions.
00032 ///
00033 /// For now this mimics the old symbolization logic (from both ARM and x86), that
00034 /// relied on user-provided (C API) callbacks to do the actual symbol lookup in
00035 /// the object file. This was moved to MCExternalSymbolizer.
00036 /// A better API would not rely on actually calling the two methods here from
00037 /// inside each disassembler, but would use the instr info to determine what
00038 /// operands are actually symbolizable, and in what way. I don't think this
00039 /// information exists right now.
00040 class MCSymbolizer {
00041   MCSymbolizer(const MCSymbolizer &) LLVM_DELETED_FUNCTION;
00042   void operator=(const MCSymbolizer &) LLVM_DELETED_FUNCTION;
00043 
00044 protected:
00045   MCContext &Ctx;
00046   std::unique_ptr<MCRelocationInfo> RelInfo;
00047 
00048 public:
00049   /// \brief Construct an MCSymbolizer, taking ownership of \p RelInfo.
00050   MCSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> RelInfo)
00051     : Ctx(Ctx), RelInfo(std::move(RelInfo)) {
00052   }
00053 
00054   virtual ~MCSymbolizer();
00055 
00056   /// \brief Try to add a symbolic operand instead of \p Value to the MCInst.
00057   ///
00058   /// Instead of having a difficult to read immediate, a symbolic operand would
00059   /// represent this immediate in a more understandable way, for instance as a
00060   /// symbol or an offset from a symbol. Relocations can also be used to enrich
00061   /// the symbolic expression.
00062   /// @param Inst      - The MCInst where to insert the symbolic operand.
00063   /// @param cStream   - Stream to print comments and annotations on.
00064   /// @param Value     - Operand value, pc-adjusted by the caller if necessary.
00065   /// @param Address   - Load address of the instruction.
00066   /// @param IsBranch  - Is the instruction a branch?
00067   /// @param Offset    - Byte offset of the operand inside the inst.
00068   /// @param InstSize  - Size of the instruction in bytes.
00069   /// @return Whether a symbolic operand was added.
00070   virtual bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream,
00071                                         int64_t Value, uint64_t Address,
00072                                         bool IsBranch, uint64_t Offset,
00073                                         uint64_t InstSize) = 0;
00074 
00075   /// \brief Try to add a comment on the PC-relative load.
00076   /// For instance, in Mach-O, this is used to add annotations to instructions
00077   /// that use C string literals, as found in __cstring.
00078   virtual void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
00079                                                int64_t Value,
00080                                                uint64_t Address) = 0;
00081 };
00082 
00083 }
00084 
00085 #endif