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