1 //===--- RuntimeDyldCOFFI386.h --- COFF/X86_64 specific code ---*- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // COFF x86 support for MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
17 #include "../RuntimeDyldCOFF.h"
18 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/Object/COFF.h"
21 #define DEBUG_TYPE "dyld"
25 class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
27 RuntimeDyldCOFFI386(RuntimeDyld::MemoryManager &MM,
28 JITSymbolResolver &Resolver)
29 : RuntimeDyldCOFF(MM, Resolver) {}
31 unsigned getMaxStubSize() override {
32 return 8; // 2-byte jmp instruction + 32-bit relative address + 2 byte pad
35 unsigned getStubAlignment() override { return 1; }
37 Expected<relocation_iterator>
38 processRelocationRef(unsigned SectionID,
39 relocation_iterator RelI,
40 const ObjectFile &Obj,
41 ObjSectionToIDMap &ObjSectionToID,
42 StubMap &Stubs) override {
44 auto Symbol = RelI->getSymbol();
45 if (Symbol == Obj.symbol_end())
46 report_fatal_error("Unknown symbol in relocation");
48 Expected<StringRef> TargetNameOrErr = Symbol->getName();
50 return TargetNameOrErr.takeError();
51 StringRef TargetName = *TargetNameOrErr;
53 auto SectionOrErr = Symbol->getSection();
55 return SectionOrErr.takeError();
56 auto Section = *SectionOrErr;
58 uint64_t RelType = RelI->getType();
59 uint64_t Offset = RelI->getOffset();
61 // Determine the Addend used to adjust the relocation value.
63 SectionEntry &AddendSection = Sections[SectionID];
64 uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset;
65 uint8_t *Displacement = (uint8_t *)ObjTarget;
68 case COFF::IMAGE_REL_I386_DIR32:
69 case COFF::IMAGE_REL_I386_DIR32NB:
70 case COFF::IMAGE_REL_I386_SECREL:
71 case COFF::IMAGE_REL_I386_REL32: {
72 Addend = readBytesUnaligned(Displacement, 4);
80 SmallString<32> RelTypeName;
81 RelI->getTypeName(RelTypeName);
83 LLVM_DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
84 << " RelType: " << RelTypeName << " TargetName: "
85 << TargetName << " Addend " << Addend << "\n");
87 unsigned TargetSectionID = -1;
88 if (Section == Obj.section_end()) {
89 RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
90 addRelocationForSymbol(RE, TargetName);
92 if (auto TargetSectionIDOrErr =
93 findOrEmitSection(Obj, *Section, Section->isText(), ObjSectionToID))
94 TargetSectionID = *TargetSectionIDOrErr;
96 return TargetSectionIDOrErr.takeError();
99 case COFF::IMAGE_REL_I386_ABSOLUTE:
100 // This relocation is ignored.
102 case COFF::IMAGE_REL_I386_DIR32:
103 case COFF::IMAGE_REL_I386_DIR32NB:
104 case COFF::IMAGE_REL_I386_REL32: {
106 RelocationEntry(SectionID, Offset, RelType, Addend, TargetSectionID,
107 getSymbolOffset(*Symbol), 0, 0, false, 0);
108 addRelocationForSection(RE, TargetSectionID);
111 case COFF::IMAGE_REL_I386_SECTION: {
113 RelocationEntry(TargetSectionID, Offset, RelType, 0);
114 addRelocationForSection(RE, TargetSectionID);
117 case COFF::IMAGE_REL_I386_SECREL: {
118 RelocationEntry RE = RelocationEntry(SectionID, Offset, RelType,
119 getSymbolOffset(*Symbol) + Addend);
120 addRelocationForSection(RE, TargetSectionID);
124 llvm_unreachable("unsupported relocation type");
132 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
133 const auto Section = Sections[RE.SectionID];
134 uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
136 switch (RE.RelType) {
137 case COFF::IMAGE_REL_I386_ABSOLUTE:
138 // This relocation is ignored.
140 case COFF::IMAGE_REL_I386_DIR32: {
141 // The target's 32-bit VA.
143 RE.Sections.SectionA == static_cast<uint32_t>(-1)
145 : Sections[RE.Sections.SectionA].getLoadAddressWithOffset(
147 assert(Result <= UINT32_MAX && "relocation overflow");
148 LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
149 << " RelType: IMAGE_REL_I386_DIR32"
150 << " TargetSection: " << RE.Sections.SectionA
151 << " Value: " << format("0x%08" PRIx32, Result)
153 writeBytesUnaligned(Result, Target, 4);
156 case COFF::IMAGE_REL_I386_DIR32NB: {
157 // The target's 32-bit RVA.
158 // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase
160 Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend) -
161 Sections[0].getLoadAddress();
162 assert(Result <= UINT32_MAX && "relocation overflow");
163 LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
164 << " RelType: IMAGE_REL_I386_DIR32NB"
165 << " TargetSection: " << RE.Sections.SectionA
166 << " Value: " << format("0x%08" PRIx32, Result)
168 writeBytesUnaligned(Result, Target, 4);
171 case COFF::IMAGE_REL_I386_REL32: {
172 // 32-bit relative displacement to the target.
173 uint64_t Result = RE.Sections.SectionA == static_cast<uint32_t>(-1)
175 : Sections[RE.Sections.SectionA].getLoadAddress();
176 Result = Result - Section.getLoadAddress() + RE.Addend - 4 - RE.Offset;
177 assert(static_cast<int64_t>(Result) <= INT32_MAX &&
178 "relocation overflow");
179 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
180 "relocation underflow");
181 LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
182 << " RelType: IMAGE_REL_I386_REL32"
183 << " TargetSection: " << RE.Sections.SectionA
184 << " Value: " << format("0x%08" PRIx32, Result)
186 writeBytesUnaligned(Result, Target, 4);
189 case COFF::IMAGE_REL_I386_SECTION:
190 // 16-bit section index of the section that contains the target.
191 assert(static_cast<uint32_t>(RE.SectionID) <= UINT16_MAX &&
192 "relocation overflow");
193 LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
194 << " RelType: IMAGE_REL_I386_SECTION Value: "
195 << RE.SectionID << '\n');
196 writeBytesUnaligned(RE.SectionID, Target, 2);
198 case COFF::IMAGE_REL_I386_SECREL:
199 // 32-bit offset of the target from the beginning of its section.
200 assert(static_cast<uint64_t>(RE.Addend) <= UINT32_MAX &&
201 "relocation overflow");
202 LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
203 << " RelType: IMAGE_REL_I386_SECREL Value: "
204 << RE.Addend << '\n');
205 writeBytesUnaligned(RE.Addend, Target, 4);
208 llvm_unreachable("unsupported relocation type");
212 void registerEHFrames() override {}