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