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