blob: 796a69cc836e03a4782f3545e0d035be9a0e44d3 [file] [log] [blame]
Jim Grosbach06594e12012-01-16 23:50:58 +00001//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
Danil Malyshev72510f22011-07-13 07:57:58 +00002//
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// Implementation of the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
Eli Bendersky058d6472012-01-22 07:05:02 +000014#include "RuntimeDyldMachO.h"
Lang Hamesa5216882014-07-17 18:54:50 +000015#include "Targets/RuntimeDyldMachOAArch64.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "Targets/RuntimeDyldMachOARM.h"
Lang Hamesa5216882014-07-17 18:54:50 +000017#include "Targets/RuntimeDyldMachOI386.h"
18#include "Targets/RuntimeDyldMachOX86_64.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringRef.h"
Lang Hamesa5216882014-07-17 18:54:50 +000021
Danil Malyshev72510f22011-07-13 07:57:58 +000022using namespace llvm;
23using namespace llvm::object;
24
Chandler Carruthf58e3762014-04-22 03:04:17 +000025#define DEBUG_TYPE "dyld"
26
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000027namespace {
28
Tobias Grosser87fc5f82015-05-22 06:01:04 +000029class LoadedMachOObjectInfo : public RuntimeDyld::LoadedObjectInfo {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000030public:
31 LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
32 unsigned EndIdx)
Tobias Grosser87fc5f82015-05-22 06:01:04 +000033 : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000034
35 OwningBinary<ObjectFile>
36 getObjectForDebug(const ObjectFile &Obj) const override {
37 return OwningBinary<ObjectFile>();
38 }
Tobias Grosser87fc5f82015-05-22 06:01:04 +000039
40 RuntimeDyld::LoadedObjectInfo *clone() const { return new LoadedMachOObjectInfo(*this); }
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000041};
42
43}
44
Danil Malyshev72510f22011-07-13 07:57:58 +000045namespace llvm {
46
Lang Hames25d93092014-08-08 23:12:22 +000047int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
Lang Hames25d93092014-08-08 23:12:22 +000048 unsigned NumBytes = 1 << RE.Size;
Lang Hamese1287c02014-08-29 23:17:47 +000049 uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset;
Lang Hamesdc77feb2014-08-27 17:41:06 +000050
Lang Hamese1287c02014-08-29 23:17:47 +000051 return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
Lang Hamesa5216882014-07-17 18:54:50 +000052}
53
54RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000055 const ObjectFile &BaseTObj, const relocation_iterator &RI,
Lang Hamesa5cd9502014-11-27 05:40:13 +000056 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
Lang Hamesa5216882014-07-17 18:54:50 +000057
58 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000059 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hamesa5216882014-07-17 18:54:50 +000060 MachO::any_relocation_info RelInfo =
61 Obj.getRelocation(RI->getRawDataRefImpl());
62 RelocationValueRef Value;
63
64 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
65 if (IsExternal) {
66 symbol_iterator Symbol = RI->getSymbol();
67 StringRef TargetName;
68 Symbol->getName(TargetName);
Lang Hames6bfd3982015-01-16 23:13:56 +000069 RTDyldSymbolTable::const_iterator SI =
Lang Hamesa5cd9502014-11-27 05:40:13 +000070 GlobalSymbolTable.find(TargetName.data());
71 if (SI != GlobalSymbolTable.end()) {
Lang Hames6bfd3982015-01-16 23:13:56 +000072 const auto &SymInfo = SI->second;
73 Value.SectionID = SymInfo.getSectionID();
74 Value.Offset = SymInfo.getOffset() + RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000075 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +000076 Value.SymbolName = TargetName.data();
77 Value.Offset = RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000078 }
79 } else {
Keno Fischerc780e8e2015-05-21 21:24:32 +000080 SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
Rafael Espindola80291272014-10-08 15:28:58 +000081 bool IsCode = Sec.isText();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000082 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola80291272014-10-08 15:28:58 +000083 uint64_t Addr = Sec.getAddress();
Lang Hamesca279c22014-09-07 04:03:32 +000084 Value.Offset = RE.Addend - Addr;
Lang Hamesa5216882014-07-17 18:54:50 +000085 }
86
87 return Value;
88}
89
90void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000091 const ObjectFile &BaseTObj,
Lang Hames13163652014-07-30 03:35:05 +000092 const relocation_iterator &RI,
93 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000094 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000095 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hamesa5216882014-07-17 18:54:50 +000096 MachO::any_relocation_info RelInfo =
97 Obj.getRelocation(RI->getRawDataRefImpl());
98
99 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
100 if (IsPCRel) {
101 uint64_t RelocAddr = 0;
102 RI->getAddress(RelocAddr);
Lang Hamesca279c22014-09-07 04:03:32 +0000103 Value.Offset += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +0000104 }
105}
106
107void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
108 uint64_t Value) const {
109 const SectionEntry &Section = Sections[RE.SectionID];
110 uint8_t *LocalAddress = Section.Address + RE.Offset;
111 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
112
113 dbgs() << "resolveRelocation Section: " << RE.SectionID
114 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000115 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
116 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000117 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
118 << " Size: " << (1 << RE.Size) << "\n";
119}
120
Lang Hames6f1048f2014-09-11 19:21:14 +0000121section_iterator
122RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
123 uint64_t Addr) {
124 section_iterator SI = Obj.section_begin();
125 section_iterator SE = Obj.section_end();
126
127 for (; SI != SE; ++SI) {
Rafael Espindola80291272014-10-08 15:28:58 +0000128 uint64_t SAddr = SI->getAddress();
129 uint64_t SSize = SI->getSize();
Lang Hames6f1048f2014-09-11 19:21:14 +0000130 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
131 return SI;
132 }
133
134 return SE;
135}
136
137
138// Populate __pointers section.
139void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000140 const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000141 const SectionRef &PTSection,
142 unsigned PTSectionID) {
143 assert(!Obj.is64Bit() &&
144 "Pointer table section not supported in 64-bit MachO.");
145
146 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
147 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
148 uint32_t PTSectionSize = Sec32.size;
149 unsigned FirstIndirectSymbol = Sec32.reserved1;
150 const unsigned PTEntrySize = 4;
151 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
152 unsigned PTEntryOffset = 0;
153
154 assert((PTSectionSize % PTEntrySize) == 0 &&
155 "Pointers section does not contain a whole number of stubs?");
156
157 DEBUG(dbgs() << "Populating pointer table section "
158 << Sections[PTSectionID].Name
159 << ", Section ID " << PTSectionID << ", "
160 << NumPTEntries << " entries, " << PTEntrySize
161 << " bytes each:\n");
162
163 for (unsigned i = 0; i < NumPTEntries; ++i) {
164 unsigned SymbolIndex =
165 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
166 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
167 StringRef IndirectSymbolName;
168 SI->getName(IndirectSymbolName);
169 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
170 << ", PT offset: " << PTEntryOffset << "\n");
171 RelocationEntry RE(PTSectionID, PTEntryOffset,
172 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
173 addRelocationForSymbol(RE, IndirectSymbolName);
174 PTEntryOffset += PTEntrySize;
175 }
176}
177
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000178bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
179 return Obj.isMachO();
Lang Hamesa5216882014-07-17 18:54:50 +0000180}
181
Lang Hameseb195f02014-09-04 04:53:03 +0000182template <typename Impl>
Lang Hames38aac642015-04-15 03:39:22 +0000183void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
Lang Hameseb195f02014-09-04 04:53:03 +0000184 ObjSectionToIDMap &SectionMap) {
185 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
186 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
187 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
Lang Hameseb195f02014-09-04 04:53:03 +0000188
Lang Hames38aac642015-04-15 03:39:22 +0000189 for (const auto &Section : Obj.sections()) {
Lang Hameseb195f02014-09-04 04:53:03 +0000190 StringRef Name;
191 Section.getName(Name);
Lang Hames38aac642015-04-15 03:39:22 +0000192
193 // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
194 // if they're present. Otherwise call down to the impl to handle other
195 // sections that have already been emitted.
196 if (Name == "__text")
197 TextSID = findOrEmitSection(Obj, Section, true, SectionMap);
198 else if (Name == "__eh_frame")
199 EHFrameSID = findOrEmitSection(Obj, Section, false, SectionMap);
Lang Hameseb195f02014-09-04 04:53:03 +0000200 else if (Name == "__gcc_except_tab")
Lang Hames38aac642015-04-15 03:39:22 +0000201 ExceptTabSID = findOrEmitSection(Obj, Section, true, SectionMap);
202 else {
203 auto I = SectionMap.find(Section);
204 if (I != SectionMap.end())
205 impl().finalizeSection(Obj, I->second, Section);
206 }
Lang Hameseb195f02014-09-04 04:53:03 +0000207 }
208 UnregisteredEHFrameSections.push_back(
209 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
210}
211
212template <typename Impl>
213unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
214 int64_t DeltaForText,
215 int64_t DeltaForEH) {
216 typedef typename Impl::TargetPtrT TargetPtrT;
217
Lang Hames36072da2014-05-12 21:39:59 +0000218 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
219 << ", Delta for EH: " << DeltaForEH << "\n");
Daniel Sanders523b1712014-11-01 15:52:31 +0000220 uint32_t Length = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000221 P += 4;
222 unsigned char *Ret = P + Length;
Daniel Sanders523b1712014-11-01 15:52:31 +0000223 uint32_t Offset = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000224 if (Offset == 0) // is a CIE
225 return Ret;
226
227 P += 4;
Daniel Sanders523b1712014-11-01 15:52:31 +0000228 TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000229 TargetPtrT NewLocation = FDELocation - DeltaForText;
Daniel Sanders523b1712014-11-01 15:52:31 +0000230 writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
231
Lang Hameseb195f02014-09-04 04:53:03 +0000232 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000233
234 // Skip the FDE address range
Lang Hameseb195f02014-09-04 04:53:03 +0000235 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000236
237 uint8_t Augmentationsize = *P;
238 P += 1;
239 if (Augmentationsize != 0) {
Daniel Sanders523b1712014-11-01 15:52:31 +0000240 TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000241 TargetPtrT NewLSDA = LSDA - DeltaForEH;
Daniel Sanders523b1712014-11-01 15:52:31 +0000242 writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000243 }
244
245 return Ret;
246}
247
Lang Hameseb195f02014-09-04 04:53:03 +0000248static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
Lang Hames042e35c2015-04-15 04:46:01 +0000249 int64_t ObjDistance =
250 static_cast<int64_t>(A->ObjAddress) - static_cast<int64_t>(B->ObjAddress);
Lang Hameseb195f02014-09-04 04:53:03 +0000251 int64_t MemDistance = A->LoadAddress - B->LoadAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000252 return ObjDistance - MemDistance;
253}
254
Lang Hameseb195f02014-09-04 04:53:03 +0000255template <typename Impl>
256void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000257
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000258 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
259 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
260 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
261 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
262 continue;
263 SectionEntry *Text = &Sections[SectionInfo.TextSID];
264 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000265 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000266 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
267 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
268
Lang Hameseb195f02014-09-04 04:53:03 +0000269 int64_t DeltaForText = computeDelta(Text, EHFrame);
270 int64_t DeltaForEH = 0;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000271 if (ExceptTab)
272 DeltaForEH = computeDelta(ExceptTab, EHFrame);
273
274 unsigned char *P = EHFrame->Address;
275 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000276 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000277 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000278 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000279
Lang Hames633fe142015-03-30 03:37:06 +0000280 MemMgr.registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
281 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000282 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000283 UnregisteredEHFrameSections.clear();
284}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000285
Lang Hamesa5216882014-07-17 18:54:50 +0000286std::unique_ptr<RuntimeDyldMachO>
Lang Hames633fe142015-03-30 03:37:06 +0000287RuntimeDyldMachO::create(Triple::ArchType Arch,
288 RuntimeDyld::MemoryManager &MemMgr,
289 RuntimeDyld::SymbolResolver &Resolver) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000290 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000291 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000292 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000293 break;
Lang Hames633fe142015-03-30 03:37:06 +0000294 case Triple::arm:
295 return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
296 case Triple::aarch64:
297 return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
298 case Triple::x86:
299 return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
300 case Triple::x86_64:
301 return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
Danil Malyshev72510f22011-07-13 07:57:58 +0000302 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000303}
304
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000305std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
306RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
307 unsigned SectionStartIdx, SectionEndIdx;
308 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
309 return llvm::make_unique<LoadedMachOObjectInfo>(*this, SectionStartIdx,
310 SectionEndIdx);
311}
312
Danil Malyshev72510f22011-07-13 07:57:58 +0000313} // end namespace llvm