RuntimeDyldMachOARM.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //===----- RuntimeDyldMachOARM.h ---- MachO/ARM specific code. ----*- C++ -*-=//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
  10. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
  11. #include "../RuntimeDyldMachO.h"
  12. #define DEBUG_TYPE "dyld"
  13. namespace llvm {
  14. class RuntimeDyldMachOARM
  15. : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
  16. private:
  17. typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
  18. public:
  19. typedef uint32_t TargetPtrT;
  20. RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM,
  21. RuntimeDyld::SymbolResolver &Resolver)
  22. : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
  23. unsigned getMaxStubSize() override { return 8; }
  24. unsigned getStubAlignment() override { return 4; }
  25. int64_t decodeAddend(const RelocationEntry &RE) const {
  26. const SectionEntry &Section = Sections[RE.SectionID];
  27. uint8_t *LocalAddress = Section.Address + RE.Offset;
  28. switch (RE.RelType) {
  29. default:
  30. return memcpyAddend(RE);
  31. case MachO::ARM_RELOC_BR24: {
  32. uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
  33. Temp &= 0x00ffffff; // Mask out the opcode.
  34. // Now we've got the shifted immediate, shift by 2, sign extend and ret.
  35. return SignExtend32<26>(Temp << 2);
  36. }
  37. }
  38. }
  39. relocation_iterator
  40. processRelocationRef(unsigned SectionID, relocation_iterator RelI,
  41. const ObjectFile &BaseObjT,
  42. ObjSectionToIDMap &ObjSectionToID,
  43. StubMap &Stubs) override {
  44. const MachOObjectFile &Obj =
  45. static_cast<const MachOObjectFile &>(BaseObjT);
  46. MachO::any_relocation_info RelInfo =
  47. Obj.getRelocation(RelI->getRawDataRefImpl());
  48. uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
  49. if (Obj.isRelocationScattered(RelInfo)) {
  50. if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF)
  51. return processHALFSECTDIFFRelocation(SectionID, RelI, Obj,
  52. ObjSectionToID);
  53. else
  54. return ++++RelI;
  55. }
  56. RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
  57. RE.Addend = decodeAddend(RE);
  58. RelocationValueRef Value(
  59. getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
  60. if (RE.IsPCRel)
  61. makeValueAddendPCRel(Value, RelI, 8);
  62. if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
  63. processBranchRelocation(RE, Value, Stubs);
  64. else {
  65. RE.Addend = Value.Offset;
  66. if (Value.SymbolName)
  67. addRelocationForSymbol(RE, Value.SymbolName);
  68. else
  69. addRelocationForSection(RE, Value.SectionID);
  70. }
  71. return ++RelI;
  72. }
  73. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
  74. DEBUG(dumpRelocationToResolve(RE, Value));
  75. const SectionEntry &Section = Sections[RE.SectionID];
  76. uint8_t *LocalAddress = Section.Address + RE.Offset;
  77. // If the relocation is PC-relative, the value to be encoded is the
  78. // pointer difference.
  79. if (RE.IsPCRel) {
  80. uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
  81. Value -= FinalAddress;
  82. // ARM PCRel relocations have an effective-PC offset of two instructions
  83. // (four bytes in Thumb mode, 8 bytes in ARM mode).
  84. // FIXME: For now, assume ARM mode.
  85. Value -= 8;
  86. }
  87. switch (RE.RelType) {
  88. default:
  89. llvm_unreachable("Invalid relocation type!");
  90. case MachO::ARM_RELOC_VANILLA:
  91. writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
  92. break;
  93. case MachO::ARM_RELOC_BR24: {
  94. // Mask the value into the target address. We know instructions are
  95. // 32-bit aligned, so we can do it all at once.
  96. Value += RE.Addend;
  97. // The low two bits of the value are not encoded.
  98. Value >>= 2;
  99. // Mask the value to 24 bits.
  100. uint64_t FinalValue = Value & 0xffffff;
  101. // FIXME: If the destination is a Thumb function (and the instruction
  102. // is a non-predicated BL instruction), we need to change it to a BLX
  103. // instruction instead.
  104. // Insert the value into the instruction.
  105. uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
  106. writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4);
  107. break;
  108. }
  109. case MachO::ARM_RELOC_HALF_SECTDIFF: {
  110. uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
  111. uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
  112. assert((Value == SectionABase || Value == SectionBBase) &&
  113. "Unexpected HALFSECTDIFF relocation value.");
  114. Value = SectionABase - SectionBBase + RE.Addend;
  115. if (RE.Size & 0x1) // :upper16:
  116. Value = (Value >> 16);
  117. Value &= 0xffff;
  118. uint32_t Insn = readBytesUnaligned(LocalAddress, 4);
  119. Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
  120. writeBytesUnaligned(Insn, LocalAddress, 4);
  121. break;
  122. }
  123. case MachO::ARM_THUMB_RELOC_BR22:
  124. case MachO::ARM_THUMB_32BIT_BRANCH:
  125. case MachO::ARM_RELOC_HALF:
  126. case MachO::ARM_RELOC_PAIR:
  127. case MachO::ARM_RELOC_SECTDIFF:
  128. case MachO::ARM_RELOC_LOCAL_SECTDIFF:
  129. case MachO::ARM_RELOC_PB_LA_PTR:
  130. Error("Relocation type not implemented yet!");
  131. return;
  132. }
  133. }
  134. void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
  135. const SectionRef &Section) {
  136. StringRef Name;
  137. Section.getName(Name);
  138. if (Name == "__nl_symbol_ptr")
  139. populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
  140. Section, SectionID);
  141. }
  142. private:
  143. void processBranchRelocation(const RelocationEntry &RE,
  144. const RelocationValueRef &Value,
  145. StubMap &Stubs) {
  146. // This is an ARM branch relocation, need to use a stub function.
  147. // Look up for existing stub.
  148. SectionEntry &Section = Sections[RE.SectionID];
  149. RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
  150. uint8_t *Addr;
  151. if (i != Stubs.end()) {
  152. Addr = Section.Address + i->second;
  153. } else {
  154. // Create a new stub function.
  155. Stubs[Value] = Section.StubOffset;
  156. uint8_t *StubTargetAddr =
  157. createStubFunction(Section.Address + Section.StubOffset);
  158. RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address,
  159. MachO::GENERIC_RELOC_VANILLA, Value.Offset, false,
  160. 2);
  161. if (Value.SymbolName)
  162. addRelocationForSymbol(StubRE, Value.SymbolName);
  163. else
  164. addRelocationForSection(StubRE, Value.SectionID);
  165. Addr = Section.Address + Section.StubOffset;
  166. Section.StubOffset += getMaxStubSize();
  167. }
  168. RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
  169. RE.IsPCRel, RE.Size);
  170. resolveRelocation(TargetRE, (uint64_t)Addr);
  171. }
  172. relocation_iterator
  173. processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
  174. const ObjectFile &BaseTObj,
  175. ObjSectionToIDMap &ObjSectionToID) {
  176. const MachOObjectFile &MachO =
  177. static_cast<const MachOObjectFile&>(BaseTObj);
  178. MachO::any_relocation_info RE =
  179. MachO.getRelocation(RelI->getRawDataRefImpl());
  180. // For a half-diff relocation the length bits actually record whether this
  181. // is a movw/movt, and whether this is arm or thumb.
  182. // Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
  183. // Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
  184. unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE);
  185. if (HalfDiffKindBits & 0x2)
  186. llvm_unreachable("Thumb not yet supported.");
  187. SectionEntry &Section = Sections[SectionID];
  188. uint32_t RelocType = MachO.getAnyRelocationType(RE);
  189. bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
  190. uint64_t Offset = RelI->getOffset();
  191. uint8_t *LocalAddress = Section.Address + Offset;
  192. int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
  193. Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
  194. ++RelI;
  195. MachO::any_relocation_info RE2 =
  196. MachO.getRelocation(RelI->getRawDataRefImpl());
  197. uint32_t AddrA = MachO.getScatteredRelocationValue(RE);
  198. section_iterator SAI = getSectionByAddress(MachO, AddrA);
  199. assert(SAI != MachO.section_end() && "Can't find section for address A");
  200. uint64_t SectionABase = SAI->getAddress();
  201. uint64_t SectionAOffset = AddrA - SectionABase;
  202. SectionRef SectionA = *SAI;
  203. bool IsCode = SectionA.isText();
  204. uint32_t SectionAID =
  205. findOrEmitSection(MachO, SectionA, IsCode, ObjSectionToID);
  206. uint32_t AddrB = MachO.getScatteredRelocationValue(RE2);
  207. section_iterator SBI = getSectionByAddress(MachO, AddrB);
  208. assert(SBI != MachO.section_end() && "Can't find section for address B");
  209. uint64_t SectionBBase = SBI->getAddress();
  210. uint64_t SectionBOffset = AddrB - SectionBBase;
  211. SectionRef SectionB = *SBI;
  212. uint32_t SectionBID =
  213. findOrEmitSection(MachO, SectionB, IsCode, ObjSectionToID);
  214. uint32_t OtherHalf = MachO.getAnyRelocationAddress(RE2) & 0xffff;
  215. unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
  216. uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
  217. int64_t Addend = FullImmVal - (AddrA - AddrB);
  218. // addend = Encoded - Expected
  219. // = Encoded - (AddrA - AddrB)
  220. DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
  221. << ", Addend: " << Addend << ", SectionA ID: " << SectionAID
  222. << ", SectionAOffset: " << SectionAOffset
  223. << ", SectionB ID: " << SectionBID
  224. << ", SectionBOffset: " << SectionBOffset << "\n");
  225. RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
  226. SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
  227. HalfDiffKindBits);
  228. addRelocationForSection(R, SectionAID);
  229. addRelocationForSection(R, SectionBID);
  230. return ++RelI;
  231. }
  232. };
  233. }
  234. #undef DEBUG_TYPE
  235. #endif