LLVM API Documentation

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