LLVM 20.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"
15#include <cstring>
16
17using namespace llvm;
18
19namespace llvm {
20class 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 MCInst &MI, raw_ostream &cStream, int64_t Value, uint64_t Address,
37 bool IsBranch, uint64_t Offset, uint64_t OpSize, 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, OpSize, InstSize, 1, &SymbolicOp)) {
44 // Clear SymbolicOp.Value from above and also all other fields.
45 std::memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
46
47 // At this point, GetOpInfo() did not find any relocation information about
48 // this operand and we are left to use the SymbolLookUp() call back to guess
49 // if the Value is the address of a symbol. In the case this is a branch
50 // that always makes sense to guess. But in the case of an immediate it is
51 // a bit more questionable if it is an address of a symbol or some other
52 // reference. So if the immediate Value comes from a width of 1 byte,
53 // OpSize, we will not guess it is an address of a symbol. Because in
54 // object files assembled starting at address 0 this usually leads to
55 // incorrect symbolication.
56 if (!SymbolLookUp || (OpSize == 1 && !IsBranch))
57 return false;
58
60 if (IsBranch)
62 else
64 const char *ReferenceName;
66 &ReferenceName);
67 if (Name) {
68 SymbolicOp.AddSymbol.Name = Name;
69 SymbolicOp.AddSymbol.Present = true;
70 // If Name is a C++ symbol name put the human readable name in a comment.
72 cStream << ReferenceName;
73 }
74 // For branches always create an MCExpr so it gets printed as hex address.
75 else if (IsBranch) {
76 SymbolicOp.Value = Value;
77 }
79 cStream << "symbol stub for: " << ReferenceName;
81 cStream << "Objc message: " << ReferenceName;
82 if (!Name && !IsBranch)
83 return false;
84 }
85
86 const MCExpr *Add = nullptr;
87 if (SymbolicOp.AddSymbol.Present) {
88 if (SymbolicOp.AddSymbol.Name) {
89 StringRef Name(SymbolicOp.AddSymbol.Name);
92 } else {
93 Add = MCConstantExpr::create((int)SymbolicOp.AddSymbol.Value, Ctx);
94 }
95 }
96
97 const MCExpr *Sub = nullptr;
98 if (SymbolicOp.SubtractSymbol.Present) {
99 if (SymbolicOp.SubtractSymbol.Name) {
100 StringRef Name(SymbolicOp.SubtractSymbol.Name);
103 } else {
104 Sub = MCConstantExpr::create((int)SymbolicOp.SubtractSymbol.Value, Ctx);
105 }
106 }
107
108 const MCExpr *Off = nullptr;
109 if (SymbolicOp.Value != 0)
110 Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
111
112 const MCExpr *Expr;
113 if (Sub) {
114 const MCExpr *LHS;
115 if (Add)
117 else
119 if (Off)
120 Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
121 else
122 Expr = LHS;
123 } else if (Add) {
124 if (Off)
125 Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
126 else
127 Expr = Add;
128 } else {
129 if (Off)
130 Expr = Off;
131 else
132 Expr = MCConstantExpr::create(0, Ctx);
133 }
134
135 Expr = RelInfo->createExprForCAPIVariantKind(Expr, SymbolicOp.VariantKind);
136 if (!Expr)
137 return false;
138
139 MI.addOperand(MCOperand::createExpr(Expr));
140 return true;
141}
142
143// This function tries to add a comment as to what is being referenced by a load
144// instruction with the base register that is the Pc. These can often be values
145// in a literal pool near the Address of the instruction. The Address of the
146// instruction and its immediate Value are used as a possible literal pool entry.
147// The SymbolLookUp call back will return the name of a symbol referenced by the
148// literal pool's entry if the referenced address is that of a symbol. Or it
149// will return a pointer to a literal 'C' string if the referenced address of
150// the literal pool's entry is an address into a section with C string literals.
151// Or if the reference is to an Objective-C data structure it will return a
152// specific reference type for it and a string.
154 int64_t Value,
156 if (SymbolLookUp) {
158 const char *ReferenceName;
159 (void)SymbolLookUp(DisInfo, Value, &ReferenceType, Address, &ReferenceName);
161 cStream << "literal pool symbol address: " << ReferenceName;
162 else if(ReferenceType ==
164 cStream << "literal pool for: \"";
165 cStream.write_escaped(ReferenceName);
166 cStream << "\"";
167 }
168 else if(ReferenceType ==
170 cStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
171 else if(ReferenceType ==
173 cStream << "Objc message: " << ReferenceName;
174 else if(ReferenceType ==
176 cStream << "Objc message ref: " << ReferenceName;
177 else if(ReferenceType ==
179 cStream << "Objc selector ref: " << ReferenceName;
180 else if(ReferenceType ==
182 cStream << "Objc class ref: " << ReferenceName;
183 }
184}
185
186namespace llvm {
188 LLVMSymbolLookupCallback SymbolLookUp,
189 void *DisInfo, MCContext *Ctx,
190 std::unique_ptr<MCRelocationInfo> &&RelInfo) {
191 assert(Ctx && "No MCContext given for symbolic disassembly");
192
193 return new MCExternalSymbolizer(*Ctx, std::move(RelInfo), GetOpInfo,
194 SymbolLookUp, DisInfo);
195}
196}
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * LHS
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:617
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Symbolize using user-provided, C API, callbacks.
void * DisInfo
The pointer to the block of symbolic information for above call back.
LLVMSymbolLookupCallback SymbolLookUp
The function to lookup a symbol name.
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 OpSize, uint64_t InstSize) override
Try to add a symbolic operand instead of Value to the MCInst.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Symbolize and annotate disassembled instructions.
Definition: MCSymbolizer.h:39
std::unique_ptr< MCRelocationInfo > RelInfo
Definition: MCSymbolizer.h:42
MCContext & Ctx
Definition: MCSymbolizer.h:41
static const MCUnaryExpr * createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:458
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
The type for the symbol lookup function.
#define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref
#define LLVMDisassembler_ReferenceType_Out_SymbolStub
#define LLVMDisassembler_ReferenceType_In_PCrel_Load
#define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref
#define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr
#define LLVMDisassembler_ReferenceType_In_Branch
#define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr
#define LLVMDisassembler_ReferenceType_Out_Objc_Message
#define LLVMDisassembler_ReferenceType_InOut_None
The reference types on input and output.
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t OpSize, uint64_t InstSize, int TagType, void *TagBuf)
The type for the operand information call back function.
#define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref
#define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref
#define LLVMDisassembler_ReferenceType_DeMangled_Name
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MCSymbolizer * createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo)
@ Add
Sum of integers.
struct LLVMOpInfoSymbol1 SubtractSymbol
struct LLVMOpInfoSymbol1 AddSymbol
uint64_t VariantKind