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