AArch64/ARM64: move ARM64 into AArch64's place
[oota-llvm.git] / lib / Target / AArch64 / Disassembler / AArch64ExternalSymbolizer.cpp
1 //===- AArch64ExternalSymbolizer.cpp - Symbolizer for AArch64 ---*- 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
10 #include "AArch64ExternalSymbolizer.h"
11 #include "AArch64Subtarget.h"
12 #include "MCTargetDesc/AArch64AddressingModes.h"
13 #include "Utils/AArch64BaseInfo.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"
19
20 using namespace llvm;
21
22 #define DEBUG_TYPE "aarch64-disassembler"
23
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:
39   default:
40     assert(0 && "bad LLVMDisassembler_VariantKind");
41     return MCSymbolRefExpr::VK_None;
42   }
43 }
44
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 AArch64ExternalSymbolizer::tryAddingSymbolicOperand(
62     MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address,
63     bool IsBranch, uint64_t Offset, uint64_t InstSize) {
64   // FIXME: This method shares a lot of code with
65   //        MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
66   //        refactor the MCExternalSymbolizer interface to allow more of this
67   //        implementation to be shared.
68   //
69   struct LLVMOpInfo1 SymbolicOp;
70   memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
71   SymbolicOp.Value = Value;
72   uint64_t ReferenceType;
73   const char *ReferenceName;
74   if (!GetOpInfo ||
75       !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) {
76     if (IsBranch) {
77       ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
78       const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
79                                       Address, &ReferenceName);
80       if (Name) {
81         SymbolicOp.AddSymbol.Name = Name;
82         SymbolicOp.AddSymbol.Present = true;
83         SymbolicOp.Value = 0;
84       } else {
85         SymbolicOp.Value = Address + Value;
86       }
87       if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
88         CommentStream << "symbol stub for: " << ReferenceName;
89       else if (ReferenceType ==
90                LLVMDisassembler_ReferenceType_Out_Objc_Message)
91         CommentStream << "Objc message: " << ReferenceName;
92     } else if (MI.getOpcode() == AArch64::ADRP) {
93         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
94         // otool expects the fully encoded ADRP instruction to be passed in as
95         // the value here, so reconstruct it:
96         const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
97         uint32_t EncodedInst = 0x90000000;
98         EncodedInst |= (Value & 0x3) << 29; // immlo
99         EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
100         EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
101         SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
102                      &ReferenceName);
103         CommentStream << format("0x%llx",
104                                 0xfffffffffffff000LL & (Address + Value));
105     } else if (MI.getOpcode() == AArch64::ADDXri ||
106                MI.getOpcode() == AArch64::LDRXui ||
107                MI.getOpcode() == AArch64::LDRXl ||
108                MI.getOpcode() == AArch64::ADR) {
109       if (MI.getOpcode() == AArch64::ADDXri)
110         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
111       else if (MI.getOpcode() == AArch64::LDRXui)
112         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
113       if (MI.getOpcode() == AArch64::LDRXl) {
114         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
115         SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
116                      &ReferenceName);
117       } else if (MI.getOpcode() == AArch64::ADR) {
118         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
119         SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
120                             &ReferenceName);
121       } else {
122         const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
123         // otool expects the fully encoded ADD/LDR instruction to be passed in
124         // as the value here, so reconstruct it:
125         unsigned EncodedInst =
126           MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000;
127         EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
128         EncodedInst |=
129           MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
130         EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
131
132         SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
133                      &ReferenceName);
134       }
135       if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
136         CommentStream << "literal pool symbol address: " << ReferenceName;
137       else if (ReferenceType ==
138                LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr)
139         CommentStream << "literal pool for: \"" << ReferenceName << "\"";
140       else if (ReferenceType ==
141                LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
142         CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
143       else if (ReferenceType ==
144                LLVMDisassembler_ReferenceType_Out_Objc_Message)
145         CommentStream << "Objc message: " << ReferenceName;
146       else if (ReferenceType ==
147                LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
148         CommentStream << "Objc message ref: " << ReferenceName;
149       else if (ReferenceType ==
150                LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
151         CommentStream << "Objc selector ref: " << ReferenceName;
152       else if (ReferenceType ==
153                LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
154         CommentStream << "Objc class ref: " << ReferenceName;
155       // For these instructions, the SymbolLookUp() above is just to get the
156       // ReferenceType and ReferenceName.  We want to make sure not to
157       // fall through so we don't build an MCExpr to leave the disassembly
158       // of the immediate values of these instructions to the InstPrinter.
159       return false;
160     } else {
161       return false;
162     }
163   }
164
165   const MCExpr *Add = nullptr;
166   if (SymbolicOp.AddSymbol.Present) {
167     if (SymbolicOp.AddSymbol.Name) {
168       StringRef Name(SymbolicOp.AddSymbol.Name);
169       MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
170       MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
171       if (Variant != MCSymbolRefExpr::VK_None)
172         Add = MCSymbolRefExpr::Create(Sym, Variant, Ctx);
173       else
174         Add = MCSymbolRefExpr::Create(Sym, Ctx);
175     } else {
176       Add = MCConstantExpr::Create(SymbolicOp.AddSymbol.Value, Ctx);
177     }
178   }
179
180   const MCExpr *Sub = nullptr;
181   if (SymbolicOp.SubtractSymbol.Present) {
182     if (SymbolicOp.SubtractSymbol.Name) {
183       StringRef Name(SymbolicOp.SubtractSymbol.Name);
184       MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
185       Sub = MCSymbolRefExpr::Create(Sym, Ctx);
186     } else {
187       Sub = MCConstantExpr::Create(SymbolicOp.SubtractSymbol.Value, Ctx);
188     }
189   }
190
191   const MCExpr *Off = nullptr;
192   if (SymbolicOp.Value != 0)
193     Off = MCConstantExpr::Create(SymbolicOp.Value, Ctx);
194
195   const MCExpr *Expr;
196   if (Sub) {
197     const MCExpr *LHS;
198     if (Add)
199       LHS = MCBinaryExpr::CreateSub(Add, Sub, Ctx);
200     else
201       LHS = MCUnaryExpr::CreateMinus(Sub, Ctx);
202     if (Off)
203       Expr = MCBinaryExpr::CreateAdd(LHS, Off, Ctx);
204     else
205       Expr = LHS;
206   } else if (Add) {
207     if (Off)
208       Expr = MCBinaryExpr::CreateAdd(Add, Off, Ctx);
209     else
210       Expr = Add;
211   } else {
212     if (Off)
213       Expr = Off;
214     else
215       Expr = MCConstantExpr::Create(0, Ctx);
216   }
217
218   MI.addOperand(MCOperand::CreateExpr(Expr));
219
220   return true;
221 }