LLVM  mainline
RuntimeDyld.cpp
Go to the documentation of this file.
00001 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Implementation of the MC-JIT runtime dynamic linker.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/ExecutionEngine/RuntimeDyld.h"
00015 #include "RuntimeDyldCheckerImpl.h"
00016 #include "RuntimeDyldCOFF.h"
00017 #include "RuntimeDyldELF.h"
00018 #include "RuntimeDyldImpl.h"
00019 #include "RuntimeDyldMachO.h"
00020 #include "llvm/Object/ELFObjectFile.h"
00021 #include "llvm/Object/COFF.h"
00022 #include "llvm/Support/MathExtras.h"
00023 #include "llvm/Support/MutexGuard.h"
00024 
00025 using namespace llvm;
00026 using namespace llvm::object;
00027 
00028 #define DEBUG_TYPE "dyld"
00029 
00030 // Empty out-of-line virtual destructor as the key function.
00031 RuntimeDyldImpl::~RuntimeDyldImpl() {}
00032 
00033 // Pin LoadedObjectInfo's vtables to this file.
00034 void RuntimeDyld::LoadedObjectInfo::anchor() {}
00035 
00036 namespace llvm {
00037 
00038 void RuntimeDyldImpl::registerEHFrames() {}
00039 
00040 void RuntimeDyldImpl::deregisterEHFrames() {}
00041 
00042 #ifndef NDEBUG
00043 static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
00044   dbgs() << "----- Contents of section " << S.Name << " " << State << " -----";
00045 
00046   if (S.Address == nullptr) {
00047     dbgs() << "\n          <section not emitted>\n";
00048     return;
00049   }
00050 
00051   const unsigned ColsPerRow = 16;
00052 
00053   uint8_t *DataAddr = S.Address;
00054   uint64_t LoadAddr = S.LoadAddress;
00055 
00056   unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
00057   unsigned BytesRemaining = S.Size;
00058 
00059   if (StartPadding) {
00060     dbgs() << "\n" << format("0x%016" PRIx64,
00061                              LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
00062     while (StartPadding--)
00063       dbgs() << "   ";
00064   }
00065 
00066   while (BytesRemaining > 0) {
00067     if ((LoadAddr & (ColsPerRow - 1)) == 0)
00068       dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
00069 
00070     dbgs() << " " << format("%02x", *DataAddr);
00071 
00072     ++DataAddr;
00073     ++LoadAddr;
00074     --BytesRemaining;
00075   }
00076 
00077   dbgs() << "\n";
00078 }
00079 #endif
00080 
00081 // Resolve the relocations for all symbols we currently know about.
00082 void RuntimeDyldImpl::resolveRelocations() {
00083   MutexGuard locked(lock);
00084 
00085   // First, resolve relocations associated with external symbols.
00086   resolveExternalSymbols();
00087 
00088   // Just iterate over the sections we have and resolve all the relocations
00089   // in them. Gross overkill, but it gets the job done.
00090   for (int i = 0, e = Sections.size(); i != e; ++i) {
00091     // The Section here (Sections[i]) refers to the section in which the
00092     // symbol for the relocation is located.  The SectionID in the relocation
00093     // entry provides the section to which the relocation will be applied.
00094     uint64_t Addr = Sections[i].LoadAddress;
00095     DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
00096                  << format("%p", (uintptr_t)Addr) << "\n");
00097     DEBUG(dumpSectionMemory(Sections[i], "before relocations"));
00098     resolveRelocationList(Relocations[i], Addr);
00099     DEBUG(dumpSectionMemory(Sections[i], "after relocations"));
00100     Relocations.erase(i);
00101   }
00102 }
00103 
00104 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
00105                                         uint64_t TargetAddress) {
00106   MutexGuard locked(lock);
00107   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
00108     if (Sections[i].Address == LocalAddress) {
00109       reassignSectionAddress(i, TargetAddress);
00110       return;
00111     }
00112   }
00113   llvm_unreachable("Attempting to remap address of unknown section!");
00114 }
00115 
00116 static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
00117   uint64_t Address;
00118   if (std::error_code EC = Sym.getAddress(Address))
00119     return EC;
00120 
00121   if (Address == UnknownAddressOrSize) {
00122     Result = UnknownAddressOrSize;
00123     return std::error_code();
00124   }
00125 
00126   const ObjectFile *Obj = Sym.getObject();
00127   section_iterator SecI(Obj->section_begin());
00128   if (std::error_code EC = Sym.getSection(SecI))
00129     return EC;
00130 
00131   if (SecI == Obj->section_end()) {
00132     Result = UnknownAddressOrSize;
00133     return std::error_code();
00134   }
00135 
00136   uint64_t SectionAddress = SecI->getAddress();
00137   Result = Address - SectionAddress;
00138   return std::error_code();
00139 }
00140 
00141 std::pair<unsigned, unsigned>
00142 RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
00143   MutexGuard locked(lock);
00144 
00145   // Grab the first Section ID. We'll use this later to construct the underlying
00146   // range for the returned LoadedObjectInfo.
00147   unsigned SectionsAddedBeginIdx = Sections.size();
00148 
00149   // Save information about our target
00150   Arch = (Triple::ArchType)Obj.getArch();
00151   IsTargetLittleEndian = Obj.isLittleEndian();
00152   setMipsABI(Obj);
00153 
00154   // Compute the memory size required to load all sections to be loaded
00155   // and pass this information to the memory manager
00156   if (MemMgr.needsToReserveAllocationSpace()) {
00157     uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
00158     computeTotalAllocSize(Obj, CodeSize, DataSizeRO, DataSizeRW);
00159     MemMgr.reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
00160   }
00161 
00162   // Used sections from the object file
00163   ObjSectionToIDMap LocalSections;
00164 
00165   // Common symbols requiring allocation, with their sizes and alignments
00166   CommonSymbolList CommonSymbols;
00167 
00168   // Parse symbols
00169   DEBUG(dbgs() << "Parse symbols:\n");
00170   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
00171        ++I) {
00172     uint32_t Flags = I->getFlags();
00173 
00174     bool IsCommon = Flags & SymbolRef::SF_Common;
00175     if (IsCommon)
00176       CommonSymbols.push_back(*I);
00177     else {
00178       object::SymbolRef::Type SymType;
00179       Check(I->getType(SymType));
00180 
00181       if (SymType == object::SymbolRef::ST_Function ||
00182           SymType == object::SymbolRef::ST_Data ||
00183           SymType == object::SymbolRef::ST_Unknown) {
00184 
00185         StringRef Name;
00186         uint64_t SectOffset;
00187         Check(I->getName(Name));
00188         Check(getOffset(*I, SectOffset));
00189         section_iterator SI = Obj.section_end();
00190         Check(I->getSection(SI));
00191         if (SI == Obj.section_end())
00192           continue;
00193         StringRef SectionData;
00194         Check(SI->getContents(SectionData));
00195         bool IsCode = SI->isText();
00196         unsigned SectionID =
00197             findOrEmitSection(Obj, *SI, IsCode, LocalSections);
00198         DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
00199                      << " SID: " << SectionID << " Offset: "
00200                      << format("%p", (uintptr_t)SectOffset)
00201                      << " flags: " << Flags << "\n");
00202         JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
00203         if (Flags & SymbolRef::SF_Weak)
00204           RTDyldSymFlags |= JITSymbolFlags::Weak;
00205         if (Flags & SymbolRef::SF_Exported)
00206           RTDyldSymFlags |= JITSymbolFlags::Exported;
00207         GlobalSymbolTable[Name] =
00208           SymbolTableEntry(SectionID, SectOffset, RTDyldSymFlags);
00209       }
00210     }
00211   }
00212 
00213   // Allocate common symbols
00214   emitCommonSymbols(Obj, CommonSymbols);
00215 
00216   // Parse and process relocations
00217   DEBUG(dbgs() << "Parse relocations:\n");
00218   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
00219        SI != SE; ++SI) {
00220     unsigned SectionID = 0;
00221     StubMap Stubs;
00222     section_iterator RelocatedSection = SI->getRelocatedSection();
00223 
00224     if (RelocatedSection == SE)
00225       continue;
00226 
00227     relocation_iterator I = SI->relocation_begin();
00228     relocation_iterator E = SI->relocation_end();
00229 
00230     if (I == E && !ProcessAllSections)
00231       continue;
00232 
00233     bool IsCode = RelocatedSection->isText();
00234     SectionID =
00235         findOrEmitSection(Obj, *RelocatedSection, IsCode, LocalSections);
00236     DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
00237 
00238     for (; I != E;)
00239       I = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs);
00240 
00241     // If there is an attached checker, notify it about the stubs for this
00242     // section so that they can be verified.
00243     if (Checker)
00244       Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
00245   }
00246 
00247   // Give the subclasses a chance to tie-up any loose ends.
00248   finalizeLoad(Obj, LocalSections);
00249 
00250   unsigned SectionsAddedEndIdx = Sections.size();
00251 
00252   return std::make_pair(SectionsAddedBeginIdx, SectionsAddedEndIdx);
00253 }
00254 
00255 // A helper method for computeTotalAllocSize.
00256 // Computes the memory size required to allocate sections with the given sizes,
00257 // assuming that all sections are allocated with the given alignment
00258 static uint64_t
00259 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
00260                                  uint64_t Alignment) {
00261   uint64_t TotalSize = 0;
00262   for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
00263     uint64_t AlignedSize =
00264         (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
00265     TotalSize += AlignedSize;
00266   }
00267   return TotalSize;
00268 }
00269 
00270 static bool isRequiredForExecution(const SectionRef &Section) {
00271   const ObjectFile *Obj = Section.getObject();
00272   if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj))
00273     return ELFObj->getSectionFlags(Section) & ELF::SHF_ALLOC;
00274   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
00275     const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
00276     // Avoid loading zero-sized COFF sections.
00277     // In PE files, VirtualSize gives the section size, and SizeOfRawData
00278     // may be zero for sections with content. In Obj files, SizeOfRawData 
00279     // gives the section size, and VirtualSize is always zero. Hence
00280     // the need to check for both cases below.
00281     bool HasContent = (CoffSection->VirtualSize > 0) 
00282       || (CoffSection->SizeOfRawData > 0);
00283     bool IsDiscardable = CoffSection->Characteristics &
00284       (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
00285     return HasContent && !IsDiscardable;
00286   }
00287   
00288   assert(isa<MachOObjectFile>(Obj));
00289   return true;
00290  }
00291 
00292 static bool isReadOnlyData(const SectionRef &Section) {
00293   const ObjectFile *Obj = Section.getObject();
00294   if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj))
00295     return !(ELFObj->getSectionFlags(Section) &
00296              (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
00297   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
00298     return ((COFFObj->getCOFFSection(Section)->Characteristics &
00299              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
00300              | COFF::IMAGE_SCN_MEM_READ
00301              | COFF::IMAGE_SCN_MEM_WRITE))
00302              ==
00303              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
00304              | COFF::IMAGE_SCN_MEM_READ));
00305 
00306   assert(isa<MachOObjectFile>(Obj));
00307   return false;
00308 }
00309 
00310 static bool isZeroInit(const SectionRef &Section) {
00311   const ObjectFile *Obj = Section.getObject();
00312   if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj))
00313     return ELFObj->getSectionType(Section) == ELF::SHT_NOBITS;
00314   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
00315     return COFFObj->getCOFFSection(Section)->Characteristics &
00316             COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
00317 
00318   auto *MachO = cast<MachOObjectFile>(Obj);
00319   unsigned SectionType = MachO->getSectionType(Section);
00320   return SectionType == MachO::S_ZEROFILL ||
00321          SectionType == MachO::S_GB_ZEROFILL;
00322 }
00323 
00324 // Compute an upper bound of the memory size that is required to load all
00325 // sections
00326 void RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
00327                                             uint64_t &CodeSize,
00328                                             uint64_t &DataSizeRO,
00329                                             uint64_t &DataSizeRW) {
00330   // Compute the size of all sections required for execution
00331   std::vector<uint64_t> CodeSectionSizes;
00332   std::vector<uint64_t> ROSectionSizes;
00333   std::vector<uint64_t> RWSectionSizes;
00334   uint64_t MaxAlignment = sizeof(void *);
00335 
00336   // Collect sizes of all sections to be loaded;
00337   // also determine the max alignment of all sections
00338   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
00339        SI != SE; ++SI) {
00340     const SectionRef &Section = *SI;
00341 
00342     bool IsRequired = isRequiredForExecution(Section);
00343 
00344     // Consider only the sections that are required to be loaded for execution
00345     if (IsRequired) {
00346       StringRef Name;
00347       uint64_t DataSize = Section.getSize();
00348       uint64_t Alignment64 = Section.getAlignment();
00349       bool IsCode = Section.isText();
00350       bool IsReadOnly = isReadOnlyData(Section);
00351       Check(Section.getName(Name));
00352       unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
00353 
00354       uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
00355       uint64_t SectionSize = DataSize + StubBufSize;
00356 
00357       // The .eh_frame section (at least on Linux) needs an extra four bytes
00358       // padded
00359       // with zeroes added at the end.  For MachO objects, this section has a
00360       // slightly different name, so this won't have any effect for MachO
00361       // objects.
00362       if (Name == ".eh_frame")
00363         SectionSize += 4;
00364 
00365       if (!SectionSize)
00366         SectionSize = 1;
00367 
00368       if (IsCode) {
00369         CodeSectionSizes.push_back(SectionSize);
00370       } else if (IsReadOnly) {
00371         ROSectionSizes.push_back(SectionSize);
00372       } else {
00373         RWSectionSizes.push_back(SectionSize);
00374       }
00375 
00376       // update the max alignment
00377       if (Alignment > MaxAlignment) {
00378         MaxAlignment = Alignment;
00379       }
00380     }
00381   }
00382 
00383   // Compute the size of all common symbols
00384   uint64_t CommonSize = 0;
00385   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
00386        ++I) {
00387     uint32_t Flags = I->getFlags();
00388     if (Flags & SymbolRef::SF_Common) {
00389       // Add the common symbols to a list.  We'll allocate them all below.
00390       uint64_t Size = I->getSize();
00391       CommonSize += Size;
00392     }
00393   }
00394   if (CommonSize != 0) {
00395     RWSectionSizes.push_back(CommonSize);
00396   }
00397 
00398   // Compute the required allocation space for each different type of sections
00399   // (code, read-only data, read-write data) assuming that all sections are
00400   // allocated with the max alignment. Note that we cannot compute with the
00401   // individual alignments of the sections, because then the required size
00402   // depends on the order, in which the sections are allocated.
00403   CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
00404   DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
00405   DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
00406 }
00407 
00408 // compute stub buffer size for the given section
00409 unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
00410                                                     const SectionRef &Section) {
00411   unsigned StubSize = getMaxStubSize();
00412   if (StubSize == 0) {
00413     return 0;
00414   }
00415   // FIXME: this is an inefficient way to handle this. We should computed the
00416   // necessary section allocation size in loadObject by walking all the sections
00417   // once.
00418   unsigned StubBufSize = 0;
00419   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
00420        SI != SE; ++SI) {
00421     section_iterator RelSecI = SI->getRelocatedSection();
00422     if (!(RelSecI == Section))
00423       continue;
00424 
00425     for (const RelocationRef &Reloc : SI->relocations()) {
00426       (void)Reloc;
00427       StubBufSize += StubSize;
00428     }
00429   }
00430 
00431   // Get section data size and alignment
00432   uint64_t DataSize = Section.getSize();
00433   uint64_t Alignment64 = Section.getAlignment();
00434 
00435   // Add stubbuf size alignment
00436   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
00437   unsigned StubAlignment = getStubAlignment();
00438   unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
00439   if (StubAlignment > EndAlignment)
00440     StubBufSize += StubAlignment - EndAlignment;
00441   return StubBufSize;
00442 }
00443 
00444 uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
00445                                              unsigned Size) const {
00446   uint64_t Result = 0;
00447   if (IsTargetLittleEndian) {
00448     Src += Size - 1;
00449     while (Size--)
00450       Result = (Result << 8) | *Src--;
00451   } else
00452     while (Size--)
00453       Result = (Result << 8) | *Src++;
00454 
00455   return Result;
00456 }
00457 
00458 void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
00459                                           unsigned Size) const {
00460   if (IsTargetLittleEndian) {
00461     while (Size--) {
00462       *Dst++ = Value & 0xFF;
00463       Value >>= 8;
00464     }
00465   } else {
00466     Dst += Size - 1;
00467     while (Size--) {
00468       *Dst-- = Value & 0xFF;
00469       Value >>= 8;
00470     }
00471   }
00472 }
00473 
00474 void RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
00475                                         CommonSymbolList &CommonSymbols) {
00476   if (CommonSymbols.empty())
00477     return;
00478 
00479   uint64_t CommonSize = 0;
00480   CommonSymbolList SymbolsToAllocate;
00481 
00482   DEBUG(dbgs() << "Processing common symbols...\n");
00483 
00484   for (const auto &Sym : CommonSymbols) {
00485     StringRef Name;
00486     Check(Sym.getName(Name));
00487 
00488     // Skip common symbols already elsewhere.
00489     if (GlobalSymbolTable.count(Name) ||
00490         Resolver.findSymbolInLogicalDylib(Name)) {
00491       DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
00492                    << "'\n");
00493       continue;
00494     }
00495 
00496     uint32_t Align = Sym.getAlignment();
00497     uint64_t Size = Sym.getSize();
00498 
00499     CommonSize += Align + Size;
00500     SymbolsToAllocate.push_back(Sym);
00501   }
00502 
00503   // Allocate memory for the section
00504   unsigned SectionID = Sections.size();
00505   uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, sizeof(void *),
00506                                              SectionID, StringRef(), false);
00507   if (!Addr)
00508     report_fatal_error("Unable to allocate memory for common symbols!");
00509   uint64_t Offset = 0;
00510   Sections.push_back(SectionEntry("<common symbols>", Addr, CommonSize, 0));
00511   memset(Addr, 0, CommonSize);
00512 
00513   DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
00514                << format("%p", Addr) << " DataSize: " << CommonSize << "\n");
00515 
00516   // Assign the address of each symbol
00517   for (auto &Sym : SymbolsToAllocate) {
00518     uint32_t Align = Sym.getAlignment();
00519     StringRef Name;
00520     uint64_t Size = Sym.getSize();
00521     Check(Sym.getName(Name));
00522     if (Align) {
00523       // This symbol has an alignment requirement.
00524       uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
00525       Addr += AlignOffset;
00526       Offset += AlignOffset;
00527     }
00528     uint32_t Flags = Sym.getFlags();
00529     JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
00530     if (Flags & SymbolRef::SF_Weak)
00531       RTDyldSymFlags |= JITSymbolFlags::Weak;
00532     if (Flags & SymbolRef::SF_Exported)
00533       RTDyldSymFlags |= JITSymbolFlags::Exported;
00534     DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
00535                  << format("%p", Addr) << "\n");
00536     GlobalSymbolTable[Name] =
00537       SymbolTableEntry(SectionID, Offset, RTDyldSymFlags);
00538     Offset += Size;
00539     Addr += Size;
00540   }
00541 }
00542 
00543 unsigned RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
00544                                       const SectionRef &Section, bool IsCode) {
00545 
00546   StringRef data;
00547   uint64_t Alignment64 = Section.getAlignment();
00548 
00549   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
00550   unsigned PaddingSize = 0;
00551   unsigned StubBufSize = 0;
00552   StringRef Name;
00553   bool IsRequired = isRequiredForExecution(Section);
00554   bool IsVirtual = Section.isVirtual();
00555   bool IsZeroInit = isZeroInit(Section);
00556   bool IsReadOnly = isReadOnlyData(Section);
00557   uint64_t DataSize = Section.getSize();
00558   Check(Section.getName(Name));
00559 
00560   StubBufSize = computeSectionStubBufSize(Obj, Section);
00561 
00562   // The .eh_frame section (at least on Linux) needs an extra four bytes padded
00563   // with zeroes added at the end.  For MachO objects, this section has a
00564   // slightly different name, so this won't have any effect for MachO objects.
00565   if (Name == ".eh_frame")
00566     PaddingSize = 4;
00567 
00568   uintptr_t Allocate;
00569   unsigned SectionID = Sections.size();
00570   uint8_t *Addr;
00571   const char *pData = nullptr;
00572 
00573   // In either case, set the location of the unrelocated section in memory,
00574   // since we still process relocations for it even if we're not applying them.
00575   Check(Section.getContents(data));
00576   // Virtual sections have no data in the object image, so leave pData = 0
00577   if (!IsVirtual)
00578     pData = data.data();
00579 
00580   // Some sections, such as debug info, don't need to be loaded for execution.
00581   // Leave those where they are.
00582   if (IsRequired) {
00583     Allocate = DataSize + PaddingSize + StubBufSize;
00584     if (!Allocate)
00585       Allocate = 1;
00586     Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
00587                                                Name)
00588                   : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
00589                                                Name, IsReadOnly);
00590     if (!Addr)
00591       report_fatal_error("Unable to allocate section memory!");
00592 
00593     // Zero-initialize or copy the data from the image
00594     if (IsZeroInit || IsVirtual)
00595       memset(Addr, 0, DataSize);
00596     else
00597       memcpy(Addr, pData, DataSize);
00598 
00599     // Fill in any extra bytes we allocated for padding
00600     if (PaddingSize != 0) {
00601       memset(Addr + DataSize, 0, PaddingSize);
00602       // Update the DataSize variable so that the stub offset is set correctly.
00603       DataSize += PaddingSize;
00604     }
00605 
00606     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
00607                  << " obj addr: " << format("%p", pData)
00608                  << " new addr: " << format("%p", Addr)
00609                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
00610                  << " Allocate: " << Allocate << "\n");
00611   } else {
00612     // Even if we didn't load the section, we need to record an entry for it
00613     // to handle later processing (and by 'handle' I mean don't do anything
00614     // with these sections).
00615     Allocate = 0;
00616     Addr = nullptr;
00617     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
00618                  << " obj addr: " << format("%p", data.data()) << " new addr: 0"
00619                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
00620                  << " Allocate: " << Allocate << "\n");
00621   }
00622 
00623   Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
00624 
00625   if (Checker)
00626     Checker->registerSection(Obj.getFileName(), SectionID);
00627 
00628   return SectionID;
00629 }
00630 
00631 unsigned RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
00632                                             const SectionRef &Section,
00633                                             bool IsCode,
00634                                             ObjSectionToIDMap &LocalSections) {
00635 
00636   unsigned SectionID = 0;
00637   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
00638   if (i != LocalSections.end())
00639     SectionID = i->second;
00640   else {
00641     SectionID = emitSection(Obj, Section, IsCode);
00642     LocalSections[Section] = SectionID;
00643   }
00644   return SectionID;
00645 }
00646 
00647 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
00648                                               unsigned SectionID) {
00649   Relocations[SectionID].push_back(RE);
00650 }
00651 
00652 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
00653                                              StringRef SymbolName) {
00654   // Relocation by symbol.  If the symbol is found in the global symbol table,
00655   // create an appropriate section relocation.  Otherwise, add it to
00656   // ExternalSymbolRelocations.
00657   RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
00658   if (Loc == GlobalSymbolTable.end()) {
00659     ExternalSymbolRelocations[SymbolName].push_back(RE);
00660   } else {
00661     // Copy the RE since we want to modify its addend.
00662     RelocationEntry RECopy = RE;
00663     const auto &SymInfo = Loc->second;
00664     RECopy.Addend += SymInfo.getOffset();
00665     Relocations[SymInfo.getSectionID()].push_back(RECopy);
00666   }
00667 }
00668 
00669 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
00670                                              unsigned AbiVariant) {
00671   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
00672     // This stub has to be able to access the full address space,
00673     // since symbol lookup won't necessarily find a handy, in-range,
00674     // PLT stub for functions which could be anywhere.
00675     // Stub can use ip0 (== x16) to calculate address
00676     writeBytesUnaligned(0xd2e00010, Addr,    4); // movz ip0, #:abs_g3:<addr>
00677     writeBytesUnaligned(0xf2c00010, Addr+4,  4); // movk ip0, #:abs_g2_nc:<addr>
00678     writeBytesUnaligned(0xf2a00010, Addr+8,  4); // movk ip0, #:abs_g1_nc:<addr>
00679     writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
00680     writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
00681 
00682     return Addr;
00683   } else if (Arch == Triple::arm || Arch == Triple::armeb) {
00684     // TODO: There is only ARM far stub now. We should add the Thumb stub,
00685     // and stubs for branches Thumb - ARM and ARM - Thumb.
00686     writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label>
00687     return Addr + 4;
00688   } else if (IsMipsO32ABI) {
00689     // 0:   3c190000        lui     t9,%hi(addr).
00690     // 4:   27390000        addiu   t9,t9,%lo(addr).
00691     // 8:   03200008        jr      t9.
00692     // c:   00000000        nop.
00693     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
00694     const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
00695 
00696     writeBytesUnaligned(LuiT9Instr, Addr, 4);
00697     writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
00698     writeBytesUnaligned(JrT9Instr, Addr+8, 4);
00699     writeBytesUnaligned(NopInstr, Addr+12, 4);
00700     return Addr;
00701   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
00702     // Depending on which version of the ELF ABI is in use, we need to
00703     // generate one of two variants of the stub.  They both start with
00704     // the same sequence to load the target address into r12.
00705     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
00706     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
00707     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
00708     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
00709     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
00710     if (AbiVariant == 2) {
00711       // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
00712       // The address is already in r12 as required by the ABI.  Branch to it.
00713       writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
00714       writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
00715       writeInt32BE(Addr+28, 0x4E800420); // bctr
00716     } else {
00717       // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
00718       // Load the function address on r11 and sets it to control register. Also
00719       // loads the function TOC in r2 and environment pointer to r11.
00720       writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
00721       writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
00722       writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
00723       writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
00724       writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
00725       writeInt32BE(Addr+40, 0x4E800420); // bctr
00726     }
00727     return Addr;
00728   } else if (Arch == Triple::systemz) {
00729     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
00730     writeInt16BE(Addr+2,  0x0000);
00731     writeInt16BE(Addr+4,  0x0004);
00732     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
00733     // 8-byte address stored at Addr + 8
00734     return Addr;
00735   } else if (Arch == Triple::x86_64) {
00736     *Addr      = 0xFF; // jmp
00737     *(Addr+1)  = 0x25; // rip
00738     // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
00739   } else if (Arch == Triple::x86) {
00740     *Addr      = 0xE9; // 32-bit pc-relative jump.
00741   }
00742   return Addr;
00743 }
00744 
00745 // Assign an address to a symbol name and resolve all the relocations
00746 // associated with it.
00747 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
00748                                              uint64_t Addr) {
00749   // The address to use for relocation resolution is not
00750   // the address of the local section buffer. We must be doing
00751   // a remote execution environment of some sort. Relocations can't
00752   // be applied until all the sections have been moved.  The client must
00753   // trigger this with a call to MCJIT::finalize() or
00754   // RuntimeDyld::resolveRelocations().
00755   //
00756   // Addr is a uint64_t because we can't assume the pointer width
00757   // of the target is the same as that of the host. Just use a generic
00758   // "big enough" type.
00759   DEBUG(dbgs() << "Reassigning address for section "
00760                << SectionID << " (" << Sections[SectionID].Name << "): "
00761                << format("0x%016" PRIx64, Sections[SectionID].LoadAddress) << " -> "
00762                << format("0x%016" PRIx64, Addr) << "\n");
00763   Sections[SectionID].LoadAddress = Addr;
00764 }
00765 
00766 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
00767                                             uint64_t Value) {
00768   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
00769     const RelocationEntry &RE = Relocs[i];
00770     // Ignore relocations for sections that were not loaded
00771     if (Sections[RE.SectionID].Address == nullptr)
00772       continue;
00773     resolveRelocation(RE, Value);
00774   }
00775 }
00776 
00777 void RuntimeDyldImpl::resolveExternalSymbols() {
00778   while (!ExternalSymbolRelocations.empty()) {
00779     StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
00780 
00781     StringRef Name = i->first();
00782     if (Name.size() == 0) {
00783       // This is an absolute symbol, use an address of zero.
00784       DEBUG(dbgs() << "Resolving absolute relocations."
00785                    << "\n");
00786       RelocationList &Relocs = i->second;
00787       resolveRelocationList(Relocs, 0);
00788     } else {
00789       uint64_t Addr = 0;
00790       RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
00791       if (Loc == GlobalSymbolTable.end()) {
00792         // This is an external symbol, try to get its address from the symbol
00793         // resolver.
00794         Addr = Resolver.findSymbol(Name.data()).getAddress();
00795         // The call to getSymbolAddress may have caused additional modules to
00796         // be loaded, which may have added new entries to the
00797         // ExternalSymbolRelocations map.  Consquently, we need to update our
00798         // iterator.  This is also why retrieval of the relocation list
00799         // associated with this symbol is deferred until below this point.
00800         // New entries may have been added to the relocation list.
00801         i = ExternalSymbolRelocations.find(Name);
00802       } else {
00803         // We found the symbol in our global table.  It was probably in a
00804         // Module that we loaded previously.
00805         const auto &SymInfo = Loc->second;
00806         Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
00807                SymInfo.getOffset();
00808       }
00809 
00810       // FIXME: Implement error handling that doesn't kill the host program!
00811       if (!Addr)
00812         report_fatal_error("Program used external function '" + Name +
00813                            "' which could not be resolved!");
00814 
00815       DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
00816                    << format("0x%lx", Addr) << "\n");
00817       // This list may have been updated when we called getSymbolAddress, so
00818       // don't change this code to get the list earlier.
00819       RelocationList &Relocs = i->second;
00820       resolveRelocationList(Relocs, Addr);
00821     }
00822 
00823     ExternalSymbolRelocations.erase(i);
00824   }
00825 }
00826 
00827 //===----------------------------------------------------------------------===//
00828 // RuntimeDyld class implementation
00829 
00830 uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
00831                                                   StringRef SectionName) const {
00832   for (unsigned I = BeginIdx; I != EndIdx; ++I)
00833     if (RTDyld.Sections[I].Name == SectionName)
00834       return RTDyld.Sections[I].LoadAddress;
00835 
00836   return 0;
00837 }
00838 
00839 void RuntimeDyld::MemoryManager::anchor() {}
00840 void RuntimeDyld::SymbolResolver::anchor() {}
00841 
00842 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
00843                          RuntimeDyld::SymbolResolver &Resolver)
00844     : MemMgr(MemMgr), Resolver(Resolver) {
00845   // FIXME: There's a potential issue lurking here if a single instance of
00846   // RuntimeDyld is used to load multiple objects.  The current implementation
00847   // associates a single memory manager with a RuntimeDyld instance.  Even
00848   // though the public class spawns a new 'impl' instance for each load,
00849   // they share a single memory manager.  This can become a problem when page
00850   // permissions are applied.
00851   Dyld = nullptr;
00852   ProcessAllSections = false;
00853   Checker = nullptr;
00854 }
00855 
00856 RuntimeDyld::~RuntimeDyld() {}
00857 
00858 static std::unique_ptr<RuntimeDyldCOFF>
00859 createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
00860                       RuntimeDyld::SymbolResolver &Resolver,
00861                       bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
00862   std::unique_ptr<RuntimeDyldCOFF> Dyld =
00863     RuntimeDyldCOFF::create(Arch, MM, Resolver);
00864   Dyld->setProcessAllSections(ProcessAllSections);
00865   Dyld->setRuntimeDyldChecker(Checker);
00866   return Dyld;
00867 }
00868 
00869 static std::unique_ptr<RuntimeDyldELF>
00870 createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
00871                      RuntimeDyld::SymbolResolver &Resolver,
00872                      bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
00873   std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM, Resolver));
00874   Dyld->setProcessAllSections(ProcessAllSections);
00875   Dyld->setRuntimeDyldChecker(Checker);
00876   return Dyld;
00877 }
00878 
00879 static std::unique_ptr<RuntimeDyldMachO>
00880 createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
00881                        RuntimeDyld::SymbolResolver &Resolver,
00882                        bool ProcessAllSections,
00883                        RuntimeDyldCheckerImpl *Checker) {
00884   std::unique_ptr<RuntimeDyldMachO> Dyld =
00885     RuntimeDyldMachO::create(Arch, MM, Resolver);
00886   Dyld->setProcessAllSections(ProcessAllSections);
00887   Dyld->setRuntimeDyldChecker(Checker);
00888   return Dyld;
00889 }
00890 
00891 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
00892 RuntimeDyld::loadObject(const ObjectFile &Obj) {
00893   if (!Dyld) {
00894     if (Obj.isELF())
00895       Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker);
00896     else if (Obj.isMachO())
00897       Dyld = createRuntimeDyldMachO(
00898                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
00899                ProcessAllSections, Checker);
00900     else if (Obj.isCOFF())
00901       Dyld = createRuntimeDyldCOFF(
00902                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
00903                ProcessAllSections, Checker);
00904     else
00905       report_fatal_error("Incompatible object format!");
00906   }
00907 
00908   if (!Dyld->isCompatibleFile(Obj))
00909     report_fatal_error("Incompatible object format!");
00910 
00911   return Dyld->loadObject(Obj);
00912 }
00913 
00914 void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
00915   if (!Dyld)
00916     return nullptr;
00917   return Dyld->getSymbolLocalAddress(Name);
00918 }
00919 
00920 RuntimeDyld::SymbolInfo RuntimeDyld::getSymbol(StringRef Name) const {
00921   if (!Dyld)
00922     return nullptr;
00923   return Dyld->getSymbol(Name);
00924 }
00925 
00926 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
00927 
00928 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
00929   Dyld->reassignSectionAddress(SectionID, Addr);
00930 }
00931 
00932 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
00933                                     uint64_t TargetAddress) {
00934   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
00935 }
00936 
00937 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
00938 
00939 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
00940 
00941 void RuntimeDyld::registerEHFrames() {
00942   if (Dyld)
00943     Dyld->registerEHFrames();
00944 }
00945 
00946 void RuntimeDyld::deregisterEHFrames() {
00947   if (Dyld)
00948     Dyld->deregisterEHFrames();
00949 }
00950 
00951 } // end namespace llvm