1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- 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 // Implementation of ELF support for the MC-JIT runtime dynamic linker.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "RuntimeDyldELF.h"
14 #include "RuntimeDyldCheckerImpl.h"
15 #include "Targets/RuntimeDyldELFMips.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MemoryBuffer.h"
24
25 using namespace llvm;
26 using namespace llvm::object;
27 using namespace llvm::support::endian;
28
29 #define DEBUG_TYPE "dyld"
30
or32le(void * P,int32_t V)31 static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
32
or32AArch64Imm(void * L,uint64_t Imm)33 static void or32AArch64Imm(void *L, uint64_t Imm) {
34 or32le(L, (Imm & 0xFFF) << 10);
35 }
36
write(bool isBE,void * P,T V)37 template <class T> static void write(bool isBE, void *P, T V) {
38 isBE ? write<T, support::big>(P, V) : write<T, support::little>(P, V);
39 }
40
write32AArch64Addr(void * L,uint64_t Imm)41 static void write32AArch64Addr(void *L, uint64_t Imm) {
42 uint32_t ImmLo = (Imm & 0x3) << 29;
43 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
44 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
45 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
46 }
47
48 // Return the bits [Start, End] from Val shifted Start bits.
49 // For instance, getBits(0xF0, 4, 8) returns 0xF.
getBits(uint64_t Val,int Start,int End)50 static uint64_t getBits(uint64_t Val, int Start, int End) {
51 uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
52 return (Val >> Start) & Mask;
53 }
54
55 namespace {
56
57 template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
58 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
59
60 typedef typename ELFT::uint addr_type;
61
62 DyldELFObject(ELFObjectFile<ELFT> &&Obj);
63
64 public:
65 static Expected<std::unique_ptr<DyldELFObject>>
66 create(MemoryBufferRef Wrapper);
67
68 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
69
70 void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
71
72 // Methods for type inquiry through isa, cast and dyn_cast
classof(const Binary * v)73 static bool classof(const Binary *v) {
74 return (isa<ELFObjectFile<ELFT>>(v) &&
75 classof(cast<ELFObjectFile<ELFT>>(v)));
76 }
classof(const ELFObjectFile<ELFT> * v)77 static bool classof(const ELFObjectFile<ELFT> *v) {
78 return v->isDyldType();
79 }
80 };
81
82
83
84 // The MemoryBuffer passed into this constructor is just a wrapper around the
85 // actual memory. Ultimately, the Binary parent class will take ownership of
86 // this MemoryBuffer object but not the underlying memory.
87 template <class ELFT>
DyldELFObject(ELFObjectFile<ELFT> && Obj)88 DyldELFObject<ELFT>::DyldELFObject(ELFObjectFile<ELFT> &&Obj)
89 : ELFObjectFile<ELFT>(std::move(Obj)) {
90 this->isDyldELFObject = true;
91 }
92
93 template <class ELFT>
94 Expected<std::unique_ptr<DyldELFObject<ELFT>>>
create(MemoryBufferRef Wrapper)95 DyldELFObject<ELFT>::create(MemoryBufferRef Wrapper) {
96 auto Obj = ELFObjectFile<ELFT>::create(Wrapper);
97 if (auto E = Obj.takeError())
98 return std::move(E);
99 std::unique_ptr<DyldELFObject<ELFT>> Ret(
100 new DyldELFObject<ELFT>(std::move(*Obj)));
101 return std::move(Ret);
102 }
103
104 template <class ELFT>
updateSectionAddress(const SectionRef & Sec,uint64_t Addr)105 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
106 uint64_t Addr) {
107 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
108 Elf_Shdr *shdr =
109 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
110
111 // This assumes the address passed in matches the target address bitness
112 // The template-based type cast handles everything else.
113 shdr->sh_addr = static_cast<addr_type>(Addr);
114 }
115
116 template <class ELFT>
updateSymbolAddress(const SymbolRef & SymRef,uint64_t Addr)117 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
118 uint64_t Addr) {
119
120 Elf_Sym *sym = const_cast<Elf_Sym *>(
121 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
122
123 // This assumes the address passed in matches the target address bitness
124 // The template-based type cast handles everything else.
125 sym->st_value = static_cast<addr_type>(Addr);
126 }
127
128 class LoadedELFObjectInfo final
129 : public LoadedObjectInfoHelper<LoadedELFObjectInfo,
130 RuntimeDyld::LoadedObjectInfo> {
131 public:
LoadedELFObjectInfo(RuntimeDyldImpl & RTDyld,ObjSectionToIDMap ObjSecToIDMap)132 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
133 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
134
135 OwningBinary<ObjectFile>
136 getObjectForDebug(const ObjectFile &Obj) const override;
137 };
138
139 template <typename ELFT>
140 static Expected<std::unique_ptr<DyldELFObject<ELFT>>>
createRTDyldELFObject(MemoryBufferRef Buffer,const ObjectFile & SourceObject,const LoadedELFObjectInfo & L)141 createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject,
142 const LoadedELFObjectInfo &L) {
143 typedef typename ELFT::Shdr Elf_Shdr;
144 typedef typename ELFT::uint addr_type;
145
146 Expected<std::unique_ptr<DyldELFObject<ELFT>>> ObjOrErr =
147 DyldELFObject<ELFT>::create(Buffer);
148 if (Error E = ObjOrErr.takeError())
149 return std::move(E);
150
151 std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr);
152
153 // Iterate over all sections in the object.
154 auto SI = SourceObject.section_begin();
155 for (const auto &Sec : Obj->sections()) {
156 Expected<StringRef> NameOrErr = Sec.getName();
157 if (!NameOrErr) {
158 consumeError(NameOrErr.takeError());
159 continue;
160 }
161
162 if (*NameOrErr != "") {
163 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
164 Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
165 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
166
167 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) {
168 // This assumes that the address passed in matches the target address
169 // bitness. The template-based type cast handles everything else.
170 shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
171 }
172 }
173 ++SI;
174 }
175
176 return std::move(Obj);
177 }
178
179 static OwningBinary<ObjectFile>
createELFDebugObject(const ObjectFile & Obj,const LoadedELFObjectInfo & L)180 createELFDebugObject(const ObjectFile &Obj, const LoadedELFObjectInfo &L) {
181 assert(Obj.isELF() && "Not an ELF object file.");
182
183 std::unique_ptr<MemoryBuffer> Buffer =
184 MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
185
186 Expected<std::unique_ptr<ObjectFile>> DebugObj(nullptr);
187 handleAllErrors(DebugObj.takeError());
188 if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian())
189 DebugObj =
190 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
191 else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian())
192 DebugObj =
193 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
194 else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian())
195 DebugObj =
196 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
197 else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian())
198 DebugObj =
199 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
200 else
201 llvm_unreachable("Unexpected ELF format");
202
203 handleAllErrors(DebugObj.takeError());
204 return OwningBinary<ObjectFile>(std::move(*DebugObj), std::move(Buffer));
205 }
206
207 OwningBinary<ObjectFile>
getObjectForDebug(const ObjectFile & Obj) const208 LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
209 return createELFDebugObject(Obj, *this);
210 }
211
212 } // anonymous namespace
213
214 namespace llvm {
215
RuntimeDyldELF(RuntimeDyld::MemoryManager & MemMgr,JITSymbolResolver & Resolver)216 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
217 JITSymbolResolver &Resolver)
218 : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
219 RuntimeDyldELF::~RuntimeDyldELF() = default;
220
registerEHFrames()221 void RuntimeDyldELF::registerEHFrames() {
222 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
223 SID EHFrameSID = UnregisteredEHFrameSections[i];
224 uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
225 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
226 size_t EHFrameSize = Sections[EHFrameSID].getSize();
227 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
228 }
229 UnregisteredEHFrameSections.clear();
230 }
231
232 std::unique_ptr<RuntimeDyldELF>
create(Triple::ArchType Arch,RuntimeDyld::MemoryManager & MemMgr,JITSymbolResolver & Resolver)233 llvm::RuntimeDyldELF::create(Triple::ArchType Arch,
234 RuntimeDyld::MemoryManager &MemMgr,
235 JITSymbolResolver &Resolver) {
236 switch (Arch) {
237 default:
238 return std::make_unique<RuntimeDyldELF>(MemMgr, Resolver);
239 case Triple::mips:
240 case Triple::mipsel:
241 case Triple::mips64:
242 case Triple::mips64el:
243 return std::make_unique<RuntimeDyldELFMips>(MemMgr, Resolver);
244 }
245 }
246
247 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const object::ObjectFile & O)248 RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
249 if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
250 return std::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr);
251 else {
252 HasError = true;
253 raw_string_ostream ErrStream(ErrorStr);
254 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
255 return nullptr;
256 }
257 }
258
resolveX86_64Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend,uint64_t SymOffset)259 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
260 uint64_t Offset, uint64_t Value,
261 uint32_t Type, int64_t Addend,
262 uint64_t SymOffset) {
263 switch (Type) {
264 default:
265 report_fatal_error("Relocation type not implemented yet!");
266 break;
267 case ELF::R_X86_64_NONE:
268 break;
269 case ELF::R_X86_64_8: {
270 Value += Addend;
271 assert((int64_t)Value <= INT8_MAX && (int64_t)Value >= INT8_MIN);
272 uint8_t TruncatedAddr = (Value & 0xFF);
273 *Section.getAddressWithOffset(Offset) = TruncatedAddr;
274 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
275 << format("%p\n", Section.getAddressWithOffset(Offset)));
276 break;
277 }
278 case ELF::R_X86_64_16: {
279 Value += Addend;
280 assert((int64_t)Value <= INT16_MAX && (int64_t)Value >= INT16_MIN);
281 uint16_t TruncatedAddr = (Value & 0xFFFF);
282 support::ulittle16_t::ref(Section.getAddressWithOffset(Offset)) =
283 TruncatedAddr;
284 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
285 << format("%p\n", Section.getAddressWithOffset(Offset)));
286 break;
287 }
288 case ELF::R_X86_64_64: {
289 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
290 Value + Addend;
291 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
292 << format("%p\n", Section.getAddressWithOffset(Offset)));
293 break;
294 }
295 case ELF::R_X86_64_32:
296 case ELF::R_X86_64_32S: {
297 Value += Addend;
298 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
299 (Type == ELF::R_X86_64_32S &&
300 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
301 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
302 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
303 TruncatedAddr;
304 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
305 << format("%p\n", Section.getAddressWithOffset(Offset)));
306 break;
307 }
308 case ELF::R_X86_64_PC8: {
309 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
310 int64_t RealOffset = Value + Addend - FinalAddress;
311 assert(isInt<8>(RealOffset));
312 int8_t TruncOffset = (RealOffset & 0xFF);
313 Section.getAddress()[Offset] = TruncOffset;
314 break;
315 }
316 case ELF::R_X86_64_PC32: {
317 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
318 int64_t RealOffset = Value + Addend - FinalAddress;
319 assert(isInt<32>(RealOffset));
320 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
321 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
322 TruncOffset;
323 break;
324 }
325 case ELF::R_X86_64_PC64: {
326 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
327 int64_t RealOffset = Value + Addend - FinalAddress;
328 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
329 RealOffset;
330 LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset) << " at "
331 << format("%p\n", FinalAddress));
332 break;
333 }
334 case ELF::R_X86_64_GOTOFF64: {
335 // Compute Value - GOTBase.
336 uint64_t GOTBase = 0;
337 for (const auto &Section : Sections) {
338 if (Section.getName() == ".got") {
339 GOTBase = Section.getLoadAddressWithOffset(0);
340 break;
341 }
342 }
343 assert(GOTBase != 0 && "missing GOT");
344 int64_t GOTOffset = Value - GOTBase + Addend;
345 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = GOTOffset;
346 break;
347 }
348 case ELF::R_X86_64_DTPMOD64: {
349 // We only have one DSO, so the module id is always 1.
350 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 1;
351 break;
352 }
353 case ELF::R_X86_64_DTPOFF64:
354 case ELF::R_X86_64_TPOFF64: {
355 // DTPOFF64 should resolve to the offset in the TLS block, TPOFF64 to the
356 // offset in the *initial* TLS block. Since we are statically linking, all
357 // TLS blocks already exist in the initial block, so resolve both
358 // relocations equally.
359 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
360 Value + Addend;
361 break;
362 }
363 case ELF::R_X86_64_DTPOFF32:
364 case ELF::R_X86_64_TPOFF32: {
365 // As for the (D)TPOFF64 relocations above, both DTPOFF32 and TPOFF32 can
366 // be resolved equally.
367 int64_t RealValue = Value + Addend;
368 assert(RealValue >= INT32_MIN && RealValue <= INT32_MAX);
369 int32_t TruncValue = RealValue;
370 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
371 TruncValue;
372 break;
373 }
374 }
375 }
376
resolveX86Relocation(const SectionEntry & Section,uint64_t Offset,uint32_t Value,uint32_t Type,int32_t Addend)377 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
378 uint64_t Offset, uint32_t Value,
379 uint32_t Type, int32_t Addend) {
380 switch (Type) {
381 case ELF::R_386_32: {
382 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
383 Value + Addend;
384 break;
385 }
386 // Handle R_386_PLT32 like R_386_PC32 since it should be able to
387 // reach any 32 bit address.
388 case ELF::R_386_PLT32:
389 case ELF::R_386_PC32: {
390 uint32_t FinalAddress =
391 Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
392 uint32_t RealOffset = Value + Addend - FinalAddress;
393 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
394 RealOffset;
395 break;
396 }
397 default:
398 // There are other relocation types, but it appears these are the
399 // only ones currently used by the LLVM ELF object writer
400 report_fatal_error("Relocation type not implemented yet!");
401 break;
402 }
403 }
404
resolveAArch64Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)405 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
406 uint64_t Offset, uint64_t Value,
407 uint32_t Type, int64_t Addend) {
408 uint32_t *TargetPtr =
409 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
410 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
411 // Data should use target endian. Code should always use little endian.
412 bool isBE = Arch == Triple::aarch64_be;
413
414 LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
415 << format("%llx", Section.getAddressWithOffset(Offset))
416 << " FinalAddress: 0x" << format("%llx", FinalAddress)
417 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
418 << format("%x", Type) << " Addend: 0x"
419 << format("%llx", Addend) << "\n");
420
421 switch (Type) {
422 default:
423 report_fatal_error("Relocation type not implemented yet!");
424 break;
425 case ELF::R_AARCH64_NONE:
426 break;
427 case ELF::R_AARCH64_ABS16: {
428 uint64_t Result = Value + Addend;
429 assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
430 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
431 break;
432 }
433 case ELF::R_AARCH64_ABS32: {
434 uint64_t Result = Value + Addend;
435 assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
436 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
437 break;
438 }
439 case ELF::R_AARCH64_ABS64:
440 write(isBE, TargetPtr, Value + Addend);
441 break;
442 case ELF::R_AARCH64_PLT32: {
443 uint64_t Result = Value + Addend - FinalAddress;
444 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
445 static_cast<int64_t>(Result) <= INT32_MAX);
446 write(isBE, TargetPtr, static_cast<uint32_t>(Result));
447 break;
448 }
449 case ELF::R_AARCH64_PREL16: {
450 uint64_t Result = Value + Addend - FinalAddress;
451 assert(static_cast<int64_t>(Result) >= INT16_MIN &&
452 static_cast<int64_t>(Result) <= UINT16_MAX);
453 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
454 break;
455 }
456 case ELF::R_AARCH64_PREL32: {
457 uint64_t Result = Value + Addend - FinalAddress;
458 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
459 static_cast<int64_t>(Result) <= UINT32_MAX);
460 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
461 break;
462 }
463 case ELF::R_AARCH64_PREL64:
464 write(isBE, TargetPtr, Value + Addend - FinalAddress);
465 break;
466 case ELF::R_AARCH64_CONDBR19: {
467 uint64_t BranchImm = Value + Addend - FinalAddress;
468
469 assert(isInt<21>(BranchImm));
470 *TargetPtr &= 0xff00001fU;
471 // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ
472 or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3);
473 break;
474 }
475 case ELF::R_AARCH64_TSTBR14: {
476 uint64_t BranchImm = Value + Addend - FinalAddress;
477
478 assert(isInt<16>(BranchImm));
479
480 *TargetPtr &= 0xfff8001fU;
481 // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ
482 or32le(TargetPtr, (BranchImm & 0x0000FFFC) << 3);
483 break;
484 }
485 case ELF::R_AARCH64_CALL26: // fallthrough
486 case ELF::R_AARCH64_JUMP26: {
487 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
488 // calculation.
489 uint64_t BranchImm = Value + Addend - FinalAddress;
490
491 // "Check that -2^27 <= result < 2^27".
492 assert(isInt<28>(BranchImm));
493 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
494 break;
495 }
496 case ELF::R_AARCH64_MOVW_UABS_G3:
497 or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
498 break;
499 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
500 or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
501 break;
502 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
503 or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
504 break;
505 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
506 or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
507 break;
508 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
509 // Operation: Page(S+A) - Page(P)
510 uint64_t Result =
511 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
512
513 // Check that -2^32 <= X < 2^32
514 assert(isInt<33>(Result) && "overflow check failed for relocation");
515
516 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
517 // from bits 32:12 of X.
518 write32AArch64Addr(TargetPtr, Result >> 12);
519 break;
520 }
521 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
522 // Operation: S + A
523 // Immediate goes in bits 21:10 of LD/ST instruction, taken
524 // from bits 11:0 of X
525 or32AArch64Imm(TargetPtr, Value + Addend);
526 break;
527 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
528 // Operation: S + A
529 // Immediate goes in bits 21:10 of LD/ST instruction, taken
530 // from bits 11:0 of X
531 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11));
532 break;
533 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
534 // Operation: S + A
535 // Immediate goes in bits 21:10 of LD/ST instruction, taken
536 // from bits 11:1 of X
537 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11));
538 break;
539 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
540 // Operation: S + A
541 // Immediate goes in bits 21:10 of LD/ST instruction, taken
542 // from bits 11:2 of X
543 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
544 break;
545 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
546 // Operation: S + A
547 // Immediate goes in bits 21:10 of LD/ST instruction, taken
548 // from bits 11:3 of X
549 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
550 break;
551 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
552 // Operation: S + A
553 // Immediate goes in bits 21:10 of LD/ST instruction, taken
554 // from bits 11:4 of X
555 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11));
556 break;
557 case ELF::R_AARCH64_LD_PREL_LO19: {
558 // Operation: S + A - P
559 uint64_t Result = Value + Addend - FinalAddress;
560
561 // "Check that -2^20 <= result < 2^20".
562 assert(isInt<21>(Result));
563
564 *TargetPtr &= 0xff00001fU;
565 // Immediate goes in bits 23:5 of LD imm instruction, taken
566 // from bits 20:2 of X
567 *TargetPtr |= ((Result & 0xffc) << (5 - 2));
568 break;
569 }
570 case ELF::R_AARCH64_ADR_PREL_LO21: {
571 // Operation: S + A - P
572 uint64_t Result = Value + Addend - FinalAddress;
573
574 // "Check that -2^20 <= result < 2^20".
575 assert(isInt<21>(Result));
576
577 *TargetPtr &= 0x9f00001fU;
578 // Immediate goes in bits 23:5, 30:29 of ADR imm instruction, taken
579 // from bits 20:0 of X
580 *TargetPtr |= ((Result & 0xffc) << (5 - 2));
581 *TargetPtr |= (Result & 0x3) << 29;
582 break;
583 }
584 }
585 }
586
resolveARMRelocation(const SectionEntry & Section,uint64_t Offset,uint32_t Value,uint32_t Type,int32_t Addend)587 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
588 uint64_t Offset, uint32_t Value,
589 uint32_t Type, int32_t Addend) {
590 // TODO: Add Thumb relocations.
591 uint32_t *TargetPtr =
592 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
593 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
594 Value += Addend;
595
596 LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
597 << Section.getAddressWithOffset(Offset)
598 << " FinalAddress: " << format("%p", FinalAddress)
599 << " Value: " << format("%x", Value)
600 << " Type: " << format("%x", Type)
601 << " Addend: " << format("%x", Addend) << "\n");
602
603 switch (Type) {
604 default:
605 llvm_unreachable("Not implemented relocation type!");
606
607 case ELF::R_ARM_NONE:
608 break;
609 // Write a 31bit signed offset
610 case ELF::R_ARM_PREL31:
611 support::ulittle32_t::ref{TargetPtr} =
612 (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
613 ((Value - FinalAddress) & ~0x80000000);
614 break;
615 case ELF::R_ARM_TARGET1:
616 case ELF::R_ARM_ABS32:
617 support::ulittle32_t::ref{TargetPtr} = Value;
618 break;
619 // Write first 16 bit of 32 bit value to the mov instruction.
620 // Last 4 bit should be shifted.
621 case ELF::R_ARM_MOVW_ABS_NC:
622 case ELF::R_ARM_MOVT_ABS:
623 if (Type == ELF::R_ARM_MOVW_ABS_NC)
624 Value = Value & 0xFFFF;
625 else if (Type == ELF::R_ARM_MOVT_ABS)
626 Value = (Value >> 16) & 0xFFFF;
627 support::ulittle32_t::ref{TargetPtr} =
628 (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
629 (((Value >> 12) & 0xF) << 16);
630 break;
631 // Write 24 bit relative value to the branch instruction.
632 case ELF::R_ARM_PC24: // Fall through.
633 case ELF::R_ARM_CALL: // Fall through.
634 case ELF::R_ARM_JUMP24:
635 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
636 RelValue = (RelValue & 0x03FFFFFC) >> 2;
637 assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
638 support::ulittle32_t::ref{TargetPtr} =
639 (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
640 break;
641 }
642 }
643
setMipsABI(const ObjectFile & Obj)644 void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
645 if (Arch == Triple::UnknownArch ||
646 !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
647 IsMipsO32ABI = false;
648 IsMipsN32ABI = false;
649 IsMipsN64ABI = false;
650 return;
651 }
652 if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
653 unsigned AbiVariant = E->getPlatformFlags();
654 IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
655 IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
656 }
657 IsMipsN64ABI = Obj.getFileFormatName().equals("elf64-mips");
658 }
659
660 // Return the .TOC. section and offset.
findPPC64TOCSection(const ELFObjectFileBase & Obj,ObjSectionToIDMap & LocalSections,RelocationValueRef & Rel)661 Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
662 ObjSectionToIDMap &LocalSections,
663 RelocationValueRef &Rel) {
664 // Set a default SectionID in case we do not find a TOC section below.
665 // This may happen for references to TOC base base (sym@toc, .odp
666 // relocation) without a .toc directive. In this case just use the
667 // first section (which is usually the .odp) since the code won't
668 // reference the .toc base directly.
669 Rel.SymbolName = nullptr;
670 Rel.SectionID = 0;
671
672 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
673 // order. The TOC starts where the first of these sections starts.
674 for (auto &Section : Obj.sections()) {
675 Expected<StringRef> NameOrErr = Section.getName();
676 if (!NameOrErr)
677 return NameOrErr.takeError();
678 StringRef SectionName = *NameOrErr;
679
680 if (SectionName == ".got"
681 || SectionName == ".toc"
682 || SectionName == ".tocbss"
683 || SectionName == ".plt") {
684 if (auto SectionIDOrErr =
685 findOrEmitSection(Obj, Section, false, LocalSections))
686 Rel.SectionID = *SectionIDOrErr;
687 else
688 return SectionIDOrErr.takeError();
689 break;
690 }
691 }
692
693 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
694 // thus permitting a full 64 Kbytes segment.
695 Rel.Addend = 0x8000;
696
697 return Error::success();
698 }
699
700 // Returns the sections and offset associated with the ODP entry referenced
701 // by Symbol.
findOPDEntrySection(const ELFObjectFileBase & Obj,ObjSectionToIDMap & LocalSections,RelocationValueRef & Rel)702 Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
703 ObjSectionToIDMap &LocalSections,
704 RelocationValueRef &Rel) {
705 // Get the ELF symbol value (st_value) to compare with Relocation offset in
706 // .opd entries
707 for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
708 si != se; ++si) {
709
710 Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
711 if (!RelSecOrErr)
712 report_fatal_error(Twine(toString(RelSecOrErr.takeError())));
713
714 section_iterator RelSecI = *RelSecOrErr;
715 if (RelSecI == Obj.section_end())
716 continue;
717
718 Expected<StringRef> NameOrErr = RelSecI->getName();
719 if (!NameOrErr)
720 return NameOrErr.takeError();
721 StringRef RelSectionName = *NameOrErr;
722
723 if (RelSectionName != ".opd")
724 continue;
725
726 for (elf_relocation_iterator i = si->relocation_begin(),
727 e = si->relocation_end();
728 i != e;) {
729 // The R_PPC64_ADDR64 relocation indicates the first field
730 // of a .opd entry
731 uint64_t TypeFunc = i->getType();
732 if (TypeFunc != ELF::R_PPC64_ADDR64) {
733 ++i;
734 continue;
735 }
736
737 uint64_t TargetSymbolOffset = i->getOffset();
738 symbol_iterator TargetSymbol = i->getSymbol();
739 int64_t Addend;
740 if (auto AddendOrErr = i->getAddend())
741 Addend = *AddendOrErr;
742 else
743 return AddendOrErr.takeError();
744
745 ++i;
746 if (i == e)
747 break;
748
749 // Just check if following relocation is a R_PPC64_TOC
750 uint64_t TypeTOC = i->getType();
751 if (TypeTOC != ELF::R_PPC64_TOC)
752 continue;
753
754 // Finally compares the Symbol value and the target symbol offset
755 // to check if this .opd entry refers to the symbol the relocation
756 // points to.
757 if (Rel.Addend != (int64_t)TargetSymbolOffset)
758 continue;
759
760 section_iterator TSI = Obj.section_end();
761 if (auto TSIOrErr = TargetSymbol->getSection())
762 TSI = *TSIOrErr;
763 else
764 return TSIOrErr.takeError();
765 assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
766
767 bool IsCode = TSI->isText();
768 if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
769 LocalSections))
770 Rel.SectionID = *SectionIDOrErr;
771 else
772 return SectionIDOrErr.takeError();
773 Rel.Addend = (intptr_t)Addend;
774 return Error::success();
775 }
776 }
777 llvm_unreachable("Attempting to get address of ODP entry!");
778 }
779
780 // Relocation masks following the #lo(value), #hi(value), #ha(value),
781 // #higher(value), #highera(value), #highest(value), and #highesta(value)
782 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
783 // document.
784
applyPPClo(uint64_t value)785 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
786
applyPPChi(uint64_t value)787 static inline uint16_t applyPPChi(uint64_t value) {
788 return (value >> 16) & 0xffff;
789 }
790
applyPPCha(uint64_t value)791 static inline uint16_t applyPPCha (uint64_t value) {
792 return ((value + 0x8000) >> 16) & 0xffff;
793 }
794
applyPPChigher(uint64_t value)795 static inline uint16_t applyPPChigher(uint64_t value) {
796 return (value >> 32) & 0xffff;
797 }
798
applyPPChighera(uint64_t value)799 static inline uint16_t applyPPChighera (uint64_t value) {
800 return ((value + 0x8000) >> 32) & 0xffff;
801 }
802
applyPPChighest(uint64_t value)803 static inline uint16_t applyPPChighest(uint64_t value) {
804 return (value >> 48) & 0xffff;
805 }
806
applyPPChighesta(uint64_t value)807 static inline uint16_t applyPPChighesta (uint64_t value) {
808 return ((value + 0x8000) >> 48) & 0xffff;
809 }
810
resolvePPC32Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)811 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
812 uint64_t Offset, uint64_t Value,
813 uint32_t Type, int64_t Addend) {
814 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
815 switch (Type) {
816 default:
817 report_fatal_error("Relocation type not implemented yet!");
818 break;
819 case ELF::R_PPC_ADDR16_LO:
820 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
821 break;
822 case ELF::R_PPC_ADDR16_HI:
823 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
824 break;
825 case ELF::R_PPC_ADDR16_HA:
826 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
827 break;
828 }
829 }
830
resolvePPC64Relocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)831 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
832 uint64_t Offset, uint64_t Value,
833 uint32_t Type, int64_t Addend) {
834 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
835 switch (Type) {
836 default:
837 report_fatal_error("Relocation type not implemented yet!");
838 break;
839 case ELF::R_PPC64_ADDR16:
840 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
841 break;
842 case ELF::R_PPC64_ADDR16_DS:
843 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
844 break;
845 case ELF::R_PPC64_ADDR16_LO:
846 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
847 break;
848 case ELF::R_PPC64_ADDR16_LO_DS:
849 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
850 break;
851 case ELF::R_PPC64_ADDR16_HI:
852 case ELF::R_PPC64_ADDR16_HIGH:
853 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
854 break;
855 case ELF::R_PPC64_ADDR16_HA:
856 case ELF::R_PPC64_ADDR16_HIGHA:
857 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
858 break;
859 case ELF::R_PPC64_ADDR16_HIGHER:
860 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
861 break;
862 case ELF::R_PPC64_ADDR16_HIGHERA:
863 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
864 break;
865 case ELF::R_PPC64_ADDR16_HIGHEST:
866 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
867 break;
868 case ELF::R_PPC64_ADDR16_HIGHESTA:
869 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
870 break;
871 case ELF::R_PPC64_ADDR14: {
872 assert(((Value + Addend) & 3) == 0);
873 // Preserve the AA/LK bits in the branch instruction
874 uint8_t aalk = *(LocalAddress + 3);
875 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
876 } break;
877 case ELF::R_PPC64_REL16_LO: {
878 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
879 uint64_t Delta = Value - FinalAddress + Addend;
880 writeInt16BE(LocalAddress, applyPPClo(Delta));
881 } break;
882 case ELF::R_PPC64_REL16_HI: {
883 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
884 uint64_t Delta = Value - FinalAddress + Addend;
885 writeInt16BE(LocalAddress, applyPPChi(Delta));
886 } break;
887 case ELF::R_PPC64_REL16_HA: {
888 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
889 uint64_t Delta = Value - FinalAddress + Addend;
890 writeInt16BE(LocalAddress, applyPPCha(Delta));
891 } break;
892 case ELF::R_PPC64_ADDR32: {
893 int64_t Result = static_cast<int64_t>(Value + Addend);
894 if (SignExtend64<32>(Result) != Result)
895 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
896 writeInt32BE(LocalAddress, Result);
897 } break;
898 case ELF::R_PPC64_REL24: {
899 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
900 int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
901 if (SignExtend64<26>(delta) != delta)
902 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
903 // We preserve bits other than LI field, i.e. PO and AA/LK fields.
904 uint32_t Inst = readBytesUnaligned(LocalAddress, 4);
905 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
906 } break;
907 case ELF::R_PPC64_REL32: {
908 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
909 int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
910 if (SignExtend64<32>(delta) != delta)
911 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
912 writeInt32BE(LocalAddress, delta);
913 } break;
914 case ELF::R_PPC64_REL64: {
915 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
916 uint64_t Delta = Value - FinalAddress + Addend;
917 writeInt64BE(LocalAddress, Delta);
918 } break;
919 case ELF::R_PPC64_ADDR64:
920 writeInt64BE(LocalAddress, Value + Addend);
921 break;
922 }
923 }
924
resolveSystemZRelocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)925 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
926 uint64_t Offset, uint64_t Value,
927 uint32_t Type, int64_t Addend) {
928 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
929 switch (Type) {
930 default:
931 report_fatal_error("Relocation type not implemented yet!");
932 break;
933 case ELF::R_390_PC16DBL:
934 case ELF::R_390_PLT16DBL: {
935 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
936 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
937 writeInt16BE(LocalAddress, Delta / 2);
938 break;
939 }
940 case ELF::R_390_PC32DBL:
941 case ELF::R_390_PLT32DBL: {
942 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
943 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
944 writeInt32BE(LocalAddress, Delta / 2);
945 break;
946 }
947 case ELF::R_390_PC16: {
948 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
949 assert(int16_t(Delta) == Delta && "R_390_PC16 overflow");
950 writeInt16BE(LocalAddress, Delta);
951 break;
952 }
953 case ELF::R_390_PC32: {
954 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
955 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
956 writeInt32BE(LocalAddress, Delta);
957 break;
958 }
959 case ELF::R_390_PC64: {
960 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
961 writeInt64BE(LocalAddress, Delta);
962 break;
963 }
964 case ELF::R_390_8:
965 *LocalAddress = (uint8_t)(Value + Addend);
966 break;
967 case ELF::R_390_16:
968 writeInt16BE(LocalAddress, Value + Addend);
969 break;
970 case ELF::R_390_32:
971 writeInt32BE(LocalAddress, Value + Addend);
972 break;
973 case ELF::R_390_64:
974 writeInt64BE(LocalAddress, Value + Addend);
975 break;
976 }
977 }
978
resolveBPFRelocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend)979 void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section,
980 uint64_t Offset, uint64_t Value,
981 uint32_t Type, int64_t Addend) {
982 bool isBE = Arch == Triple::bpfeb;
983
984 switch (Type) {
985 default:
986 report_fatal_error("Relocation type not implemented yet!");
987 break;
988 case ELF::R_BPF_NONE:
989 case ELF::R_BPF_64_64:
990 case ELF::R_BPF_64_32:
991 case ELF::R_BPF_64_NODYLD32:
992 break;
993 case ELF::R_BPF_64_ABS64: {
994 write(isBE, Section.getAddressWithOffset(Offset), Value + Addend);
995 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
996 << format("%p\n", Section.getAddressWithOffset(Offset)));
997 break;
998 }
999 case ELF::R_BPF_64_ABS32: {
1000 Value += Addend;
1001 assert(Value <= UINT32_MAX);
1002 write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value));
1003 LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at "
1004 << format("%p\n", Section.getAddressWithOffset(Offset)));
1005 break;
1006 }
1007 }
1008 }
1009
1010 // The target location for the relocation is described by RE.SectionID and
1011 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
1012 // SectionEntry has three members describing its location.
1013 // SectionEntry::Address is the address at which the section has been loaded
1014 // into memory in the current (host) process. SectionEntry::LoadAddress is the
1015 // address that the section will have in the target process.
1016 // SectionEntry::ObjAddress is the address of the bits for this section in the
1017 // original emitted object image (also in the current address space).
1018 //
1019 // Relocations will be applied as if the section were loaded at
1020 // SectionEntry::LoadAddress, but they will be applied at an address based
1021 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
1022 // Target memory contents if they are required for value calculations.
1023 //
1024 // The Value parameter here is the load address of the symbol for the
1025 // relocation to be applied. For relocations which refer to symbols in the
1026 // current object Value will be the LoadAddress of the section in which
1027 // the symbol resides (RE.Addend provides additional information about the
1028 // symbol location). For external symbols, Value will be the address of the
1029 // symbol in the target address space.
resolveRelocation(const RelocationEntry & RE,uint64_t Value)1030 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
1031 uint64_t Value) {
1032 const SectionEntry &Section = Sections[RE.SectionID];
1033 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
1034 RE.SymOffset, RE.SectionID);
1035 }
1036
resolveRelocation(const SectionEntry & Section,uint64_t Offset,uint64_t Value,uint32_t Type,int64_t Addend,uint64_t SymOffset,SID SectionID)1037 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
1038 uint64_t Offset, uint64_t Value,
1039 uint32_t Type, int64_t Addend,
1040 uint64_t SymOffset, SID SectionID) {
1041 switch (Arch) {
1042 case Triple::x86_64:
1043 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
1044 break;
1045 case Triple::x86:
1046 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1047 (uint32_t)(Addend & 0xffffffffL));
1048 break;
1049 case Triple::aarch64:
1050 case Triple::aarch64_be:
1051 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
1052 break;
1053 case Triple::arm: // Fall through.
1054 case Triple::armeb:
1055 case Triple::thumb:
1056 case Triple::thumbeb:
1057 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1058 (uint32_t)(Addend & 0xffffffffL));
1059 break;
1060 case Triple::ppc: // Fall through.
1061 case Triple::ppcle:
1062 resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
1063 break;
1064 case Triple::ppc64: // Fall through.
1065 case Triple::ppc64le:
1066 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
1067 break;
1068 case Triple::systemz:
1069 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
1070 break;
1071 case Triple::bpfel:
1072 case Triple::bpfeb:
1073 resolveBPFRelocation(Section, Offset, Value, Type, Addend);
1074 break;
1075 default:
1076 llvm_unreachable("Unsupported CPU type!");
1077 }
1078 }
1079
computePlaceholderAddress(unsigned SectionID,uint64_t Offset) const1080 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
1081 return (void *)(Sections[SectionID].getObjAddress() + Offset);
1082 }
1083
processSimpleRelocation(unsigned SectionID,uint64_t Offset,unsigned RelType,RelocationValueRef Value)1084 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
1085 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
1086 if (Value.SymbolName)
1087 addRelocationForSymbol(RE, Value.SymbolName);
1088 else
1089 addRelocationForSection(RE, Value.SectionID);
1090 }
1091
getMatchingLoRelocation(uint32_t RelType,bool IsLocal) const1092 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
1093 bool IsLocal) const {
1094 switch (RelType) {
1095 case ELF::R_MICROMIPS_GOT16:
1096 if (IsLocal)
1097 return ELF::R_MICROMIPS_LO16;
1098 break;
1099 case ELF::R_MICROMIPS_HI16:
1100 return ELF::R_MICROMIPS_LO16;
1101 case ELF::R_MIPS_GOT16:
1102 if (IsLocal)
1103 return ELF::R_MIPS_LO16;
1104 break;
1105 case ELF::R_MIPS_HI16:
1106 return ELF::R_MIPS_LO16;
1107 case ELF::R_MIPS_PCHI16:
1108 return ELF::R_MIPS_PCLO16;
1109 default:
1110 break;
1111 }
1112 return ELF::R_MIPS_NONE;
1113 }
1114
1115 // Sometimes we don't need to create thunk for a branch.
1116 // This typically happens when branch target is located
1117 // in the same object file. In such case target is either
1118 // a weak symbol or symbol in a different executable section.
1119 // This function checks if branch target is located in the
1120 // same object file and if distance between source and target
1121 // fits R_AARCH64_CALL26 relocation. If both conditions are
1122 // met, it emits direct jump to the target and returns true.
1123 // Otherwise false is returned and thunk is created.
resolveAArch64ShortBranch(unsigned SectionID,relocation_iterator RelI,const RelocationValueRef & Value)1124 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1125 unsigned SectionID, relocation_iterator RelI,
1126 const RelocationValueRef &Value) {
1127 uint64_t Address;
1128 if (Value.SymbolName) {
1129 auto Loc = GlobalSymbolTable.find(Value.SymbolName);
1130
1131 // Don't create direct branch for external symbols.
1132 if (Loc == GlobalSymbolTable.end())
1133 return false;
1134
1135 const auto &SymInfo = Loc->second;
1136 Address =
1137 uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
1138 SymInfo.getOffset()));
1139 } else {
1140 Address = uint64_t(Sections[Value.SectionID].getLoadAddress());
1141 }
1142 uint64_t Offset = RelI->getOffset();
1143 uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
1144
1145 // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
1146 // If distance between source and target is out of range then we should
1147 // create thunk.
1148 if (!isInt<28>(Address + Value.Addend - SourceAddress))
1149 return false;
1150
1151 resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
1152 Value.Addend);
1153
1154 return true;
1155 }
1156
resolveAArch64Branch(unsigned SectionID,const RelocationValueRef & Value,relocation_iterator RelI,StubMap & Stubs)1157 void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
1158 const RelocationValueRef &Value,
1159 relocation_iterator RelI,
1160 StubMap &Stubs) {
1161
1162 LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1163 SectionEntry &Section = Sections[SectionID];
1164
1165 uint64_t Offset = RelI->getOffset();
1166 unsigned RelType = RelI->getType();
1167 // Look for an existing stub.
1168 StubMap::const_iterator i = Stubs.find(Value);
1169 if (i != Stubs.end()) {
1170 resolveRelocation(Section, Offset,
1171 (uint64_t)Section.getAddressWithOffset(i->second),
1172 RelType, 0);
1173 LLVM_DEBUG(dbgs() << " Stub function found\n");
1174 } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1175 // Create a new stub function.
1176 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1177 Stubs[Value] = Section.getStubOffset();
1178 uint8_t *StubTargetAddr = createStubFunction(
1179 Section.getAddressWithOffset(Section.getStubOffset()));
1180
1181 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(),
1182 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1183 RelocationEntry REmovk_g2(SectionID,
1184 StubTargetAddr - Section.getAddress() + 4,
1185 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1186 RelocationEntry REmovk_g1(SectionID,
1187 StubTargetAddr - Section.getAddress() + 8,
1188 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1189 RelocationEntry REmovk_g0(SectionID,
1190 StubTargetAddr - Section.getAddress() + 12,
1191 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1192
1193 if (Value.SymbolName) {
1194 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1195 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1196 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1197 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1198 } else {
1199 addRelocationForSection(REmovz_g3, Value.SectionID);
1200 addRelocationForSection(REmovk_g2, Value.SectionID);
1201 addRelocationForSection(REmovk_g1, Value.SectionID);
1202 addRelocationForSection(REmovk_g0, Value.SectionID);
1203 }
1204 resolveRelocation(Section, Offset,
1205 reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
1206 Section.getStubOffset())),
1207 RelType, 0);
1208 Section.advanceStubOffset(getMaxStubSize());
1209 }
1210 }
1211
1212 Expected<relocation_iterator>
processRelocationRef(unsigned SectionID,relocation_iterator RelI,const ObjectFile & O,ObjSectionToIDMap & ObjSectionToID,StubMap & Stubs)1213 RuntimeDyldELF::processRelocationRef(
1214 unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
1215 ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
1216 const auto &Obj = cast<ELFObjectFileBase>(O);
1217 uint64_t RelType = RelI->getType();
1218 int64_t Addend = 0;
1219 if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend())
1220 Addend = *AddendOrErr;
1221 else
1222 consumeError(AddendOrErr.takeError());
1223 elf_symbol_iterator Symbol = RelI->getSymbol();
1224
1225 // Obtain the symbol name which is referenced in the relocation
1226 StringRef TargetName;
1227 if (Symbol != Obj.symbol_end()) {
1228 if (auto TargetNameOrErr = Symbol->getName())
1229 TargetName = *TargetNameOrErr;
1230 else
1231 return TargetNameOrErr.takeError();
1232 }
1233 LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
1234 << " TargetName: " << TargetName << "\n");
1235 RelocationValueRef Value;
1236 // First search for the symbol in the local symbol table
1237 SymbolRef::Type SymType = SymbolRef::ST_Unknown;
1238
1239 // Search for the symbol in the global symbol table
1240 RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
1241 if (Symbol != Obj.symbol_end()) {
1242 gsi = GlobalSymbolTable.find(TargetName.data());
1243 Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1244 if (!SymTypeOrErr) {
1245 std::string Buf;
1246 raw_string_ostream OS(Buf);
1247 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
1248 report_fatal_error(Twine(OS.str()));
1249 }
1250 SymType = *SymTypeOrErr;
1251 }
1252 if (gsi != GlobalSymbolTable.end()) {
1253 const auto &SymInfo = gsi->second;
1254 Value.SectionID = SymInfo.getSectionID();
1255 Value.Offset = SymInfo.getOffset();
1256 Value.Addend = SymInfo.getOffset() + Addend;
1257 } else {
1258 switch (SymType) {
1259 case SymbolRef::ST_Debug: {
1260 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
1261 // and can be changed by another developers. Maybe best way is add
1262 // a new symbol type ST_Section to SymbolRef and use it.
1263 auto SectionOrErr = Symbol->getSection();
1264 if (!SectionOrErr) {
1265 std::string Buf;
1266 raw_string_ostream OS(Buf);
1267 logAllUnhandledErrors(SectionOrErr.takeError(), OS);
1268 report_fatal_error(Twine(OS.str()));
1269 }
1270 section_iterator si = *SectionOrErr;
1271 if (si == Obj.section_end())
1272 llvm_unreachable("Symbol section not found, bad object file format!");
1273 LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n");
1274 bool isCode = si->isText();
1275 if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode,
1276 ObjSectionToID))
1277 Value.SectionID = *SectionIDOrErr;
1278 else
1279 return SectionIDOrErr.takeError();
1280 Value.Addend = Addend;
1281 break;
1282 }
1283 case SymbolRef::ST_Data:
1284 case SymbolRef::ST_Function:
1285 case SymbolRef::ST_Unknown: {
1286 Value.SymbolName = TargetName.data();
1287 Value.Addend = Addend;
1288
1289 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
1290 // will manifest here as a NULL symbol name.
1291 // We can set this as a valid (but empty) symbol name, and rely
1292 // on addRelocationForSymbol to handle this.
1293 if (!Value.SymbolName)
1294 Value.SymbolName = "";
1295 break;
1296 }
1297 default:
1298 llvm_unreachable("Unresolved symbol type!");
1299 break;
1300 }
1301 }
1302
1303 uint64_t Offset = RelI->getOffset();
1304
1305 LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
1306 << "\n");
1307 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be)) {
1308 if ((RelType == ELF::R_AARCH64_CALL26 ||
1309 RelType == ELF::R_AARCH64_JUMP26) &&
1310 MemMgr.allowStubAllocation()) {
1311 resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1312 } else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1313 // Create new GOT entry or find existing one. If GOT entry is
1314 // to be created, then we also emit ABS64 relocation for it.
1315 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1316 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1317 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1318
1319 } else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1320 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1321 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1322 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1323 } else {
1324 processSimpleRelocation(SectionID, Offset, RelType, Value);
1325 }
1326 } else if (Arch == Triple::arm) {
1327 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1328 RelType == ELF::R_ARM_JUMP24) {
1329 // This is an ARM branch relocation, need to use a stub function.
1330 LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
1331 SectionEntry &Section = Sections[SectionID];
1332
1333 // Look for an existing stub.
1334 StubMap::const_iterator i = Stubs.find(Value);
1335 if (i != Stubs.end()) {
1336 resolveRelocation(
1337 Section, Offset,
1338 reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
1339 RelType, 0);
1340 LLVM_DEBUG(dbgs() << " Stub function found\n");
1341 } else {
1342 // Create a new stub function.
1343 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1344 Stubs[Value] = Section.getStubOffset();
1345 uint8_t *StubTargetAddr = createStubFunction(
1346 Section.getAddressWithOffset(Section.getStubOffset()));
1347 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1348 ELF::R_ARM_ABS32, Value.Addend);
1349 if (Value.SymbolName)
1350 addRelocationForSymbol(RE, Value.SymbolName);
1351 else
1352 addRelocationForSection(RE, Value.SectionID);
1353
1354 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1355 Section.getAddressWithOffset(
1356 Section.getStubOffset())),
1357 RelType, 0);
1358 Section.advanceStubOffset(getMaxStubSize());
1359 }
1360 } else {
1361 uint32_t *Placeholder =
1362 reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
1363 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1364 RelType == ELF::R_ARM_ABS32) {
1365 Value.Addend += *Placeholder;
1366 } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1367 // See ELF for ARM documentation
1368 Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1369 }
1370 processSimpleRelocation(SectionID, Offset, RelType, Value);
1371 }
1372 } else if (IsMipsO32ABI) {
1373 uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
1374 computePlaceholderAddress(SectionID, Offset));
1375 uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
1376 if (RelType == ELF::R_MIPS_26) {
1377 // This is an Mips branch relocation, need to use a stub function.
1378 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1379 SectionEntry &Section = Sections[SectionID];
1380
1381 // Extract the addend from the instruction.
1382 // We shift up by two since the Value will be down shifted again
1383 // when applying the relocation.
1384 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1385
1386 Value.Addend += Addend;
1387
1388 // Look up for existing stub.
1389 StubMap::const_iterator i = Stubs.find(Value);
1390 if (i != Stubs.end()) {
1391 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1392 addRelocationForSection(RE, SectionID);
1393 LLVM_DEBUG(dbgs() << " Stub function found\n");
1394 } else {
1395 // Create a new stub function.
1396 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1397 Stubs[Value] = Section.getStubOffset();
1398
1399 unsigned AbiVariant = Obj.getPlatformFlags();
1400
1401 uint8_t *StubTargetAddr = createStubFunction(
1402 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1403
1404 // Creating Hi and Lo relocations for the filled stub instructions.
1405 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1406 ELF::R_MIPS_HI16, Value.Addend);
1407 RelocationEntry RELo(SectionID,
1408 StubTargetAddr - Section.getAddress() + 4,
1409 ELF::R_MIPS_LO16, Value.Addend);
1410
1411 if (Value.SymbolName) {
1412 addRelocationForSymbol(REHi, Value.SymbolName);
1413 addRelocationForSymbol(RELo, Value.SymbolName);
1414 } else {
1415 addRelocationForSection(REHi, Value.SectionID);
1416 addRelocationForSection(RELo, Value.SectionID);
1417 }
1418
1419 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1420 addRelocationForSection(RE, SectionID);
1421 Section.advanceStubOffset(getMaxStubSize());
1422 }
1423 } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1424 int64_t Addend = (Opcode & 0x0000ffff) << 16;
1425 RelocationEntry RE(SectionID, Offset, RelType, Addend);
1426 PendingRelocs.push_back(std::make_pair(Value, RE));
1427 } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1428 int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1429 for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) {
1430 const RelocationValueRef &MatchingValue = I->first;
1431 RelocationEntry &Reloc = I->second;
1432 if (MatchingValue == Value &&
1433 RelType == getMatchingLoRelocation(Reloc.RelType) &&
1434 SectionID == Reloc.SectionID) {
1435 Reloc.Addend += Addend;
1436 if (Value.SymbolName)
1437 addRelocationForSymbol(Reloc, Value.SymbolName);
1438 else
1439 addRelocationForSection(Reloc, Value.SectionID);
1440 I = PendingRelocs.erase(I);
1441 } else
1442 ++I;
1443 }
1444 RelocationEntry RE(SectionID, Offset, RelType, Addend);
1445 if (Value.SymbolName)
1446 addRelocationForSymbol(RE, Value.SymbolName);
1447 else
1448 addRelocationForSection(RE, Value.SectionID);
1449 } else {
1450 if (RelType == ELF::R_MIPS_32)
1451 Value.Addend += Opcode;
1452 else if (RelType == ELF::R_MIPS_PC16)
1453 Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1454 else if (RelType == ELF::R_MIPS_PC19_S2)
1455 Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1456 else if (RelType == ELF::R_MIPS_PC21_S2)
1457 Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1458 else if (RelType == ELF::R_MIPS_PC26_S2)
1459 Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1460 processSimpleRelocation(SectionID, Offset, RelType, Value);
1461 }
1462 } else if (IsMipsN32ABI || IsMipsN64ABI) {
1463 uint32_t r_type = RelType & 0xff;
1464 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1465 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1466 || r_type == ELF::R_MIPS_GOT_DISP) {
1467 StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName);
1468 if (i != GOTSymbolOffsets.end())
1469 RE.SymOffset = i->second;
1470 else {
1471 RE.SymOffset = allocateGOTEntries(1);
1472 GOTSymbolOffsets[TargetName] = RE.SymOffset;
1473 }
1474 if (Value.SymbolName)
1475 addRelocationForSymbol(RE, Value.SymbolName);
1476 else
1477 addRelocationForSection(RE, Value.SectionID);
1478 } else if (RelType == ELF::R_MIPS_26) {
1479 // This is an Mips branch relocation, need to use a stub function.
1480 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1481 SectionEntry &Section = Sections[SectionID];
1482
1483 // Look up for existing stub.
1484 StubMap::const_iterator i = Stubs.find(Value);
1485 if (i != Stubs.end()) {
1486 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1487 addRelocationForSection(RE, SectionID);
1488 LLVM_DEBUG(dbgs() << " Stub function found\n");
1489 } else {
1490 // Create a new stub function.
1491 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1492 Stubs[Value] = Section.getStubOffset();
1493
1494 unsigned AbiVariant = Obj.getPlatformFlags();
1495
1496 uint8_t *StubTargetAddr = createStubFunction(
1497 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1498
1499 if (IsMipsN32ABI) {
1500 // Creating Hi and Lo relocations for the filled stub instructions.
1501 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1502 ELF::R_MIPS_HI16, Value.Addend);
1503 RelocationEntry RELo(SectionID,
1504 StubTargetAddr - Section.getAddress() + 4,
1505 ELF::R_MIPS_LO16, Value.Addend);
1506 if (Value.SymbolName) {
1507 addRelocationForSymbol(REHi, Value.SymbolName);
1508 addRelocationForSymbol(RELo, Value.SymbolName);
1509 } else {
1510 addRelocationForSection(REHi, Value.SectionID);
1511 addRelocationForSection(RELo, Value.SectionID);
1512 }
1513 } else {
1514 // Creating Highest, Higher, Hi and Lo relocations for the filled stub
1515 // instructions.
1516 RelocationEntry REHighest(SectionID,
1517 StubTargetAddr - Section.getAddress(),
1518 ELF::R_MIPS_HIGHEST, Value.Addend);
1519 RelocationEntry REHigher(SectionID,
1520 StubTargetAddr - Section.getAddress() + 4,
1521 ELF::R_MIPS_HIGHER, Value.Addend);
1522 RelocationEntry REHi(SectionID,
1523 StubTargetAddr - Section.getAddress() + 12,
1524 ELF::R_MIPS_HI16, Value.Addend);
1525 RelocationEntry RELo(SectionID,
1526 StubTargetAddr - Section.getAddress() + 20,
1527 ELF::R_MIPS_LO16, Value.Addend);
1528 if (Value.SymbolName) {
1529 addRelocationForSymbol(REHighest, Value.SymbolName);
1530 addRelocationForSymbol(REHigher, Value.SymbolName);
1531 addRelocationForSymbol(REHi, Value.SymbolName);
1532 addRelocationForSymbol(RELo, Value.SymbolName);
1533 } else {
1534 addRelocationForSection(REHighest, Value.SectionID);
1535 addRelocationForSection(REHigher, Value.SectionID);
1536 addRelocationForSection(REHi, Value.SectionID);
1537 addRelocationForSection(RELo, Value.SectionID);
1538 }
1539 }
1540 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1541 addRelocationForSection(RE, SectionID);
1542 Section.advanceStubOffset(getMaxStubSize());
1543 }
1544 } else {
1545 processSimpleRelocation(SectionID, Offset, RelType, Value);
1546 }
1547
1548 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
1549 if (RelType == ELF::R_PPC64_REL24) {
1550 // Determine ABI variant in use for this object.
1551 unsigned AbiVariant = Obj.getPlatformFlags();
1552 AbiVariant &= ELF::EF_PPC64_ABI;
1553 // A PPC branch relocation will need a stub function if the target is
1554 // an external symbol (either Value.SymbolName is set, or SymType is
1555 // Symbol::ST_Unknown) or if the target address is not within the
1556 // signed 24-bits branch address.
1557 SectionEntry &Section = Sections[SectionID];
1558 uint8_t *Target = Section.getAddressWithOffset(Offset);
1559 bool RangeOverflow = false;
1560 bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown;
1561 if (!IsExtern) {
1562 if (AbiVariant != 2) {
1563 // In the ELFv1 ABI, a function call may point to the .opd entry,
1564 // so the final symbol value is calculated based on the relocation
1565 // values in the .opd section.
1566 if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
1567 return std::move(Err);
1568 } else {
1569 // In the ELFv2 ABI, a function symbol may provide a local entry
1570 // point, which must be used for direct calls.
1571 if (Value.SectionID == SectionID){
1572 uint8_t SymOther = Symbol->getOther();
1573 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1574 }
1575 }
1576 uint8_t *RelocTarget =
1577 Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
1578 int64_t delta = static_cast<int64_t>(Target - RelocTarget);
1579 // If it is within 26-bits branch range, just set the branch target
1580 if (SignExtend64<26>(delta) != delta) {
1581 RangeOverflow = true;
1582 } else if ((AbiVariant != 2) ||
1583 (AbiVariant == 2 && Value.SectionID == SectionID)) {
1584 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1585 addRelocationForSection(RE, Value.SectionID);
1586 }
1587 }
1588 if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID) ||
1589 RangeOverflow) {
1590 // It is an external symbol (either Value.SymbolName is set, or
1591 // SymType is SymbolRef::ST_Unknown) or out of range.
1592 StubMap::const_iterator i = Stubs.find(Value);
1593 if (i != Stubs.end()) {
1594 // Symbol function stub already created, just relocate to it
1595 resolveRelocation(Section, Offset,
1596 reinterpret_cast<uint64_t>(
1597 Section.getAddressWithOffset(i->second)),
1598 RelType, 0);
1599 LLVM_DEBUG(dbgs() << " Stub function found\n");
1600 } else {
1601 // Create a new stub function.
1602 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1603 Stubs[Value] = Section.getStubOffset();
1604 uint8_t *StubTargetAddr = createStubFunction(
1605 Section.getAddressWithOffset(Section.getStubOffset()),
1606 AbiVariant);
1607 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1608 ELF::R_PPC64_ADDR64, Value.Addend);
1609
1610 // Generates the 64-bits address loads as exemplified in section
1611 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1612 // apply to the low part of the instructions, so we have to update
1613 // the offset according to the target endianness.
1614 uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
1615 if (!IsTargetLittleEndian)
1616 StubRelocOffset += 2;
1617
1618 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
1619 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
1620 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
1621 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
1622 RelocationEntry REh(SectionID, StubRelocOffset + 12,
1623 ELF::R_PPC64_ADDR16_HI, Value.Addend);
1624 RelocationEntry REl(SectionID, StubRelocOffset + 16,
1625 ELF::R_PPC64_ADDR16_LO, Value.Addend);
1626
1627 if (Value.SymbolName) {
1628 addRelocationForSymbol(REhst, Value.SymbolName);
1629 addRelocationForSymbol(REhr, Value.SymbolName);
1630 addRelocationForSymbol(REh, Value.SymbolName);
1631 addRelocationForSymbol(REl, Value.SymbolName);
1632 } else {
1633 addRelocationForSection(REhst, Value.SectionID);
1634 addRelocationForSection(REhr, Value.SectionID);
1635 addRelocationForSection(REh, Value.SectionID);
1636 addRelocationForSection(REl, Value.SectionID);
1637 }
1638
1639 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1640 Section.getAddressWithOffset(
1641 Section.getStubOffset())),
1642 RelType, 0);
1643 Section.advanceStubOffset(getMaxStubSize());
1644 }
1645 if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) {
1646 // Restore the TOC for external calls
1647 if (AbiVariant == 2)
1648 writeInt32BE(Target + 4, 0xE8410018); // ld r2,24(r1)
1649 else
1650 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1651 }
1652 }
1653 } else if (RelType == ELF::R_PPC64_TOC16 ||
1654 RelType == ELF::R_PPC64_TOC16_DS ||
1655 RelType == ELF::R_PPC64_TOC16_LO ||
1656 RelType == ELF::R_PPC64_TOC16_LO_DS ||
1657 RelType == ELF::R_PPC64_TOC16_HI ||
1658 RelType == ELF::R_PPC64_TOC16_HA) {
1659 // These relocations are supposed to subtract the TOC address from
1660 // the final value. This does not fit cleanly into the RuntimeDyld
1661 // scheme, since there may be *two* sections involved in determining
1662 // the relocation value (the section of the symbol referred to by the
1663 // relocation, and the TOC section associated with the current module).
1664 //
1665 // Fortunately, these relocations are currently only ever generated
1666 // referring to symbols that themselves reside in the TOC, which means
1667 // that the two sections are actually the same. Thus they cancel out
1668 // and we can immediately resolve the relocation right now.
1669 switch (RelType) {
1670 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1671 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1672 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1673 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1674 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1675 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1676 default: llvm_unreachable("Wrong relocation type.");
1677 }
1678
1679 RelocationValueRef TOCValue;
1680 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1681 return std::move(Err);
1682 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1683 llvm_unreachable("Unsupported TOC relocation.");
1684 Value.Addend -= TOCValue.Addend;
1685 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
1686 } else {
1687 // There are two ways to refer to the TOC address directly: either
1688 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1689 // ignored), or via any relocation that refers to the magic ".TOC."
1690 // symbols (in which case the addend is respected).
1691 if (RelType == ELF::R_PPC64_TOC) {
1692 RelType = ELF::R_PPC64_ADDR64;
1693 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1694 return std::move(Err);
1695 } else if (TargetName == ".TOC.") {
1696 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1697 return std::move(Err);
1698 Value.Addend += Addend;
1699 }
1700
1701 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1702
1703 if (Value.SymbolName)
1704 addRelocationForSymbol(RE, Value.SymbolName);
1705 else
1706 addRelocationForSection(RE, Value.SectionID);
1707 }
1708 } else if (Arch == Triple::systemz &&
1709 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1710 // Create function stubs for both PLT and GOT references, regardless of
1711 // whether the GOT reference is to data or code. The stub contains the
1712 // full address of the symbol, as needed by GOT references, and the
1713 // executable part only adds an overhead of 8 bytes.
1714 //
1715 // We could try to conserve space by allocating the code and data
1716 // parts of the stub separately. However, as things stand, we allocate
1717 // a stub for every relocation, so using a GOT in JIT code should be
1718 // no less space efficient than using an explicit constant pool.
1719 LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1720 SectionEntry &Section = Sections[SectionID];
1721
1722 // Look for an existing stub.
1723 StubMap::const_iterator i = Stubs.find(Value);
1724 uintptr_t StubAddress;
1725 if (i != Stubs.end()) {
1726 StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
1727 LLVM_DEBUG(dbgs() << " Stub function found\n");
1728 } else {
1729 // Create a new stub function.
1730 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1731
1732 uintptr_t BaseAddress = uintptr_t(Section.getAddress());
1733 StubAddress =
1734 alignTo(BaseAddress + Section.getStubOffset(), getStubAlignment());
1735 unsigned StubOffset = StubAddress - BaseAddress;
1736
1737 Stubs[Value] = StubOffset;
1738 createStubFunction((uint8_t *)StubAddress);
1739 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
1740 Value.Offset);
1741 if (Value.SymbolName)
1742 addRelocationForSymbol(RE, Value.SymbolName);
1743 else
1744 addRelocationForSection(RE, Value.SectionID);
1745 Section.advanceStubOffset(getMaxStubSize());
1746 }
1747
1748 if (RelType == ELF::R_390_GOTENT)
1749 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1750 Addend);
1751 else
1752 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1753 } else if (Arch == Triple::x86_64) {
1754 if (RelType == ELF::R_X86_64_PLT32) {
1755 // The way the PLT relocations normally work is that the linker allocates
1756 // the
1757 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
1758 // entry will then jump to an address provided by the GOT. On first call,
1759 // the
1760 // GOT address will point back into PLT code that resolves the symbol. After
1761 // the first call, the GOT entry points to the actual function.
1762 //
1763 // For local functions we're ignoring all of that here and just replacing
1764 // the PLT32 relocation type with PC32, which will translate the relocation
1765 // into a PC-relative call directly to the function. For external symbols we
1766 // can't be sure the function will be within 2^32 bytes of the call site, so
1767 // we need to create a stub, which calls into the GOT. This case is
1768 // equivalent to the usual PLT implementation except that we use the stub
1769 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1770 // rather than allocating a PLT section.
1771 if (Value.SymbolName && MemMgr.allowStubAllocation()) {
1772 // This is a call to an external function.
1773 // Look for an existing stub.
1774 SectionEntry *Section = &Sections[SectionID];
1775 StubMap::const_iterator i = Stubs.find(Value);
1776 uintptr_t StubAddress;
1777 if (i != Stubs.end()) {
1778 StubAddress = uintptr_t(Section->getAddress()) + i->second;
1779 LLVM_DEBUG(dbgs() << " Stub function found\n");
1780 } else {
1781 // Create a new stub function (equivalent to a PLT entry).
1782 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1783
1784 uintptr_t BaseAddress = uintptr_t(Section->getAddress());
1785 StubAddress = alignTo(BaseAddress + Section->getStubOffset(),
1786 getStubAlignment());
1787 unsigned StubOffset = StubAddress - BaseAddress;
1788 Stubs[Value] = StubOffset;
1789 createStubFunction((uint8_t *)StubAddress);
1790
1791 // Bump our stub offset counter
1792 Section->advanceStubOffset(getMaxStubSize());
1793
1794 // Allocate a GOT Entry
1795 uint64_t GOTOffset = allocateGOTEntries(1);
1796 // This potentially creates a new Section which potentially
1797 // invalidates the Section pointer, so reload it.
1798 Section = &Sections[SectionID];
1799
1800 // The load of the GOT address has an addend of -4
1801 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1802 ELF::R_X86_64_PC32);
1803
1804 // Fill in the value of the symbol we're targeting into the GOT
1805 addRelocationForSymbol(
1806 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1807 Value.SymbolName);
1808 }
1809
1810 // Make the target call a call into the stub table.
1811 resolveRelocation(*Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1812 Addend);
1813 } else {
1814 Value.Addend += support::ulittle32_t::ref(
1815 computePlaceholderAddress(SectionID, Offset));
1816 processSimpleRelocation(SectionID, Offset, ELF::R_X86_64_PC32, Value);
1817 }
1818 } else if (RelType == ELF::R_X86_64_GOTPCREL ||
1819 RelType == ELF::R_X86_64_GOTPCRELX ||
1820 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1821 uint64_t GOTOffset = allocateGOTEntries(1);
1822 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1823 ELF::R_X86_64_PC32);
1824
1825 // Fill in the value of the symbol we're targeting into the GOT
1826 RelocationEntry RE =
1827 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
1828 if (Value.SymbolName)
1829 addRelocationForSymbol(RE, Value.SymbolName);
1830 else
1831 addRelocationForSection(RE, Value.SectionID);
1832 } else if (RelType == ELF::R_X86_64_GOT64) {
1833 // Fill in a 64-bit GOT offset.
1834 uint64_t GOTOffset = allocateGOTEntries(1);
1835 resolveRelocation(Sections[SectionID], Offset, GOTOffset,
1836 ELF::R_X86_64_64, 0);
1837
1838 // Fill in the value of the symbol we're targeting into the GOT
1839 RelocationEntry RE =
1840 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
1841 if (Value.SymbolName)
1842 addRelocationForSymbol(RE, Value.SymbolName);
1843 else
1844 addRelocationForSection(RE, Value.SectionID);
1845 } else if (RelType == ELF::R_X86_64_GOTPC32) {
1846 // Materialize the address of the base of the GOT relative to the PC.
1847 // This doesn't create a GOT entry, but it does mean we need a GOT
1848 // section.
1849 (void)allocateGOTEntries(0);
1850 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC32);
1851 } else if (RelType == ELF::R_X86_64_GOTPC64) {
1852 (void)allocateGOTEntries(0);
1853 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
1854 } else if (RelType == ELF::R_X86_64_GOTOFF64) {
1855 // GOTOFF relocations ultimately require a section difference relocation.
1856 (void)allocateGOTEntries(0);
1857 processSimpleRelocation(SectionID, Offset, RelType, Value);
1858 } else if (RelType == ELF::R_X86_64_PC32) {
1859 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
1860 processSimpleRelocation(SectionID, Offset, RelType, Value);
1861 } else if (RelType == ELF::R_X86_64_PC64) {
1862 Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset));
1863 processSimpleRelocation(SectionID, Offset, RelType, Value);
1864 } else if (RelType == ELF::R_X86_64_GOTTPOFF) {
1865 processX86_64GOTTPOFFRelocation(SectionID, Offset, Value, Addend);
1866 } else if (RelType == ELF::R_X86_64_TLSGD ||
1867 RelType == ELF::R_X86_64_TLSLD) {
1868 // The next relocation must be the relocation for __tls_get_addr.
1869 ++RelI;
1870 auto &GetAddrRelocation = *RelI;
1871 processX86_64TLSRelocation(SectionID, Offset, RelType, Value, Addend,
1872 GetAddrRelocation);
1873 } else {
1874 processSimpleRelocation(SectionID, Offset, RelType, Value);
1875 }
1876 } else {
1877 if (Arch == Triple::x86) {
1878 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
1879 }
1880 processSimpleRelocation(SectionID, Offset, RelType, Value);
1881 }
1882 return ++RelI;
1883 }
1884
processX86_64GOTTPOFFRelocation(unsigned SectionID,uint64_t Offset,RelocationValueRef Value,int64_t Addend)1885 void RuntimeDyldELF::processX86_64GOTTPOFFRelocation(unsigned SectionID,
1886 uint64_t Offset,
1887 RelocationValueRef Value,
1888 int64_t Addend) {
1889 // Use the approach from "x86-64 Linker Optimizations" from the TLS spec
1890 // to replace the GOTTPOFF relocation with a TPOFF relocation. The spec
1891 // only mentions one optimization even though there are two different
1892 // code sequences for the Initial Exec TLS Model. We match the code to
1893 // find out which one was used.
1894
1895 // A possible TLS code sequence and its replacement
1896 struct CodeSequence {
1897 // The expected code sequence
1898 ArrayRef<uint8_t> ExpectedCodeSequence;
1899 // The negative offset of the GOTTPOFF relocation to the beginning of
1900 // the sequence
1901 uint64_t TLSSequenceOffset;
1902 // The new code sequence
1903 ArrayRef<uint8_t> NewCodeSequence;
1904 // The offset of the new TPOFF relocation
1905 uint64_t TpoffRelocationOffset;
1906 };
1907
1908 std::array<CodeSequence, 2> CodeSequences;
1909
1910 // Initial Exec Code Model Sequence
1911 {
1912 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
1913 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
1914 0x00, // mov %fs:0, %rax
1915 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // add x@gotpoff(%rip),
1916 // %rax
1917 };
1918 CodeSequences[0].ExpectedCodeSequence =
1919 ArrayRef<uint8_t>(ExpectedCodeSequenceList);
1920 CodeSequences[0].TLSSequenceOffset = 12;
1921
1922 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
1923 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0, %rax
1924 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax), %rax
1925 };
1926 CodeSequences[0].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
1927 CodeSequences[0].TpoffRelocationOffset = 12;
1928 }
1929
1930 // Initial Exec Code Model Sequence, II
1931 {
1932 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
1933 0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00, // mov x@gotpoff(%rip), %rax
1934 0x64, 0x48, 0x8b, 0x00, 0x00, 0x00, 0x00 // mov %fs:(%rax), %rax
1935 };
1936 CodeSequences[1].ExpectedCodeSequence =
1937 ArrayRef<uint8_t>(ExpectedCodeSequenceList);
1938 CodeSequences[1].TLSSequenceOffset = 3;
1939
1940 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
1941 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, // 6 byte nop
1942 0x64, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:x@tpoff, %rax
1943 };
1944 CodeSequences[1].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
1945 CodeSequences[1].TpoffRelocationOffset = 10;
1946 }
1947
1948 bool Resolved = false;
1949 auto &Section = Sections[SectionID];
1950 for (const auto &C : CodeSequences) {
1951 assert(C.ExpectedCodeSequence.size() == C.NewCodeSequence.size() &&
1952 "Old and new code sequences must have the same size");
1953
1954 if (Offset < C.TLSSequenceOffset ||
1955 (Offset - C.TLSSequenceOffset + C.NewCodeSequence.size()) >
1956 Section.getSize()) {
1957 // This can't be a matching sequence as it doesn't fit in the current
1958 // section
1959 continue;
1960 }
1961
1962 auto TLSSequenceStartOffset = Offset - C.TLSSequenceOffset;
1963 auto *TLSSequence = Section.getAddressWithOffset(TLSSequenceStartOffset);
1964 if (ArrayRef<uint8_t>(TLSSequence, C.ExpectedCodeSequence.size()) !=
1965 C.ExpectedCodeSequence) {
1966 continue;
1967 }
1968
1969 memcpy(TLSSequence, C.NewCodeSequence.data(), C.NewCodeSequence.size());
1970
1971 // The original GOTTPOFF relocation has an addend as it is PC relative,
1972 // so it needs to be corrected. The TPOFF32 relocation is used as an
1973 // absolute value (which is an offset from %fs:0), so remove the addend
1974 // again.
1975 RelocationEntry RE(SectionID,
1976 TLSSequenceStartOffset + C.TpoffRelocationOffset,
1977 ELF::R_X86_64_TPOFF32, Value.Addend - Addend);
1978
1979 if (Value.SymbolName)
1980 addRelocationForSymbol(RE, Value.SymbolName);
1981 else
1982 addRelocationForSection(RE, Value.SectionID);
1983
1984 Resolved = true;
1985 break;
1986 }
1987
1988 if (!Resolved) {
1989 // The GOTTPOFF relocation was not used in one of the sequences
1990 // described in the spec, so we can't optimize it to a TPOFF
1991 // relocation.
1992 uint64_t GOTOffset = allocateGOTEntries(1);
1993 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1994 ELF::R_X86_64_PC32);
1995 RelocationEntry RE =
1996 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_TPOFF64);
1997 if (Value.SymbolName)
1998 addRelocationForSymbol(RE, Value.SymbolName);
1999 else
2000 addRelocationForSection(RE, Value.SectionID);
2001 }
2002 }
2003
processX86_64TLSRelocation(unsigned SectionID,uint64_t Offset,uint64_t RelType,RelocationValueRef Value,int64_t Addend,const RelocationRef & GetAddrRelocation)2004 void RuntimeDyldELF::processX86_64TLSRelocation(
2005 unsigned SectionID, uint64_t Offset, uint64_t RelType,
2006 RelocationValueRef Value, int64_t Addend,
2007 const RelocationRef &GetAddrRelocation) {
2008 // Since we are statically linking and have no additional DSOs, we can resolve
2009 // the relocation directly without using __tls_get_addr.
2010 // Use the approach from "x86-64 Linker Optimizations" from the TLS spec
2011 // to replace it with the Local Exec relocation variant.
2012
2013 // Find out whether the code was compiled with the large or small memory
2014 // model. For this we look at the next relocation which is the relocation
2015 // for the __tls_get_addr function. If it's a 32 bit relocation, it's the
2016 // small code model, with a 64 bit relocation it's the large code model.
2017 bool IsSmallCodeModel;
2018 // Is the relocation for the __tls_get_addr a PC-relative GOT relocation?
2019 bool IsGOTPCRel = false;
2020
2021 switch (GetAddrRelocation.getType()) {
2022 case ELF::R_X86_64_GOTPCREL:
2023 case ELF::R_X86_64_REX_GOTPCRELX:
2024 case ELF::R_X86_64_GOTPCRELX:
2025 IsGOTPCRel = true;
2026 [[fallthrough]];
2027 case ELF::R_X86_64_PLT32:
2028 IsSmallCodeModel = true;
2029 break;
2030 case ELF::R_X86_64_PLTOFF64:
2031 IsSmallCodeModel = false;
2032 break;
2033 default:
2034 report_fatal_error(
2035 "invalid TLS relocations for General/Local Dynamic TLS Model: "
2036 "expected PLT or GOT relocation for __tls_get_addr function");
2037 }
2038
2039 // The negative offset to the start of the TLS code sequence relative to
2040 // the offset of the TLSGD/TLSLD relocation
2041 uint64_t TLSSequenceOffset;
2042 // The expected start of the code sequence
2043 ArrayRef<uint8_t> ExpectedCodeSequence;
2044 // The new TLS code sequence that will replace the existing code
2045 ArrayRef<uint8_t> NewCodeSequence;
2046
2047 if (RelType == ELF::R_X86_64_TLSGD) {
2048 // The offset of the new TPOFF32 relocation (offset starting from the
2049 // beginning of the whole TLS sequence)
2050 uint64_t TpoffRelocOffset;
2051
2052 if (IsSmallCodeModel) {
2053 if (!IsGOTPCRel) {
2054 static const std::initializer_list<uint8_t> CodeSequence = {
2055 0x66, // data16 (no-op prefix)
2056 0x48, 0x8d, 0x3d, 0x00, 0x00,
2057 0x00, 0x00, // lea <disp32>(%rip), %rdi
2058 0x66, 0x66, // two data16 prefixes
2059 0x48, // rex64 (no-op prefix)
2060 0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt
2061 };
2062 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2063 TLSSequenceOffset = 4;
2064 } else {
2065 // This code sequence is not described in the TLS spec but gcc
2066 // generates it sometimes.
2067 static const std::initializer_list<uint8_t> CodeSequence = {
2068 0x66, // data16 (no-op prefix)
2069 0x48, 0x8d, 0x3d, 0x00, 0x00,
2070 0x00, 0x00, // lea <disp32>(%rip), %rdi
2071 0x66, // data16 prefix (no-op prefix)
2072 0x48, // rex64 (no-op prefix)
2073 0xff, 0x15, 0x00, 0x00, 0x00,
2074 0x00 // call *__tls_get_addr@gotpcrel(%rip)
2075 };
2076 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2077 TLSSequenceOffset = 4;
2078 }
2079
2080 // The replacement code for the small code model. It's the same for
2081 // both sequences.
2082 static const std::initializer_list<uint8_t> SmallSequence = {
2083 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2084 0x00, // mov %fs:0, %rax
2085 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax),
2086 // %rax
2087 };
2088 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2089 TpoffRelocOffset = 12;
2090 } else {
2091 static const std::initializer_list<uint8_t> CodeSequence = {
2092 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip),
2093 // %rdi
2094 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2095 0x00, // movabs $__tls_get_addr@pltoff, %rax
2096 0x48, 0x01, 0xd8, // add %rbx, %rax
2097 0xff, 0xd0 // call *%rax
2098 };
2099 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2100 TLSSequenceOffset = 3;
2101
2102 // The replacement code for the large code model
2103 static const std::initializer_list<uint8_t> LargeSequence = {
2104 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2105 0x00, // mov %fs:0, %rax
2106 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00, // lea x@tpoff(%rax),
2107 // %rax
2108 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 // nopw 0x0(%rax,%rax,1)
2109 };
2110 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2111 TpoffRelocOffset = 12;
2112 }
2113
2114 // The TLSGD/TLSLD relocations are PC-relative, so they have an addend.
2115 // The new TPOFF32 relocations is used as an absolute offset from
2116 // %fs:0, so remove the TLSGD/TLSLD addend again.
2117 RelocationEntry RE(SectionID, Offset - TLSSequenceOffset + TpoffRelocOffset,
2118 ELF::R_X86_64_TPOFF32, Value.Addend - Addend);
2119 if (Value.SymbolName)
2120 addRelocationForSymbol(RE, Value.SymbolName);
2121 else
2122 addRelocationForSection(RE, Value.SectionID);
2123 } else if (RelType == ELF::R_X86_64_TLSLD) {
2124 if (IsSmallCodeModel) {
2125 if (!IsGOTPCRel) {
2126 static const std::initializer_list<uint8_t> CodeSequence = {
2127 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi
2128 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt
2129 };
2130 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2131 TLSSequenceOffset = 3;
2132
2133 // The replacement code for the small code model
2134 static const std::initializer_list<uint8_t> SmallSequence = {
2135 0x66, 0x66, 0x66, // three data16 prefixes (no-op)
2136 0x64, 0x48, 0x8b, 0x04, 0x25,
2137 0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax
2138 };
2139 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2140 } else {
2141 // This code sequence is not described in the TLS spec but gcc
2142 // generates it sometimes.
2143 static const std::initializer_list<uint8_t> CodeSequence = {
2144 0x48, 0x8d, 0x3d, 0x00,
2145 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi
2146 0xff, 0x15, 0x00, 0x00,
2147 0x00, 0x00 // call
2148 // *__tls_get_addr@gotpcrel(%rip)
2149 };
2150 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2151 TLSSequenceOffset = 3;
2152
2153 // The replacement is code is just like above but it needs to be
2154 // one byte longer.
2155 static const std::initializer_list<uint8_t> SmallSequence = {
2156 0x0f, 0x1f, 0x40, 0x00, // 4 byte nop
2157 0x64, 0x48, 0x8b, 0x04, 0x25,
2158 0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax
2159 };
2160 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2161 }
2162 } else {
2163 // This is the same sequence as for the TLSGD sequence with the large
2164 // memory model above
2165 static const std::initializer_list<uint8_t> CodeSequence = {
2166 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip),
2167 // %rdi
2168 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2169 0x48, // movabs $__tls_get_addr@pltoff, %rax
2170 0x01, 0xd8, // add %rbx, %rax
2171 0xff, 0xd0 // call *%rax
2172 };
2173 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2174 TLSSequenceOffset = 3;
2175
2176 // The replacement code for the large code model
2177 static const std::initializer_list<uint8_t> LargeSequence = {
2178 0x66, 0x66, 0x66, // three data16 prefixes (no-op)
2179 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00,
2180 0x00, // 10 byte nop
2181 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
2182 };
2183 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2184 }
2185 } else {
2186 llvm_unreachable("both TLS relocations handled above");
2187 }
2188
2189 assert(ExpectedCodeSequence.size() == NewCodeSequence.size() &&
2190 "Old and new code sequences must have the same size");
2191
2192 auto &Section = Sections[SectionID];
2193 if (Offset < TLSSequenceOffset ||
2194 (Offset - TLSSequenceOffset + NewCodeSequence.size()) >
2195 Section.getSize()) {
2196 report_fatal_error("unexpected end of section in TLS sequence");
2197 }
2198
2199 auto *TLSSequence = Section.getAddressWithOffset(Offset - TLSSequenceOffset);
2200 if (ArrayRef<uint8_t>(TLSSequence, ExpectedCodeSequence.size()) !=
2201 ExpectedCodeSequence) {
2202 report_fatal_error(
2203 "invalid TLS sequence for Global/Local Dynamic TLS Model");
2204 }
2205
2206 memcpy(TLSSequence, NewCodeSequence.data(), NewCodeSequence.size());
2207 }
2208
getGOTEntrySize()2209 size_t RuntimeDyldELF::getGOTEntrySize() {
2210 // We don't use the GOT in all of these cases, but it's essentially free
2211 // to put them all here.
2212 size_t Result = 0;
2213 switch (Arch) {
2214 case Triple::x86_64:
2215 case Triple::aarch64:
2216 case Triple::aarch64_be:
2217 case Triple::ppc64:
2218 case Triple::ppc64le:
2219 case Triple::systemz:
2220 Result = sizeof(uint64_t);
2221 break;
2222 case Triple::x86:
2223 case Triple::arm:
2224 case Triple::thumb:
2225 Result = sizeof(uint32_t);
2226 break;
2227 case Triple::mips:
2228 case Triple::mipsel:
2229 case Triple::mips64:
2230 case Triple::mips64el:
2231 if (IsMipsO32ABI || IsMipsN32ABI)
2232 Result = sizeof(uint32_t);
2233 else if (IsMipsN64ABI)
2234 Result = sizeof(uint64_t);
2235 else
2236 llvm_unreachable("Mips ABI not handled");
2237 break;
2238 default:
2239 llvm_unreachable("Unsupported CPU type!");
2240 }
2241 return Result;
2242 }
2243
allocateGOTEntries(unsigned no)2244 uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no) {
2245 if (GOTSectionID == 0) {
2246 GOTSectionID = Sections.size();
2247 // Reserve a section id. We'll allocate the section later
2248 // once we know the total size
2249 Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0));
2250 }
2251 uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
2252 CurrentGOTIndex += no;
2253 return StartOffset;
2254 }
2255
findOrAllocGOTEntry(const RelocationValueRef & Value,unsigned GOTRelType)2256 uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef &Value,
2257 unsigned GOTRelType) {
2258 auto E = GOTOffsetMap.insert({Value, 0});
2259 if (E.second) {
2260 uint64_t GOTOffset = allocateGOTEntries(1);
2261
2262 // Create relocation for newly created GOT entry
2263 RelocationEntry RE =
2264 computeGOTOffsetRE(GOTOffset, Value.Offset, GOTRelType);
2265 if (Value.SymbolName)
2266 addRelocationForSymbol(RE, Value.SymbolName);
2267 else
2268 addRelocationForSection(RE, Value.SectionID);
2269
2270 E.first->second = GOTOffset;
2271 }
2272
2273 return E.first->second;
2274 }
2275
resolveGOTOffsetRelocation(unsigned SectionID,uint64_t Offset,uint64_t GOTOffset,uint32_t Type)2276 void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID,
2277 uint64_t Offset,
2278 uint64_t GOTOffset,
2279 uint32_t Type) {
2280 // Fill in the relative address of the GOT Entry into the stub
2281 RelocationEntry GOTRE(SectionID, Offset, Type, GOTOffset);
2282 addRelocationForSection(GOTRE, GOTSectionID);
2283 }
2284
computeGOTOffsetRE(uint64_t GOTOffset,uint64_t SymbolOffset,uint32_t Type)2285 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
2286 uint64_t SymbolOffset,
2287 uint32_t Type) {
2288 return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
2289 }
2290
processNewSymbol(const SymbolRef & ObjSymbol,SymbolTableEntry & Symbol)2291 void RuntimeDyldELF::processNewSymbol(const SymbolRef &ObjSymbol, SymbolTableEntry& Symbol) {
2292 // This should never return an error as `processNewSymbol` wouldn't have been
2293 // called if getFlags() returned an error before.
2294 auto ObjSymbolFlags = cantFail(ObjSymbol.getFlags());
2295
2296 if (ObjSymbolFlags & SymbolRef::SF_Indirect) {
2297 if (IFuncStubSectionID == 0) {
2298 // Create a dummy section for the ifunc stubs. It will be actually
2299 // allocated in finalizeLoad() below.
2300 IFuncStubSectionID = Sections.size();
2301 Sections.push_back(
2302 SectionEntry(".text.__llvm_IFuncStubs", nullptr, 0, 0, 0));
2303 // First 64B are reserverd for the IFunc resolver
2304 IFuncStubOffset = 64;
2305 }
2306
2307 IFuncStubs.push_back(IFuncStub{IFuncStubOffset, Symbol});
2308 // Modify the symbol so that it points to the ifunc stub instead of to the
2309 // resolver function.
2310 Symbol = SymbolTableEntry(IFuncStubSectionID, IFuncStubOffset,
2311 Symbol.getFlags());
2312 IFuncStubOffset += getMaxIFuncStubSize();
2313 }
2314 }
2315
finalizeLoad(const ObjectFile & Obj,ObjSectionToIDMap & SectionMap)2316 Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
2317 ObjSectionToIDMap &SectionMap) {
2318 if (IsMipsO32ABI)
2319 if (!PendingRelocs.empty())
2320 return make_error<RuntimeDyldError>("Can't find matching LO16 reloc");
2321
2322 // Create the IFunc stubs if necessary. This must be done before processing
2323 // the GOT entries, as the IFunc stubs may create some.
2324 if (IFuncStubSectionID != 0) {
2325 uint8_t *IFuncStubsAddr = MemMgr.allocateCodeSection(
2326 IFuncStubOffset, 1, IFuncStubSectionID, ".text.__llvm_IFuncStubs");
2327 if (!IFuncStubsAddr)
2328 return make_error<RuntimeDyldError>(
2329 "Unable to allocate memory for IFunc stubs!");
2330 Sections[IFuncStubSectionID] =
2331 SectionEntry(".text.__llvm_IFuncStubs", IFuncStubsAddr, IFuncStubOffset,
2332 IFuncStubOffset, 0);
2333
2334 createIFuncResolver(IFuncStubsAddr);
2335
2336 LLVM_DEBUG(dbgs() << "Creating IFunc stubs SectionID: "
2337 << IFuncStubSectionID << " Addr: "
2338 << Sections[IFuncStubSectionID].getAddress() << '\n');
2339 for (auto &IFuncStub : IFuncStubs) {
2340 auto &Symbol = IFuncStub.OriginalSymbol;
2341 LLVM_DEBUG(dbgs() << "\tSectionID: " << Symbol.getSectionID()
2342 << " Offset: " << format("%p", Symbol.getOffset())
2343 << " IFuncStubOffset: "
2344 << format("%p\n", IFuncStub.StubOffset));
2345 createIFuncStub(IFuncStubSectionID, 0, IFuncStub.StubOffset,
2346 Symbol.getSectionID(), Symbol.getOffset());
2347 }
2348
2349 IFuncStubSectionID = 0;
2350 IFuncStubOffset = 0;
2351 IFuncStubs.clear();
2352 }
2353
2354 // If necessary, allocate the global offset table
2355 if (GOTSectionID != 0) {
2356 // Allocate memory for the section
2357 size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
2358 uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
2359 GOTSectionID, ".got", false);
2360 if (!Addr)
2361 return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!");
2362
2363 Sections[GOTSectionID] =
2364 SectionEntry(".got", Addr, TotalSize, TotalSize, 0);
2365
2366 // For now, initialize all GOT entries to zero. We'll fill them in as
2367 // needed when GOT-based relocations are applied.
2368 memset(Addr, 0, TotalSize);
2369 if (IsMipsN32ABI || IsMipsN64ABI) {
2370 // To correctly resolve Mips GOT relocations, we need a mapping from
2371 // object's sections to GOTs.
2372 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
2373 SI != SE; ++SI) {
2374 if (SI->relocation_begin() != SI->relocation_end()) {
2375 Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
2376 if (!RelSecOrErr)
2377 return make_error<RuntimeDyldError>(
2378 toString(RelSecOrErr.takeError()));
2379
2380 section_iterator RelocatedSection = *RelSecOrErr;
2381 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
2382 assert(i != SectionMap.end());
2383 SectionToGOTMap[i->second] = GOTSectionID;
2384 }
2385 }
2386 GOTSymbolOffsets.clear();
2387 }
2388 }
2389
2390 // Look for and record the EH frame section.
2391 ObjSectionToIDMap::iterator i, e;
2392 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
2393 const SectionRef &Section = i->first;
2394
2395 StringRef Name;
2396 Expected<StringRef> NameOrErr = Section.getName();
2397 if (NameOrErr)
2398 Name = *NameOrErr;
2399 else
2400 consumeError(NameOrErr.takeError());
2401
2402 if (Name == ".eh_frame") {
2403 UnregisteredEHFrameSections.push_back(i->second);
2404 break;
2405 }
2406 }
2407
2408 GOTSectionID = 0;
2409 CurrentGOTIndex = 0;
2410
2411 return Error::success();
2412 }
2413
isCompatibleFile(const object::ObjectFile & Obj) const2414 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
2415 return Obj.isELF();
2416 }
2417
createIFuncResolver(uint8_t * Addr) const2418 void RuntimeDyldELF::createIFuncResolver(uint8_t *Addr) const {
2419 if (Arch == Triple::x86_64) {
2420 // The adddres of the GOT1 entry is in %r11, the GOT2 entry is in %r11+8
2421 // (see createIFuncStub() for details)
2422 // The following code first saves all registers that contain the original
2423 // function arguments as those registers are not saved by the resolver
2424 // function. %r11 is saved as well so that the GOT2 entry can be updated
2425 // afterwards. Then it calls the actual IFunc resolver function whose
2426 // address is stored in GOT2. After the resolver function returns, all
2427 // saved registers are restored and the return value is written to GOT1.
2428 // Finally, jump to the now resolved function.
2429 // clang-format off
2430 const uint8_t StubCode[] = {
2431 0x57, // push %rdi
2432 0x56, // push %rsi
2433 0x52, // push %rdx
2434 0x51, // push %rcx
2435 0x41, 0x50, // push %r8
2436 0x41, 0x51, // push %r9
2437 0x41, 0x53, // push %r11
2438 0x41, 0xff, 0x53, 0x08, // call *0x8(%r11)
2439 0x41, 0x5b, // pop %r11
2440 0x41, 0x59, // pop %r9
2441 0x41, 0x58, // pop %r8
2442 0x59, // pop %rcx
2443 0x5a, // pop %rdx
2444 0x5e, // pop %rsi
2445 0x5f, // pop %rdi
2446 0x49, 0x89, 0x03, // mov %rax,(%r11)
2447 0xff, 0xe0 // jmp *%rax
2448 };
2449 // clang-format on
2450 static_assert(sizeof(StubCode) <= 64,
2451 "maximum size of the IFunc resolver is 64B");
2452 memcpy(Addr, StubCode, sizeof(StubCode));
2453 } else {
2454 report_fatal_error(
2455 "IFunc resolver is not supported for target architecture");
2456 }
2457 }
2458
createIFuncStub(unsigned IFuncStubSectionID,uint64_t IFuncResolverOffset,uint64_t IFuncStubOffset,unsigned IFuncSectionID,uint64_t IFuncOffset)2459 void RuntimeDyldELF::createIFuncStub(unsigned IFuncStubSectionID,
2460 uint64_t IFuncResolverOffset,
2461 uint64_t IFuncStubOffset,
2462 unsigned IFuncSectionID,
2463 uint64_t IFuncOffset) {
2464 auto &IFuncStubSection = Sections[IFuncStubSectionID];
2465 auto *Addr = IFuncStubSection.getAddressWithOffset(IFuncStubOffset);
2466
2467 if (Arch == Triple::x86_64) {
2468 // The first instruction loads a PC-relative address into %r11 which is a
2469 // GOT entry for this stub. This initially contains the address to the
2470 // IFunc resolver. We can use %r11 here as it's caller saved but not used
2471 // to pass any arguments. In fact, x86_64 ABI even suggests using %r11 for
2472 // code in the PLT. The IFunc resolver will use %r11 to update the GOT
2473 // entry.
2474 //
2475 // The next instruction just jumps to the address contained in the GOT
2476 // entry. As mentioned above, we do this two-step jump by first setting
2477 // %r11 so that the IFunc resolver has access to it.
2478 //
2479 // The IFunc resolver of course also needs to know the actual address of
2480 // the actual IFunc resolver function. This will be stored in a GOT entry
2481 // right next to the first one for this stub. So, the IFunc resolver will
2482 // be able to call it with %r11+8.
2483 //
2484 // In total, two adjacent GOT entries (+relocation) and one additional
2485 // relocation are required:
2486 // GOT1: Address of the IFunc resolver.
2487 // GOT2: Address of the IFunc resolver function.
2488 // IFuncStubOffset+3: 32-bit PC-relative address of GOT1.
2489 uint64_t GOT1 = allocateGOTEntries(2);
2490 uint64_t GOT2 = GOT1 + getGOTEntrySize();
2491
2492 RelocationEntry RE1(GOTSectionID, GOT1, ELF::R_X86_64_64,
2493 IFuncResolverOffset, {});
2494 addRelocationForSection(RE1, IFuncStubSectionID);
2495 RelocationEntry RE2(GOTSectionID, GOT2, ELF::R_X86_64_64, IFuncOffset, {});
2496 addRelocationForSection(RE2, IFuncSectionID);
2497
2498 const uint8_t StubCode[] = {
2499 0x4c, 0x8d, 0x1d, 0x00, 0x00, 0x00, 0x00, // leaq 0x0(%rip),%r11
2500 0x41, 0xff, 0x23 // jmpq *(%r11)
2501 };
2502 assert(sizeof(StubCode) <= getMaxIFuncStubSize() &&
2503 "IFunc stub size must not exceed getMaxIFuncStubSize()");
2504 memcpy(Addr, StubCode, sizeof(StubCode));
2505
2506 // The PC-relative value starts 4 bytes from the end of the leaq
2507 // instruction, so the addend is -4.
2508 resolveGOTOffsetRelocation(IFuncStubSectionID, IFuncStubOffset + 3,
2509 GOT1 - 4, ELF::R_X86_64_PC32);
2510 } else {
2511 report_fatal_error("IFunc stub is not supported for target architecture");
2512 }
2513 }
2514
getMaxIFuncStubSize() const2515 unsigned RuntimeDyldELF::getMaxIFuncStubSize() const {
2516 if (Arch == Triple::x86_64) {
2517 return 10;
2518 }
2519 return 0;
2520 }
2521
relocationNeedsGot(const RelocationRef & R) const2522 bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef &R) const {
2523 unsigned RelTy = R.getType();
2524 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
2525 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
2526 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
2527
2528 if (Arch == Triple::x86_64)
2529 return RelTy == ELF::R_X86_64_GOTPCREL ||
2530 RelTy == ELF::R_X86_64_GOTPCRELX ||
2531 RelTy == ELF::R_X86_64_GOT64 ||
2532 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
2533 return false;
2534 }
2535
relocationNeedsStub(const RelocationRef & R) const2536 bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const {
2537 if (Arch != Triple::x86_64)
2538 return true; // Conservative answer
2539
2540 switch (R.getType()) {
2541 default:
2542 return true; // Conservative answer
2543
2544
2545 case ELF::R_X86_64_GOTPCREL:
2546 case ELF::R_X86_64_GOTPCRELX:
2547 case ELF::R_X86_64_REX_GOTPCRELX:
2548 case ELF::R_X86_64_GOTPC64:
2549 case ELF::R_X86_64_GOT64:
2550 case ELF::R_X86_64_GOTOFF64:
2551 case ELF::R_X86_64_PC32:
2552 case ELF::R_X86_64_PC64:
2553 case ELF::R_X86_64_64:
2554 // We know that these reloation types won't need a stub function. This list
2555 // can be extended as needed.
2556 return false;
2557 }
2558 }
2559
2560 } // namespace llvm
2561