13b0846e8STim Northover //===- AArch64ExternalSymbolizer.cpp - Symbolizer for AArch64 ---*- C++ -*-===// 23b0846e8STim Northover // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler 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 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: 373b0846e8STim Northover case LLVMDisassembler_VariantKind_ARM64_TLVOFF: 383b0846e8STim Northover default: 3935b2f757SCraig Topper llvm_unreachable("bad LLVMDisassembler_VariantKind"); 403b0846e8STim Northover } 413b0846e8STim Northover } 423b0846e8STim Northover 433b0846e8STim Northover /// tryAddingSymbolicOperand - tryAddingSymbolicOperand trys to add a symbolic 443b0846e8STim Northover /// operand in place of the immediate Value in the MCInst. The immediate 453b0846e8STim Northover /// Value has not had any PC adjustment made by the caller. If the instruction 463b0846e8STim Northover /// is a branch that adds the PC to the immediate Value then isBranch is 473b0846e8STim Northover /// Success, else Fail. If GetOpInfo is non-null, then it is called to get any 483b0846e8STim Northover /// symbolic information at the Address for this instrution. If that returns 493b0846e8STim Northover /// non-zero then the symbolic information it returns is used to create an 503b0846e8STim Northover /// MCExpr and that is added as an operand to the MCInst. If GetOpInfo() 513b0846e8STim Northover /// returns zero and isBranch is Success then a symbol look up for 523b0846e8STim Northover /// Address + Value is done and if a symbol is found an MCExpr is created with 533b0846e8STim Northover /// that, else an MCExpr with Address + Value is created. If GetOpInfo() 54572e03a3SEric Christopher /// returns zero and isBranch is Fail then the Opcode of the MCInst is 553b0846e8STim Northover /// tested and for ADRP an other instructions that help to load of pointers 563b0846e8STim Northover /// a symbol look up is done to see it is returns a specific reference type 573b0846e8STim Northover /// to add to the comment stream. This function returns Success if it adds 583b0846e8STim Northover /// an operand to the MCInst and Fail otherwise. 593b0846e8STim Northover bool AArch64ExternalSymbolizer::tryAddingSymbolicOperand( 603b0846e8STim Northover MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address, 613b0846e8STim Northover bool IsBranch, uint64_t Offset, uint64_t InstSize) { 626a943fb1SDaniel Sanders if (!SymbolLookUp) 636a943fb1SDaniel Sanders return false; 643b0846e8STim Northover // FIXME: This method shares a lot of code with 653b0846e8STim Northover // MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible 663b0846e8STim Northover // refactor the MCExternalSymbolizer interface to allow more of this 673b0846e8STim Northover // implementation to be shared. 683b0846e8STim Northover // 693b0846e8STim Northover struct LLVMOpInfo1 SymbolicOp; 703b0846e8STim Northover memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1)); 713b0846e8STim Northover SymbolicOp.Value = Value; 723b0846e8STim Northover uint64_t ReferenceType; 733b0846e8STim Northover const char *ReferenceName; 743b0846e8STim Northover if (!GetOpInfo || 753b0846e8STim Northover !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) { 763b0846e8STim Northover if (IsBranch) { 773b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_Branch; 783b0846e8STim Northover const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType, 793b0846e8STim Northover Address, &ReferenceName); 803b0846e8STim Northover if (Name) { 813b0846e8STim Northover SymbolicOp.AddSymbol.Name = Name; 823b0846e8STim Northover SymbolicOp.AddSymbol.Present = true; 833b0846e8STim Northover SymbolicOp.Value = 0; 843b0846e8STim Northover } else { 853b0846e8STim Northover SymbolicOp.Value = Address + Value; 863b0846e8STim Northover } 873b0846e8STim Northover if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub) 883b0846e8STim Northover CommentStream << "symbol stub for: " << ReferenceName; 893b0846e8STim Northover else if (ReferenceType == 903b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Message) 913b0846e8STim Northover CommentStream << "Objc message: " << ReferenceName; 923b0846e8STim Northover } else if (MI.getOpcode() == AArch64::ADRP) { 933b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP; 943b0846e8STim Northover // otool expects the fully encoded ADRP instruction to be passed in as 953b0846e8STim Northover // the value here, so reconstruct it: 963b0846e8STim Northover const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo(); 973b0846e8STim Northover uint32_t EncodedInst = 0x90000000; 983b0846e8STim Northover EncodedInst |= (Value & 0x3) << 29; // immlo 993b0846e8STim Northover EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi 1003b0846e8STim Northover EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg 1013b0846e8STim Northover SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address, 1023b0846e8STim Northover &ReferenceName); 103d8949f50STim Northover CommentStream << format("0x%llx", (0xfffffffffffff000LL & Address) + 104d8949f50STim Northover Value * 0x1000); 1053b0846e8STim Northover } else if (MI.getOpcode() == AArch64::ADDXri || 1063b0846e8STim Northover MI.getOpcode() == AArch64::LDRXui || 1073b0846e8STim Northover MI.getOpcode() == AArch64::LDRXl || 1083b0846e8STim Northover MI.getOpcode() == AArch64::ADR) { 1093b0846e8STim Northover if (MI.getOpcode() == AArch64::ADDXri) 1103b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri; 1113b0846e8STim Northover else if (MI.getOpcode() == AArch64::LDRXui) 1123b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui; 1133b0846e8STim Northover if (MI.getOpcode() == AArch64::LDRXl) { 1143b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl; 1153b0846e8STim Northover SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address, 1163b0846e8STim Northover &ReferenceName); 1173b0846e8STim Northover } else if (MI.getOpcode() == AArch64::ADR) { 1183b0846e8STim Northover ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR; 1193b0846e8STim Northover SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address, 1203b0846e8STim Northover &ReferenceName); 1213b0846e8STim Northover } else { 1223b0846e8STim Northover const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo(); 1233b0846e8STim Northover // otool expects the fully encoded ADD/LDR instruction to be passed in 1243b0846e8STim Northover // as the value here, so reconstruct it: 1253b0846e8STim Northover unsigned EncodedInst = 1263b0846e8STim Northover MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000; 1273b0846e8STim Northover EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD] 1283b0846e8STim Northover EncodedInst |= 1293b0846e8STim Northover MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn 1303b0846e8STim Northover EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd 1313b0846e8STim Northover 1323b0846e8STim Northover SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address, 1333b0846e8STim Northover &ReferenceName); 1343b0846e8STim Northover } 1353b0846e8STim Northover if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr) 1363b0846e8STim Northover CommentStream << "literal pool symbol address: " << ReferenceName; 1373b0846e8STim Northover else if (ReferenceType == 138d2d2ce9bSKevin Enderby LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) { 139d2d2ce9bSKevin Enderby CommentStream << "literal pool for: \""; 140d2d2ce9bSKevin Enderby CommentStream.write_escaped(ReferenceName); 141d2d2ce9bSKevin Enderby CommentStream << "\""; 142d2d2ce9bSKevin Enderby } else if (ReferenceType == 1433b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref) 1443b0846e8STim Northover CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\""; 1453b0846e8STim Northover else if (ReferenceType == 1463b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Message) 1473b0846e8STim Northover CommentStream << "Objc message: " << ReferenceName; 1483b0846e8STim Northover else if (ReferenceType == 1493b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref) 1503b0846e8STim Northover CommentStream << "Objc message ref: " << ReferenceName; 1513b0846e8STim Northover else if (ReferenceType == 1523b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref) 1533b0846e8STim Northover CommentStream << "Objc selector ref: " << ReferenceName; 1543b0846e8STim Northover else if (ReferenceType == 1553b0846e8STim Northover LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref) 1563b0846e8STim Northover CommentStream << "Objc class ref: " << ReferenceName; 1573b0846e8STim Northover // For these instructions, the SymbolLookUp() above is just to get the 1583b0846e8STim Northover // ReferenceType and ReferenceName. We want to make sure not to 1593b0846e8STim Northover // fall through so we don't build an MCExpr to leave the disassembly 1603b0846e8STim Northover // of the immediate values of these instructions to the InstPrinter. 1613b0846e8STim Northover return false; 1623b0846e8STim Northover } else { 1633b0846e8STim Northover return false; 1643b0846e8STim Northover } 1653b0846e8STim Northover } 1663b0846e8STim Northover 1673b0846e8STim Northover const MCExpr *Add = nullptr; 1683b0846e8STim Northover if (SymbolicOp.AddSymbol.Present) { 1693b0846e8STim Northover if (SymbolicOp.AddSymbol.Name) { 1703b0846e8STim Northover StringRef Name(SymbolicOp.AddSymbol.Name); 1716f482000SJim Grosbach MCSymbol *Sym = Ctx.getOrCreateSymbol(Name); 1723b0846e8STim Northover MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind); 1733b0846e8STim Northover if (Variant != MCSymbolRefExpr::VK_None) 17413760bd1SJim Grosbach Add = MCSymbolRefExpr::create(Sym, Variant, Ctx); 1753b0846e8STim Northover else 17613760bd1SJim Grosbach Add = MCSymbolRefExpr::create(Sym, Ctx); 1773b0846e8STim Northover } else { 17813760bd1SJim Grosbach Add = MCConstantExpr::create(SymbolicOp.AddSymbol.Value, Ctx); 1793b0846e8STim Northover } 1803b0846e8STim Northover } 1813b0846e8STim Northover 1823b0846e8STim Northover const MCExpr *Sub = nullptr; 1833b0846e8STim Northover if (SymbolicOp.SubtractSymbol.Present) { 1843b0846e8STim Northover if (SymbolicOp.SubtractSymbol.Name) { 1853b0846e8STim Northover StringRef Name(SymbolicOp.SubtractSymbol.Name); 1866f482000SJim Grosbach MCSymbol *Sym = Ctx.getOrCreateSymbol(Name); 18713760bd1SJim Grosbach Sub = MCSymbolRefExpr::create(Sym, Ctx); 1883b0846e8STim Northover } else { 18913760bd1SJim Grosbach Sub = MCConstantExpr::create(SymbolicOp.SubtractSymbol.Value, Ctx); 1903b0846e8STim Northover } 1913b0846e8STim Northover } 1923b0846e8STim Northover 1933b0846e8STim Northover const MCExpr *Off = nullptr; 1943b0846e8STim Northover if (SymbolicOp.Value != 0) 19513760bd1SJim Grosbach Off = MCConstantExpr::create(SymbolicOp.Value, Ctx); 1963b0846e8STim Northover 1973b0846e8STim Northover const MCExpr *Expr; 1983b0846e8STim Northover if (Sub) { 1993b0846e8STim Northover const MCExpr *LHS; 2003b0846e8STim Northover if (Add) 20113760bd1SJim Grosbach LHS = MCBinaryExpr::createSub(Add, Sub, Ctx); 2023b0846e8STim Northover else 20313760bd1SJim Grosbach LHS = MCUnaryExpr::createMinus(Sub, Ctx); 2043b0846e8STim Northover if (Off) 20513760bd1SJim Grosbach Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx); 2063b0846e8STim Northover else 2073b0846e8STim Northover Expr = LHS; 2083b0846e8STim Northover } else if (Add) { 2093b0846e8STim Northover if (Off) 21013760bd1SJim Grosbach Expr = MCBinaryExpr::createAdd(Add, Off, Ctx); 2113b0846e8STim Northover else 2123b0846e8STim Northover Expr = Add; 2133b0846e8STim Northover } else { 2143b0846e8STim Northover if (Off) 2153b0846e8STim Northover Expr = Off; 2163b0846e8STim Northover else 21713760bd1SJim Grosbach Expr = MCConstantExpr::create(0, Ctx); 2183b0846e8STim Northover } 2193b0846e8STim Northover 220e9119e41SJim Grosbach MI.addOperand(MCOperand::createExpr(Expr)); 2213b0846e8STim Northover 2223b0846e8STim Northover return true; 2233b0846e8STim Northover } 224