LLVM API Documentation

RuntimeDyldELF.h
Go to the documentation of this file.
00001 //===-- RuntimeDyldELF.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 // ELF support for MC-JIT runtime dynamic linker.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
00015 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
00016 
00017 #include "RuntimeDyldImpl.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 
00020 using namespace llvm;
00021 
00022 namespace llvm {
00023 namespace {
00024 // Helper for extensive error checking in debug builds.
00025 std::error_code Check(std::error_code Err) {
00026   if (Err) {
00027     report_fatal_error(Err.message());
00028   }
00029   return Err;
00030 }
00031 } // end anonymous namespace
00032 
00033 class RuntimeDyldELF : public RuntimeDyldImpl {
00034   void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
00035                          uint64_t Value, uint32_t Type, int64_t Addend,
00036                          uint64_t SymOffset = 0);
00037 
00038   void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
00039                                uint64_t Value, uint32_t Type, int64_t Addend,
00040                                uint64_t SymOffset);
00041 
00042   void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
00043                             uint32_t Value, uint32_t Type, int32_t Addend);
00044 
00045   void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
00046                                 uint64_t Value, uint32_t Type, int64_t Addend);
00047 
00048   void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
00049                             uint32_t Value, uint32_t Type, int32_t Addend);
00050 
00051   void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset,
00052                              uint32_t Value, uint32_t Type, int32_t Addend);
00053 
00054   void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
00055                               uint64_t Value, uint32_t Type, int64_t Addend);
00056 
00057   void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
00058                                 uint64_t Value, uint32_t Type, int64_t Addend);
00059 
00060   unsigned getMaxStubSize() override {
00061     if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
00062       return 20; // movz; movk; movk; movk; br
00063     if (Arch == Triple::arm || Arch == Triple::thumb)
00064       return 8; // 32-bit instruction and 32-bit address
00065     else if (Arch == Triple::mipsel || Arch == Triple::mips)
00066       return 16;
00067     else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
00068       return 44;
00069     else if (Arch == Triple::x86_64)
00070       return 6; // 2-byte jmp instruction + 32-bit relative address
00071     else if (Arch == Triple::systemz)
00072       return 16;
00073     else
00074       return 0;
00075   }
00076 
00077   unsigned getStubAlignment() override {
00078     if (Arch == Triple::systemz)
00079       return 8;
00080     else
00081       return 1;
00082   }
00083 
00084   void findPPC64TOCSection(ObjectImage &Obj, ObjSectionToIDMap &LocalSections,
00085                            RelocationValueRef &Rel);
00086   void findOPDEntrySection(ObjectImage &Obj, ObjSectionToIDMap &LocalSections,
00087                            RelocationValueRef &Rel);
00088 
00089   uint64_t findGOTEntry(uint64_t LoadAddr, uint64_t Offset);
00090   size_t getGOTEntrySize();
00091 
00092   void updateGOTEntries(StringRef Name, uint64_t Addr) override;
00093 
00094   // Relocation entries for symbols whose position-independent offset is
00095   // updated in a global offset table.
00096   typedef SmallVector<RelocationValueRef, 2> GOTRelocations;
00097   GOTRelocations GOTEntries; // List of entries requiring finalization.
00098   SmallVector<std::pair<SID, GOTRelocations>, 8> GOTs; // Allocated tables.
00099 
00100   // When a module is loaded we save the SectionID of the EH frame section
00101   // in a table until we receive a request to register all unregistered
00102   // EH frame sections with the memory manager.
00103   SmallVector<SID, 2> UnregisteredEHFrameSections;
00104   SmallVector<SID, 2> RegisteredEHFrameSections;
00105 
00106 public:
00107   RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
00108 
00109   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
00110   relocation_iterator
00111   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
00112                        ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
00113                        const SymbolTableMap &Symbols, StubMap &Stubs) override;
00114   bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
00115   bool isCompatibleFile(const object::ObjectFile *Buffer) const override;
00116   void registerEHFrames() override;
00117   void deregisterEHFrames() override;
00118   void finalizeLoad(ObjectImage &ObjImg,
00119                     ObjSectionToIDMap &SectionMap) override;
00120   virtual ~RuntimeDyldELF();
00121 
00122   static std::unique_ptr<ObjectImage>
00123   createObjectImage(std::unique_ptr<ObjectBuffer> InputBuffer);
00124   static ObjectImage *createObjectImageFromFile(std::unique_ptr<object::ObjectFile> Obj);
00125 };
00126 
00127 } // end namespace llvm
00128 
00129 #endif