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 "AArch64Subtarget.h"
123b0846e8STim Northover #include "MCTargetDesc/AArch64AddressingModes.h"
133b0846e8STim Northover #include "Utils/AArch64BaseInfo.h"
143b0846e8STim Northover #include "llvm/MC/MCContext.h"
153b0846e8STim Northover #include "llvm/MC/MCExpr.h"
163b0846e8STim Northover #include "llvm/MC/MCInst.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) {
633b0846e8STim Northover   // FIXME: This method shares a lot of code with
643b0846e8STim Northover   //        MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
653b0846e8STim Northover   //        refactor the MCExternalSymbolizer interface to allow more of this
663b0846e8STim Northover   //        implementation to be shared.
673b0846e8STim Northover   //
683b0846e8STim Northover   struct LLVMOpInfo1 SymbolicOp;
693b0846e8STim Northover   memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
703b0846e8STim Northover   SymbolicOp.Value = Value;
713b0846e8STim Northover   uint64_t ReferenceType;
723b0846e8STim Northover   const char *ReferenceName;
733b0846e8STim Northover   if (!GetOpInfo ||
743b0846e8STim Northover       !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) {
753b0846e8STim Northover     if (IsBranch) {
763b0846e8STim Northover       ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
773b0846e8STim Northover       const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
783b0846e8STim Northover                                       Address, &ReferenceName);
793b0846e8STim Northover       if (Name) {
803b0846e8STim Northover         SymbolicOp.AddSymbol.Name = Name;
813b0846e8STim Northover         SymbolicOp.AddSymbol.Present = true;
823b0846e8STim Northover         SymbolicOp.Value = 0;
833b0846e8STim Northover       } else {
843b0846e8STim Northover         SymbolicOp.Value = Address + Value;
853b0846e8STim Northover       }
863b0846e8STim Northover       if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
873b0846e8STim Northover         CommentStream << "symbol stub for: " << ReferenceName;
883b0846e8STim Northover       else if (ReferenceType ==
893b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Message)
903b0846e8STim Northover         CommentStream << "Objc message: " << ReferenceName;
913b0846e8STim Northover     } else if (MI.getOpcode() == AArch64::ADRP) {
923b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
933b0846e8STim Northover         // otool expects the fully encoded ADRP instruction to be passed in as
943b0846e8STim Northover         // the value here, so reconstruct it:
953b0846e8STim Northover         const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
963b0846e8STim Northover         uint32_t EncodedInst = 0x90000000;
973b0846e8STim Northover         EncodedInst |= (Value & 0x3) << 29; // immlo
983b0846e8STim Northover         EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
993b0846e8STim Northover         EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
1003b0846e8STim Northover         SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
1013b0846e8STim Northover                      &ReferenceName);
1023b0846e8STim Northover         CommentStream << format("0x%llx",
1033b0846e8STim Northover                                 0xfffffffffffff000LL & (Address + Value));
1043b0846e8STim Northover     } else if (MI.getOpcode() == AArch64::ADDXri ||
1053b0846e8STim Northover                MI.getOpcode() == AArch64::LDRXui ||
1063b0846e8STim Northover                MI.getOpcode() == AArch64::LDRXl ||
1073b0846e8STim Northover                MI.getOpcode() == AArch64::ADR) {
1083b0846e8STim Northover       if (MI.getOpcode() == AArch64::ADDXri)
1093b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
1103b0846e8STim Northover       else if (MI.getOpcode() == AArch64::LDRXui)
1113b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
1123b0846e8STim Northover       if (MI.getOpcode() == AArch64::LDRXl) {
1133b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
1143b0846e8STim Northover         SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
1153b0846e8STim Northover                      &ReferenceName);
1163b0846e8STim Northover       } else if (MI.getOpcode() == AArch64::ADR) {
1173b0846e8STim Northover         ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
1183b0846e8STim Northover         SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
1193b0846e8STim Northover                             &ReferenceName);
1203b0846e8STim Northover       } else {
1213b0846e8STim Northover         const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
1223b0846e8STim Northover         // otool expects the fully encoded ADD/LDR instruction to be passed in
1233b0846e8STim Northover         // as the value here, so reconstruct it:
1243b0846e8STim Northover         unsigned EncodedInst =
1253b0846e8STim Northover           MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000;
1263b0846e8STim Northover         EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
1273b0846e8STim Northover         EncodedInst |=
1283b0846e8STim Northover           MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
1293b0846e8STim Northover         EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
1303b0846e8STim Northover 
1313b0846e8STim Northover         SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
1323b0846e8STim Northover                      &ReferenceName);
1333b0846e8STim Northover       }
1343b0846e8STim Northover       if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
1353b0846e8STim Northover         CommentStream << "literal pool symbol address: " << ReferenceName;
1363b0846e8STim Northover       else if (ReferenceType ==
137*d2d2ce9bSKevin Enderby                LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) {
138*d2d2ce9bSKevin Enderby         CommentStream << "literal pool for: \"";
139*d2d2ce9bSKevin Enderby 	CommentStream.write_escaped(ReferenceName);
140*d2d2ce9bSKevin Enderby         CommentStream << "\"";
141*d2d2ce9bSKevin Enderby       } else if (ReferenceType ==
1423b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
1433b0846e8STim Northover         CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
1443b0846e8STim Northover       else if (ReferenceType ==
1453b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Message)
1463b0846e8STim Northover         CommentStream << "Objc message: " << ReferenceName;
1473b0846e8STim Northover       else if (ReferenceType ==
1483b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
1493b0846e8STim Northover         CommentStream << "Objc message ref: " << ReferenceName;
1503b0846e8STim Northover       else if (ReferenceType ==
1513b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
1523b0846e8STim Northover         CommentStream << "Objc selector ref: " << ReferenceName;
1533b0846e8STim Northover       else if (ReferenceType ==
1543b0846e8STim Northover                LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
1553b0846e8STim Northover         CommentStream << "Objc class ref: " << ReferenceName;
1563b0846e8STim Northover       // For these instructions, the SymbolLookUp() above is just to get the
1573b0846e8STim Northover       // ReferenceType and ReferenceName.  We want to make sure not to
1583b0846e8STim Northover       // fall through so we don't build an MCExpr to leave the disassembly
1593b0846e8STim Northover       // of the immediate values of these instructions to the InstPrinter.
1603b0846e8STim Northover       return false;
1613b0846e8STim Northover     } else {
1623b0846e8STim Northover       return false;
1633b0846e8STim Northover     }
1643b0846e8STim Northover   }
1653b0846e8STim Northover 
1663b0846e8STim Northover   const MCExpr *Add = nullptr;
1673b0846e8STim Northover   if (SymbolicOp.AddSymbol.Present) {
1683b0846e8STim Northover     if (SymbolicOp.AddSymbol.Name) {
1693b0846e8STim Northover       StringRef Name(SymbolicOp.AddSymbol.Name);
1706f482000SJim Grosbach       MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
1713b0846e8STim Northover       MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
1723b0846e8STim Northover       if (Variant != MCSymbolRefExpr::VK_None)
17313760bd1SJim Grosbach         Add = MCSymbolRefExpr::create(Sym, Variant, Ctx);
1743b0846e8STim Northover       else
17513760bd1SJim Grosbach         Add = MCSymbolRefExpr::create(Sym, Ctx);
1763b0846e8STim Northover     } else {
17713760bd1SJim Grosbach       Add = MCConstantExpr::create(SymbolicOp.AddSymbol.Value, Ctx);
1783b0846e8STim Northover     }
1793b0846e8STim Northover   }
1803b0846e8STim Northover 
1813b0846e8STim Northover   const MCExpr *Sub = nullptr;
1823b0846e8STim Northover   if (SymbolicOp.SubtractSymbol.Present) {
1833b0846e8STim Northover     if (SymbolicOp.SubtractSymbol.Name) {
1843b0846e8STim Northover       StringRef Name(SymbolicOp.SubtractSymbol.Name);
1856f482000SJim Grosbach       MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
18613760bd1SJim Grosbach       Sub = MCSymbolRefExpr::create(Sym, Ctx);
1873b0846e8STim Northover     } else {
18813760bd1SJim Grosbach       Sub = MCConstantExpr::create(SymbolicOp.SubtractSymbol.Value, Ctx);
1893b0846e8STim Northover     }
1903b0846e8STim Northover   }
1913b0846e8STim Northover 
1923b0846e8STim Northover   const MCExpr *Off = nullptr;
1933b0846e8STim Northover   if (SymbolicOp.Value != 0)
19413760bd1SJim Grosbach     Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
1953b0846e8STim Northover 
1963b0846e8STim Northover   const MCExpr *Expr;
1973b0846e8STim Northover   if (Sub) {
1983b0846e8STim Northover     const MCExpr *LHS;
1993b0846e8STim Northover     if (Add)
20013760bd1SJim Grosbach       LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
2013b0846e8STim Northover     else
20213760bd1SJim Grosbach       LHS = MCUnaryExpr::createMinus(Sub, Ctx);
2033b0846e8STim Northover     if (Off)
20413760bd1SJim Grosbach       Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
2053b0846e8STim Northover     else
2063b0846e8STim Northover       Expr = LHS;
2073b0846e8STim Northover   } else if (Add) {
2083b0846e8STim Northover     if (Off)
20913760bd1SJim Grosbach       Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
2103b0846e8STim Northover     else
2113b0846e8STim Northover       Expr = Add;
2123b0846e8STim Northover   } else {
2133b0846e8STim Northover     if (Off)
2143b0846e8STim Northover       Expr = Off;
2153b0846e8STim Northover     else
21613760bd1SJim Grosbach       Expr = MCConstantExpr::create(0, Ctx);
2173b0846e8STim Northover   }
2183b0846e8STim Northover 
219e9119e41SJim Grosbach   MI.addOperand(MCOperand::createExpr(Expr));
2203b0846e8STim Northover 
2213b0846e8STim Northover   return true;
2223b0846e8STim Northover }
223