RuntimeDyldCOFFI386.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===--- RuntimeDyldCOFFI386.h --- COFF/X86_64 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 x86 support for MC-JIT runtime dynamic linker.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
  13. #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_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. class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
  20. public:
  21. RuntimeDyldCOFFI386(RuntimeDyld::MemoryManager &MM,
  22. JITSymbolResolver &Resolver)
  23. : RuntimeDyldCOFF(MM, Resolver, 4, COFF::IMAGE_REL_I386_DIR32) {}
  24. unsigned getMaxStubSize() const override {
  25. return 8; // 2-byte jmp instruction + 32-bit relative address + 2 byte pad
  26. }
  27. unsigned getStubAlignment() override { return 1; }
  28. Expected<object::relocation_iterator>
  29. processRelocationRef(unsigned SectionID,
  30. object::relocation_iterator RelI,
  31. const object::ObjectFile &Obj,
  32. ObjSectionToIDMap &ObjSectionToID,
  33. StubMap &Stubs) override {
  34. auto Symbol = RelI->getSymbol();
  35. if (Symbol == Obj.symbol_end())
  36. report_fatal_error("Unknown symbol in relocation");
  37. Expected<StringRef> TargetNameOrErr = Symbol->getName();
  38. if (!TargetNameOrErr)
  39. return TargetNameOrErr.takeError();
  40. StringRef TargetName = *TargetNameOrErr;
  41. auto SectionOrErr = Symbol->getSection();
  42. if (!SectionOrErr)
  43. return SectionOrErr.takeError();
  44. auto Section = *SectionOrErr;
  45. bool IsExtern = Section == Obj.section_end();
  46. uint64_t RelType = RelI->getType();
  47. uint64_t Offset = RelI->getOffset();
  48. unsigned TargetSectionID = -1;
  49. uint64_t TargetOffset = -1;
  50. if (TargetName.startswith(getImportSymbolPrefix())) {
  51. TargetSectionID = SectionID;
  52. TargetOffset = getDLLImportOffset(SectionID, Stubs, TargetName, true);
  53. TargetName = StringRef();
  54. IsExtern = false;
  55. } else if (!IsExtern) {
  56. if (auto TargetSectionIDOrErr = findOrEmitSection(
  57. Obj, *Section, Section->isText(), ObjSectionToID))
  58. TargetSectionID = *TargetSectionIDOrErr;
  59. else
  60. return TargetSectionIDOrErr.takeError();
  61. if (RelType != COFF::IMAGE_REL_I386_SECTION)
  62. TargetOffset = getSymbolOffset(*Symbol);
  63. }
  64. // Determine the Addend used to adjust the relocation value.
  65. uint64_t Addend = 0;
  66. SectionEntry &AddendSection = Sections[SectionID];
  67. uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset;
  68. uint8_t *Displacement = (uint8_t *)ObjTarget;
  69. switch (RelType) {
  70. case COFF::IMAGE_REL_I386_DIR32:
  71. case COFF::IMAGE_REL_I386_DIR32NB:
  72. case COFF::IMAGE_REL_I386_SECREL:
  73. case COFF::IMAGE_REL_I386_REL32: {
  74. Addend = readBytesUnaligned(Displacement, 4);
  75. break;
  76. }
  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. if (IsExtern) {
  88. RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
  89. addRelocationForSymbol(RE, TargetName);
  90. } else {
  91. switch (RelType) {
  92. case COFF::IMAGE_REL_I386_ABSOLUTE:
  93. // This relocation is ignored.
  94. break;
  95. case COFF::IMAGE_REL_I386_DIR32:
  96. case COFF::IMAGE_REL_I386_DIR32NB:
  97. case COFF::IMAGE_REL_I386_REL32: {
  98. RelocationEntry RE =
  99. RelocationEntry(SectionID, Offset, RelType, Addend, TargetSectionID,
  100. TargetOffset, 0, 0, false, 0);
  101. addRelocationForSection(RE, TargetSectionID);
  102. break;
  103. }
  104. case COFF::IMAGE_REL_I386_SECTION: {
  105. RelocationEntry RE =
  106. RelocationEntry(TargetSectionID, Offset, RelType, 0);
  107. addRelocationForSection(RE, TargetSectionID);
  108. break;
  109. }
  110. case COFF::IMAGE_REL_I386_SECREL: {
  111. RelocationEntry RE =
  112. RelocationEntry(SectionID, Offset, RelType, TargetOffset + Addend);
  113. addRelocationForSection(RE, TargetSectionID);
  114. break;
  115. }
  116. default:
  117. llvm_unreachable("unsupported relocation type");
  118. }
  119. }
  120. return ++RelI;
  121. }
  122. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
  123. const auto Section = Sections[RE.SectionID];
  124. uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
  125. switch (RE.RelType) {
  126. case COFF::IMAGE_REL_I386_ABSOLUTE:
  127. // This relocation is ignored.
  128. break;
  129. case COFF::IMAGE_REL_I386_DIR32: {
  130. // The target's 32-bit VA.
  131. uint64_t Result =
  132. RE.Sections.SectionA == static_cast<uint32_t>(-1)
  133. ? Value
  134. : Sections[RE.Sections.SectionA].getLoadAddressWithOffset(
  135. RE.Addend);
  136. assert(Result <= UINT32_MAX && "relocation overflow");
  137. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  138. << " RelType: IMAGE_REL_I386_DIR32"
  139. << " TargetSection: " << RE.Sections.SectionA
  140. << " Value: " << format("0x%08" PRIx32, Result)
  141. << '\n');
  142. writeBytesUnaligned(Result, Target, 4);
  143. break;
  144. }
  145. case COFF::IMAGE_REL_I386_DIR32NB: {
  146. // The target's 32-bit RVA.
  147. // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase
  148. uint64_t Result =
  149. Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend) -
  150. Sections[0].getLoadAddress();
  151. assert(Result <= UINT32_MAX && "relocation overflow");
  152. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  153. << " RelType: IMAGE_REL_I386_DIR32NB"
  154. << " TargetSection: " << RE.Sections.SectionA
  155. << " Value: " << format("0x%08" PRIx32, Result)
  156. << '\n');
  157. writeBytesUnaligned(Result, Target, 4);
  158. break;
  159. }
  160. case COFF::IMAGE_REL_I386_REL32: {
  161. // 32-bit relative displacement to the target.
  162. uint64_t Result = RE.Sections.SectionA == static_cast<uint32_t>(-1)
  163. ? Value
  164. : Sections[RE.Sections.SectionA].getLoadAddress();
  165. Result = Result - Section.getLoadAddress() + RE.Addend - 4 - RE.Offset;
  166. assert(static_cast<int64_t>(Result) <= INT32_MAX &&
  167. "relocation overflow");
  168. assert(static_cast<int64_t>(Result) >= INT32_MIN &&
  169. "relocation underflow");
  170. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  171. << " RelType: IMAGE_REL_I386_REL32"
  172. << " TargetSection: " << RE.Sections.SectionA
  173. << " Value: " << format("0x%08" PRIx32, Result)
  174. << '\n');
  175. writeBytesUnaligned(Result, Target, 4);
  176. break;
  177. }
  178. case COFF::IMAGE_REL_I386_SECTION:
  179. // 16-bit section index of the section that contains the target.
  180. assert(static_cast<uint32_t>(RE.SectionID) <= UINT16_MAX &&
  181. "relocation overflow");
  182. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  183. << " RelType: IMAGE_REL_I386_SECTION Value: "
  184. << RE.SectionID << '\n');
  185. writeBytesUnaligned(RE.SectionID, Target, 2);
  186. break;
  187. case COFF::IMAGE_REL_I386_SECREL:
  188. // 32-bit offset of the target from the beginning of its section.
  189. assert(static_cast<uint64_t>(RE.Addend) <= UINT32_MAX &&
  190. "relocation overflow");
  191. LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
  192. << " RelType: IMAGE_REL_I386_SECREL Value: "
  193. << RE.Addend << '\n');
  194. writeBytesUnaligned(RE.Addend, Target, 4);
  195. break;
  196. default:
  197. llvm_unreachable("unsupported relocation type");
  198. }
  199. }
  200. void registerEHFrames() override {}
  201. };
  202. }
  203. #endif