13b0846e8STim Northover //===- AArch64ExternalSymbolizer.cpp - Symbolizer for AArch64 ---*- C++ -*-===//
23b0846e8STim Northover //
33b0846e8STim Northover //                     The LLVM Compiler Infrastructure
43b0846e8STim Northover //
53b0846e8STim Northover // This file is distributed under the University of Illinois Open Source
63b0846e8STim Northover // License. See LICENSE.TXT for details.
73b0846e8STim Northover //
83b0846e8STim Northover //===----------------------------------------------------------------------===//
93b0846e8STim Northover 
103b0846e8STim Northover #include "AArch64ExternalSymbolizer.h"
113b0846e8STim Northover #include "MCTargetDesc/AArch64AddressingModes.h"
123b0846e8STim Northover #include "Utils/AArch64BaseInfo.h"
133b0846e8STim Northover #include "llvm/MC/MCContext.h"
143b0846e8STim Northover #include "llvm/MC/MCExpr.h"
153b0846e8STim Northover #include "llvm/MC/MCInst.h"
16*27c769d2SBenjamin Kramer #include "llvm/MC/MCRegisterInfo.h"
173b0846e8STim Northover #include "llvm/Support/Format.h"
183b0846e8STim Northover #include "llvm/Support/raw_ostream.h"
193b0846e8STim Northover 
203b0846e8STim Northover using namespace llvm;
213b0846e8STim Northover 
223b0846e8STim Northover #define DEBUG_TYPE "aarch64-disassembler"
233b0846e8STim Northover 
243b0846e8STim Northover static MCSymbolRefExpr::VariantKind
253b0846e8STim Northover getVariant(uint64_t LLVMDisassembler_VariantKind) {
263b0846e8STim Northover   switch (LLVMDisassembler_VariantKind) {
273b0846e8STim Northover   case LLVMDisassembler_VariantKind_None:
283b0846e8STim Northover     return MCSymbolRefExpr::VK_None;
293b0846e8STim Northover   case LLVMDisassembler_VariantKind_ARM64_PAGE:
303b0846e8STim Northover     return MCSymbolRefExpr::VK_PAGE;
313b0846e8STim Northover   case LLVMDisassembler_VariantKind_ARM64_PAGEOFF:
323b0846e8STim Northover     return MCSymbolRefExpr::VK_PAGEOFF;
333b0846e8STim Northover   case LLVMDisassembler_VariantKind_ARM64_GOTPAGE:
343b0846e8STim Northover     return MCSymbolRefExpr::VK_GOTPAGE;
353b0846e8STim Northover   case LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF:
363b0846e8STim Northover     return MCSymbolRefExpr::VK_GOTPAGEOFF;
373b0846e8STim Northover   case LLVMDisassembler_VariantKind_ARM64_TLVP:
383b0846e8STim Northover   case LLVMDisassembler_VariantKind_ARM64_TLVOFF:
393b0846e8STim Northover   default:
4035b2f757SCraig Topper     llvm_unreachable("bad LLVMDisassembler_VariantKind");
413b0846e8STim Northover   }
423b0846e8STim Northover }
433b0846e8STim Northover 
443b0846e8STim Northover /// tryAddingSymbolicOperand - tryAddingSymbolicOperand trys to add a symbolic
453b0846e8STim Northover /// operand in place of the immediate Value in the MCInst.  The immediate
463b0846e8STim Northover /// Value has not had any PC adjustment made by the caller. If the instruction
473b0846e8STim Northover /// is a branch that adds the PC to the immediate Value then isBranch is
483b0846e8STim Northover /// Success, else Fail. If GetOpInfo is non-null, then it is called to get any
493b0846e8STim Northover /// symbolic information at the Address for this instrution.  If that returns
503b0846e8STim Northover /// non-zero then the symbolic information it returns is used to create an
513b0846e8STim Northover /// MCExpr and that is added as an operand to the MCInst.  If GetOpInfo()
523b0846e8STim Northover /// returns zero and isBranch is Success then a symbol look up for
533b0846e8STim Northover /// Address + Value is done and if a symbol is found an MCExpr is created with
543b0846e8STim Northover /// that, else an MCExpr with Address + Value is created.  If GetOpInfo()
55572e03a3SEric Christopher /// returns zero and isBranch is Fail then the Opcode of the MCInst is
563b0846e8STim Northover /// tested and for ADRP an other instructions that help to load of pointers
573b0846e8STim Northover /// a symbol look up is done to see it is returns a specific reference type
583b0846e8STim Northover /// to add to the comment stream.  This function returns Success if it adds
593b0846e8STim Northover /// an operand to the MCInst and Fail otherwise.
603b0846e8STim Northover bool AArch64ExternalSymbolizer::tryAddingSymbolicOperand(
613b0846e8STim Northover     MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address,
623b0846e8STim Northover     bool IsBranch, uint64_t Offset, uint64_t InstSize) {
636a943fb1SDaniel Sanders   if (!SymbolLookUp)
646a943fb1SDaniel Sanders     return false;
653b0846e8STim Northover   // FIXME: This method shares a lot of code with
663b0846e8STim Northover   //        MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
673b0846e8STim Northover   //        refactor the MCExternalSymbolizer interface to allow more of this
683b0846e8STim Northover   //        implementation to be shared.
693b0846e8STim Northover   //
703b0846e8STim Northover   struct LLVMOpInfo1 SymbolicOp;
713b0846e8STim Northover   memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
723b0846e8STim Northover   SymbolicOp.Value = Value;
733b0846e8STim Northover   uint64_t ReferenceType;
743b0846e8STim Northover   const char *ReferenceName;
753b0846e8STim Northover   if (!GetOpInfo ||
763b0846e8STim Northover       !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) {
773b0846e8STim Northover     if (IsBranch) {
783b0846e8STim Northover       ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
793b0846e8STim Northover       const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
803b0846e8STim Northover                                       Address, &ReferenceName);
813b0846e8STim Northover       if (Name) {
823b0846e8STim Northover         SymbolicOp.AddSymbol.Name = Name;
833b0846e8STim Northover         SymbolicOp.AddSymbol.Present = true;
843b0846e8STim Northover         SymbolicOp.Value = 0;
853b0846e8STim Northover       } else {
863b0846e8STim Northover         SymbolicOp.Value = Address + Value;
873b0846e8STim Northover       }
883b0846e8STim Northover       if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
893b0846e8STim Northover         CommentStream << "symbol stub for: " << ReferenceName;
903b0846e8STim Northover       else if (ReferenceType ==
913b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Message)
923b0846e8STim Northover         CommentStream << "Objc message: " << ReferenceName;
933b0846e8STim Northover     } else if (MI.getOpcode() == AArch64::ADRP) {
943b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
953b0846e8STim Northover         // otool expects the fully encoded ADRP instruction to be passed in as
963b0846e8STim Northover         // the value here, so reconstruct it:
973b0846e8STim Northover         const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
983b0846e8STim Northover         uint32_t EncodedInst = 0x90000000;
993b0846e8STim Northover         EncodedInst |= (Value & 0x3) << 29; // immlo
1003b0846e8STim Northover         EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
1013b0846e8STim Northover         EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
1023b0846e8STim Northover         SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
1033b0846e8STim Northover                      &ReferenceName);
104d8949f50STim Northover         CommentStream << format("0x%llx", (0xfffffffffffff000LL & Address) +
105d8949f50STim Northover                                               Value * 0x1000);
1063b0846e8STim Northover     } else if (MI.getOpcode() == AArch64::ADDXri ||
1073b0846e8STim Northover                MI.getOpcode() == AArch64::LDRXui ||
1083b0846e8STim Northover                MI.getOpcode() == AArch64::LDRXl ||
1093b0846e8STim Northover                MI.getOpcode() == AArch64::ADR) {
1103b0846e8STim Northover       if (MI.getOpcode() == AArch64::ADDXri)
1113b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
1123b0846e8STim Northover       else if (MI.getOpcode() == AArch64::LDRXui)
1133b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
1143b0846e8STim Northover       if (MI.getOpcode() == AArch64::LDRXl) {
1153b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
1163b0846e8STim Northover         SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
1173b0846e8STim Northover                      &ReferenceName);
1183b0846e8STim Northover       } else if (MI.getOpcode() == AArch64::ADR) {
1193b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
1203b0846e8STim Northover         SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
1213b0846e8STim Northover                             &ReferenceName);
1223b0846e8STim Northover       } else {
1233b0846e8STim Northover         const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
1243b0846e8STim Northover         // otool expects the fully encoded ADD/LDR instruction to be passed in
1253b0846e8STim Northover         // as the value here, so reconstruct it:
1263b0846e8STim Northover         unsigned EncodedInst =
1273b0846e8STim Northover           MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000;
1283b0846e8STim Northover         EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
1293b0846e8STim Northover         EncodedInst |=
1303b0846e8STim Northover           MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
1313b0846e8STim Northover         EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
1323b0846e8STim Northover 
1333b0846e8STim Northover         SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
1343b0846e8STim Northover                      &ReferenceName);
1353b0846e8STim Northover       }
1363b0846e8STim Northover       if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
1373b0846e8STim Northover         CommentStream << "literal pool symbol address: " << ReferenceName;
1383b0846e8STim Northover       else if (ReferenceType ==
139d2d2ce9bSKevin Enderby                LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) {
140d2d2ce9bSKevin Enderby         CommentStream << "literal pool for: \"";
141d2d2ce9bSKevin Enderby         CommentStream.write_escaped(ReferenceName);
142d2d2ce9bSKevin Enderby         CommentStream << "\"";
143d2d2ce9bSKevin Enderby       } else if (ReferenceType ==
1443b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
1453b0846e8STim Northover         CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
1463b0846e8STim Northover       else if (ReferenceType ==
1473b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Message)
1483b0846e8STim Northover         CommentStream << "Objc message: " << ReferenceName;
1493b0846e8STim Northover       else if (ReferenceType ==
1503b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
1513b0846e8STim Northover         CommentStream << "Objc message ref: " << ReferenceName;
1523b0846e8STim Northover       else if (ReferenceType ==
1533b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
1543b0846e8STim Northover         CommentStream << "Objc selector ref: " << ReferenceName;
1553b0846e8STim Northover       else if (ReferenceType ==
1563b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
1573b0846e8STim Northover         CommentStream << "Objc class ref: " << ReferenceName;
1583b0846e8STim Northover       // For these instructions, the SymbolLookUp() above is just to get the
1593b0846e8STim Northover       // ReferenceType and ReferenceName.  We want to make sure not to
1603b0846e8STim Northover       // fall through so we don't build an MCExpr to leave the disassembly
1613b0846e8STim Northover       // of the immediate values of these instructions to the InstPrinter.
1623b0846e8STim Northover       return false;
1633b0846e8STim Northover     } else {
1643b0846e8STim Northover       return false;
1653b0846e8STim Northover     }
1663b0846e8STim Northover   }
1673b0846e8STim Northover 
1683b0846e8STim Northover   const MCExpr *Add = nullptr;
1693b0846e8STim Northover   if (SymbolicOp.AddSymbol.Present) {
1703b0846e8STim Northover     if (SymbolicOp.AddSymbol.Name) {
1713b0846e8STim Northover       StringRef Name(SymbolicOp.AddSymbol.Name);
1726f482000SJim Grosbach       MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
1733b0846e8STim Northover       MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
1743b0846e8STim Northover       if (Variant != MCSymbolRefExpr::VK_None)
17513760bd1SJim Grosbach         Add = MCSymbolRefExpr::create(Sym, Variant, Ctx);
1763b0846e8STim Northover       else
17713760bd1SJim Grosbach         Add = MCSymbolRefExpr::create(Sym, Ctx);
1783b0846e8STim Northover     } else {
17913760bd1SJim Grosbach       Add = MCConstantExpr::create(SymbolicOp.AddSymbol.Value, Ctx);
1803b0846e8STim Northover     }
1813b0846e8STim Northover   }
1823b0846e8STim Northover 
1833b0846e8STim Northover   const MCExpr *Sub = nullptr;
1843b0846e8STim Northover   if (SymbolicOp.SubtractSymbol.Present) {
1853b0846e8STim Northover     if (SymbolicOp.SubtractSymbol.Name) {
1863b0846e8STim Northover       StringRef Name(SymbolicOp.SubtractSymbol.Name);
1876f482000SJim Grosbach       MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
18813760bd1SJim Grosbach       Sub = MCSymbolRefExpr::create(Sym, Ctx);
1893b0846e8STim Northover     } else {
19013760bd1SJim Grosbach       Sub = MCConstantExpr::create(SymbolicOp.SubtractSymbol.Value, Ctx);
1913b0846e8STim Northover     }
1923b0846e8STim Northover   }
1933b0846e8STim Northover 
1943b0846e8STim Northover   const MCExpr *Off = nullptr;
1953b0846e8STim Northover   if (SymbolicOp.Value != 0)
19613760bd1SJim Grosbach     Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
1973b0846e8STim Northover 
1983b0846e8STim Northover   const MCExpr *Expr;
1993b0846e8STim Northover   if (Sub) {
2003b0846e8STim Northover     const MCExpr *LHS;
2013b0846e8STim Northover     if (Add)
20213760bd1SJim Grosbach       LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
2033b0846e8STim Northover     else
20413760bd1SJim Grosbach       LHS = MCUnaryExpr::createMinus(Sub, Ctx);
2053b0846e8STim Northover     if (Off)
20613760bd1SJim Grosbach       Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
2073b0846e8STim Northover     else
2083b0846e8STim Northover       Expr = LHS;
2093b0846e8STim Northover   } else if (Add) {
2103b0846e8STim Northover     if (Off)
21113760bd1SJim Grosbach       Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
2123b0846e8STim Northover     else
2133b0846e8STim Northover       Expr = Add;
2143b0846e8STim Northover   } else {
2153b0846e8STim Northover     if (Off)
2163b0846e8STim Northover       Expr = Off;
2173b0846e8STim Northover     else
21813760bd1SJim Grosbach       Expr = MCConstantExpr::create(0, Ctx);
2193b0846e8STim Northover   }
2203b0846e8STim Northover 
221e9119e41SJim Grosbach   MI.addOperand(MCOperand::createExpr(Expr));
2223b0846e8STim Northover 
2233b0846e8STim Northover   return true;
2243b0846e8STim Northover }
225