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