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 ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
47 if (auto EC = TargetNameOrErr.getError())
48 report_fatal_error(EC.message());
49 StringRef TargetName = *TargetNameOrErr;
51 auto Section = *Symbol->getSection();
53 uint64_t RelType = RelI->getType();
54 uint64_t Offset = RelI->getOffset();
57 SmallString<32> RelTypeName;
58 RelI->getTypeName(RelTypeName);
60 DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
61 << " RelType: " << RelTypeName << " TargetName: " << TargetName
64 unsigned TargetSectionID = -1;
65 if (Section == Obj.section_end()) {
66 RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
67 addRelocationForSymbol(RE, TargetName);
70 findOrEmitSection(Obj, *Section, Section->isText(), ObjSectionToID);
73 case COFF::IMAGE_REL_I386_ABSOLUTE:
74 // This relocation is ignored.
76 case COFF::IMAGE_REL_I386_DIR32:
77 case COFF::IMAGE_REL_I386_DIR32NB:
78 case COFF::IMAGE_REL_I386_REL32: {
80 RelocationEntry(SectionID, Offset, RelType, 0, TargetSectionID,
81 getSymbolOffset(*Symbol), 0, 0, false, 0);
82 addRelocationForSection(RE, TargetSectionID);
85 case COFF::IMAGE_REL_I386_SECTION: {
87 RelocationEntry(TargetSectionID, Offset, RelType, 0);
88 addRelocationForSection(RE, TargetSectionID);
91 case COFF::IMAGE_REL_I386_SECREL: {
92 RelocationEntry RE = RelocationEntry(SectionID, Offset, RelType,
93 getSymbolOffset(*Symbol));
94 addRelocationForSection(RE, TargetSectionID);
98 llvm_unreachable("unsupported relocation type");
106 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
107 const auto Section = Sections[RE.SectionID];
108 uint8_t *Target = Section.Address + RE.Offset;
110 switch (RE.RelType) {
111 case COFF::IMAGE_REL_I386_ABSOLUTE:
112 // This relocation is ignored.
114 case COFF::IMAGE_REL_I386_DIR32: {
115 // The target's 32-bit VA.
117 RE.Sections.SectionA == static_cast<uint32_t>(-1)
119 : Sections[RE.Sections.SectionA].LoadAddress + RE.Addend;
120 assert(static_cast<int32_t>(Result) <= INT32_MAX &&
121 "relocation overflow");
122 assert(static_cast<int32_t>(Result) >= INT32_MIN &&
123 "relocation underflow");
124 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
125 << " RelType: IMAGE_REL_I386_DIR32"
126 << " TargetSection: " << RE.Sections.SectionA
127 << " Value: " << format("0x%08" PRIx32, Result) << '\n');
128 writeBytesUnaligned(Result, Target, 4);
131 case COFF::IMAGE_REL_I386_DIR32NB: {
132 // The target's 32-bit RVA.
133 // NOTE: use Section[0].LoadAddress as an approximation of ImageBase
134 uint64_t Result = Sections[RE.Sections.SectionA].LoadAddress + RE.Addend -
135 Sections[0].LoadAddress;
136 assert(static_cast<int32_t>(Result) <= INT32_MAX &&
137 "relocation overflow");
138 assert(static_cast<int32_t>(Result) >= INT32_MIN &&
139 "relocation underflow");
140 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
141 << " RelType: IMAGE_REL_I386_DIR32NB"
142 << " TargetSection: " << RE.Sections.SectionA
143 << " Value: " << format("0x%08" PRIx32, Result) << '\n');
144 writeBytesUnaligned(Result, Target, 4);
147 case COFF::IMAGE_REL_I386_REL32: {
148 // 32-bit relative displacement to the target.
149 uint64_t Result = Sections[RE.Sections.SectionA].LoadAddress -
150 Section.LoadAddress + RE.Addend - 4 - RE.Offset;
151 assert(static_cast<int32_t>(Result) <= INT32_MAX &&
152 "relocation overflow");
153 assert(static_cast<int32_t>(Result) >= INT32_MIN &&
154 "relocation underflow");
155 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
156 << " RelType: IMAGE_REL_I386_REL32"
157 << " TargetSection: " << RE.Sections.SectionA
158 << " Value: " << format("0x%08" PRIx32, Result) << '\n');
159 writeBytesUnaligned(Result, Target, 4);
162 case COFF::IMAGE_REL_I386_SECTION:
163 // 16-bit section index of the section that contains the target.
164 assert(static_cast<int32_t>(RE.SectionID) <= INT16_MAX &&
165 "relocation overflow");
166 assert(static_cast<int32_t>(RE.SectionID) >= INT16_MIN &&
167 "relocation underflow");
168 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
169 << " RelType: IMAGE_REL_I386_SECTION Value: " << RE.SectionID
171 writeBytesUnaligned(RE.SectionID, Target, 2);
173 case COFF::IMAGE_REL_I386_SECREL:
174 // 32-bit offset of the target from the beginning of its section.
175 assert(static_cast<int32_t>(RE.Addend) <= INT32_MAX &&
176 "relocation overflow");
177 assert(static_cast<int32_t>(RE.Addend) >= INT32_MIN &&
178 "relocation underflow");
179 DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
180 << " RelType: IMAGE_REL_I386_SECREL Value: " << RE.Addend
182 writeBytesUnaligned(RE.Addend, Target, 2);
185 llvm_unreachable("unsupported relocation type");
189 void registerEHFrames() override {}
190 void deregisterEHFrames() override {}
192 void finalizeLoad(const ObjectFile &Obj,
193 ObjSectionToIDMap &SectionMap) override {}