blob: 4e9b75eeb4ff5fc95c83156463573f5192bb09a9 [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===- 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
20using namespace llvm;
21
22#define DEBUG_TYPE "aarch64-disassembler"
23
24static MCSymbolRefExpr::VariantKind
25getVariant(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:
Craig Topper35b2f752014-06-19 06:10:58 +000040 llvm_unreachable("bad LLVMDisassembler_VariantKind");
Tim Northover3b0846e2014-05-24 12:50:23 +000041 }
42}
43
44/// tryAddingSymbolicOperand - tryAddingSymbolicOperand trys to add a symbolic
45/// operand in place of the immediate Value in the MCInst. The immediate
46/// Value has not had any PC adjustment made by the caller. If the instruction
47/// is a branch that adds the PC to the immediate Value then isBranch is
48/// Success, else Fail. If GetOpInfo is non-null, then it is called to get any
49/// symbolic information at the Address for this instrution. If that returns
50/// non-zero then the symbolic information it returns is used to create an
51/// MCExpr and that is added as an operand to the MCInst. If GetOpInfo()
52/// returns zero and isBranch is Success then a symbol look up for
53/// Address + Value is done and if a symbol is found an MCExpr is created with
54/// that, else an MCExpr with Address + Value is created. If GetOpInfo()
Eric Christopher572e03a2015-06-19 01:53:21 +000055/// returns zero and isBranch is Fail then the Opcode of the MCInst is
Tim Northover3b0846e2014-05-24 12:50:23 +000056/// tested and for ADRP an other instructions that help to load of pointers
57/// a symbol look up is done to see it is returns a specific reference type
58/// to add to the comment stream. This function returns Success if it adds
59/// an operand to the MCInst and Fail otherwise.
60bool AArch64ExternalSymbolizer::tryAddingSymbolicOperand(
61 MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address,
62 bool IsBranch, uint64_t Offset, uint64_t InstSize) {
Daniel Sanders6a943fb2018-08-21 15:47:25 +000063 if (!SymbolLookUp)
64 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +000065 // FIXME: This method shares a lot of code with
66 // MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
67 // refactor the MCExternalSymbolizer interface to allow more of this
68 // implementation to be shared.
69 //
70 struct LLVMOpInfo1 SymbolicOp;
71 memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
72 SymbolicOp.Value = Value;
73 uint64_t ReferenceType;
74 const char *ReferenceName;
75 if (!GetOpInfo ||
76 !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) {
77 if (IsBranch) {
78 ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
79 const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
80 Address, &ReferenceName);
81 if (Name) {
82 SymbolicOp.AddSymbol.Name = Name;
83 SymbolicOp.AddSymbol.Present = true;
84 SymbolicOp.Value = 0;
85 } else {
86 SymbolicOp.Value = Address + Value;
87 }
88 if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
89 CommentStream << "symbol stub for: " << ReferenceName;
90 else if (ReferenceType ==
91 LLVMDisassembler_ReferenceType_Out_Objc_Message)
92 CommentStream << "Objc message: " << ReferenceName;
93 } else if (MI.getOpcode() == AArch64::ADRP) {
94 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
95 // otool expects the fully encoded ADRP instruction to be passed in as
96 // the value here, so reconstruct it:
97 const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
98 uint32_t EncodedInst = 0x90000000;
99 EncodedInst |= (Value & 0x3) << 29; // immlo
100 EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
101 EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
102 SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
103 &ReferenceName);
Tim Northoverd8949f52018-05-30 09:54:59 +0000104 CommentStream << format("0x%llx", (0xfffffffffffff000LL & Address) +
105 Value * 0x1000);
Tim Northover3b0846e2014-05-24 12:50:23 +0000106 } else if (MI.getOpcode() == AArch64::ADDXri ||
107 MI.getOpcode() == AArch64::LDRXui ||
108 MI.getOpcode() == AArch64::LDRXl ||
109 MI.getOpcode() == AArch64::ADR) {
110 if (MI.getOpcode() == AArch64::ADDXri)
111 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
112 else if (MI.getOpcode() == AArch64::LDRXui)
113 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
114 if (MI.getOpcode() == AArch64::LDRXl) {
115 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
116 SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
117 &ReferenceName);
118 } else if (MI.getOpcode() == AArch64::ADR) {
119 ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
120 SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
121 &ReferenceName);
122 } else {
123 const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
124 // otool expects the fully encoded ADD/LDR instruction to be passed in
125 // as the value here, so reconstruct it:
126 unsigned EncodedInst =
127 MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000;
128 EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
129 EncodedInst |=
130 MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
131 EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
132
133 SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
134 &ReferenceName);
135 }
136 if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
137 CommentStream << "literal pool symbol address: " << ReferenceName;
138 else if (ReferenceType ==
Kevin Enderbyd2d2ce92016-06-13 21:08:57 +0000139 LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) {
140 CommentStream << "literal pool for: \"";
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000141 CommentStream.write_escaped(ReferenceName);
Kevin Enderbyd2d2ce92016-06-13 21:08:57 +0000142 CommentStream << "\"";
143 } else if (ReferenceType ==
Tim Northover3b0846e2014-05-24 12:50:23 +0000144 LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
145 CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
146 else if (ReferenceType ==
147 LLVMDisassembler_ReferenceType_Out_Objc_Message)
148 CommentStream << "Objc message: " << ReferenceName;
149 else if (ReferenceType ==
150 LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
151 CommentStream << "Objc message ref: " << ReferenceName;
152 else if (ReferenceType ==
153 LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
154 CommentStream << "Objc selector ref: " << ReferenceName;
155 else if (ReferenceType ==
156 LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
157 CommentStream << "Objc class ref: " << ReferenceName;
158 // For these instructions, the SymbolLookUp() above is just to get the
159 // ReferenceType and ReferenceName. We want to make sure not to
160 // fall through so we don't build an MCExpr to leave the disassembly
161 // of the immediate values of these instructions to the InstPrinter.
162 return false;
163 } else {
164 return false;
165 }
166 }
167
168 const MCExpr *Add = nullptr;
169 if (SymbolicOp.AddSymbol.Present) {
170 if (SymbolicOp.AddSymbol.Name) {
171 StringRef Name(SymbolicOp.AddSymbol.Name);
Jim Grosbach6f482002015-05-18 18:43:14 +0000172 MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
Tim Northover3b0846e2014-05-24 12:50:23 +0000173 MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
174 if (Variant != MCSymbolRefExpr::VK_None)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000175 Add = MCSymbolRefExpr::create(Sym, Variant, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000176 else
Jim Grosbach13760bd2015-05-30 01:25:56 +0000177 Add = MCSymbolRefExpr::create(Sym, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000178 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000179 Add = MCConstantExpr::create(SymbolicOp.AddSymbol.Value, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000180 }
181 }
182
183 const MCExpr *Sub = nullptr;
184 if (SymbolicOp.SubtractSymbol.Present) {
185 if (SymbolicOp.SubtractSymbol.Name) {
186 StringRef Name(SymbolicOp.SubtractSymbol.Name);
Jim Grosbach6f482002015-05-18 18:43:14 +0000187 MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000188 Sub = MCSymbolRefExpr::create(Sym, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000189 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000190 Sub = MCConstantExpr::create(SymbolicOp.SubtractSymbol.Value, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000191 }
192 }
193
194 const MCExpr *Off = nullptr;
195 if (SymbolicOp.Value != 0)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000196 Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000197
198 const MCExpr *Expr;
199 if (Sub) {
200 const MCExpr *LHS;
201 if (Add)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000202 LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000203 else
Jim Grosbach13760bd2015-05-30 01:25:56 +0000204 LHS = MCUnaryExpr::createMinus(Sub, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000205 if (Off)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000206 Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000207 else
208 Expr = LHS;
209 } else if (Add) {
210 if (Off)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000211 Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000212 else
213 Expr = Add;
214 } else {
215 if (Off)
216 Expr = Off;
217 else
Jim Grosbach13760bd2015-05-30 01:25:56 +0000218 Expr = MCConstantExpr::create(0, Ctx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000219 }
220
Jim Grosbache9119e42015-05-13 18:37:00 +0000221 MI.addOperand(MCOperand::createExpr(Expr));
Tim Northover3b0846e2014-05-24 12:50:23 +0000222
223 return true;
224}