RuntimeDyldMachOARM.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //===----- RuntimeDyldMachOARM.h ---- MachO/ARM specific code. ----*- 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. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
  9. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
  10. #include "../RuntimeDyldMachO.h"
  11. #define DEBUG_TYPE "dyld"
  12. namespace llvm {
  13. class RuntimeDyldMachOARM
  14. : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
  15. private:
  16. typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
  17. public:
  18. typedef uint32_t TargetPtrT;
  19. RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM,
  20. JITSymbolResolver &Resolver)
  21. : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
  22. unsigned getMaxStubSize() const override { return 8; }
  23. unsigned getStubAlignment() override { return 4; }
  24. Expected<JITSymbolFlags> getJITSymbolFlags(const SymbolRef &SR) override {
  25. auto Flags = RuntimeDyldImpl::getJITSymbolFlags(SR);
  26. if (!Flags)
  27. return Flags.takeError();
  28. Flags->getTargetFlags() = ARMJITSymbolFlags::fromObjectSymbol(SR);
  29. return Flags;
  30. }
  31. uint64_t modifyAddressBasedOnFlags(uint64_t Addr,
  32. JITSymbolFlags Flags) const override {
  33. if (Flags.getTargetFlags() & ARMJITSymbolFlags::Thumb)
  34. Addr |= 0x1;
  35. return Addr;
  36. }
  37. bool isAddrTargetThumb(unsigned SectionID, uint64_t Offset) {
  38. auto TargetObjAddr = Sections[SectionID].getObjAddress() + Offset;
  39. for (auto &KV : GlobalSymbolTable) {
  40. auto &Entry = KV.second;
  41. auto SymbolObjAddr =
  42. Sections[Entry.getSectionID()].getObjAddress() + Entry.getOffset();
  43. if (TargetObjAddr == SymbolObjAddr)
  44. return (Entry.getFlags().getTargetFlags() & ARMJITSymbolFlags::Thumb);
  45. }
  46. return false;
  47. }
  48. Expected<int64_t> decodeAddend(const RelocationEntry &RE) const {
  49. const SectionEntry &Section = Sections[RE.SectionID];
  50. uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
  51. switch (RE.RelType) {
  52. default:
  53. return memcpyAddend(RE);
  54. case MachO::ARM_RELOC_BR24: {
  55. uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
  56. Temp &= 0x00ffffff; // Mask out the opcode.
  57. // Now we've got the shifted immediate, shift by 2, sign extend and ret.
  58. return SignExtend32<26>(Temp << 2);
  59. }
  60. case MachO::ARM_THUMB_RELOC_BR22: {
  61. // This is a pair of instructions whose operands combine to provide 22
  62. // bits of displacement:
  63. // Encoding for high bits 1111 0XXX XXXX XXXX
  64. // Encoding for low bits 1111 1XXX XXXX XXXX
  65. uint16_t HighInsn = readBytesUnaligned(LocalAddress, 2);
  66. if ((HighInsn & 0xf800) != 0xf000)
  67. return make_error<StringError>("Unrecognized thumb branch encoding "
  68. "(BR22 high bits)",
  69. inconvertibleErrorCode());
  70. uint16_t LowInsn = readBytesUnaligned(LocalAddress + 2, 2);
  71. if ((LowInsn & 0xf800) != 0xf800)
  72. return make_error<StringError>("Unrecognized thumb branch encoding "
  73. "(BR22 low bits)",
  74. inconvertibleErrorCode());
  75. return SignExtend64<23>(((HighInsn & 0x7ff) << 12) |
  76. ((LowInsn & 0x7ff) << 1));
  77. }
  78. }
  79. }
  80. Expected<relocation_iterator>
  81. processRelocationRef(unsigned SectionID, relocation_iterator RelI,
  82. const ObjectFile &BaseObjT,
  83. ObjSectionToIDMap &ObjSectionToID,
  84. StubMap &Stubs) override {
  85. const MachOObjectFile &Obj =
  86. static_cast<const MachOObjectFile &>(BaseObjT);
  87. MachO::any_relocation_info RelInfo =
  88. Obj.getRelocation(RelI->getRawDataRefImpl());
  89. uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
  90. // Set to true for thumb functions in this (or previous) TUs.
  91. // Will be used to set the TargetIsThumbFunc member on the relocation entry.
  92. bool TargetIsLocalThumbFunc = false;
  93. if (Obj.getPlainRelocationExternal(RelInfo)) {
  94. auto Symbol = RelI->getSymbol();
  95. StringRef TargetName;
  96. if (auto TargetNameOrErr = Symbol->getName())
  97. TargetName = *TargetNameOrErr;
  98. else
  99. return TargetNameOrErr.takeError();
  100. // If the target is external but the value doesn't have a name then we've
  101. // converted the value to a section/offset pair, but we still need to set
  102. // the IsTargetThumbFunc bit, so look the value up in the globla symbol table.
  103. auto EntryItr = GlobalSymbolTable.find(TargetName);
  104. if (EntryItr != GlobalSymbolTable.end()) {
  105. TargetIsLocalThumbFunc =
  106. EntryItr->second.getFlags().getTargetFlags() &
  107. ARMJITSymbolFlags::Thumb;
  108. }
  109. }
  110. if (Obj.isRelocationScattered(RelInfo)) {
  111. if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF)
  112. return processHALFSECTDIFFRelocation(SectionID, RelI, Obj,
  113. ObjSectionToID);
  114. else if (RelType == MachO::GENERIC_RELOC_VANILLA)
  115. return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID,
  116. TargetIsLocalThumbFunc);
  117. else
  118. return ++RelI;
  119. }
  120. // Validate the relocation type.
  121. switch (RelType) {
  122. UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PAIR);
  123. UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_SECTDIFF);
  124. UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_LOCAL_SECTDIFF);
  125. UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PB_LA_PTR);
  126. UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_32BIT_BRANCH);
  127. UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_HALF);
  128. default:
  129. if (RelType > MachO::ARM_RELOC_HALF_SECTDIFF)
  130. return make_error<RuntimeDyldError>(("MachO ARM relocation type " +
  131. Twine(RelType) +
  132. " is out of range").str());
  133. break;
  134. }
  135. RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
  136. if (auto AddendOrErr = decodeAddend(RE))
  137. RE.Addend = *AddendOrErr;
  138. else
  139. return AddendOrErr.takeError();
  140. RE.IsTargetThumbFunc = TargetIsLocalThumbFunc;
  141. RelocationValueRef Value;
  142. if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
  143. Value = *ValueOrErr;
  144. else
  145. return ValueOrErr.takeError();
  146. // If this is a branch from a thumb function (BR22) then make sure we mark
  147. // the value as being a thumb stub: we don't want to mix it up with an ARM
  148. // stub targeting the same function.
  149. if (RE.RelType == MachO::ARM_THUMB_RELOC_BR22)
  150. Value.IsStubThumb = true;
  151. if (RE.IsPCRel)
  152. makeValueAddendPCRel(Value, RelI,
  153. (RE.RelType == MachO::ARM_THUMB_RELOC_BR22) ? 4 : 8);
  154. // If this is a non-external branch target check whether Value points to a
  155. // thumb func.
  156. if (!Value.SymbolName && (RelType == MachO::ARM_RELOC_BR24 ||
  157. RelType == MachO::ARM_THUMB_RELOC_BR22))
  158. RE.IsTargetThumbFunc = isAddrTargetThumb(Value.SectionID, Value.Offset);
  159. if (RE.RelType == MachO::ARM_RELOC_BR24 ||
  160. RE.RelType == MachO::ARM_THUMB_RELOC_BR22)
  161. processBranchRelocation(RE, Value, Stubs);
  162. else {
  163. RE.Addend = Value.Offset;
  164. if (Value.SymbolName)
  165. addRelocationForSymbol(RE, Value.SymbolName);
  166. else
  167. addRelocationForSection(RE, Value.SectionID);
  168. }
  169. return ++RelI;
  170. }
  171. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
  172. LLVM_DEBUG(dumpRelocationToResolve(RE, Value));
  173. const SectionEntry &Section = Sections[RE.SectionID];
  174. uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
  175. // If the relocation is PC-relative, the value to be encoded is the
  176. // pointer difference.
  177. if (RE.IsPCRel) {
  178. uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
  179. Value -= FinalAddress;
  180. // ARM PCRel relocations have an effective-PC offset of two instructions
  181. // (four bytes in Thumb mode, 8 bytes in ARM mode).
  182. Value -= (RE.RelType == MachO::ARM_THUMB_RELOC_BR22) ? 4 : 8;
  183. }
  184. switch (RE.RelType) {
  185. case MachO::ARM_THUMB_RELOC_BR22: {
  186. Value += RE.Addend;
  187. uint16_t HighInsn = readBytesUnaligned(LocalAddress, 2);
  188. assert((HighInsn & 0xf800) == 0xf000 &&
  189. "Unrecognized thumb branch encoding (BR22 high bits)");
  190. HighInsn = (HighInsn & 0xf800) | ((Value >> 12) & 0x7ff);
  191. uint16_t LowInsn = readBytesUnaligned(LocalAddress + 2, 2);
  192. assert((LowInsn & 0xf800) == 0xf800 &&
  193. "Unrecognized thumb branch encoding (BR22 low bits)");
  194. LowInsn = (LowInsn & 0xf800) | ((Value >> 1) & 0x7ff);
  195. writeBytesUnaligned(HighInsn, LocalAddress, 2);
  196. writeBytesUnaligned(LowInsn, LocalAddress + 2, 2);
  197. break;
  198. }
  199. case MachO::ARM_RELOC_VANILLA:
  200. if (RE.IsTargetThumbFunc)
  201. Value |= 0x01;
  202. writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
  203. break;
  204. case MachO::ARM_RELOC_BR24: {
  205. // Mask the value into the target address. We know instructions are
  206. // 32-bit aligned, so we can do it all at once.
  207. Value += RE.Addend;
  208. // The low two bits of the value are not encoded.
  209. Value >>= 2;
  210. // Mask the value to 24 bits.
  211. uint64_t FinalValue = Value & 0xffffff;
  212. // FIXME: If the destination is a Thumb function (and the instruction
  213. // is a non-predicated BL instruction), we need to change it to a BLX
  214. // instruction instead.
  215. // Insert the value into the instruction.
  216. uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
  217. writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4);
  218. break;
  219. }
  220. case MachO::ARM_RELOC_HALF_SECTDIFF: {
  221. uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
  222. uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
  223. assert((Value == SectionABase || Value == SectionBBase) &&
  224. "Unexpected HALFSECTDIFF relocation value.");
  225. Value = SectionABase - SectionBBase + RE.Addend;
  226. if (RE.Size & 0x1) // :upper16:
  227. Value = (Value >> 16);
  228. bool IsThumb = RE.Size & 0x2;
  229. Value &= 0xffff;
  230. uint32_t Insn = readBytesUnaligned(LocalAddress, 4);
  231. if (IsThumb)
  232. Insn = (Insn & 0x8f00fbf0) | ((Value & 0xf000) >> 12) |
  233. ((Value & 0x0800) >> 1) | ((Value & 0x0700) << 20) |
  234. ((Value & 0x00ff) << 16);
  235. else
  236. Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
  237. writeBytesUnaligned(Insn, LocalAddress, 4);
  238. break;
  239. }
  240. default:
  241. llvm_unreachable("Invalid relocation type");
  242. }
  243. }
  244. Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
  245. const SectionRef &Section) {
  246. StringRef Name;
  247. if (Expected<StringRef> NameOrErr = Section.getName())
  248. Name = *NameOrErr;
  249. else
  250. consumeError(NameOrErr.takeError());
  251. if (Name == "__nl_symbol_ptr")
  252. return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
  253. Section, SectionID);
  254. return Error::success();
  255. }
  256. private:
  257. void processBranchRelocation(const RelocationEntry &RE,
  258. const RelocationValueRef &Value,
  259. StubMap &Stubs) {
  260. // This is an ARM branch relocation, need to use a stub function.
  261. // Look up for existing stub.
  262. SectionEntry &Section = Sections[RE.SectionID];
  263. RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
  264. uint8_t *Addr;
  265. if (i != Stubs.end()) {
  266. Addr = Section.getAddressWithOffset(i->second);
  267. } else {
  268. // Create a new stub function.
  269. assert(Section.getStubOffset() % 4 == 0 && "Misaligned stub");
  270. Stubs[Value] = Section.getStubOffset();
  271. uint32_t StubOpcode = 0;
  272. if (RE.RelType == MachO::ARM_RELOC_BR24)
  273. StubOpcode = 0xe51ff004; // ldr pc, [pc, #-4]
  274. else if (RE.RelType == MachO::ARM_THUMB_RELOC_BR22)
  275. StubOpcode = 0xf000f8df; // ldr pc, [pc]
  276. else
  277. llvm_unreachable("Unrecognized relocation");
  278. Addr = Section.getAddressWithOffset(Section.getStubOffset());
  279. writeBytesUnaligned(StubOpcode, Addr, 4);
  280. uint8_t *StubTargetAddr = Addr + 4;
  281. RelocationEntry StubRE(
  282. RE.SectionID, StubTargetAddr - Section.getAddress(),
  283. MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, 2);
  284. StubRE.IsTargetThumbFunc = RE.IsTargetThumbFunc;
  285. if (Value.SymbolName)
  286. addRelocationForSymbol(StubRE, Value.SymbolName);
  287. else
  288. addRelocationForSection(StubRE, Value.SectionID);
  289. Section.advanceStubOffset(getMaxStubSize());
  290. }
  291. RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
  292. RE.IsPCRel, RE.Size);
  293. resolveRelocation(TargetRE, (uint64_t)Addr);
  294. }
  295. Expected<relocation_iterator>
  296. processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
  297. const ObjectFile &BaseTObj,
  298. ObjSectionToIDMap &ObjSectionToID) {
  299. const MachOObjectFile &MachO =
  300. static_cast<const MachOObjectFile&>(BaseTObj);
  301. MachO::any_relocation_info RE =
  302. MachO.getRelocation(RelI->getRawDataRefImpl());
  303. // For a half-diff relocation the length bits actually record whether this
  304. // is a movw/movt, and whether this is arm or thumb.
  305. // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
  306. // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
  307. unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE);
  308. bool IsThumb = HalfDiffKindBits & 0x2;
  309. SectionEntry &Section = Sections[SectionID];
  310. uint32_t RelocType = MachO.getAnyRelocationType(RE);
  311. bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
  312. uint64_t Offset = RelI->getOffset();
  313. uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
  314. int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
  315. if (IsThumb)
  316. Immediate = ((Immediate & 0x0000000f) << 12) |
  317. ((Immediate & 0x00000400) << 1) |
  318. ((Immediate & 0x70000000) >> 20) |
  319. ((Immediate & 0x00ff0000) >> 16);
  320. else
  321. Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
  322. ++RelI;
  323. MachO::any_relocation_info RE2 =
  324. MachO.getRelocation(RelI->getRawDataRefImpl());
  325. uint32_t AddrA = MachO.getScatteredRelocationValue(RE);
  326. section_iterator SAI = getSectionByAddress(MachO, AddrA);
  327. assert(SAI != MachO.section_end() && "Can't find section for address A");
  328. uint64_t SectionABase = SAI->getAddress();
  329. uint64_t SectionAOffset = AddrA - SectionABase;
  330. SectionRef SectionA = *SAI;
  331. bool IsCode = SectionA.isText();
  332. uint32_t SectionAID = ~0U;
  333. if (auto SectionAIDOrErr =
  334. findOrEmitSection(MachO, SectionA, IsCode, ObjSectionToID))
  335. SectionAID = *SectionAIDOrErr;
  336. else
  337. return SectionAIDOrErr.takeError();
  338. uint32_t AddrB = MachO.getScatteredRelocationValue(RE2);
  339. section_iterator SBI = getSectionByAddress(MachO, AddrB);
  340. assert(SBI != MachO.section_end() && "Can't find section for address B");
  341. uint64_t SectionBBase = SBI->getAddress();
  342. uint64_t SectionBOffset = AddrB - SectionBBase;
  343. SectionRef SectionB = *SBI;
  344. uint32_t SectionBID = ~0U;
  345. if (auto SectionBIDOrErr =
  346. findOrEmitSection(MachO, SectionB, IsCode, ObjSectionToID))
  347. SectionBID = *SectionBIDOrErr;
  348. else
  349. return SectionBIDOrErr.takeError();
  350. uint32_t OtherHalf = MachO.getAnyRelocationAddress(RE2) & 0xffff;
  351. unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
  352. uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
  353. int64_t Addend = FullImmVal - (AddrA - AddrB);
  354. // addend = Encoded - Expected
  355. // = Encoded - (AddrA - AddrB)
  356. LLVM_DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA
  357. << ", AddrB: " << AddrB << ", Addend: " << Addend
  358. << ", SectionA ID: " << SectionAID << ", SectionAOffset: "
  359. << SectionAOffset << ", SectionB ID: " << SectionBID
  360. << ", SectionBOffset: " << SectionBOffset << "\n");
  361. RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
  362. SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
  363. HalfDiffKindBits);
  364. addRelocationForSection(R, SectionAID);
  365. return ++RelI;
  366. }
  367. };
  368. }
  369. #undef DEBUG_TYPE
  370. #endif