13b0846e8STim Northover //===- AArch64ExternalSymbolizer.cpp - Symbolizer for AArch64 ---*- C++ -*-===//
23b0846e8STim Northover //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63b0846e8STim Northover //
73b0846e8STim Northover //===----------------------------------------------------------------------===//
83b0846e8STim Northover
93b0846e8STim Northover #include "AArch64ExternalSymbolizer.h"
103b0846e8STim Northover #include "MCTargetDesc/AArch64AddressingModes.h"
113b0846e8STim Northover #include "Utils/AArch64BaseInfo.h"
123b0846e8STim Northover #include "llvm/MC/MCContext.h"
133b0846e8STim Northover #include "llvm/MC/MCExpr.h"
143b0846e8STim Northover #include "llvm/MC/MCInst.h"
1527c769d2SBenjamin Kramer #include "llvm/MC/MCRegisterInfo.h"
163b0846e8STim Northover #include "llvm/Support/Format.h"
173b0846e8STim Northover #include "llvm/Support/raw_ostream.h"
183b0846e8STim Northover
193b0846e8STim Northover using namespace llvm;
203b0846e8STim Northover
213b0846e8STim Northover #define DEBUG_TYPE "aarch64-disassembler"
223b0846e8STim Northover
233b0846e8STim Northover static MCSymbolRefExpr::VariantKind
getVariant(uint64_t LLVMDisassembler_VariantKind)243b0846e8STim Northover getVariant(uint64_t LLVMDisassembler_VariantKind) {
253b0846e8STim Northover switch (LLVMDisassembler_VariantKind) {
263b0846e8STim Northover case LLVMDisassembler_VariantKind_None:
273b0846e8STim Northover return MCSymbolRefExpr::VK_None;
283b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_PAGE:
293b0846e8STim Northover return MCSymbolRefExpr::VK_PAGE;
303b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_PAGEOFF:
313b0846e8STim Northover return MCSymbolRefExpr::VK_PAGEOFF;
323b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_GOTPAGE:
333b0846e8STim Northover return MCSymbolRefExpr::VK_GOTPAGE;
343b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF:
353b0846e8STim Northover return MCSymbolRefExpr::VK_GOTPAGEOFF;
363b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_TLVP:
37e23c6cc5SNico Weber return MCSymbolRefExpr::VK_TLVPPAGE;
383b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_TLVOFF:
39e23c6cc5SNico Weber return MCSymbolRefExpr::VK_TLVPPAGEOFF;
403b0846e8STim Northover default:
4135b2f757SCraig Topper llvm_unreachable("bad LLVMDisassembler_VariantKind");
423b0846e8STim Northover }
433b0846e8STim Northover }
443b0846e8STim Northover
453b0846e8STim Northover /// tryAddingSymbolicOperand - tryAddingSymbolicOperand trys to add a symbolic
463b0846e8STim Northover /// operand in place of the immediate Value in the MCInst. The immediate
473b0846e8STim Northover /// Value has not had any PC adjustment made by the caller. If the instruction
483b0846e8STim Northover /// is a branch that adds the PC to the immediate Value then isBranch is
493b0846e8STim Northover /// Success, else Fail. If GetOpInfo is non-null, then it is called to get any
503b0846e8STim Northover /// symbolic information at the Address for this instrution. If that returns
513b0846e8STim Northover /// non-zero then the symbolic information it returns is used to create an
523b0846e8STim Northover /// MCExpr and that is added as an operand to the MCInst. If GetOpInfo()
533b0846e8STim Northover /// returns zero and isBranch is Success then a symbol look up for
543b0846e8STim Northover /// Address + Value is done and if a symbol is found an MCExpr is created with
553b0846e8STim Northover /// that, else an MCExpr with Address + Value is created. If GetOpInfo()
56572e03a3SEric Christopher /// returns zero and isBranch is Fail then the Opcode of the MCInst is
573b0846e8STim Northover /// tested and for ADRP an other instructions that help to load of pointers
583b0846e8STim Northover /// a symbol look up is done to see it is returns a specific reference type
593b0846e8STim Northover /// to add to the comment stream. This function returns Success if it adds
603b0846e8STim Northover /// an operand to the MCInst and Fail otherwise.
tryAddingSymbolicOperand(MCInst & MI,raw_ostream & CommentStream,int64_t Value,uint64_t Address,bool IsBranch,uint64_t Offset,uint64_t OpSize,uint64_t InstSize)613b0846e8STim Northover bool AArch64ExternalSymbolizer::tryAddingSymbolicOperand(
623b0846e8STim Northover MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address,
63*bed9efedSMaksim Panchenko bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) {
646a943fb1SDaniel Sanders if (!SymbolLookUp)
656a943fb1SDaniel Sanders return false;
663b0846e8STim Northover // FIXME: This method shares a lot of code with
673b0846e8STim Northover // MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
683b0846e8STim Northover // refactor the MCExternalSymbolizer interface to allow more of this
693b0846e8STim Northover // implementation to be shared.
703b0846e8STim Northover //
713b0846e8STim Northover struct LLVMOpInfo1 SymbolicOp;
723b0846e8STim Northover memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
733b0846e8STim Northover SymbolicOp.Value = Value;
743b0846e8STim Northover uint64_t ReferenceType;
753b0846e8STim Northover const char *ReferenceName;
76*bed9efedSMaksim Panchenko if (!GetOpInfo || !GetOpInfo(DisInfo, Address, /*Offset=*/0, OpSize, InstSize,
77*bed9efedSMaksim Panchenko 1, &SymbolicOp)) {
783b0846e8STim Northover if (IsBranch) {
793b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
803b0846e8STim Northover const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
813b0846e8STim Northover Address, &ReferenceName);
823b0846e8STim Northover if (Name) {
833b0846e8STim Northover SymbolicOp.AddSymbol.Name = Name;
843b0846e8STim Northover SymbolicOp.AddSymbol.Present = true;
853b0846e8STim Northover SymbolicOp.Value = 0;
863b0846e8STim Northover } else {
873b0846e8STim Northover SymbolicOp.Value = Address + Value;
883b0846e8STim Northover }
893b0846e8STim Northover if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
903b0846e8STim Northover CommentStream << "symbol stub for: " << ReferenceName;
913b0846e8STim Northover else if (ReferenceType ==
923b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Message)
933b0846e8STim Northover CommentStream << "Objc message: " << ReferenceName;
943b0846e8STim Northover } else if (MI.getOpcode() == AArch64::ADRP) {
953b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
963b0846e8STim Northover // otool expects the fully encoded ADRP instruction to be passed in as
973b0846e8STim Northover // the value here, so reconstruct it:
983b0846e8STim Northover const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
993b0846e8STim Northover uint32_t EncodedInst = 0x90000000;
1003b0846e8STim Northover EncodedInst |= (Value & 0x3) << 29; // immlo
1013b0846e8STim Northover EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
1023b0846e8STim Northover EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
1033b0846e8STim Northover SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
1043b0846e8STim Northover &ReferenceName);
105d8949f50STim Northover CommentStream << format("0x%llx", (0xfffffffffffff000LL & Address) +
106d8949f50STim Northover Value * 0x1000);
1073b0846e8STim Northover } else if (MI.getOpcode() == AArch64::ADDXri ||
1083b0846e8STim Northover MI.getOpcode() == AArch64::LDRXui ||
1093b0846e8STim Northover MI.getOpcode() == AArch64::LDRXl ||
1103b0846e8STim Northover MI.getOpcode() == AArch64::ADR) {
1113b0846e8STim Northover if (MI.getOpcode() == AArch64::ADDXri)
1123b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
1133b0846e8STim Northover else if (MI.getOpcode() == AArch64::LDRXui)
1143b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
1153b0846e8STim Northover if (MI.getOpcode() == AArch64::LDRXl) {
1163b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
1173b0846e8STim Northover SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
1183b0846e8STim Northover &ReferenceName);
1193b0846e8STim Northover } else if (MI.getOpcode() == AArch64::ADR) {
1203b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
1213b0846e8STim Northover SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
1223b0846e8STim Northover &ReferenceName);
1233b0846e8STim Northover } else {
1243b0846e8STim Northover const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
1253b0846e8STim Northover // otool expects the fully encoded ADD/LDR instruction to be passed in
1263b0846e8STim Northover // as the value here, so reconstruct it:
1273b0846e8STim Northover unsigned EncodedInst =
1283b0846e8STim Northover MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000;
1293b0846e8STim Northover EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
1303b0846e8STim Northover EncodedInst |=
1313b0846e8STim Northover MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
1323b0846e8STim Northover EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
1333b0846e8STim Northover
1343b0846e8STim Northover SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
1353b0846e8STim Northover &ReferenceName);
1363b0846e8STim Northover }
1373b0846e8STim Northover if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
1383b0846e8STim Northover CommentStream << "literal pool symbol address: " << ReferenceName;
1393b0846e8STim Northover else if (ReferenceType ==
140d2d2ce9bSKevin Enderby LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) {
141d2d2ce9bSKevin Enderby CommentStream << "literal pool for: \"";
142d2d2ce9bSKevin Enderby CommentStream.write_escaped(ReferenceName);
143d2d2ce9bSKevin Enderby CommentStream << "\"";
144d2d2ce9bSKevin Enderby } else if (ReferenceType ==
1453b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
1463b0846e8STim Northover CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
1473b0846e8STim Northover else if (ReferenceType ==
1483b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Message)
1493b0846e8STim Northover CommentStream << "Objc message: " << ReferenceName;
1503b0846e8STim Northover else if (ReferenceType ==
1513b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
1523b0846e8STim Northover CommentStream << "Objc message ref: " << ReferenceName;
1533b0846e8STim Northover else if (ReferenceType ==
1543b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
1553b0846e8STim Northover CommentStream << "Objc selector ref: " << ReferenceName;
1563b0846e8STim Northover else if (ReferenceType ==
1573b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
1583b0846e8STim Northover CommentStream << "Objc class ref: " << ReferenceName;
1593b0846e8STim Northover // For these instructions, the SymbolLookUp() above is just to get the
1603b0846e8STim Northover // ReferenceType and ReferenceName. We want to make sure not to
1613b0846e8STim Northover // fall through so we don't build an MCExpr to leave the disassembly
1623b0846e8STim Northover // of the immediate values of these instructions to the InstPrinter.
1633b0846e8STim Northover return false;
1643b0846e8STim Northover } else {
1653b0846e8STim Northover return false;
1663b0846e8STim Northover }
1673b0846e8STim Northover }
1683b0846e8STim Northover
1693b0846e8STim Northover const MCExpr *Add = nullptr;
1703b0846e8STim Northover if (SymbolicOp.AddSymbol.Present) {
1713b0846e8STim Northover if (SymbolicOp.AddSymbol.Name) {
1723b0846e8STim Northover StringRef Name(SymbolicOp.AddSymbol.Name);
1736f482000SJim Grosbach MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
1743b0846e8STim Northover MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
1753b0846e8STim Northover if (Variant != MCSymbolRefExpr::VK_None)
17613760bd1SJim Grosbach Add = MCSymbolRefExpr::create(Sym, Variant, Ctx);
1773b0846e8STim Northover else
17813760bd1SJim Grosbach Add = MCSymbolRefExpr::create(Sym, Ctx);
1793b0846e8STim Northover } else {
18013760bd1SJim Grosbach Add = MCConstantExpr::create(SymbolicOp.AddSymbol.Value, Ctx);
1813b0846e8STim Northover }
1823b0846e8STim Northover }
1833b0846e8STim Northover
1843b0846e8STim Northover const MCExpr *Sub = nullptr;
1853b0846e8STim Northover if (SymbolicOp.SubtractSymbol.Present) {
1863b0846e8STim Northover if (SymbolicOp.SubtractSymbol.Name) {
1873b0846e8STim Northover StringRef Name(SymbolicOp.SubtractSymbol.Name);
1886f482000SJim Grosbach MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
18913760bd1SJim Grosbach Sub = MCSymbolRefExpr::create(Sym, Ctx);
1903b0846e8STim Northover } else {
19113760bd1SJim Grosbach Sub = MCConstantExpr::create(SymbolicOp.SubtractSymbol.Value, Ctx);
1923b0846e8STim Northover }
1933b0846e8STim Northover }
1943b0846e8STim Northover
1953b0846e8STim Northover const MCExpr *Off = nullptr;
1963b0846e8STim Northover if (SymbolicOp.Value != 0)
19713760bd1SJim Grosbach Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
1983b0846e8STim Northover
1993b0846e8STim Northover const MCExpr *Expr;
2003b0846e8STim Northover if (Sub) {
2013b0846e8STim Northover const MCExpr *LHS;
2023b0846e8STim Northover if (Add)
20313760bd1SJim Grosbach LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
2043b0846e8STim Northover else
20513760bd1SJim Grosbach LHS = MCUnaryExpr::createMinus(Sub, Ctx);
2063b0846e8STim Northover if (Off)
20713760bd1SJim Grosbach Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
2083b0846e8STim Northover else
2093b0846e8STim Northover Expr = LHS;
2103b0846e8STim Northover } else if (Add) {
2113b0846e8STim Northover if (Off)
21213760bd1SJim Grosbach Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
2133b0846e8STim Northover else
2143b0846e8STim Northover Expr = Add;
2153b0846e8STim Northover } else {
2163b0846e8STim Northover if (Off)
2173b0846e8STim Northover Expr = Off;
2183b0846e8STim Northover else
21913760bd1SJim Grosbach Expr = MCConstantExpr::create(0, Ctx);
2203b0846e8STim Northover }
2213b0846e8STim Northover
222e9119e41SJim Grosbach MI.addOperand(MCOperand::createExpr(Expr));
2233b0846e8STim Northover
2243b0846e8STim Northover return true;
2253b0846e8STim Northover }
226