ecbbf8282513d2f5c12f5cb5e38be0bea9c40d7c
[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/Object/MachO.h"
20 #include "llvm/Support/Format.h"
21
22 #define DEBUG_TYPE "dyld"
23
24 using namespace llvm;
25 using namespace llvm::object;
26
27 namespace llvm {
28 class RuntimeDyldMachO : public RuntimeDyldImpl {
29 protected:
30   struct SectionOffsetPair {
31     unsigned SectionID;
32     uint64_t Offset;
33   };
34
35   struct EHFrameRelatedSections {
36     EHFrameRelatedSections()
37         : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
38           TextSID(RTDYLD_INVALID_SECTION_ID),
39           ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
40     EHFrameRelatedSections(SID EH, SID T, SID Ex)
41         : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
42     SID EHFrameSID;
43     SID TextSID;
44     SID ExceptTabSID;
45   };
46
47   // When a module is loaded we save the SectionID of the EH frame section
48   // in a table until we receive a request to register all unregistered
49   // EH frame sections with the memory manager.
50   SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
51
52   RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
53
54   /// Extract the addend encoded in the instruction.
55   int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
56                        uint32_t RelType) const;
57
58   /// Construct a RelocationValueRef representing the relocation target.
59   /// For Symbols in known sections, this will return a RelocationValueRef
60   /// representing a (SectionID, Offset) pair.
61   /// For Symbols whose section is not known, this will return a
62   /// (SymbolName, Offset) pair, where the Offset is taken from the instruction
63   /// immediate (held in RE.Addend).
64   /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That
65   /// should be done by the caller where appropriate by calling makePCRel on
66   /// the RelocationValueRef.
67   RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg,
68                                            const relocation_iterator &RI,
69                                            const RelocationEntry &RE,
70                                            ObjSectionToIDMap &ObjSectionToID,
71                                            const SymbolTableMap &Symbols);
72
73   /// Make the RelocationValueRef addend PC-relative.
74   void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg,
75                             const relocation_iterator &RI);
76
77   /// Dump information about the relocation entry (RE) and resolved value.
78   void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
79
80 public:
81   /// Create an ObjectImage from the given ObjectBuffer.
82   static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
83     return new ObjectImageCommon(InputBuffer);
84   }
85
86   /// Create an ObjectImage from the given ObjectFile.
87   static ObjectImage *
88   createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
89     return new ObjectImageCommon(std::move(InputObject));
90   }
91
92   /// Create a RuntimeDyldMachO instance for the given target architecture.
93   static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch,
94                                                   RTDyldMemoryManager *mm);
95
96   /// Write the least significant 'Size' bytes in 'Value' out at the address
97   /// pointed to by Addr. Check for overflow.
98   bool writeBytesUnaligned(uint8_t *Addr, uint64_t Value, unsigned Size);
99
100   SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
101
102   bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
103   bool isCompatibleFile(const object::ObjectFile *Obj) const override;
104   void registerEHFrames() override;
105 };
106
107 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
108 /// algorithms and data structures.
109 ///
110 /// Concrete, target specific sub-classes can be accessed via the impl()
111 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
112 /// Recurring Template Idiom). Concrete subclasses for each target
113 /// can be found in ./Targets.
114 template <typename Impl>
115 class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
116 private:
117   Impl &impl() { return static_cast<Impl &>(*this); }
118   const Impl &impl() const { return static_cast<const Impl &>(*this); }
119
120 protected:
121
122   /// Parse the given relocation, which must be a non-scattered, and
123   /// return a RelocationEntry representing the information. The 'Addend' field
124   /// will contain the unmodified instruction immediate.
125   RelocationEntry getBasicRelocationEntry(unsigned SectionID,
126                                           ObjectImage &ObjImg,
127                                           const relocation_iterator &RI) const {
128     const MachOObjectFile &Obj =
129       static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
130     MachO::any_relocation_info RelInfo =
131       Obj.getRelocation(RI->getRawDataRefImpl());
132
133     const SectionEntry &Section = Sections[SectionID];
134     bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
135     unsigned Size = Obj.getAnyRelocationLength(RelInfo);
136     uint64_t Offset;
137     RI->getOffset(Offset);
138     uint8_t *LocalAddress = Section.Address + Offset;
139     unsigned NumBytes = 1 << Size;
140     uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
141     int64_t Addend = impl().decodeAddend(LocalAddress, NumBytes, RelType);
142
143     return RelocationEntry(SectionID, Offset, RelType, Addend, IsPCRel, Size);
144   }
145
146 public:
147   RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {}
148
149   void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) {
150     unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
151     unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
152     unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
153     ObjSectionToIDMap::iterator i, e;
154
155     for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
156       const SectionRef &Section = i->first;
157       StringRef Name;
158       Section.getName(Name);
159       if (Name == "__eh_frame")
160         EHFrameSID = i->second;
161       else if (Name == "__text")
162         TextSID = i->second;
163       else if (Name == "__gcc_except_tab")
164         ExceptTabSID = i->second;
165       else
166         impl().finalizeSection(ObjImg, i->second, Section);
167     }
168     UnregisteredEHFrameSections.push_back(
169         EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
170   }
171 };
172
173 } // end namespace llvm
174
175 #undef DEBUG_TYPE
176
177 #endif