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 "llvm/Object/COFF.h"
18 #include "llvm/Support/COFF.h"
19 #include "../RuntimeDyldCOFF.h"
21 #define DEBUG_TYPE "dyld"
25 class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
27 RuntimeDyldCOFFI386(RuntimeDyld::MemoryManager &MM,
28 RuntimeDyld::SymbolResolver &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 relocation_iterator processRelocationRef(unsigned SectionID,
38 relocation_iterator RelI,
39 const ObjectFile &Obj,
40 ObjSectionToIDMap &ObjSectionToID,
41 StubMap &Stubs) override {
42 auto Symbol = RelI->getSymbol();
43 if (Symbol == Obj.symbol_end())
44 report_fatal_error("Unknown symbol in relocation");
46 Expected<StringRef> TargetNameOrErr = Symbol->getName();
47 if (!TargetNameOrErr) {
49 raw_string_ostream OS(Buf);
50 logAllUnhandledErrors(TargetNameOrErr.takeError(), OS, "");
52 report_fatal_error(Buf);
54 StringRef TargetName = *TargetNameOrErr;
56 auto Section = *Symbol->getSection();
58 uint64_t RelType = RelI->getType();
59 uint64_t Offset = RelI->getOffset();
62 SmallString<32> RelTypeName;
63 RelI->getTypeName(RelTypeName);
65 DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
66 << " RelType: " << RelTypeName << " TargetName: " << TargetName
69 unsigned TargetSectionID = -1;
70 if (Section == Obj.section_end()) {
71 RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
72 addRelocationForSymbol(RE, TargetName);
75 findOrEmitSection(Obj, *Section, Section->isText(), ObjSectionToID);
78 case COFF::IMAGE_REL_I386_ABSOLUTE:
79 // This relocation is ignored.
81 case COFF::IMAGE_REL_I386_DIR32:
82 case COFF::IMAGE_REL_I386_DIR32NB:
83 case COFF::IMAGE_REL_I386_REL32: {
85 RelocationEntry(SectionID, Offset, RelType, 0, TargetSectionID,
86 getSymbolOffset(*Symbol), 0, 0, false, 0);
87 addRelocationForSection(RE, TargetSectionID);
90 case COFF::IMAGE_REL_I386_SECTION: {
92 RelocationEntry(TargetSectionID, Offset, RelType, 0);
93 addRelocationForSection(RE, TargetSectionID);
96 case COFF::IMAGE_REL_I386_SECREL: {
97 RelocationEntry RE = RelocationEntry(SectionID, Offset, RelType,
98 getSymbolOffset(*Symbol));
99 addRelocationForSection(RE, TargetSectionID);
103 llvm_unreachable("unsupported relocation type");
111 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
112 const auto Section = Sections[RE.SectionID];
113 uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
115 switch (RE.RelType) {
116 case COFF::IMAGE_REL_I386_ABSOLUTE:
117 // This relocation is ignored.
119 case COFF::IMAGE_REL_I386_DIR32: {
120 // The target's 32-bit VA.
122 RE.Sections.SectionA == static_cast<uint32_t>(-1)
124 : Sections[RE.Sections.SectionA].getLoadAddressWithOffset(
126 assert(static_cast<int32_t>(Result) <= INT32_MAX &&
127 "relocation overflow");
128 assert(static_cast<int32_t>(Result) >= INT32_MIN &&
129 "relocation underflow");
130 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
131 << " RelType: IMAGE_REL_I386_DIR32"
132 << " TargetSection: " << RE.Sections.SectionA
133 << " Value: " << format("0x%08" PRIx32, Result) << '\n');
134 writeBytesUnaligned(Result, Target, 4);
137 case COFF::IMAGE_REL_I386_DIR32NB: {
138 // The target's 32-bit RVA.
139 // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase
141 Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend) -
142 Sections[0].getLoadAddress();
143 assert(static_cast<int32_t>(Result) <= INT32_MAX &&
144 "relocation overflow");
145 assert(static_cast<int32_t>(Result) >= INT32_MIN &&
146 "relocation underflow");
147 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
148 << " RelType: IMAGE_REL_I386_DIR32NB"
149 << " TargetSection: " << RE.Sections.SectionA
150 << " Value: " << format("0x%08" PRIx32, Result) << '\n');
151 writeBytesUnaligned(Result, Target, 4);
154 case COFF::IMAGE_REL_I386_REL32: {
155 // 32-bit relative displacement to the target.
156 uint64_t Result = Sections[RE.Sections.SectionA].getLoadAddress() -
157 Section.getLoadAddress() + RE.Addend - 4 - RE.Offset;
158 assert(static_cast<int32_t>(Result) <= INT32_MAX &&
159 "relocation overflow");
160 assert(static_cast<int32_t>(Result) >= INT32_MIN &&
161 "relocation underflow");
162 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
163 << " RelType: IMAGE_REL_I386_REL32"
164 << " TargetSection: " << RE.Sections.SectionA
165 << " Value: " << format("0x%08" PRIx32, Result) << '\n');
166 writeBytesUnaligned(Result, Target, 4);
169 case COFF::IMAGE_REL_I386_SECTION:
170 // 16-bit section index of the section that contains the target.
171 assert(static_cast<int32_t>(RE.SectionID) <= INT16_MAX &&
172 "relocation overflow");
173 assert(static_cast<int32_t>(RE.SectionID) >= INT16_MIN &&
174 "relocation underflow");
175 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
176 << " RelType: IMAGE_REL_I386_SECTION Value: " << RE.SectionID
178 writeBytesUnaligned(RE.SectionID, Target, 2);
180 case COFF::IMAGE_REL_I386_SECREL:
181 // 32-bit offset of the target from the beginning of its section.
182 assert(static_cast<int32_t>(RE.Addend) <= INT32_MAX &&
183 "relocation overflow");
184 assert(static_cast<int32_t>(RE.Addend) >= INT32_MIN &&
185 "relocation underflow");
186 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
187 << " RelType: IMAGE_REL_I386_SECREL Value: " << RE.Addend
189 writeBytesUnaligned(RE.Addend, Target, 2);
192 llvm_unreachable("unsupported relocation type");
196 void registerEHFrames() override {}
197 void deregisterEHFrames() override {}
199 void finalizeLoad(const ObjectFile &Obj,
200 ObjSectionToIDMap &SectionMap) override {}