08573eed5c87474c09903ad2563d509e49f1e22a
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldMachO.h
1 //===-- RuntimeDyldMachO.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 // MachO support for MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_RUNTIME_DYLD_MACHO_H
15 #define LLVM_RUNTIME_DYLD_MACHO_H
16
17 #include "ObjectImageCommon.h"
18 #include "RuntimeDyldImpl.h"
19 #include "llvm/ADT/IndexedMap.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/Format.h"
22
23 using namespace llvm;
24 using namespace llvm::object;
25
26 namespace llvm {
27 class RuntimeDyldMachO : public RuntimeDyldImpl {
28 private:
29
30   /// Write the least significant 'Size' bytes in 'Value' out at the address
31   /// pointed to by Addr.
32   bool applyRelocationValue(uint8_t *Addr, uint64_t Value, unsigned Size) {
33     for (unsigned i = 0; i < Size; ++i) {
34       *Addr++ = (uint8_t)Value;
35       Value >>= 8;
36     }
37
38     return false;
39   }
40
41   bool resolveI386Relocation(const RelocationEntry &RE, uint64_t Value);
42   bool resolveX86_64Relocation(const RelocationEntry &RE, uint64_t Value);
43   bool resolveARMRelocation(const RelocationEntry &RE, uint64_t Value);
44   bool resolveARM64Relocation(const RelocationEntry &RE, uint64_t Value);
45
46   // Populate stubs in __jump_table section.
47   void populateJumpTable(MachOObjectFile &Obj, const SectionRef &JTSection,
48                          unsigned JTSectionID);
49
50   // Populate __pointers section.
51   void populatePointersSection(MachOObjectFile &Obj, const SectionRef &PTSection,
52                                unsigned PTSectionID);
53
54   unsigned getMaxStubSize() override {
55     if (Arch == Triple::arm || Arch == Triple::thumb)
56       return 8; // 32-bit instruction and 32-bit address
57     else if (Arch == Triple::x86_64)
58       return 8; // GOT entry
59     else
60       return 0;
61   }
62
63   unsigned getStubAlignment() override { return 1; }
64
65   relocation_iterator processSECTDIFFRelocation(
66                                              unsigned SectionID,
67                                              relocation_iterator RelI,
68                                              ObjectImage &ObjImg,
69                                              ObjSectionToIDMap &ObjSectionToID);
70
71   relocation_iterator processI386ScatteredVANILLA(
72                                              unsigned SectionID,
73                                              relocation_iterator RelI,
74                                              ObjectImage &ObjImg,
75                                              ObjSectionToIDMap &ObjSectionToID);
76
77   struct EHFrameRelatedSections {
78     EHFrameRelatedSections()
79         : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
80           TextSID(RTDYLD_INVALID_SECTION_ID),
81           ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
82     EHFrameRelatedSections(SID EH, SID T, SID Ex)
83         : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
84     SID EHFrameSID;
85     SID TextSID;
86     SID ExceptTabSID;
87   };
88
89   // When a module is loaded we save the SectionID of the EH frame section
90   // in a table until we receive a request to register all unregistered
91   // EH frame sections with the memory manager.
92   SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
93
94 public:
95   RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
96
97   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
98   relocation_iterator
99   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
100                        ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
101                        const SymbolTableMap &Symbols, StubMap &Stubs) override;
102   bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
103   bool isCompatibleFile(const object::ObjectFile *Obj) const override;
104   void registerEHFrames() override;
105   void finalizeLoad(ObjectImage &ObjImg,
106                     ObjSectionToIDMap &SectionMap) override;
107
108   static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
109     return new ObjectImageCommon(InputBuffer);
110   }
111
112   static ObjectImage *
113   createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
114     return new ObjectImageCommon(std::move(InputObject));
115   }
116 };
117
118 } // end namespace llvm
119
120 #endif