xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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_NONE:
426     break;
427   case ELF::R_AARCH64_ABS16: {
428     uint64_t Result = Value + Addend;
429     assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
430     write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
431     break;
432   }
433   case ELF::R_AARCH64_ABS32: {
434     uint64_t Result = Value + Addend;
435     assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
436     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
437     break;
438   }
439   case ELF::R_AARCH64_ABS64:
440     write(isBE, TargetPtr, Value + Addend);
441     break;
442   case ELF::R_AARCH64_PLT32: {
443     uint64_t Result = Value + Addend - FinalAddress;
444     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
445            static_cast<int64_t>(Result) <= INT32_MAX);
446     write(isBE, TargetPtr, static_cast<uint32_t>(Result));
447     break;
448   }
449   case ELF::R_AARCH64_PREL32: {
450     uint64_t Result = Value + Addend - FinalAddress;
451     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
452            static_cast<int64_t>(Result) <= UINT32_MAX);
453     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
454     break;
455   }
456   case ELF::R_AARCH64_PREL64:
457     write(isBE, TargetPtr, Value + Addend - FinalAddress);
458     break;
459   case ELF::R_AARCH64_CONDBR19: {
460     uint64_t BranchImm = Value + Addend - FinalAddress;
461 
462     assert(isInt<21>(BranchImm));
463     *TargetPtr &= 0xff00001fU;
464     // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ
465     or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3);
466     break;
467   }
468   case ELF::R_AARCH64_TSTBR14: {
469     uint64_t BranchImm = Value + Addend - FinalAddress;
470 
471     assert(isInt<16>(BranchImm));
472 
473     *TargetPtr &= 0xfff8001fU;
474     // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ
475     or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) << 3);
476     break;
477   }
478   case ELF::R_AARCH64_CALL26: // fallthrough
479   case ELF::R_AARCH64_JUMP26: {
480     // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
481     // calculation.
482     uint64_t BranchImm = Value + Addend - FinalAddress;
483 
484     // "Check that -2^27 <= result < 2^27".
485     assert(isInt<28>(BranchImm));
486     or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
487     break;
488   }
489   case ELF::R_AARCH64_MOVW_UABS_G3:
490     or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
491     break;
492   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
493     or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
494     break;
495   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
496     or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
497     break;
498   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
499     or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
500     break;
501   case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
502     // Operation: Page(S+A) - Page(P)
503     uint64_t Result =
504         ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
505 
506     // Check that -2^32 <= X < 2^32
507     assert(isInt<33>(Result) && "overflow check failed for relocation");
508 
509     // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
510     // from bits 32:12 of X.
511     write32AArch64Addr(TargetPtr, Result >> 12);
512     break;
513   }
514   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
515     // Operation: S + A
516     // Immediate goes in bits 21:10 of LD/ST instruction, taken
517     // from bits 11:0 of X
518     or32AArch64Imm(TargetPtr, Value + Addend);
519     break;
520   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
521     // Operation: S + A
522     // Immediate goes in bits 21:10 of LD/ST instruction, taken
523     // from bits 11:0 of X
524     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11));
525     break;
526   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
527     // Operation: S + A
528     // Immediate goes in bits 21:10 of LD/ST instruction, taken
529     // from bits 11:1 of X
530     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11));
531     break;
532   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
533     // Operation: S + A
534     // Immediate goes in bits 21:10 of LD/ST instruction, taken
535     // from bits 11:2 of X
536     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
537     break;
538   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
539     // Operation: S + A
540     // Immediate goes in bits 21:10 of LD/ST instruction, taken
541     // from bits 11:3 of X
542     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
543     break;
544   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
545     // Operation: S + A
546     // Immediate goes in bits 21:10 of LD/ST instruction, taken
547     // from bits 11:4 of X
548     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11));
549     break;
550   case ELF::R_AARCH64_LD_PREL_LO19: {
551     // Operation: S + A - P
552     uint64_t Result = Value + Addend - FinalAddress;
553 
554     // "Check that -2^20 <= result < 2^20".
555     assert(isInt<21>(Result));
556 
557     *TargetPtr &= 0xff00001fU;
558     // Immediate goes in bits 23:5 of LD imm instruction, taken
559     // from bits 20:2 of X
560     *TargetPtr |= ((Result & 0xffc) << (5 - 2));
561     break;
562   }
563   case ELF::R_AARCH64_ADR_PREL_LO21: {
564     // Operation: S + A - P
565     uint64_t Result = Value + Addend - FinalAddress;
566 
567     // "Check that -2^20 <= result < 2^20".
568     assert(isInt<21>(Result));
569 
570     *TargetPtr &= 0x9f00001fU;
571     // Immediate goes in bits 23:5, 30:29 of ADR imm instruction, taken
572     // from bits 20:0 of X
573     *TargetPtr |= ((Result & 0xffc) << (5 - 2));
574     *TargetPtr |= (Result & 0x3) << 29;
575     break;
576   }
577   }
578 }
579 
580 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
581                                           uint64_t Offset, uint32_t Value,
582                                           uint32_t Type, int32_t Addend) {
583   // TODO: Add Thumb relocations.
584   uint32_t *TargetPtr =
585       reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
586   uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
587   Value += Addend;
588 
589   LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
590                     << Section.getAddressWithOffset(Offset)
591                     << " FinalAddress: " << format("%p", FinalAddress)
592                     << " Value: " << format("%x", Value)
593                     << " Type: " << format("%x", Type)
594                     << " Addend: " << format("%x", Addend) << "\n");
595 
596   switch (Type) {
597   default:
598     llvm_unreachable("Not implemented relocation type!");
599 
600   case ELF::R_ARM_NONE:
601     break;
602     // Write a 31bit signed offset
603   case ELF::R_ARM_PREL31:
604     support::ulittle32_t::ref{TargetPtr} =
605         (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
606         ((Value - FinalAddress) & ~0x80000000);
607     break;
608   case ELF::R_ARM_TARGET1:
609   case ELF::R_ARM_ABS32:
610     support::ulittle32_t::ref{TargetPtr} = Value;
611     break;
612     // Write first 16 bit of 32 bit value to the mov instruction.
613     // Last 4 bit should be shifted.
614   case ELF::R_ARM_MOVW_ABS_NC:
615   case ELF::R_ARM_MOVT_ABS:
616     if (Type == ELF::R_ARM_MOVW_ABS_NC)
617       Value = Value & 0xFFFF;
618     else if (Type == ELF::R_ARM_MOVT_ABS)
619       Value = (Value >> 16) & 0xFFFF;
620     support::ulittle32_t::ref{TargetPtr} =
621         (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
622         (((Value >> 12) & 0xF) << 16);
623     break;
624     // Write 24 bit relative value to the branch instruction.
625   case ELF::R_ARM_PC24: // Fall through.
626   case ELF::R_ARM_CALL: // Fall through.
627   case ELF::R_ARM_JUMP24:
628     int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
629     RelValue = (RelValue & 0x03FFFFFC) >> 2;
630     assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
631     support::ulittle32_t::ref{TargetPtr} =
632         (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
633     break;
634   }
635 }
636 
637 void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
638   if (Arch == Triple::UnknownArch ||
639       !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
640     IsMipsO32ABI = false;
641     IsMipsN32ABI = false;
642     IsMipsN64ABI = false;
643     return;
644   }
645   if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
646     unsigned AbiVariant = E->getPlatformFlags();
647     IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
648     IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
649   }
650   IsMipsN64ABI = Obj.getFileFormatName().equals("elf64-mips");
651 }
652 
653 // Return the .TOC. section and offset.
654 Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
655                                           ObjSectionToIDMap &LocalSections,
656                                           RelocationValueRef &Rel) {
657   // Set a default SectionID in case we do not find a TOC section below.
658   // This may happen for references to TOC base base (sym@toc, .odp
659   // relocation) without a .toc directive.  In this case just use the
660   // first section (which is usually the .odp) since the code won't
661   // reference the .toc base directly.
662   Rel.SymbolName = nullptr;
663   Rel.SectionID = 0;
664 
665   // The TOC consists of sections .got, .toc, .tocbss, .plt in that
666   // order. The TOC starts where the first of these sections starts.
667   for (auto &Section : Obj.sections()) {
668     Expected<StringRef> NameOrErr = Section.getName();
669     if (!NameOrErr)
670       return NameOrErr.takeError();
671     StringRef SectionName = *NameOrErr;
672 
673     if (SectionName == ".got"
674         || SectionName == ".toc"
675         || SectionName == ".tocbss"
676         || SectionName == ".plt") {
677       if (auto SectionIDOrErr =
678             findOrEmitSection(Obj, Section, false, LocalSections))
679         Rel.SectionID = *SectionIDOrErr;
680       else
681         return SectionIDOrErr.takeError();
682       break;
683     }
684   }
685 
686   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
687   // thus permitting a full 64 Kbytes segment.
688   Rel.Addend = 0x8000;
689 
690   return Error::success();
691 }
692 
693 // Returns the sections and offset associated with the ODP entry referenced
694 // by Symbol.
695 Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
696                                           ObjSectionToIDMap &LocalSections,
697                                           RelocationValueRef &Rel) {
698   // Get the ELF symbol value (st_value) to compare with Relocation offset in
699   // .opd entries
700   for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
701        si != se; ++si) {
702 
703     Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
704     if (!RelSecOrErr)
705       report_fatal_error(Twine(toString(RelSecOrErr.takeError())));
706 
707     section_iterator RelSecI = *RelSecOrErr;
708     if (RelSecI == Obj.section_end())
709       continue;
710 
711     Expected<StringRef> NameOrErr = RelSecI->getName();
712     if (!NameOrErr)
713       return NameOrErr.takeError();
714     StringRef RelSectionName = *NameOrErr;
715 
716     if (RelSectionName != ".opd")
717       continue;
718 
719     for (elf_relocation_iterator i = si->relocation_begin(),
720                                  e = si->relocation_end();
721          i != e;) {
722       // The R_PPC64_ADDR64 relocation indicates the first field
723       // of a .opd entry
724       uint64_t TypeFunc = i->getType();
725       if (TypeFunc != ELF::R_PPC64_ADDR64) {
726         ++i;
727         continue;
728       }
729 
730       uint64_t TargetSymbolOffset = i->getOffset();
731       symbol_iterator TargetSymbol = i->getSymbol();
732       int64_t Addend;
733       if (auto AddendOrErr = i->getAddend())
734         Addend = *AddendOrErr;
735       else
736         return AddendOrErr.takeError();
737 
738       ++i;
739       if (i == e)
740         break;
741 
742       // Just check if following relocation is a R_PPC64_TOC
743       uint64_t TypeTOC = i->getType();
744       if (TypeTOC != ELF::R_PPC64_TOC)
745         continue;
746 
747       // Finally compares the Symbol value and the target symbol offset
748       // to check if this .opd entry refers to the symbol the relocation
749       // points to.
750       if (Rel.Addend != (int64_t)TargetSymbolOffset)
751         continue;
752 
753       section_iterator TSI = Obj.section_end();
754       if (auto TSIOrErr = TargetSymbol->getSection())
755         TSI = *TSIOrErr;
756       else
757         return TSIOrErr.takeError();
758       assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
759 
760       bool IsCode = TSI->isText();
761       if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
762                                                   LocalSections))
763         Rel.SectionID = *SectionIDOrErr;
764       else
765         return SectionIDOrErr.takeError();
766       Rel.Addend = (intptr_t)Addend;
767       return Error::success();
768     }
769   }
770   llvm_unreachable("Attempting to get address of ODP entry!");
771 }
772 
773 // Relocation masks following the #lo(value), #hi(value), #ha(value),
774 // #higher(value), #highera(value), #highest(value), and #highesta(value)
775 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
776 // document.
777 
778 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
779 
780 static inline uint16_t applyPPChi(uint64_t value) {
781   return (value >> 16) & 0xffff;
782 }
783 
784 static inline uint16_t applyPPCha (uint64_t value) {
785   return ((value + 0x8000) >> 16) & 0xffff;
786 }
787 
788 static inline uint16_t applyPPChigher(uint64_t value) {
789   return (value >> 32) & 0xffff;
790 }
791 
792 static inline uint16_t applyPPChighera (uint64_t value) {
793   return ((value + 0x8000) >> 32) & 0xffff;
794 }
795 
796 static inline uint16_t applyPPChighest(uint64_t value) {
797   return (value >> 48) & 0xffff;
798 }
799 
800 static inline uint16_t applyPPChighesta (uint64_t value) {
801   return ((value + 0x8000) >> 48) & 0xffff;
802 }
803 
804 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
805                                             uint64_t Offset, uint64_t Value,
806                                             uint32_t Type, int64_t Addend) {
807   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
808   switch (Type) {
809   default:
810     report_fatal_error("Relocation type not implemented yet!");
811     break;
812   case ELF::R_PPC_ADDR16_LO:
813     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
814     break;
815   case ELF::R_PPC_ADDR16_HI:
816     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
817     break;
818   case ELF::R_PPC_ADDR16_HA:
819     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
820     break;
821   }
822 }
823 
824 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
825                                             uint64_t Offset, uint64_t Value,
826                                             uint32_t Type, int64_t Addend) {
827   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
828   switch (Type) {
829   default:
830     report_fatal_error("Relocation type not implemented yet!");
831     break;
832   case ELF::R_PPC64_ADDR16:
833     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
834     break;
835   case ELF::R_PPC64_ADDR16_DS:
836     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
837     break;
838   case ELF::R_PPC64_ADDR16_LO:
839     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
840     break;
841   case ELF::R_PPC64_ADDR16_LO_DS:
842     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
843     break;
844   case ELF::R_PPC64_ADDR16_HI:
845   case ELF::R_PPC64_ADDR16_HIGH:
846     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
847     break;
848   case ELF::R_PPC64_ADDR16_HA:
849   case ELF::R_PPC64_ADDR16_HIGHA:
850     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
851     break;
852   case ELF::R_PPC64_ADDR16_HIGHER:
853     writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
854     break;
855   case ELF::R_PPC64_ADDR16_HIGHERA:
856     writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
857     break;
858   case ELF::R_PPC64_ADDR16_HIGHEST:
859     writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
860     break;
861   case ELF::R_PPC64_ADDR16_HIGHESTA:
862     writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
863     break;
864   case ELF::R_PPC64_ADDR14: {
865     assert(((Value + Addend) & 3) == 0);
866     // Preserve the AA/LK bits in the branch instruction
867     uint8_t aalk = *(LocalAddress + 3);
868     writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
869   } break;
870   case ELF::R_PPC64_REL16_LO: {
871     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
872     uint64_t Delta = Value - FinalAddress + Addend;
873     writeInt16BE(LocalAddress, applyPPClo(Delta));
874   } break;
875   case ELF::R_PPC64_REL16_HI: {
876     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
877     uint64_t Delta = Value - FinalAddress + Addend;
878     writeInt16BE(LocalAddress, applyPPChi(Delta));
879   } break;
880   case ELF::R_PPC64_REL16_HA: {
881     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
882     uint64_t Delta = Value - FinalAddress + Addend;
883     writeInt16BE(LocalAddress, applyPPCha(Delta));
884   } break;
885   case ELF::R_PPC64_ADDR32: {
886     int64_t Result = static_cast<int64_t>(Value + Addend);
887     if (SignExtend64<32>(Result) != Result)
888       llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
889     writeInt32BE(LocalAddress, Result);
890   } break;
891   case ELF::R_PPC64_REL24: {
892     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
893     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
894     if (SignExtend64<26>(delta) != delta)
895       llvm_unreachable("Relocation R_PPC64_REL24 overflow");
896     // We preserve bits other than LI field, i.e. PO and AA/LK fields.
897     uint32_t Inst = readBytesUnaligned(LocalAddress, 4);
898     writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
899   } break;
900   case ELF::R_PPC64_REL32: {
901     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
902     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
903     if (SignExtend64<32>(delta) != delta)
904       llvm_unreachable("Relocation R_PPC64_REL32 overflow");
905     writeInt32BE(LocalAddress, delta);
906   } break;
907   case ELF::R_PPC64_REL64: {
908     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
909     uint64_t Delta = Value - FinalAddress + Addend;
910     writeInt64BE(LocalAddress, Delta);
911   } break;
912   case ELF::R_PPC64_ADDR64:
913     writeInt64BE(LocalAddress, Value + Addend);
914     break;
915   }
916 }
917 
918 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
919                                               uint64_t Offset, uint64_t Value,
920                                               uint32_t Type, int64_t Addend) {
921   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
922   switch (Type) {
923   default:
924     report_fatal_error("Relocation type not implemented yet!");
925     break;
926   case ELF::R_390_PC16DBL:
927   case ELF::R_390_PLT16DBL: {
928     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
929     assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
930     writeInt16BE(LocalAddress, Delta / 2);
931     break;
932   }
933   case ELF::R_390_PC32DBL:
934   case ELF::R_390_PLT32DBL: {
935     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
936     assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
937     writeInt32BE(LocalAddress, Delta / 2);
938     break;
939   }
940   case ELF::R_390_PC16: {
941     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
942     assert(int16_t(Delta) == Delta && "R_390_PC16 overflow");
943     writeInt16BE(LocalAddress, Delta);
944     break;
945   }
946   case ELF::R_390_PC32: {
947     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
948     assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
949     writeInt32BE(LocalAddress, Delta);
950     break;
951   }
952   case ELF::R_390_PC64: {
953     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
954     writeInt64BE(LocalAddress, Delta);
955     break;
956   }
957   case ELF::R_390_8:
958     *LocalAddress = (uint8_t)(Value + Addend);
959     break;
960   case ELF::R_390_16:
961     writeInt16BE(LocalAddress, Value + Addend);
962     break;
963   case ELF::R_390_32:
964     writeInt32BE(LocalAddress, Value + Addend);
965     break;
966   case ELF::R_390_64:
967     writeInt64BE(LocalAddress, Value + Addend);
968     break;
969   }
970 }
971 
972 void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section,
973                                           uint64_t Offset, uint64_t Value,
974                                           uint32_t Type, int64_t Addend) {
975   bool isBE = Arch == Triple::bpfeb;
976 
977   switch (Type) {
978   default:
979     report_fatal_error("Relocation type not implemented yet!");
980     break;
981   case ELF::R_BPF_NONE:
982   case ELF::R_BPF_64_64:
983   case ELF::R_BPF_64_32:
984   case ELF::R_BPF_64_NODYLD32:
985     break;
986   case ELF::R_BPF_64_ABS64: {
987     write(isBE, Section.getAddressWithOffset(Offset), Value + Addend);
988     LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
989                       << format("%p\n", Section.getAddressWithOffset(Offset)));
990     break;
991   }
992   case ELF::R_BPF_64_ABS32: {
993     Value += Addend;
994     assert(Value <= UINT32_MAX);
995     write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value));
996     LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at "
997                       << format("%p\n", Section.getAddressWithOffset(Offset)));
998     break;
999   }
1000   }
1001 }
1002 
1003 // The target location for the relocation is described by RE.SectionID and
1004 // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
1005 // SectionEntry has three members describing its location.
1006 // SectionEntry::Address is the address at which the section has been loaded
1007 // into memory in the current (host) process.  SectionEntry::LoadAddress is the
1008 // address that the section will have in the target process.
1009 // SectionEntry::ObjAddress is the address of the bits for this section in the
1010 // original emitted object image (also in the current address space).
1011 //
1012 // Relocations will be applied as if the section were loaded at
1013 // SectionEntry::LoadAddress, but they will be applied at an address based
1014 // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer to
1015 // Target memory contents if they are required for value calculations.
1016 //
1017 // The Value parameter here is the load address of the symbol for the
1018 // relocation to be applied.  For relocations which refer to symbols in the
1019 // current object Value will be the LoadAddress of the section in which
1020 // the symbol resides (RE.Addend provides additional information about the
1021 // symbol location).  For external symbols, Value will be the address of the
1022 // symbol in the target address space.
1023 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
1024                                        uint64_t Value) {
1025   const SectionEntry &Section = Sections[RE.SectionID];
1026   return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
1027                            RE.SymOffset, RE.SectionID);
1028 }
1029 
1030 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
1031                                        uint64_t Offset, uint64_t Value,
1032                                        uint32_t Type, int64_t Addend,
1033                                        uint64_t SymOffset, SID SectionID) {
1034   switch (Arch) {
1035   case Triple::x86_64:
1036     resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
1037     break;
1038   case Triple::x86:
1039     resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1040                          (uint32_t)(Addend & 0xffffffffL));
1041     break;
1042   case Triple::aarch64:
1043   case Triple::aarch64_be:
1044     resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
1045     break;
1046   case Triple::arm: // Fall through.
1047   case Triple::armeb:
1048   case Triple::thumb:
1049   case Triple::thumbeb:
1050     resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1051                          (uint32_t)(Addend & 0xffffffffL));
1052     break;
1053   case Triple::ppc: // Fall through.
1054   case Triple::ppcle:
1055     resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
1056     break;
1057   case Triple::ppc64: // Fall through.
1058   case Triple::ppc64le:
1059     resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
1060     break;
1061   case Triple::systemz:
1062     resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
1063     break;
1064   case Triple::bpfel:
1065   case Triple::bpfeb:
1066     resolveBPFRelocation(Section, Offset, Value, Type, Addend);
1067     break;
1068   default:
1069     llvm_unreachable("Unsupported CPU type!");
1070   }
1071 }
1072 
1073 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
1074   return (void *)(Sections[SectionID].getObjAddress() + Offset);
1075 }
1076 
1077 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
1078   RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
1079   if (Value.SymbolName)
1080     addRelocationForSymbol(RE, Value.SymbolName);
1081   else
1082     addRelocationForSection(RE, Value.SectionID);
1083 }
1084 
1085 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
1086                                                  bool IsLocal) const {
1087   switch (RelType) {
1088   case ELF::R_MICROMIPS_GOT16:
1089     if (IsLocal)
1090       return ELF::R_MICROMIPS_LO16;
1091     break;
1092   case ELF::R_MICROMIPS_HI16:
1093     return ELF::R_MICROMIPS_LO16;
1094   case ELF::R_MIPS_GOT16:
1095     if (IsLocal)
1096       return ELF::R_MIPS_LO16;
1097     break;
1098   case ELF::R_MIPS_HI16:
1099     return ELF::R_MIPS_LO16;
1100   case ELF::R_MIPS_PCHI16:
1101     return ELF::R_MIPS_PCLO16;
1102   default:
1103     break;
1104   }
1105   return ELF::R_MIPS_NONE;
1106 }
1107 
1108 // Sometimes we don't need to create thunk for a branch.
1109 // This typically happens when branch target is located
1110 // in the same object file. In such case target is either
1111 // a weak symbol or symbol in a different executable section.
1112 // This function checks if branch target is located in the
1113 // same object file and if distance between source and target
1114 // fits R_AARCH64_CALL26 relocation. If both conditions are
1115 // met, it emits direct jump to the target and returns true.
1116 // Otherwise false is returned and thunk is created.
1117 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1118     unsigned SectionID, relocation_iterator RelI,
1119     const RelocationValueRef &Value) {
1120   uint64_t Address;
1121   if (Value.SymbolName) {
1122     auto Loc = GlobalSymbolTable.find(Value.SymbolName);
1123 
1124     // Don't create direct branch for external symbols.
1125     if (Loc == GlobalSymbolTable.end())
1126       return false;
1127 
1128     const auto &SymInfo = Loc->second;
1129     Address =
1130         uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
1131             SymInfo.getOffset()));
1132   } else {
1133     Address = uint64_t(Sections[Value.SectionID].getLoadAddress());
1134   }
1135   uint64_t Offset = RelI->getOffset();
1136   uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
1137 
1138   // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
1139   // If distance between source and target is out of range then we should
1140   // create thunk.
1141   if (!isInt<28>(Address + Value.Addend - SourceAddress))
1142     return false;
1143 
1144   resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
1145                     Value.Addend);
1146 
1147   return true;
1148 }
1149 
1150 void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
1151                                           const RelocationValueRef &Value,
1152                                           relocation_iterator RelI,
1153                                           StubMap &Stubs) {
1154 
1155   LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1156   SectionEntry &Section = Sections[SectionID];
1157 
1158   uint64_t Offset = RelI->getOffset();
1159   unsigned RelType = RelI->getType();
1160   // Look for an existing stub.
1161   StubMap::const_iterator i = Stubs.find(Value);
1162   if (i != Stubs.end()) {
1163     resolveRelocation(Section, Offset,
1164                       (uint64_t)Section.getAddressWithOffset(i->second),
1165                       RelType, 0);
1166     LLVM_DEBUG(dbgs() << " Stub function found\n");
1167   } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1168     // Create a new stub function.
1169     LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1170     Stubs[Value] = Section.getStubOffset();
1171     uint8_t *StubTargetAddr = createStubFunction(
1172         Section.getAddressWithOffset(Section.getStubOffset()));
1173 
1174     RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(),
1175                               ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1176     RelocationEntry REmovk_g2(SectionID,
1177                               StubTargetAddr - Section.getAddress() + 4,
1178                               ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1179     RelocationEntry REmovk_g1(SectionID,
1180                               StubTargetAddr - Section.getAddress() + 8,
1181                               ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1182     RelocationEntry REmovk_g0(SectionID,
1183                               StubTargetAddr - Section.getAddress() + 12,
1184                               ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1185 
1186     if (Value.SymbolName) {
1187       addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1188       addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1189       addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1190       addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1191     } else {
1192       addRelocationForSection(REmovz_g3, Value.SectionID);
1193       addRelocationForSection(REmovk_g2, Value.SectionID);
1194       addRelocationForSection(REmovk_g1, Value.SectionID);
1195       addRelocationForSection(REmovk_g0, Value.SectionID);
1196     }
1197     resolveRelocation(Section, Offset,
1198                       reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
1199                           Section.getStubOffset())),
1200                       RelType, 0);
1201     Section.advanceStubOffset(getMaxStubSize());
1202   }
1203 }
1204 
1205 Expected<relocation_iterator>
1206 RuntimeDyldELF::processRelocationRef(
1207     unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
1208     ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
1209   const auto &Obj = cast<ELFObjectFileBase>(O);
1210   uint64_t RelType = RelI->getType();
1211   int64_t Addend = 0;
1212   if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend())
1213     Addend = *AddendOrErr;
1214   else
1215     consumeError(AddendOrErr.takeError());
1216   elf_symbol_iterator Symbol = RelI->getSymbol();
1217 
1218   // Obtain the symbol name which is referenced in the relocation
1219   StringRef TargetName;
1220   if (Symbol != Obj.symbol_end()) {
1221     if (auto TargetNameOrErr = Symbol->getName())
1222       TargetName = *TargetNameOrErr;
1223     else
1224       return TargetNameOrErr.takeError();
1225   }
1226   LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
1227                     << " TargetName: " << TargetName << "\n");
1228   RelocationValueRef Value;
1229   // First search for the symbol in the local symbol table
1230   SymbolRef::Type SymType = SymbolRef::ST_Unknown;
1231 
1232   // Search for the symbol in the global symbol table
1233   RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
1234   if (Symbol != Obj.symbol_end()) {
1235     gsi = GlobalSymbolTable.find(TargetName.data());
1236     Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1237     if (!SymTypeOrErr) {
1238       std::string Buf;
1239       raw_string_ostream OS(Buf);
1240       logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
1241       report_fatal_error(Twine(OS.str()));
1242     }
1243     SymType = *SymTypeOrErr;
1244   }
1245   if (gsi != GlobalSymbolTable.end()) {
1246     const auto &SymInfo = gsi->second;
1247     Value.SectionID = SymInfo.getSectionID();
1248     Value.Offset = SymInfo.getOffset();
1249     Value.Addend = SymInfo.getOffset() + Addend;
1250   } else {
1251     switch (SymType) {
1252     case SymbolRef::ST_Debug: {
1253       // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
1254       // and can be changed by another developers. Maybe best way is add
1255       // a new symbol type ST_Section to SymbolRef and use it.
1256       auto SectionOrErr = Symbol->getSection();
1257       if (!SectionOrErr) {
1258         std::string Buf;
1259         raw_string_ostream OS(Buf);
1260         logAllUnhandledErrors(SectionOrErr.takeError(), OS);
1261         report_fatal_error(Twine(OS.str()));
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       // Create 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