48b0481ce58e0697b3310695922f75b2e1e2fec2
[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
27 namespace llvm {
28 class RuntimeDyldMachO : public RuntimeDyldImpl {
29   bool resolveI386Relocation(uint8_t *LocalAddress,
30                              uint64_t FinalAddress,
31                              uint64_t Value,
32                              bool isPCRel,
33                              unsigned Type,
34                              unsigned Size,
35                              int64_t Addend);
36   bool resolveX86_64Relocation(uint8_t *LocalAddress,
37                                uint64_t FinalAddress,
38                                uint64_t Value,
39                                bool isPCRel,
40                                unsigned Type,
41                                unsigned Size,
42                                int64_t Addend);
43   bool resolveARMRelocation(uint8_t *LocalAddress,
44                             uint64_t FinalAddress,
45                             uint64_t Value,
46                             bool isPCRel,
47                             unsigned Type,
48                             unsigned Size,
49                             int64_t Addend);
50
51   void resolveRelocation(const SectionEntry &Section,
52                          uint64_t Offset,
53                          uint64_t Value,
54                          uint32_t Type,
55                          int64_t Addend,
56                          bool isPCRel,
57                          unsigned Size);
58
59   unsigned getMaxStubSize() override {
60     if (Arch == Triple::arm || Arch == Triple::thumb)
61       return 8; // 32-bit instruction and 32-bit address
62     else if (Arch == Triple::x86_64)
63       return 8; // GOT entry
64     else
65       return 0;
66   }
67
68   unsigned getStubAlignment() override {
69     return 1;
70   }
71
72   struct EHFrameRelatedSections {
73     EHFrameRelatedSections() : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
74                                TextSID(RTDYLD_INVALID_SECTION_ID),
75                                ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
76     EHFrameRelatedSections(SID EH, SID T, SID Ex)
77       : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
78     SID EHFrameSID;
79     SID TextSID;
80     SID ExceptTabSID;
81   };
82
83   // When a module is loaded we save the SectionID of the EH frame section
84   // in a table until we receive a request to register all unregistered
85   // EH frame sections with the memory manager.
86   SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
87 public:
88   RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
89
90   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
91   relocation_iterator
92   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
93                        ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
94                        const SymbolTableMap &Symbols, StubMap &Stubs) override;
95   bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
96   bool isCompatibleFile(const object::ObjectFile *Obj) const override;
97   void registerEHFrames() override;
98   void finalizeLoad(ObjSectionToIDMap &SectionMap) override;
99
100
101   static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
102     return new ObjectImageCommon(InputBuffer);
103   }
104
105   static ObjectImage *createObjectImageFromFile(object::ObjectFile *InputObject) {
106     return new ObjectImageCommon(InputObject);
107   }
108 };
109
110 } // end namespace llvm
111
112 #endif