blob: ecbbf8282513d2f5c12f5cb5e38be0bea9c40d7c [file] [log] [blame]
Eli Bendersky058d6472012-01-22 07:05:02 +00001//===-- 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
Lang Hames951b2352014-03-08 18:45:12 +000017#include "ObjectImageCommon.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000018#include "RuntimeDyldImpl.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000019#include "llvm/Object/MachO.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000020#include "llvm/Support/Format.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000021
Lang Hamesa5216882014-07-17 18:54:50 +000022#define DEBUG_TYPE "dyld"
23
Eli Bendersky058d6472012-01-22 07:05:02 +000024using namespace llvm;
25using namespace llvm::object;
26
Eli Bendersky058d6472012-01-22 07:05:02 +000027namespace llvm {
28class RuntimeDyldMachO : public RuntimeDyldImpl {
Lang Hamesa5216882014-07-17 18:54:50 +000029protected:
30 struct SectionOffsetPair {
31 unsigned SectionID;
32 uint64_t Offset;
33 };
Lang Hames7f9fc2b2014-05-22 22:30:13 +000034
Andrew Kaylor7bb13442013-10-11 21:25:48 +000035 struct EHFrameRelatedSections {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000036 EHFrameRelatedSections()
37 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
38 TextSID(RTDYLD_INVALID_SECTION_ID),
39 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
NAKAMURA Takumi87e08802013-12-07 11:21:42 +000040 EHFrameRelatedSections(SID EH, SID T, SID Ex)
Juergen Ributzka7608dc02014-03-21 20:28:42 +000041 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
Andrew Kaylor7bb13442013-10-11 21:25:48 +000042 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;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000051
Eli Bendersky058d6472012-01-22 07:05:02 +000052 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
53
Lang Hames3fda7d82014-07-19 00:19:17 +000054 /// Extract the addend encoded in the instruction.
Juergen Ributzka175b78b2014-07-22 21:42:46 +000055 int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
56 uint32_t RelType) const;
Lang Hames951b2352014-03-08 18:45:12 +000057
Lang Hamesa5216882014-07-17 18:54:50 +000058 /// 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
80public:
81 /// Create an ObjectImage from the given ObjectBuffer.
Lang Hames84bc8182014-07-15 19:35:22 +000082 static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
83 return new ObjectImageCommon(InputBuffer);
84 }
85
Lang Hamesa5216882014-07-17 18:54:50 +000086 /// Create an ObjectImage from the given ObjectFile.
Juergen Ributzka7608dc02014-03-21 20:28:42 +000087 static ObjectImage *
Lang Hames84bc8182014-07-15 19:35:22 +000088 createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
89 return new ObjectImageCommon(std::move(InputObject));
90 }
Lang Hamesa5216882014-07-17 18:54:50 +000091
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.
114template <typename Impl>
115class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
116private:
117 Impl &impl() { return static_cast<Impl &>(*this); }
Lang Hames3fda7d82014-07-19 00:19:17 +0000118 const Impl &impl() const { return static_cast<const Impl &>(*this); }
119
120protected:
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);
Juergen Ributzka175b78b2014-07-22 21:42:46 +0000141 int64_t Addend = impl().decodeAddend(LocalAddress, NumBytes, RelType);
Lang Hames3fda7d82014-07-19 00:19:17 +0000142
143 return RelocationEntry(SectionID, Offset, RelType, Addend, IsPCRel, Size);
144 }
Lang Hamesa5216882014-07-17 18:54:50 +0000145
146public:
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 }
Eli Bendersky058d6472012-01-22 07:05:02 +0000171};
172
173} // end namespace llvm
174
Lang Hamesa5216882014-07-17 18:54:50 +0000175#undef DEBUG_TYPE
176
Eli Bendersky058d6472012-01-22 07:05:02 +0000177#endif