xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implementation of ELF support for the MC-JIT runtime dynamic linker.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "RuntimeDyldELF.h"
140b57cec5SDimitry Andric #include "RuntimeDyldCheckerImpl.h"
150b57cec5SDimitry Andric #include "Targets/RuntimeDyldELFMips.h"
160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
190b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
200b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
210b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
220b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
230b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric using namespace llvm::object;
270b57cec5SDimitry Andric using namespace llvm::support::endian;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #define DEBUG_TYPE "dyld"
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric static void or32AArch64Imm(void *L, uint64_t Imm) {
340b57cec5SDimitry Andric   or32le(L, (Imm & 0xFFF) << 10);
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric template <class T> static void write(bool isBE, void *P, T V) {
380b57cec5SDimitry Andric   isBE ? write<T, support::big>(P, V) : write<T, support::little>(P, V);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric static void write32AArch64Addr(void *L, uint64_t Imm) {
420b57cec5SDimitry Andric   uint32_t ImmLo = (Imm & 0x3) << 29;
430b57cec5SDimitry Andric   uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
440b57cec5SDimitry Andric   uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
450b57cec5SDimitry Andric   write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric // Return the bits [Start, End] from Val shifted Start bits.
490b57cec5SDimitry Andric // For instance, getBits(0xF0, 4, 8) returns 0xF.
500b57cec5SDimitry Andric static uint64_t getBits(uint64_t Val, int Start, int End) {
510b57cec5SDimitry Andric   uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
520b57cec5SDimitry Andric   return (Val >> Start) & Mask;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric namespace {
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
580b57cec5SDimitry Andric   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
610b57cec5SDimitry Andric   typedef Elf_Sym_Impl<ELFT> Elf_Sym;
620b57cec5SDimitry Andric   typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
630b57cec5SDimitry Andric   typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   typedef typename ELFT::uint addr_type;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   DyldELFObject(ELFObjectFile<ELFT> &&Obj);
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric public:
720b57cec5SDimitry Andric   static Expected<std::unique_ptr<DyldELFObject>>
730b57cec5SDimitry Andric   create(MemoryBufferRef Wrapper);
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // Methods for type inquiry through isa, cast and dyn_cast
800b57cec5SDimitry Andric   static bool classof(const Binary *v) {
810b57cec5SDimitry Andric     return (isa<ELFObjectFile<ELFT>>(v) &&
820b57cec5SDimitry Andric             classof(cast<ELFObjectFile<ELFT>>(v)));
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric   static bool classof(const ELFObjectFile<ELFT> *v) {
850b57cec5SDimitry Andric     return v->isDyldType();
860b57cec5SDimitry Andric   }
870b57cec5SDimitry Andric };
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric // The MemoryBuffer passed into this constructor is just a wrapper around the
920b57cec5SDimitry Andric // actual memory.  Ultimately, the Binary parent class will take ownership of
930b57cec5SDimitry Andric // this MemoryBuffer object but not the underlying memory.
940b57cec5SDimitry Andric template <class ELFT>
950b57cec5SDimitry Andric DyldELFObject<ELFT>::DyldELFObject(ELFObjectFile<ELFT> &&Obj)
960b57cec5SDimitry Andric     : ELFObjectFile<ELFT>(std::move(Obj)) {
970b57cec5SDimitry Andric   this->isDyldELFObject = true;
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric template <class ELFT>
1010b57cec5SDimitry Andric Expected<std::unique_ptr<DyldELFObject<ELFT>>>
1020b57cec5SDimitry Andric DyldELFObject<ELFT>::create(MemoryBufferRef Wrapper) {
1030b57cec5SDimitry Andric   auto Obj = ELFObjectFile<ELFT>::create(Wrapper);
1040b57cec5SDimitry Andric   if (auto E = Obj.takeError())
1050b57cec5SDimitry Andric     return std::move(E);
1060b57cec5SDimitry Andric   std::unique_ptr<DyldELFObject<ELFT>> Ret(
1070b57cec5SDimitry Andric       new DyldELFObject<ELFT>(std::move(*Obj)));
1080b57cec5SDimitry Andric   return std::move(Ret);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric template <class ELFT>
1120b57cec5SDimitry Andric void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
1130b57cec5SDimitry Andric                                                uint64_t Addr) {
1140b57cec5SDimitry Andric   DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
1150b57cec5SDimitry Andric   Elf_Shdr *shdr =
1160b57cec5SDimitry Andric       const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   // This assumes the address passed in matches the target address bitness
1190b57cec5SDimitry Andric   // The template-based type cast handles everything else.
1200b57cec5SDimitry Andric   shdr->sh_addr = static_cast<addr_type>(Addr);
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric template <class ELFT>
1240b57cec5SDimitry Andric void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
1250b57cec5SDimitry Andric                                               uint64_t Addr) {
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   Elf_Sym *sym = const_cast<Elf_Sym *>(
1280b57cec5SDimitry Andric       ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl()));
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   // This assumes the address passed in matches the target address bitness
1310b57cec5SDimitry Andric   // The template-based type cast handles everything else.
1320b57cec5SDimitry Andric   sym->st_value = static_cast<addr_type>(Addr);
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric class LoadedELFObjectInfo final
1360b57cec5SDimitry Andric     : public LoadedObjectInfoHelper<LoadedELFObjectInfo,
1370b57cec5SDimitry Andric                                     RuntimeDyld::LoadedObjectInfo> {
1380b57cec5SDimitry Andric public:
1390b57cec5SDimitry Andric   LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
1400b57cec5SDimitry Andric       : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   OwningBinary<ObjectFile>
1430b57cec5SDimitry Andric   getObjectForDebug(const ObjectFile &Obj) const override;
1440b57cec5SDimitry Andric };
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric template <typename ELFT>
1470b57cec5SDimitry Andric static Expected<std::unique_ptr<DyldELFObject<ELFT>>>
1480b57cec5SDimitry Andric createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject,
1490b57cec5SDimitry Andric                       const LoadedELFObjectInfo &L) {
1500b57cec5SDimitry Andric   typedef typename ELFT::Shdr Elf_Shdr;
1510b57cec5SDimitry Andric   typedef typename ELFT::uint addr_type;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   Expected<std::unique_ptr<DyldELFObject<ELFT>>> ObjOrErr =
1540b57cec5SDimitry Andric       DyldELFObject<ELFT>::create(Buffer);
1550b57cec5SDimitry Andric   if (Error E = ObjOrErr.takeError())
1560b57cec5SDimitry Andric     return std::move(E);
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr);
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Iterate over all sections in the object.
1610b57cec5SDimitry Andric   auto SI = SourceObject.section_begin();
1620b57cec5SDimitry Andric   for (const auto &Sec : Obj->sections()) {
163*8bcb0991SDimitry Andric     Expected<StringRef> NameOrErr = Sec.getName();
164*8bcb0991SDimitry Andric     if (!NameOrErr) {
165*8bcb0991SDimitry Andric       consumeError(NameOrErr.takeError());
166*8bcb0991SDimitry Andric       continue;
167*8bcb0991SDimitry Andric     }
168*8bcb0991SDimitry Andric 
169*8bcb0991SDimitry Andric     if (*NameOrErr != "") {
1700b57cec5SDimitry Andric       DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
1710b57cec5SDimitry Andric       Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
1720b57cec5SDimitry Andric           reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric       if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) {
1750b57cec5SDimitry Andric         // This assumes that the address passed in matches the target address
1760b57cec5SDimitry Andric         // bitness. The template-based type cast handles everything else.
1770b57cec5SDimitry Andric         shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
1780b57cec5SDimitry Andric       }
1790b57cec5SDimitry Andric     }
1800b57cec5SDimitry Andric     ++SI;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   return std::move(Obj);
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric static OwningBinary<ObjectFile>
1870b57cec5SDimitry Andric createELFDebugObject(const ObjectFile &Obj, const LoadedELFObjectInfo &L) {
1880b57cec5SDimitry Andric   assert(Obj.isELF() && "Not an ELF object file.");
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Buffer =
1910b57cec5SDimitry Andric     MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName());
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   Expected<std::unique_ptr<ObjectFile>> DebugObj(nullptr);
1940b57cec5SDimitry Andric   handleAllErrors(DebugObj.takeError());
1950b57cec5SDimitry Andric   if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian())
1960b57cec5SDimitry Andric     DebugObj =
1970b57cec5SDimitry Andric         createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
1980b57cec5SDimitry Andric   else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian())
1990b57cec5SDimitry Andric     DebugObj =
2000b57cec5SDimitry Andric         createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
2010b57cec5SDimitry Andric   else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian())
2020b57cec5SDimitry Andric     DebugObj =
2030b57cec5SDimitry Andric         createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
2040b57cec5SDimitry Andric   else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian())
2050b57cec5SDimitry Andric     DebugObj =
2060b57cec5SDimitry Andric         createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
2070b57cec5SDimitry Andric   else
2080b57cec5SDimitry Andric     llvm_unreachable("Unexpected ELF format");
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   handleAllErrors(DebugObj.takeError());
2110b57cec5SDimitry Andric   return OwningBinary<ObjectFile>(std::move(*DebugObj), std::move(Buffer));
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric OwningBinary<ObjectFile>
2150b57cec5SDimitry Andric LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
2160b57cec5SDimitry Andric   return createELFDebugObject(Obj, *this);
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric } // anonymous namespace
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric namespace llvm {
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
2240b57cec5SDimitry Andric                                JITSymbolResolver &Resolver)
2250b57cec5SDimitry Andric     : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
2260b57cec5SDimitry Andric RuntimeDyldELF::~RuntimeDyldELF() {}
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric void RuntimeDyldELF::registerEHFrames() {
2290b57cec5SDimitry Andric   for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
2300b57cec5SDimitry Andric     SID EHFrameSID = UnregisteredEHFrameSections[i];
2310b57cec5SDimitry Andric     uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
2320b57cec5SDimitry Andric     uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
2330b57cec5SDimitry Andric     size_t EHFrameSize = Sections[EHFrameSID].getSize();
2340b57cec5SDimitry Andric     MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric   UnregisteredEHFrameSections.clear();
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldELF>
2400b57cec5SDimitry Andric llvm::RuntimeDyldELF::create(Triple::ArchType Arch,
2410b57cec5SDimitry Andric                              RuntimeDyld::MemoryManager &MemMgr,
2420b57cec5SDimitry Andric                              JITSymbolResolver &Resolver) {
2430b57cec5SDimitry Andric   switch (Arch) {
2440b57cec5SDimitry Andric   default:
245*8bcb0991SDimitry Andric     return std::make_unique<RuntimeDyldELF>(MemMgr, Resolver);
2460b57cec5SDimitry Andric   case Triple::mips:
2470b57cec5SDimitry Andric   case Triple::mipsel:
2480b57cec5SDimitry Andric   case Triple::mips64:
2490b57cec5SDimitry Andric   case Triple::mips64el:
250*8bcb0991SDimitry Andric     return std::make_unique<RuntimeDyldELFMips>(MemMgr, Resolver);
2510b57cec5SDimitry Andric   }
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
2550b57cec5SDimitry Andric RuntimeDyldELF::loadObject(const object::ObjectFile &O) {
2560b57cec5SDimitry Andric   if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
257*8bcb0991SDimitry Andric     return std::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr);
2580b57cec5SDimitry Andric   else {
2590b57cec5SDimitry Andric     HasError = true;
2600b57cec5SDimitry Andric     raw_string_ostream ErrStream(ErrorStr);
2610b57cec5SDimitry Andric     logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
2620b57cec5SDimitry Andric     return nullptr;
2630b57cec5SDimitry Andric   }
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
2670b57cec5SDimitry Andric                                              uint64_t Offset, uint64_t Value,
2680b57cec5SDimitry Andric                                              uint32_t Type, int64_t Addend,
2690b57cec5SDimitry Andric                                              uint64_t SymOffset) {
2700b57cec5SDimitry Andric   switch (Type) {
2710b57cec5SDimitry Andric   default:
2720b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
2730b57cec5SDimitry Andric     break;
2740b57cec5SDimitry Andric   case ELF::R_X86_64_NONE:
2750b57cec5SDimitry Andric     break;
2760b57cec5SDimitry Andric   case ELF::R_X86_64_64: {
2770b57cec5SDimitry Andric     support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
2780b57cec5SDimitry Andric         Value + Addend;
2790b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
2800b57cec5SDimitry Andric                       << format("%p\n", Section.getAddressWithOffset(Offset)));
2810b57cec5SDimitry Andric     break;
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric   case ELF::R_X86_64_32:
2840b57cec5SDimitry Andric   case ELF::R_X86_64_32S: {
2850b57cec5SDimitry Andric     Value += Addend;
2860b57cec5SDimitry Andric     assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
2870b57cec5SDimitry Andric            (Type == ELF::R_X86_64_32S &&
2880b57cec5SDimitry Andric             ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
2890b57cec5SDimitry Andric     uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
2900b57cec5SDimitry Andric     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
2910b57cec5SDimitry Andric         TruncatedAddr;
2920b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
2930b57cec5SDimitry Andric                       << format("%p\n", Section.getAddressWithOffset(Offset)));
2940b57cec5SDimitry Andric     break;
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric   case ELF::R_X86_64_PC8: {
2970b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
2980b57cec5SDimitry Andric     int64_t RealOffset = Value + Addend - FinalAddress;
2990b57cec5SDimitry Andric     assert(isInt<8>(RealOffset));
3000b57cec5SDimitry Andric     int8_t TruncOffset = (RealOffset & 0xFF);
3010b57cec5SDimitry Andric     Section.getAddress()[Offset] = TruncOffset;
3020b57cec5SDimitry Andric     break;
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric   case ELF::R_X86_64_PC32: {
3050b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
3060b57cec5SDimitry Andric     int64_t RealOffset = Value + Addend - FinalAddress;
3070b57cec5SDimitry Andric     assert(isInt<32>(RealOffset));
3080b57cec5SDimitry Andric     int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
3090b57cec5SDimitry Andric     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
3100b57cec5SDimitry Andric         TruncOffset;
3110b57cec5SDimitry Andric     break;
3120b57cec5SDimitry Andric   }
3130b57cec5SDimitry Andric   case ELF::R_X86_64_PC64: {
3140b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
3150b57cec5SDimitry Andric     int64_t RealOffset = Value + Addend - FinalAddress;
3160b57cec5SDimitry Andric     support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
3170b57cec5SDimitry Andric         RealOffset;
3180b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset) << " at "
3190b57cec5SDimitry Andric                       << format("%p\n", FinalAddress));
3200b57cec5SDimitry Andric     break;
3210b57cec5SDimitry Andric   }
3220b57cec5SDimitry Andric   case ELF::R_X86_64_GOTOFF64: {
3230b57cec5SDimitry Andric     // Compute Value - GOTBase.
3240b57cec5SDimitry Andric     uint64_t GOTBase = 0;
3250b57cec5SDimitry Andric     for (const auto &Section : Sections) {
3260b57cec5SDimitry Andric       if (Section.getName() == ".got") {
3270b57cec5SDimitry Andric         GOTBase = Section.getLoadAddressWithOffset(0);
3280b57cec5SDimitry Andric         break;
3290b57cec5SDimitry Andric       }
3300b57cec5SDimitry Andric     }
3310b57cec5SDimitry Andric     assert(GOTBase != 0 && "missing GOT");
3320b57cec5SDimitry Andric     int64_t GOTOffset = Value - GOTBase + Addend;
3330b57cec5SDimitry Andric     support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = GOTOffset;
3340b57cec5SDimitry Andric     break;
3350b57cec5SDimitry Andric   }
3360b57cec5SDimitry Andric   }
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
3400b57cec5SDimitry Andric                                           uint64_t Offset, uint32_t Value,
3410b57cec5SDimitry Andric                                           uint32_t Type, int32_t Addend) {
3420b57cec5SDimitry Andric   switch (Type) {
3430b57cec5SDimitry Andric   case ELF::R_386_32: {
3440b57cec5SDimitry Andric     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
3450b57cec5SDimitry Andric         Value + Addend;
3460b57cec5SDimitry Andric     break;
3470b57cec5SDimitry Andric   }
3480b57cec5SDimitry Andric   // Handle R_386_PLT32 like R_386_PC32 since it should be able to
3490b57cec5SDimitry Andric   // reach any 32 bit address.
3500b57cec5SDimitry Andric   case ELF::R_386_PLT32:
3510b57cec5SDimitry Andric   case ELF::R_386_PC32: {
3520b57cec5SDimitry Andric     uint32_t FinalAddress =
3530b57cec5SDimitry Andric         Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
3540b57cec5SDimitry Andric     uint32_t RealOffset = Value + Addend - FinalAddress;
3550b57cec5SDimitry Andric     support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
3560b57cec5SDimitry Andric         RealOffset;
3570b57cec5SDimitry Andric     break;
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric   default:
3600b57cec5SDimitry Andric     // There are other relocation types, but it appears these are the
3610b57cec5SDimitry Andric     // only ones currently used by the LLVM ELF object writer
3620b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
3630b57cec5SDimitry Andric     break;
3640b57cec5SDimitry Andric   }
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
3680b57cec5SDimitry Andric                                               uint64_t Offset, uint64_t Value,
3690b57cec5SDimitry Andric                                               uint32_t Type, int64_t Addend) {
3700b57cec5SDimitry Andric   uint32_t *TargetPtr =
3710b57cec5SDimitry Andric       reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
3720b57cec5SDimitry Andric   uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
3730b57cec5SDimitry Andric   // Data should use target endian. Code should always use little endian.
3740b57cec5SDimitry Andric   bool isBE = Arch == Triple::aarch64_be;
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
3770b57cec5SDimitry Andric                     << format("%llx", Section.getAddressWithOffset(Offset))
3780b57cec5SDimitry Andric                     << " FinalAddress: 0x" << format("%llx", FinalAddress)
3790b57cec5SDimitry Andric                     << " Value: 0x" << format("%llx", Value) << " Type: 0x"
3800b57cec5SDimitry Andric                     << format("%x", Type) << " Addend: 0x"
3810b57cec5SDimitry Andric                     << format("%llx", Addend) << "\n");
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   switch (Type) {
3840b57cec5SDimitry Andric   default:
3850b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
3860b57cec5SDimitry Andric     break;
3870b57cec5SDimitry Andric   case ELF::R_AARCH64_ABS16: {
3880b57cec5SDimitry Andric     uint64_t Result = Value + Addend;
3890b57cec5SDimitry Andric     assert(static_cast<int64_t>(Result) >= INT16_MIN && Result < UINT16_MAX);
3900b57cec5SDimitry Andric     write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
3910b57cec5SDimitry Andric     break;
3920b57cec5SDimitry Andric   }
3930b57cec5SDimitry Andric   case ELF::R_AARCH64_ABS32: {
3940b57cec5SDimitry Andric     uint64_t Result = Value + Addend;
3950b57cec5SDimitry Andric     assert(static_cast<int64_t>(Result) >= INT32_MIN && Result < UINT32_MAX);
3960b57cec5SDimitry Andric     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
3970b57cec5SDimitry Andric     break;
3980b57cec5SDimitry Andric   }
3990b57cec5SDimitry Andric   case ELF::R_AARCH64_ABS64:
4000b57cec5SDimitry Andric     write(isBE, TargetPtr, Value + Addend);
4010b57cec5SDimitry Andric     break;
4020b57cec5SDimitry Andric   case ELF::R_AARCH64_PREL32: {
4030b57cec5SDimitry Andric     uint64_t Result = Value + Addend - FinalAddress;
4040b57cec5SDimitry Andric     assert(static_cast<int64_t>(Result) >= INT32_MIN &&
4050b57cec5SDimitry Andric            static_cast<int64_t>(Result) <= UINT32_MAX);
4060b57cec5SDimitry Andric     write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
4070b57cec5SDimitry Andric     break;
4080b57cec5SDimitry Andric   }
4090b57cec5SDimitry Andric   case ELF::R_AARCH64_PREL64:
4100b57cec5SDimitry Andric     write(isBE, TargetPtr, Value + Addend - FinalAddress);
4110b57cec5SDimitry Andric     break;
4120b57cec5SDimitry Andric   case ELF::R_AARCH64_CALL26: // fallthrough
4130b57cec5SDimitry Andric   case ELF::R_AARCH64_JUMP26: {
4140b57cec5SDimitry Andric     // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
4150b57cec5SDimitry Andric     // calculation.
4160b57cec5SDimitry Andric     uint64_t BranchImm = Value + Addend - FinalAddress;
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric     // "Check that -2^27 <= result < 2^27".
4190b57cec5SDimitry Andric     assert(isInt<28>(BranchImm));
4200b57cec5SDimitry Andric     or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
4210b57cec5SDimitry Andric     break;
4220b57cec5SDimitry Andric   }
4230b57cec5SDimitry Andric   case ELF::R_AARCH64_MOVW_UABS_G3:
4240b57cec5SDimitry Andric     or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
4250b57cec5SDimitry Andric     break;
4260b57cec5SDimitry Andric   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
4270b57cec5SDimitry Andric     or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
4280b57cec5SDimitry Andric     break;
4290b57cec5SDimitry Andric   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
4300b57cec5SDimitry Andric     or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
4310b57cec5SDimitry Andric     break;
4320b57cec5SDimitry Andric   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
4330b57cec5SDimitry Andric     or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
4340b57cec5SDimitry Andric     break;
4350b57cec5SDimitry Andric   case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
4360b57cec5SDimitry Andric     // Operation: Page(S+A) - Page(P)
4370b57cec5SDimitry Andric     uint64_t Result =
4380b57cec5SDimitry Andric         ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric     // Check that -2^32 <= X < 2^32
4410b57cec5SDimitry Andric     assert(isInt<33>(Result) && "overflow check failed for relocation");
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric     // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
4440b57cec5SDimitry Andric     // from bits 32:12 of X.
4450b57cec5SDimitry Andric     write32AArch64Addr(TargetPtr, Result >> 12);
4460b57cec5SDimitry Andric     break;
4470b57cec5SDimitry Andric   }
4480b57cec5SDimitry Andric   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
4490b57cec5SDimitry Andric     // Operation: S + A
4500b57cec5SDimitry Andric     // Immediate goes in bits 21:10 of LD/ST instruction, taken
4510b57cec5SDimitry Andric     // from bits 11:0 of X
4520b57cec5SDimitry Andric     or32AArch64Imm(TargetPtr, Value + Addend);
4530b57cec5SDimitry Andric     break;
4540b57cec5SDimitry Andric   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
4550b57cec5SDimitry Andric     // Operation: S + A
4560b57cec5SDimitry Andric     // Immediate goes in bits 21:10 of LD/ST instruction, taken
4570b57cec5SDimitry Andric     // from bits 11:0 of X
4580b57cec5SDimitry Andric     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11));
4590b57cec5SDimitry Andric     break;
4600b57cec5SDimitry Andric   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
4610b57cec5SDimitry Andric     // Operation: S + A
4620b57cec5SDimitry Andric     // Immediate goes in bits 21:10 of LD/ST instruction, taken
4630b57cec5SDimitry Andric     // from bits 11:1 of X
4640b57cec5SDimitry Andric     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11));
4650b57cec5SDimitry Andric     break;
4660b57cec5SDimitry Andric   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
4670b57cec5SDimitry Andric     // Operation: S + A
4680b57cec5SDimitry Andric     // Immediate goes in bits 21:10 of LD/ST instruction, taken
4690b57cec5SDimitry Andric     // from bits 11:2 of X
4700b57cec5SDimitry Andric     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
4710b57cec5SDimitry Andric     break;
4720b57cec5SDimitry Andric   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
4730b57cec5SDimitry Andric     // Operation: S + A
4740b57cec5SDimitry Andric     // Immediate goes in bits 21:10 of LD/ST instruction, taken
4750b57cec5SDimitry Andric     // from bits 11:3 of X
4760b57cec5SDimitry Andric     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
4770b57cec5SDimitry Andric     break;
4780b57cec5SDimitry Andric   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
4790b57cec5SDimitry Andric     // Operation: S + A
4800b57cec5SDimitry Andric     // Immediate goes in bits 21:10 of LD/ST instruction, taken
4810b57cec5SDimitry Andric     // from bits 11:4 of X
4820b57cec5SDimitry Andric     or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11));
4830b57cec5SDimitry Andric     break;
4840b57cec5SDimitry Andric   }
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
4880b57cec5SDimitry Andric                                           uint64_t Offset, uint32_t Value,
4890b57cec5SDimitry Andric                                           uint32_t Type, int32_t Addend) {
4900b57cec5SDimitry Andric   // TODO: Add Thumb relocations.
4910b57cec5SDimitry Andric   uint32_t *TargetPtr =
4920b57cec5SDimitry Andric       reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
4930b57cec5SDimitry Andric   uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
4940b57cec5SDimitry Andric   Value += Addend;
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
4970b57cec5SDimitry Andric                     << Section.getAddressWithOffset(Offset)
4980b57cec5SDimitry Andric                     << " FinalAddress: " << format("%p", FinalAddress)
4990b57cec5SDimitry Andric                     << " Value: " << format("%x", Value)
5000b57cec5SDimitry Andric                     << " Type: " << format("%x", Type)
5010b57cec5SDimitry Andric                     << " Addend: " << format("%x", Addend) << "\n");
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   switch (Type) {
5040b57cec5SDimitry Andric   default:
5050b57cec5SDimitry Andric     llvm_unreachable("Not implemented relocation type!");
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric   case ELF::R_ARM_NONE:
5080b57cec5SDimitry Andric     break;
5090b57cec5SDimitry Andric     // Write a 31bit signed offset
5100b57cec5SDimitry Andric   case ELF::R_ARM_PREL31:
5110b57cec5SDimitry Andric     support::ulittle32_t::ref{TargetPtr} =
5120b57cec5SDimitry Andric         (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
5130b57cec5SDimitry Andric         ((Value - FinalAddress) & ~0x80000000);
5140b57cec5SDimitry Andric     break;
5150b57cec5SDimitry Andric   case ELF::R_ARM_TARGET1:
5160b57cec5SDimitry Andric   case ELF::R_ARM_ABS32:
5170b57cec5SDimitry Andric     support::ulittle32_t::ref{TargetPtr} = Value;
5180b57cec5SDimitry Andric     break;
5190b57cec5SDimitry Andric     // Write first 16 bit of 32 bit value to the mov instruction.
5200b57cec5SDimitry Andric     // Last 4 bit should be shifted.
5210b57cec5SDimitry Andric   case ELF::R_ARM_MOVW_ABS_NC:
5220b57cec5SDimitry Andric   case ELF::R_ARM_MOVT_ABS:
5230b57cec5SDimitry Andric     if (Type == ELF::R_ARM_MOVW_ABS_NC)
5240b57cec5SDimitry Andric       Value = Value & 0xFFFF;
5250b57cec5SDimitry Andric     else if (Type == ELF::R_ARM_MOVT_ABS)
5260b57cec5SDimitry Andric       Value = (Value >> 16) & 0xFFFF;
5270b57cec5SDimitry Andric     support::ulittle32_t::ref{TargetPtr} =
5280b57cec5SDimitry Andric         (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
5290b57cec5SDimitry Andric         (((Value >> 12) & 0xF) << 16);
5300b57cec5SDimitry Andric     break;
5310b57cec5SDimitry Andric     // Write 24 bit relative value to the branch instruction.
5320b57cec5SDimitry Andric   case ELF::R_ARM_PC24: // Fall through.
5330b57cec5SDimitry Andric   case ELF::R_ARM_CALL: // Fall through.
5340b57cec5SDimitry Andric   case ELF::R_ARM_JUMP24:
5350b57cec5SDimitry Andric     int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
5360b57cec5SDimitry Andric     RelValue = (RelValue & 0x03FFFFFC) >> 2;
5370b57cec5SDimitry Andric     assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
5380b57cec5SDimitry Andric     support::ulittle32_t::ref{TargetPtr} =
5390b57cec5SDimitry Andric         (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
5400b57cec5SDimitry Andric     break;
5410b57cec5SDimitry Andric   }
5420b57cec5SDimitry Andric }
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
5450b57cec5SDimitry Andric   if (Arch == Triple::UnknownArch ||
5460b57cec5SDimitry Andric       !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) {
5470b57cec5SDimitry Andric     IsMipsO32ABI = false;
5480b57cec5SDimitry Andric     IsMipsN32ABI = false;
5490b57cec5SDimitry Andric     IsMipsN64ABI = false;
5500b57cec5SDimitry Andric     return;
5510b57cec5SDimitry Andric   }
5520b57cec5SDimitry Andric   if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
5530b57cec5SDimitry Andric     unsigned AbiVariant = E->getPlatformFlags();
5540b57cec5SDimitry Andric     IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
5550b57cec5SDimitry Andric     IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
5560b57cec5SDimitry Andric   }
5570b57cec5SDimitry Andric   IsMipsN64ABI = Obj.getFileFormatName().equals("ELF64-mips");
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric // Return the .TOC. section and offset.
5610b57cec5SDimitry Andric Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
5620b57cec5SDimitry Andric                                           ObjSectionToIDMap &LocalSections,
5630b57cec5SDimitry Andric                                           RelocationValueRef &Rel) {
5640b57cec5SDimitry Andric   // Set a default SectionID in case we do not find a TOC section below.
5650b57cec5SDimitry Andric   // This may happen for references to TOC base base (sym@toc, .odp
5660b57cec5SDimitry Andric   // relocation) without a .toc directive.  In this case just use the
5670b57cec5SDimitry Andric   // first section (which is usually the .odp) since the code won't
5680b57cec5SDimitry Andric   // reference the .toc base directly.
5690b57cec5SDimitry Andric   Rel.SymbolName = nullptr;
5700b57cec5SDimitry Andric   Rel.SectionID = 0;
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   // The TOC consists of sections .got, .toc, .tocbss, .plt in that
5730b57cec5SDimitry Andric   // order. The TOC starts where the first of these sections starts.
5740b57cec5SDimitry Andric   for (auto &Section : Obj.sections()) {
575*8bcb0991SDimitry Andric     Expected<StringRef> NameOrErr = Section.getName();
576*8bcb0991SDimitry Andric     if (!NameOrErr)
577*8bcb0991SDimitry Andric       return NameOrErr.takeError();
578*8bcb0991SDimitry Andric     StringRef SectionName = *NameOrErr;
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric     if (SectionName == ".got"
5810b57cec5SDimitry Andric         || SectionName == ".toc"
5820b57cec5SDimitry Andric         || SectionName == ".tocbss"
5830b57cec5SDimitry Andric         || SectionName == ".plt") {
5840b57cec5SDimitry Andric       if (auto SectionIDOrErr =
5850b57cec5SDimitry Andric             findOrEmitSection(Obj, Section, false, LocalSections))
5860b57cec5SDimitry Andric         Rel.SectionID = *SectionIDOrErr;
5870b57cec5SDimitry Andric       else
5880b57cec5SDimitry Andric         return SectionIDOrErr.takeError();
5890b57cec5SDimitry Andric       break;
5900b57cec5SDimitry Andric     }
5910b57cec5SDimitry Andric   }
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric   // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
5940b57cec5SDimitry Andric   // thus permitting a full 64 Kbytes segment.
5950b57cec5SDimitry Andric   Rel.Addend = 0x8000;
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   return Error::success();
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric // Returns the sections and offset associated with the ODP entry referenced
6010b57cec5SDimitry Andric // by Symbol.
6020b57cec5SDimitry Andric Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
6030b57cec5SDimitry Andric                                           ObjSectionToIDMap &LocalSections,
6040b57cec5SDimitry Andric                                           RelocationValueRef &Rel) {
6050b57cec5SDimitry Andric   // Get the ELF symbol value (st_value) to compare with Relocation offset in
6060b57cec5SDimitry Andric   // .opd entries
6070b57cec5SDimitry Andric   for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
6080b57cec5SDimitry Andric        si != se; ++si) {
609*8bcb0991SDimitry Andric 
610*8bcb0991SDimitry Andric     Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
611*8bcb0991SDimitry Andric     if (!RelSecOrErr)
612*8bcb0991SDimitry Andric       report_fatal_error(toString(RelSecOrErr.takeError()));
613*8bcb0991SDimitry Andric 
614*8bcb0991SDimitry Andric     section_iterator RelSecI = *RelSecOrErr;
6150b57cec5SDimitry Andric     if (RelSecI == Obj.section_end())
6160b57cec5SDimitry Andric       continue;
6170b57cec5SDimitry Andric 
618*8bcb0991SDimitry Andric     Expected<StringRef> NameOrErr = RelSecI->getName();
619*8bcb0991SDimitry Andric     if (!NameOrErr)
620*8bcb0991SDimitry Andric       return NameOrErr.takeError();
621*8bcb0991SDimitry Andric     StringRef RelSectionName = *NameOrErr;
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric     if (RelSectionName != ".opd")
6240b57cec5SDimitry Andric       continue;
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric     for (elf_relocation_iterator i = si->relocation_begin(),
6270b57cec5SDimitry Andric                                  e = si->relocation_end();
6280b57cec5SDimitry Andric          i != e;) {
6290b57cec5SDimitry Andric       // The R_PPC64_ADDR64 relocation indicates the first field
6300b57cec5SDimitry Andric       // of a .opd entry
6310b57cec5SDimitry Andric       uint64_t TypeFunc = i->getType();
6320b57cec5SDimitry Andric       if (TypeFunc != ELF::R_PPC64_ADDR64) {
6330b57cec5SDimitry Andric         ++i;
6340b57cec5SDimitry Andric         continue;
6350b57cec5SDimitry Andric       }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric       uint64_t TargetSymbolOffset = i->getOffset();
6380b57cec5SDimitry Andric       symbol_iterator TargetSymbol = i->getSymbol();
6390b57cec5SDimitry Andric       int64_t Addend;
6400b57cec5SDimitry Andric       if (auto AddendOrErr = i->getAddend())
6410b57cec5SDimitry Andric         Addend = *AddendOrErr;
6420b57cec5SDimitry Andric       else
6430b57cec5SDimitry Andric         return AddendOrErr.takeError();
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric       ++i;
6460b57cec5SDimitry Andric       if (i == e)
6470b57cec5SDimitry Andric         break;
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric       // Just check if following relocation is a R_PPC64_TOC
6500b57cec5SDimitry Andric       uint64_t TypeTOC = i->getType();
6510b57cec5SDimitry Andric       if (TypeTOC != ELF::R_PPC64_TOC)
6520b57cec5SDimitry Andric         continue;
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric       // Finally compares the Symbol value and the target symbol offset
6550b57cec5SDimitry Andric       // to check if this .opd entry refers to the symbol the relocation
6560b57cec5SDimitry Andric       // points to.
6570b57cec5SDimitry Andric       if (Rel.Addend != (int64_t)TargetSymbolOffset)
6580b57cec5SDimitry Andric         continue;
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric       section_iterator TSI = Obj.section_end();
6610b57cec5SDimitry Andric       if (auto TSIOrErr = TargetSymbol->getSection())
6620b57cec5SDimitry Andric         TSI = *TSIOrErr;
6630b57cec5SDimitry Andric       else
6640b57cec5SDimitry Andric         return TSIOrErr.takeError();
6650b57cec5SDimitry Andric       assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric       bool IsCode = TSI->isText();
6680b57cec5SDimitry Andric       if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
6690b57cec5SDimitry Andric                                                   LocalSections))
6700b57cec5SDimitry Andric         Rel.SectionID = *SectionIDOrErr;
6710b57cec5SDimitry Andric       else
6720b57cec5SDimitry Andric         return SectionIDOrErr.takeError();
6730b57cec5SDimitry Andric       Rel.Addend = (intptr_t)Addend;
6740b57cec5SDimitry Andric       return Error::success();
6750b57cec5SDimitry Andric     }
6760b57cec5SDimitry Andric   }
6770b57cec5SDimitry Andric   llvm_unreachable("Attempting to get address of ODP entry!");
6780b57cec5SDimitry Andric }
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric // Relocation masks following the #lo(value), #hi(value), #ha(value),
6810b57cec5SDimitry Andric // #higher(value), #highera(value), #highest(value), and #highesta(value)
6820b57cec5SDimitry Andric // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
6830b57cec5SDimitry Andric // document.
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric static inline uint16_t applyPPChi(uint64_t value) {
6880b57cec5SDimitry Andric   return (value >> 16) & 0xffff;
6890b57cec5SDimitry Andric }
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric static inline uint16_t applyPPCha (uint64_t value) {
6920b57cec5SDimitry Andric   return ((value + 0x8000) >> 16) & 0xffff;
6930b57cec5SDimitry Andric }
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric static inline uint16_t applyPPChigher(uint64_t value) {
6960b57cec5SDimitry Andric   return (value >> 32) & 0xffff;
6970b57cec5SDimitry Andric }
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric static inline uint16_t applyPPChighera (uint64_t value) {
7000b57cec5SDimitry Andric   return ((value + 0x8000) >> 32) & 0xffff;
7010b57cec5SDimitry Andric }
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric static inline uint16_t applyPPChighest(uint64_t value) {
7040b57cec5SDimitry Andric   return (value >> 48) & 0xffff;
7050b57cec5SDimitry Andric }
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric static inline uint16_t applyPPChighesta (uint64_t value) {
7080b57cec5SDimitry Andric   return ((value + 0x8000) >> 48) & 0xffff;
7090b57cec5SDimitry Andric }
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
7120b57cec5SDimitry Andric                                             uint64_t Offset, uint64_t Value,
7130b57cec5SDimitry Andric                                             uint32_t Type, int64_t Addend) {
7140b57cec5SDimitry Andric   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
7150b57cec5SDimitry Andric   switch (Type) {
7160b57cec5SDimitry Andric   default:
7170b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
7180b57cec5SDimitry Andric     break;
7190b57cec5SDimitry Andric   case ELF::R_PPC_ADDR16_LO:
7200b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
7210b57cec5SDimitry Andric     break;
7220b57cec5SDimitry Andric   case ELF::R_PPC_ADDR16_HI:
7230b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
7240b57cec5SDimitry Andric     break;
7250b57cec5SDimitry Andric   case ELF::R_PPC_ADDR16_HA:
7260b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
7270b57cec5SDimitry Andric     break;
7280b57cec5SDimitry Andric   }
7290b57cec5SDimitry Andric }
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
7320b57cec5SDimitry Andric                                             uint64_t Offset, uint64_t Value,
7330b57cec5SDimitry Andric                                             uint32_t Type, int64_t Addend) {
7340b57cec5SDimitry Andric   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
7350b57cec5SDimitry Andric   switch (Type) {
7360b57cec5SDimitry Andric   default:
7370b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
7380b57cec5SDimitry Andric     break;
7390b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16:
7400b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
7410b57cec5SDimitry Andric     break;
7420b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_DS:
7430b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
7440b57cec5SDimitry Andric     break;
7450b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_LO:
7460b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
7470b57cec5SDimitry Andric     break;
7480b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_LO_DS:
7490b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
7500b57cec5SDimitry Andric     break;
7510b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HI:
7520b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HIGH:
7530b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
7540b57cec5SDimitry Andric     break;
7550b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HA:
7560b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HIGHA:
7570b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
7580b57cec5SDimitry Andric     break;
7590b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HIGHER:
7600b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
7610b57cec5SDimitry Andric     break;
7620b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HIGHERA:
7630b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
7640b57cec5SDimitry Andric     break;
7650b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HIGHEST:
7660b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
7670b57cec5SDimitry Andric     break;
7680b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR16_HIGHESTA:
7690b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
7700b57cec5SDimitry Andric     break;
7710b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR14: {
7720b57cec5SDimitry Andric     assert(((Value + Addend) & 3) == 0);
7730b57cec5SDimitry Andric     // Preserve the AA/LK bits in the branch instruction
7740b57cec5SDimitry Andric     uint8_t aalk = *(LocalAddress + 3);
7750b57cec5SDimitry Andric     writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
7760b57cec5SDimitry Andric   } break;
7770b57cec5SDimitry Andric   case ELF::R_PPC64_REL16_LO: {
7780b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
7790b57cec5SDimitry Andric     uint64_t Delta = Value - FinalAddress + Addend;
7800b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPClo(Delta));
7810b57cec5SDimitry Andric   } break;
7820b57cec5SDimitry Andric   case ELF::R_PPC64_REL16_HI: {
7830b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
7840b57cec5SDimitry Andric     uint64_t Delta = Value - FinalAddress + Addend;
7850b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPChi(Delta));
7860b57cec5SDimitry Andric   } break;
7870b57cec5SDimitry Andric   case ELF::R_PPC64_REL16_HA: {
7880b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
7890b57cec5SDimitry Andric     uint64_t Delta = Value - FinalAddress + Addend;
7900b57cec5SDimitry Andric     writeInt16BE(LocalAddress, applyPPCha(Delta));
7910b57cec5SDimitry Andric   } break;
7920b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR32: {
7930b57cec5SDimitry Andric     int64_t Result = static_cast<int64_t>(Value + Addend);
7940b57cec5SDimitry Andric     if (SignExtend64<32>(Result) != Result)
7950b57cec5SDimitry Andric       llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
7960b57cec5SDimitry Andric     writeInt32BE(LocalAddress, Result);
7970b57cec5SDimitry Andric   } break;
7980b57cec5SDimitry Andric   case ELF::R_PPC64_REL24: {
7990b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
8000b57cec5SDimitry Andric     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
8010b57cec5SDimitry Andric     if (SignExtend64<26>(delta) != delta)
8020b57cec5SDimitry Andric       llvm_unreachable("Relocation R_PPC64_REL24 overflow");
8030b57cec5SDimitry Andric     // We preserve bits other than LI field, i.e. PO and AA/LK fields.
8040b57cec5SDimitry Andric     uint32_t Inst = readBytesUnaligned(LocalAddress, 4);
8050b57cec5SDimitry Andric     writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
8060b57cec5SDimitry Andric   } break;
8070b57cec5SDimitry Andric   case ELF::R_PPC64_REL32: {
8080b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
8090b57cec5SDimitry Andric     int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
8100b57cec5SDimitry Andric     if (SignExtend64<32>(delta) != delta)
8110b57cec5SDimitry Andric       llvm_unreachable("Relocation R_PPC64_REL32 overflow");
8120b57cec5SDimitry Andric     writeInt32BE(LocalAddress, delta);
8130b57cec5SDimitry Andric   } break;
8140b57cec5SDimitry Andric   case ELF::R_PPC64_REL64: {
8150b57cec5SDimitry Andric     uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
8160b57cec5SDimitry Andric     uint64_t Delta = Value - FinalAddress + Addend;
8170b57cec5SDimitry Andric     writeInt64BE(LocalAddress, Delta);
8180b57cec5SDimitry Andric   } break;
8190b57cec5SDimitry Andric   case ELF::R_PPC64_ADDR64:
8200b57cec5SDimitry Andric     writeInt64BE(LocalAddress, Value + Addend);
8210b57cec5SDimitry Andric     break;
8220b57cec5SDimitry Andric   }
8230b57cec5SDimitry Andric }
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
8260b57cec5SDimitry Andric                                               uint64_t Offset, uint64_t Value,
8270b57cec5SDimitry Andric                                               uint32_t Type, int64_t Addend) {
8280b57cec5SDimitry Andric   uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
8290b57cec5SDimitry Andric   switch (Type) {
8300b57cec5SDimitry Andric   default:
8310b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
8320b57cec5SDimitry Andric     break;
8330b57cec5SDimitry Andric   case ELF::R_390_PC16DBL:
8340b57cec5SDimitry Andric   case ELF::R_390_PLT16DBL: {
8350b57cec5SDimitry Andric     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
8360b57cec5SDimitry Andric     assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
8370b57cec5SDimitry Andric     writeInt16BE(LocalAddress, Delta / 2);
8380b57cec5SDimitry Andric     break;
8390b57cec5SDimitry Andric   }
8400b57cec5SDimitry Andric   case ELF::R_390_PC32DBL:
8410b57cec5SDimitry Andric   case ELF::R_390_PLT32DBL: {
8420b57cec5SDimitry Andric     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
8430b57cec5SDimitry Andric     assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
8440b57cec5SDimitry Andric     writeInt32BE(LocalAddress, Delta / 2);
8450b57cec5SDimitry Andric     break;
8460b57cec5SDimitry Andric   }
8470b57cec5SDimitry Andric   case ELF::R_390_PC16: {
8480b57cec5SDimitry Andric     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
8490b57cec5SDimitry Andric     assert(int16_t(Delta) == Delta && "R_390_PC16 overflow");
8500b57cec5SDimitry Andric     writeInt16BE(LocalAddress, Delta);
8510b57cec5SDimitry Andric     break;
8520b57cec5SDimitry Andric   }
8530b57cec5SDimitry Andric   case ELF::R_390_PC32: {
8540b57cec5SDimitry Andric     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
8550b57cec5SDimitry Andric     assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
8560b57cec5SDimitry Andric     writeInt32BE(LocalAddress, Delta);
8570b57cec5SDimitry Andric     break;
8580b57cec5SDimitry Andric   }
8590b57cec5SDimitry Andric   case ELF::R_390_PC64: {
8600b57cec5SDimitry Andric     int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
8610b57cec5SDimitry Andric     writeInt64BE(LocalAddress, Delta);
8620b57cec5SDimitry Andric     break;
8630b57cec5SDimitry Andric   }
8640b57cec5SDimitry Andric   case ELF::R_390_8:
8650b57cec5SDimitry Andric     *LocalAddress = (uint8_t)(Value + Addend);
8660b57cec5SDimitry Andric     break;
8670b57cec5SDimitry Andric   case ELF::R_390_16:
8680b57cec5SDimitry Andric     writeInt16BE(LocalAddress, Value + Addend);
8690b57cec5SDimitry Andric     break;
8700b57cec5SDimitry Andric   case ELF::R_390_32:
8710b57cec5SDimitry Andric     writeInt32BE(LocalAddress, Value + Addend);
8720b57cec5SDimitry Andric     break;
8730b57cec5SDimitry Andric   case ELF::R_390_64:
8740b57cec5SDimitry Andric     writeInt64BE(LocalAddress, Value + Addend);
8750b57cec5SDimitry Andric     break;
8760b57cec5SDimitry Andric   }
8770b57cec5SDimitry Andric }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section,
8800b57cec5SDimitry Andric                                           uint64_t Offset, uint64_t Value,
8810b57cec5SDimitry Andric                                           uint32_t Type, int64_t Addend) {
8820b57cec5SDimitry Andric   bool isBE = Arch == Triple::bpfeb;
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   switch (Type) {
8850b57cec5SDimitry Andric   default:
8860b57cec5SDimitry Andric     llvm_unreachable("Relocation type not implemented yet!");
8870b57cec5SDimitry Andric     break;
8880b57cec5SDimitry Andric   case ELF::R_BPF_NONE:
8890b57cec5SDimitry Andric     break;
8900b57cec5SDimitry Andric   case ELF::R_BPF_64_64: {
8910b57cec5SDimitry Andric     write(isBE, Section.getAddressWithOffset(Offset), Value + Addend);
8920b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
8930b57cec5SDimitry Andric                       << format("%p\n", Section.getAddressWithOffset(Offset)));
8940b57cec5SDimitry Andric     break;
8950b57cec5SDimitry Andric   }
8960b57cec5SDimitry Andric   case ELF::R_BPF_64_32: {
8970b57cec5SDimitry Andric     Value += Addend;
8980b57cec5SDimitry Andric     assert(Value <= UINT32_MAX);
8990b57cec5SDimitry Andric     write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value));
9000b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at "
9010b57cec5SDimitry Andric                       << format("%p\n", Section.getAddressWithOffset(Offset)));
9020b57cec5SDimitry Andric     break;
9030b57cec5SDimitry Andric   }
9040b57cec5SDimitry Andric   }
9050b57cec5SDimitry Andric }
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric // The target location for the relocation is described by RE.SectionID and
9080b57cec5SDimitry Andric // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
9090b57cec5SDimitry Andric // SectionEntry has three members describing its location.
9100b57cec5SDimitry Andric // SectionEntry::Address is the address at which the section has been loaded
9110b57cec5SDimitry Andric // into memory in the current (host) process.  SectionEntry::LoadAddress is the
9120b57cec5SDimitry Andric // address that the section will have in the target process.
9130b57cec5SDimitry Andric // SectionEntry::ObjAddress is the address of the bits for this section in the
9140b57cec5SDimitry Andric // original emitted object image (also in the current address space).
9150b57cec5SDimitry Andric //
9160b57cec5SDimitry Andric // Relocations will be applied as if the section were loaded at
9170b57cec5SDimitry Andric // SectionEntry::LoadAddress, but they will be applied at an address based
9180b57cec5SDimitry Andric // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer to
9190b57cec5SDimitry Andric // Target memory contents if they are required for value calculations.
9200b57cec5SDimitry Andric //
9210b57cec5SDimitry Andric // The Value parameter here is the load address of the symbol for the
9220b57cec5SDimitry Andric // relocation to be applied.  For relocations which refer to symbols in the
9230b57cec5SDimitry Andric // current object Value will be the LoadAddress of the section in which
9240b57cec5SDimitry Andric // the symbol resides (RE.Addend provides additional information about the
9250b57cec5SDimitry Andric // symbol location).  For external symbols, Value will be the address of the
9260b57cec5SDimitry Andric // symbol in the target address space.
9270b57cec5SDimitry Andric void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
9280b57cec5SDimitry Andric                                        uint64_t Value) {
9290b57cec5SDimitry Andric   const SectionEntry &Section = Sections[RE.SectionID];
9300b57cec5SDimitry Andric   return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
9310b57cec5SDimitry Andric                            RE.SymOffset, RE.SectionID);
9320b57cec5SDimitry Andric }
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
9350b57cec5SDimitry Andric                                        uint64_t Offset, uint64_t Value,
9360b57cec5SDimitry Andric                                        uint32_t Type, int64_t Addend,
9370b57cec5SDimitry Andric                                        uint64_t SymOffset, SID SectionID) {
9380b57cec5SDimitry Andric   switch (Arch) {
9390b57cec5SDimitry Andric   case Triple::x86_64:
9400b57cec5SDimitry Andric     resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
9410b57cec5SDimitry Andric     break;
9420b57cec5SDimitry Andric   case Triple::x86:
9430b57cec5SDimitry Andric     resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
9440b57cec5SDimitry Andric                          (uint32_t)(Addend & 0xffffffffL));
9450b57cec5SDimitry Andric     break;
9460b57cec5SDimitry Andric   case Triple::aarch64:
9470b57cec5SDimitry Andric   case Triple::aarch64_be:
9480b57cec5SDimitry Andric     resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
9490b57cec5SDimitry Andric     break;
9500b57cec5SDimitry Andric   case Triple::arm: // Fall through.
9510b57cec5SDimitry Andric   case Triple::armeb:
9520b57cec5SDimitry Andric   case Triple::thumb:
9530b57cec5SDimitry Andric   case Triple::thumbeb:
9540b57cec5SDimitry Andric     resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
9550b57cec5SDimitry Andric                          (uint32_t)(Addend & 0xffffffffL));
9560b57cec5SDimitry Andric     break;
9570b57cec5SDimitry Andric   case Triple::ppc:
9580b57cec5SDimitry Andric     resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
9590b57cec5SDimitry Andric     break;
9600b57cec5SDimitry Andric   case Triple::ppc64: // Fall through.
9610b57cec5SDimitry Andric   case Triple::ppc64le:
9620b57cec5SDimitry Andric     resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
9630b57cec5SDimitry Andric     break;
9640b57cec5SDimitry Andric   case Triple::systemz:
9650b57cec5SDimitry Andric     resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
9660b57cec5SDimitry Andric     break;
9670b57cec5SDimitry Andric   case Triple::bpfel:
9680b57cec5SDimitry Andric   case Triple::bpfeb:
9690b57cec5SDimitry Andric     resolveBPFRelocation(Section, Offset, Value, Type, Addend);
9700b57cec5SDimitry Andric     break;
9710b57cec5SDimitry Andric   default:
9720b57cec5SDimitry Andric     llvm_unreachable("Unsupported CPU type!");
9730b57cec5SDimitry Andric   }
9740b57cec5SDimitry Andric }
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
9770b57cec5SDimitry Andric   return (void *)(Sections[SectionID].getObjAddress() + Offset);
9780b57cec5SDimitry Andric }
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
9810b57cec5SDimitry Andric   RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
9820b57cec5SDimitry Andric   if (Value.SymbolName)
9830b57cec5SDimitry Andric     addRelocationForSymbol(RE, Value.SymbolName);
9840b57cec5SDimitry Andric   else
9850b57cec5SDimitry Andric     addRelocationForSection(RE, Value.SectionID);
9860b57cec5SDimitry Andric }
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
9890b57cec5SDimitry Andric                                                  bool IsLocal) const {
9900b57cec5SDimitry Andric   switch (RelType) {
9910b57cec5SDimitry Andric   case ELF::R_MICROMIPS_GOT16:
9920b57cec5SDimitry Andric     if (IsLocal)
9930b57cec5SDimitry Andric       return ELF::R_MICROMIPS_LO16;
9940b57cec5SDimitry Andric     break;
9950b57cec5SDimitry Andric   case ELF::R_MICROMIPS_HI16:
9960b57cec5SDimitry Andric     return ELF::R_MICROMIPS_LO16;
9970b57cec5SDimitry Andric   case ELF::R_MIPS_GOT16:
9980b57cec5SDimitry Andric     if (IsLocal)
9990b57cec5SDimitry Andric       return ELF::R_MIPS_LO16;
10000b57cec5SDimitry Andric     break;
10010b57cec5SDimitry Andric   case ELF::R_MIPS_HI16:
10020b57cec5SDimitry Andric     return ELF::R_MIPS_LO16;
10030b57cec5SDimitry Andric   case ELF::R_MIPS_PCHI16:
10040b57cec5SDimitry Andric     return ELF::R_MIPS_PCLO16;
10050b57cec5SDimitry Andric   default:
10060b57cec5SDimitry Andric     break;
10070b57cec5SDimitry Andric   }
10080b57cec5SDimitry Andric   return ELF::R_MIPS_NONE;
10090b57cec5SDimitry Andric }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric // Sometimes we don't need to create thunk for a branch.
10120b57cec5SDimitry Andric // This typically happens when branch target is located
10130b57cec5SDimitry Andric // in the same object file. In such case target is either
10140b57cec5SDimitry Andric // a weak symbol or symbol in a different executable section.
10150b57cec5SDimitry Andric // This function checks if branch target is located in the
10160b57cec5SDimitry Andric // same object file and if distance between source and target
10170b57cec5SDimitry Andric // fits R_AARCH64_CALL26 relocation. If both conditions are
10180b57cec5SDimitry Andric // met, it emits direct jump to the target and returns true.
10190b57cec5SDimitry Andric // Otherwise false is returned and thunk is created.
10200b57cec5SDimitry Andric bool RuntimeDyldELF::resolveAArch64ShortBranch(
10210b57cec5SDimitry Andric     unsigned SectionID, relocation_iterator RelI,
10220b57cec5SDimitry Andric     const RelocationValueRef &Value) {
10230b57cec5SDimitry Andric   uint64_t Address;
10240b57cec5SDimitry Andric   if (Value.SymbolName) {
10250b57cec5SDimitry Andric     auto Loc = GlobalSymbolTable.find(Value.SymbolName);
10260b57cec5SDimitry Andric 
10270b57cec5SDimitry Andric     // Don't create direct branch for external symbols.
10280b57cec5SDimitry Andric     if (Loc == GlobalSymbolTable.end())
10290b57cec5SDimitry Andric       return false;
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric     const auto &SymInfo = Loc->second;
10320b57cec5SDimitry Andric     Address =
10330b57cec5SDimitry Andric         uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
10340b57cec5SDimitry Andric             SymInfo.getOffset()));
10350b57cec5SDimitry Andric   } else {
10360b57cec5SDimitry Andric     Address = uint64_t(Sections[Value.SectionID].getLoadAddress());
10370b57cec5SDimitry Andric   }
10380b57cec5SDimitry Andric   uint64_t Offset = RelI->getOffset();
10390b57cec5SDimitry Andric   uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric   // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
10420b57cec5SDimitry Andric   // If distance between source and target is out of range then we should
10430b57cec5SDimitry Andric   // create thunk.
10440b57cec5SDimitry Andric   if (!isInt<28>(Address + Value.Addend - SourceAddress))
10450b57cec5SDimitry Andric     return false;
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
10480b57cec5SDimitry Andric                     Value.Addend);
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric   return true;
10510b57cec5SDimitry Andric }
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
10540b57cec5SDimitry Andric                                           const RelocationValueRef &Value,
10550b57cec5SDimitry Andric                                           relocation_iterator RelI,
10560b57cec5SDimitry Andric                                           StubMap &Stubs) {
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
10590b57cec5SDimitry Andric   SectionEntry &Section = Sections[SectionID];
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric   uint64_t Offset = RelI->getOffset();
10620b57cec5SDimitry Andric   unsigned RelType = RelI->getType();
10630b57cec5SDimitry Andric   // Look for an existing stub.
10640b57cec5SDimitry Andric   StubMap::const_iterator i = Stubs.find(Value);
10650b57cec5SDimitry Andric   if (i != Stubs.end()) {
10660b57cec5SDimitry Andric     resolveRelocation(Section, Offset,
10670b57cec5SDimitry Andric                       (uint64_t)Section.getAddressWithOffset(i->second),
10680b57cec5SDimitry Andric                       RelType, 0);
10690b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " Stub function found\n");
10700b57cec5SDimitry Andric   } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
10710b57cec5SDimitry Andric     // Create a new stub function.
10720b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " Create a new stub function\n");
10730b57cec5SDimitry Andric     Stubs[Value] = Section.getStubOffset();
10740b57cec5SDimitry Andric     uint8_t *StubTargetAddr = createStubFunction(
10750b57cec5SDimitry Andric         Section.getAddressWithOffset(Section.getStubOffset()));
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric     RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(),
10780b57cec5SDimitry Andric                               ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
10790b57cec5SDimitry Andric     RelocationEntry REmovk_g2(SectionID,
10800b57cec5SDimitry Andric                               StubTargetAddr - Section.getAddress() + 4,
10810b57cec5SDimitry Andric                               ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
10820b57cec5SDimitry Andric     RelocationEntry REmovk_g1(SectionID,
10830b57cec5SDimitry Andric                               StubTargetAddr - Section.getAddress() + 8,
10840b57cec5SDimitry Andric                               ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
10850b57cec5SDimitry Andric     RelocationEntry REmovk_g0(SectionID,
10860b57cec5SDimitry Andric                               StubTargetAddr - Section.getAddress() + 12,
10870b57cec5SDimitry Andric                               ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric     if (Value.SymbolName) {
10900b57cec5SDimitry Andric       addRelocationForSymbol(REmovz_g3, Value.SymbolName);
10910b57cec5SDimitry Andric       addRelocationForSymbol(REmovk_g2, Value.SymbolName);
10920b57cec5SDimitry Andric       addRelocationForSymbol(REmovk_g1, Value.SymbolName);
10930b57cec5SDimitry Andric       addRelocationForSymbol(REmovk_g0, Value.SymbolName);
10940b57cec5SDimitry Andric     } else {
10950b57cec5SDimitry Andric       addRelocationForSection(REmovz_g3, Value.SectionID);
10960b57cec5SDimitry Andric       addRelocationForSection(REmovk_g2, Value.SectionID);
10970b57cec5SDimitry Andric       addRelocationForSection(REmovk_g1, Value.SectionID);
10980b57cec5SDimitry Andric       addRelocationForSection(REmovk_g0, Value.SectionID);
10990b57cec5SDimitry Andric     }
11000b57cec5SDimitry Andric     resolveRelocation(Section, Offset,
11010b57cec5SDimitry Andric                       reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
11020b57cec5SDimitry Andric                           Section.getStubOffset())),
11030b57cec5SDimitry Andric                       RelType, 0);
11040b57cec5SDimitry Andric     Section.advanceStubOffset(getMaxStubSize());
11050b57cec5SDimitry Andric   }
11060b57cec5SDimitry Andric }
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric Expected<relocation_iterator>
11090b57cec5SDimitry Andric RuntimeDyldELF::processRelocationRef(
11100b57cec5SDimitry Andric     unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
11110b57cec5SDimitry Andric     ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
11120b57cec5SDimitry Andric   const auto &Obj = cast<ELFObjectFileBase>(O);
11130b57cec5SDimitry Andric   uint64_t RelType = RelI->getType();
11140b57cec5SDimitry Andric   int64_t Addend = 0;
11150b57cec5SDimitry Andric   if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend())
11160b57cec5SDimitry Andric     Addend = *AddendOrErr;
11170b57cec5SDimitry Andric   else
11180b57cec5SDimitry Andric     consumeError(AddendOrErr.takeError());
11190b57cec5SDimitry Andric   elf_symbol_iterator Symbol = RelI->getSymbol();
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric   // Obtain the symbol name which is referenced in the relocation
11220b57cec5SDimitry Andric   StringRef TargetName;
11230b57cec5SDimitry Andric   if (Symbol != Obj.symbol_end()) {
11240b57cec5SDimitry Andric     if (auto TargetNameOrErr = Symbol->getName())
11250b57cec5SDimitry Andric       TargetName = *TargetNameOrErr;
11260b57cec5SDimitry Andric     else
11270b57cec5SDimitry Andric       return TargetNameOrErr.takeError();
11280b57cec5SDimitry Andric   }
11290b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
11300b57cec5SDimitry Andric                     << " TargetName: " << TargetName << "\n");
11310b57cec5SDimitry Andric   RelocationValueRef Value;
11320b57cec5SDimitry Andric   // First search for the symbol in the local symbol table
11330b57cec5SDimitry Andric   SymbolRef::Type SymType = SymbolRef::ST_Unknown;
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric   // Search for the symbol in the global symbol table
11360b57cec5SDimitry Andric   RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end();
11370b57cec5SDimitry Andric   if (Symbol != Obj.symbol_end()) {
11380b57cec5SDimitry Andric     gsi = GlobalSymbolTable.find(TargetName.data());
11390b57cec5SDimitry Andric     Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
11400b57cec5SDimitry Andric     if (!SymTypeOrErr) {
11410b57cec5SDimitry Andric       std::string Buf;
11420b57cec5SDimitry Andric       raw_string_ostream OS(Buf);
11430b57cec5SDimitry Andric       logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
11440b57cec5SDimitry Andric       OS.flush();
11450b57cec5SDimitry Andric       report_fatal_error(Buf);
11460b57cec5SDimitry Andric     }
11470b57cec5SDimitry Andric     SymType = *SymTypeOrErr;
11480b57cec5SDimitry Andric   }
11490b57cec5SDimitry Andric   if (gsi != GlobalSymbolTable.end()) {
11500b57cec5SDimitry Andric     const auto &SymInfo = gsi->second;
11510b57cec5SDimitry Andric     Value.SectionID = SymInfo.getSectionID();
11520b57cec5SDimitry Andric     Value.Offset = SymInfo.getOffset();
11530b57cec5SDimitry Andric     Value.Addend = SymInfo.getOffset() + Addend;
11540b57cec5SDimitry Andric   } else {
11550b57cec5SDimitry Andric     switch (SymType) {
11560b57cec5SDimitry Andric     case SymbolRef::ST_Debug: {
11570b57cec5SDimitry Andric       // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
11580b57cec5SDimitry Andric       // and can be changed by another developers. Maybe best way is add
11590b57cec5SDimitry Andric       // a new symbol type ST_Section to SymbolRef and use it.
11600b57cec5SDimitry Andric       auto SectionOrErr = Symbol->getSection();
11610b57cec5SDimitry Andric       if (!SectionOrErr) {
11620b57cec5SDimitry Andric         std::string Buf;
11630b57cec5SDimitry Andric         raw_string_ostream OS(Buf);
11640b57cec5SDimitry Andric         logAllUnhandledErrors(SectionOrErr.takeError(), OS);
11650b57cec5SDimitry Andric         OS.flush();
11660b57cec5SDimitry Andric         report_fatal_error(Buf);
11670b57cec5SDimitry Andric       }
11680b57cec5SDimitry Andric       section_iterator si = *SectionOrErr;
11690b57cec5SDimitry Andric       if (si == Obj.section_end())
11700b57cec5SDimitry Andric         llvm_unreachable("Symbol section not found, bad object file format!");
11710b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n");
11720b57cec5SDimitry Andric       bool isCode = si->isText();
11730b57cec5SDimitry Andric       if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode,
11740b57cec5SDimitry Andric                                                   ObjSectionToID))
11750b57cec5SDimitry Andric         Value.SectionID = *SectionIDOrErr;
11760b57cec5SDimitry Andric       else
11770b57cec5SDimitry Andric         return SectionIDOrErr.takeError();
11780b57cec5SDimitry Andric       Value.Addend = Addend;
11790b57cec5SDimitry Andric       break;
11800b57cec5SDimitry Andric     }
11810b57cec5SDimitry Andric     case SymbolRef::ST_Data:
11820b57cec5SDimitry Andric     case SymbolRef::ST_Function:
11830b57cec5SDimitry Andric     case SymbolRef::ST_Unknown: {
11840b57cec5SDimitry Andric       Value.SymbolName = TargetName.data();
11850b57cec5SDimitry Andric       Value.Addend = Addend;
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric       // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
11880b57cec5SDimitry Andric       // will manifest here as a NULL symbol name.
11890b57cec5SDimitry Andric       // We can set this as a valid (but empty) symbol name, and rely
11900b57cec5SDimitry Andric       // on addRelocationForSymbol to handle this.
11910b57cec5SDimitry Andric       if (!Value.SymbolName)
11920b57cec5SDimitry Andric         Value.SymbolName = "";
11930b57cec5SDimitry Andric       break;
11940b57cec5SDimitry Andric     }
11950b57cec5SDimitry Andric     default:
11960b57cec5SDimitry Andric       llvm_unreachable("Unresolved symbol type!");
11970b57cec5SDimitry Andric       break;
11980b57cec5SDimitry Andric     }
11990b57cec5SDimitry Andric   }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric   uint64_t Offset = RelI->getOffset();
12020b57cec5SDimitry Andric 
12030b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
12040b57cec5SDimitry Andric                     << "\n");
12050b57cec5SDimitry Andric   if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be)) {
12060b57cec5SDimitry Andric     if (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26) {
12070b57cec5SDimitry Andric       resolveAArch64Branch(SectionID, Value, RelI, Stubs);
12080b57cec5SDimitry Andric     } else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
12090b57cec5SDimitry Andric       // Craete new GOT entry or find existing one. If GOT entry is
12100b57cec5SDimitry Andric       // to be created, then we also emit ABS64 relocation for it.
12110b57cec5SDimitry Andric       uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
12120b57cec5SDimitry Andric       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
12130b57cec5SDimitry Andric                                  ELF::R_AARCH64_ADR_PREL_PG_HI21);
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric     } else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
12160b57cec5SDimitry Andric       uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
12170b57cec5SDimitry Andric       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
12180b57cec5SDimitry Andric                                  ELF::R_AARCH64_LDST64_ABS_LO12_NC);
12190b57cec5SDimitry Andric     } else {
12200b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
12210b57cec5SDimitry Andric     }
12220b57cec5SDimitry Andric   } else if (Arch == Triple::arm) {
12230b57cec5SDimitry Andric     if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
12240b57cec5SDimitry Andric       RelType == ELF::R_ARM_JUMP24) {
12250b57cec5SDimitry Andric       // This is an ARM branch relocation, need to use a stub function.
12260b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
12270b57cec5SDimitry Andric       SectionEntry &Section = Sections[SectionID];
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric       // Look for an existing stub.
12300b57cec5SDimitry Andric       StubMap::const_iterator i = Stubs.find(Value);
12310b57cec5SDimitry Andric       if (i != Stubs.end()) {
12320b57cec5SDimitry Andric         resolveRelocation(
12330b57cec5SDimitry Andric             Section, Offset,
12340b57cec5SDimitry Andric             reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
12350b57cec5SDimitry Andric             RelType, 0);
12360b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " Stub function found\n");
12370b57cec5SDimitry Andric       } else {
12380b57cec5SDimitry Andric         // Create a new stub function.
12390b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
12400b57cec5SDimitry Andric         Stubs[Value] = Section.getStubOffset();
12410b57cec5SDimitry Andric         uint8_t *StubTargetAddr = createStubFunction(
12420b57cec5SDimitry Andric             Section.getAddressWithOffset(Section.getStubOffset()));
12430b57cec5SDimitry Andric         RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
12440b57cec5SDimitry Andric                            ELF::R_ARM_ABS32, Value.Addend);
12450b57cec5SDimitry Andric         if (Value.SymbolName)
12460b57cec5SDimitry Andric           addRelocationForSymbol(RE, Value.SymbolName);
12470b57cec5SDimitry Andric         else
12480b57cec5SDimitry Andric           addRelocationForSection(RE, Value.SectionID);
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric         resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
12510b57cec5SDimitry Andric                                                Section.getAddressWithOffset(
12520b57cec5SDimitry Andric                                                    Section.getStubOffset())),
12530b57cec5SDimitry Andric                           RelType, 0);
12540b57cec5SDimitry Andric         Section.advanceStubOffset(getMaxStubSize());
12550b57cec5SDimitry Andric       }
12560b57cec5SDimitry Andric     } else {
12570b57cec5SDimitry Andric       uint32_t *Placeholder =
12580b57cec5SDimitry Andric         reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
12590b57cec5SDimitry Andric       if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
12600b57cec5SDimitry Andric           RelType == ELF::R_ARM_ABS32) {
12610b57cec5SDimitry Andric         Value.Addend += *Placeholder;
12620b57cec5SDimitry Andric       } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
12630b57cec5SDimitry Andric         // See ELF for ARM documentation
12640b57cec5SDimitry Andric         Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
12650b57cec5SDimitry Andric       }
12660b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
12670b57cec5SDimitry Andric     }
12680b57cec5SDimitry Andric   } else if (IsMipsO32ABI) {
12690b57cec5SDimitry Andric     uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
12700b57cec5SDimitry Andric         computePlaceholderAddress(SectionID, Offset));
12710b57cec5SDimitry Andric     uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
12720b57cec5SDimitry Andric     if (RelType == ELF::R_MIPS_26) {
12730b57cec5SDimitry Andric       // This is an Mips branch relocation, need to use a stub function.
12740b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
12750b57cec5SDimitry Andric       SectionEntry &Section = Sections[SectionID];
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric       // Extract the addend from the instruction.
12780b57cec5SDimitry Andric       // We shift up by two since the Value will be down shifted again
12790b57cec5SDimitry Andric       // when applying the relocation.
12800b57cec5SDimitry Andric       uint32_t Addend = (Opcode & 0x03ffffff) << 2;
12810b57cec5SDimitry Andric 
12820b57cec5SDimitry Andric       Value.Addend += Addend;
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric       //  Look up for existing stub.
12850b57cec5SDimitry Andric       StubMap::const_iterator i = Stubs.find(Value);
12860b57cec5SDimitry Andric       if (i != Stubs.end()) {
12870b57cec5SDimitry Andric         RelocationEntry RE(SectionID, Offset, RelType, i->second);
12880b57cec5SDimitry Andric         addRelocationForSection(RE, SectionID);
12890b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " Stub function found\n");
12900b57cec5SDimitry Andric       } else {
12910b57cec5SDimitry Andric         // Create a new stub function.
12920b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
12930b57cec5SDimitry Andric         Stubs[Value] = Section.getStubOffset();
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric         unsigned AbiVariant = Obj.getPlatformFlags();
12960b57cec5SDimitry Andric 
12970b57cec5SDimitry Andric         uint8_t *StubTargetAddr = createStubFunction(
12980b57cec5SDimitry Andric             Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric         // Creating Hi and Lo relocations for the filled stub instructions.
13010b57cec5SDimitry Andric         RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
13020b57cec5SDimitry Andric                              ELF::R_MIPS_HI16, Value.Addend);
13030b57cec5SDimitry Andric         RelocationEntry RELo(SectionID,
13040b57cec5SDimitry Andric                              StubTargetAddr - Section.getAddress() + 4,
13050b57cec5SDimitry Andric                              ELF::R_MIPS_LO16, Value.Addend);
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric         if (Value.SymbolName) {
13080b57cec5SDimitry Andric           addRelocationForSymbol(REHi, Value.SymbolName);
13090b57cec5SDimitry Andric           addRelocationForSymbol(RELo, Value.SymbolName);
13100b57cec5SDimitry Andric         } else {
13110b57cec5SDimitry Andric           addRelocationForSection(REHi, Value.SectionID);
13120b57cec5SDimitry Andric           addRelocationForSection(RELo, Value.SectionID);
13130b57cec5SDimitry Andric         }
13140b57cec5SDimitry Andric 
13150b57cec5SDimitry Andric         RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
13160b57cec5SDimitry Andric         addRelocationForSection(RE, SectionID);
13170b57cec5SDimitry Andric         Section.advanceStubOffset(getMaxStubSize());
13180b57cec5SDimitry Andric       }
13190b57cec5SDimitry Andric     } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
13200b57cec5SDimitry Andric       int64_t Addend = (Opcode & 0x0000ffff) << 16;
13210b57cec5SDimitry Andric       RelocationEntry RE(SectionID, Offset, RelType, Addend);
13220b57cec5SDimitry Andric       PendingRelocs.push_back(std::make_pair(Value, RE));
13230b57cec5SDimitry Andric     } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
13240b57cec5SDimitry Andric       int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
13250b57cec5SDimitry Andric       for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) {
13260b57cec5SDimitry Andric         const RelocationValueRef &MatchingValue = I->first;
13270b57cec5SDimitry Andric         RelocationEntry &Reloc = I->second;
13280b57cec5SDimitry Andric         if (MatchingValue == Value &&
13290b57cec5SDimitry Andric             RelType == getMatchingLoRelocation(Reloc.RelType) &&
13300b57cec5SDimitry Andric             SectionID == Reloc.SectionID) {
13310b57cec5SDimitry Andric           Reloc.Addend += Addend;
13320b57cec5SDimitry Andric           if (Value.SymbolName)
13330b57cec5SDimitry Andric             addRelocationForSymbol(Reloc, Value.SymbolName);
13340b57cec5SDimitry Andric           else
13350b57cec5SDimitry Andric             addRelocationForSection(Reloc, Value.SectionID);
13360b57cec5SDimitry Andric           I = PendingRelocs.erase(I);
13370b57cec5SDimitry Andric         } else
13380b57cec5SDimitry Andric           ++I;
13390b57cec5SDimitry Andric       }
13400b57cec5SDimitry Andric       RelocationEntry RE(SectionID, Offset, RelType, Addend);
13410b57cec5SDimitry Andric       if (Value.SymbolName)
13420b57cec5SDimitry Andric         addRelocationForSymbol(RE, Value.SymbolName);
13430b57cec5SDimitry Andric       else
13440b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
13450b57cec5SDimitry Andric     } else {
13460b57cec5SDimitry Andric       if (RelType == ELF::R_MIPS_32)
13470b57cec5SDimitry Andric         Value.Addend += Opcode;
13480b57cec5SDimitry Andric       else if (RelType == ELF::R_MIPS_PC16)
13490b57cec5SDimitry Andric         Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
13500b57cec5SDimitry Andric       else if (RelType == ELF::R_MIPS_PC19_S2)
13510b57cec5SDimitry Andric         Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
13520b57cec5SDimitry Andric       else if (RelType == ELF::R_MIPS_PC21_S2)
13530b57cec5SDimitry Andric         Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
13540b57cec5SDimitry Andric       else if (RelType == ELF::R_MIPS_PC26_S2)
13550b57cec5SDimitry Andric         Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
13560b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
13570b57cec5SDimitry Andric     }
13580b57cec5SDimitry Andric   } else if (IsMipsN32ABI || IsMipsN64ABI) {
13590b57cec5SDimitry Andric     uint32_t r_type = RelType & 0xff;
13600b57cec5SDimitry Andric     RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
13610b57cec5SDimitry Andric     if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
13620b57cec5SDimitry Andric         || r_type == ELF::R_MIPS_GOT_DISP) {
13630b57cec5SDimitry Andric       StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName);
13640b57cec5SDimitry Andric       if (i != GOTSymbolOffsets.end())
13650b57cec5SDimitry Andric         RE.SymOffset = i->second;
13660b57cec5SDimitry Andric       else {
13670b57cec5SDimitry Andric         RE.SymOffset = allocateGOTEntries(1);
13680b57cec5SDimitry Andric         GOTSymbolOffsets[TargetName] = RE.SymOffset;
13690b57cec5SDimitry Andric       }
13700b57cec5SDimitry Andric       if (Value.SymbolName)
13710b57cec5SDimitry Andric         addRelocationForSymbol(RE, Value.SymbolName);
13720b57cec5SDimitry Andric       else
13730b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
13740b57cec5SDimitry Andric     } else if (RelType == ELF::R_MIPS_26) {
13750b57cec5SDimitry Andric       // This is an Mips branch relocation, need to use a stub function.
13760b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
13770b57cec5SDimitry Andric       SectionEntry &Section = Sections[SectionID];
13780b57cec5SDimitry Andric 
13790b57cec5SDimitry Andric       //  Look up for existing stub.
13800b57cec5SDimitry Andric       StubMap::const_iterator i = Stubs.find(Value);
13810b57cec5SDimitry Andric       if (i != Stubs.end()) {
13820b57cec5SDimitry Andric         RelocationEntry RE(SectionID, Offset, RelType, i->second);
13830b57cec5SDimitry Andric         addRelocationForSection(RE, SectionID);
13840b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " Stub function found\n");
13850b57cec5SDimitry Andric       } else {
13860b57cec5SDimitry Andric         // Create a new stub function.
13870b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " Create a new stub function\n");
13880b57cec5SDimitry Andric         Stubs[Value] = Section.getStubOffset();
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric         unsigned AbiVariant = Obj.getPlatformFlags();
13910b57cec5SDimitry Andric 
13920b57cec5SDimitry Andric         uint8_t *StubTargetAddr = createStubFunction(
13930b57cec5SDimitry Andric             Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric         if (IsMipsN32ABI) {
13960b57cec5SDimitry Andric           // Creating Hi and Lo relocations for the filled stub instructions.
13970b57cec5SDimitry Andric           RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
13980b57cec5SDimitry Andric                                ELF::R_MIPS_HI16, Value.Addend);
13990b57cec5SDimitry Andric           RelocationEntry RELo(SectionID,
14000b57cec5SDimitry Andric                                StubTargetAddr - Section.getAddress() + 4,
14010b57cec5SDimitry Andric                                ELF::R_MIPS_LO16, Value.Addend);
14020b57cec5SDimitry Andric           if (Value.SymbolName) {
14030b57cec5SDimitry Andric             addRelocationForSymbol(REHi, Value.SymbolName);
14040b57cec5SDimitry Andric             addRelocationForSymbol(RELo, Value.SymbolName);
14050b57cec5SDimitry Andric           } else {
14060b57cec5SDimitry Andric             addRelocationForSection(REHi, Value.SectionID);
14070b57cec5SDimitry Andric             addRelocationForSection(RELo, Value.SectionID);
14080b57cec5SDimitry Andric           }
14090b57cec5SDimitry Andric         } else {
14100b57cec5SDimitry Andric           // Creating Highest, Higher, Hi and Lo relocations for the filled stub
14110b57cec5SDimitry Andric           // instructions.
14120b57cec5SDimitry Andric           RelocationEntry REHighest(SectionID,
14130b57cec5SDimitry Andric                                     StubTargetAddr - Section.getAddress(),
14140b57cec5SDimitry Andric                                     ELF::R_MIPS_HIGHEST, Value.Addend);
14150b57cec5SDimitry Andric           RelocationEntry REHigher(SectionID,
14160b57cec5SDimitry Andric                                    StubTargetAddr - Section.getAddress() + 4,
14170b57cec5SDimitry Andric                                    ELF::R_MIPS_HIGHER, Value.Addend);
14180b57cec5SDimitry Andric           RelocationEntry REHi(SectionID,
14190b57cec5SDimitry Andric                                StubTargetAddr - Section.getAddress() + 12,
14200b57cec5SDimitry Andric                                ELF::R_MIPS_HI16, Value.Addend);
14210b57cec5SDimitry Andric           RelocationEntry RELo(SectionID,
14220b57cec5SDimitry Andric                                StubTargetAddr - Section.getAddress() + 20,
14230b57cec5SDimitry Andric                                ELF::R_MIPS_LO16, Value.Addend);
14240b57cec5SDimitry Andric           if (Value.SymbolName) {
14250b57cec5SDimitry Andric             addRelocationForSymbol(REHighest, Value.SymbolName);
14260b57cec5SDimitry Andric             addRelocationForSymbol(REHigher, Value.SymbolName);
14270b57cec5SDimitry Andric             addRelocationForSymbol(REHi, Value.SymbolName);
14280b57cec5SDimitry Andric             addRelocationForSymbol(RELo, Value.SymbolName);
14290b57cec5SDimitry Andric           } else {
14300b57cec5SDimitry Andric             addRelocationForSection(REHighest, Value.SectionID);
14310b57cec5SDimitry Andric             addRelocationForSection(REHigher, Value.SectionID);
14320b57cec5SDimitry Andric             addRelocationForSection(REHi, Value.SectionID);
14330b57cec5SDimitry Andric             addRelocationForSection(RELo, Value.SectionID);
14340b57cec5SDimitry Andric           }
14350b57cec5SDimitry Andric         }
14360b57cec5SDimitry Andric         RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
14370b57cec5SDimitry Andric         addRelocationForSection(RE, SectionID);
14380b57cec5SDimitry Andric         Section.advanceStubOffset(getMaxStubSize());
14390b57cec5SDimitry Andric       }
14400b57cec5SDimitry Andric     } else {
14410b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
14420b57cec5SDimitry Andric     }
14430b57cec5SDimitry Andric 
14440b57cec5SDimitry Andric   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
14450b57cec5SDimitry Andric     if (RelType == ELF::R_PPC64_REL24) {
14460b57cec5SDimitry Andric       // Determine ABI variant in use for this object.
14470b57cec5SDimitry Andric       unsigned AbiVariant = Obj.getPlatformFlags();
14480b57cec5SDimitry Andric       AbiVariant &= ELF::EF_PPC64_ABI;
14490b57cec5SDimitry Andric       // A PPC branch relocation will need a stub function if the target is
14500b57cec5SDimitry Andric       // an external symbol (either Value.SymbolName is set, or SymType is
14510b57cec5SDimitry Andric       // Symbol::ST_Unknown) or if the target address is not within the
14520b57cec5SDimitry Andric       // signed 24-bits branch address.
14530b57cec5SDimitry Andric       SectionEntry &Section = Sections[SectionID];
14540b57cec5SDimitry Andric       uint8_t *Target = Section.getAddressWithOffset(Offset);
14550b57cec5SDimitry Andric       bool RangeOverflow = false;
14560b57cec5SDimitry Andric       bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown;
14570b57cec5SDimitry Andric       if (!IsExtern) {
14580b57cec5SDimitry Andric         if (AbiVariant != 2) {
14590b57cec5SDimitry Andric           // In the ELFv1 ABI, a function call may point to the .opd entry,
14600b57cec5SDimitry Andric           // so the final symbol value is calculated based on the relocation
14610b57cec5SDimitry Andric           // values in the .opd section.
14620b57cec5SDimitry Andric           if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
14630b57cec5SDimitry Andric             return std::move(Err);
14640b57cec5SDimitry Andric         } else {
14650b57cec5SDimitry Andric           // In the ELFv2 ABI, a function symbol may provide a local entry
14660b57cec5SDimitry Andric           // point, which must be used for direct calls.
14670b57cec5SDimitry Andric           if (Value.SectionID == SectionID){
14680b57cec5SDimitry Andric             uint8_t SymOther = Symbol->getOther();
14690b57cec5SDimitry Andric             Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
14700b57cec5SDimitry Andric           }
14710b57cec5SDimitry Andric         }
14720b57cec5SDimitry Andric         uint8_t *RelocTarget =
14730b57cec5SDimitry Andric             Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
14740b57cec5SDimitry Andric         int64_t delta = static_cast<int64_t>(Target - RelocTarget);
14750b57cec5SDimitry Andric         // If it is within 26-bits branch range, just set the branch target
14760b57cec5SDimitry Andric         if (SignExtend64<26>(delta) != delta) {
14770b57cec5SDimitry Andric           RangeOverflow = true;
14780b57cec5SDimitry Andric         } else if ((AbiVariant != 2) ||
14790b57cec5SDimitry Andric                    (AbiVariant == 2  && Value.SectionID == SectionID)) {
14800b57cec5SDimitry Andric           RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
14810b57cec5SDimitry Andric           addRelocationForSection(RE, Value.SectionID);
14820b57cec5SDimitry Andric         }
14830b57cec5SDimitry Andric       }
14840b57cec5SDimitry Andric       if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID) ||
14850b57cec5SDimitry Andric           RangeOverflow) {
14860b57cec5SDimitry Andric         // It is an external symbol (either Value.SymbolName is set, or
14870b57cec5SDimitry Andric         // SymType is SymbolRef::ST_Unknown) or out of range.
14880b57cec5SDimitry Andric         StubMap::const_iterator i = Stubs.find(Value);
14890b57cec5SDimitry Andric         if (i != Stubs.end()) {
14900b57cec5SDimitry Andric           // Symbol function stub already created, just relocate to it
14910b57cec5SDimitry Andric           resolveRelocation(Section, Offset,
14920b57cec5SDimitry Andric                             reinterpret_cast<uint64_t>(
14930b57cec5SDimitry Andric                                 Section.getAddressWithOffset(i->second)),
14940b57cec5SDimitry Andric                             RelType, 0);
14950b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " Stub function found\n");
14960b57cec5SDimitry Andric         } else {
14970b57cec5SDimitry Andric           // Create a new stub function.
14980b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " Create a new stub function\n");
14990b57cec5SDimitry Andric           Stubs[Value] = Section.getStubOffset();
15000b57cec5SDimitry Andric           uint8_t *StubTargetAddr = createStubFunction(
15010b57cec5SDimitry Andric               Section.getAddressWithOffset(Section.getStubOffset()),
15020b57cec5SDimitry Andric               AbiVariant);
15030b57cec5SDimitry Andric           RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
15040b57cec5SDimitry Andric                              ELF::R_PPC64_ADDR64, Value.Addend);
15050b57cec5SDimitry Andric 
15060b57cec5SDimitry Andric           // Generates the 64-bits address loads as exemplified in section
15070b57cec5SDimitry Andric           // 4.5.1 in PPC64 ELF ABI.  Note that the relocations need to
15080b57cec5SDimitry Andric           // apply to the low part of the instructions, so we have to update
15090b57cec5SDimitry Andric           // the offset according to the target endianness.
15100b57cec5SDimitry Andric           uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
15110b57cec5SDimitry Andric           if (!IsTargetLittleEndian)
15120b57cec5SDimitry Andric             StubRelocOffset += 2;
15130b57cec5SDimitry Andric 
15140b57cec5SDimitry Andric           RelocationEntry REhst(SectionID, StubRelocOffset + 0,
15150b57cec5SDimitry Andric                                 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
15160b57cec5SDimitry Andric           RelocationEntry REhr(SectionID, StubRelocOffset + 4,
15170b57cec5SDimitry Andric                                ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
15180b57cec5SDimitry Andric           RelocationEntry REh(SectionID, StubRelocOffset + 12,
15190b57cec5SDimitry Andric                               ELF::R_PPC64_ADDR16_HI, Value.Addend);
15200b57cec5SDimitry Andric           RelocationEntry REl(SectionID, StubRelocOffset + 16,
15210b57cec5SDimitry Andric                               ELF::R_PPC64_ADDR16_LO, Value.Addend);
15220b57cec5SDimitry Andric 
15230b57cec5SDimitry Andric           if (Value.SymbolName) {
15240b57cec5SDimitry Andric             addRelocationForSymbol(REhst, Value.SymbolName);
15250b57cec5SDimitry Andric             addRelocationForSymbol(REhr, Value.SymbolName);
15260b57cec5SDimitry Andric             addRelocationForSymbol(REh, Value.SymbolName);
15270b57cec5SDimitry Andric             addRelocationForSymbol(REl, Value.SymbolName);
15280b57cec5SDimitry Andric           } else {
15290b57cec5SDimitry Andric             addRelocationForSection(REhst, Value.SectionID);
15300b57cec5SDimitry Andric             addRelocationForSection(REhr, Value.SectionID);
15310b57cec5SDimitry Andric             addRelocationForSection(REh, Value.SectionID);
15320b57cec5SDimitry Andric             addRelocationForSection(REl, Value.SectionID);
15330b57cec5SDimitry Andric           }
15340b57cec5SDimitry Andric 
15350b57cec5SDimitry Andric           resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
15360b57cec5SDimitry Andric                                                  Section.getAddressWithOffset(
15370b57cec5SDimitry Andric                                                      Section.getStubOffset())),
15380b57cec5SDimitry Andric                             RelType, 0);
15390b57cec5SDimitry Andric           Section.advanceStubOffset(getMaxStubSize());
15400b57cec5SDimitry Andric         }
15410b57cec5SDimitry Andric         if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) {
15420b57cec5SDimitry Andric           // Restore the TOC for external calls
15430b57cec5SDimitry Andric           if (AbiVariant == 2)
15440b57cec5SDimitry Andric             writeInt32BE(Target + 4, 0xE8410018); // ld r2,24(r1)
15450b57cec5SDimitry Andric           else
15460b57cec5SDimitry Andric             writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
15470b57cec5SDimitry Andric         }
15480b57cec5SDimitry Andric       }
15490b57cec5SDimitry Andric     } else if (RelType == ELF::R_PPC64_TOC16 ||
15500b57cec5SDimitry Andric                RelType == ELF::R_PPC64_TOC16_DS ||
15510b57cec5SDimitry Andric                RelType == ELF::R_PPC64_TOC16_LO ||
15520b57cec5SDimitry Andric                RelType == ELF::R_PPC64_TOC16_LO_DS ||
15530b57cec5SDimitry Andric                RelType == ELF::R_PPC64_TOC16_HI ||
15540b57cec5SDimitry Andric                RelType == ELF::R_PPC64_TOC16_HA) {
15550b57cec5SDimitry Andric       // These relocations are supposed to subtract the TOC address from
15560b57cec5SDimitry Andric       // the final value.  This does not fit cleanly into the RuntimeDyld
15570b57cec5SDimitry Andric       // scheme, since there may be *two* sections involved in determining
15580b57cec5SDimitry Andric       // the relocation value (the section of the symbol referred to by the
15590b57cec5SDimitry Andric       // relocation, and the TOC section associated with the current module).
15600b57cec5SDimitry Andric       //
15610b57cec5SDimitry Andric       // Fortunately, these relocations are currently only ever generated
15620b57cec5SDimitry Andric       // referring to symbols that themselves reside in the TOC, which means
15630b57cec5SDimitry Andric       // that the two sections are actually the same.  Thus they cancel out
15640b57cec5SDimitry Andric       // and we can immediately resolve the relocation right now.
15650b57cec5SDimitry Andric       switch (RelType) {
15660b57cec5SDimitry Andric       case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
15670b57cec5SDimitry Andric       case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
15680b57cec5SDimitry Andric       case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
15690b57cec5SDimitry Andric       case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
15700b57cec5SDimitry Andric       case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
15710b57cec5SDimitry Andric       case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
15720b57cec5SDimitry Andric       default: llvm_unreachable("Wrong relocation type.");
15730b57cec5SDimitry Andric       }
15740b57cec5SDimitry Andric 
15750b57cec5SDimitry Andric       RelocationValueRef TOCValue;
15760b57cec5SDimitry Andric       if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
15770b57cec5SDimitry Andric         return std::move(Err);
15780b57cec5SDimitry Andric       if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
15790b57cec5SDimitry Andric         llvm_unreachable("Unsupported TOC relocation.");
15800b57cec5SDimitry Andric       Value.Addend -= TOCValue.Addend;
15810b57cec5SDimitry Andric       resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
15820b57cec5SDimitry Andric     } else {
15830b57cec5SDimitry Andric       // There are two ways to refer to the TOC address directly: either
15840b57cec5SDimitry Andric       // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
15850b57cec5SDimitry Andric       // ignored), or via any relocation that refers to the magic ".TOC."
15860b57cec5SDimitry Andric       // symbols (in which case the addend is respected).
15870b57cec5SDimitry Andric       if (RelType == ELF::R_PPC64_TOC) {
15880b57cec5SDimitry Andric         RelType = ELF::R_PPC64_ADDR64;
15890b57cec5SDimitry Andric         if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
15900b57cec5SDimitry Andric           return std::move(Err);
15910b57cec5SDimitry Andric       } else if (TargetName == ".TOC.") {
15920b57cec5SDimitry Andric         if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
15930b57cec5SDimitry Andric           return std::move(Err);
15940b57cec5SDimitry Andric         Value.Addend += Addend;
15950b57cec5SDimitry Andric       }
15960b57cec5SDimitry Andric 
15970b57cec5SDimitry Andric       RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
15980b57cec5SDimitry Andric 
15990b57cec5SDimitry Andric       if (Value.SymbolName)
16000b57cec5SDimitry Andric         addRelocationForSymbol(RE, Value.SymbolName);
16010b57cec5SDimitry Andric       else
16020b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
16030b57cec5SDimitry Andric     }
16040b57cec5SDimitry Andric   } else if (Arch == Triple::systemz &&
16050b57cec5SDimitry Andric              (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
16060b57cec5SDimitry Andric     // Create function stubs for both PLT and GOT references, regardless of
16070b57cec5SDimitry Andric     // whether the GOT reference is to data or code.  The stub contains the
16080b57cec5SDimitry Andric     // full address of the symbol, as needed by GOT references, and the
16090b57cec5SDimitry Andric     // executable part only adds an overhead of 8 bytes.
16100b57cec5SDimitry Andric     //
16110b57cec5SDimitry Andric     // We could try to conserve space by allocating the code and data
16120b57cec5SDimitry Andric     // parts of the stub separately.  However, as things stand, we allocate
16130b57cec5SDimitry Andric     // a stub for every relocation, so using a GOT in JIT code should be
16140b57cec5SDimitry Andric     // no less space efficient than using an explicit constant pool.
16150b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
16160b57cec5SDimitry Andric     SectionEntry &Section = Sections[SectionID];
16170b57cec5SDimitry Andric 
16180b57cec5SDimitry Andric     // Look for an existing stub.
16190b57cec5SDimitry Andric     StubMap::const_iterator i = Stubs.find(Value);
16200b57cec5SDimitry Andric     uintptr_t StubAddress;
16210b57cec5SDimitry Andric     if (i != Stubs.end()) {
16220b57cec5SDimitry Andric       StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
16230b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " Stub function found\n");
16240b57cec5SDimitry Andric     } else {
16250b57cec5SDimitry Andric       // Create a new stub function.
16260b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " Create a new stub function\n");
16270b57cec5SDimitry Andric 
16280b57cec5SDimitry Andric       uintptr_t BaseAddress = uintptr_t(Section.getAddress());
16290b57cec5SDimitry Andric       uintptr_t StubAlignment = getStubAlignment();
16300b57cec5SDimitry Andric       StubAddress =
16310b57cec5SDimitry Andric           (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
16320b57cec5SDimitry Andric           -StubAlignment;
16330b57cec5SDimitry Andric       unsigned StubOffset = StubAddress - BaseAddress;
16340b57cec5SDimitry Andric 
16350b57cec5SDimitry Andric       Stubs[Value] = StubOffset;
16360b57cec5SDimitry Andric       createStubFunction((uint8_t *)StubAddress);
16370b57cec5SDimitry Andric       RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
16380b57cec5SDimitry Andric                          Value.Offset);
16390b57cec5SDimitry Andric       if (Value.SymbolName)
16400b57cec5SDimitry Andric         addRelocationForSymbol(RE, Value.SymbolName);
16410b57cec5SDimitry Andric       else
16420b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
16430b57cec5SDimitry Andric       Section.advanceStubOffset(getMaxStubSize());
16440b57cec5SDimitry Andric     }
16450b57cec5SDimitry Andric 
16460b57cec5SDimitry Andric     if (RelType == ELF::R_390_GOTENT)
16470b57cec5SDimitry Andric       resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
16480b57cec5SDimitry Andric                         Addend);
16490b57cec5SDimitry Andric     else
16500b57cec5SDimitry Andric       resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
16510b57cec5SDimitry Andric   } else if (Arch == Triple::x86_64) {
16520b57cec5SDimitry Andric     if (RelType == ELF::R_X86_64_PLT32) {
16530b57cec5SDimitry Andric       // The way the PLT relocations normally work is that the linker allocates
16540b57cec5SDimitry Andric       // the
16550b57cec5SDimitry Andric       // PLT and this relocation makes a PC-relative call into the PLT.  The PLT
16560b57cec5SDimitry Andric       // entry will then jump to an address provided by the GOT.  On first call,
16570b57cec5SDimitry Andric       // the
16580b57cec5SDimitry Andric       // GOT address will point back into PLT code that resolves the symbol. After
16590b57cec5SDimitry Andric       // the first call, the GOT entry points to the actual function.
16600b57cec5SDimitry Andric       //
16610b57cec5SDimitry Andric       // For local functions we're ignoring all of that here and just replacing
16620b57cec5SDimitry Andric       // the PLT32 relocation type with PC32, which will translate the relocation
16630b57cec5SDimitry Andric       // into a PC-relative call directly to the function. For external symbols we
16640b57cec5SDimitry Andric       // can't be sure the function will be within 2^32 bytes of the call site, so
16650b57cec5SDimitry Andric       // we need to create a stub, which calls into the GOT.  This case is
16660b57cec5SDimitry Andric       // equivalent to the usual PLT implementation except that we use the stub
16670b57cec5SDimitry Andric       // mechanism in RuntimeDyld (which puts stubs at the end of the section)
16680b57cec5SDimitry Andric       // rather than allocating a PLT section.
16690b57cec5SDimitry Andric       if (Value.SymbolName) {
16700b57cec5SDimitry Andric         // This is a call to an external function.
16710b57cec5SDimitry Andric         // Look for an existing stub.
16720b57cec5SDimitry Andric         SectionEntry &Section = Sections[SectionID];
16730b57cec5SDimitry Andric         StubMap::const_iterator i = Stubs.find(Value);
16740b57cec5SDimitry Andric         uintptr_t StubAddress;
16750b57cec5SDimitry Andric         if (i != Stubs.end()) {
16760b57cec5SDimitry Andric           StubAddress = uintptr_t(Section.getAddress()) + i->second;
16770b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " Stub function found\n");
16780b57cec5SDimitry Andric         } else {
16790b57cec5SDimitry Andric           // Create a new stub function (equivalent to a PLT entry).
16800b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << " Create a new stub function\n");
16810b57cec5SDimitry Andric 
16820b57cec5SDimitry Andric           uintptr_t BaseAddress = uintptr_t(Section.getAddress());
16830b57cec5SDimitry Andric           uintptr_t StubAlignment = getStubAlignment();
16840b57cec5SDimitry Andric           StubAddress =
16850b57cec5SDimitry Andric               (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
16860b57cec5SDimitry Andric               -StubAlignment;
16870b57cec5SDimitry Andric           unsigned StubOffset = StubAddress - BaseAddress;
16880b57cec5SDimitry Andric           Stubs[Value] = StubOffset;
16890b57cec5SDimitry Andric           createStubFunction((uint8_t *)StubAddress);
16900b57cec5SDimitry Andric 
16910b57cec5SDimitry Andric           // Bump our stub offset counter
16920b57cec5SDimitry Andric           Section.advanceStubOffset(getMaxStubSize());
16930b57cec5SDimitry Andric 
16940b57cec5SDimitry Andric           // Allocate a GOT Entry
16950b57cec5SDimitry Andric           uint64_t GOTOffset = allocateGOTEntries(1);
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric           // The load of the GOT address has an addend of -4
16980b57cec5SDimitry Andric           resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
16990b57cec5SDimitry Andric                                      ELF::R_X86_64_PC32);
17000b57cec5SDimitry Andric 
17010b57cec5SDimitry Andric           // Fill in the value of the symbol we're targeting into the GOT
17020b57cec5SDimitry Andric           addRelocationForSymbol(
17030b57cec5SDimitry Andric               computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
17040b57cec5SDimitry Andric               Value.SymbolName);
17050b57cec5SDimitry Andric         }
17060b57cec5SDimitry Andric 
17070b57cec5SDimitry Andric         // Make the target call a call into the stub table.
17080b57cec5SDimitry Andric         resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32,
17090b57cec5SDimitry Andric                           Addend);
17100b57cec5SDimitry Andric       } else {
17110b57cec5SDimitry Andric         RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend,
17120b57cec5SDimitry Andric                   Value.Offset);
17130b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
17140b57cec5SDimitry Andric       }
17150b57cec5SDimitry Andric     } else if (RelType == ELF::R_X86_64_GOTPCREL ||
17160b57cec5SDimitry Andric                RelType == ELF::R_X86_64_GOTPCRELX ||
17170b57cec5SDimitry Andric                RelType == ELF::R_X86_64_REX_GOTPCRELX) {
17180b57cec5SDimitry Andric       uint64_t GOTOffset = allocateGOTEntries(1);
17190b57cec5SDimitry Andric       resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
17200b57cec5SDimitry Andric                                  ELF::R_X86_64_PC32);
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric       // Fill in the value of the symbol we're targeting into the GOT
17230b57cec5SDimitry Andric       RelocationEntry RE =
17240b57cec5SDimitry Andric           computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
17250b57cec5SDimitry Andric       if (Value.SymbolName)
17260b57cec5SDimitry Andric         addRelocationForSymbol(RE, Value.SymbolName);
17270b57cec5SDimitry Andric       else
17280b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
17290b57cec5SDimitry Andric     } else if (RelType == ELF::R_X86_64_GOT64) {
17300b57cec5SDimitry Andric       // Fill in a 64-bit GOT offset.
17310b57cec5SDimitry Andric       uint64_t GOTOffset = allocateGOTEntries(1);
17320b57cec5SDimitry Andric       resolveRelocation(Sections[SectionID], Offset, GOTOffset,
17330b57cec5SDimitry Andric                         ELF::R_X86_64_64, 0);
17340b57cec5SDimitry Andric 
17350b57cec5SDimitry Andric       // Fill in the value of the symbol we're targeting into the GOT
17360b57cec5SDimitry Andric       RelocationEntry RE =
17370b57cec5SDimitry Andric           computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
17380b57cec5SDimitry Andric       if (Value.SymbolName)
17390b57cec5SDimitry Andric         addRelocationForSymbol(RE, Value.SymbolName);
17400b57cec5SDimitry Andric       else
17410b57cec5SDimitry Andric         addRelocationForSection(RE, Value.SectionID);
17420b57cec5SDimitry Andric     } else if (RelType == ELF::R_X86_64_GOTPC64) {
17430b57cec5SDimitry Andric       // Materialize the address of the base of the GOT relative to the PC.
17440b57cec5SDimitry Andric       // This doesn't create a GOT entry, but it does mean we need a GOT
17450b57cec5SDimitry Andric       // section.
17460b57cec5SDimitry Andric       (void)allocateGOTEntries(0);
17470b57cec5SDimitry Andric       resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
17480b57cec5SDimitry Andric     } else if (RelType == ELF::R_X86_64_GOTOFF64) {
17490b57cec5SDimitry Andric       // GOTOFF relocations ultimately require a section difference relocation.
17500b57cec5SDimitry Andric       (void)allocateGOTEntries(0);
17510b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
17520b57cec5SDimitry Andric     } else if (RelType == ELF::R_X86_64_PC32) {
17530b57cec5SDimitry Andric       Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
17540b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
17550b57cec5SDimitry Andric     } else if (RelType == ELF::R_X86_64_PC64) {
17560b57cec5SDimitry Andric       Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset));
17570b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
17580b57cec5SDimitry Andric     } else {
17590b57cec5SDimitry Andric       processSimpleRelocation(SectionID, Offset, RelType, Value);
17600b57cec5SDimitry Andric     }
17610b57cec5SDimitry Andric   } else {
17620b57cec5SDimitry Andric     if (Arch == Triple::x86) {
17630b57cec5SDimitry Andric       Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
17640b57cec5SDimitry Andric     }
17650b57cec5SDimitry Andric     processSimpleRelocation(SectionID, Offset, RelType, Value);
17660b57cec5SDimitry Andric   }
17670b57cec5SDimitry Andric   return ++RelI;
17680b57cec5SDimitry Andric }
17690b57cec5SDimitry Andric 
17700b57cec5SDimitry Andric size_t RuntimeDyldELF::getGOTEntrySize() {
17710b57cec5SDimitry Andric   // We don't use the GOT in all of these cases, but it's essentially free
17720b57cec5SDimitry Andric   // to put them all here.
17730b57cec5SDimitry Andric   size_t Result = 0;
17740b57cec5SDimitry Andric   switch (Arch) {
17750b57cec5SDimitry Andric   case Triple::x86_64:
17760b57cec5SDimitry Andric   case Triple::aarch64:
17770b57cec5SDimitry Andric   case Triple::aarch64_be:
17780b57cec5SDimitry Andric   case Triple::ppc64:
17790b57cec5SDimitry Andric   case Triple::ppc64le:
17800b57cec5SDimitry Andric   case Triple::systemz:
17810b57cec5SDimitry Andric     Result = sizeof(uint64_t);
17820b57cec5SDimitry Andric     break;
17830b57cec5SDimitry Andric   case Triple::x86:
17840b57cec5SDimitry Andric   case Triple::arm:
17850b57cec5SDimitry Andric   case Triple::thumb:
17860b57cec5SDimitry Andric     Result = sizeof(uint32_t);
17870b57cec5SDimitry Andric     break;
17880b57cec5SDimitry Andric   case Triple::mips:
17890b57cec5SDimitry Andric   case Triple::mipsel:
17900b57cec5SDimitry Andric   case Triple::mips64:
17910b57cec5SDimitry Andric   case Triple::mips64el:
17920b57cec5SDimitry Andric     if (IsMipsO32ABI || IsMipsN32ABI)
17930b57cec5SDimitry Andric       Result = sizeof(uint32_t);
17940b57cec5SDimitry Andric     else if (IsMipsN64ABI)
17950b57cec5SDimitry Andric       Result = sizeof(uint64_t);
17960b57cec5SDimitry Andric     else
17970b57cec5SDimitry Andric       llvm_unreachable("Mips ABI not handled");
17980b57cec5SDimitry Andric     break;
17990b57cec5SDimitry Andric   default:
18000b57cec5SDimitry Andric     llvm_unreachable("Unsupported CPU type!");
18010b57cec5SDimitry Andric   }
18020b57cec5SDimitry Andric   return Result;
18030b57cec5SDimitry Andric }
18040b57cec5SDimitry Andric 
18050b57cec5SDimitry Andric uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no) {
18060b57cec5SDimitry Andric   if (GOTSectionID == 0) {
18070b57cec5SDimitry Andric     GOTSectionID = Sections.size();
18080b57cec5SDimitry Andric     // Reserve a section id. We'll allocate the section later
18090b57cec5SDimitry Andric     // once we know the total size
18100b57cec5SDimitry Andric     Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0));
18110b57cec5SDimitry Andric   }
18120b57cec5SDimitry Andric   uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
18130b57cec5SDimitry Andric   CurrentGOTIndex += no;
18140b57cec5SDimitry Andric   return StartOffset;
18150b57cec5SDimitry Andric }
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef &Value,
18180b57cec5SDimitry Andric                                              unsigned GOTRelType) {
18190b57cec5SDimitry Andric   auto E = GOTOffsetMap.insert({Value, 0});
18200b57cec5SDimitry Andric   if (E.second) {
18210b57cec5SDimitry Andric     uint64_t GOTOffset = allocateGOTEntries(1);
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric     // Create relocation for newly created GOT entry
18240b57cec5SDimitry Andric     RelocationEntry RE =
18250b57cec5SDimitry Andric         computeGOTOffsetRE(GOTOffset, Value.Offset, GOTRelType);
18260b57cec5SDimitry Andric     if (Value.SymbolName)
18270b57cec5SDimitry Andric       addRelocationForSymbol(RE, Value.SymbolName);
18280b57cec5SDimitry Andric     else
18290b57cec5SDimitry Andric       addRelocationForSection(RE, Value.SectionID);
18300b57cec5SDimitry Andric 
18310b57cec5SDimitry Andric     E.first->second = GOTOffset;
18320b57cec5SDimitry Andric   }
18330b57cec5SDimitry Andric 
18340b57cec5SDimitry Andric   return E.first->second;
18350b57cec5SDimitry Andric }
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID,
18380b57cec5SDimitry Andric                                                 uint64_t Offset,
18390b57cec5SDimitry Andric                                                 uint64_t GOTOffset,
18400b57cec5SDimitry Andric                                                 uint32_t Type) {
18410b57cec5SDimitry Andric   // Fill in the relative address of the GOT Entry into the stub
18420b57cec5SDimitry Andric   RelocationEntry GOTRE(SectionID, Offset, Type, GOTOffset);
18430b57cec5SDimitry Andric   addRelocationForSection(GOTRE, GOTSectionID);
18440b57cec5SDimitry Andric }
18450b57cec5SDimitry Andric 
18460b57cec5SDimitry Andric RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
18470b57cec5SDimitry Andric                                                    uint64_t SymbolOffset,
18480b57cec5SDimitry Andric                                                    uint32_t Type) {
18490b57cec5SDimitry Andric   return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
18500b57cec5SDimitry Andric }
18510b57cec5SDimitry Andric 
18520b57cec5SDimitry Andric Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj,
18530b57cec5SDimitry Andric                                   ObjSectionToIDMap &SectionMap) {
18540b57cec5SDimitry Andric   if (IsMipsO32ABI)
18550b57cec5SDimitry Andric     if (!PendingRelocs.empty())
18560b57cec5SDimitry Andric       return make_error<RuntimeDyldError>("Can't find matching LO16 reloc");
18570b57cec5SDimitry Andric 
18580b57cec5SDimitry Andric   // If necessary, allocate the global offset table
18590b57cec5SDimitry Andric   if (GOTSectionID != 0) {
18600b57cec5SDimitry Andric     // Allocate memory for the section
18610b57cec5SDimitry Andric     size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
18620b57cec5SDimitry Andric     uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(),
18630b57cec5SDimitry Andric                                                 GOTSectionID, ".got", false);
18640b57cec5SDimitry Andric     if (!Addr)
18650b57cec5SDimitry Andric       return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!");
18660b57cec5SDimitry Andric 
18670b57cec5SDimitry Andric     Sections[GOTSectionID] =
18680b57cec5SDimitry Andric         SectionEntry(".got", Addr, TotalSize, TotalSize, 0);
18690b57cec5SDimitry Andric 
18700b57cec5SDimitry Andric     // For now, initialize all GOT entries to zero.  We'll fill them in as
18710b57cec5SDimitry Andric     // needed when GOT-based relocations are applied.
18720b57cec5SDimitry Andric     memset(Addr, 0, TotalSize);
18730b57cec5SDimitry Andric     if (IsMipsN32ABI || IsMipsN64ABI) {
18740b57cec5SDimitry Andric       // To correctly resolve Mips GOT relocations, we need a mapping from
18750b57cec5SDimitry Andric       // object's sections to GOTs.
18760b57cec5SDimitry Andric       for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
18770b57cec5SDimitry Andric            SI != SE; ++SI) {
18780b57cec5SDimitry Andric         if (SI->relocation_begin() != SI->relocation_end()) {
1879*8bcb0991SDimitry Andric           Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
1880*8bcb0991SDimitry Andric           if (!RelSecOrErr)
1881*8bcb0991SDimitry Andric             return make_error<RuntimeDyldError>(
1882*8bcb0991SDimitry Andric                 toString(RelSecOrErr.takeError()));
1883*8bcb0991SDimitry Andric 
1884*8bcb0991SDimitry Andric           section_iterator RelocatedSection = *RelSecOrErr;
18850b57cec5SDimitry Andric           ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
18860b57cec5SDimitry Andric           assert (i != SectionMap.end());
18870b57cec5SDimitry Andric           SectionToGOTMap[i->second] = GOTSectionID;
18880b57cec5SDimitry Andric         }
18890b57cec5SDimitry Andric       }
18900b57cec5SDimitry Andric       GOTSymbolOffsets.clear();
18910b57cec5SDimitry Andric     }
18920b57cec5SDimitry Andric   }
18930b57cec5SDimitry Andric 
18940b57cec5SDimitry Andric   // Look for and record the EH frame section.
18950b57cec5SDimitry Andric   ObjSectionToIDMap::iterator i, e;
18960b57cec5SDimitry Andric   for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
18970b57cec5SDimitry Andric     const SectionRef &Section = i->first;
1898*8bcb0991SDimitry Andric 
18990b57cec5SDimitry Andric     StringRef Name;
1900*8bcb0991SDimitry Andric     Expected<StringRef> NameOrErr = Section.getName();
1901*8bcb0991SDimitry Andric     if (NameOrErr)
1902*8bcb0991SDimitry Andric       Name = *NameOrErr;
1903*8bcb0991SDimitry Andric     else
1904*8bcb0991SDimitry Andric       consumeError(NameOrErr.takeError());
1905*8bcb0991SDimitry Andric 
19060b57cec5SDimitry Andric     if (Name == ".eh_frame") {
19070b57cec5SDimitry Andric       UnregisteredEHFrameSections.push_back(i->second);
19080b57cec5SDimitry Andric       break;
19090b57cec5SDimitry Andric     }
19100b57cec5SDimitry Andric   }
19110b57cec5SDimitry Andric 
19120b57cec5SDimitry Andric   GOTSectionID = 0;
19130b57cec5SDimitry Andric   CurrentGOTIndex = 0;
19140b57cec5SDimitry Andric 
19150b57cec5SDimitry Andric   return Error::success();
19160b57cec5SDimitry Andric }
19170b57cec5SDimitry Andric 
19180b57cec5SDimitry Andric bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const {
19190b57cec5SDimitry Andric   return Obj.isELF();
19200b57cec5SDimitry Andric }
19210b57cec5SDimitry Andric 
19220b57cec5SDimitry Andric bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef &R) const {
19230b57cec5SDimitry Andric   unsigned RelTy = R.getType();
19240b57cec5SDimitry Andric   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
19250b57cec5SDimitry Andric     return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
19260b57cec5SDimitry Andric            RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
19270b57cec5SDimitry Andric 
19280b57cec5SDimitry Andric   if (Arch == Triple::x86_64)
19290b57cec5SDimitry Andric     return RelTy == ELF::R_X86_64_GOTPCREL ||
19300b57cec5SDimitry Andric            RelTy == ELF::R_X86_64_GOTPCRELX ||
19310b57cec5SDimitry Andric            RelTy == ELF::R_X86_64_GOT64 ||
19320b57cec5SDimitry Andric            RelTy == ELF::R_X86_64_REX_GOTPCRELX;
19330b57cec5SDimitry Andric   return false;
19340b57cec5SDimitry Andric }
19350b57cec5SDimitry Andric 
19360b57cec5SDimitry Andric bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const {
19370b57cec5SDimitry Andric   if (Arch != Triple::x86_64)
19380b57cec5SDimitry Andric     return true;  // Conservative answer
19390b57cec5SDimitry Andric 
19400b57cec5SDimitry Andric   switch (R.getType()) {
19410b57cec5SDimitry Andric   default:
19420b57cec5SDimitry Andric     return true;  // Conservative answer
19430b57cec5SDimitry Andric 
19440b57cec5SDimitry Andric 
19450b57cec5SDimitry Andric   case ELF::R_X86_64_GOTPCREL:
19460b57cec5SDimitry Andric   case ELF::R_X86_64_GOTPCRELX:
19470b57cec5SDimitry Andric   case ELF::R_X86_64_REX_GOTPCRELX:
19480b57cec5SDimitry Andric   case ELF::R_X86_64_GOTPC64:
19490b57cec5SDimitry Andric   case ELF::R_X86_64_GOT64:
19500b57cec5SDimitry Andric   case ELF::R_X86_64_GOTOFF64:
19510b57cec5SDimitry Andric   case ELF::R_X86_64_PC32:
19520b57cec5SDimitry Andric   case ELF::R_X86_64_PC64:
19530b57cec5SDimitry Andric   case ELF::R_X86_64_64:
19540b57cec5SDimitry Andric     // We know that these reloation types won't need a stub function.  This list
19550b57cec5SDimitry Andric     // can be extended as needed.
19560b57cec5SDimitry Andric     return false;
19570b57cec5SDimitry Andric   }
19580b57cec5SDimitry Andric }
19590b57cec5SDimitry Andric 
19600b57cec5SDimitry Andric } // namespace llvm
1961