1 //===- ARM64ExternalSymbolizer.cpp - Symbolizer for ARM64 -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ARM64ExternalSymbolizer.h"
11 #include "ARM64Subtarget.h"
12 #include "MCTargetDesc/ARM64AddressingModes.h"
13 #include "Utils/ARM64BaseInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
22 #define DEBUG_TYPE "arm64-disassembler"
24 static MCSymbolRefExpr::VariantKind
25 getVariant(uint64_t LLVMDisassembler_VariantKind) {
26 switch (LLVMDisassembler_VariantKind) {
27 case LLVMDisassembler_VariantKind_None:
28 return MCSymbolRefExpr::VK_None;
29 case LLVMDisassembler_VariantKind_ARM64_PAGE:
30 return MCSymbolRefExpr::VK_PAGE;
31 case LLVMDisassembler_VariantKind_ARM64_PAGEOFF:
32 return MCSymbolRefExpr::VK_PAGEOFF;
33 case LLVMDisassembler_VariantKind_ARM64_GOTPAGE:
34 return MCSymbolRefExpr::VK_GOTPAGE;
35 case LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF:
36 return MCSymbolRefExpr::VK_GOTPAGEOFF;
37 case LLVMDisassembler_VariantKind_ARM64_TLVP:
38 case LLVMDisassembler_VariantKind_ARM64_TLVOFF:
40 assert(0 && "bad LLVMDisassembler_VariantKind");
41 return MCSymbolRefExpr::VK_None;
45 /// tryAddingSymbolicOperand - tryAddingSymbolicOperand trys to add a symbolic
46 /// operand in place of the immediate Value in the MCInst. The immediate
47 /// Value has not had any PC adjustment made by the caller. If the instruction
48 /// is a branch that adds the PC to the immediate Value then isBranch is
49 /// Success, else Fail. If GetOpInfo is non-null, then it is called to get any
50 /// symbolic information at the Address for this instrution. If that returns
51 /// non-zero then the symbolic information it returns is used to create an
52 /// MCExpr and that is added as an operand to the MCInst. If GetOpInfo()
53 /// returns zero and isBranch is Success then a symbol look up for
54 /// Address + Value is done and if a symbol is found an MCExpr is created with
55 /// that, else an MCExpr with Address + Value is created. If GetOpInfo()
56 /// returns zero and isBranch is Fail then the the Opcode of the MCInst is
57 /// tested and for ADRP an other instructions that help to load of pointers
58 /// a symbol look up is done to see it is returns a specific reference type
59 /// to add to the comment stream. This function returns Success if it adds
60 /// an operand to the MCInst and Fail otherwise.
61 bool ARM64ExternalSymbolizer::tryAddingSymbolicOperand(
63 raw_ostream &CommentStream,
69 // FIXME: This method shares a lot of code with
70 // MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
71 // refactor the MCExternalSymbolizer interface to allow more of this
72 // implementation to be shared.
74 struct LLVMOpInfo1 SymbolicOp;
75 memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
76 SymbolicOp.Value = Value;
77 uint64_t ReferenceType;
78 const char *ReferenceName;
80 !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) {
82 ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
83 const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
84 Address, &ReferenceName);
86 SymbolicOp.AddSymbol.Name = Name;
87 SymbolicOp.AddSymbol.Present = true;
90 SymbolicOp.Value = Address + Value;
92 if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
93 CommentStream << "symbol stub for: " << ReferenceName;
94 else if (ReferenceType ==
95 LLVMDisassembler_ReferenceType_Out_Objc_Message)
96 CommentStream << "Objc message: " << ReferenceName;
97 } else if (MI.getOpcode() == ARM64::ADRP) {
98 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
99 // otool expects the fully encoded ADRP instruction to be passed in as
100 // the value here, so reconstruct it:
101 const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
102 uint32_t EncodedInst = 0x90000000;
103 EncodedInst |= (Value & 0x3) << 29; // immlo
104 EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
105 EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
106 SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
108 CommentStream << format("0x%llx",
109 0xfffffffffffff000LL & (Address + Value));
110 } else if (MI.getOpcode() == ARM64::ADDXri ||
111 MI.getOpcode() == ARM64::LDRXui ||
112 MI.getOpcode() == ARM64::LDRXl ||
113 MI.getOpcode() == ARM64::ADR) {
114 if (MI.getOpcode() == ARM64::ADDXri)
115 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
116 else if (MI.getOpcode() == ARM64::LDRXui)
117 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
118 if (MI.getOpcode() == ARM64::LDRXl) {
119 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
120 SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
122 } else if (MI.getOpcode() == ARM64::ADR) {
123 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
124 SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
127 const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
128 // otool expects the fully encoded ADD/LDR instruction to be passed in
129 // as the value here, so reconstruct it:
130 unsigned EncodedInst =
131 MI.getOpcode() == ARM64::ADDXri ? 0x91000000: 0xF9400000;
132 EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
134 MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
135 EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
137 SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
140 if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
141 CommentStream << "literal pool symbol address: " << ReferenceName;
142 else if (ReferenceType ==
143 LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr)
144 CommentStream << "literal pool for: \"" << ReferenceName << "\"";
145 else if (ReferenceType ==
146 LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
147 CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
148 else if (ReferenceType ==
149 LLVMDisassembler_ReferenceType_Out_Objc_Message)
150 CommentStream << "Objc message: " << ReferenceName;
151 else if (ReferenceType ==
152 LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
153 CommentStream << "Objc message ref: " << ReferenceName;
154 else if (ReferenceType ==
155 LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
156 CommentStream << "Objc selector ref: " << ReferenceName;
157 else if (ReferenceType ==
158 LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
159 CommentStream << "Objc class ref: " << ReferenceName;
160 // For these instructions, the SymbolLookUp() above is just to get the
161 // ReferenceType and ReferenceName. We want to make sure not to
162 // fall through so we don't build an MCExpr to leave the disassembly
163 // of the immediate values of these instructions to the InstPrinter.
170 const MCExpr *Add = nullptr;
171 if (SymbolicOp.AddSymbol.Present) {
172 if (SymbolicOp.AddSymbol.Name) {
173 StringRef Name(SymbolicOp.AddSymbol.Name);
174 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
175 MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
176 if (Variant != MCSymbolRefExpr::VK_None)
177 Add = MCSymbolRefExpr::Create(Sym, Variant, Ctx);
179 Add = MCSymbolRefExpr::Create(Sym, Ctx);
181 Add = MCConstantExpr::Create(SymbolicOp.AddSymbol.Value, Ctx);
185 const MCExpr *Sub = nullptr;
186 if (SymbolicOp.SubtractSymbol.Present) {
187 if (SymbolicOp.SubtractSymbol.Name) {
188 StringRef Name(SymbolicOp.SubtractSymbol.Name);
189 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
190 Sub = MCSymbolRefExpr::Create(Sym, Ctx);
192 Sub = MCConstantExpr::Create(SymbolicOp.SubtractSymbol.Value, Ctx);
196 const MCExpr *Off = nullptr;
197 if (SymbolicOp.Value != 0)
198 Off = MCConstantExpr::Create(SymbolicOp.Value, Ctx);
204 LHS = MCBinaryExpr::CreateSub(Add, Sub, Ctx);
206 LHS = MCUnaryExpr::CreateMinus(Sub, Ctx);
208 Expr = MCBinaryExpr::CreateAdd(LHS, Off, Ctx);
213 Expr = MCBinaryExpr::CreateAdd(Add, Off, Ctx);
220 Expr = MCConstantExpr::Create(0, Ctx);
223 MI.addOperand(MCOperand::CreateExpr(Expr));