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