ExecutionEngine: Preliminary support for dynamically loadable coff objects
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldELF.h
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
24 class RuntimeDyldELF : public RuntimeDyldImpl {
25
26   void resolveRelocation(const SectionEntry &Section, uint64_t Offset,
27                          uint64_t Value, uint32_t Type, int64_t Addend,
28                          uint64_t SymOffset = 0);
29
30   void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,
31                                uint64_t Value, uint32_t Type, int64_t Addend,
32                                uint64_t SymOffset);
33
34   void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,
35                             uint32_t Value, uint32_t Type, int32_t Addend);
36
37   void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,
38                                 uint64_t Value, uint32_t Type, int64_t Addend);
39
40   void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,
41                             uint32_t Value, uint32_t Type, int32_t Addend);
42
43   void resolveMIPSRelocation(const SectionEntry &Section, uint64_t Offset,
44                              uint32_t Value, uint32_t Type, int32_t Addend);
45
46   void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,
47                               uint64_t Value, uint32_t Type, int64_t Addend);
48
49   void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,
50                                 uint64_t Value, uint32_t Type, int64_t Addend);
51
52   unsigned getMaxStubSize() override {
53     if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)
54       return 20; // movz; movk; movk; movk; br
55     if (Arch == Triple::arm || Arch == Triple::thumb)
56       return 8; // 32-bit instruction and 32-bit address
57     else if (Arch == Triple::mipsel || Arch == Triple::mips)
58       return 16;
59     else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
60       return 44;
61     else if (Arch == Triple::x86_64)
62       return 6; // 2-byte jmp instruction + 32-bit relative address
63     else if (Arch == Triple::systemz)
64       return 16;
65     else
66       return 0;
67   }
68
69   unsigned getStubAlignment() override {
70     if (Arch == Triple::systemz)
71       return 8;
72     else
73       return 1;
74   }
75
76   void findPPC64TOCSection(const ObjectFile &Obj,
77                            ObjSectionToIDMap &LocalSections,
78                            RelocationValueRef &Rel);
79   void findOPDEntrySection(const ObjectFile &Obj,
80                            ObjSectionToIDMap &LocalSections,
81                            RelocationValueRef &Rel);
82
83   uint64_t findGOTEntry(uint64_t LoadAddr, uint64_t Offset);
84   size_t getGOTEntrySize();
85
86   void updateGOTEntries(StringRef Name, uint64_t Addr) override;
87
88   // Relocation entries for symbols whose position-independent offset is
89   // updated in a global offset table.
90   typedef SmallVector<RelocationValueRef, 2> GOTRelocations;
91   GOTRelocations GOTEntries; // List of entries requiring finalization.
92   SmallVector<std::pair<SID, GOTRelocations>, 8> GOTs; // Allocated tables.
93
94   // When a module is loaded we save the SectionID of the EH frame section
95   // in a table until we receive a request to register all unregistered
96   // EH frame sections with the memory manager.
97   SmallVector<SID, 2> UnregisteredEHFrameSections;
98   SmallVector<SID, 2> RegisteredEHFrameSections;
99
100 public:
101   RuntimeDyldELF(RTDyldMemoryManager *mm);
102   virtual ~RuntimeDyldELF();
103
104   std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
105   loadObject(const object::ObjectFile &O) override;
106
107   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
108   relocation_iterator
109   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
110                        const ObjectFile &Obj,
111                        ObjSectionToIDMap &ObjSectionToID,
112                        StubMap &Stubs) override;
113   bool isCompatibleFile(const object::ObjectFile &Obj) const override;
114   void registerEHFrames() override;
115   void deregisterEHFrames() override;
116   void finalizeLoad(const ObjectFile &Obj,
117                     ObjSectionToIDMap &SectionMap) override;
118 };
119
120 } // end namespace llvm
121
122 #endif