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 
31 static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
32 
33 static void or32AArch64Imm(void *L, uint64_t Imm) {
34   or32le(L, (Imm & 0xFFF) << 10);
35 }
36 
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 
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.
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
73   static bool classof(const Binary *v) {
74     return (isa<ELFObjectFile<ELFT>>(v) &&
75             classof(cast<ELFObjectFile<ELFT>>(v)));
76   }
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>
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>>>
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>
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>
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:
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>>>
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>
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>
208 LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
209   return createELFDebugObject(Obj, *this);
210 }
211 
212 } // anonymous namespace
213 
214 namespace llvm {
215 
216 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
217                                JITSymbolResolver &Resolver)
218     : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
219 RuntimeDyldELF::~RuntimeDyldELF() {}
220 
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>
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>
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 
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 
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 
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_ABS16: {
426     uint64_t Result = Value + Addend;
427     assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
428     write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
429     break;
430   }
431   case ELF::R_AARCH64_ABS32: {
432     uint64_t Result = Value + Addend;
433     assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
434     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
435     break;
436   }
437   case ELF::R_AARCH64_ABS64:
438     write(isBE, TargetPtr, Value + Addend);
439     break;
440   case ELF::R_AARCH64_PLT32: {
441     uint64_t Result = Value + Addend - FinalAddress;
442     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
443            static_cast<int64_t>(Result) <= INT32_MAX);
444     write(isBE, TargetPtr, static_cast<uint32_t>(Result));
445     break;
446   }
447   case ELF::R_AARCH64_PREL32: {
448     uint64_t Result = Value + Addend - FinalAddress;
449     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
450            static_cast<int64_t>(Result) <= UINT32_MAX);
451     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
452     break;
453   }
454   case ELF::R_AARCH64_PREL64:
455     write(isBE, TargetPtr, Value + Addend - FinalAddress);
456     break;
457   case ELF::R_AARCH64_CONDBR19: {
458     uint64_t BranchImm = Value + Addend - FinalAddress;
459 
460     assert(isInt<21>(BranchImm));
461     *TargetPtr &= 0xff00001fU;
462     // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ
463     or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3);
464     break;
465   }
466   case ELF::R_AARCH64_TSTBR14: {
467     uint64_t BranchImm = Value + Addend - FinalAddress;
468 
469     assert(isInt<16>(BranchImm));
470 
471     *TargetPtr &= 0xfff8001fU;
472     // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ
473     or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) << 3);
474     break;
475   }
476   case ELF::R_AARCH64_CALL26: // fallthrough
477   case ELF::R_AARCH64_JUMP26: {
478     // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
479     // calculation.
480     uint64_t BranchImm = Value + Addend - FinalAddress;
481 
482     // "Check that -2^27 <= result < 2^27".
483     assert(isInt<28>(BranchImm));
484     or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
485     break;
486   }
487   case ELF::R_AARCH64_MOVW_UABS_G3:
488     or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
489     break;
490   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
491     or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
492     break;
493   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
494     or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
495     break;
496   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
497     or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
498     break;
499   case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
500     // Operation: Page(S+A) - Page(P)
501     uint64_t Result =
502         ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
503 
504     // Check that -2^32 <= X < 2^32
505     assert(isInt<33>(Result) && "overflow check failed for relocation");
506 
507     // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
508     // from bits 32:12 of X.
509     write32AArch64Addr(TargetPtr, Result >> 12);
510     break;
511   }
512   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
513     // Operation: S + A
514     // Immediate goes in bits 21:10 of LD/ST instruction, taken
515     // from bits 11:0 of X
516     or32AArch64Imm(TargetPtr, Value + Addend);
517     break;
518   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
519     // Operation: S + A
520     // Immediate goes in bits 21:10 of LD/ST instruction, taken
521     // from bits 11:0 of X
522     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11));
523     break;
524   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
525     // Operation: S + A
526     // Immediate goes in bits 21:10 of LD/ST instruction, taken
527     // from bits 11:1 of X
528     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11));
529     break;
530   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
531     // Operation: S + A
532     // Immediate goes in bits 21:10 of LD/ST instruction, taken
533     // from bits 11:2 of X
534     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
535     break;
536   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
537     // Operation: S + A
538     // Immediate goes in bits 21:10 of LD/ST instruction, taken
539     // from bits 11:3 of X
540     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
541     break;
542   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
543     // Operation: S + A
544     // Immediate goes in bits 21:10 of LD/ST instruction, taken
545     // from bits 11:4 of X
546     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11));
547     break;
548   case ELF::R_AARCH64_LD_PREL_LO19: {
549     // Operation: S + A - P
550     uint64_t Result = Value + Addend - FinalAddress;
551 
552     // "Check that -2^20 <= result < 2^20".
553     assert(isInt<21>(Result));
554 
555     *TargetPtr &= 0xff00001fU;
556     // Immediate goes in bits 23:5 of LD imm instruction, taken
557     // from bits 20:2 of X
558     *TargetPtr |= ((Result & 0xffc) << (5 - 2));
559     break;
560   }
561   case ELF::R_AARCH64_ADR_PREL_LO21: {
562     // Operation: S + A - P
563     uint64_t Result = Value + Addend - FinalAddress;
564 
565     // "Check that -2^20 <= result < 2^20".
566     assert(isInt<21>(Result));
567 
568     *TargetPtr &= 0x9f00001fU;
569     // Immediate goes in bits 23:5, 30:29 of ADR imm instruction, taken
570     // from bits 20:0 of X
571     *TargetPtr |= ((Result & 0xffc) << (5 - 2));
572     *TargetPtr |= (Result & 0x3) << 29;
573     break;
574   }
575   }
576 }
577 
578 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
579                                           uint64_t Offset, uint32_t Value,
580                                           uint32_t Type, int32_t Addend) {
581   // TODO: Add Thumb relocations.
582   uint32_t *TargetPtr =
583       reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
584   uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
585   Value += Addend;
586 
587   LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
588                     << Section.getAddressWithOffset(Offset)
589                     << " FinalAddress: " << format("%p", FinalAddress)
590                     << " Value: " << format("%x", Value)
591                     << " Type: " << format("%x", Type)
592                     << " Addend: " << format("%x", Addend) << "\n");
593 
594   switch (Type) {
595   default:
596     llvm_unreachable("Not implemented relocation type!");
597 
598   case ELF::R_ARM_NONE:
599     break;
600     // Write a 31bit signed offset
601   case ELF::R_ARM_PREL31:
602     support::ulittle32_t::ref{TargetPtr} =
603         (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
604         ((Value - FinalAddress) & ~0x80000000);
605     break;
606   case ELF::R_ARM_TARGET1:
607   case ELF::R_ARM_ABS32:
608     support::ulittle32_t::ref{TargetPtr} = Value;
609     break;
610     // Write first 16 bit of 32 bit value to the mov instruction.
611     // Last 4 bit should be shifted.
612   case ELF::R_ARM_MOVW_ABS_NC:
613   case ELF::R_ARM_MOVT_ABS:
614     if (Type == ELF::R_ARM_MOVW_ABS_NC)
615       Value = Value & 0xFFFF;
616     else if (Type == ELF::R_ARM_MOVT_ABS)
617       Value = (Value >> 16) & 0xFFFF;
618     support::ulittle32_t::ref{TargetPtr} =
619         (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
620         (((Value >> 12) & 0xF) << 16);
621     break;
622     // Write 24 bit relative value to the branch instruction.
623   case ELF::R_ARM_PC24: // Fall through.
624   case ELF::R_ARM_CALL: // Fall through.
625   case ELF::R_ARM_JUMP24:
626     int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
627     RelValue = (RelValue & 0x03FFFFFC) >> 2;
628     assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
629     support::ulittle32_t::ref{TargetPtr} =
630         (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
631     break;
632   }
633 }
634 
635 void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
636   if (Arch == Triple::UnknownArch ||
637       !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
638     IsMipsO32ABI = false;
639     IsMipsN32ABI = false;
640     IsMipsN64ABI = false;
641     return;
642   }
643   if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
644     unsigned AbiVariant = E->getPlatformFlags();
645     IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
646     IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
647   }
648   IsMipsN64ABI = Obj.getFileFormatName().equals("elf64-mips");
649 }
650 
651 // Return the .TOC. section and offset.
652 Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
653                                           ObjSectionToIDMap &LocalSections,
654                                           RelocationValueRef &Rel) {
655   // Set a default SectionID in case we do not find a TOC section below.
656   // This may happen for references to TOC base base (sym@toc, .odp
657   // relocation) without a .toc directive.  In this case just use the
658   // first section (which is usually the .odp) since the code won't
659   // reference the .toc base directly.
660   Rel.SymbolName = nullptr;
661   Rel.SectionID = 0;
662 
663   // The TOC consists of sections .got, .toc, .tocbss, .plt in that
664   // order. The TOC starts where the first of these sections starts.
665   for (auto &Section : Obj.sections()) {
666     Expected<StringRef> NameOrErr = Section.getName();
667     if (!NameOrErr)
668       return NameOrErr.takeError();
669     StringRef SectionName = *NameOrErr;
670 
671     if (SectionName == ".got"
672         || SectionName == ".toc"
673         || SectionName == ".tocbss"
674         || SectionName == ".plt") {
675       if (auto SectionIDOrErr =
676             findOrEmitSection(Obj, Section, false, LocalSections))
677         Rel.SectionID = *SectionIDOrErr;
678       else
679         return SectionIDOrErr.takeError();
680       break;
681     }
682   }
683 
684   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
685   // thus permitting a full 64 Kbytes segment.
686   Rel.Addend = 0x8000;
687 
688   return Error::success();
689 }
690 
691 // Returns the sections and offset associated with the ODP entry referenced
692 // by Symbol.
693 Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
694                                           ObjSectionToIDMap &LocalSections,
695                                           RelocationValueRef &Rel) {
696   // Get the ELF symbol value (st_value) to compare with Relocation offset in
697   // .opd entries
698   for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
699        si != se; ++si) {
700 
701     Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
702     if (!RelSecOrErr)
703       report_fatal_error(toString(RelSecOrErr.takeError()));
704 
705     section_iterator RelSecI = *RelSecOrErr;
706     if (RelSecI == Obj.section_end())
707       continue;
708 
709     Expected<StringRef> NameOrErr = RelSecI->getName();
710     if (!NameOrErr)
711       return NameOrErr.takeError();
712     StringRef RelSectionName = *NameOrErr;
713 
714     if (RelSectionName != ".opd")
715       continue;
716 
717     for (elf_relocation_iterator i = si->relocation_begin(),
718                                  e = si->relocation_end();
719          i != e;) {
720       // The R_PPC64_ADDR64 relocation indicates the first field
721       // of a .opd entry
722       uint64_t TypeFunc = i->getType();
723       if (TypeFunc != ELF::R_PPC64_ADDR64) {
724         ++i;
725         continue;
726       }
727 
728       uint64_t TargetSymbolOffset = i->getOffset();
729       symbol_iterator TargetSymbol = i->getSymbol();
730       int64_t Addend;
731       if (auto AddendOrErr = i->getAddend())
732         Addend = *AddendOrErr;
733       else
734         return AddendOrErr.takeError();
735 
736       ++i;
737       if (i == e)
738         break;
739 
740       // Just check if following relocation is a R_PPC64_TOC
741       uint64_t TypeTOC = i->getType();
742       if (TypeTOC != ELF::R_PPC64_TOC)
743         continue;
744 
745       // Finally compares the Symbol value and the target symbol offset
746       // to check if this .opd entry refers to the symbol the relocation
747       // points to.
748       if (Rel.Addend != (int64_t)TargetSymbolOffset)
749         continue;
750 
751       section_iterator TSI = Obj.section_end();
752       if (auto TSIOrErr = TargetSymbol->getSection())
753         TSI = *TSIOrErr;
754       else
755         return TSIOrErr.takeError();
756       assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
757 
758       bool IsCode = TSI->isText();
759       if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
760                                                   LocalSections))
761         Rel.SectionID = *SectionIDOrErr;
762       else
763         return SectionIDOrErr.takeError();
764       Rel.Addend = (intptr_t)Addend;
765       return Error::success();
766     }
767   }
768   llvm_unreachable("Attempting to get address of ODP entry!");
769 }
770 
771 // Relocation masks following the #lo(value), #hi(value), #ha(value),
772 // #higher(value), #highera(value), #highest(value), and #highesta(value)
773 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
774 // document.
775 
776 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
777 
778 static inline uint16_t applyPPChi(uint64_t value) {
779   return (value >> 16) & 0xffff;
780 }
781 
782 static inline uint16_t applyPPCha (uint64_t value) {
783   return ((value + 0x8000) >> 16) & 0xffff;
784 }
785 
786 static inline uint16_t applyPPChigher(uint64_t value) {
787   return (value >> 32) & 0xffff;
788 }
789 
790 static inline uint16_t applyPPChighera (uint64_t value) {
791   return ((value + 0x8000) >> 32) & 0xffff;
792 }
793 
794 static inline uint16_t applyPPChighest(uint64_t value) {
795   return (value >> 48) & 0xffff;
796 }
797 
798 static inline uint16_t applyPPChighesta (uint64_t value) {
799   return ((value + 0x8000) >> 48) & 0xffff;
800 }
801 
802 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
803                                             uint64_t Offset, uint64_t Value,
804                                             uint32_t Type, int64_t Addend) {
805   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
806   switch (Type) {
807   default:
808     report_fatal_error("Relocation type not implemented yet!");
809     break;
810   case ELF::R_PPC_ADDR16_LO:
811     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
812     break;
813   case ELF::R_PPC_ADDR16_HI:
814     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
815     break;
816   case ELF::R_PPC_ADDR16_HA:
817     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
818     break;
819   }
820 }
821 
822 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
823                                             uint64_t Offset, uint64_t Value,
824                                             uint32_t Type, int64_t Addend) {
825   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
826   switch (Type) {
827   default:
828     report_fatal_error("Relocation type not implemented yet!");
829     break;
830   case ELF::R_PPC64_ADDR16:
831     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
832     break;
833   case ELF::R_PPC64_ADDR16_DS:
834     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
835     break;
836   case ELF::R_PPC64_ADDR16_LO:
837     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
838     break;
839   case ELF::R_PPC64_ADDR16_LO_DS:
840     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
841     break;
842   case ELF::R_PPC64_ADDR16_HI:
843   case ELF::R_PPC64_ADDR16_HIGH:
844     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
845     break;
846   case ELF::R_PPC64_ADDR16_HA:
847   case ELF::R_PPC64_ADDR16_HIGHA:
848     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
849     break;
850   case ELF::R_PPC64_ADDR16_HIGHER:
851     writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
852     break;
853   case ELF::R_PPC64_ADDR16_HIGHERA:
854     writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
855     break;
856   case ELF::R_PPC64_ADDR16_HIGHEST:
857     writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
858     break;
859   case ELF::R_PPC64_ADDR16_HIGHESTA:
860     writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
861     break;
862   case ELF::R_PPC64_ADDR14: {
863     assert(((Value + Addend) & 3) == 0);
864     // Preserve the AA/LK bits in the branch instruction
865     uint8_t aalk = *(LocalAddress + 3);
866     writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
867   } break;
868   case ELF::R_PPC64_REL16_LO: {
869     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
870     uint64_t Delta = Value - FinalAddress + Addend;
871     writeInt16BE(LocalAddress, applyPPClo(Delta));
872   } break;
873   case ELF::R_PPC64_REL16_HI: {
874     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
875     uint64_t Delta = Value - FinalAddress + Addend;
876     writeInt16BE(LocalAddress, applyPPChi(Delta));
877   } break;
878   case ELF::R_PPC64_REL16_HA: {
879     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
880     uint64_t Delta = Value - FinalAddress + Addend;
881     writeInt16BE(LocalAddress, applyPPCha(Delta));
882   } break;
883   case ELF::R_PPC64_ADDR32: {
884     int64_t Result = static_cast<int64_t>(Value + Addend);
885     if (SignExtend64<32>(Result) != Result)
886       llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
887     writeInt32BE(LocalAddress, Result);
888   } break;
889   case ELF::R_PPC64_REL24: {
890     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
891     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
892     if (SignExtend64<26>(delta) != delta)
893       llvm_unreachable("Relocation R_PPC64_REL24 overflow");
894     // We preserve bits other than LI field, i.e. PO and AA/LK fields.
895     uint32_t Inst = readBytesUnaligned(LocalAddress, 4);
896     writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
897   } break;
898   case ELF::R_PPC64_REL32: {
899     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
900     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
901     if (SignExtend64<32>(delta) != delta)
902       llvm_unreachable("Relocation R_PPC64_REL32 overflow");
903     writeInt32BE(LocalAddress, delta);
904   } break;
905   case ELF::R_PPC64_REL64: {
906     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
907     uint64_t Delta = Value - FinalAddress + Addend;
908     writeInt64BE(LocalAddress, Delta);
909   } break;
910   case ELF::R_PPC64_ADDR64:
911     writeInt64BE(LocalAddress, Value + Addend);
912     break;
913   }
914 }
915 
916 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
917                                               uint64_t Offset, uint64_t Value,
918                                               uint32_t Type, int64_t Addend) {
919   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
920   switch (Type) {
921   default:
922     report_fatal_error("Relocation type not implemented yet!");
923     break;
924   case ELF::R_390_PC16DBL:
925   case ELF::R_390_PLT16DBL: {
926     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
927     assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
928     writeInt16BE(LocalAddress, Delta / 2);
929     break;
930   }
931   case ELF::R_390_PC32DBL:
932   case ELF::R_390_PLT32DBL: {
933     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
934     assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
935     writeInt32BE(LocalAddress, Delta / 2);
936     break;
937   }
938   case ELF::R_390_PC16: {
939     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
940     assert(int16_t(Delta) == Delta && "R_390_PC16 overflow");
941     writeInt16BE(LocalAddress, Delta);
942     break;
943   }
944   case ELF::R_390_PC32: {
945     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
946     assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
947     writeInt32BE(LocalAddress, Delta);
948     break;
949   }
950   case ELF::R_390_PC64: {
951     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
952     writeInt64BE(LocalAddress, Delta);
953     break;
954   }
955   case ELF::R_390_8:
956     *LocalAddress = (uint8_t)(Value + Addend);
957     break;
958   case ELF::R_390_16:
959     writeInt16BE(LocalAddress, Value + Addend);
960     break;
961   case ELF::R_390_32:
962     writeInt32BE(LocalAddress, Value + Addend);
963     break;
964   case ELF::R_390_64:
965     writeInt64BE(LocalAddress, Value + Addend);
966     break;
967   }
968 }
969 
970 void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section,
971                                           uint64_t Offset, uint64_t Value,
972                                           uint32_t Type, int64_t Addend) {
973   bool isBE = Arch == Triple::bpfeb;
974 
975   switch (Type) {
976   default:
977     report_fatal_error("Relocation type not implemented yet!");
978     break;
979   case ELF::R_BPF_NONE:
980   case ELF::R_BPF_64_64:
981   case ELF::R_BPF_64_32:
982   case ELF::R_BPF_64_NODYLD32:
983     break;
984   case ELF::R_BPF_64_ABS64: {
985     write(isBE, Section.getAddressWithOffset(Offset), Value + Addend);
986     LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
987                       << format("%p\n", Section.getAddressWithOffset(Offset)));
988     break;
989   }
990   case ELF::R_BPF_64_ABS32: {
991     Value += Addend;
992     assert(Value <= UINT32_MAX);
993     write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value));
994     LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at "
995                       << format("%p\n", Section.getAddressWithOffset(Offset)));
996     break;
997   }
998   }
999 }
1000 
1001 // The target location for the relocation is described by RE.SectionID and
1002 // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
1003 // SectionEntry has three members describing its location.
1004 // SectionEntry::Address is the address at which the section has been loaded
1005 // into memory in the current (host) process.  SectionEntry::LoadAddress is the
1006 // address that the section will have in the target process.
1007 // SectionEntry::ObjAddress is the address of the bits for this section in the
1008 // original emitted object image (also in the current address space).
1009 //
1010 // Relocations will be applied as if the section were loaded at
1011 // SectionEntry::LoadAddress, but they will be applied at an address based
1012 // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer to
1013 // Target memory contents if they are required for value calculations.
1014 //
1015 // The Value parameter here is the load address of the symbol for the
1016 // relocation to be applied.  For relocations which refer to symbols in the
1017 // current object Value will be the LoadAddress of the section in which
1018 // the symbol resides (RE.Addend provides additional information about the
1019 // symbol location).  For external symbols, Value will be the address of the
1020 // symbol in the target address space.
1021 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
1022                                        uint64_t Value) {
1023   const SectionEntry &Section = Sections[RE.SectionID];
1024   return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
1025                            RE.SymOffset, RE.SectionID);
1026 }
1027 
1028 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
1029                                        uint64_t Offset, uint64_t Value,
1030                                        uint32_t Type, int64_t Addend,
1031                                        uint64_t SymOffset, SID SectionID) {
1032   switch (Arch) {
1033   case Triple::x86_64:
1034     resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
1035     break;
1036   case Triple::x86:
1037     resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1038                          (uint32_t)(Addend & 0xffffffffL));
1039     break;
1040   case Triple::aarch64:
1041   case Triple::aarch64_be:
1042     resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
1043     break;
1044   case Triple::arm: // Fall through.
1045   case Triple::armeb:
1046   case Triple::thumb:
1047   case Triple::thumbeb:
1048     resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1049                          (uint32_t)(Addend & 0xffffffffL));
1050     break;
1051   case Triple::ppc: // Fall through.
1052   case Triple::ppcle:
1053     resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
1054     break;
1055   case Triple::ppc64: // Fall through.
1056   case Triple::ppc64le:
1057     resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
1058     break;
1059   case Triple::systemz:
1060     resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
1061     break;
1062   case Triple::bpfel:
1063   case Triple::bpfeb:
1064     resolveBPFRelocation(Section, Offset, Value, Type, Addend);
1065     break;
1066   default:
1067     llvm_unreachable("Unsupported CPU type!");
1068   }
1069 }
1070 
1071 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
1072   return (void *)(Sections[SectionID].getObjAddress() + Offset);
1073 }
1074 
1075 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
1076   RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
1077   if (Value.SymbolName)
1078     addRelocationForSymbol(RE, Value.SymbolName);
1079   else
1080     addRelocationForSection(RE, Value.SectionID);
1081 }
1082 
1083 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
1084                                                  bool IsLocal) const {
1085   switch (RelType) {
1086   case ELF::R_MICROMIPS_GOT16:
1087     if (IsLocal)
1088       return ELF::R_MICROMIPS_LO16;
1089     break;
1090   case ELF::R_MICROMIPS_HI16:
1091     return ELF::R_MICROMIPS_LO16;
1092   case ELF::R_MIPS_GOT16:
1093     if (IsLocal)
1094       return ELF::R_MIPS_LO16;
1095     break;
1096   case ELF::R_MIPS_HI16:
1097     return ELF::R_MIPS_LO16;
1098   case ELF::R_MIPS_PCHI16:
1099     return ELF::R_MIPS_PCLO16;
1100   default:
1101     break;
1102   }
1103   return ELF::R_MIPS_NONE;
1104 }
1105 
1106 // Sometimes we don't need to create thunk for a branch.
1107 // This typically happens when branch target is located
1108 // in the same object file. In such case target is either
1109 // a weak symbol or symbol in a different executable section.
1110 // This function checks if branch target is located in the
1111 // same object file and if distance between source and target
1112 // fits R_AARCH64_CALL26 relocation. If both conditions are
1113 // met, it emits direct jump to the target and returns true.
1114 // Otherwise false is returned and thunk is created.
1115 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1116     unsigned SectionID, relocation_iterator RelI,
1117     const RelocationValueRef &Value) {
1118   uint64_t Address;
1119   if (Value.SymbolName) {
1120     auto Loc = GlobalSymbolTable.find(Value.SymbolName);
1121 
1122     // Don't create direct branch for external symbols.
1123     if (Loc == GlobalSymbolTable.end())
1124       return false;
1125 
1126     const auto &SymInfo = Loc->second;
1127     Address =
1128         uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
1129             SymInfo.getOffset()));
1130   } else {
1131     Address = uint64_t(Sections[Value.SectionID].getLoadAddress());
1132   }
1133   uint64_t Offset = RelI->getOffset();
1134   uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
1135 
1136   // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
1137   // If distance between source and target is out of range then we should
1138   // create thunk.
1139   if (!isInt<28>(Address + Value.Addend - SourceAddress))
1140     return false;
1141 
1142   resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
1143                     Value.Addend);
1144 
1145   return true;
1146 }
1147 
1148 void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
1149                                           const RelocationValueRef &Value,
1150                                           relocation_iterator RelI,
1151                                           StubMap &Stubs) {
1152 
1153   LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1154   SectionEntry &Section = Sections[SectionID];
1155 
1156   uint64_t Offset = RelI->getOffset();
1157   unsigned RelType = RelI->getType();
1158   // Look for an existing stub.
1159   StubMap::const_iterator i = Stubs.find(Value);
1160   if (i != Stubs.end()) {
1161     resolveRelocation(Section, Offset,
1162                       (uint64_t)Section.getAddressWithOffset(i->second),
1163                       RelType, 0);
1164     LLVM_DEBUG(dbgs() << " Stub function found\n");
1165   } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1166     // Create a new stub function.
1167     LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1168     Stubs[Value] = Section.getStubOffset();
1169     uint8_t *StubTargetAddr = createStubFunction(
1170         Section.getAddressWithOffset(Section.getStubOffset()));
1171 
1172     RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(),
1173                               ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1174     RelocationEntry REmovk_g2(SectionID,
1175                               StubTargetAddr - Section.getAddress() + 4,
1176                               ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1177     RelocationEntry REmovk_g1(SectionID,
1178                               StubTargetAddr - Section.getAddress() + 8,
1179                               ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1180     RelocationEntry REmovk_g0(SectionID,
1181                               StubTargetAddr - Section.getAddress() + 12,
1182                               ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1183 
1184     if (Value.SymbolName) {
1185       addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1186       addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1187       addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1188       addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1189     } else {
1190       addRelocationForSection(REmovz_g3, Value.SectionID);
1191       addRelocationForSection(REmovk_g2, Value.SectionID);
1192       addRelocationForSection(REmovk_g1, Value.SectionID);
1193       addRelocationForSection(REmovk_g0, Value.SectionID);
1194     }
1195     resolveRelocation(Section, Offset,
1196                       reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
1197                           Section.getStubOffset())),
1198                       RelType, 0);
1199     Section.advanceStubOffset(getMaxStubSize());
1200   }
1201 }
1202 
1203 Expected<relocation_iterator>
1204 RuntimeDyldELF::processRelocationRef(
1205     unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
1206     ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
1207   const auto &Obj = cast<ELFObjectFileBase>(O);
1208   uint64_t RelType = RelI->getType();
1209   int64_t Addend = 0;
1210   if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend())
1211     Addend = *AddendOrErr;
1212   else
1213     consumeError(AddendOrErr.takeError());
1214   elf_symbol_iterator Symbol = RelI->getSymbol();
1215 
1216   // Obtain the symbol name which is referenced in the relocation
1217   StringRef TargetName;
1218   if (Symbol != Obj.symbol_end()) {
1219     if (auto TargetNameOrErr = Symbol->getName())
1220       TargetName = *TargetNameOrErr;
1221     else
1222       return TargetNameOrErr.takeError();
1223   }
1224   LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
1225                     << " TargetName: " << TargetName << "\n");
1226   RelocationValueRef Value;
1227   // First search for the symbol in the local symbol table
1228   SymbolRef::Type SymType = SymbolRef::ST_Unknown;
1229 
1230   // Search for the symbol in the global symbol table
1231   RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
1232   if (Symbol != Obj.symbol_end()) {
1233     gsi = GlobalSymbolTable.find(TargetName.data());
1234     Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1235     if (!SymTypeOrErr) {
1236       std::string Buf;
1237       raw_string_ostream OS(Buf);
1238       logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
1239       OS.flush();
1240       report_fatal_error(Buf);
1241     }
1242     SymType = *SymTypeOrErr;
1243   }
1244   if (gsi != GlobalSymbolTable.end()) {
1245     const auto &SymInfo = gsi->second;
1246     Value.SectionID = SymInfo.getSectionID();
1247     Value.Offset = SymInfo.getOffset();
1248     Value.Addend = SymInfo.getOffset() + Addend;
1249   } else {
1250     switch (SymType) {
1251     case SymbolRef::ST_Debug: {
1252       // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
1253       // and can be changed by another developers. Maybe best way is add
1254       // a new symbol type ST_Section to SymbolRef and use it.
1255       auto SectionOrErr = Symbol->getSection();
1256       if (!SectionOrErr) {
1257         std::string Buf;
1258         raw_string_ostream OS(Buf);
1259         logAllUnhandledErrors(SectionOrErr.takeError(), OS);
1260         OS.flush();
1261         report_fatal_error(Buf);
1262       }
1263       section_iterator si = *SectionOrErr;
1264       if (si == Obj.section_end())
1265         llvm_unreachable("Symbol section not found, bad object file format!");
1266       LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n");
1267       bool isCode = si->isText();
1268       if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode,
1269                                                   ObjSectionToID))
1270         Value.SectionID = *SectionIDOrErr;
1271       else
1272         return SectionIDOrErr.takeError();
1273       Value.Addend = Addend;
1274       break;
1275     }
1276     case SymbolRef::ST_Data:
1277     case SymbolRef::ST_Function:
1278     case SymbolRef::ST_Unknown: {
1279       Value.SymbolName = TargetName.data();
1280       Value.Addend = Addend;
1281 
1282       // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
1283       // will manifest here as a NULL symbol name.
1284       // We can set this as a valid (but empty) symbol name, and rely
1285       // on addRelocationForSymbol to handle this.
1286       if (!Value.SymbolName)
1287         Value.SymbolName = "";
1288       break;
1289     }
1290     default:
1291       llvm_unreachable("Unresolved symbol type!");
1292       break;
1293     }
1294   }
1295 
1296   uint64_t Offset = RelI->getOffset();
1297 
1298   LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
1299                     << "\n");
1300   if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be)) {
1301     if ((RelType == ELF::R_AARCH64_CALL26 ||
1302          RelType == ELF::R_AARCH64_JUMP26) &&
1303         MemMgr.allowStubAllocation()) {
1304       resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1305     } else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1306       // Craete new GOT entry or find existing one. If GOT entry is
1307       // to be created, then we also emit ABS64 relocation for it.
1308       uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1309       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1310                                  ELF::R_AARCH64_ADR_PREL_PG_HI21);
1311 
1312     } else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1313       uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1314       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1315                                  ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1316     } else {
1317       processSimpleRelocation(SectionID, Offset, RelType, Value);
1318     }
1319   } else if (Arch == Triple::arm) {
1320     if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1321       RelType == ELF::R_ARM_JUMP24) {
1322       // This is an ARM branch relocation, need to use a stub function.
1323       LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
1324       SectionEntry &Section = Sections[SectionID];
1325 
1326       // Look for an existing stub.
1327       StubMap::const_iterator i = Stubs.find(Value);
1328       if (i != Stubs.end()) {
1329         resolveRelocation(
1330             Section, Offset,
1331             reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
1332             RelType, 0);
1333         LLVM_DEBUG(dbgs() << " Stub function found\n");
1334       } else {
1335         // Create a new stub function.
1336         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1337         Stubs[Value] = Section.getStubOffset();
1338         uint8_t *StubTargetAddr = createStubFunction(
1339             Section.getAddressWithOffset(Section.getStubOffset()));
1340         RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1341                            ELF::R_ARM_ABS32, Value.Addend);
1342         if (Value.SymbolName)
1343           addRelocationForSymbol(RE, Value.SymbolName);
1344         else
1345           addRelocationForSection(RE, Value.SectionID);
1346 
1347         resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1348                                                Section.getAddressWithOffset(
1349                                                    Section.getStubOffset())),
1350                           RelType, 0);
1351         Section.advanceStubOffset(getMaxStubSize());
1352       }
1353     } else {
1354       uint32_t *Placeholder =
1355         reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
1356       if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1357           RelType == ELF::R_ARM_ABS32) {
1358         Value.Addend += *Placeholder;
1359       } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1360         // See ELF for ARM documentation
1361         Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1362       }
1363       processSimpleRelocation(SectionID, Offset, RelType, Value);
1364     }
1365   } else if (IsMipsO32ABI) {
1366     uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
1367         computePlaceholderAddress(SectionID, Offset));
1368     uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
1369     if (RelType == ELF::R_MIPS_26) {
1370       // This is an Mips branch relocation, need to use a stub function.
1371       LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1372       SectionEntry &Section = Sections[SectionID];
1373 
1374       // Extract the addend from the instruction.
1375       // We shift up by two since the Value will be down shifted again
1376       // when applying the relocation.
1377       uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1378 
1379       Value.Addend += Addend;
1380 
1381       //  Look up for existing stub.
1382       StubMap::const_iterator i = Stubs.find(Value);
1383       if (i != Stubs.end()) {
1384         RelocationEntry RE(SectionID, Offset, RelType, i->second);
1385         addRelocationForSection(RE, SectionID);
1386         LLVM_DEBUG(dbgs() << " Stub function found\n");
1387       } else {
1388         // Create a new stub function.
1389         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1390         Stubs[Value] = Section.getStubOffset();
1391 
1392         unsigned AbiVariant = Obj.getPlatformFlags();
1393 
1394         uint8_t *StubTargetAddr = createStubFunction(
1395             Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1396 
1397         // Creating Hi and Lo relocations for the filled stub instructions.
1398         RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1399                              ELF::R_MIPS_HI16, Value.Addend);
1400         RelocationEntry RELo(SectionID,
1401                              StubTargetAddr - Section.getAddress() + 4,
1402                              ELF::R_MIPS_LO16, Value.Addend);
1403 
1404         if (Value.SymbolName) {
1405           addRelocationForSymbol(REHi, Value.SymbolName);
1406           addRelocationForSymbol(RELo, Value.SymbolName);
1407         } else {
1408           addRelocationForSection(REHi, Value.SectionID);
1409           addRelocationForSection(RELo, Value.SectionID);
1410         }
1411 
1412         RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1413         addRelocationForSection(RE, SectionID);
1414         Section.advanceStubOffset(getMaxStubSize());
1415       }
1416     } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1417       int64_t Addend = (Opcode & 0x0000ffff) << 16;
1418       RelocationEntry RE(SectionID, Offset, RelType, Addend);
1419       PendingRelocs.push_back(std::make_pair(Value, RE));
1420     } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1421       int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1422       for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) {
1423         const RelocationValueRef &MatchingValue = I->first;
1424         RelocationEntry &Reloc = I->second;
1425         if (MatchingValue == Value &&
1426             RelType == getMatchingLoRelocation(Reloc.RelType) &&
1427             SectionID == Reloc.SectionID) {
1428           Reloc.Addend += Addend;
1429           if (Value.SymbolName)
1430             addRelocationForSymbol(Reloc, Value.SymbolName);
1431           else
1432             addRelocationForSection(Reloc, Value.SectionID);
1433           I = PendingRelocs.erase(I);
1434         } else
1435           ++I;
1436       }
1437       RelocationEntry RE(SectionID, Offset, RelType, Addend);
1438       if (Value.SymbolName)
1439         addRelocationForSymbol(RE, Value.SymbolName);
1440       else
1441         addRelocationForSection(RE, Value.SectionID);
1442     } else {
1443       if (RelType == ELF::R_MIPS_32)
1444         Value.Addend += Opcode;
1445       else if (RelType == ELF::R_MIPS_PC16)
1446         Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1447       else if (RelType == ELF::R_MIPS_PC19_S2)
1448         Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1449       else if (RelType == ELF::R_MIPS_PC21_S2)
1450         Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1451       else if (RelType == ELF::R_MIPS_PC26_S2)
1452         Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1453       processSimpleRelocation(SectionID, Offset, RelType, Value);
1454     }
1455   } else if (IsMipsN32ABI || IsMipsN64ABI) {
1456     uint32_t r_type = RelType & 0xff;
1457     RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1458     if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1459         || r_type == ELF::R_MIPS_GOT_DISP) {
1460       StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName);
1461       if (i != GOTSymbolOffsets.end())
1462         RE.SymOffset = i->second;
1463       else {
1464         RE.SymOffset = allocateGOTEntries(1);
1465         GOTSymbolOffsets[TargetName] = RE.SymOffset;
1466       }
1467       if (Value.SymbolName)
1468         addRelocationForSymbol(RE, Value.SymbolName);
1469       else
1470         addRelocationForSection(RE, Value.SectionID);
1471     } else if (RelType == ELF::R_MIPS_26) {
1472       // This is an Mips branch relocation, need to use a stub function.
1473       LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1474       SectionEntry &Section = Sections[SectionID];
1475 
1476       //  Look up for existing stub.
1477       StubMap::const_iterator i = Stubs.find(Value);
1478       if (i != Stubs.end()) {
1479         RelocationEntry RE(SectionID, Offset, RelType, i->second);
1480         addRelocationForSection(RE, SectionID);
1481         LLVM_DEBUG(dbgs() << " Stub function found\n");
1482       } else {
1483         // Create a new stub function.
1484         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1485         Stubs[Value] = Section.getStubOffset();
1486 
1487         unsigned AbiVariant = Obj.getPlatformFlags();
1488 
1489         uint8_t *StubTargetAddr = createStubFunction(
1490             Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1491 
1492         if (IsMipsN32ABI) {
1493           // Creating Hi and Lo relocations for the filled stub instructions.
1494           RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1495                                ELF::R_MIPS_HI16, Value.Addend);
1496           RelocationEntry RELo(SectionID,
1497                                StubTargetAddr - Section.getAddress() + 4,
1498                                ELF::R_MIPS_LO16, Value.Addend);
1499           if (Value.SymbolName) {
1500             addRelocationForSymbol(REHi, Value.SymbolName);
1501             addRelocationForSymbol(RELo, Value.SymbolName);
1502           } else {
1503             addRelocationForSection(REHi, Value.SectionID);
1504             addRelocationForSection(RELo, Value.SectionID);
1505           }
1506         } else {
1507           // Creating Highest, Higher, Hi and Lo relocations for the filled stub
1508           // instructions.
1509           RelocationEntry REHighest(SectionID,
1510                                     StubTargetAddr - Section.getAddress(),
1511                                     ELF::R_MIPS_HIGHEST, Value.Addend);
1512           RelocationEntry REHigher(SectionID,
1513                                    StubTargetAddr - Section.getAddress() + 4,
1514                                    ELF::R_MIPS_HIGHER, Value.Addend);
1515           RelocationEntry REHi(SectionID,
1516                                StubTargetAddr - Section.getAddress() + 12,
1517                                ELF::R_MIPS_HI16, Value.Addend);
1518           RelocationEntry RELo(SectionID,
1519                                StubTargetAddr - Section.getAddress() + 20,
1520                                ELF::R_MIPS_LO16, Value.Addend);
1521           if (Value.SymbolName) {
1522             addRelocationForSymbol(REHighest, Value.SymbolName);
1523             addRelocationForSymbol(REHigher, Value.SymbolName);
1524             addRelocationForSymbol(REHi, Value.SymbolName);
1525             addRelocationForSymbol(RELo, Value.SymbolName);
1526           } else {
1527             addRelocationForSection(REHighest, Value.SectionID);
1528             addRelocationForSection(REHigher, Value.SectionID);
1529             addRelocationForSection(REHi, Value.SectionID);
1530             addRelocationForSection(RELo, Value.SectionID);
1531           }
1532         }
1533         RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1534         addRelocationForSection(RE, SectionID);
1535         Section.advanceStubOffset(getMaxStubSize());
1536       }
1537     } else {
1538       processSimpleRelocation(SectionID, Offset, RelType, Value);
1539     }
1540 
1541   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
1542     if (RelType == ELF::R_PPC64_REL24) {
1543       // Determine ABI variant in use for this object.
1544       unsigned AbiVariant = Obj.getPlatformFlags();
1545       AbiVariant &= ELF::EF_PPC64_ABI;
1546       // A PPC branch relocation will need a stub function if the target is
1547       // an external symbol (either Value.SymbolName is set, or SymType is
1548       // Symbol::ST_Unknown) or if the target address is not within the
1549       // signed 24-bits branch address.
1550       SectionEntry &Section = Sections[SectionID];
1551       uint8_t *Target = Section.getAddressWithOffset(Offset);
1552       bool RangeOverflow = false;
1553       bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown;
1554       if (!IsExtern) {
1555         if (AbiVariant != 2) {
1556           // In the ELFv1 ABI, a function call may point to the .opd entry,
1557           // so the final symbol value is calculated based on the relocation
1558           // values in the .opd section.
1559           if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
1560             return std::move(Err);
1561         } else {
1562           // In the ELFv2 ABI, a function symbol may provide a local entry
1563           // point, which must be used for direct calls.
1564           if (Value.SectionID == SectionID){
1565             uint8_t SymOther = Symbol->getOther();
1566             Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1567           }
1568         }
1569         uint8_t *RelocTarget =
1570             Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
1571         int64_t delta = static_cast<int64_t>(Target - RelocTarget);
1572         // If it is within 26-bits branch range, just set the branch target
1573         if (SignExtend64<26>(delta) != delta) {
1574           RangeOverflow = true;
1575         } else if ((AbiVariant != 2) ||
1576                    (AbiVariant == 2  && Value.SectionID == SectionID)) {
1577           RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1578           addRelocationForSection(RE, Value.SectionID);
1579         }
1580       }
1581       if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID) ||
1582           RangeOverflow) {
1583         // It is an external symbol (either Value.SymbolName is set, or
1584         // SymType is SymbolRef::ST_Unknown) or out of range.
1585         StubMap::const_iterator i = Stubs.find(Value);
1586         if (i != Stubs.end()) {
1587           // Symbol function stub already created, just relocate to it
1588           resolveRelocation(Section, Offset,
1589                             reinterpret_cast<uint64_t>(
1590                                 Section.getAddressWithOffset(i->second)),
1591                             RelType, 0);
1592           LLVM_DEBUG(dbgs() << " Stub function found\n");
1593         } else {
1594           // Create a new stub function.
1595           LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1596           Stubs[Value] = Section.getStubOffset();
1597           uint8_t *StubTargetAddr = createStubFunction(
1598               Section.getAddressWithOffset(Section.getStubOffset()),
1599               AbiVariant);
1600           RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1601                              ELF::R_PPC64_ADDR64, Value.Addend);
1602 
1603           // Generates the 64-bits address loads as exemplified in section
1604           // 4.5.1 in PPC64 ELF ABI.  Note that the relocations need to
1605           // apply to the low part of the instructions, so we have to update
1606           // the offset according to the target endianness.
1607           uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
1608           if (!IsTargetLittleEndian)
1609             StubRelocOffset += 2;
1610 
1611           RelocationEntry REhst(SectionID, StubRelocOffset + 0,
1612                                 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
1613           RelocationEntry REhr(SectionID, StubRelocOffset + 4,
1614                                ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
1615           RelocationEntry REh(SectionID, StubRelocOffset + 12,
1616                               ELF::R_PPC64_ADDR16_HI, Value.Addend);
1617           RelocationEntry REl(SectionID, StubRelocOffset + 16,
1618                               ELF::R_PPC64_ADDR16_LO, Value.Addend);
1619 
1620           if (Value.SymbolName) {
1621             addRelocationForSymbol(REhst, Value.SymbolName);
1622             addRelocationForSymbol(REhr, Value.SymbolName);
1623             addRelocationForSymbol(REh, Value.SymbolName);
1624             addRelocationForSymbol(REl, Value.SymbolName);
1625           } else {
1626             addRelocationForSection(REhst, Value.SectionID);
1627             addRelocationForSection(REhr, Value.SectionID);
1628             addRelocationForSection(REh, Value.SectionID);
1629             addRelocationForSection(REl, Value.SectionID);
1630           }
1631 
1632           resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1633                                                  Section.getAddressWithOffset(
1634                                                      Section.getStubOffset())),
1635                             RelType, 0);
1636           Section.advanceStubOffset(getMaxStubSize());
1637         }
1638         if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) {
1639           // Restore the TOC for external calls
1640           if (AbiVariant == 2)
1641             writeInt32BE(Target + 4, 0xE8410018); // ld r2,24(r1)
1642           else
1643             writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1644         }
1645       }
1646     } else if (RelType == ELF::R_PPC64_TOC16 ||
1647                RelType == ELF::R_PPC64_TOC16_DS ||
1648                RelType == ELF::R_PPC64_TOC16_LO ||
1649                RelType == ELF::R_PPC64_TOC16_LO_DS ||
1650                RelType == ELF::R_PPC64_TOC16_HI ||
1651                RelType == ELF::R_PPC64_TOC16_HA) {
1652       // These relocations are supposed to subtract the TOC address from
1653       // the final value.  This does not fit cleanly into the RuntimeDyld
1654       // scheme, since there may be *two* sections involved in determining
1655       // the relocation value (the section of the symbol referred to by the
1656       // relocation, and the TOC section associated with the current module).
1657       //
1658       // Fortunately, these relocations are currently only ever generated
1659       // referring to symbols that themselves reside in the TOC, which means
1660       // that the two sections are actually the same.  Thus they cancel out
1661       // and we can immediately resolve the relocation right now.
1662       switch (RelType) {
1663       case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1664       case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1665       case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1666       case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1667       case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1668       case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1669       default: llvm_unreachable("Wrong relocation type.");
1670       }
1671 
1672       RelocationValueRef TOCValue;
1673       if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1674         return std::move(Err);
1675       if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1676         llvm_unreachable("Unsupported TOC relocation.");
1677       Value.Addend -= TOCValue.Addend;
1678       resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
1679     } else {
1680       // There are two ways to refer to the TOC address directly: either
1681       // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1682       // ignored), or via any relocation that refers to the magic ".TOC."
1683       // symbols (in which case the addend is respected).
1684       if (RelType == ELF::R_PPC64_TOC) {
1685         RelType = ELF::R_PPC64_ADDR64;
1686         if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1687           return std::move(Err);
1688       } else if (TargetName == ".TOC.") {
1689         if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1690           return std::move(Err);
1691         Value.Addend += Addend;
1692       }
1693 
1694       RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1695 
1696       if (Value.SymbolName)
1697         addRelocationForSymbol(RE, Value.SymbolName);
1698       else
1699         addRelocationForSection(RE, Value.SectionID);
1700     }
1701   } else if (Arch == Triple::systemz &&
1702              (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1703     // Create function stubs for both PLT and GOT references, regardless of
1704     // whether the GOT reference is to data or code.  The stub contains the
1705     // full address of the symbol, as needed by GOT references, and the
1706     // executable part only adds an overhead of 8 bytes.
1707     //
1708     // We could try to conserve space by allocating the code and data
1709     // parts of the stub separately.  However, as things stand, we allocate
1710     // a stub for every relocation, so using a GOT in JIT code should be
1711     // no less space efficient than using an explicit constant pool.
1712     LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1713     SectionEntry &Section = Sections[SectionID];
1714 
1715     // Look for an existing stub.
1716     StubMap::const_iterator i = Stubs.find(Value);
1717     uintptr_t StubAddress;
1718     if (i != Stubs.end()) {
1719       StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
1720       LLVM_DEBUG(dbgs() << " Stub function found\n");
1721     } else {
1722       // Create a new stub function.
1723       LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1724 
1725       uintptr_t BaseAddress = uintptr_t(Section.getAddress());
1726       uintptr_t StubAlignment = getStubAlignment();
1727       StubAddress =
1728           (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
1729           -StubAlignment;
1730       unsigned StubOffset = StubAddress - BaseAddress;
1731 
1732       Stubs[Value] = StubOffset;
1733       createStubFunction((uint8_t *)StubAddress);
1734       RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
1735                          Value.Offset);
1736       if (Value.SymbolName)
1737         addRelocationForSymbol(RE, Value.SymbolName);
1738       else
1739         addRelocationForSection(RE, Value.SectionID);
1740       Section.advanceStubOffset(getMaxStubSize());
1741     }
1742 
1743     if (RelType == ELF::R_390_GOTENT)
1744       resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1745                         Addend);
1746     else
1747       resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1748   } else if (Arch == Triple::x86_64) {
1749     if (RelType == ELF::R_X86_64_PLT32) {
1750       // The way the PLT relocations normally work is that the linker allocates
1751       // the
1752       // PLT and this relocation makes a PC-relative call into the PLT.  The PLT
1753       // entry will then jump to an address provided by the GOT.  On first call,
1754       // the
1755       // GOT address will point back into PLT code that resolves the symbol. After
1756       // the first call, the GOT entry points to the actual function.
1757       //
1758       // For local functions we're ignoring all of that here and just replacing
1759       // the PLT32 relocation type with PC32, which will translate the relocation
1760       // into a PC-relative call directly to the function. For external symbols we
1761       // can't be sure the function will be within 2^32 bytes of the call site, so
1762       // we need to create a stub, which calls into the GOT.  This case is
1763       // equivalent to the usual PLT implementation except that we use the stub
1764       // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1765       // rather than allocating a PLT section.
1766       if (Value.SymbolName && MemMgr.allowStubAllocation()) {
1767         // This is a call to an external function.
1768         // Look for an existing stub.
1769         SectionEntry *Section = &Sections[SectionID];
1770         StubMap::const_iterator i = Stubs.find(Value);
1771         uintptr_t StubAddress;
1772         if (i != Stubs.end()) {
1773           StubAddress = uintptr_t(Section->getAddress()) + i->second;
1774           LLVM_DEBUG(dbgs() << " Stub function found\n");
1775         } else {
1776           // Create a new stub function (equivalent to a PLT entry).
1777           LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1778 
1779           uintptr_t BaseAddress = uintptr_t(Section->getAddress());
1780           uintptr_t StubAlignment = getStubAlignment();
1781           StubAddress =
1782               (BaseAddress + Section->getStubOffset() + StubAlignment - 1) &
1783               -StubAlignment;
1784           unsigned StubOffset = StubAddress - BaseAddress;
1785           Stubs[Value] = StubOffset;
1786           createStubFunction((uint8_t *)StubAddress);
1787 
1788           // Bump our stub offset counter
1789           Section->advanceStubOffset(getMaxStubSize());
1790 
1791           // Allocate a GOT Entry
1792           uint64_t GOTOffset = allocateGOTEntries(1);
1793           // This potentially creates a new Section which potentially
1794           // invalidates the Section pointer, so reload it.
1795           Section = &Sections[SectionID];
1796 
1797           // The load of the GOT address has an addend of -4
1798           resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1799                                      ELF::R_X86_64_PC32);
1800 
1801           // Fill in the value of the symbol we're targeting into the GOT
1802           addRelocationForSymbol(
1803               computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1804               Value.SymbolName);
1805         }
1806 
1807         // Make the target call a call into the stub table.
1808         resolveRelocation(*Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1809                           Addend);
1810       } else {
1811         Value.Addend += support::ulittle32_t::ref(
1812             computePlaceholderAddress(SectionID, Offset));
1813         processSimpleRelocation(SectionID, Offset, ELF::R_X86_64_PC32, Value);
1814       }
1815     } else if (RelType == ELF::R_X86_64_GOTPCREL ||
1816                RelType == ELF::R_X86_64_GOTPCRELX ||
1817                RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1818       uint64_t GOTOffset = allocateGOTEntries(1);
1819       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1820                                  ELF::R_X86_64_PC32);
1821 
1822       // Fill in the value of the symbol we're targeting into the GOT
1823       RelocationEntry RE =
1824           computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
1825       if (Value.SymbolName)
1826         addRelocationForSymbol(RE, Value.SymbolName);
1827       else
1828         addRelocationForSection(RE, Value.SectionID);
1829     } else if (RelType == ELF::R_X86_64_GOT64) {
1830       // Fill in a 64-bit GOT offset.
1831       uint64_t GOTOffset = allocateGOTEntries(1);
1832       resolveRelocation(Sections[SectionID], Offset, GOTOffset,
1833                         ELF::R_X86_64_64, 0);
1834 
1835       // Fill in the value of the symbol we're targeting into the GOT
1836       RelocationEntry RE =
1837           computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
1838       if (Value.SymbolName)
1839         addRelocationForSymbol(RE, Value.SymbolName);
1840       else
1841         addRelocationForSection(RE, Value.SectionID);
1842     } else if (RelType == ELF::R_X86_64_GOTPC32) {
1843       // Materialize the address of the base of the GOT relative to the PC.
1844       // This doesn't create a GOT entry, but it does mean we need a GOT
1845       // section.
1846       (void)allocateGOTEntries(0);
1847       resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC32);
1848     } else if (RelType == ELF::R_X86_64_GOTPC64) {
1849       (void)allocateGOTEntries(0);
1850       resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
1851     } else if (RelType == ELF::R_X86_64_GOTOFF64) {
1852       // GOTOFF relocations ultimately require a section difference relocation.
1853       (void)allocateGOTEntries(0);
1854       processSimpleRelocation(SectionID, Offset, RelType, Value);
1855     } else if (RelType == ELF::R_X86_64_PC32) {
1856       Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
1857       processSimpleRelocation(SectionID, Offset, RelType, Value);
1858     } else if (RelType == ELF::R_X86_64_PC64) {
1859       Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset));
1860       processSimpleRelocation(SectionID, Offset, RelType, Value);
1861     } else if (RelType == ELF::R_X86_64_GOTTPOFF) {
1862       processX86_64GOTTPOFFRelocation(SectionID, Offset, Value, Addend);
1863     } else if (RelType == ELF::R_X86_64_TLSGD ||
1864                RelType == ELF::R_X86_64_TLSLD) {
1865       // The next relocation must be the relocation for __tls_get_addr.
1866       ++RelI;
1867       auto &GetAddrRelocation = *RelI;
1868       processX86_64TLSRelocation(SectionID, Offset, RelType, Value, Addend,
1869                                  GetAddrRelocation);
1870     } else {
1871       processSimpleRelocation(SectionID, Offset, RelType, Value);
1872     }
1873   } else {
1874     if (Arch == Triple::x86) {
1875       Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
1876     }
1877     processSimpleRelocation(SectionID, Offset, RelType, Value);
1878   }
1879   return ++RelI;
1880 }
1881 
1882 void RuntimeDyldELF::processX86_64GOTTPOFFRelocation(unsigned SectionID,
1883                                                      uint64_t Offset,
1884                                                      RelocationValueRef Value,
1885                                                      int64_t Addend) {
1886   // Use the approach from "x86-64 Linker Optimizations" from the TLS spec
1887   // to replace the GOTTPOFF relocation with a TPOFF relocation. The spec
1888   // only mentions one optimization even though there are two different
1889   // code sequences for the Initial Exec TLS Model. We match the code to
1890   // find out which one was used.
1891 
1892   // A possible TLS code sequence and its replacement
1893   struct CodeSequence {
1894     // The expected code sequence
1895     ArrayRef<uint8_t> ExpectedCodeSequence;
1896     // The negative offset of the GOTTPOFF relocation to the beginning of
1897     // the sequence
1898     uint64_t TLSSequenceOffset;
1899     // The new code sequence
1900     ArrayRef<uint8_t> NewCodeSequence;
1901     // The offset of the new TPOFF relocation
1902     uint64_t TpoffRelocationOffset;
1903   };
1904 
1905   std::array<CodeSequence, 2> CodeSequences;
1906 
1907   // Initial Exec Code Model Sequence
1908   {
1909     static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
1910         0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
1911         0x00,                                    // mov %fs:0, %rax
1912         0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // add x@gotpoff(%rip),
1913                                                  // %rax
1914     };
1915     CodeSequences[0].ExpectedCodeSequence =
1916         ArrayRef<uint8_t>(ExpectedCodeSequenceList);
1917     CodeSequences[0].TLSSequenceOffset = 12;
1918 
1919     static const std::initializer_list<uint8_t> NewCodeSequenceList = {
1920         0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0, %rax
1921         0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax), %rax
1922     };
1923     CodeSequences[0].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
1924     CodeSequences[0].TpoffRelocationOffset = 12;
1925   }
1926 
1927   // Initial Exec Code Model Sequence, II
1928   {
1929     static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
1930         0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00, // mov x@gotpoff(%rip), %rax
1931         0x64, 0x48, 0x8b, 0x00, 0x00, 0x00, 0x00  // mov %fs:(%rax), %rax
1932     };
1933     CodeSequences[1].ExpectedCodeSequence =
1934         ArrayRef<uint8_t>(ExpectedCodeSequenceList);
1935     CodeSequences[1].TLSSequenceOffset = 3;
1936 
1937     static const std::initializer_list<uint8_t> NewCodeSequenceList = {
1938         0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,             // 6 byte nop
1939         0x64, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:x@tpoff, %rax
1940     };
1941     CodeSequences[1].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
1942     CodeSequences[1].TpoffRelocationOffset = 10;
1943   }
1944 
1945   bool Resolved = false;
1946   auto &Section = Sections[SectionID];
1947   for (const auto &C : CodeSequences) {
1948     assert(C.ExpectedCodeSequence.size() == C.NewCodeSequence.size() &&
1949            "Old and new code sequences must have the same size");
1950 
1951     if (Offset < C.TLSSequenceOffset ||
1952         (Offset - C.TLSSequenceOffset + C.NewCodeSequence.size()) >
1953             Section.getSize()) {
1954       // This can't be a matching sequence as it doesn't fit in the current
1955       // section
1956       continue;
1957     }
1958 
1959     auto TLSSequenceStartOffset = Offset - C.TLSSequenceOffset;
1960     auto *TLSSequence = Section.getAddressWithOffset(TLSSequenceStartOffset);
1961     if (ArrayRef<uint8_t>(TLSSequence, C.ExpectedCodeSequence.size()) !=
1962         C.ExpectedCodeSequence) {
1963       continue;
1964     }
1965 
1966     memcpy(TLSSequence, C.NewCodeSequence.data(), C.NewCodeSequence.size());
1967 
1968     // The original GOTTPOFF relocation has an addend as it is PC relative,
1969     // so it needs to be corrected. The TPOFF32 relocation is used as an
1970     // absolute value (which is an offset from %fs:0), so remove the addend
1971     // again.
1972     RelocationEntry RE(SectionID,
1973                        TLSSequenceStartOffset + C.TpoffRelocationOffset,
1974                        ELF::R_X86_64_TPOFF32, Value.Addend - Addend);
1975 
1976     if (Value.SymbolName)
1977       addRelocationForSymbol(RE, Value.SymbolName);
1978     else
1979       addRelocationForSection(RE, Value.SectionID);
1980 
1981     Resolved = true;
1982     break;
1983   }
1984 
1985   if (!Resolved) {
1986     // The GOTTPOFF relocation was not used in one of the sequences
1987     // described in the spec, so we can't optimize it to a TPOFF
1988     // relocation.
1989     uint64_t GOTOffset = allocateGOTEntries(1);
1990     resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1991                                ELF::R_X86_64_PC32);
1992     RelocationEntry RE =
1993         computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_TPOFF64);
1994     if (Value.SymbolName)
1995       addRelocationForSymbol(RE, Value.SymbolName);
1996     else
1997       addRelocationForSection(RE, Value.SectionID);
1998   }
1999 }
2000 
2001 void RuntimeDyldELF::processX86_64TLSRelocation(
2002     unsigned SectionID, uint64_t Offset, uint64_t RelType,
2003     RelocationValueRef Value, int64_t Addend,
2004     const RelocationRef &GetAddrRelocation) {
2005   // Since we are statically linking and have no additional DSOs, we can resolve
2006   // the relocation directly without using __tls_get_addr.
2007   // Use the approach from "x86-64 Linker Optimizations" from the TLS spec
2008   // to replace it with the Local Exec relocation variant.
2009 
2010   // Find out whether the code was compiled with the large or small memory
2011   // model. For this we look at the next relocation which is the relocation
2012   // for the __tls_get_addr function. If it's a 32 bit relocation, it's the
2013   // small code model, with a 64 bit relocation it's the large code model.
2014   bool IsSmallCodeModel;
2015   // Is the relocation for the __tls_get_addr a PC-relative GOT relocation?
2016   bool IsGOTPCRel = false;
2017 
2018   switch (GetAddrRelocation.getType()) {
2019   case ELF::R_X86_64_GOTPCREL:
2020   case ELF::R_X86_64_REX_GOTPCRELX:
2021   case ELF::R_X86_64_GOTPCRELX:
2022     IsGOTPCRel = true;
2023     LLVM_FALLTHROUGH;
2024   case ELF::R_X86_64_PLT32:
2025     IsSmallCodeModel = true;
2026     break;
2027   case ELF::R_X86_64_PLTOFF64:
2028     IsSmallCodeModel = false;
2029     break;
2030   default:
2031     report_fatal_error(
2032         "invalid TLS relocations for General/Local Dynamic TLS Model: "
2033         "expected PLT or GOT relocation for __tls_get_addr function");
2034   }
2035 
2036   // The negative offset to the start of the TLS code sequence relative to
2037   // the offset of the TLSGD/TLSLD relocation
2038   uint64_t TLSSequenceOffset;
2039   // The expected start of the code sequence
2040   ArrayRef<uint8_t> ExpectedCodeSequence;
2041   // The new TLS code sequence that will replace the existing code
2042   ArrayRef<uint8_t> NewCodeSequence;
2043 
2044   if (RelType == ELF::R_X86_64_TLSGD) {
2045     // The offset of the new TPOFF32 relocation (offset starting from the
2046     // beginning of the whole TLS sequence)
2047     uint64_t TpoffRelocOffset;
2048 
2049     if (IsSmallCodeModel) {
2050       if (!IsGOTPCRel) {
2051         static const std::initializer_list<uint8_t> CodeSequence = {
2052             0x66, // data16 (no-op prefix)
2053             0x48, 0x8d, 0x3d, 0x00, 0x00,
2054             0x00, 0x00,                  // lea <disp32>(%rip), %rdi
2055             0x66, 0x66,                  // two data16 prefixes
2056             0x48,                        // rex64 (no-op prefix)
2057             0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt
2058         };
2059         ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2060         TLSSequenceOffset = 4;
2061       } else {
2062         // This code sequence is not described in the TLS spec but gcc
2063         // generates it sometimes.
2064         static const std::initializer_list<uint8_t> CodeSequence = {
2065             0x66, // data16 (no-op prefix)
2066             0x48, 0x8d, 0x3d, 0x00, 0x00,
2067             0x00, 0x00, // lea <disp32>(%rip), %rdi
2068             0x66,       // data16 prefix (no-op prefix)
2069             0x48,       // rex64 (no-op prefix)
2070             0xff, 0x15, 0x00, 0x00, 0x00,
2071             0x00 // call *__tls_get_addr@gotpcrel(%rip)
2072         };
2073         ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2074         TLSSequenceOffset = 4;
2075       }
2076 
2077       // The replacement code for the small code model. It's the same for
2078       // both sequences.
2079       static const std::initializer_list<uint8_t> SmallSequence = {
2080           0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2081           0x00,                                    // mov %fs:0, %rax
2082           0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax),
2083                                                    // %rax
2084       };
2085       NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2086       TpoffRelocOffset = 12;
2087     } else {
2088       static const std::initializer_list<uint8_t> CodeSequence = {
2089           0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip),
2090                                                     // %rdi
2091           0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2092           0x00,             // movabs $__tls_get_addr@pltoff, %rax
2093           0x48, 0x01, 0xd8, // add %rbx, %rax
2094           0xff, 0xd0        // call *%rax
2095       };
2096       ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2097       TLSSequenceOffset = 3;
2098 
2099       // The replacement code for the large code model
2100       static const std::initializer_list<uint8_t> LargeSequence = {
2101           0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2102           0x00,                                     // mov %fs:0, %rax
2103           0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00, // lea x@tpoff(%rax),
2104                                                     // %rax
2105           0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00        // nopw 0x0(%rax,%rax,1)
2106       };
2107       NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2108       TpoffRelocOffset = 12;
2109     }
2110 
2111     // The TLSGD/TLSLD relocations are PC-relative, so they have an addend.
2112     // The new TPOFF32 relocations is used as an absolute offset from
2113     // %fs:0, so remove the TLSGD/TLSLD addend again.
2114     RelocationEntry RE(SectionID, Offset - TLSSequenceOffset + TpoffRelocOffset,
2115                        ELF::R_X86_64_TPOFF32, Value.Addend - Addend);
2116     if (Value.SymbolName)
2117       addRelocationForSymbol(RE, Value.SymbolName);
2118     else
2119       addRelocationForSection(RE, Value.SectionID);
2120   } else if (RelType == ELF::R_X86_64_TLSLD) {
2121     if (IsSmallCodeModel) {
2122       if (!IsGOTPCRel) {
2123         static const std::initializer_list<uint8_t> CodeSequence = {
2124             0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi
2125             0x00, 0xe8, 0x00, 0x00, 0x00, 0x00  // call __tls_get_addr@plt
2126         };
2127         ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2128         TLSSequenceOffset = 3;
2129 
2130         // The replacement code for the small code model
2131         static const std::initializer_list<uint8_t> SmallSequence = {
2132             0x66, 0x66, 0x66, // three data16 prefixes (no-op)
2133             0x64, 0x48, 0x8b, 0x04, 0x25,
2134             0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax
2135         };
2136         NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2137       } else {
2138         // This code sequence is not described in the TLS spec but gcc
2139         // generates it sometimes.
2140         static const std::initializer_list<uint8_t> CodeSequence = {
2141             0x48, 0x8d, 0x3d, 0x00,
2142             0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi
2143             0xff, 0x15, 0x00, 0x00,
2144             0x00, 0x00 // call
2145                        // *__tls_get_addr@gotpcrel(%rip)
2146         };
2147         ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2148         TLSSequenceOffset = 3;
2149 
2150         // The replacement is code is just like above but it needs to be
2151         // one byte longer.
2152         static const std::initializer_list<uint8_t> SmallSequence = {
2153             0x0f, 0x1f, 0x40, 0x00, // 4 byte nop
2154             0x64, 0x48, 0x8b, 0x04, 0x25,
2155             0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax
2156         };
2157         NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2158       }
2159     } else {
2160       // This is the same sequence as for the TLSGD sequence with the large
2161       // memory model above
2162       static const std::initializer_list<uint8_t> CodeSequence = {
2163           0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip),
2164                                                     // %rdi
2165           0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2166           0x48,       // movabs $__tls_get_addr@pltoff, %rax
2167           0x01, 0xd8, // add %rbx, %rax
2168           0xff, 0xd0  // call *%rax
2169       };
2170       ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2171       TLSSequenceOffset = 3;
2172 
2173       // The replacement code for the large code model
2174       static const std::initializer_list<uint8_t> LargeSequence = {
2175           0x66, 0x66, 0x66, // three data16 prefixes (no-op)
2176           0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00,
2177           0x00,                                                // 10 byte nop
2178           0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
2179       };
2180       NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2181     }
2182   } else {
2183     llvm_unreachable("both TLS relocations handled above");
2184   }
2185 
2186   assert(ExpectedCodeSequence.size() == NewCodeSequence.size() &&
2187          "Old and new code sequences must have the same size");
2188 
2189   auto &Section = Sections[SectionID];
2190   if (Offset < TLSSequenceOffset ||
2191       (Offset - TLSSequenceOffset + NewCodeSequence.size()) >
2192           Section.getSize()) {
2193     report_fatal_error("unexpected end of section in TLS sequence");
2194   }
2195 
2196   auto *TLSSequence = Section.getAddressWithOffset(Offset - TLSSequenceOffset);
2197   if (ArrayRef<uint8_t>(TLSSequence, ExpectedCodeSequence.size()) !=
2198       ExpectedCodeSequence) {
2199     report_fatal_error(
2200         "invalid TLS sequence for Global/Local Dynamic TLS Model");
2201   }
2202 
2203   memcpy(TLSSequence, NewCodeSequence.data(), NewCodeSequence.size());
2204 }
2205 
2206 size_t RuntimeDyldELF::getGOTEntrySize() {
2207   // We don't use the GOT in all of these cases, but it's essentially free
2208   // to put them all here.
2209   size_t Result = 0;
2210   switch (Arch) {
2211   case Triple::x86_64:
2212   case Triple::aarch64:
2213   case Triple::aarch64_be:
2214   case Triple::ppc64:
2215   case Triple::ppc64le:
2216   case Triple::systemz:
2217     Result = sizeof(uint64_t);
2218     break;
2219   case Triple::x86:
2220   case Triple::arm:
2221   case Triple::thumb:
2222     Result = sizeof(uint32_t);
2223     break;
2224   case Triple::mips:
2225   case Triple::mipsel:
2226   case Triple::mips64:
2227   case Triple::mips64el:
2228     if (IsMipsO32ABI || IsMipsN32ABI)
2229       Result = sizeof(uint32_t);
2230     else if (IsMipsN64ABI)
2231       Result = sizeof(uint64_t);
2232     else
2233       llvm_unreachable("Mips ABI not handled");
2234     break;
2235   default:
2236     llvm_unreachable("Unsupported CPU type!");
2237   }
2238   return Result;
2239 }
2240 
2241 uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no) {
2242   if (GOTSectionID == 0) {
2243     GOTSectionID = Sections.size();
2244     // Reserve a section id. We'll allocate the section later
2245     // once we know the total size
2246     Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0));
2247   }
2248   uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
2249   CurrentGOTIndex += no;
2250   return StartOffset;
2251 }
2252 
2253 uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef &Value,
2254                                              unsigned GOTRelType) {
2255   auto E = GOTOffsetMap.insert({Value, 0});
2256   if (E.second) {
2257     uint64_t GOTOffset = allocateGOTEntries(1);
2258 
2259     // Create relocation for newly created GOT entry
2260     RelocationEntry RE =
2261         computeGOTOffsetRE(GOTOffset, Value.Offset, GOTRelType);
2262     if (Value.SymbolName)
2263       addRelocationForSymbol(RE, Value.SymbolName);
2264     else
2265       addRelocationForSection(RE, Value.SectionID);
2266 
2267     E.first->second = GOTOffset;
2268   }
2269 
2270   return E.first->second;
2271 }
2272 
2273 void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID,
2274                                                 uint64_t Offset,
2275                                                 uint64_t GOTOffset,
2276                                                 uint32_t Type) {
2277   // Fill in the relative address of the GOT Entry into the stub
2278   RelocationEntry GOTRE(SectionID, Offset, Type, GOTOffset);
2279   addRelocationForSection(GOTRE, GOTSectionID);
2280 }
2281 
2282 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
2283                                                    uint64_t SymbolOffset,
2284                                                    uint32_t Type) {
2285   return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
2286 }
2287 
2288 Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
2289                                   ObjSectionToIDMap &SectionMap) {
2290   if (IsMipsO32ABI)
2291     if (!PendingRelocs.empty())
2292       return make_error<RuntimeDyldError>("Can't find matching LO16 reloc");
2293 
2294   // If necessary, allocate the global offset table
2295   if (GOTSectionID != 0) {
2296     // Allocate memory for the section
2297     size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
2298     uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
2299                                                 GOTSectionID, ".got", false);
2300     if (!Addr)
2301       return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!");
2302 
2303     Sections[GOTSectionID] =
2304         SectionEntry(".got", Addr, TotalSize, TotalSize, 0);
2305 
2306     // For now, initialize all GOT entries to zero.  We'll fill them in as
2307     // needed when GOT-based relocations are applied.
2308     memset(Addr, 0, TotalSize);
2309     if (IsMipsN32ABI || IsMipsN64ABI) {
2310       // To correctly resolve Mips GOT relocations, we need a mapping from
2311       // object's sections to GOTs.
2312       for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
2313            SI != SE; ++SI) {
2314         if (SI->relocation_begin() != SI->relocation_end()) {
2315           Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
2316           if (!RelSecOrErr)
2317             return make_error<RuntimeDyldError>(
2318                 toString(RelSecOrErr.takeError()));
2319 
2320           section_iterator RelocatedSection = *RelSecOrErr;
2321           ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
2322           assert (i != SectionMap.end());
2323           SectionToGOTMap[i->second] = GOTSectionID;
2324         }
2325       }
2326       GOTSymbolOffsets.clear();
2327     }
2328   }
2329 
2330   // Look for and record the EH frame section.
2331   ObjSectionToIDMap::iterator i, e;
2332   for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
2333     const SectionRef &Section = i->first;
2334 
2335     StringRef Name;
2336     Expected<StringRef> NameOrErr = Section.getName();
2337     if (NameOrErr)
2338       Name = *NameOrErr;
2339     else
2340       consumeError(NameOrErr.takeError());
2341 
2342     if (Name == ".eh_frame") {
2343       UnregisteredEHFrameSections.push_back(i->second);
2344       break;
2345     }
2346   }
2347 
2348   GOTSectionID = 0;
2349   CurrentGOTIndex = 0;
2350 
2351   return Error::success();
2352 }
2353 
2354 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
2355   return Obj.isELF();
2356 }
2357 
2358 bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef &R) const {
2359   unsigned RelTy = R.getType();
2360   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
2361     return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
2362            RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
2363 
2364   if (Arch == Triple::x86_64)
2365     return RelTy == ELF::R_X86_64_GOTPCREL ||
2366            RelTy == ELF::R_X86_64_GOTPCRELX ||
2367            RelTy == ELF::R_X86_64_GOT64 ||
2368            RelTy == ELF::R_X86_64_REX_GOTPCRELX;
2369   return false;
2370 }
2371 
2372 bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const {
2373   if (Arch != Triple::x86_64)
2374     return true;  // Conservative answer
2375 
2376   switch (R.getType()) {
2377   default:
2378     return true;  // Conservative answer
2379 
2380 
2381   case ELF::R_X86_64_GOTPCREL:
2382   case ELF::R_X86_64_GOTPCRELX:
2383   case ELF::R_X86_64_REX_GOTPCRELX:
2384   case ELF::R_X86_64_GOTPC64:
2385   case ELF::R_X86_64_GOT64:
2386   case ELF::R_X86_64_GOTOFF64:
2387   case ELF::R_X86_64_PC32:
2388   case ELF::R_X86_64_PC64:
2389   case ELF::R_X86_64_64:
2390     // We know that these reloation types won't need a stub function.  This list
2391     // can be extended as needed.
2392     return false;
2393   }
2394 }
2395 
2396 } // namespace llvm
2397