LCOV - code coverage report
Current view: top level - lib/ExecutionEngine/RuntimeDyld - RuntimeDyld.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 468 558 83.9 %
Date: 2017-09-14 15:23:50 Functions: 35 49 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Implementation of the MC-JIT runtime dynamic linker.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/ExecutionEngine/RuntimeDyld.h"
      15             : #include "RuntimeDyldCOFF.h"
      16             : #include "RuntimeDyldCheckerImpl.h"
      17             : #include "RuntimeDyldELF.h"
      18             : #include "RuntimeDyldImpl.h"
      19             : #include "RuntimeDyldMachO.h"
      20             : #include "llvm/Object/COFF.h"
      21             : #include "llvm/Object/ELFObjectFile.h"
      22             : #include "llvm/Support/ManagedStatic.h"
      23             : #include "llvm/Support/MathExtras.h"
      24             : #include "llvm/Support/MutexGuard.h"
      25             : 
      26             : using namespace llvm;
      27             : using namespace llvm::object;
      28             : 
      29             : #define DEBUG_TYPE "dyld"
      30             : 
      31             : namespace {
      32             : 
      33             : enum RuntimeDyldErrorCode {
      34             :   GenericRTDyldError = 1
      35             : };
      36             : 
      37             : // FIXME: This class is only here to support the transition to llvm::Error. It
      38             : // will be removed once this transition is complete. Clients should prefer to
      39             : // deal with the Error value directly, rather than converting to error_code.
      40           0 : class RuntimeDyldErrorCategory : public std::error_category {
      41             : public:
      42           0 :   const char *name() const noexcept override { return "runtimedyld"; }
      43             : 
      44           0 :   std::string message(int Condition) const override {
      45           0 :     switch (static_cast<RuntimeDyldErrorCode>(Condition)) {
      46           0 :       case GenericRTDyldError: return "Generic RuntimeDyld error";
      47             :     }
      48           0 :     llvm_unreachable("Unrecognized RuntimeDyldErrorCode");
      49             :   }
      50             : };
      51             : 
      52             : static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;
      53             : 
      54             : }
      55             : 
      56             : char RuntimeDyldError::ID = 0;
      57             : 
      58           0 : void RuntimeDyldError::log(raw_ostream &OS) const {
      59           0 :   OS << ErrMsg << "\n";
      60           0 : }
      61             : 
      62           0 : std::error_code RuntimeDyldError::convertToErrorCode() const {
      63           0 :   return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
      64             : }
      65             : 
      66             : // Empty out-of-line virtual destructor as the key function.
      67        1100 : RuntimeDyldImpl::~RuntimeDyldImpl() {}
      68             : 
      69             : // Pin LoadedObjectInfo's vtables to this file.
      70           0 : void RuntimeDyld::LoadedObjectInfo::anchor() {}
      71             : 
      72             : namespace llvm {
      73             : 
      74           0 : void RuntimeDyldImpl::registerEHFrames() {}
      75             : 
      76          46 : void RuntimeDyldImpl::deregisterEHFrames() {
      77          46 :   MemMgr.deregisterEHFrames();
      78          46 : }
      79             : 
      80             : #ifndef NDEBUG
      81             : static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
      82             :   dbgs() << "----- Contents of section " << S.getName() << " " << State
      83             :          << " -----";
      84             : 
      85             :   if (S.getAddress() == nullptr) {
      86             :     dbgs() << "\n          <section not emitted>\n";
      87             :     return;
      88             :   }
      89             : 
      90             :   const unsigned ColsPerRow = 16;
      91             : 
      92             :   uint8_t *DataAddr = S.getAddress();
      93             :   uint64_t LoadAddr = S.getLoadAddress();
      94             : 
      95             :   unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
      96             :   unsigned BytesRemaining = S.getSize();
      97             : 
      98             :   if (StartPadding) {
      99             :     dbgs() << "\n" << format("0x%016" PRIx64,
     100             :                              LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
     101             :     while (StartPadding--)
     102             :       dbgs() << "   ";
     103             :   }
     104             : 
     105             :   while (BytesRemaining > 0) {
     106             :     if ((LoadAddr & (ColsPerRow - 1)) == 0)
     107             :       dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
     108             : 
     109             :     dbgs() << " " << format("%02x", *DataAddr);
     110             : 
     111             :     ++DataAddr;
     112             :     ++LoadAddr;
     113             :     --BytesRemaining;
     114             :   }
     115             : 
     116             :   dbgs() << "\n";
     117             : }
     118             : #endif
     119             : 
     120             : // Resolve the relocations for all symbols we currently know about.
     121         469 : void RuntimeDyldImpl::resolveRelocations() {
     122        1407 :   MutexGuard locked(lock);
     123             : 
     124             :   // Print out the sections prior to relocation.
     125             :   DEBUG(
     126             :     for (int i = 0, e = Sections.size(); i != e; ++i)
     127             :       dumpSectionMemory(Sections[i], "before relocations");
     128             :   );
     129             : 
     130             :   // First, resolve relocations associated with external symbols.
     131        1407 :   if (auto Err = resolveExternalSymbols()) {
     132           0 :     HasError = true;
     133           0 :     ErrorStr = toString(std::move(Err));
     134             :   }
     135             : 
     136             :   // Iterate over all outstanding relocations
     137        1407 :   for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
     138             :     // The Section here (Sections[i]) refers to the section in which the
     139             :     // symbol for the relocation is located.  The SectionID in the relocation
     140             :     // entry provides the section to which the relocation will be applied.
     141         508 :     int Idx = it->first;
     142        1016 :     uint64_t Addr = Sections[Idx].getLoadAddress();
     143             :     DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
     144             :                  << format("%p", (uintptr_t)Addr) << "\n");
     145         508 :     resolveRelocationList(it->second, Addr);
     146             :   }
     147         938 :   Relocations.clear();
     148             : 
     149             :   // Print out sections after relocation.
     150             :   DEBUG(
     151             :     for (int i = 0, e = Sections.size(); i != e; ++i)
     152             :       dumpSectionMemory(Sections[i], "after relocations");
     153             :   );
     154             : 
     155         469 : }
     156             : 
     157         194 : void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
     158             :                                         uint64_t TargetAddress) {
     159         582 :   MutexGuard locked(lock);
     160         701 :   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
     161        1014 :     if (Sections[i].getAddress() == LocalAddress) {
     162         194 :       reassignSectionAddress(i, TargetAddress);
     163         194 :       return;
     164             :     }
     165             :   }
     166           0 :   llvm_unreachable("Attempting to remap address of unknown section!");
     167             : }
     168             : 
     169         802 : static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
     170             :                        uint64_t &Result) {
     171        1604 :   Expected<uint64_t> AddressOrErr = Sym.getAddress();
     172         802 :   if (!AddressOrErr)
     173             :     return AddressOrErr.takeError();
     174        1604 :   Result = *AddressOrErr - Sec.getAddress();
     175        2406 :   return Error::success();
     176             : }
     177             : 
     178             : Expected<RuntimeDyldImpl::ObjSectionToIDMap>
     179         339 : RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
     180        1017 :   MutexGuard locked(lock);
     181             : 
     182             :   // Save information about our target
     183         339 :   Arch = (Triple::ArchType)Obj.getArch();
     184         678 :   IsTargetLittleEndian = Obj.isLittleEndian();
     185         339 :   setMipsABI(Obj);
     186             : 
     187             :   // Compute the memory size required to load all sections to be loaded
     188             :   // and pass this information to the memory manager
     189         339 :   if (MemMgr.needsToReserveAllocationSpace()) {
     190          31 :     uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
     191          31 :     uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1;
     192          31 :     if (auto Err = computeTotalAllocSize(Obj,
     193             :                                          CodeSize, CodeAlign,
     194             :                                          RODataSize, RODataAlign,
     195          93 :                                          RWDataSize, RWDataAlign))
     196           0 :       return std::move(Err);
     197          62 :     MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
     198          31 :                                   RWDataSize, RWDataAlign);
     199             :   }
     200             : 
     201             :   // Used sections from the object file
     202         339 :   ObjSectionToIDMap LocalSections;
     203             : 
     204             :   // Common symbols requiring allocation, with their sizes and alignments
     205         678 :   CommonSymbolList CommonSymbols;
     206             : 
     207             :   // Parse symbols
     208             :   DEBUG(dbgs() << "Parse symbols:\n");
     209        2792 :   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
     210             :        ++I) {
     211        4228 :     uint32_t Flags = I->getFlags();
     212             : 
     213             :     // Skip undefined symbols.
     214        2114 :     if (Flags & SymbolRef::SF_Undefined)
     215         532 :       continue;
     216             : 
     217        1582 :     if (Flags & SymbolRef::SF_Common)
     218          45 :       CommonSymbols.push_back(*I);
     219             :     else {
     220             : 
     221             :       // Get the symbol type.
     222             :       object::SymbolRef::Type SymType;
     223        6148 :       if (auto SymTypeOrErr = I->getType())
     224        1537 :         SymType =  *SymTypeOrErr;
     225             :       else
     226           0 :         return SymTypeOrErr.takeError();
     227             : 
     228             :       // Get symbol name.
     229        1537 :       StringRef Name;
     230        6148 :       if (auto NameOrErr = I->getName())
     231        1537 :         Name = *NameOrErr;
     232             :       else
     233           0 :         return NameOrErr.takeError();
     234             : 
     235             :       // Compute JIT symbol flags.
     236        3074 :       JITSymbolFlags JITSymFlags = getJITSymbolFlags(*I);
     237             : 
     238             :       // If this is a weak definition, check to see if there's a strong one.
     239             :       // If there is, skip this symbol (we won't be providing it: the strong
     240             :       // definition will). If there's no strong definition, make this definition
     241             :       // strong.
     242        1537 :       if (JITSymFlags.isWeak()) {
     243             :         // First check whether there's already a definition in this instance.
     244             :         // FIXME: Override existing weak definitions with strong ones.
     245          22 :         if (GlobalSymbolTable.count(Name))
     246           4 :           continue;
     247             :         // Then check the symbol resolver to see if there's a definition
     248             :         // elsewhere in this logical dylib.
     249          38 :         if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) {
     250           6 :           if (Sym.getFlags().isStrongDefinition())
     251           2 :             continue;
     252          12 :         } else if (auto Err = Sym.takeError())
     253           0 :           return std::move(Err);
     254             :         // else
     255           8 :         JITSymFlags &= ~JITSymbolFlags::Weak;
     256             :       }
     257             : 
     258        1534 :       if (Flags & SymbolRef::SF_Absolute &&
     259             :           SymType != object::SymbolRef::ST_File) {
     260           4 :         uint64_t Addr = 0;
     261          16 :         if (auto AddrOrErr = I->getAddress())
     262           4 :           Addr = *AddrOrErr;
     263             :         else
     264           0 :           return AddrOrErr.takeError();
     265             : 
     266           4 :         unsigned SectionID = AbsoluteSymbolSection;
     267             : 
     268             :         DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name
     269             :                      << " SID: " << SectionID << " Offset: "
     270             :                      << format("%p", (uintptr_t)Addr)
     271             :                      << " flags: " << Flags << "\n");
     272           8 :         GlobalSymbolTable[Name] =
     273           8 :           SymbolTableEntry(SectionID, Addr, JITSymFlags);
     274        3060 :       } else if (SymType == object::SymbolRef::ST_Function ||
     275        1530 :                  SymType == object::SymbolRef::ST_Data ||
     276        1742 :                  SymType == object::SymbolRef::ST_Unknown ||
     277         871 :                  SymType == object::SymbolRef::ST_Other) {
     278             : 
     279         802 :         section_iterator SI = Obj.section_end();
     280        3208 :         if (auto SIOrErr = I->getSection())
     281         802 :           SI = *SIOrErr;
     282             :         else
     283           0 :           return SIOrErr.takeError();
     284             : 
     285        1604 :         if (SI == Obj.section_end())
     286           0 :           continue;
     287             : 
     288             :         // Get symbol offset.
     289             :         uint64_t SectOffset;
     290        3208 :         if (auto Err = getOffset(*I, *SI, SectOffset))
     291           0 :           return std::move(Err);
     292             : 
     293        1604 :         bool IsCode = SI->isText();
     294             :         unsigned SectionID;
     295         802 :         if (auto SectionIDOrErr = findOrEmitSection(Obj, *SI, IsCode,
     296        2406 :                                                     LocalSections))
     297         802 :           SectionID = *SectionIDOrErr;
     298             :         else
     299           0 :           return SectionIDOrErr.takeError();
     300             : 
     301             :         DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
     302             :                      << " SID: " << SectionID << " Offset: "
     303             :                      << format("%p", (uintptr_t)SectOffset)
     304             :                      << " flags: " << Flags << "\n");
     305        1604 :         GlobalSymbolTable[Name] =
     306         802 :           SymbolTableEntry(SectionID, SectOffset, JITSymFlags);
     307             :       }
     308             :     }
     309             :   }
     310             : 
     311             :   // Allocate common symbols
     312        1017 :   if (auto Err = emitCommonSymbols(Obj, CommonSymbols))
     313           0 :     return std::move(Err);
     314             : 
     315             :   // Parse and process relocations
     316             :   DEBUG(dbgs() << "Parse relocations:\n");
     317         339 :   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
     318        3194 :        SI != SE; ++SI) {
     319        3439 :     StubMap Stubs;
     320        5710 :     section_iterator RelocatedSection = SI->getRelocatedSection();
     321             : 
     322        2855 :     if (RelocatedSection == SE)
     323        4512 :       continue;
     324             : 
     325        1228 :     relocation_iterator I = SI->relocation_begin();
     326        1228 :     relocation_iterator E = SI->relocation_end();
     327             : 
     328         644 :     if (I == E && !ProcessAllSections)
     329          30 :       continue;
     330             : 
     331        1168 :     bool IsCode = RelocatedSection->isText();
     332         584 :     unsigned SectionID = 0;
     333         584 :     if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
     334        1752 :                                                 LocalSections))
     335         584 :       SectionID = *SectionIDOrErr;
     336             :     else
     337           0 :       return SectionIDOrErr.takeError();
     338             : 
     339             :     DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
     340             : 
     341        1945 :     for (; I != E;)
     342        4083 :       if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
     343        1361 :         I = *IOrErr;
     344             :       else
     345           0 :         return IOrErr.takeError();
     346             : 
     347             :     // If there is an attached checker, notify it about the stubs for this
     348             :     // section so that they can be verified.
     349         584 :     if (Checker)
     350          98 :       Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
     351             :   }
     352             : 
     353             :   // Give the subclasses a chance to tie-up any loose ends.
     354        1017 :   if (auto Err = finalizeLoad(Obj, LocalSections))
     355           0 :     return std::move(Err);
     356             : 
     357             : //   for (auto E : LocalSections)
     358             : //     llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
     359             : 
     360             :   return LocalSections;
     361             : }
     362             : 
     363             : // A helper method for computeTotalAllocSize.
     364             : // Computes the memory size required to allocate sections with the given sizes,
     365             : // assuming that all sections are allocated with the given alignment
     366             : static uint64_t
     367             : computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
     368             :                                  uint64_t Alignment) {
     369          93 :   uint64_t TotalSize = 0;
     370         253 :   for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
     371             :     uint64_t AlignedSize =
     372         134 :         (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
     373          67 :     TotalSize += AlignedSize;
     374             :   }
     375             :   return TotalSize;
     376             : }
     377             : 
     378        1078 : static bool isRequiredForExecution(const SectionRef Section) {
     379        1078 :   const ObjectFile *Obj = Section.getObject();
     380        2156 :   if (isa<object::ELFObjectFileBase>(Obj))
     381        2048 :     return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
     382          67 :   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
     383          13 :     const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
     384             :     // Avoid loading zero-sized COFF sections.
     385             :     // In PE files, VirtualSize gives the section size, and SizeOfRawData
     386             :     // may be zero for sections with content. In Obj files, SizeOfRawData
     387             :     // gives the section size, and VirtualSize is always zero. Hence
     388             :     // the need to check for both cases below.
     389             :     bool HasContent =
     390          39 :         (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0);
     391             :     bool IsDiscardable =
     392          13 :         CoffSection->Characteristics &
     393          39 :         (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
     394          13 :     return HasContent && !IsDiscardable;
     395             :   }
     396             : 
     397             :   assert(isa<MachOObjectFile>(Obj));
     398             :   return true;
     399             : }
     400             : 
     401         914 : static bool isReadOnlyData(const SectionRef Section) {
     402         914 :   const ObjectFile *Obj = Section.getObject();
     403        1828 :   if (isa<object::ELFObjectFileBase>(Obj))
     404        2580 :     return !(ELFSectionRef(Section).getFlags() &
     405         860 :              (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
     406          67 :   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
     407          26 :     return ((COFFObj->getCOFFSection(Section)->Characteristics &
     408             :              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
     409             :              | COFF::IMAGE_SCN_MEM_READ
     410          26 :              | COFF::IMAGE_SCN_MEM_WRITE))
     411             :              ==
     412             :              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
     413          13 :              | COFF::IMAGE_SCN_MEM_READ));
     414             : 
     415             :   assert(isa<MachOObjectFile>(Obj));
     416             :   return false;
     417             : }
     418             : 
     419         855 : static bool isZeroInit(const SectionRef Section) {
     420         855 :   const ObjectFile *Obj = Section.getObject();
     421        1710 :   if (isa<object::ELFObjectFileBase>(Obj))
     422        1602 :     return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS;
     423          67 :   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
     424          26 :     return COFFObj->getCOFFSection(Section)->Characteristics &
     425          39 :             COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
     426             : 
     427          82 :   auto *MachO = cast<MachOObjectFile>(Obj);
     428          41 :   unsigned SectionType = MachO->getSectionType(Section);
     429          41 :   return SectionType == MachO::S_ZEROFILL ||
     430          41 :          SectionType == MachO::S_GB_ZEROFILL;
     431             : }
     432             : 
     433             : // Compute an upper bound of the memory size that is required to load all
     434             : // sections
     435          31 : Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
     436             :                                              uint64_t &CodeSize,
     437             :                                              uint32_t &CodeAlign,
     438             :                                              uint64_t &RODataSize,
     439             :                                              uint32_t &RODataAlign,
     440             :                                              uint64_t &RWDataSize,
     441             :                                              uint32_t &RWDataAlign) {
     442             :   // Compute the size of all sections required for execution
     443          62 :   std::vector<uint64_t> CodeSectionSizes;
     444          62 :   std::vector<uint64_t> ROSectionSizes;
     445          62 :   std::vector<uint64_t> RWSectionSizes;
     446             : 
     447             :   // Collect sizes of all sections to be loaded;
     448             :   // also determine the max alignment of all sections
     449          31 :   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
     450         254 :        SI != SE; ++SI) {
     451         223 :     const SectionRef &Section = *SI;
     452             : 
     453         223 :     bool IsRequired = isRequiredForExecution(Section) || ProcessAllSections;
     454             : 
     455             :     // Consider only the sections that are required to be loaded for execution
     456             :     if (IsRequired) {
     457          59 :       uint64_t DataSize = Section.getSize();
     458          59 :       uint64_t Alignment64 = Section.getAlignment();
     459          59 :       unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
     460          59 :       bool IsCode = Section.isText();
     461          59 :       bool IsReadOnly = isReadOnlyData(Section);
     462             : 
     463          59 :       StringRef Name;
     464          59 :       if (auto EC = Section.getName(Name))
     465           0 :         return errorCodeToError(EC);
     466             : 
     467          59 :       uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
     468          59 :       uint64_t SectionSize = DataSize + StubBufSize;
     469             : 
     470             :       // The .eh_frame section (at least on Linux) needs an extra four bytes
     471             :       // padded
     472             :       // with zeroes added at the end.  For MachO objects, this section has a
     473             :       // slightly different name, so this won't have any effect for MachO
     474             :       // objects.
     475          64 :       if (Name == ".eh_frame")
     476           5 :         SectionSize += 4;
     477             : 
     478          59 :       if (!SectionSize)
     479           0 :         SectionSize = 1;
     480             : 
     481          59 :       if (IsCode) {
     482          31 :         CodeAlign = std::max(CodeAlign, Alignment);
     483          31 :         CodeSectionSizes.push_back(SectionSize);
     484          28 :       } else if (IsReadOnly) {
     485          17 :         RODataAlign = std::max(RODataAlign, Alignment);
     486          17 :         ROSectionSizes.push_back(SectionSize);
     487             :       } else {
     488          11 :         RWDataAlign = std::max(RWDataAlign, Alignment);
     489          11 :         RWSectionSizes.push_back(SectionSize);
     490             :       }
     491             :     }
     492             :   }
     493             : 
     494             :   // Compute Global Offset Table size. If it is not zero we
     495             :   // also update alignment, which is equal to a size of a
     496             :   // single GOT entry.
     497          31 :   if (unsigned GotSize = computeGOTSize(Obj)) {
     498           8 :     RWSectionSizes.push_back(GotSize);
     499           8 :     RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize());
     500             :   }
     501             : 
     502             :   // Compute the size of all common symbols
     503          31 :   uint64_t CommonSize = 0;
     504          31 :   uint32_t CommonAlign = 1;
     505         236 :   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
     506             :        ++I) {
     507         348 :     uint32_t Flags = I->getFlags();
     508         174 :     if (Flags & SymbolRef::SF_Common) {
     509             :       // Add the common symbols to a list.  We'll allocate them all below.
     510          16 :       uint64_t Size = I->getCommonSize();
     511          16 :       uint32_t Align = I->getAlignment();
     512             :       // If this is the first common symbol, use its alignment as the alignment
     513             :       // for the common symbols section.
     514           8 :       if (CommonSize == 0)
     515           4 :         CommonAlign = Align;
     516          16 :       CommonSize = alignTo(CommonSize, Align) + Size;
     517             :     }
     518             :   }
     519          31 :   if (CommonSize != 0) {
     520           4 :     RWSectionSizes.push_back(CommonSize);
     521           4 :     RWDataAlign = std::max(RWDataAlign, CommonAlign);
     522             :   }
     523             : 
     524             :   // Compute the required allocation space for each different type of sections
     525             :   // (code, read-only data, read-write data) assuming that all sections are
     526             :   // allocated with the max alignment. Note that we cannot compute with the
     527             :   // individual alignments of the sections, because then the required size
     528             :   // depends on the order, in which the sections are allocated.
     529          62 :   CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign);
     530          62 :   RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign);
     531          62 :   RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign);
     532             : 
     533          93 :   return Error::success();
     534             : }
     535             : 
     536             : // compute GOT size
     537          31 : unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile &Obj) {
     538          31 :   size_t GotEntrySize = getGOTEntrySize();
     539          31 :   if (!GotEntrySize)
     540             :     return 0;
     541             : 
     542          31 :   size_t GotSize = 0;
     543          31 :   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
     544         254 :        SI != SE; ++SI) {
     545             : 
     546         990 :     for (const RelocationRef &Reloc : SI->relocations())
     547          98 :       if (relocationNeedsGot(Reloc))
     548           6 :         GotSize += GotEntrySize;
     549             :   }
     550             : 
     551          31 :   return GotSize;
     552             : }
     553             : 
     554             : // compute stub buffer size for the given section
     555         914 : unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
     556             :                                                     const SectionRef &Section) {
     557         914 :   unsigned StubSize = getMaxStubSize();
     558         914 :   if (StubSize == 0) {
     559             :     return 0;
     560             :   }
     561             :   // FIXME: this is an inefficient way to handle this. We should computed the
     562             :   // necessary section allocation size in loadObject by walking all the sections
     563             :   // once.
     564         869 :   unsigned StubBufSize = 0;
     565         869 :   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
     566        9470 :        SI != SE; ++SI) {
     567       17202 :     section_iterator RelSecI = SI->getRelocatedSection();
     568       17202 :     if (!(RelSecI == Section))
     569        8005 :       continue;
     570             : 
     571        3778 :     for (const RelocationRef &Reloc : SI->relocations())
     572        1394 :       if (relocationNeedsStub(Reloc))
     573         391 :         StubBufSize += StubSize;
     574             :   }
     575             : 
     576             :   // Get section data size and alignment
     577         869 :   uint64_t DataSize = Section.getSize();
     578         869 :   uint64_t Alignment64 = Section.getAlignment();
     579             : 
     580             :   // Add stubbuf size alignment
     581         869 :   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
     582         869 :   unsigned StubAlignment = getStubAlignment();
     583         869 :   unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
     584         869 :   if (StubAlignment > EndAlignment)
     585           4 :     StubBufSize += StubAlignment - EndAlignment;
     586             :   return StubBufSize;
     587             : }
     588             : 
     589         359 : uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
     590             :                                              unsigned Size) const {
     591         359 :   uint64_t Result = 0;
     592         359 :   if (IsTargetLittleEndian) {
     593         258 :     Src += Size - 1;
     594        2690 :     while (Size--)
     595        1216 :       Result = (Result << 8) | *Src--;
     596             :   } else
     597         973 :     while (Size--)
     598         436 :       Result = (Result << 8) | *Src++;
     599             : 
     600         359 :   return Result;
     601             : }
     602             : 
     603         219 : void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
     604             :                                           unsigned Size) const {
     605         219 :   if (IsTargetLittleEndian) {
     606        1642 :     while (Size--) {
     607         744 :       *Dst++ = Value & 0xFF;
     608         744 :       Value >>= 8;
     609             :     }
     610             :   } else {
     611          65 :     Dst += Size - 1;
     612         633 :     while (Size--) {
     613         284 :       *Dst-- = Value & 0xFF;
     614         284 :       Value >>= 8;
     615             :     }
     616             :   }
     617         219 : }
     618             : 
     619        1582 : JITSymbolFlags RuntimeDyldImpl::getJITSymbolFlags(const BasicSymbolRef &SR) {
     620        1582 :   return JITSymbolFlags::fromObjectSymbol(SR);
     621             : }
     622             : 
     623         339 : Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
     624             :                                          CommonSymbolList &CommonSymbols) {
     625         339 :   if (CommonSymbols.empty())
     626         942 :     return Error::success();
     627             : 
     628          25 :   uint64_t CommonSize = 0;
     629          50 :   uint32_t CommonAlign = CommonSymbols.begin()->getAlignment();
     630          25 :   CommonSymbolList SymbolsToAllocate;
     631             : 
     632             :   DEBUG(dbgs() << "Processing common symbols...\n");
     633             : 
     634         145 :   for (const auto &Sym : CommonSymbols) {
     635          45 :     StringRef Name;
     636         135 :     if (auto NameOrErr = Sym.getName())
     637          45 :       Name = *NameOrErr;
     638             :     else
     639           0 :       return NameOrErr.takeError();
     640             : 
     641             :     // Skip common symbols already elsewhere.
     642          90 :     if (GlobalSymbolTable.count(Name)) {
     643             :       DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
     644             :                    << "'\n");
     645           0 :       continue;
     646             :     }
     647             : 
     648         180 :     if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) {
     649           0 :       if (!Sym.getFlags().isCommon()) {
     650             :         DEBUG(dbgs() << "\tSkipping common symbol '" << Name
     651             :                      << "' in favor of stronger definition.\n");
     652           0 :         continue;
     653             :       }
     654             :     }
     655          45 :     uint32_t Align = Sym.getAlignment();
     656          45 :     uint64_t Size = Sym.getCommonSize();
     657             : 
     658          90 :     CommonSize = alignTo(CommonSize, Align) + Size;
     659             : 
     660          45 :     SymbolsToAllocate.push_back(Sym);
     661             :   }
     662             : 
     663             :   // Allocate memory for the section
     664          50 :   unsigned SectionID = Sections.size();
     665          75 :   uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
     666          50 :                                              "<common symbols>", false);
     667          25 :   if (!Addr)
     668           0 :     report_fatal_error("Unable to allocate memory for common symbols!");
     669          25 :   uint64_t Offset = 0;
     670          50 :   Sections.push_back(
     671          75 :       SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0));
     672          25 :   memset(Addr, 0, CommonSize);
     673             : 
     674             :   DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
     675             :                << format("%p", Addr) << " DataSize: " << CommonSize << "\n");
     676             : 
     677             :   // Assign the address of each symbol
     678         145 :   for (auto &Sym : SymbolsToAllocate) {
     679          45 :     uint32_t Align = Sym.getAlignment();
     680          45 :     uint64_t Size = Sym.getCommonSize();
     681          45 :     StringRef Name;
     682         135 :     if (auto NameOrErr = Sym.getName())
     683          45 :       Name = *NameOrErr;
     684             :     else
     685           0 :       return NameOrErr.takeError();
     686          45 :     if (Align) {
     687             :       // This symbol has an alignment requirement.
     688          90 :       uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
     689          45 :       Addr += AlignOffset;
     690          45 :       Offset += AlignOffset;
     691             :     }
     692          45 :     JITSymbolFlags JITSymFlags = getJITSymbolFlags(Sym);
     693             :     DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
     694             :                  << format("%p", Addr) << "\n");
     695          90 :     GlobalSymbolTable[Name] =
     696          45 :       SymbolTableEntry(SectionID, Offset, JITSymFlags);
     697          45 :     Offset += Size;
     698          45 :     Addr += Size;
     699             :   }
     700             : 
     701          25 :   if (Checker)
     702          10 :     Checker->registerSection(Obj.getFileName(), SectionID);
     703             : 
     704          75 :   return Error::success();
     705             : }
     706             : 
     707             : Expected<unsigned>
     708         855 : RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
     709             :                              const SectionRef &Section,
     710             :                              bool IsCode) {
     711         855 :   StringRef data;
     712         855 :   uint64_t Alignment64 = Section.getAlignment();
     713             : 
     714         855 :   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
     715         855 :   unsigned PaddingSize = 0;
     716         855 :   unsigned StubBufSize = 0;
     717         855 :   bool IsRequired = isRequiredForExecution(Section);
     718         855 :   bool IsVirtual = Section.isVirtual();
     719         855 :   bool IsZeroInit = isZeroInit(Section);
     720         855 :   bool IsReadOnly = isReadOnlyData(Section);
     721         855 :   uint64_t DataSize = Section.getSize();
     722             : 
     723         855 :   StringRef Name;
     724         855 :   if (auto EC = Section.getName(Name))
     725           0 :     return errorCodeToError(EC);
     726             : 
     727         855 :   StubBufSize = computeSectionStubBufSize(Obj, Section);
     728             : 
     729             :   // The .eh_frame section (at least on Linux) needs an extra four bytes padded
     730             :   // with zeroes added at the end.  For MachO objects, this section has a
     731             :   // slightly different name, so this won't have any effect for MachO objects.
     732        1091 :   if (Name == ".eh_frame")
     733             :     PaddingSize = 4;
     734             : 
     735             :   uintptr_t Allocate;
     736        1710 :   unsigned SectionID = Sections.size();
     737             :   uint8_t *Addr;
     738         855 :   const char *pData = nullptr;
     739             : 
     740             :   // If this section contains any bits (i.e. isn't a virtual or bss section),
     741             :   // grab a reference to them.
     742         855 :   if (!IsVirtual && !IsZeroInit) {
     743             :     // In either case, set the location of the unrelocated section in memory,
     744             :     // since we still process relocations for it even if we're not applying them.
     745         821 :     if (auto EC = Section.getContents(data))
     746           0 :       return errorCodeToError(EC);
     747         821 :     pData = data.data();
     748             :   }
     749             : 
     750             :   // Code section alignment needs to be at least as high as stub alignment or
     751             :   // padding calculations may by incorrect when the section is remapped to a
     752             :   // higher alignment.
     753         855 :   if (IsCode) {
     754         680 :     Alignment = std::max(Alignment, getStubAlignment());
     755         340 :     if (StubBufSize > 0)
     756          29 :       PaddingSize += getStubAlignment() - 1;
     757             :   }
     758             : 
     759             :   // Some sections, such as debug info, don't need to be loaded for execution.
     760             :   // Process those only if explicitly requested.
     761         855 :   if (IsRequired || ProcessAllSections) {
     762         838 :     Allocate = DataSize + PaddingSize + StubBufSize;
     763         838 :     if (!Allocate)
     764           7 :       Allocate = 1;
     765        1676 :     Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
     766         340 :                                                Name)
     767         996 :                   : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
     768         498 :                                                Name, IsReadOnly);
     769         838 :     if (!Addr)
     770           0 :       report_fatal_error("Unable to allocate section memory!");
     771             : 
     772             :     // Zero-initialize or copy the data from the image
     773         838 :     if (IsZeroInit || IsVirtual)
     774          34 :       memset(Addr, 0, DataSize);
     775             :     else
     776         804 :       memcpy(Addr, pData, DataSize);
     777             : 
     778             :     // Fill in any extra bytes we allocated for padding
     779         838 :     if (PaddingSize != 0) {
     780         240 :       memset(Addr + DataSize, 0, PaddingSize);
     781             :       // Update the DataSize variable to include padding.
     782         240 :       DataSize += PaddingSize;
     783             : 
     784             :       // Align DataSize to stub alignment if we have any stubs (PaddingSize will
     785             :       // have been increased above to account for this).
     786         240 :       if (StubBufSize > 0)
     787           9 :         DataSize &= ~(getStubAlignment() - 1);
     788             :     }
     789             : 
     790             :     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
     791             :                  << " obj addr: " << format("%p", pData)
     792             :                  << " new addr: " << format("%p", Addr)
     793             :                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
     794             :                  << " Allocate: " << Allocate << "\n");
     795             :   } else {
     796             :     // Even if we didn't load the section, we need to record an entry for it
     797             :     // to handle later processing (and by 'handle' I mean don't do anything
     798             :     // with these sections).
     799             :     Allocate = 0;
     800             :     Addr = nullptr;
     801             :     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
     802             :                  << " obj addr: " << format("%p", data.data()) << " new addr: 0"
     803             :                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
     804             :                  << " Allocate: " << Allocate << "\n");
     805             :   }
     806             : 
     807        1710 :   Sections.push_back(
     808        2565 :       SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
     809             : 
     810             :   // Debug info sections are linked as if their load address was zero
     811         855 :   if (!IsRequired)
     812         154 :     Sections.back().setLoadAddress(0);
     813             : 
     814         855 :   if (Checker)
     815         127 :     Checker->registerSection(Obj.getFileName(), SectionID);
     816             : 
     817             :   return SectionID;
     818             : }
     819             : 
     820             : Expected<unsigned>
     821        2227 : RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
     822             :                                    const SectionRef &Section,
     823             :                                    bool IsCode,
     824             :                                    ObjSectionToIDMap &LocalSections) {
     825             : 
     826        2227 :   unsigned SectionID = 0;
     827        2227 :   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
     828        2227 :   if (i != LocalSections.end())
     829        1372 :     SectionID = i->second;
     830             :   else {
     831        2565 :     if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
     832         855 :       SectionID = *SectionIDOrErr;
     833             :     else
     834           0 :       return SectionIDOrErr.takeError();
     835         855 :     LocalSections[Section] = SectionID;
     836             :   }
     837             :   return SectionID;
     838             : }
     839             : 
     840        1144 : void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
     841             :                                               unsigned SectionID) {
     842        2288 :   Relocations[SectionID].push_back(RE);
     843        1144 : }
     844             : 
     845         264 : void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
     846             :                                              StringRef SymbolName) {
     847             :   // Relocation by symbol.  If the symbol is found in the global symbol table,
     848             :   // create an appropriate section relocation.  Otherwise, add it to
     849             :   // ExternalSymbolRelocations.
     850         528 :   RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
     851         792 :   if (Loc == GlobalSymbolTable.end()) {
     852         522 :     ExternalSymbolRelocations[SymbolName].push_back(RE);
     853             :   } else {
     854             :     // Copy the RE since we want to modify its addend.
     855           3 :     RelocationEntry RECopy = RE;
     856           3 :     const auto &SymInfo = Loc->second;
     857           3 :     RECopy.Addend += SymInfo.getOffset();
     858           6 :     Relocations[SymInfo.getSectionID()].push_back(RECopy);
     859             :   }
     860         264 : }
     861             : 
     862          14 : uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
     863             :                                              unsigned AbiVariant) {
     864          14 :   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
     865             :     // This stub has to be able to access the full address space,
     866             :     // since symbol lookup won't necessarily find a handy, in-range,
     867             :     // PLT stub for functions which could be anywhere.
     868             :     // Stub can use ip0 (== x16) to calculate address
     869           0 :     writeBytesUnaligned(0xd2e00010, Addr,    4); // movz ip0, #:abs_g3:<addr>
     870           0 :     writeBytesUnaligned(0xf2c00010, Addr+4,  4); // movk ip0, #:abs_g2_nc:<addr>
     871           0 :     writeBytesUnaligned(0xf2a00010, Addr+8,  4); // movk ip0, #:abs_g1_nc:<addr>
     872           0 :     writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
     873           0 :     writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
     874             : 
     875           0 :     return Addr;
     876          14 :   } else if (Arch == Triple::arm || Arch == Triple::armeb) {
     877             :     // TODO: There is only ARM far stub now. We should add the Thumb stub,
     878             :     // and stubs for branches Thumb - ARM and ARM - Thumb.
     879           0 :     writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc, [pc, #-4]
     880           0 :     return Addr + 4;
     881          14 :   } else if (IsMipsO32ABI) {
     882             :     // 0:   3c190000        lui     t9,%hi(addr).
     883             :     // 4:   27390000        addiu   t9,t9,%lo(addr).
     884             :     // 8:   03200008        jr      t9.
     885             :     // c:   00000000        nop.
     886           2 :     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
     887           2 :     const unsigned NopInstr = 0x0;
     888           2 :     unsigned JrT9Instr = 0x03200008;
     889           2 :     if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6)
     890           0 :         JrT9Instr = 0x03200009;
     891             : 
     892           2 :     writeBytesUnaligned(LuiT9Instr, Addr, 4);
     893           2 :     writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
     894           2 :     writeBytesUnaligned(JrT9Instr, Addr+8, 4);
     895           2 :     writeBytesUnaligned(NopInstr, Addr+12, 4);
     896           2 :     return Addr;
     897          12 :   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
     898             :     // Depending on which version of the ELF ABI is in use, we need to
     899             :     // generate one of two variants of the stub.  They both start with
     900             :     // the same sequence to load the target address into r12.
     901           0 :     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
     902           0 :     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
     903           0 :     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
     904           0 :     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
     905           0 :     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
     906           0 :     if (AbiVariant == 2) {
     907             :       // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
     908             :       // The address is already in r12 as required by the ABI.  Branch to it.
     909           0 :       writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
     910           0 :       writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
     911           0 :       writeInt32BE(Addr+28, 0x4E800420); // bctr
     912             :     } else {
     913             :       // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
     914             :       // Load the function address on r11 and sets it to control register. Also
     915             :       // loads the function TOC in r2 and environment pointer to r11.
     916           0 :       writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
     917           0 :       writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
     918           0 :       writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
     919           0 :       writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
     920           0 :       writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
     921           0 :       writeInt32BE(Addr+40, 0x4E800420); // bctr
     922             :     }
     923             :     return Addr;
     924          12 :   } else if (Arch == Triple::systemz) {
     925           0 :     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
     926           0 :     writeInt16BE(Addr+2,  0x0000);
     927           0 :     writeInt16BE(Addr+4,  0x0004);
     928           0 :     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
     929             :     // 8-byte address stored at Addr + 8
     930           0 :     return Addr;
     931          12 :   } else if (Arch == Triple::x86_64) {
     932          11 :     *Addr      = 0xFF; // jmp
     933          11 :     *(Addr+1)  = 0x25; // rip
     934             :     // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
     935           1 :   } else if (Arch == Triple::x86) {
     936           1 :     *Addr      = 0xE9; // 32-bit pc-relative jump.
     937             :   }
     938             :   return Addr;
     939             : }
     940             : 
     941             : // Assign an address to a symbol name and resolve all the relocations
     942             : // associated with it.
     943         194 : void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
     944             :                                              uint64_t Addr) {
     945             :   // The address to use for relocation resolution is not
     946             :   // the address of the local section buffer. We must be doing
     947             :   // a remote execution environment of some sort. Relocations can't
     948             :   // be applied until all the sections have been moved.  The client must
     949             :   // trigger this with a call to MCJIT::finalize() or
     950             :   // RuntimeDyld::resolveRelocations().
     951             :   //
     952             :   // Addr is a uint64_t because we can't assume the pointer width
     953             :   // of the target is the same as that of the host. Just use a generic
     954             :   // "big enough" type.
     955             :   DEBUG(dbgs() << "Reassigning address for section " << SectionID << " ("
     956             :                << Sections[SectionID].getName() << "): "
     957             :                << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
     958             :                << " -> " << format("0x%016" PRIx64, Addr) << "\n");
     959         582 :   Sections[SectionID].setLoadAddress(Addr);
     960         194 : }
     961             : 
     962         689 : void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
     963             :                                             uint64_t Value) {
     964        2782 :   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
     965        2808 :     const RelocationEntry &RE = Relocs[i];
     966             :     // Ignore relocations for sections that were not loaded
     967        2808 :     if (Sections[RE.SectionID].getAddress() == nullptr)
     968         190 :       continue;
     969        1214 :     resolveRelocation(RE, Value);
     970             :   }
     971         689 : }
     972             : 
     973         469 : Error RuntimeDyldImpl::resolveExternalSymbols() {
     974         839 :   while (!ExternalSymbolRelocations.empty()) {
     975         370 :     StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
     976             : 
     977         370 :     StringRef Name = i->first();
     978         185 :     if (Name.size() == 0) {
     979             :       // This is an absolute symbol, use an address of zero.
     980             :       DEBUG(dbgs() << "Resolving absolute relocations."
     981             :                    << "\n");
     982           0 :       RelocationList &Relocs = i->second;
     983           0 :       resolveRelocationList(Relocs, 0);
     984             :     } else {
     985         185 :       uint64_t Addr = 0;
     986         185 :       JITSymbolFlags Flags;
     987         370 :       RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
     988         555 :       if (Loc == GlobalSymbolTable.end()) {
     989             :         // This is an external symbol, try to get its address from the symbol
     990             :         // resolver.
     991             :         // First search for the symbol in this logical dylib.
     992         966 :         if (auto Sym = Resolver.findSymbolInLogicalDylib(Name.data())) {
     993         243 :           if (auto AddrOrErr = Sym.getAddress()) {
     994          81 :             Addr = *AddrOrErr;
     995          81 :             Flags = Sym.getFlags();
     996             :           } else
     997           0 :             return AddrOrErr.takeError();
     998         160 :         } else if (auto Err = Sym.takeError())
     999           0 :           return Err;
    1000             : 
    1001             :         // If that fails, try searching for an external symbol.
    1002         161 :         if (!Addr) {
    1003         480 :           if (auto Sym = Resolver.findSymbol(Name.data())) {
    1004         240 :             if (auto AddrOrErr = Sym.getAddress()) {
    1005          80 :               Addr = *AddrOrErr;
    1006          80 :               Flags = Sym.getFlags();
    1007             :             } else
    1008           0 :               return AddrOrErr.takeError();
    1009           0 :           } else if (auto Err = Sym.takeError())
    1010           0 :             return Err;
    1011             :         }
    1012             :         // The call to getSymbolAddress may have caused additional modules to
    1013             :         // be loaded, which may have added new entries to the
    1014             :         // ExternalSymbolRelocations map.  Consquently, we need to update our
    1015             :         // iterator.  This is also why retrieval of the relocation list
    1016             :         // associated with this symbol is deferred until below this point.
    1017             :         // New entries may have been added to the relocation list.
    1018         161 :         i = ExternalSymbolRelocations.find(Name);
    1019             :       } else {
    1020             :         // We found the symbol in our global table.  It was probably in a
    1021             :         // Module that we loaded previously.
    1022          24 :         const auto &SymInfo = Loc->second;
    1023          72 :         Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
    1024          24 :                SymInfo.getOffset();
    1025          24 :         Flags = SymInfo.getFlags();
    1026             :       }
    1027             : 
    1028             :       // FIXME: Implement error handling that doesn't kill the host program!
    1029         185 :       if (!Addr)
    1030           0 :         report_fatal_error("Program used external function '" + Name +
    1031           0 :                            "' which could not be resolved!");
    1032             : 
    1033             :       // If Resolver returned UINT64_MAX, the client wants to handle this symbol
    1034             :       // manually and we shouldn't resolve its relocations.
    1035         185 :       if (Addr != UINT64_MAX) {
    1036             : 
    1037             :         // Tweak the address based on the symbol flags if necessary.
    1038             :         // For example, this is used by RuntimeDyldMachOARM to toggle the low bit
    1039             :         // if the target symbol is Thumb.
    1040         181 :         Addr = modifyAddressBasedOnFlags(Addr, Flags);
    1041             : 
    1042             :         DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
    1043             :                      << format("0x%lx", Addr) << "\n");
    1044             :         // This list may have been updated when we called getSymbolAddress, so
    1045             :         // don't change this code to get the list earlier.
    1046         181 :         RelocationList &Relocs = i->second;
    1047         181 :         resolveRelocationList(Relocs, Addr);
    1048             :       }
    1049             :     }
    1050             : 
    1051         370 :     ExternalSymbolRelocations.erase(i);
    1052             :   }
    1053             : 
    1054        1407 :   return Error::success();
    1055             : }
    1056             : 
    1057             : //===----------------------------------------------------------------------===//
    1058             : // RuntimeDyld class implementation
    1059             : 
    1060        1102 : uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
    1061             :                                           const object::SectionRef &Sec) const {
    1062             : 
    1063        2204 :   auto I = ObjSecToIDMap.find(Sec);
    1064        2204 :   if (I != ObjSecToIDMap.end())
    1065         780 :     return RTDyld.Sections[I->second].getLoadAddress();
    1066             : 
    1067             :   return 0;
    1068             : }
    1069             : 
    1070           0 : void RuntimeDyld::MemoryManager::anchor() {}
    1071           0 : void JITSymbolResolver::anchor() {}
    1072             : 
    1073         298 : RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
    1074         298 :                          JITSymbolResolver &Resolver)
    1075         596 :     : MemMgr(MemMgr), Resolver(Resolver) {
    1076             :   // FIXME: There's a potential issue lurking here if a single instance of
    1077             :   // RuntimeDyld is used to load multiple objects.  The current implementation
    1078             :   // associates a single memory manager with a RuntimeDyld instance.  Even
    1079             :   // though the public class spawns a new 'impl' instance for each load,
    1080             :   // they share a single memory manager.  This can become a problem when page
    1081             :   // permissions are applied.
    1082         596 :   Dyld = nullptr;
    1083         298 :   ProcessAllSections = false;
    1084         298 :   Checker = nullptr;
    1085         298 : }
    1086             : 
    1087         452 : RuntimeDyld::~RuntimeDyld() {}
    1088             : 
    1089             : static std::unique_ptr<RuntimeDyldCOFF>
    1090             : createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
    1091             :                       JITSymbolResolver &Resolver, bool ProcessAllSections,
    1092             :                       RuntimeDyldCheckerImpl *Checker) {
    1093             :   std::unique_ptr<RuntimeDyldCOFF> Dyld =
    1094           3 :     RuntimeDyldCOFF::create(Arch, MM, Resolver);
    1095           6 :   Dyld->setProcessAllSections(ProcessAllSections);
    1096           6 :   Dyld->setRuntimeDyldChecker(Checker);
    1097             :   return Dyld;
    1098             : }
    1099             : 
    1100             : static std::unique_ptr<RuntimeDyldELF>
    1101             : createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
    1102             :                      JITSymbolResolver &Resolver, bool ProcessAllSections,
    1103             :                      RuntimeDyldCheckerImpl *Checker) {
    1104             :   std::unique_ptr<RuntimeDyldELF> Dyld =
    1105         277 :       RuntimeDyldELF::create(Arch, MM, Resolver);
    1106         554 :   Dyld->setProcessAllSections(ProcessAllSections);
    1107         554 :   Dyld->setRuntimeDyldChecker(Checker);
    1108             :   return Dyld;
    1109             : }
    1110             : 
    1111             : static std::unique_ptr<RuntimeDyldMachO>
    1112             : createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
    1113             :                        JITSymbolResolver &Resolver,
    1114             :                        bool ProcessAllSections,
    1115             :                        RuntimeDyldCheckerImpl *Checker) {
    1116             :   std::unique_ptr<RuntimeDyldMachO> Dyld =
    1117          11 :     RuntimeDyldMachO::create(Arch, MM, Resolver);
    1118          22 :   Dyld->setProcessAllSections(ProcessAllSections);
    1119          22 :   Dyld->setRuntimeDyldChecker(Checker);
    1120             :   return Dyld;
    1121             : }
    1122             : 
    1123             : std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
    1124         339 : RuntimeDyld::loadObject(const ObjectFile &Obj) {
    1125         678 :   if (!Dyld) {
    1126         582 :     if (Obj.isELF())
    1127         277 :       Dyld =
    1128        1108 :           createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()),
    1129         554 :                                MemMgr, Resolver, ProcessAllSections, Checker);
    1130          28 :     else if (Obj.isMachO())
    1131          55 :       Dyld = createRuntimeDyldMachO(
    1132          11 :                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
    1133          22 :                ProcessAllSections, Checker);
    1134           3 :     else if (Obj.isCOFF())
    1135          15 :       Dyld = createRuntimeDyldCOFF(
    1136           3 :                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
    1137           6 :                ProcessAllSections, Checker);
    1138             :     else
    1139           0 :       report_fatal_error("Incompatible object format!");
    1140             :   }
    1141             : 
    1142         678 :   if (!Dyld->isCompatibleFile(Obj))
    1143           0 :     report_fatal_error("Incompatible object format!");
    1144             : 
    1145         678 :   auto LoadedObjInfo = Dyld->loadObject(Obj);
    1146         339 :   MemMgr.notifyObjectLoaded(*this, Obj);
    1147         339 :   return LoadedObjInfo;
    1148             : }
    1149             : 
    1150           0 : void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
    1151           0 :   if (!Dyld)
    1152             :     return nullptr;
    1153           0 :   return Dyld->getSymbolLocalAddress(Name);
    1154             : }
    1155             : 
    1156         894 : JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const {
    1157        1788 :   if (!Dyld)
    1158             :     return nullptr;
    1159        1720 :   return Dyld->getSymbol(Name);
    1160             : }
    1161             : 
    1162         938 : void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
    1163             : 
    1164           0 : void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
    1165           0 :   Dyld->reassignSectionAddress(SectionID, Addr);
    1166           0 : }
    1167             : 
    1168         194 : void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
    1169             :                                     uint64_t TargetAddress) {
    1170         388 :   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
    1171         194 : }
    1172             : 
    1173         488 : bool RuntimeDyld::hasError() { return Dyld->hasError(); }
    1174             : 
    1175           0 : StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
    1176             : 
    1177         131 : void RuntimeDyld::finalizeWithMemoryManagerLocking() {
    1178         131 :   bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
    1179         131 :   MemMgr.FinalizationLocked = true;
    1180         131 :   resolveRelocations();
    1181         131 :   registerEHFrames();
    1182         131 :   if (!MemoryFinalizationLocked) {
    1183         113 :     MemMgr.finalizeMemory();
    1184         113 :     MemMgr.FinalizationLocked = false;
    1185             :   }
    1186         131 : }
    1187             : 
    1188         462 : void RuntimeDyld::registerEHFrames() {
    1189         924 :   if (Dyld)
    1190         924 :     Dyld->registerEHFrames();
    1191         462 : }
    1192             : 
    1193          49 : void RuntimeDyld::deregisterEHFrames() {
    1194          98 :   if (Dyld)
    1195          92 :     Dyld->deregisterEHFrames();
    1196          49 : }
    1197             : 
    1198             : } // end namespace llvm

Generated by: LCOV version 1.13