AArch64ExternalSymbolizer.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //===- AArch64ExternalSymbolizer.cpp - Symbolizer for AArch64 ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "AArch64ExternalSymbolizer.h"
  9. #include "MCTargetDesc/AArch64AddressingModes.h"
  10. #include "Utils/AArch64BaseInfo.h"
  11. #include "llvm/MC/MCContext.h"
  12. #include "llvm/MC/MCExpr.h"
  13. #include "llvm/MC/MCInst.h"
  14. #include "llvm/MC/MCRegisterInfo.h"
  15. #include "llvm/Support/Format.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace llvm;
  18. #define DEBUG_TYPE "aarch64-disassembler"
  19. static MCSymbolRefExpr::VariantKind
  20. getVariant(uint64_t LLVMDisassembler_VariantKind) {
  21. switch (LLVMDisassembler_VariantKind) {
  22. case LLVMDisassembler_VariantKind_None:
  23. return MCSymbolRefExpr::VK_None;
  24. case LLVMDisassembler_VariantKind_ARM64_PAGE:
  25. return MCSymbolRefExpr::VK_PAGE;
  26. case LLVMDisassembler_VariantKind_ARM64_PAGEOFF:
  27. return MCSymbolRefExpr::VK_PAGEOFF;
  28. case LLVMDisassembler_VariantKind_ARM64_GOTPAGE:
  29. return MCSymbolRefExpr::VK_GOTPAGE;
  30. case LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF:
  31. return MCSymbolRefExpr::VK_GOTPAGEOFF;
  32. case LLVMDisassembler_VariantKind_ARM64_TLVP:
  33. return MCSymbolRefExpr::VK_TLVPPAGE;
  34. case LLVMDisassembler_VariantKind_ARM64_TLVOFF:
  35. return MCSymbolRefExpr::VK_TLVPPAGEOFF;
  36. default:
  37. llvm_unreachable("bad LLVMDisassembler_VariantKind");
  38. }
  39. }
  40. /// tryAddingSymbolicOperand - tryAddingSymbolicOperand trys to add a symbolic
  41. /// operand in place of the immediate Value in the MCInst. The immediate
  42. /// Value has not had any PC adjustment made by the caller. If the instruction
  43. /// is a branch that adds the PC to the immediate Value then isBranch is
  44. /// Success, else Fail. If GetOpInfo is non-null, then it is called to get any
  45. /// symbolic information at the Address for this instrution. If that returns
  46. /// non-zero then the symbolic information it returns is used to create an
  47. /// MCExpr and that is added as an operand to the MCInst. If GetOpInfo()
  48. /// returns zero and isBranch is Success then a symbol look up for
  49. /// Address + Value is done and if a symbol is found an MCExpr is created with
  50. /// that, else an MCExpr with Address + Value is created. If GetOpInfo()
  51. /// returns zero and isBranch is Fail then the Opcode of the MCInst is
  52. /// tested and for ADRP an other instructions that help to load of pointers
  53. /// a symbol look up is done to see it is returns a specific reference type
  54. /// to add to the comment stream. This function returns Success if it adds
  55. /// an operand to the MCInst and Fail otherwise.
  56. bool AArch64ExternalSymbolizer::tryAddingSymbolicOperand(
  57. MCInst &MI, raw_ostream &CommentStream, int64_t Value, uint64_t Address,
  58. bool IsBranch, uint64_t Offset, uint64_t InstSize) {
  59. if (!SymbolLookUp)
  60. return false;
  61. // FIXME: This method shares a lot of code with
  62. // MCExternalSymbolizer::tryAddingSymbolicOperand. It may be possible
  63. // refactor the MCExternalSymbolizer interface to allow more of this
  64. // implementation to be shared.
  65. //
  66. struct LLVMOpInfo1 SymbolicOp;
  67. memset(&SymbolicOp, '\0', sizeof(struct LLVMOpInfo1));
  68. SymbolicOp.Value = Value;
  69. uint64_t ReferenceType;
  70. const char *ReferenceName;
  71. if (!GetOpInfo ||
  72. !GetOpInfo(DisInfo, Address, 0 /* Offset */, InstSize, 1, &SymbolicOp)) {
  73. if (IsBranch) {
  74. ReferenceType = LLVMDisassembler_ReferenceType_In_Branch;
  75. const char *Name = SymbolLookUp(DisInfo, Address + Value, &ReferenceType,
  76. Address, &ReferenceName);
  77. if (Name) {
  78. SymbolicOp.AddSymbol.Name = Name;
  79. SymbolicOp.AddSymbol.Present = true;
  80. SymbolicOp.Value = 0;
  81. } else {
  82. SymbolicOp.Value = Address + Value;
  83. }
  84. if (ReferenceType == LLVMDisassembler_ReferenceType_Out_SymbolStub)
  85. CommentStream << "symbol stub for: " << ReferenceName;
  86. else if (ReferenceType ==
  87. LLVMDisassembler_ReferenceType_Out_Objc_Message)
  88. CommentStream << "Objc message: " << ReferenceName;
  89. } else if (MI.getOpcode() == AArch64::ADRP) {
  90. ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADRP;
  91. // otool expects the fully encoded ADRP instruction to be passed in as
  92. // the value here, so reconstruct it:
  93. const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
  94. uint32_t EncodedInst = 0x90000000;
  95. EncodedInst |= (Value & 0x3) << 29; // immlo
  96. EncodedInst |= ((Value >> 2) & 0x7FFFF) << 5; // immhi
  97. EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // reg
  98. SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
  99. &ReferenceName);
  100. CommentStream << format("0x%llx", (0xfffffffffffff000LL & Address) +
  101. Value * 0x1000);
  102. } else if (MI.getOpcode() == AArch64::ADDXri ||
  103. MI.getOpcode() == AArch64::LDRXui ||
  104. MI.getOpcode() == AArch64::LDRXl ||
  105. MI.getOpcode() == AArch64::ADR) {
  106. if (MI.getOpcode() == AArch64::ADDXri)
  107. ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADDXri;
  108. else if (MI.getOpcode() == AArch64::LDRXui)
  109. ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXui;
  110. if (MI.getOpcode() == AArch64::LDRXl) {
  111. ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_LDRXl;
  112. SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
  113. &ReferenceName);
  114. } else if (MI.getOpcode() == AArch64::ADR) {
  115. ReferenceType = LLVMDisassembler_ReferenceType_In_ARM64_ADR;
  116. SymbolLookUp(DisInfo, Address + Value, &ReferenceType, Address,
  117. &ReferenceName);
  118. } else {
  119. const MCRegisterInfo &MCRI = *Ctx.getRegisterInfo();
  120. // otool expects the fully encoded ADD/LDR instruction to be passed in
  121. // as the value here, so reconstruct it:
  122. unsigned EncodedInst =
  123. MI.getOpcode() == AArch64::ADDXri ? 0x91000000: 0xF9400000;
  124. EncodedInst |= Value << 10; // imm12 [+ shift:2 for ADD]
  125. EncodedInst |=
  126. MCRI.getEncodingValue(MI.getOperand(1).getReg()) << 5; // Rn
  127. EncodedInst |= MCRI.getEncodingValue(MI.getOperand(0).getReg()); // Rd
  128. SymbolLookUp(DisInfo, EncodedInst, &ReferenceType, Address,
  129. &ReferenceName);
  130. }
  131. if (ReferenceType == LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr)
  132. CommentStream << "literal pool symbol address: " << ReferenceName;
  133. else if (ReferenceType ==
  134. LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr) {
  135. CommentStream << "literal pool for: \"";
  136. CommentStream.write_escaped(ReferenceName);
  137. CommentStream << "\"";
  138. } else if (ReferenceType ==
  139. LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref)
  140. CommentStream << "Objc cfstring ref: @\"" << ReferenceName << "\"";
  141. else if (ReferenceType ==
  142. LLVMDisassembler_ReferenceType_Out_Objc_Message)
  143. CommentStream << "Objc message: " << ReferenceName;
  144. else if (ReferenceType ==
  145. LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref)
  146. CommentStream << "Objc message ref: " << ReferenceName;
  147. else if (ReferenceType ==
  148. LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref)
  149. CommentStream << "Objc selector ref: " << ReferenceName;
  150. else if (ReferenceType ==
  151. LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref)
  152. CommentStream << "Objc class ref: " << ReferenceName;
  153. // For these instructions, the SymbolLookUp() above is just to get the
  154. // ReferenceType and ReferenceName. We want to make sure not to
  155. // fall through so we don't build an MCExpr to leave the disassembly
  156. // of the immediate values of these instructions to the InstPrinter.
  157. return false;
  158. } else {
  159. return false;
  160. }
  161. }
  162. const MCExpr *Add = nullptr;
  163. if (SymbolicOp.AddSymbol.Present) {
  164. if (SymbolicOp.AddSymbol.Name) {
  165. StringRef Name(SymbolicOp.AddSymbol.Name);
  166. MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
  167. MCSymbolRefExpr::VariantKind Variant = getVariant(SymbolicOp.VariantKind);
  168. if (Variant != MCSymbolRefExpr::VK_None)
  169. Add = MCSymbolRefExpr::create(Sym, Variant, Ctx);
  170. else
  171. Add = MCSymbolRefExpr::create(Sym, Ctx);
  172. } else {
  173. Add = MCConstantExpr::create(SymbolicOp.AddSymbol.Value, Ctx);
  174. }
  175. }
  176. const MCExpr *Sub = nullptr;
  177. if (SymbolicOp.SubtractSymbol.Present) {
  178. if (SymbolicOp.SubtractSymbol.Name) {
  179. StringRef Name(SymbolicOp.SubtractSymbol.Name);
  180. MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
  181. Sub = MCSymbolRefExpr::create(Sym, Ctx);
  182. } else {
  183. Sub = MCConstantExpr::create(SymbolicOp.SubtractSymbol.Value, Ctx);
  184. }
  185. }
  186. const MCExpr *Off = nullptr;
  187. if (SymbolicOp.Value != 0)
  188. Off = MCConstantExpr::create(SymbolicOp.Value, Ctx);
  189. const MCExpr *Expr;
  190. if (Sub) {
  191. const MCExpr *LHS;
  192. if (Add)
  193. LHS = MCBinaryExpr::createSub(Add, Sub, Ctx);
  194. else
  195. LHS = MCUnaryExpr::createMinus(Sub, Ctx);
  196. if (Off)
  197. Expr = MCBinaryExpr::createAdd(LHS, Off, Ctx);
  198. else
  199. Expr = LHS;
  200. } else if (Add) {
  201. if (Off)
  202. Expr = MCBinaryExpr::createAdd(Add, Off, Ctx);
  203. else
  204. Expr = Add;
  205. } else {
  206. if (Off)
  207. Expr = Off;
  208. else
  209. Expr = MCConstantExpr::create(0, Ctx);
  210. }
  211. MI.addOperand(MCOperand::createExpr(Expr));
  212. return true;
  213. }