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