LLVM  8.0.1
MCExternalSymbolizer.cpp
Go to the documentation of this file.
1 //===-- MCExternalSymbolizer.cpp - External symbolizer --------------------===//
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 
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCExpr.h"
13 #include "llvm/MC/MCInst.h"
15 #include <cstring>
16 
17 using namespace llvm;
18 
19 namespace llvm {
20 class Triple;
21 }
22 
23 // This function tries to add a symbolic operand in place of the immediate
24 // Value in the MCInst. The immediate Value has had any PC adjustment made by
25 // the caller. If the instruction is a branch instruction then IsBranch is true,
26 // else false. If the getOpInfo() function was set as part of the
27 // setupForSymbolicDisassembly() call then that function is called to get any
28 // symbolic information at the Address for this instruction. If that returns
29 // non-zero then the symbolic information it returns is used to create an MCExpr
30 // and that is added as an operand to the MCInst. If getOpInfo() returns zero
31 // and IsBranch is true then a symbol look up for Value is done and if a symbol
32 // is found an MCExpr is created with that, else an MCExpr with Value is
33 // created. This function returns true if it adds an operand to the MCInst and
34 // false otherwise.
36  raw_ostream &cStream,
37  int64_t Value,
38  uint64_t Address,
39  bool IsBranch,
40  uint64_t Offset,
41  uint64_t InstSize) {
42  struct LLVMOpInfo1 SymbolicOp;
43  std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
44  SymbolicOp.Value = Value;
45 
46  if (!GetOpInfo ||
47  !GetOpInfo(DisInfo, Address, Offset, InstSize, 1, &SymbolicOp)) {
48  // Clear SymbolicOp.Value from above and also all other fields.
49  std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
50 
51  // At this point, GetOpInfo() did not find any relocation information about
52  // this operand and we are left to use the SymbolLookUp() call back to guess
53  // if the Value is the address of a symbol. In the case this is a branch
54  // that always makes sense to guess. But in the case of an immediate it is
55  // a bit more questionable if it is an address of a symbol or some other
56  // reference. So if the immediate Value comes from a width of 1 byte,
57  // InstSize, we will not guess it is an address of a symbol. Because in
58  // object files assembled starting at address 0 this usually leads to
59  // incorrect symbolication.
60  if (!SymbolLookUp || (InstSize == 1 && !IsBranch))
61  return false;
62 
63  uint64_t ReferenceType;
64  if (IsBranch)
66  else
68  const char *ReferenceName;
69  const char *Name = SymbolLookUp(DisInfo, Value, &ReferenceType, Address,
70  &ReferenceName);
71  if (Name) {
72  SymbolicOp.AddSymbol.Name = Name;
73  SymbolicOp.AddSymbol.Present = true;
74  // If Name is a C++ symbol name put the human readable name in a comment.
76  cStream << ReferenceName;
77  }
78  // For branches always create an MCExpr so it gets printed as hex address.
79  else if (IsBranch) {
80  SymbolicOp.Value = Value;
81  }
83  cStream << "symbol stub for: " << ReferenceName;
84  else if(ReferenceType == LLVMDisassembler_ReferenceType_Out_Objc_Message)
85  cStream << "Objc message: " << ReferenceName;
86  if (!Name && !IsBranch)
87  return false;
88  }
89 
90  const MCExpr *Add = nullptr;
91  if (SymbolicOp.AddSymbol.Present) {
92  if (SymbolicOp.AddSymbol.Name) {
93  StringRef Name(SymbolicOp.AddSymbol.Name);
94  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
95  Add = MCSymbolRefExpr::create(Sym, Ctx);
96  } else {
97  Add = MCConstantExpr::create((int)SymbolicOp.AddSymbol.Value, Ctx);
98  }
99  }
100 
101  const MCExpr *Sub = nullptr;
102  if (SymbolicOp.SubtractSymbol.Present) {
103  if (SymbolicOp.SubtractSymbol.Name) {
104  StringRef Name(SymbolicOp.SubtractSymbol.Name);
105  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
106  Sub = MCSymbolRefExpr::create(Sym, Ctx);
107  } else {
108  Sub = MCConstantExpr::create((int)SymbolicOp.SubtractSymbol.Value, Ctx);
109  }
110  }
111 
112  const MCExpr *Off = nullptr;
113  if (SymbolicOp.Value != 0)
114  Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
115 
116  const MCExpr *Expr;
117  if (Sub) {
118  const MCExpr *LHS;
119  if (Add)
120  LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
121  else
122  LHS = MCUnaryExpr::createMinus(Sub, Ctx);
123  if (Off)
124  Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
125  else
126  Expr = LHS;
127  } else if (Add) {
128  if (Off)
129  Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
130  else
131  Expr = Add;
132  } else {
133  if (Off)
134  Expr = Off;
135  else
136  Expr = MCConstantExpr::create(0, Ctx);
137  }
138 
139  Expr = RelInfo->createExprForCAPIVariantKind(Expr, SymbolicOp.VariantKind);
140  if (!Expr)
141  return false;
142 
144  return true;
145 }
146 
147 // This function tries to add a comment as to what is being referenced by a load
148 // instruction with the base register that is the Pc. These can often be values
149 // in a literal pool near the Address of the instruction. The Address of the
150 // instruction and its immediate Value are used as a possible literal pool entry.
151 // The SymbolLookUp call back will return the name of a symbol referenced by the
152 // literal pool's entry if the referenced address is that of a symbol. Or it
153 // will return a pointer to a literal 'C' string if the referenced address of
154 // the literal pool's entry is an address into a section with C string literals.
155 // Or if the reference is to an Objective-C data structure it will return a
156 // specific reference type for it and a string.
158  int64_t Value,
159  uint64_t Address) {
160  if (SymbolLookUp) {
161  uint64_t ReferenceType = LLVMDisassembler_ReferenceType_In_PCrel_Load;
162  const char *ReferenceName;
163  (void)SymbolLookUp(DisInfo, Value, &ReferenceType, Address, &ReferenceName);
165  cStream << "literal pool symbol address: " << ReferenceName;
166  else if(ReferenceType ==
168  cStream << "literal pool for: \"";
169  cStream.write_escaped(ReferenceName);
170  cStream << "\"";
171  }
172  else if(ReferenceType ==
174  cStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
175  else if(ReferenceType ==
177  cStream << "Objc message: " << ReferenceName;
178  else if(ReferenceType ==
180  cStream << "Objc message ref: " << ReferenceName;
181  else if(ReferenceType ==
183  cStream << "Objc selector ref: " << ReferenceName;
184  else if(ReferenceType ==
186  cStream << "Objc class ref: " << ReferenceName;
187  }
188 }
189 
190 namespace llvm {
193  void *DisInfo, MCContext *Ctx,
194  std::unique_ptr<MCRelocationInfo> &&RelInfo) {
195  assert(Ctx && "No MCContext given for symbolic disassembly");
196 
197  return new MCExternalSymbolizer(*Ctx, std::move(RelInfo), GetOpInfo,
198  SymbolLookUp, DisInfo);
199 }
200 }
std::unique_ptr< MCRelocationInfo > RelInfo
Definition: MCSymbolizer.h:42
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:323
#define LLVMDisassembler_ReferenceType_Out_Objc_Message
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
MCContext & Ctx
Definition: MCSymbolizer.h:41
#define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
MCSymbolizer * createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo)
uint64_t VariantKind
void * DisInfo
The pointer to the block of symbolic information for above call back.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
#define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref
#define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBuf)
The type for the operand information call back function.
Context object for machine code objects.
Definition: MCContext.h:63
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:546
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:461
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
MCExternalSymbolizer(MCContext &Ctx, std::unique_ptr< MCRelocationInfo > RelInfo, LLVMOpInfoCallback getOpInfo, LLVMSymbolLookupCallback symbolLookUp, void *disInfo)
#define LLVMDisassembler_ReferenceType_In_PCrel_Load
Symbolize and annotate disassembled instructions.
Definition: MCSymbolizer.h:39
#define LLVMDisassembler_ReferenceType_Out_SymbolStub
#define LLVMDisassembler_ReferenceType_InOut_None
The reference types on input and output.
struct LLVMOpInfoSymbol1 AddSymbol
struct LLVMOpInfoSymbol1 SubtractSymbol
#define LLVMDisassembler_ReferenceType_In_Branch
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static const MCUnaryExpr * createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:387
#define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning &#39;\&#39;, &#39;&#39;, &#39; &#39;, &#39;"&#39;, and anything that doesn&#39;t satisfy llvm::isPrint into an escape...
void tryAddingPcLoadReferenceComment(raw_ostream &CommentStream, int64_t Value, uint64_t Address) override
Try to add a comment on the PC-relative load.
bool tryAddingSymbolicOperand(MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize) override
Try to add a symbolic operand instead of Value to the MCInst.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:123
#define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
#define LLVMDisassembler_ReferenceType_DeMangled_Name
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:186
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
The type for the symbol lookup function.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
LLVMSymbolLookupCallback SymbolLookUp
The function to lookup a symbol name.