LLVM API Documentation

RuntimeDyldImpl.h
Go to the documentation of this file.
00001 //===-- RuntimeDyldImpl.h - 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 // Interface for the implementations of runtime dynamic linker facilities.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
00015 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/ADT/StringMap.h"
00020 #include "llvm/ADT/Triple.h"
00021 #include "llvm/ExecutionEngine/ObjectImage.h"
00022 #include "llvm/ExecutionEngine/RuntimeDyld.h"
00023 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
00024 #include "llvm/Object/ObjectFile.h"
00025 #include "llvm/Support/Debug.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/Support/Format.h"
00028 #include "llvm/Support/Host.h"
00029 #include "llvm/Support/Mutex.h"
00030 #include "llvm/Support/SwapByteOrder.h"
00031 #include "llvm/Support/raw_ostream.h"
00032 #include <map>
00033 #include <system_error>
00034 
00035 using namespace llvm;
00036 using namespace llvm::object;
00037 
00038 namespace llvm {
00039 
00040 class ObjectBuffer;
00041 class Twine;
00042 
00043 /// SectionEntry - represents a section emitted into memory by the dynamic
00044 /// linker.
00045 class SectionEntry {
00046 public:
00047   /// Name - section name.
00048   StringRef Name;
00049 
00050   /// Address - address in the linker's memory where the section resides.
00051   uint8_t *Address;
00052 
00053   /// Size - section size. Doesn't include the stubs.
00054   size_t Size;
00055 
00056   /// LoadAddress - the address of the section in the target process's memory.
00057   /// Used for situations in which JIT-ed code is being executed in the address
00058   /// space of a separate process.  If the code executes in the same address
00059   /// space where it was JIT-ed, this just equals Address.
00060   uint64_t LoadAddress;
00061 
00062   /// StubOffset - used for architectures with stub functions for far
00063   /// relocations (like ARM).
00064   uintptr_t StubOffset;
00065 
00066   /// ObjAddress - address of the section in the in-memory object file.  Used
00067   /// for calculating relocations in some object formats (like MachO).
00068   uintptr_t ObjAddress;
00069 
00070   SectionEntry(StringRef name, uint8_t *address, size_t size,
00071                uintptr_t objAddress)
00072       : Name(name), Address(address), Size(size),
00073         LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
00074         ObjAddress(objAddress) {}
00075 };
00076 
00077 /// RelocationEntry - used to represent relocations internally in the dynamic
00078 /// linker.
00079 class RelocationEntry {
00080 public:
00081   /// SectionID - the section this relocation points to.
00082   unsigned SectionID;
00083 
00084   /// Offset - offset into the section.
00085   uint64_t Offset;
00086 
00087   /// RelType - relocation type.
00088   uint32_t RelType;
00089 
00090   /// Addend - the relocation addend encoded in the instruction itself.  Also
00091   /// used to make a relocation section relative instead of symbol relative.
00092   int64_t Addend;
00093 
00094   struct SectionPair {
00095       uint32_t SectionA;
00096       uint32_t SectionB;
00097   };
00098 
00099   /// SymOffset - Section offset of the relocation entry's symbol (used for GOT
00100   /// lookup).
00101   union {
00102     uint64_t SymOffset;
00103     SectionPair Sections;
00104   };
00105 
00106   /// True if this is a PCRel relocation (MachO specific).
00107   bool IsPCRel;
00108 
00109   /// The size of this relocation (MachO specific).
00110   unsigned Size;
00111 
00112   RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
00113       : SectionID(id), Offset(offset), RelType(type), Addend(addend),
00114         SymOffset(0), IsPCRel(false), Size(0) {}
00115 
00116   RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
00117                   uint64_t symoffset)
00118       : SectionID(id), Offset(offset), RelType(type), Addend(addend),
00119         SymOffset(symoffset), IsPCRel(false), Size(0) {}
00120 
00121   RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
00122                   bool IsPCRel, unsigned Size)
00123       : SectionID(id), Offset(offset), RelType(type), Addend(addend),
00124         SymOffset(0), IsPCRel(IsPCRel), Size(Size) {}
00125 
00126   RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
00127                   unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
00128                   uint64_t SectionBOffset, bool IsPCRel, unsigned Size)
00129       : SectionID(id), Offset(offset), RelType(type),
00130         Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
00131         Size(Size) {
00132     Sections.SectionA = SectionA;
00133     Sections.SectionB = SectionB;
00134   }
00135 };
00136 
00137 class RelocationValueRef {
00138 public:
00139   unsigned SectionID;
00140   uint64_t Offset;
00141   int64_t Addend;
00142   const char *SymbolName;
00143   RelocationValueRef() : SectionID(0), Offset(0), Addend(0),
00144                          SymbolName(nullptr) {}
00145 
00146   inline bool operator==(const RelocationValueRef &Other) const {
00147     return SectionID == Other.SectionID && Offset == Other.Offset &&
00148            Addend == Other.Addend && SymbolName == Other.SymbolName;
00149   }
00150   inline bool operator<(const RelocationValueRef &Other) const {
00151     if (SectionID != Other.SectionID)
00152       return SectionID < Other.SectionID;
00153     if (Offset != Other.Offset)
00154       return Offset < Other.Offset;
00155     if (Addend != Other.Addend)
00156       return Addend < Other.Addend;
00157     return SymbolName < Other.SymbolName;
00158   }
00159 };
00160 
00161 class RuntimeDyldImpl {
00162   friend class RuntimeDyldCheckerImpl;
00163 private:
00164 
00165   uint64_t getAnySymbolRemoteAddress(StringRef Symbol) {
00166     if (uint64_t InternalSymbolAddr = getSymbolLoadAddress(Symbol))
00167       return InternalSymbolAddr;
00168     return MemMgr->getSymbolAddress(Symbol);
00169   }
00170 
00171 protected:
00172   // The MemoryManager to load objects into.
00173   RTDyldMemoryManager *MemMgr;
00174 
00175   // Attached RuntimeDyldChecker instance. Null if no instance attached.
00176   RuntimeDyldCheckerImpl *Checker;
00177 
00178   // A list of all sections emitted by the dynamic linker.  These sections are
00179   // referenced in the code by means of their index in this list - SectionID.
00180   typedef SmallVector<SectionEntry, 64> SectionList;
00181   SectionList Sections;
00182 
00183   typedef unsigned SID; // Type for SectionIDs
00184 #define RTDYLD_INVALID_SECTION_ID ((SID)(-1))
00185 
00186   // Keep a map of sections from object file to the SectionID which
00187   // references it.
00188   typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
00189 
00190   // A global symbol table for symbols from all loaded modules.  Maps the
00191   // symbol name to a (SectionID, offset in section) pair.
00192   typedef std::pair<unsigned, uintptr_t> SymbolLoc;
00193   typedef StringMap<SymbolLoc> SymbolTableMap;
00194   SymbolTableMap GlobalSymbolTable;
00195 
00196   // Pair representing the size and alignment requirement for a common symbol.
00197   typedef std::pair<unsigned, unsigned> CommonSymbolInfo;
00198   // Keep a map of common symbols to their info pairs
00199   typedef std::map<SymbolRef, CommonSymbolInfo> CommonSymbolMap;
00200 
00201   // For each symbol, keep a list of relocations based on it. Anytime
00202   // its address is reassigned (the JIT re-compiled the function, e.g.),
00203   // the relocations get re-resolved.
00204   // The symbol (or section) the relocation is sourced from is the Key
00205   // in the relocation list where it's stored.
00206   typedef SmallVector<RelocationEntry, 64> RelocationList;
00207   // Relocations to sections already loaded. Indexed by SectionID which is the
00208   // source of the address. The target where the address will be written is
00209   // SectionID/Offset in the relocation itself.
00210   DenseMap<unsigned, RelocationList> Relocations;
00211 
00212   // Relocations to external symbols that are not yet resolved.  Symbols are
00213   // external when they aren't found in the global symbol table of all loaded
00214   // modules.  This map is indexed by symbol name.
00215   StringMap<RelocationList> ExternalSymbolRelocations;
00216 
00217 
00218   typedef std::map<RelocationValueRef, uintptr_t> StubMap;
00219 
00220   Triple::ArchType Arch;
00221   bool IsTargetLittleEndian;
00222 
00223   // True if all sections should be passed to the memory manager, false if only
00224   // sections containing relocations should be. Defaults to 'false'.
00225   bool ProcessAllSections;
00226 
00227   // This mutex prevents simultaneously loading objects from two different
00228   // threads.  This keeps us from having to protect individual data structures
00229   // and guarantees that section allocation requests to the memory manager
00230   // won't be interleaved between modules.  It is also used in mapSectionAddress
00231   // and resolveRelocations to protect write access to internal data structures.
00232   //
00233   // loadObject may be called on the same thread during the handling of of
00234   // processRelocations, and that's OK.  The handling of the relocation lists
00235   // is written in such a way as to work correctly if new elements are added to
00236   // the end of the list while the list is being processed.
00237   sys::Mutex lock;
00238 
00239   virtual unsigned getMaxStubSize() = 0;
00240   virtual unsigned getStubAlignment() = 0;
00241 
00242   bool HasError;
00243   std::string ErrorStr;
00244 
00245   // Set the error state and record an error string.
00246   bool Error(const Twine &Msg) {
00247     ErrorStr = Msg.str();
00248     HasError = true;
00249     return true;
00250   }
00251 
00252   uint64_t getSectionLoadAddress(unsigned SectionID) const {
00253     return Sections[SectionID].LoadAddress;
00254   }
00255 
00256   uint8_t *getSectionAddress(unsigned SectionID) const {
00257     return (uint8_t *)Sections[SectionID].Address;
00258   }
00259 
00260   void writeInt16BE(uint8_t *Addr, uint16_t Value) {
00261     if (IsTargetLittleEndian)
00262       sys::swapByteOrder(Value);
00263     *Addr       = (Value >> 8) & 0xFF;
00264     *(Addr + 1) = Value & 0xFF;
00265   }
00266 
00267   void writeInt32BE(uint8_t *Addr, uint32_t Value) {
00268     if (IsTargetLittleEndian)
00269       sys::swapByteOrder(Value);
00270     *Addr       = (Value >> 24) & 0xFF;
00271     *(Addr + 1) = (Value >> 16) & 0xFF;
00272     *(Addr + 2) = (Value >> 8) & 0xFF;
00273     *(Addr + 3) = Value & 0xFF;
00274   }
00275 
00276   void writeInt64BE(uint8_t *Addr, uint64_t Value) {
00277     if (IsTargetLittleEndian)
00278       sys::swapByteOrder(Value);
00279     *Addr       = (Value >> 56) & 0xFF;
00280     *(Addr + 1) = (Value >> 48) & 0xFF;
00281     *(Addr + 2) = (Value >> 40) & 0xFF;
00282     *(Addr + 3) = (Value >> 32) & 0xFF;
00283     *(Addr + 4) = (Value >> 24) & 0xFF;
00284     *(Addr + 5) = (Value >> 16) & 0xFF;
00285     *(Addr + 6) = (Value >> 8) & 0xFF;
00286     *(Addr + 7) = Value & 0xFF;
00287   }
00288 
00289   /// Endian-aware read Read the least significant Size bytes from Src.
00290   uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const;
00291 
00292   /// Endian-aware write. Write the least significant Size bytes from Value to
00293   /// Dst.
00294   void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const;
00295 
00296   /// \brief Given the common symbols discovered in the object file, emit a
00297   /// new section for them and update the symbol mappings in the object and
00298   /// symbol table.
00299   void emitCommonSymbols(ObjectImage &Obj, const CommonSymbolMap &CommonSymbols,
00300                          uint64_t TotalSize, SymbolTableMap &SymbolTable);
00301 
00302   /// \brief Emits section data from the object file to the MemoryManager.
00303   /// \param IsCode if it's true then allocateCodeSection() will be
00304   ///        used for emits, else allocateDataSection() will be used.
00305   /// \return SectionID.
00306   unsigned emitSection(ObjectImage &Obj, const SectionRef &Section,
00307                        bool IsCode);
00308 
00309   /// \brief Find Section in LocalSections. If the secton is not found - emit
00310   ///        it and store in LocalSections.
00311   /// \param IsCode if it's true then allocateCodeSection() will be
00312   ///        used for emmits, else allocateDataSection() will be used.
00313   /// \return SectionID.
00314   unsigned findOrEmitSection(ObjectImage &Obj, const SectionRef &Section,
00315                              bool IsCode, ObjSectionToIDMap &LocalSections);
00316 
00317   // \brief Add a relocation entry that uses the given section.
00318   void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
00319 
00320   // \brief Add a relocation entry that uses the given symbol.  This symbol may
00321   // be found in the global symbol table, or it may be external.
00322   void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
00323 
00324   /// \brief Emits long jump instruction to Addr.
00325   /// \return Pointer to the memory area for emitting target address.
00326   uint8_t *createStubFunction(uint8_t *Addr, unsigned AbiVariant = 0);
00327 
00328   /// \brief Resolves relocations from Relocs list with address from Value.
00329   void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
00330 
00331   /// \brief A object file specific relocation resolver
00332   /// \param RE The relocation to be resolved
00333   /// \param Value Target symbol address to apply the relocation action
00334   virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
00335 
00336   /// \brief Parses one or more object file relocations (some object files use
00337   ///        relocation pairs) and stores it to Relocations or SymbolRelocations
00338   ///        (this depends on the object file type).
00339   /// \return Iterator to the next relocation that needs to be parsed.
00340   virtual relocation_iterator
00341   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
00342                        ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
00343                        const SymbolTableMap &Symbols, StubMap &Stubs) = 0;
00344 
00345   /// \brief Resolve relocations to external symbols.
00346   void resolveExternalSymbols();
00347 
00348   /// \brief Update GOT entries for external symbols.
00349   // The base class does nothing.  ELF overrides this.
00350   virtual void updateGOTEntries(StringRef Name, uint64_t Addr) {}
00351 
00352   // \brief Compute an upper bound of the memory that is required to load all
00353   // sections
00354   void computeTotalAllocSize(ObjectImage &Obj, uint64_t &CodeSize,
00355                              uint64_t &DataSizeRO, uint64_t &DataSizeRW);
00356 
00357   // \brief Compute the stub buffer size required for a section
00358   unsigned computeSectionStubBufSize(ObjectImage &Obj,
00359                                      const SectionRef &Section);
00360 
00361 public:
00362   RuntimeDyldImpl(RTDyldMemoryManager *mm)
00363     : MemMgr(mm), Checker(nullptr), ProcessAllSections(false), HasError(false) {
00364   }
00365 
00366   virtual ~RuntimeDyldImpl();
00367 
00368   void setProcessAllSections(bool ProcessAllSections) {
00369     this->ProcessAllSections = ProcessAllSections;
00370   }
00371 
00372   void setRuntimeDyldChecker(RuntimeDyldCheckerImpl *Checker) {
00373     this->Checker = Checker;
00374   }
00375 
00376   std::unique_ptr<ObjectImage>
00377   loadObject(std::unique_ptr<ObjectImage> InputObject);
00378 
00379   uint8_t* getSymbolAddress(StringRef Name) const {
00380     // FIXME: Just look up as a function for now. Overly simple of course.
00381     // Work in progress.
00382     SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name);
00383     if (pos == GlobalSymbolTable.end())
00384       return nullptr;
00385     SymbolLoc Loc = pos->second;
00386     return getSectionAddress(Loc.first) + Loc.second;
00387   }
00388 
00389   uint64_t getSymbolLoadAddress(StringRef Name) const {
00390     // FIXME: Just look up as a function for now. Overly simple of course.
00391     // Work in progress.
00392     SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name);
00393     if (pos == GlobalSymbolTable.end())
00394       return 0;
00395     SymbolLoc Loc = pos->second;
00396     return getSectionLoadAddress(Loc.first) + Loc.second;
00397   }
00398 
00399   void resolveRelocations();
00400 
00401   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
00402 
00403   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
00404 
00405   // Is the linker in an error state?
00406   bool hasError() { return HasError; }
00407 
00408   // Mark the error condition as handled and continue.
00409   void clearError() { HasError = false; }
00410 
00411   // Get the error message.
00412   StringRef getErrorString() { return ErrorStr; }
00413 
00414   virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const = 0;
00415   virtual bool isCompatibleFile(const ObjectFile *Obj) const = 0;
00416 
00417   virtual void registerEHFrames();
00418 
00419   virtual void deregisterEHFrames();
00420 
00421   virtual void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) {}
00422 };
00423 
00424 } // end namespace llvm
00425 
00426 #endif