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