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