RuntimeDyldCOFFThumb.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //===--- RuntimeDyldCOFFThumb.h --- COFF/Thumb 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. //
  9. // COFF thumb support for MC-JIT runtime dynamic linker.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFTHUMB_H
  13. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFTHUMB_H
  14. #include "../RuntimeDyldCOFF.h"
  15. #include "llvm/BinaryFormat/COFF.h"
  16. #include "llvm/Object/COFF.h"
  17. #define DEBUG_TYPE "dyld"
  18. namespace llvm {
  19. static bool isThumbFunc(object::symbol_iterator Symbol,
  20. const object::ObjectFile &Obj,
  21. object::section_iterator Section) {
  22. Expected<object::SymbolRef::Type> SymTypeOrErr = Symbol->getType();
  23. if (!SymTypeOrErr) {
  24. std::string Buf;
  25. raw_string_ostream OS(Buf);
  26. logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
  27. report_fatal_error(Twine(OS.str()));
  28. }
  29. if (*SymTypeOrErr != object::SymbolRef::ST_Function)
  30. return false;
  31. // We check the IMAGE_SCN_MEM_16BIT flag in the section of the symbol to tell
  32. // if it's thumb or not
  33. return cast<object::COFFObjectFile>(Obj)
  34. .getCOFFSection(*Section)
  35. ->Characteristics &
  36. COFF::IMAGE_SCN_MEM_16BIT;
  37. }
  38. class RuntimeDyldCOFFThumb : public RuntimeDyldCOFF {
  39. public:
  40. RuntimeDyldCOFFThumb(RuntimeDyld::MemoryManager &MM,
  41. JITSymbolResolver &Resolver)
  42. : RuntimeDyldCOFF(MM, Resolver, 4, COFF::IMAGE_REL_ARM_ADDR32) {}
  43. unsigned getMaxStubSize() const override {
  44. return 16; // 8-byte load instructions, 4-byte jump, 4-byte padding
  45. }
  46. unsigned getStubAlignment() override { return 1; }
  47. Expected<object::relocation_iterator>
  48. processRelocationRef(unsigned SectionID,
  49. object::relocation_iterator RelI,
  50. const object::ObjectFile &Obj,
  51. ObjSectionToIDMap &ObjSectionToID,
  52. StubMap &Stubs) override {
  53. auto Symbol = RelI->getSymbol();
  54. if (Symbol == Obj.symbol_end())
  55. report_fatal_error("Unknown symbol in relocation");
  56. Expected<StringRef> TargetNameOrErr = Symbol->getName();
  57. if (!TargetNameOrErr)
  58. return TargetNameOrErr.takeError();
  59. StringRef TargetName = *TargetNameOrErr;
  60. auto SectionOrErr = Symbol->getSection();
  61. if (!SectionOrErr)
  62. return SectionOrErr.takeError();
  63. auto Section = *SectionOrErr;
  64. uint64_t RelType = RelI->getType();
  65. uint64_t Offset = RelI->getOffset();
  66. // Determine the Addend used to adjust the relocation value.
  67. uint64_t Addend = 0;
  68. SectionEntry &AddendSection = Sections[SectionID];
  69. uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset;
  70. uint8_t *Displacement = (uint8_t *)ObjTarget;
  71. switch (RelType) {
  72. case COFF::IMAGE_REL_ARM_ADDR32:
  73. case COFF::IMAGE_REL_ARM_ADDR32NB:
  74. case COFF::IMAGE_REL_ARM_SECREL:
  75. Addend = readBytesUnaligned(Displacement, 4);
  76. break;
  77. default:
  78. break;
  79. }
  80. #if !defined(NDEBUG)
  81. SmallString<32> RelTypeName;
  82. RelI->getTypeName(RelTypeName);
  83. #endif
  84. LLVM_DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
  85. << " RelType: " << RelTypeName << " TargetName: "
  86. << TargetName << " Addend " << Addend << "\n");
  87. bool IsExtern = Section == Obj.section_end();
  88. unsigned TargetSectionID = -1;
  89. uint64_t TargetOffset = -1;
  90. if (TargetName.startswith(getImportSymbolPrefix())) {
  91. TargetSectionID = SectionID;
  92. TargetOffset = getDLLImportOffset(SectionID, Stubs, TargetName, true);
  93. TargetName = StringRef();
  94. IsExtern = false;
  95. } else if (!IsExtern) {
  96. if (auto TargetSectionIDOrErr =
  97. findOrEmitSection(Obj, *Section, Section->isText(), ObjSectionToID))
  98. TargetSectionID = *TargetSectionIDOrErr;
  99. else
  100. return TargetSectionIDOrErr.takeError();
  101. if (RelType != COFF::IMAGE_REL_ARM_SECTION)
  102. TargetOffset = getSymbolOffset(*Symbol);
  103. }
  104. if (IsExtern) {
  105. RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
  106. addRelocationForSymbol(RE, TargetName);
  107. } else {
  108. // We need to find out if the relocation is relative to a thumb function
  109. // so that we include the ISA selection bit when resolve the relocation
  110. bool IsTargetThumbFunc = isThumbFunc(Symbol, Obj, Section);
  111. switch (RelType) {
  112. default: llvm_unreachable("unsupported relocation type");
  113. case COFF::IMAGE_REL_ARM_ABSOLUTE:
  114. // This relocation is ignored.
  115. break;
  116. case COFF::IMAGE_REL_ARM_ADDR32: {
  117. RelocationEntry RE =
  118. RelocationEntry(SectionID, Offset, RelType, Addend, TargetSectionID,
  119. TargetOffset, 0, 0, false, 0, IsTargetThumbFunc);
  120. addRelocationForSection(RE, TargetSectionID);
  121. break;
  122. }
  123. case COFF::IMAGE_REL_ARM_ADDR32NB: {
  124. RelocationEntry RE =
  125. RelocationEntry(SectionID, Offset, RelType, Addend, TargetSectionID,
  126. TargetOffset, 0, 0, false, 0);
  127. addRelocationForSection(RE, TargetSectionID);
  128. break;
  129. }
  130. case COFF::IMAGE_REL_ARM_SECTION: {
  131. RelocationEntry RE =
  132. RelocationEntry(TargetSectionID, Offset, RelType, 0);
  133. addRelocationForSection(RE, TargetSectionID);
  134. break;
  135. }
  136. case COFF::IMAGE_REL_ARM_SECREL: {
  137. RelocationEntry RE =
  138. RelocationEntry(SectionID, Offset, RelType, TargetOffset + Addend);
  139. addRelocationForSection(RE, TargetSectionID);
  140. break;
  141. }
  142. case COFF::IMAGE_REL_ARM_MOV32T: {
  143. RelocationEntry RE =
  144. RelocationEntry(SectionID, Offset, RelType, Addend, TargetSectionID,
  145. TargetOffset, 0, 0, false, 0, IsTargetThumbFunc);
  146. addRelocationForSection(RE, TargetSectionID);
  147. break;
  148. }
  149. case COFF::IMAGE_REL_ARM_BRANCH20T:
  150. case COFF::IMAGE_REL_ARM_BRANCH24T:
  151. case COFF::IMAGE_REL_ARM_BLX23T: {
  152. RelocationEntry RE = RelocationEntry(SectionID, Offset, RelType,
  153. TargetOffset + Addend, true, 0);
  154. addRelocationForSection(RE, TargetSectionID);
  155. break;
  156. }
  157. }
  158. }
  159. return ++RelI;
  160. }
  161. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
  162. const auto Section = Sections[RE.SectionID];
  163. uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
  164. int ISASelectionBit = RE.IsTargetThumbFunc ? 1 : 0;
  165. switch (RE.RelType) {
  166. default: llvm_unreachable("unsupported relocation type");
  167. case COFF::IMAGE_REL_ARM_ABSOLUTE:
  168. // This relocation is ignored.
  169. break;
  170. case COFF::IMAGE_REL_ARM_ADDR32: {
  171. // The target's 32-bit VA.
  172. uint64_t Result =
  173. RE.Sections.SectionA == static_cast<uint32_t>(-1)
  174. ? Value
  175. : Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend);
  176. Result |= ISASelectionBit;
  177. assert(Result <= UINT32_MAX && "relocation overflow");
  178. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  179. << " RelType: IMAGE_REL_ARM_ADDR32"
  180. << " TargetSection: " << RE.Sections.SectionA
  181. << " Value: " << format("0x%08" PRIx32, Result)
  182. << '\n');
  183. writeBytesUnaligned(Result, Target, 4);
  184. break;
  185. }
  186. case COFF::IMAGE_REL_ARM_ADDR32NB: {
  187. // The target's 32-bit RVA.
  188. // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase
  189. uint64_t Result = Sections[RE.Sections.SectionA].getLoadAddress() -
  190. Sections[0].getLoadAddress() + RE.Addend;
  191. assert(Result <= UINT32_MAX && "relocation overflow");
  192. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  193. << " RelType: IMAGE_REL_ARM_ADDR32NB"
  194. << " TargetSection: " << RE.Sections.SectionA
  195. << " Value: " << format("0x%08" PRIx32, Result)
  196. << '\n');
  197. Result |= ISASelectionBit;
  198. writeBytesUnaligned(Result, Target, 4);
  199. break;
  200. }
  201. case COFF::IMAGE_REL_ARM_SECTION:
  202. // 16-bit section index of the section that contains the target.
  203. assert(static_cast<uint32_t>(RE.SectionID) <= UINT16_MAX &&
  204. "relocation overflow");
  205. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  206. << " RelType: IMAGE_REL_ARM_SECTION Value: "
  207. << RE.SectionID << '\n');
  208. writeBytesUnaligned(RE.SectionID, Target, 2);
  209. break;
  210. case COFF::IMAGE_REL_ARM_SECREL:
  211. // 32-bit offset of the target from the beginning of its section.
  212. assert(static_cast<uint64_t>(RE.Addend) <= UINT32_MAX &&
  213. "relocation overflow");
  214. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  215. << " RelType: IMAGE_REL_ARM_SECREL Value: " << RE.Addend
  216. << '\n');
  217. writeBytesUnaligned(RE.Addend, Target, 2);
  218. break;
  219. case COFF::IMAGE_REL_ARM_MOV32T: {
  220. // 32-bit VA of the target applied to a contiguous MOVW+MOVT pair.
  221. uint64_t Result =
  222. Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend);
  223. assert(Result <= UINT32_MAX && "relocation overflow");
  224. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  225. << " RelType: IMAGE_REL_ARM_MOV32T"
  226. << " TargetSection: " << RE.Sections.SectionA
  227. << " Value: " << format("0x%08" PRIx32, Result)
  228. << '\n');
  229. // MOVW(T3): |11110|i|10|0|1|0|0|imm4|0|imm3|Rd|imm8|
  230. // imm32 = zext imm4:i:imm3:imm8
  231. // MOVT(T1): |11110|i|10|1|1|0|0|imm4|0|imm3|Rd|imm8|
  232. // imm16 = imm4:i:imm3:imm8
  233. auto EncodeImmediate = [](uint8_t *Bytes, uint16_t Immediate) {
  234. Bytes[0] |= ((Immediate & 0xf000) >> 12);
  235. Bytes[1] |= ((Immediate & 0x0800) >> 11);
  236. Bytes[2] |= ((Immediate & 0x00ff) >> 0);
  237. Bytes[3] |= (((Immediate & 0x0700) >> 8) << 4);
  238. };
  239. EncodeImmediate(&Target[0],
  240. (static_cast<uint32_t>(Result) >> 00) | ISASelectionBit);
  241. EncodeImmediate(&Target[4], static_cast<uint32_t>(Result) >> 16);
  242. break;
  243. }
  244. case COFF::IMAGE_REL_ARM_BRANCH20T: {
  245. // The most significant 20-bits of the signed 21-bit relative displacement
  246. uint64_t Value =
  247. RE.Addend - (Sections[RE.SectionID].getLoadAddress() + RE.Offset) - 4;
  248. assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX &&
  249. "relocation overflow");
  250. assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN &&
  251. "relocation underflow");
  252. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  253. << " RelType: IMAGE_REL_ARM_BRANCH20T"
  254. << " Value: " << static_cast<int32_t>(Value) << '\n');
  255. static_cast<void>(Value);
  256. llvm_unreachable("unimplemented relocation");
  257. break;
  258. }
  259. case COFF::IMAGE_REL_ARM_BRANCH24T: {
  260. // The most significant 24-bits of the signed 25-bit relative displacement
  261. uint64_t Value =
  262. RE.Addend - (Sections[RE.SectionID].getLoadAddress() + RE.Offset) - 4;
  263. assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX &&
  264. "relocation overflow");
  265. assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN &&
  266. "relocation underflow");
  267. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  268. << " RelType: IMAGE_REL_ARM_BRANCH24T"
  269. << " Value: " << static_cast<int32_t>(Value) << '\n');
  270. static_cast<void>(Value);
  271. llvm_unreachable("unimplemented relocation");
  272. break;
  273. }
  274. case COFF::IMAGE_REL_ARM_BLX23T: {
  275. // The most significant 24-bits of the signed 25-bit relative displacement
  276. uint64_t Value =
  277. RE.Addend - (Sections[RE.SectionID].getLoadAddress() + RE.Offset) - 4;
  278. assert(static_cast<int64_t>(RE.Addend) <= INT32_MAX &&
  279. "relocation overflow");
  280. assert(static_cast<int64_t>(RE.Addend) >= INT32_MIN &&
  281. "relocation underflow");
  282. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  283. << " RelType: IMAGE_REL_ARM_BLX23T"
  284. << " Value: " << static_cast<int32_t>(Value) << '\n');
  285. static_cast<void>(Value);
  286. llvm_unreachable("unimplemented relocation");
  287. break;
  288. }
  289. }
  290. }
  291. void registerEHFrames() override {}
  292. };
  293. }
  294. #endif