blob: 739e8d65dbf4710cee99ddd9828945605db6a6e1 [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
David Blaikie5e1ffae2015-08-05 20:20:29 +000029class LoadedMachOObjectInfo final
NAKAMURA Takumi73dc2e42015-05-22 10:11:07 +000030 : public RuntimeDyld::LoadedObjectInfoHelper<LoadedMachOObjectInfo> {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000031public:
Lang Hames2e88f4f2015-07-28 17:52:11 +000032 LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld,
33 ObjSectionToIDMap ObjSecToIDMap)
34 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000035
36 OwningBinary<ObjectFile>
37 getObjectForDebug(const ObjectFile &Obj) const override {
38 return OwningBinary<ObjectFile>();
39 }
40};
41
Alexander Kornienkof00654e2015-06-23 09:49:53 +000042}
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000043
Danil Malyshev72510f22011-07-13 07:57:58 +000044namespace llvm {
45
Lang Hames25d93092014-08-08 23:12:22 +000046int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
Lang Hames25d93092014-08-08 23:12:22 +000047 unsigned NumBytes = 1 << RE.Size;
Sanjoy Das277776a2015-11-23 21:47:41 +000048 uint8_t *Src = Sections[RE.SectionID].getAddress() + RE.Offset;
Lang Hamesdc77feb2014-08-27 17:41:06 +000049
Lang Hamese1287c02014-08-29 23:17:47 +000050 return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
Lang Hamesa5216882014-07-17 18:54:50 +000051}
52
Lang Hamesa8183e52015-07-24 17:40:04 +000053relocation_iterator RuntimeDyldMachO::processScatteredVANILLA(
54 unsigned SectionID, relocation_iterator RelI,
55 const ObjectFile &BaseObjT,
56 RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
57 const MachOObjectFile &Obj =
58 static_cast<const MachOObjectFile&>(BaseObjT);
59 MachO::any_relocation_info RE =
60 Obj.getRelocation(RelI->getRawDataRefImpl());
61
62 SectionEntry &Section = Sections[SectionID];
63 uint32_t RelocType = Obj.getAnyRelocationType(RE);
64 bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
65 unsigned Size = Obj.getAnyRelocationLength(RE);
66 uint64_t Offset = RelI->getOffset();
Sanjoy Das277776a2015-11-23 21:47:41 +000067 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
Lang Hamesa8183e52015-07-24 17:40:04 +000068 unsigned NumBytes = 1 << Size;
69 int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
70
71 unsigned SymbolBaseAddr = Obj.getScatteredRelocationValue(RE);
72 section_iterator TargetSI = getSectionByAddress(Obj, SymbolBaseAddr);
73 assert(TargetSI != Obj.section_end() && "Can't find section for symbol");
74 uint64_t SectionBaseAddr = TargetSI->getAddress();
75 SectionRef TargetSection = *TargetSI;
76 bool IsCode = TargetSection.isText();
77 uint32_t TargetSectionID =
78 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID);
79
80 Addend -= SectionBaseAddr;
81 RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
82
83 addRelocationForSection(R, TargetSectionID);
84
85 return ++RelI;
86}
87
88
Lang Hamesa5216882014-07-17 18:54:50 +000089RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000090 const ObjectFile &BaseTObj, const relocation_iterator &RI,
Lang Hamesa5cd9502014-11-27 05:40:13 +000091 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
Lang Hamesa5216882014-07-17 18:54:50 +000092
93 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000094 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hamesa5216882014-07-17 18:54:50 +000095 MachO::any_relocation_info RelInfo =
96 Obj.getRelocation(RI->getRawDataRefImpl());
97 RelocationValueRef Value;
98
99 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
100 if (IsExternal) {
101 symbol_iterator Symbol = RI->getSymbol();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000102 ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
103 if (std::error_code EC = TargetNameOrErr.getError())
104 report_fatal_error(EC.message());
105 StringRef TargetName = *TargetNameOrErr;
Lang Hames6bfd3982015-01-16 23:13:56 +0000106 RTDyldSymbolTable::const_iterator SI =
Lang Hamesa5cd9502014-11-27 05:40:13 +0000107 GlobalSymbolTable.find(TargetName.data());
108 if (SI != GlobalSymbolTable.end()) {
Lang Hames6bfd3982015-01-16 23:13:56 +0000109 const auto &SymInfo = SI->second;
110 Value.SectionID = SymInfo.getSectionID();
111 Value.Offset = SymInfo.getOffset() + RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +0000112 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +0000113 Value.SymbolName = TargetName.data();
114 Value.Offset = RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +0000115 }
116 } else {
Keno Fischerc780e8e2015-05-21 21:24:32 +0000117 SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
Rafael Espindola80291272014-10-08 15:28:58 +0000118 bool IsCode = Sec.isText();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000119 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola80291272014-10-08 15:28:58 +0000120 uint64_t Addr = Sec.getAddress();
Lang Hamesca279c22014-09-07 04:03:32 +0000121 Value.Offset = RE.Addend - Addr;
Lang Hamesa5216882014-07-17 18:54:50 +0000122 }
123
124 return Value;
125}
126
127void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
Lang Hames13163652014-07-30 03:35:05 +0000128 const relocation_iterator &RI,
129 unsigned OffsetToNextPC) {
Rafael Espindola76ad2322015-07-06 14:55:37 +0000130 auto &O = *cast<MachOObjectFile>(RI->getObject());
131 section_iterator SecI = O.getRelocationRelocatedSection(RI);
132 Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
Lang Hamesa5216882014-07-17 18:54:50 +0000133}
134
135void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
136 uint64_t Value) const {
137 const SectionEntry &Section = Sections[RE.SectionID];
Sanjoy Das277776a2015-11-23 21:47:41 +0000138 uint8_t *LocalAddress = Section.getAddress() + RE.Offset;
139 uint64_t FinalAddress = Section.getLoadAddress() + RE.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +0000140
141 dbgs() << "resolveRelocation Section: " << RE.SectionID
142 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000143 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
144 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000145 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
146 << " Size: " << (1 << RE.Size) << "\n";
147}
148
Lang Hames6f1048f2014-09-11 19:21:14 +0000149section_iterator
150RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
151 uint64_t Addr) {
152 section_iterator SI = Obj.section_begin();
153 section_iterator SE = Obj.section_end();
154
155 for (; SI != SE; ++SI) {
Rafael Espindola80291272014-10-08 15:28:58 +0000156 uint64_t SAddr = SI->getAddress();
157 uint64_t SSize = SI->getSize();
Lang Hames6f1048f2014-09-11 19:21:14 +0000158 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
159 return SI;
160 }
161
162 return SE;
163}
164
165
166// Populate __pointers section.
167void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000168 const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000169 const SectionRef &PTSection,
170 unsigned PTSectionID) {
171 assert(!Obj.is64Bit() &&
172 "Pointer table section not supported in 64-bit MachO.");
173
174 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
175 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
176 uint32_t PTSectionSize = Sec32.size;
177 unsigned FirstIndirectSymbol = Sec32.reserved1;
178 const unsigned PTEntrySize = 4;
179 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
180 unsigned PTEntryOffset = 0;
181
182 assert((PTSectionSize % PTEntrySize) == 0 &&
183 "Pointers section does not contain a whole number of stubs?");
184
185 DEBUG(dbgs() << "Populating pointer table section "
Sanjoy Das277776a2015-11-23 21:47:41 +0000186 << Sections[PTSectionID].getName() << ", Section ID "
187 << PTSectionID << ", " << NumPTEntries << " entries, "
188 << PTEntrySize << " bytes each:\n");
Lang Hames6f1048f2014-09-11 19:21:14 +0000189
190 for (unsigned i = 0; i < NumPTEntries; ++i) {
191 unsigned SymbolIndex =
192 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
193 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000194 ErrorOr<StringRef> IndirectSymbolNameOrErr = SI->getName();
195 if (std::error_code EC = IndirectSymbolNameOrErr.getError())
196 report_fatal_error(EC.message());
197 StringRef IndirectSymbolName = *IndirectSymbolNameOrErr;
Lang Hames6f1048f2014-09-11 19:21:14 +0000198 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
199 << ", PT offset: " << PTEntryOffset << "\n");
200 RelocationEntry RE(PTSectionID, PTEntryOffset,
201 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
202 addRelocationForSymbol(RE, IndirectSymbolName);
203 PTEntryOffset += PTEntrySize;
204 }
205}
206
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000207bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
208 return Obj.isMachO();
Lang Hamesa5216882014-07-17 18:54:50 +0000209}
210
Lang Hameseb195f02014-09-04 04:53:03 +0000211template <typename Impl>
Lang Hames38aac642015-04-15 03:39:22 +0000212void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
Lang Hameseb195f02014-09-04 04:53:03 +0000213 ObjSectionToIDMap &SectionMap) {
214 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
215 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
216 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
Lang Hameseb195f02014-09-04 04:53:03 +0000217
Lang Hames38aac642015-04-15 03:39:22 +0000218 for (const auto &Section : Obj.sections()) {
Lang Hameseb195f02014-09-04 04:53:03 +0000219 StringRef Name;
220 Section.getName(Name);
Lang Hames38aac642015-04-15 03:39:22 +0000221
222 // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
223 // if they're present. Otherwise call down to the impl to handle other
224 // sections that have already been emitted.
225 if (Name == "__text")
226 TextSID = findOrEmitSection(Obj, Section, true, SectionMap);
227 else if (Name == "__eh_frame")
228 EHFrameSID = findOrEmitSection(Obj, Section, false, SectionMap);
Lang Hameseb195f02014-09-04 04:53:03 +0000229 else if (Name == "__gcc_except_tab")
Lang Hames38aac642015-04-15 03:39:22 +0000230 ExceptTabSID = findOrEmitSection(Obj, Section, true, SectionMap);
231 else {
232 auto I = SectionMap.find(Section);
233 if (I != SectionMap.end())
234 impl().finalizeSection(Obj, I->second, Section);
235 }
Lang Hameseb195f02014-09-04 04:53:03 +0000236 }
237 UnregisteredEHFrameSections.push_back(
238 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
239}
240
241template <typename Impl>
Sanjoy Das277776a2015-11-23 21:47:41 +0000242unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(uint8_t *P,
Lang Hameseb195f02014-09-04 04:53:03 +0000243 int64_t DeltaForText,
244 int64_t DeltaForEH) {
245 typedef typename Impl::TargetPtrT TargetPtrT;
246
Lang Hames36072da2014-05-12 21:39:59 +0000247 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
248 << ", Delta for EH: " << DeltaForEH << "\n");
Daniel Sanders523b1712014-11-01 15:52:31 +0000249 uint32_t Length = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000250 P += 4;
Sanjoy Das277776a2015-11-23 21:47:41 +0000251 uint8_t *Ret = P + Length;
Daniel Sanders523b1712014-11-01 15:52:31 +0000252 uint32_t Offset = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000253 if (Offset == 0) // is a CIE
254 return Ret;
255
256 P += 4;
Daniel Sanders523b1712014-11-01 15:52:31 +0000257 TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000258 TargetPtrT NewLocation = FDELocation - DeltaForText;
Daniel Sanders523b1712014-11-01 15:52:31 +0000259 writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
260
Lang Hameseb195f02014-09-04 04:53:03 +0000261 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000262
263 // Skip the FDE address range
Lang Hameseb195f02014-09-04 04:53:03 +0000264 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000265
266 uint8_t Augmentationsize = *P;
267 P += 1;
268 if (Augmentationsize != 0) {
Daniel Sanders523b1712014-11-01 15:52:31 +0000269 TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000270 TargetPtrT NewLSDA = LSDA - DeltaForEH;
Daniel Sanders523b1712014-11-01 15:52:31 +0000271 writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000272 }
273
274 return Ret;
275}
276
Lang Hameseb195f02014-09-04 04:53:03 +0000277static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
Sanjoy Das277776a2015-11-23 21:47:41 +0000278 int64_t ObjDistance = static_cast<int64_t>(A->getObjAddress()) -
279 static_cast<int64_t>(B->getObjAddress());
280 int64_t MemDistance = A->getLoadAddress() - B->getLoadAddress();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000281 return ObjDistance - MemDistance;
282}
283
Lang Hameseb195f02014-09-04 04:53:03 +0000284template <typename Impl>
285void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000286
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000287 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
288 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
289 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
290 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
291 continue;
292 SectionEntry *Text = &Sections[SectionInfo.TextSID];
293 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000294 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000295 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
296 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
297
Lang Hameseb195f02014-09-04 04:53:03 +0000298 int64_t DeltaForText = computeDelta(Text, EHFrame);
299 int64_t DeltaForEH = 0;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000300 if (ExceptTab)
301 DeltaForEH = computeDelta(ExceptTab, EHFrame);
302
Sanjoy Das277776a2015-11-23 21:47:41 +0000303 uint8_t *P = EHFrame->getAddress();
304 uint8_t *End = P + EHFrame->getSize();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000305 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000306 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000307 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000308
Sanjoy Das277776a2015-11-23 21:47:41 +0000309 MemMgr.registerEHFrames(EHFrame->getAddress(), EHFrame->getLoadAddress(),
310 EHFrame->getSize());
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000311 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000312 UnregisteredEHFrameSections.clear();
313}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000314
Lang Hamesa5216882014-07-17 18:54:50 +0000315std::unique_ptr<RuntimeDyldMachO>
Lang Hames633fe142015-03-30 03:37:06 +0000316RuntimeDyldMachO::create(Triple::ArchType Arch,
317 RuntimeDyld::MemoryManager &MemMgr,
318 RuntimeDyld::SymbolResolver &Resolver) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000319 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000320 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000321 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000322 break;
Lang Hames633fe142015-03-30 03:37:06 +0000323 case Triple::arm:
324 return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
325 case Triple::aarch64:
326 return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
327 case Triple::x86:
328 return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
329 case Triple::x86_64:
330 return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
Danil Malyshev72510f22011-07-13 07:57:58 +0000331 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000332}
333
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000334std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
335RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
Lang Hames2e88f4f2015-07-28 17:52:11 +0000336 return llvm::make_unique<LoadedMachOObjectInfo>(*this, loadObjectImpl(O));
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000337}
338
Danil Malyshev72510f22011-07-13 07:57:58 +0000339} // end namespace llvm