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