Home | History | Annotate | Download | only in RuntimeDyld
      1 //===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // ELF support for MC-JIT runtime dynamic linker.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
     15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H
     16 
     17 #include "RuntimeDyldImpl.h"
     18 #include "llvm/ADT/DenseMap.h"
     19 
     20 using namespace llvm;
     21 
     22 namespace llvm {
     23 namespace object {
     24 class ELFObjectFileBase;
     25 }
     26 
     27 class RuntimeDyldELF : public RuntimeDyldImpl {
     28 
     29   void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
     30                          uint64_t Value, uint32_t Type, int64_t Addend,
     31                          uint64_t SymOffset = 0, SID SectionID = 0);
     32 
     33   void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
     34                                uint64_t Value, uint32_t Type, int64_t Addend,
     35                                uint64_t SymOffset);
     36 
     37   void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
     38                             uint32_t Value, uint32_t Type, int32_t Addend);
     39 
     40   void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
     41                                 uint64_t Value, uint32_t Type, int64_t Addend);
     42 
     43   void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
     44                             uint32_t Value, uint32_t Type, int32_t Addend);
     45 
     46   void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset,
     47                              uint32_t Value, uint32_t Type, int32_t Addend);
     48 
     49   void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,
     50                               uint64_t Value, uint32_t Type, int64_t Addend);
     51 
     52   void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
     53                               uint64_t Value, uint32_t Type, int64_t Addend);
     54 
     55   void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
     56                                 uint64_t Value, uint32_t Type, int64_t Addend);
     57 
     58   void resolveMIPS64Relocation(const SectionEntry &Section, uint64_t Offset,
     59                                uint64_t Value, uint32_t Type, int64_t Addend,
     60                                uint64_t SymOffset, SID SectionID);
     61 
     62   int64_t evaluateMIPS64Relocation(const SectionEntry &Section,
     63                                    uint64_t Offset, uint64_t Value,
     64                                    uint32_t Type,  int64_t Addend,
     65                                    uint64_t SymOffset, SID SectionID);
     66 
     67   void applyMIPS64Relocation(uint8_t *TargetPtr, int64_t CalculatedValue,
     68                              uint32_t Type);
     69 
     70   unsigned getMaxStubSize() override {
     71     if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
     72       return 20; // movz; movk; movk; movk; br
     73     if (Arch == Triple::arm || Arch == Triple::thumb)
     74       return 8; // 32-bit instruction and 32-bit address
     75     else if (IsMipsO32ABI)
     76       return 16;
     77     else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
     78       return 44;
     79     else if (Arch == Triple::x86_64)
     80       return 6; // 2-byte jmp instruction + 32-bit relative address
     81     else if (Arch == Triple::systemz)
     82       return 16;
     83     else
     84       return 0;
     85   }
     86 
     87   unsigned getStubAlignment() override {
     88     if (Arch == Triple::systemz)
     89       return 8;
     90     else
     91       return 1;
     92   }
     93 
     94   void setMipsABI(const ObjectFile &Obj) override;
     95 
     96   Error findPPC64TOCSection(const ELFObjectFileBase &Obj,
     97                             ObjSectionToIDMap &LocalSections,
     98                             RelocationValueRef &Rel);
     99   Error findOPDEntrySection(const ELFObjectFileBase &Obj,
    100                             ObjSectionToIDMap &LocalSections,
    101                             RelocationValueRef &Rel);
    102 
    103   size_t getGOTEntrySize();
    104 
    105   SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
    106 
    107   // Allocate no GOT entries for use in the given section.
    108   uint64_t allocateGOTEntries(unsigned SectionID, unsigned no);
    109 
    110   // Resolve the relvative address of GOTOffset in Section ID and place
    111   // it at the given Offset
    112   void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,
    113                                   uint64_t GOTOffset);
    114 
    115   // For a GOT entry referenced from SectionID, compute a relocation entry
    116   // that will place the final resolved value in the GOT slot
    117   RelocationEntry computeGOTOffsetRE(unsigned SectionID,
    118                                      uint64_t GOTOffset,
    119                                      uint64_t SymbolOffset,
    120                                      unsigned Type);
    121 
    122   // Compute the address in memory where we can find the placeholder
    123   void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;
    124 
    125   // Split out common case for createing the RelocationEntry for when the relocation requires
    126   // no particular advanced processing.
    127   void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);
    128 
    129   // Return matching *LO16 relocation (Mips specific)
    130   uint32_t getMatchingLoRelocation(uint32_t RelType,
    131                                    bool IsLocal = false) const;
    132 
    133   // The tentative ID for the GOT section
    134   unsigned GOTSectionID;
    135 
    136   // Records the current number of allocated slots in the GOT
    137   // (This would be equivalent to GOTEntries.size() were it not for relocations
    138   // that consume more than one slot)
    139   unsigned CurrentGOTIndex;
    140 
    141   // A map from section to a GOT section that has entries for section's GOT
    142   // relocations. (Mips64 specific)
    143   DenseMap<SID, SID> SectionToGOTMap;
    144 
    145   // A map to avoid duplicate got entries (Mips64 specific)
    146   StringMap<uint64_t> GOTSymbolOffsets;
    147 
    148   // *HI16 relocations will be added for resolving when we find matching
    149   // *LO16 part. (Mips specific)
    150   SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs;
    151 
    152   // When a module is loaded we save the SectionID of the EH frame section
    153   // in a table until we receive a request to register all unregistered
    154   // EH frame sections with the memory manager.
    155   SmallVector<SID, 2> UnregisteredEHFrameSections;
    156   SmallVector<SID, 2> RegisteredEHFrameSections;
    157 
    158   bool relocationNeedsStub(const RelocationRef &R) const override;
    159 
    160 public:
    161   RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,
    162                  RuntimeDyld::SymbolResolver &Resolver);
    163   ~RuntimeDyldELF() override;
    164 
    165   std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
    166   loadObject(const object::ObjectFile &O) override;
    167 
    168   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
    169   Expected<relocation_iterator>
    170   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
    171                        const ObjectFile &Obj,
    172                        ObjSectionToIDMap &ObjSectionToID,
    173                        StubMap &Stubs) override;
    174   bool isCompatibleFile(const object::ObjectFile &Obj) const override;
    175   void registerEHFrames() override;
    176   void deregisterEHFrames() override;
    177   Error finalizeLoad(const ObjectFile &Obj,
    178                      ObjSectionToIDMap &SectionMap) override;
    179 };
    180 
    181 } // end namespace llvm
    182 
    183 #endif
    184