LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCExternalSymbolizer.cpp
Go to the documentation of this file.
1 //===-- lib/MC/MCExternalSymbolizer.cpp - External symbolizer ---*- 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 
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 // This function tries to add a symbolic operand in place of the immediate
20 // Value in the MCInst. The immediate Value has had any PC adjustment made by
21 // the caller. If the instruction is a branch instruction then IsBranch is true,
22 // else false. If the getOpInfo() function was set as part of the
23 // setupForSymbolicDisassembly() call then that function is called to get any
24 // symbolic information at the Address for this instruction. If that returns
25 // non-zero then the symbolic information it returns is used to create an MCExpr
26 // and that is added as an operand to the MCInst. If getOpInfo() returns zero
27 // and IsBranch is true then a symbol look up for Value is done and if a symbol
28 // is found an MCExpr is created with that, else an MCExpr with Value is
29 // created. This function returns true if it adds an operand to the MCInst and
30 // false otherwise.
32  raw_ostream &cStream,
33  int64_t Value,
34  uint64_t Address,
35  bool IsBranch,
36  uint64_t Offset,
37  uint64_t InstSize) {
38  struct LLVMOpInfo1 SymbolicOp;
39  std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
40  SymbolicOp.Value = Value;
41 
42  if (!GetOpInfo ||
43  !GetOpInfo(DisInfo, Address, Offset, InstSize, 1, &SymbolicOp)) {
44  // Clear SymbolicOp.Value from above and also all other fields.
45  std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
46  if (!SymbolLookUp)
47  return false;
48  uint64_t ReferenceType;
49  if (IsBranch)
51  else
53  const char *ReferenceName;
54  const char *Name = SymbolLookUp(DisInfo, Value, &ReferenceType, Address,
55  &ReferenceName);
56  if (Name) {
57  SymbolicOp.AddSymbol.Name = Name;
58  SymbolicOp.AddSymbol.Present = true;
59  }
60  // For branches always create an MCExpr so it gets printed as hex address.
61  else if (IsBranch) {
62  SymbolicOp.Value = Value;
63  }
65  cStream << "symbol stub for: " << ReferenceName;
66  else if(ReferenceType == LLVMDisassembler_ReferenceType_Out_Objc_Message)
67  cStream << "Objc message: " << ReferenceName;
68  if (!Name && !IsBranch)
69  return false;
70  }
71 
72  const MCExpr *Add = NULL;
73  if (SymbolicOp.AddSymbol.Present) {
74  if (SymbolicOp.AddSymbol.Name) {
75  StringRef Name(SymbolicOp.AddSymbol.Name);
76  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
77  Add = MCSymbolRefExpr::Create(Sym, Ctx);
78  } else {
79  Add = MCConstantExpr::Create((int)SymbolicOp.AddSymbol.Value, Ctx);
80  }
81  }
82 
83  const MCExpr *Sub = NULL;
84  if (SymbolicOp.SubtractSymbol.Present) {
85  if (SymbolicOp.SubtractSymbol.Name) {
86  StringRef Name(SymbolicOp.SubtractSymbol.Name);
87  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
88  Sub = MCSymbolRefExpr::Create(Sym, Ctx);
89  } else {
90  Sub = MCConstantExpr::Create((int)SymbolicOp.SubtractSymbol.Value, Ctx);
91  }
92  }
93 
94  const MCExpr *Off = NULL;
95  if (SymbolicOp.Value != 0)
96  Off = MCConstantExpr::Create(SymbolicOp.Value, Ctx);
97 
98  const MCExpr *Expr;
99  if (Sub) {
100  const MCExpr *LHS;
101  if (Add)
102  LHS = MCBinaryExpr::CreateSub(Add, Sub, Ctx);
103  else
104  LHS = MCUnaryExpr::CreateMinus(Sub, Ctx);
105  if (Off != 0)
106  Expr = MCBinaryExpr::CreateAdd(LHS, Off, Ctx);
107  else
108  Expr = LHS;
109  } else if (Add) {
110  if (Off != 0)
111  Expr = MCBinaryExpr::CreateAdd(Add, Off, Ctx);
112  else
113  Expr = Add;
114  } else {
115  if (Off != 0)
116  Expr = Off;
117  else
118  Expr = MCConstantExpr::Create(0, Ctx);
119  }
120 
121  Expr = RelInfo->createExprForCAPIVariantKind(Expr, SymbolicOp.VariantKind);
122  if (!Expr)
123  return false;
124 
126  return true;
127 }
128 
129 // This function tries to add a comment as to what is being referenced by a load
130 // instruction with the base register that is the Pc. These can often be values
131 // in a literal pool near the Address of the instruction. The Address of the
132 // instruction and its immediate Value are used as a possible literal pool entry.
133 // The SymbolLookUp call back will return the name of a symbol referenced by the
134 // literal pool's entry if the referenced address is that of a symbol. Or it
135 // will return a pointer to a literal 'C' string if the referenced address of
136 // the literal pool's entry is an address into a section with C string literals.
137 // Or if the reference is to an Objective-C data structure it will return a
138 // specific reference type for it and a string.
140  int64_t Value,
141  uint64_t Address) {
142  if (SymbolLookUp) {
143  uint64_t ReferenceType = LLVMDisassembler_ReferenceType_In_PCrel_Load;
144  const char *ReferenceName;
145  (void)SymbolLookUp(DisInfo, Value, &ReferenceType, Address, &ReferenceName);
147  cStream << "literal pool symbol address: " << ReferenceName;
148  else if(ReferenceType ==
150  cStream << "literal pool for: \"" << ReferenceName << "\"";
151  else if(ReferenceType ==
153  cStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
154  else if(ReferenceType ==
156  cStream << "Objc message: " << ReferenceName;
157  else if(ReferenceType ==
159  cStream << "Objc message ref: " << ReferenceName;
160  else if(ReferenceType ==
162  cStream << "Objc selector ref: " << ReferenceName;
163  else if(ReferenceType ==
165  cStream << "Objc class ref: " << ReferenceName;
166  }
167 }
168 
169 namespace llvm {
171  LLVMSymbolLookupCallback SymbolLookUp,
172  void *DisInfo,
173  MCContext *Ctx,
174  MCRelocationInfo *RelInfo) {
175  assert(Ctx != 0 && "No MCContext given for symbolic disassembly");
176 
177  OwningPtr<MCRelocationInfo> RelInfoOwingPtr(RelInfo);
178  return new MCExternalSymbolizer(*Ctx, RelInfoOwingPtr, GetOpInfo,
179  SymbolLookUp, DisInfo);
180 }
181 }
static const MCConstantExpr * Create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:152
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
#define LLVMDisassembler_ReferenceType_In_Branch
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
Symbolize using user-provided, C API, callbacks.
MCSymbolizer * createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, MCRelocationInfo *RelInfo)
MCContext & Ctx
Definition: MCSymbolizer.h:44
#define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref
#define LLVMDisassembler_ReferenceType_Out_SymbolStub
MCSymbol * GetOrCreateSymbol(StringRef Name)
Definition: MCContext.cpp:118
#define LLVMDisassembler_ReferenceType_InOut_None
static const MCBinaryExpr * CreateSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:460
bool tryAddingSymbolicOperand(MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize)
Try to add a symbolic operand instead of Value to the MCInst.
#define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref
static const MCSymbolRefExpr * Create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:270
Symbolize and annotate disassembled instructions.
Definition: MCSymbolizer.h:39
OwningPtr< MCRelocationInfo > RelInfo
Definition: MCSymbolizer.h:45
#define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr
Create MCExprs from relocations found in an object file.
struct LLVMOpInfoSymbol1 AddSymbol
struct LLVMOpInfoSymbol1 SubtractSymbol
#define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
#define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr
void tryAddingPcLoadReferenceComment(raw_ostream &CommentStream, int64_t Value, uint64_t Address)
Try to add a comment on the PC-relative load. For instance, in Mach-O, this is used to add annotation...
#define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref
static const MCBinaryExpr * CreateAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:396
static const MCUnaryExpr * CreateMinus(const MCExpr *Expr, MCContext &Ctx)
Definition: MCExpr.h:328
#define LLVMDisassembler_ReferenceType_In_PCrel_Load
LLVM Value Representation.
Definition: Value.h:66
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBuf)
#define LLVMDisassembler_ReferenceType_Out_Objc_Message