1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of ELF support for the MC-JIT runtime dynamic linker.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RuntimeDyldELF.h"
14 #include "RuntimeDyldCheckerImpl.h"
15 #include "Targets/RuntimeDyldELFMips.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 
25 using namespace llvm;
26 using namespace llvm::object;
27 using namespace llvm::support::endian;
28 
29 #define DEBUG_TYPE "dyld"
30 
31 static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
32 
33 static void or32AArch64Imm(void *L, uint64_t Imm) {
34   or32le(L, (Imm & 0xFFF) << 10);
35 }
36 
37 template <class T> static void write(bool isBE, void *P, T V) {
38   isBE ? write<T, support::big>(P, V) : write<T, support::little>(P, V);
39 }
40 
41 static void write32AArch64Addr(void *L, uint64_t Imm) {
42   uint32_t ImmLo = (Imm & 0x3) << 29;
43   uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
44   uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
45   write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
46 }
47 
48 // Return the bits [Start, End] from Val shifted Start bits.
49 // For instance, getBits(0xF0, 4, 8) returns 0xF.
50 static uint64_t getBits(uint64_t Val, int Start, int End) {
51   uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
52   return (Val >> Start) & Mask;
53 }
54 
55 namespace {
56 
57 template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
58   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
59 
60   typedef typename ELFT::uint addr_type;
61 
62   DyldELFObject(ELFObjectFile<ELFT> &&Obj);
63 
64 public:
65   static Expected<std::unique_ptr<DyldELFObject>>
66   create(MemoryBufferRef Wrapper);
67 
68   void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
69 
70   void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
71 
72   // Methods for type inquiry through isa, cast and dyn_cast
73   static bool classof(const Binary *v) {
74     return (isa<ELFObjectFile<ELFT>>(v) &&
75             classof(cast<ELFObjectFile<ELFT>>(v)));
76   }
77   static bool classof(const ELFObjectFile<ELFT> *v) {
78     return v->isDyldType();
79   }
80 };
81 
82 
83 
84 // The MemoryBuffer passed into this constructor is just a wrapper around the
85 // actual memory.  Ultimately, the Binary parent class will take ownership of
86 // this MemoryBuffer object but not the underlying memory.
87 template <class ELFT>
88 DyldELFObject<ELFT>::DyldELFObject(ELFObjectFile<ELFT> &&Obj)
89     : ELFObjectFile<ELFT>(std::move(Obj)) {
90   this->isDyldELFObject = true;
91 }
92 
93 template <class ELFT>
94 Expected<std::unique_ptr<DyldELFObject<ELFT>>>
95 DyldELFObject<ELFT>::create(MemoryBufferRef Wrapper) {
96   auto Obj = ELFObjectFile<ELFT>::create(Wrapper);
97   if (auto E = Obj.takeError())
98     return std::move(E);
99   std::unique_ptr<DyldELFObject<ELFT>> Ret(
100       new DyldELFObject<ELFT>(std::move(*Obj)));
101   return std::move(Ret);
102 }
103 
104 template <class ELFT>
105 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
106                                                uint64_t Addr) {
107   DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
108   Elf_Shdr *shdr =
109       const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
110 
111   // This assumes the address passed in matches the target address bitness
112   // The template-based type cast handles everything else.
113   shdr->sh_addr = static_cast<addr_type>(Addr);
114 }
115 
116 template <class ELFT>
117 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
118                                               uint64_t Addr) {
119 
120   Elf_Sym *sym = const_cast<Elf_Sym *>(
121       ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
122 
123   // This assumes the address passed in matches the target address bitness
124   // The template-based type cast handles everything else.
125   sym->st_value = static_cast<addr_type>(Addr);
126 }
127 
128 class LoadedELFObjectInfo final
129     : public LoadedObjectInfoHelper<LoadedELFObjectInfo,
130                                     RuntimeDyld::LoadedObjectInfo> {
131 public:
132   LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
133       : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
134 
135   OwningBinary<ObjectFile>
136   getObjectForDebug(const ObjectFile &Obj) const override;
137 };
138 
139 template <typename ELFT>
140 static Expected<std::unique_ptr<DyldELFObject<ELFT>>>
141 createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject,
142                       const LoadedELFObjectInfo &L) {
143   typedef typename ELFT::Shdr Elf_Shdr;
144   typedef typename ELFT::uint addr_type;
145 
146   Expected<std::unique_ptr<DyldELFObject<ELFT>>> ObjOrErr =
147       DyldELFObject<ELFT>::create(Buffer);
148   if (Error E = ObjOrErr.takeError())
149     return std::move(E);
150 
151   std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr);
152 
153   // Iterate over all sections in the object.
154   auto SI = SourceObject.section_begin();
155   for (const auto &Sec : Obj->sections()) {
156     Expected<StringRef> NameOrErr = Sec.getName();
157     if (!NameOrErr) {
158       consumeError(NameOrErr.takeError());
159       continue;
160     }
161 
162     if (*NameOrErr != "") {
163       DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
164       Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
165           reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
166 
167       if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) {
168         // This assumes that the address passed in matches the target address
169         // bitness. The template-based type cast handles everything else.
170         shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
171       }
172     }
173     ++SI;
174   }
175 
176   return std::move(Obj);
177 }
178 
179 static OwningBinary<ObjectFile>
180 createELFDebugObject(const ObjectFile &Obj, const LoadedELFObjectInfo &L) {
181   assert(Obj.isELF() && "Not an ELF object file.");
182 
183   std::unique_ptr<MemoryBuffer> Buffer =
184     MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
185 
186   Expected<std::unique_ptr<ObjectFile>> DebugObj(nullptr);
187   handleAllErrors(DebugObj.takeError());
188   if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian())
189     DebugObj =
190         createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
191   else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian())
192     DebugObj =
193         createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
194   else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian())
195     DebugObj =
196         createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
197   else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian())
198     DebugObj =
199         createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
200   else
201     llvm_unreachable("Unexpected ELF format");
202 
203   handleAllErrors(DebugObj.takeError());
204   return OwningBinary<ObjectFile>(std::move(*DebugObj), std::move(Buffer));
205 }
206 
207 OwningBinary<ObjectFile>
208 LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
209   return createELFDebugObject(Obj, *this);
210 }
211 
212 } // anonymous namespace
213 
214 namespace llvm {
215 
216 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
217                                JITSymbolResolver &Resolver)
218     : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
219 RuntimeDyldELF::~RuntimeDyldELF() {}
220 
221 void RuntimeDyldELF::registerEHFrames() {
222   for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
223     SID EHFrameSID = UnregisteredEHFrameSections[i];
224     uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
225     uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
226     size_t EHFrameSize = Sections[EHFrameSID].getSize();
227     MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
228   }
229   UnregisteredEHFrameSections.clear();
230 }
231 
232 std::unique_ptr<RuntimeDyldELF>
233 llvm::RuntimeDyldELF::create(Triple::ArchType Arch,
234                              RuntimeDyld::MemoryManager &MemMgr,
235                              JITSymbolResolver &Resolver) {
236   switch (Arch) {
237   default:
238     return std::make_unique<RuntimeDyldELF>(MemMgr, Resolver);
239   case Triple::mips:
240   case Triple::mipsel:
241   case Triple::mips64:
242   case Triple::mips64el:
243     return std::make_unique<RuntimeDyldELFMips>(MemMgr, Resolver);
244   }
245 }
246 
247 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
248 RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
249   if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
250     return std::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr);
251   else {
252     HasError = true;
253     raw_string_ostream ErrStream(ErrorStr);
254     logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
255     return nullptr;
256   }
257 }
258 
259 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
260                                              uint64_t Offset, uint64_t Value,
261                                              uint32_t Type, int64_t Addend,
262                                              uint64_t SymOffset) {
263   switch (Type) {
264   default:
265     report_fatal_error("Relocation type not implemented yet!");
266     break;
267   case ELF::R_X86_64_NONE:
268     break;
269   case ELF::R_X86_64_64: {
270     support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
271         Value + Addend;
272     LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
273                       << format("%p\n", Section.getAddressWithOffset(Offset)));
274     break;
275   }
276   case ELF::R_X86_64_32:
277   case ELF::R_X86_64_32S: {
278     Value += Addend;
279     assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
280            (Type == ELF::R_X86_64_32S &&
281             ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
282     uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
283     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
284         TruncatedAddr;
285     LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
286                       << format("%p\n", Section.getAddressWithOffset(Offset)));
287     break;
288   }
289   case ELF::R_X86_64_PC8: {
290     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
291     int64_t RealOffset = Value + Addend - FinalAddress;
292     assert(isInt<8>(RealOffset));
293     int8_t TruncOffset = (RealOffset & 0xFF);
294     Section.getAddress()[Offset] = TruncOffset;
295     break;
296   }
297   case ELF::R_X86_64_PC32: {
298     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
299     int64_t RealOffset = Value + Addend - FinalAddress;
300     assert(isInt<32>(RealOffset));
301     int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
302     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
303         TruncOffset;
304     break;
305   }
306   case ELF::R_X86_64_PC64: {
307     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
308     int64_t RealOffset = Value + Addend - FinalAddress;
309     support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
310         RealOffset;
311     LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset) << " at "
312                       << format("%p\n", FinalAddress));
313     break;
314   }
315   case ELF::R_X86_64_GOTOFF64: {
316     // Compute Value - GOTBase.
317     uint64_t GOTBase = 0;
318     for (const auto &Section : Sections) {
319       if (Section.getName() == ".got") {
320         GOTBase = Section.getLoadAddressWithOffset(0);
321         break;
322       }
323     }
324     assert(GOTBase != 0 && "missing GOT");
325     int64_t GOTOffset = Value - GOTBase + Addend;
326     support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = GOTOffset;
327     break;
328   }
329   }
330 }
331 
332 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
333                                           uint64_t Offset, uint32_t Value,
334                                           uint32_t Type, int32_t Addend) {
335   switch (Type) {
336   case ELF::R_386_32: {
337     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
338         Value + Addend;
339     break;
340   }
341   // Handle R_386_PLT32 like R_386_PC32 since it should be able to
342   // reach any 32 bit address.
343   case ELF::R_386_PLT32:
344   case ELF::R_386_PC32: {
345     uint32_t FinalAddress =
346         Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
347     uint32_t RealOffset = Value + Addend - FinalAddress;
348     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
349         RealOffset;
350     break;
351   }
352   default:
353     // There are other relocation types, but it appears these are the
354     // only ones currently used by the LLVM ELF object writer
355     report_fatal_error("Relocation type not implemented yet!");
356     break;
357   }
358 }
359 
360 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
361                                               uint64_t Offset, uint64_t Value,
362                                               uint32_t Type, int64_t Addend) {
363   uint32_t *TargetPtr =
364       reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
365   uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
366   // Data should use target endian. Code should always use little endian.
367   bool isBE = Arch == Triple::aarch64_be;
368 
369   LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
370                     << format("%llx", Section.getAddressWithOffset(Offset))
371                     << " FinalAddress: 0x" << format("%llx", FinalAddress)
372                     << " Value: 0x" << format("%llx", Value) << " Type: 0x"
373                     << format("%x", Type) << " Addend: 0x"
374                     << format("%llx", Addend) << "\n");
375 
376   switch (Type) {
377   default:
378     report_fatal_error("Relocation type not implemented yet!");
379     break;
380   case ELF::R_AARCH64_ABS16: {
381     uint64_t Result = Value + Addend;
382     assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
383     write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
384     break;
385   }
386   case ELF::R_AARCH64_ABS32: {
387     uint64_t Result = Value + Addend;
388     assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
389     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
390     break;
391   }
392   case ELF::R_AARCH64_ABS64:
393     write(isBE, TargetPtr, Value + Addend);
394     break;
395   case ELF::R_AARCH64_PLT32: {
396     uint64_t Result = Value + Addend - FinalAddress;
397     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
398            static_cast<int64_t>(Result) <= INT32_MAX);
399     write(isBE, TargetPtr, static_cast<uint32_t>(Result));
400     break;
401   }
402   case ELF::R_AARCH64_PREL32: {
403     uint64_t Result = Value + Addend - FinalAddress;
404     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
405            static_cast<int64_t>(Result) <= UINT32_MAX);
406     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
407     break;
408   }
409   case ELF::R_AARCH64_PREL64:
410     write(isBE, TargetPtr, Value + Addend - FinalAddress);
411     break;
412   case ELF::R_AARCH64_CALL26: // fallthrough
413   case ELF::R_AARCH64_JUMP26: {
414     // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
415     // calculation.
416     uint64_t BranchImm = Value + Addend - FinalAddress;
417 
418     // "Check that -2^27 <= result < 2^27".
419     assert(isInt<28>(BranchImm));
420     or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
421     break;
422   }
423   case ELF::R_AARCH64_MOVW_UABS_G3:
424     or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
425     break;
426   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
427     or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
428     break;
429   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
430     or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
431     break;
432   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
433     or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
434     break;
435   case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
436     // Operation: Page(S+A) - Page(P)
437     uint64_t Result =
438         ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
439 
440     // Check that -2^32 <= X < 2^32
441     assert(isInt<33>(Result) && "overflow check failed for relocation");
442 
443     // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
444     // from bits 32:12 of X.
445     write32AArch64Addr(TargetPtr, Result >> 12);
446     break;
447   }
448   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
449     // Operation: S + A
450     // Immediate goes in bits 21:10 of LD/ST instruction, taken
451     // from bits 11:0 of X
452     or32AArch64Imm(TargetPtr, Value + Addend);
453     break;
454   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
455     // Operation: S + A
456     // Immediate goes in bits 21:10 of LD/ST instruction, taken
457     // from bits 11:0 of X
458     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11));
459     break;
460   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
461     // Operation: S + A
462     // Immediate goes in bits 21:10 of LD/ST instruction, taken
463     // from bits 11:1 of X
464     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11));
465     break;
466   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
467     // Operation: S + A
468     // Immediate goes in bits 21:10 of LD/ST instruction, taken
469     // from bits 11:2 of X
470     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
471     break;
472   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
473     // Operation: S + A
474     // Immediate goes in bits 21:10 of LD/ST instruction, taken
475     // from bits 11:3 of X
476     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
477     break;
478   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
479     // Operation: S + A
480     // Immediate goes in bits 21:10 of LD/ST instruction, taken
481     // from bits 11:4 of X
482     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11));
483     break;
484   }
485 }
486 
487 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
488                                           uint64_t Offset, uint32_t Value,
489                                           uint32_t Type, int32_t Addend) {
490   // TODO: Add Thumb relocations.
491   uint32_t *TargetPtr =
492       reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
493   uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
494   Value += Addend;
495 
496   LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
497                     << Section.getAddressWithOffset(Offset)
498                     << " FinalAddress: " << format("%p", FinalAddress)
499                     << " Value: " << format("%x", Value)
500                     << " Type: " << format("%x", Type)
501                     << " Addend: " << format("%x", Addend) << "\n");
502 
503   switch (Type) {
504   default:
505     llvm_unreachable("Not implemented relocation type!");
506 
507   case ELF::R_ARM_NONE:
508     break;
509     // Write a 31bit signed offset
510   case ELF::R_ARM_PREL31:
511     support::ulittle32_t::ref{TargetPtr} =
512         (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
513         ((Value - FinalAddress) & ~0x80000000);
514     break;
515   case ELF::R_ARM_TARGET1:
516   case ELF::R_ARM_ABS32:
517     support::ulittle32_t::ref{TargetPtr} = Value;
518     break;
519     // Write first 16 bit of 32 bit value to the mov instruction.
520     // Last 4 bit should be shifted.
521   case ELF::R_ARM_MOVW_ABS_NC:
522   case ELF::R_ARM_MOVT_ABS:
523     if (Type == ELF::R_ARM_MOVW_ABS_NC)
524       Value = Value & 0xFFFF;
525     else if (Type == ELF::R_ARM_MOVT_ABS)
526       Value = (Value >> 16) & 0xFFFF;
527     support::ulittle32_t::ref{TargetPtr} =
528         (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
529         (((Value >> 12) & 0xF) << 16);
530     break;
531     // Write 24 bit relative value to the branch instruction.
532   case ELF::R_ARM_PC24: // Fall through.
533   case ELF::R_ARM_CALL: // Fall through.
534   case ELF::R_ARM_JUMP24:
535     int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
536     RelValue = (RelValue & 0x03FFFFFC) >> 2;
537     assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
538     support::ulittle32_t::ref{TargetPtr} =
539         (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
540     break;
541   }
542 }
543 
544 void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
545   if (Arch == Triple::UnknownArch ||
546       !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
547     IsMipsO32ABI = false;
548     IsMipsN32ABI = false;
549     IsMipsN64ABI = false;
550     return;
551   }
552   if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
553     unsigned AbiVariant = E->getPlatformFlags();
554     IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
555     IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
556   }
557   IsMipsN64ABI = Obj.getFileFormatName().equals("elf64-mips");
558 }
559 
560 // Return the .TOC. section and offset.
561 Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
562                                           ObjSectionToIDMap &LocalSections,
563                                           RelocationValueRef &Rel) {
564   // Set a default SectionID in case we do not find a TOC section below.
565   // This may happen for references to TOC base base (sym@toc, .odp
566   // relocation) without a .toc directive.  In this case just use the
567   // first section (which is usually the .odp) since the code won't
568   // reference the .toc base directly.
569   Rel.SymbolName = nullptr;
570   Rel.SectionID = 0;
571 
572   // The TOC consists of sections .got, .toc, .tocbss, .plt in that
573   // order. The TOC starts where the first of these sections starts.
574   for (auto &Section : Obj.sections()) {
575     Expected<StringRef> NameOrErr = Section.getName();
576     if (!NameOrErr)
577       return NameOrErr.takeError();
578     StringRef SectionName = *NameOrErr;
579 
580     if (SectionName == ".got"
581         || SectionName == ".toc"
582         || SectionName == ".tocbss"
583         || SectionName == ".plt") {
584       if (auto SectionIDOrErr =
585             findOrEmitSection(Obj, Section, false, LocalSections))
586         Rel.SectionID = *SectionIDOrErr;
587       else
588         return SectionIDOrErr.takeError();
589       break;
590     }
591   }
592 
593   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
594   // thus permitting a full 64 Kbytes segment.
595   Rel.Addend = 0x8000;
596 
597   return Error::success();
598 }
599 
600 // Returns the sections and offset associated with the ODP entry referenced
601 // by Symbol.
602 Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
603                                           ObjSectionToIDMap &LocalSections,
604                                           RelocationValueRef &Rel) {
605   // Get the ELF symbol value (st_value) to compare with Relocation offset in
606   // .opd entries
607   for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
608        si != se; ++si) {
609 
610     Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
611     if (!RelSecOrErr)
612       report_fatal_error(toString(RelSecOrErr.takeError()));
613 
614     section_iterator RelSecI = *RelSecOrErr;
615     if (RelSecI == Obj.section_end())
616       continue;
617 
618     Expected<StringRef> NameOrErr = RelSecI->getName();
619     if (!NameOrErr)
620       return NameOrErr.takeError();
621     StringRef RelSectionName = *NameOrErr;
622 
623     if (RelSectionName != ".opd")
624       continue;
625 
626     for (elf_relocation_iterator i = si->relocation_begin(),
627                                  e = si->relocation_end();
628          i != e;) {
629       // The R_PPC64_ADDR64 relocation indicates the first field
630       // of a .opd entry
631       uint64_t TypeFunc = i->getType();
632       if (TypeFunc != ELF::R_PPC64_ADDR64) {
633         ++i;
634         continue;
635       }
636 
637       uint64_t TargetSymbolOffset = i->getOffset();
638       symbol_iterator TargetSymbol = i->getSymbol();
639       int64_t Addend;
640       if (auto AddendOrErr = i->getAddend())
641         Addend = *AddendOrErr;
642       else
643         return AddendOrErr.takeError();
644 
645       ++i;
646       if (i == e)
647         break;
648 
649       // Just check if following relocation is a R_PPC64_TOC
650       uint64_t TypeTOC = i->getType();
651       if (TypeTOC != ELF::R_PPC64_TOC)
652         continue;
653 
654       // Finally compares the Symbol value and the target symbol offset
655       // to check if this .opd entry refers to the symbol the relocation
656       // points to.
657       if (Rel.Addend != (int64_t)TargetSymbolOffset)
658         continue;
659 
660       section_iterator TSI = Obj.section_end();
661       if (auto TSIOrErr = TargetSymbol->getSection())
662         TSI = *TSIOrErr;
663       else
664         return TSIOrErr.takeError();
665       assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
666 
667       bool IsCode = TSI->isText();
668       if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
669                                                   LocalSections))
670         Rel.SectionID = *SectionIDOrErr;
671       else
672         return SectionIDOrErr.takeError();
673       Rel.Addend = (intptr_t)Addend;
674       return Error::success();
675     }
676   }
677   llvm_unreachable("Attempting to get address of ODP entry!");
678 }
679 
680 // Relocation masks following the #lo(value), #hi(value), #ha(value),
681 // #higher(value), #highera(value), #highest(value), and #highesta(value)
682 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
683 // document.
684 
685 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
686 
687 static inline uint16_t applyPPChi(uint64_t value) {
688   return (value >> 16) & 0xffff;
689 }
690 
691 static inline uint16_t applyPPCha (uint64_t value) {
692   return ((value + 0x8000) >> 16) & 0xffff;
693 }
694 
695 static inline uint16_t applyPPChigher(uint64_t value) {
696   return (value >> 32) & 0xffff;
697 }
698 
699 static inline uint16_t applyPPChighera (uint64_t value) {
700   return ((value + 0x8000) >> 32) & 0xffff;
701 }
702 
703 static inline uint16_t applyPPChighest(uint64_t value) {
704   return (value >> 48) & 0xffff;
705 }
706 
707 static inline uint16_t applyPPChighesta (uint64_t value) {
708   return ((value + 0x8000) >> 48) & 0xffff;
709 }
710 
711 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
712                                             uint64_t Offset, uint64_t Value,
713                                             uint32_t Type, int64_t Addend) {
714   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
715   switch (Type) {
716   default:
717     report_fatal_error("Relocation type not implemented yet!");
718     break;
719   case ELF::R_PPC_ADDR16_LO:
720     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
721     break;
722   case ELF::R_PPC_ADDR16_HI:
723     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
724     break;
725   case ELF::R_PPC_ADDR16_HA:
726     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
727     break;
728   }
729 }
730 
731 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
732                                             uint64_t Offset, uint64_t Value,
733                                             uint32_t Type, int64_t Addend) {
734   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
735   switch (Type) {
736   default:
737     report_fatal_error("Relocation type not implemented yet!");
738     break;
739   case ELF::R_PPC64_ADDR16:
740     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
741     break;
742   case ELF::R_PPC64_ADDR16_DS:
743     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
744     break;
745   case ELF::R_PPC64_ADDR16_LO:
746     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
747     break;
748   case ELF::R_PPC64_ADDR16_LO_DS:
749     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
750     break;
751   case ELF::R_PPC64_ADDR16_HI:
752   case ELF::R_PPC64_ADDR16_HIGH:
753     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
754     break;
755   case ELF::R_PPC64_ADDR16_HA:
756   case ELF::R_PPC64_ADDR16_HIGHA:
757     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
758     break;
759   case ELF::R_PPC64_ADDR16_HIGHER:
760     writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
761     break;
762   case ELF::R_PPC64_ADDR16_HIGHERA:
763     writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
764     break;
765   case ELF::R_PPC64_ADDR16_HIGHEST:
766     writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
767     break;
768   case ELF::R_PPC64_ADDR16_HIGHESTA:
769     writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
770     break;
771   case ELF::R_PPC64_ADDR14: {
772     assert(((Value + Addend) & 3) == 0);
773     // Preserve the AA/LK bits in the branch instruction
774     uint8_t aalk = *(LocalAddress + 3);
775     writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
776   } break;
777   case ELF::R_PPC64_REL16_LO: {
778     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
779     uint64_t Delta = Value - FinalAddress + Addend;
780     writeInt16BE(LocalAddress, applyPPClo(Delta));
781   } break;
782   case ELF::R_PPC64_REL16_HI: {
783     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
784     uint64_t Delta = Value - FinalAddress + Addend;
785     writeInt16BE(LocalAddress, applyPPChi(Delta));
786   } break;
787   case ELF::R_PPC64_REL16_HA: {
788     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
789     uint64_t Delta = Value - FinalAddress + Addend;
790     writeInt16BE(LocalAddress, applyPPCha(Delta));
791   } break;
792   case ELF::R_PPC64_ADDR32: {
793     int64_t Result = static_cast<int64_t>(Value + Addend);
794     if (SignExtend64<32>(Result) != Result)
795       llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
796     writeInt32BE(LocalAddress, Result);
797   } break;
798   case ELF::R_PPC64_REL24: {
799     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
800     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
801     if (SignExtend64<26>(delta) != delta)
802       llvm_unreachable("Relocation R_PPC64_REL24 overflow");
803     // We preserve bits other than LI field, i.e. PO and AA/LK fields.
804     uint32_t Inst = readBytesUnaligned(LocalAddress, 4);
805     writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
806   } break;
807   case ELF::R_PPC64_REL32: {
808     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
809     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
810     if (SignExtend64<32>(delta) != delta)
811       llvm_unreachable("Relocation R_PPC64_REL32 overflow");
812     writeInt32BE(LocalAddress, delta);
813   } break;
814   case ELF::R_PPC64_REL64: {
815     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
816     uint64_t Delta = Value - FinalAddress + Addend;
817     writeInt64BE(LocalAddress, Delta);
818   } break;
819   case ELF::R_PPC64_ADDR64:
820     writeInt64BE(LocalAddress, Value + Addend);
821     break;
822   }
823 }
824 
825 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
826                                               uint64_t Offset, uint64_t Value,
827                                               uint32_t Type, int64_t Addend) {
828   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
829   switch (Type) {
830   default:
831     report_fatal_error("Relocation type not implemented yet!");
832     break;
833   case ELF::R_390_PC16DBL:
834   case ELF::R_390_PLT16DBL: {
835     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
836     assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
837     writeInt16BE(LocalAddress, Delta / 2);
838     break;
839   }
840   case ELF::R_390_PC32DBL:
841   case ELF::R_390_PLT32DBL: {
842     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
843     assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
844     writeInt32BE(LocalAddress, Delta / 2);
845     break;
846   }
847   case ELF::R_390_PC16: {
848     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
849     assert(int16_t(Delta) == Delta && "R_390_PC16 overflow");
850     writeInt16BE(LocalAddress, Delta);
851     break;
852   }
853   case ELF::R_390_PC32: {
854     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
855     assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
856     writeInt32BE(LocalAddress, Delta);
857     break;
858   }
859   case ELF::R_390_PC64: {
860     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
861     writeInt64BE(LocalAddress, Delta);
862     break;
863   }
864   case ELF::R_390_8:
865     *LocalAddress = (uint8_t)(Value + Addend);
866     break;
867   case ELF::R_390_16:
868     writeInt16BE(LocalAddress, Value + Addend);
869     break;
870   case ELF::R_390_32:
871     writeInt32BE(LocalAddress, Value + Addend);
872     break;
873   case ELF::R_390_64:
874     writeInt64BE(LocalAddress, Value + Addend);
875     break;
876   }
877 }
878 
879 void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section,
880                                           uint64_t Offset, uint64_t Value,
881                                           uint32_t Type, int64_t Addend) {
882   bool isBE = Arch == Triple::bpfeb;
883 
884   switch (Type) {
885   default:
886     report_fatal_error("Relocation type not implemented yet!");
887     break;
888   case ELF::R_BPF_NONE:
889     break;
890   case ELF::R_BPF_64_64: {
891     write(isBE, Section.getAddressWithOffset(Offset), Value + Addend);
892     LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
893                       << format("%p\n", Section.getAddressWithOffset(Offset)));
894     break;
895   }
896   case ELF::R_BPF_64_32: {
897     Value += Addend;
898     assert(Value <= UINT32_MAX);
899     write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value));
900     LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at "
901                       << format("%p\n", Section.getAddressWithOffset(Offset)));
902     break;
903   }
904   }
905 }
906 
907 // The target location for the relocation is described by RE.SectionID and
908 // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
909 // SectionEntry has three members describing its location.
910 // SectionEntry::Address is the address at which the section has been loaded
911 // into memory in the current (host) process.  SectionEntry::LoadAddress is the
912 // address that the section will have in the target process.
913 // SectionEntry::ObjAddress is the address of the bits for this section in the
914 // original emitted object image (also in the current address space).
915 //
916 // Relocations will be applied as if the section were loaded at
917 // SectionEntry::LoadAddress, but they will be applied at an address based
918 // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer to
919 // Target memory contents if they are required for value calculations.
920 //
921 // The Value parameter here is the load address of the symbol for the
922 // relocation to be applied.  For relocations which refer to symbols in the
923 // current object Value will be the LoadAddress of the section in which
924 // the symbol resides (RE.Addend provides additional information about the
925 // symbol location).  For external symbols, Value will be the address of the
926 // symbol in the target address space.
927 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
928                                        uint64_t Value) {
929   const SectionEntry &Section = Sections[RE.SectionID];
930   return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
931                            RE.SymOffset, RE.SectionID);
932 }
933 
934 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
935                                        uint64_t Offset, uint64_t Value,
936                                        uint32_t Type, int64_t Addend,
937                                        uint64_t SymOffset, SID SectionID) {
938   switch (Arch) {
939   case Triple::x86_64:
940     resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
941     break;
942   case Triple::x86:
943     resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
944                          (uint32_t)(Addend & 0xffffffffL));
945     break;
946   case Triple::aarch64:
947   case Triple::aarch64_be:
948     resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
949     break;
950   case Triple::arm: // Fall through.
951   case Triple::armeb:
952   case Triple::thumb:
953   case Triple::thumbeb:
954     resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
955                          (uint32_t)(Addend & 0xffffffffL));
956     break;
957   case Triple::ppc: // Fall through.
958   case Triple::ppcle:
959     resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
960     break;
961   case Triple::ppc64: // Fall through.
962   case Triple::ppc64le:
963     resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
964     break;
965   case Triple::systemz:
966     resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
967     break;
968   case Triple::bpfel:
969   case Triple::bpfeb:
970     resolveBPFRelocation(Section, Offset, Value, Type, Addend);
971     break;
972   default:
973     llvm_unreachable("Unsupported CPU type!");
974   }
975 }
976 
977 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
978   return (void *)(Sections[SectionID].getObjAddress() + Offset);
979 }
980 
981 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
982   RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
983   if (Value.SymbolName)
984     addRelocationForSymbol(RE, Value.SymbolName);
985   else
986     addRelocationForSection(RE, Value.SectionID);
987 }
988 
989 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
990                                                  bool IsLocal) const {
991   switch (RelType) {
992   case ELF::R_MICROMIPS_GOT16:
993     if (IsLocal)
994       return ELF::R_MICROMIPS_LO16;
995     break;
996   case ELF::R_MICROMIPS_HI16:
997     return ELF::R_MICROMIPS_LO16;
998   case ELF::R_MIPS_GOT16:
999     if (IsLocal)
1000       return ELF::R_MIPS_LO16;
1001     break;
1002   case ELF::R_MIPS_HI16:
1003     return ELF::R_MIPS_LO16;
1004   case ELF::R_MIPS_PCHI16:
1005     return ELF::R_MIPS_PCLO16;
1006   default:
1007     break;
1008   }
1009   return ELF::R_MIPS_NONE;
1010 }
1011 
1012 // Sometimes we don't need to create thunk for a branch.
1013 // This typically happens when branch target is located
1014 // in the same object file. In such case target is either
1015 // a weak symbol or symbol in a different executable section.
1016 // This function checks if branch target is located in the
1017 // same object file and if distance between source and target
1018 // fits R_AARCH64_CALL26 relocation. If both conditions are
1019 // met, it emits direct jump to the target and returns true.
1020 // Otherwise false is returned and thunk is created.
1021 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1022     unsigned SectionID, relocation_iterator RelI,
1023     const RelocationValueRef &Value) {
1024   uint64_t Address;
1025   if (Value.SymbolName) {
1026     auto Loc = GlobalSymbolTable.find(Value.SymbolName);
1027 
1028     // Don't create direct branch for external symbols.
1029     if (Loc == GlobalSymbolTable.end())
1030       return false;
1031 
1032     const auto &SymInfo = Loc->second;
1033     Address =
1034         uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
1035             SymInfo.getOffset()));
1036   } else {
1037     Address = uint64_t(Sections[Value.SectionID].getLoadAddress());
1038   }
1039   uint64_t Offset = RelI->getOffset();
1040   uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
1041 
1042   // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
1043   // If distance between source and target is out of range then we should
1044   // create thunk.
1045   if (!isInt<28>(Address + Value.Addend - SourceAddress))
1046     return false;
1047 
1048   resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
1049                     Value.Addend);
1050 
1051   return true;
1052 }
1053 
1054 void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
1055                                           const RelocationValueRef &Value,
1056                                           relocation_iterator RelI,
1057                                           StubMap &Stubs) {
1058 
1059   LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1060   SectionEntry &Section = Sections[SectionID];
1061 
1062   uint64_t Offset = RelI->getOffset();
1063   unsigned RelType = RelI->getType();
1064   // Look for an existing stub.
1065   StubMap::const_iterator i = Stubs.find(Value);
1066   if (i != Stubs.end()) {
1067     resolveRelocation(Section, Offset,
1068                       (uint64_t)Section.getAddressWithOffset(i->second),
1069                       RelType, 0);
1070     LLVM_DEBUG(dbgs() << " Stub function found\n");
1071   } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1072     // Create a new stub function.
1073     LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1074     Stubs[Value] = Section.getStubOffset();
1075     uint8_t *StubTargetAddr = createStubFunction(
1076         Section.getAddressWithOffset(Section.getStubOffset()));
1077 
1078     RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(),
1079                               ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1080     RelocationEntry REmovk_g2(SectionID,
1081                               StubTargetAddr - Section.getAddress() + 4,
1082                               ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1083     RelocationEntry REmovk_g1(SectionID,
1084                               StubTargetAddr - Section.getAddress() + 8,
1085                               ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1086     RelocationEntry REmovk_g0(SectionID,
1087                               StubTargetAddr - Section.getAddress() + 12,
1088                               ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1089 
1090     if (Value.SymbolName) {
1091       addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1092       addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1093       addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1094       addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1095     } else {
1096       addRelocationForSection(REmovz_g3, Value.SectionID);
1097       addRelocationForSection(REmovk_g2, Value.SectionID);
1098       addRelocationForSection(REmovk_g1, Value.SectionID);
1099       addRelocationForSection(REmovk_g0, Value.SectionID);
1100     }
1101     resolveRelocation(Section, Offset,
1102                       reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
1103                           Section.getStubOffset())),
1104                       RelType, 0);
1105     Section.advanceStubOffset(getMaxStubSize());
1106   }
1107 }
1108 
1109 Expected<relocation_iterator>
1110 RuntimeDyldELF::processRelocationRef(
1111     unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
1112     ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
1113   const auto &Obj = cast<ELFObjectFileBase>(O);
1114   uint64_t RelType = RelI->getType();
1115   int64_t Addend = 0;
1116   if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend())
1117     Addend = *AddendOrErr;
1118   else
1119     consumeError(AddendOrErr.takeError());
1120   elf_symbol_iterator Symbol = RelI->getSymbol();
1121 
1122   // Obtain the symbol name which is referenced in the relocation
1123   StringRef TargetName;
1124   if (Symbol != Obj.symbol_end()) {
1125     if (auto TargetNameOrErr = Symbol->getName())
1126       TargetName = *TargetNameOrErr;
1127     else
1128       return TargetNameOrErr.takeError();
1129   }
1130   LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
1131                     << " TargetName: " << TargetName << "\n");
1132   RelocationValueRef Value;
1133   // First search for the symbol in the local symbol table
1134   SymbolRef::Type SymType = SymbolRef::ST_Unknown;
1135 
1136   // Search for the symbol in the global symbol table
1137   RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
1138   if (Symbol != Obj.symbol_end()) {
1139     gsi = GlobalSymbolTable.find(TargetName.data());
1140     Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1141     if (!SymTypeOrErr) {
1142       std::string Buf;
1143       raw_string_ostream OS(Buf);
1144       logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
1145       OS.flush();
1146       report_fatal_error(Buf);
1147     }
1148     SymType = *SymTypeOrErr;
1149   }
1150   if (gsi != GlobalSymbolTable.end()) {
1151     const auto &SymInfo = gsi->second;
1152     Value.SectionID = SymInfo.getSectionID();
1153     Value.Offset = SymInfo.getOffset();
1154     Value.Addend = SymInfo.getOffset() + Addend;
1155   } else {
1156     switch (SymType) {
1157     case SymbolRef::ST_Debug: {
1158       // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
1159       // and can be changed by another developers. Maybe best way is add
1160       // a new symbol type ST_Section to SymbolRef and use it.
1161       auto SectionOrErr = Symbol->getSection();
1162       if (!SectionOrErr) {
1163         std::string Buf;
1164         raw_string_ostream OS(Buf);
1165         logAllUnhandledErrors(SectionOrErr.takeError(), OS);
1166         OS.flush();
1167         report_fatal_error(Buf);
1168       }
1169       section_iterator si = *SectionOrErr;
1170       if (si == Obj.section_end())
1171         llvm_unreachable("Symbol section not found, bad object file format!");
1172       LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n");
1173       bool isCode = si->isText();
1174       if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode,
1175                                                   ObjSectionToID))
1176         Value.SectionID = *SectionIDOrErr;
1177       else
1178         return SectionIDOrErr.takeError();
1179       Value.Addend = Addend;
1180       break;
1181     }
1182     case SymbolRef::ST_Data:
1183     case SymbolRef::ST_Function:
1184     case SymbolRef::ST_Unknown: {
1185       Value.SymbolName = TargetName.data();
1186       Value.Addend = Addend;
1187 
1188       // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
1189       // will manifest here as a NULL symbol name.
1190       // We can set this as a valid (but empty) symbol name, and rely
1191       // on addRelocationForSymbol to handle this.
1192       if (!Value.SymbolName)
1193         Value.SymbolName = "";
1194       break;
1195     }
1196     default:
1197       llvm_unreachable("Unresolved symbol type!");
1198       break;
1199     }
1200   }
1201 
1202   uint64_t Offset = RelI->getOffset();
1203 
1204   LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
1205                     << "\n");
1206   if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be)) {
1207     if (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26) {
1208       resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1209     } else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1210       // Craete new GOT entry or find existing one. If GOT entry is
1211       // to be created, then we also emit ABS64 relocation for it.
1212       uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1213       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1214                                  ELF::R_AARCH64_ADR_PREL_PG_HI21);
1215 
1216     } else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1217       uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1218       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1219                                  ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1220     } else {
1221       processSimpleRelocation(SectionID, Offset, RelType, Value);
1222     }
1223   } else if (Arch == Triple::arm) {
1224     if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1225       RelType == ELF::R_ARM_JUMP24) {
1226       // This is an ARM branch relocation, need to use a stub function.
1227       LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
1228       SectionEntry &Section = Sections[SectionID];
1229 
1230       // Look for an existing stub.
1231       StubMap::const_iterator i = Stubs.find(Value);
1232       if (i != Stubs.end()) {
1233         resolveRelocation(
1234             Section, Offset,
1235             reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
1236             RelType, 0);
1237         LLVM_DEBUG(dbgs() << " Stub function found\n");
1238       } else {
1239         // Create a new stub function.
1240         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1241         Stubs[Value] = Section.getStubOffset();
1242         uint8_t *StubTargetAddr = createStubFunction(
1243             Section.getAddressWithOffset(Section.getStubOffset()));
1244         RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1245                            ELF::R_ARM_ABS32, Value.Addend);
1246         if (Value.SymbolName)
1247           addRelocationForSymbol(RE, Value.SymbolName);
1248         else
1249           addRelocationForSection(RE, Value.SectionID);
1250 
1251         resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1252                                                Section.getAddressWithOffset(
1253                                                    Section.getStubOffset())),
1254                           RelType, 0);
1255         Section.advanceStubOffset(getMaxStubSize());
1256       }
1257     } else {
1258       uint32_t *Placeholder =
1259         reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
1260       if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1261           RelType == ELF::R_ARM_ABS32) {
1262         Value.Addend += *Placeholder;
1263       } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1264         // See ELF for ARM documentation
1265         Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1266       }
1267       processSimpleRelocation(SectionID, Offset, RelType, Value);
1268     }
1269   } else if (IsMipsO32ABI) {
1270     uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
1271         computePlaceholderAddress(SectionID, Offset));
1272     uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
1273     if (RelType == ELF::R_MIPS_26) {
1274       // This is an Mips branch relocation, need to use a stub function.
1275       LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1276       SectionEntry &Section = Sections[SectionID];
1277 
1278       // Extract the addend from the instruction.
1279       // We shift up by two since the Value will be down shifted again
1280       // when applying the relocation.
1281       uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1282 
1283       Value.Addend += Addend;
1284 
1285       //  Look up for existing stub.
1286       StubMap::const_iterator i = Stubs.find(Value);
1287       if (i != Stubs.end()) {
1288         RelocationEntry RE(SectionID, Offset, RelType, i->second);
1289         addRelocationForSection(RE, SectionID);
1290         LLVM_DEBUG(dbgs() << " Stub function found\n");
1291       } else {
1292         // Create a new stub function.
1293         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1294         Stubs[Value] = Section.getStubOffset();
1295 
1296         unsigned AbiVariant = Obj.getPlatformFlags();
1297 
1298         uint8_t *StubTargetAddr = createStubFunction(
1299             Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1300 
1301         // Creating Hi and Lo relocations for the filled stub instructions.
1302         RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1303                              ELF::R_MIPS_HI16, Value.Addend);
1304         RelocationEntry RELo(SectionID,
1305                              StubTargetAddr - Section.getAddress() + 4,
1306                              ELF::R_MIPS_LO16, Value.Addend);
1307 
1308         if (Value.SymbolName) {
1309           addRelocationForSymbol(REHi, Value.SymbolName);
1310           addRelocationForSymbol(RELo, Value.SymbolName);
1311         } else {
1312           addRelocationForSection(REHi, Value.SectionID);
1313           addRelocationForSection(RELo, Value.SectionID);
1314         }
1315 
1316         RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1317         addRelocationForSection(RE, SectionID);
1318         Section.advanceStubOffset(getMaxStubSize());
1319       }
1320     } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1321       int64_t Addend = (Opcode & 0x0000ffff) << 16;
1322       RelocationEntry RE(SectionID, Offset, RelType, Addend);
1323       PendingRelocs.push_back(std::make_pair(Value, RE));
1324     } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1325       int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1326       for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) {
1327         const RelocationValueRef &MatchingValue = I->first;
1328         RelocationEntry &Reloc = I->second;
1329         if (MatchingValue == Value &&
1330             RelType == getMatchingLoRelocation(Reloc.RelType) &&
1331             SectionID == Reloc.SectionID) {
1332           Reloc.Addend += Addend;
1333           if (Value.SymbolName)
1334             addRelocationForSymbol(Reloc, Value.SymbolName);
1335           else
1336             addRelocationForSection(Reloc, Value.SectionID);
1337           I = PendingRelocs.erase(I);
1338         } else
1339           ++I;
1340       }
1341       RelocationEntry RE(SectionID, Offset, RelType, Addend);
1342       if (Value.SymbolName)
1343         addRelocationForSymbol(RE, Value.SymbolName);
1344       else
1345         addRelocationForSection(RE, Value.SectionID);
1346     } else {
1347       if (RelType == ELF::R_MIPS_32)
1348         Value.Addend += Opcode;
1349       else if (RelType == ELF::R_MIPS_PC16)
1350         Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1351       else if (RelType == ELF::R_MIPS_PC19_S2)
1352         Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1353       else if (RelType == ELF::R_MIPS_PC21_S2)
1354         Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1355       else if (RelType == ELF::R_MIPS_PC26_S2)
1356         Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1357       processSimpleRelocation(SectionID, Offset, RelType, Value);
1358     }
1359   } else if (IsMipsN32ABI || IsMipsN64ABI) {
1360     uint32_t r_type = RelType & 0xff;
1361     RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1362     if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1363         || r_type == ELF::R_MIPS_GOT_DISP) {
1364       StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName);
1365       if (i != GOTSymbolOffsets.end())
1366         RE.SymOffset = i->second;
1367       else {
1368         RE.SymOffset = allocateGOTEntries(1);
1369         GOTSymbolOffsets[TargetName] = RE.SymOffset;
1370       }
1371       if (Value.SymbolName)
1372         addRelocationForSymbol(RE, Value.SymbolName);
1373       else
1374         addRelocationForSection(RE, Value.SectionID);
1375     } else if (RelType == ELF::R_MIPS_26) {
1376       // This is an Mips branch relocation, need to use a stub function.
1377       LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1378       SectionEntry &Section = Sections[SectionID];
1379 
1380       //  Look up for existing stub.
1381       StubMap::const_iterator i = Stubs.find(Value);
1382       if (i != Stubs.end()) {
1383         RelocationEntry RE(SectionID, Offset, RelType, i->second);
1384         addRelocationForSection(RE, SectionID);
1385         LLVM_DEBUG(dbgs() << " Stub function found\n");
1386       } else {
1387         // Create a new stub function.
1388         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1389         Stubs[Value] = Section.getStubOffset();
1390 
1391         unsigned AbiVariant = Obj.getPlatformFlags();
1392 
1393         uint8_t *StubTargetAddr = createStubFunction(
1394             Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1395 
1396         if (IsMipsN32ABI) {
1397           // Creating Hi and Lo relocations for the filled stub instructions.
1398           RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1399                                ELF::R_MIPS_HI16, Value.Addend);
1400           RelocationEntry RELo(SectionID,
1401                                StubTargetAddr - Section.getAddress() + 4,
1402                                ELF::R_MIPS_LO16, Value.Addend);
1403           if (Value.SymbolName) {
1404             addRelocationForSymbol(REHi, Value.SymbolName);
1405             addRelocationForSymbol(RELo, Value.SymbolName);
1406           } else {
1407             addRelocationForSection(REHi, Value.SectionID);
1408             addRelocationForSection(RELo, Value.SectionID);
1409           }
1410         } else {
1411           // Creating Highest, Higher, Hi and Lo relocations for the filled stub
1412           // instructions.
1413           RelocationEntry REHighest(SectionID,
1414                                     StubTargetAddr - Section.getAddress(),
1415                                     ELF::R_MIPS_HIGHEST, Value.Addend);
1416           RelocationEntry REHigher(SectionID,
1417                                    StubTargetAddr - Section.getAddress() + 4,
1418                                    ELF::R_MIPS_HIGHER, Value.Addend);
1419           RelocationEntry REHi(SectionID,
1420                                StubTargetAddr - Section.getAddress() + 12,
1421                                ELF::R_MIPS_HI16, Value.Addend);
1422           RelocationEntry RELo(SectionID,
1423                                StubTargetAddr - Section.getAddress() + 20,
1424                                ELF::R_MIPS_LO16, Value.Addend);
1425           if (Value.SymbolName) {
1426             addRelocationForSymbol(REHighest, Value.SymbolName);
1427             addRelocationForSymbol(REHigher, Value.SymbolName);
1428             addRelocationForSymbol(REHi, Value.SymbolName);
1429             addRelocationForSymbol(RELo, Value.SymbolName);
1430           } else {
1431             addRelocationForSection(REHighest, Value.SectionID);
1432             addRelocationForSection(REHigher, Value.SectionID);
1433             addRelocationForSection(REHi, Value.SectionID);
1434             addRelocationForSection(RELo, Value.SectionID);
1435           }
1436         }
1437         RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1438         addRelocationForSection(RE, SectionID);
1439         Section.advanceStubOffset(getMaxStubSize());
1440       }
1441     } else {
1442       processSimpleRelocation(SectionID, Offset, RelType, Value);
1443     }
1444 
1445   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
1446     if (RelType == ELF::R_PPC64_REL24) {
1447       // Determine ABI variant in use for this object.
1448       unsigned AbiVariant = Obj.getPlatformFlags();
1449       AbiVariant &= ELF::EF_PPC64_ABI;
1450       // A PPC branch relocation will need a stub function if the target is
1451       // an external symbol (either Value.SymbolName is set, or SymType is
1452       // Symbol::ST_Unknown) or if the target address is not within the
1453       // signed 24-bits branch address.
1454       SectionEntry &Section = Sections[SectionID];
1455       uint8_t *Target = Section.getAddressWithOffset(Offset);
1456       bool RangeOverflow = false;
1457       bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown;
1458       if (!IsExtern) {
1459         if (AbiVariant != 2) {
1460           // In the ELFv1 ABI, a function call may point to the .opd entry,
1461           // so the final symbol value is calculated based on the relocation
1462           // values in the .opd section.
1463           if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
1464             return std::move(Err);
1465         } else {
1466           // In the ELFv2 ABI, a function symbol may provide a local entry
1467           // point, which must be used for direct calls.
1468           if (Value.SectionID == SectionID){
1469             uint8_t SymOther = Symbol->getOther();
1470             Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
1471           }
1472         }
1473         uint8_t *RelocTarget =
1474             Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
1475         int64_t delta = static_cast<int64_t>(Target - RelocTarget);
1476         // If it is within 26-bits branch range, just set the branch target
1477         if (SignExtend64<26>(delta) != delta) {
1478           RangeOverflow = true;
1479         } else if ((AbiVariant != 2) ||
1480                    (AbiVariant == 2  && Value.SectionID == SectionID)) {
1481           RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1482           addRelocationForSection(RE, Value.SectionID);
1483         }
1484       }
1485       if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID) ||
1486           RangeOverflow) {
1487         // It is an external symbol (either Value.SymbolName is set, or
1488         // SymType is SymbolRef::ST_Unknown) or out of range.
1489         StubMap::const_iterator i = Stubs.find(Value);
1490         if (i != Stubs.end()) {
1491           // Symbol function stub already created, just relocate to it
1492           resolveRelocation(Section, Offset,
1493                             reinterpret_cast<uint64_t>(
1494                                 Section.getAddressWithOffset(i->second)),
1495                             RelType, 0);
1496           LLVM_DEBUG(dbgs() << " Stub function found\n");
1497         } else {
1498           // Create a new stub function.
1499           LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1500           Stubs[Value] = Section.getStubOffset();
1501           uint8_t *StubTargetAddr = createStubFunction(
1502               Section.getAddressWithOffset(Section.getStubOffset()),
1503               AbiVariant);
1504           RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1505                              ELF::R_PPC64_ADDR64, Value.Addend);
1506 
1507           // Generates the 64-bits address loads as exemplified in section
1508           // 4.5.1 in PPC64 ELF ABI.  Note that the relocations need to
1509           // apply to the low part of the instructions, so we have to update
1510           // the offset according to the target endianness.
1511           uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
1512           if (!IsTargetLittleEndian)
1513             StubRelocOffset += 2;
1514 
1515           RelocationEntry REhst(SectionID, StubRelocOffset + 0,
1516                                 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
1517           RelocationEntry REhr(SectionID, StubRelocOffset + 4,
1518                                ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
1519           RelocationEntry REh(SectionID, StubRelocOffset + 12,
1520                               ELF::R_PPC64_ADDR16_HI, Value.Addend);
1521           RelocationEntry REl(SectionID, StubRelocOffset + 16,
1522                               ELF::R_PPC64_ADDR16_LO, Value.Addend);
1523 
1524           if (Value.SymbolName) {
1525             addRelocationForSymbol(REhst, Value.SymbolName);
1526             addRelocationForSymbol(REhr, Value.SymbolName);
1527             addRelocationForSymbol(REh, Value.SymbolName);
1528             addRelocationForSymbol(REl, Value.SymbolName);
1529           } else {
1530             addRelocationForSection(REhst, Value.SectionID);
1531             addRelocationForSection(REhr, Value.SectionID);
1532             addRelocationForSection(REh, Value.SectionID);
1533             addRelocationForSection(REl, Value.SectionID);
1534           }
1535 
1536           resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1537                                                  Section.getAddressWithOffset(
1538                                                      Section.getStubOffset())),
1539                             RelType, 0);
1540           Section.advanceStubOffset(getMaxStubSize());
1541         }
1542         if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) {
1543           // Restore the TOC for external calls
1544           if (AbiVariant == 2)
1545             writeInt32BE(Target + 4, 0xE8410018); // ld r2,24(r1)
1546           else
1547             writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
1548         }
1549       }
1550     } else if (RelType == ELF::R_PPC64_TOC16 ||
1551                RelType == ELF::R_PPC64_TOC16_DS ||
1552                RelType == ELF::R_PPC64_TOC16_LO ||
1553                RelType == ELF::R_PPC64_TOC16_LO_DS ||
1554                RelType == ELF::R_PPC64_TOC16_HI ||
1555                RelType == ELF::R_PPC64_TOC16_HA) {
1556       // These relocations are supposed to subtract the TOC address from
1557       // the final value.  This does not fit cleanly into the RuntimeDyld
1558       // scheme, since there may be *two* sections involved in determining
1559       // the relocation value (the section of the symbol referred to by the
1560       // relocation, and the TOC section associated with the current module).
1561       //
1562       // Fortunately, these relocations are currently only ever generated
1563       // referring to symbols that themselves reside in the TOC, which means
1564       // that the two sections are actually the same.  Thus they cancel out
1565       // and we can immediately resolve the relocation right now.
1566       switch (RelType) {
1567       case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
1568       case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
1569       case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
1570       case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
1571       case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
1572       case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
1573       default: llvm_unreachable("Wrong relocation type.");
1574       }
1575 
1576       RelocationValueRef TOCValue;
1577       if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
1578         return std::move(Err);
1579       if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
1580         llvm_unreachable("Unsupported TOC relocation.");
1581       Value.Addend -= TOCValue.Addend;
1582       resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
1583     } else {
1584       // There are two ways to refer to the TOC address directly: either
1585       // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1586       // ignored), or via any relocation that refers to the magic ".TOC."
1587       // symbols (in which case the addend is respected).
1588       if (RelType == ELF::R_PPC64_TOC) {
1589         RelType = ELF::R_PPC64_ADDR64;
1590         if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1591           return std::move(Err);
1592       } else if (TargetName == ".TOC.") {
1593         if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
1594           return std::move(Err);
1595         Value.Addend += Addend;
1596       }
1597 
1598       RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1599 
1600       if (Value.SymbolName)
1601         addRelocationForSymbol(RE, Value.SymbolName);
1602       else
1603         addRelocationForSection(RE, Value.SectionID);
1604     }
1605   } else if (Arch == Triple::systemz &&
1606              (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
1607     // Create function stubs for both PLT and GOT references, regardless of
1608     // whether the GOT reference is to data or code.  The stub contains the
1609     // full address of the symbol, as needed by GOT references, and the
1610     // executable part only adds an overhead of 8 bytes.
1611     //
1612     // We could try to conserve space by allocating the code and data
1613     // parts of the stub separately.  However, as things stand, we allocate
1614     // a stub for every relocation, so using a GOT in JIT code should be
1615     // no less space efficient than using an explicit constant pool.
1616     LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1617     SectionEntry &Section = Sections[SectionID];
1618 
1619     // Look for an existing stub.
1620     StubMap::const_iterator i = Stubs.find(Value);
1621     uintptr_t StubAddress;
1622     if (i != Stubs.end()) {
1623       StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
1624       LLVM_DEBUG(dbgs() << " Stub function found\n");
1625     } else {
1626       // Create a new stub function.
1627       LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1628 
1629       uintptr_t BaseAddress = uintptr_t(Section.getAddress());
1630       uintptr_t StubAlignment = getStubAlignment();
1631       StubAddress =
1632           (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
1633           -StubAlignment;
1634       unsigned StubOffset = StubAddress - BaseAddress;
1635 
1636       Stubs[Value] = StubOffset;
1637       createStubFunction((uint8_t *)StubAddress);
1638       RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
1639                          Value.Offset);
1640       if (Value.SymbolName)
1641         addRelocationForSymbol(RE, Value.SymbolName);
1642       else
1643         addRelocationForSection(RE, Value.SectionID);
1644       Section.advanceStubOffset(getMaxStubSize());
1645     }
1646 
1647     if (RelType == ELF::R_390_GOTENT)
1648       resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
1649                         Addend);
1650     else
1651       resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
1652   } else if (Arch == Triple::x86_64) {
1653     if (RelType == ELF::R_X86_64_PLT32) {
1654       // The way the PLT relocations normally work is that the linker allocates
1655       // the
1656       // PLT and this relocation makes a PC-relative call into the PLT.  The PLT
1657       // entry will then jump to an address provided by the GOT.  On first call,
1658       // the
1659       // GOT address will point back into PLT code that resolves the symbol. After
1660       // the first call, the GOT entry points to the actual function.
1661       //
1662       // For local functions we're ignoring all of that here and just replacing
1663       // the PLT32 relocation type with PC32, which will translate the relocation
1664       // into a PC-relative call directly to the function. For external symbols we
1665       // can't be sure the function will be within 2^32 bytes of the call site, so
1666       // we need to create a stub, which calls into the GOT.  This case is
1667       // equivalent to the usual PLT implementation except that we use the stub
1668       // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1669       // rather than allocating a PLT section.
1670       if (Value.SymbolName) {
1671         // This is a call to an external function.
1672         // Look for an existing stub.
1673         SectionEntry *Section = &Sections[SectionID];
1674         StubMap::const_iterator i = Stubs.find(Value);
1675         uintptr_t StubAddress;
1676         if (i != Stubs.end()) {
1677           StubAddress = uintptr_t(Section->getAddress()) + i->second;
1678           LLVM_DEBUG(dbgs() << " Stub function found\n");
1679         } else {
1680           // Create a new stub function (equivalent to a PLT entry).
1681           LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1682 
1683           uintptr_t BaseAddress = uintptr_t(Section->getAddress());
1684           uintptr_t StubAlignment = getStubAlignment();
1685           StubAddress =
1686               (BaseAddress + Section->getStubOffset() + StubAlignment - 1) &
1687               -StubAlignment;
1688           unsigned StubOffset = StubAddress - BaseAddress;
1689           Stubs[Value] = StubOffset;
1690           createStubFunction((uint8_t *)StubAddress);
1691 
1692           // Bump our stub offset counter
1693           Section->advanceStubOffset(getMaxStubSize());
1694 
1695           // Allocate a GOT Entry
1696           uint64_t GOTOffset = allocateGOTEntries(1);
1697           // This potentially creates a new Section which potentially
1698           // invalidates the Section pointer, so reload it.
1699           Section = &Sections[SectionID];
1700 
1701           // The load of the GOT address has an addend of -4
1702           resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
1703                                      ELF::R_X86_64_PC32);
1704 
1705           // Fill in the value of the symbol we're targeting into the GOT
1706           addRelocationForSymbol(
1707               computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
1708               Value.SymbolName);
1709         }
1710 
1711         // Make the target call a call into the stub table.
1712         resolveRelocation(*Section, Offset, StubAddress, ELF::R_X86_64_PC32,
1713                           Addend);
1714       } else {
1715         RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
1716                   Value.Offset);
1717         addRelocationForSection(RE, Value.SectionID);
1718       }
1719     } else if (RelType == ELF::R_X86_64_GOTPCREL ||
1720                RelType == ELF::R_X86_64_GOTPCRELX ||
1721                RelType == ELF::R_X86_64_REX_GOTPCRELX) {
1722       uint64_t GOTOffset = allocateGOTEntries(1);
1723       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1724                                  ELF::R_X86_64_PC32);
1725 
1726       // Fill in the value of the symbol we're targeting into the GOT
1727       RelocationEntry RE =
1728           computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
1729       if (Value.SymbolName)
1730         addRelocationForSymbol(RE, Value.SymbolName);
1731       else
1732         addRelocationForSection(RE, Value.SectionID);
1733     } else if (RelType == ELF::R_X86_64_GOT64) {
1734       // Fill in a 64-bit GOT offset.
1735       uint64_t GOTOffset = allocateGOTEntries(1);
1736       resolveRelocation(Sections[SectionID], Offset, GOTOffset,
1737                         ELF::R_X86_64_64, 0);
1738 
1739       // Fill in the value of the symbol we're targeting into the GOT
1740       RelocationEntry RE =
1741           computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
1742       if (Value.SymbolName)
1743         addRelocationForSymbol(RE, Value.SymbolName);
1744       else
1745         addRelocationForSection(RE, Value.SectionID);
1746     } else if (RelType == ELF::R_X86_64_GOTPC64) {
1747       // Materialize the address of the base of the GOT relative to the PC.
1748       // This doesn't create a GOT entry, but it does mean we need a GOT
1749       // section.
1750       (void)allocateGOTEntries(0);
1751       resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
1752     } else if (RelType == ELF::R_X86_64_GOTOFF64) {
1753       // GOTOFF relocations ultimately require a section difference relocation.
1754       (void)allocateGOTEntries(0);
1755       processSimpleRelocation(SectionID, Offset, RelType, Value);
1756     } else if (RelType == ELF::R_X86_64_PC32) {
1757       Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
1758       processSimpleRelocation(SectionID, Offset, RelType, Value);
1759     } else if (RelType == ELF::R_X86_64_PC64) {
1760       Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset));
1761       processSimpleRelocation(SectionID, Offset, RelType, Value);
1762     } else {
1763       processSimpleRelocation(SectionID, Offset, RelType, Value);
1764     }
1765   } else {
1766     if (Arch == Triple::x86) {
1767       Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
1768     }
1769     processSimpleRelocation(SectionID, Offset, RelType, Value);
1770   }
1771   return ++RelI;
1772 }
1773 
1774 size_t RuntimeDyldELF::getGOTEntrySize() {
1775   // We don't use the GOT in all of these cases, but it's essentially free
1776   // to put them all here.
1777   size_t Result = 0;
1778   switch (Arch) {
1779   case Triple::x86_64:
1780   case Triple::aarch64:
1781   case Triple::aarch64_be:
1782   case Triple::ppc64:
1783   case Triple::ppc64le:
1784   case Triple::systemz:
1785     Result = sizeof(uint64_t);
1786     break;
1787   case Triple::x86:
1788   case Triple::arm:
1789   case Triple::thumb:
1790     Result = sizeof(uint32_t);
1791     break;
1792   case Triple::mips:
1793   case Triple::mipsel:
1794   case Triple::mips64:
1795   case Triple::mips64el:
1796     if (IsMipsO32ABI || IsMipsN32ABI)
1797       Result = sizeof(uint32_t);
1798     else if (IsMipsN64ABI)
1799       Result = sizeof(uint64_t);
1800     else
1801       llvm_unreachable("Mips ABI not handled");
1802     break;
1803   default:
1804     llvm_unreachable("Unsupported CPU type!");
1805   }
1806   return Result;
1807 }
1808 
1809 uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no) {
1810   if (GOTSectionID == 0) {
1811     GOTSectionID = Sections.size();
1812     // Reserve a section id. We'll allocate the section later
1813     // once we know the total size
1814     Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0));
1815   }
1816   uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
1817   CurrentGOTIndex += no;
1818   return StartOffset;
1819 }
1820 
1821 uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef &Value,
1822                                              unsigned GOTRelType) {
1823   auto E = GOTOffsetMap.insert({Value, 0});
1824   if (E.second) {
1825     uint64_t GOTOffset = allocateGOTEntries(1);
1826 
1827     // Create relocation for newly created GOT entry
1828     RelocationEntry RE =
1829         computeGOTOffsetRE(GOTOffset, Value.Offset, GOTRelType);
1830     if (Value.SymbolName)
1831       addRelocationForSymbol(RE, Value.SymbolName);
1832     else
1833       addRelocationForSection(RE, Value.SectionID);
1834 
1835     E.first->second = GOTOffset;
1836   }
1837 
1838   return E.first->second;
1839 }
1840 
1841 void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID,
1842                                                 uint64_t Offset,
1843                                                 uint64_t GOTOffset,
1844                                                 uint32_t Type) {
1845   // Fill in the relative address of the GOT Entry into the stub
1846   RelocationEntry GOTRE(SectionID, Offset, Type, GOTOffset);
1847   addRelocationForSection(GOTRE, GOTSectionID);
1848 }
1849 
1850 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
1851                                                    uint64_t SymbolOffset,
1852                                                    uint32_t Type) {
1853   return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
1854 }
1855 
1856 Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
1857                                   ObjSectionToIDMap &SectionMap) {
1858   if (IsMipsO32ABI)
1859     if (!PendingRelocs.empty())
1860       return make_error<RuntimeDyldError>("Can't find matching LO16 reloc");
1861 
1862   // If necessary, allocate the global offset table
1863   if (GOTSectionID != 0) {
1864     // Allocate memory for the section
1865     size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
1866     uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
1867                                                 GOTSectionID, ".got", false);
1868     if (!Addr)
1869       return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!");
1870 
1871     Sections[GOTSectionID] =
1872         SectionEntry(".got", Addr, TotalSize, TotalSize, 0);
1873 
1874     // For now, initialize all GOT entries to zero.  We'll fill them in as
1875     // needed when GOT-based relocations are applied.
1876     memset(Addr, 0, TotalSize);
1877     if (IsMipsN32ABI || IsMipsN64ABI) {
1878       // To correctly resolve Mips GOT relocations, we need a mapping from
1879       // object's sections to GOTs.
1880       for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
1881            SI != SE; ++SI) {
1882         if (SI->relocation_begin() != SI->relocation_end()) {
1883           Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
1884           if (!RelSecOrErr)
1885             return make_error<RuntimeDyldError>(
1886                 toString(RelSecOrErr.takeError()));
1887 
1888           section_iterator RelocatedSection = *RelSecOrErr;
1889           ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
1890           assert (i != SectionMap.end());
1891           SectionToGOTMap[i->second] = GOTSectionID;
1892         }
1893       }
1894       GOTSymbolOffsets.clear();
1895     }
1896   }
1897 
1898   // Look for and record the EH frame section.
1899   ObjSectionToIDMap::iterator i, e;
1900   for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
1901     const SectionRef &Section = i->first;
1902 
1903     StringRef Name;
1904     Expected<StringRef> NameOrErr = Section.getName();
1905     if (NameOrErr)
1906       Name = *NameOrErr;
1907     else
1908       consumeError(NameOrErr.takeError());
1909 
1910     if (Name == ".eh_frame") {
1911       UnregisteredEHFrameSections.push_back(i->second);
1912       break;
1913     }
1914   }
1915 
1916   GOTSectionID = 0;
1917   CurrentGOTIndex = 0;
1918 
1919   return Error::success();
1920 }
1921 
1922 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
1923   return Obj.isELF();
1924 }
1925 
1926 bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef &R) const {
1927   unsigned RelTy = R.getType();
1928   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
1929     return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
1930            RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
1931 
1932   if (Arch == Triple::x86_64)
1933     return RelTy == ELF::R_X86_64_GOTPCREL ||
1934            RelTy == ELF::R_X86_64_GOTPCRELX ||
1935            RelTy == ELF::R_X86_64_GOT64 ||
1936            RelTy == ELF::R_X86_64_REX_GOTPCRELX;
1937   return false;
1938 }
1939 
1940 bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const {
1941   if (Arch != Triple::x86_64)
1942     return true;  // Conservative answer
1943 
1944   switch (R.getType()) {
1945   default:
1946     return true;  // Conservative answer
1947 
1948 
1949   case ELF::R_X86_64_GOTPCREL:
1950   case ELF::R_X86_64_GOTPCRELX:
1951   case ELF::R_X86_64_REX_GOTPCRELX:
1952   case ELF::R_X86_64_GOTPC64:
1953   case ELF::R_X86_64_GOT64:
1954   case ELF::R_X86_64_GOTOFF64:
1955   case ELF::R_X86_64_PC32:
1956   case ELF::R_X86_64_PC64:
1957   case ELF::R_X86_64_64:
1958     // We know that these reloation types won't need a stub function.  This list
1959     // can be extended as needed.
1960     return false;
1961   }
1962 }
1963 
1964 } // namespace llvm
1965