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