blob: c0741141757c4c129defa6cc314e89fe49d59266 [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
NAKAMURA Takumi73dc2e42015-05-22 10:11:07 +000029class LoadedMachOObjectInfo
30 : public RuntimeDyld::LoadedObjectInfoHelper<LoadedMachOObjectInfo> {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000031public:
32 LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
33 unsigned EndIdx)
NAKAMURA Takumi73dc2e42015-05-22 10:11:07 +000034 : LoadedObjectInfoHelper(RTDyld, BeginIdx, EndIdx) {}
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;
Lang Hamese1287c02014-08-29 23:17:47 +000048 uint8_t *Src = Sections[RE.SectionID].Address + 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
53RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000054 const ObjectFile &BaseTObj, const relocation_iterator &RI,
Lang Hamesa5cd9502014-11-27 05:40:13 +000055 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
Lang Hamesa5216882014-07-17 18:54:50 +000056
57 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000058 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hamesa5216882014-07-17 18:54:50 +000059 MachO::any_relocation_info RelInfo =
60 Obj.getRelocation(RI->getRawDataRefImpl());
61 RelocationValueRef Value;
62
63 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
64 if (IsExternal) {
65 symbol_iterator Symbol = RI->getSymbol();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +000066 ErrorOr<StringRef> TargetNameOrErr = Symbol->getName();
67 if (std::error_code EC = TargetNameOrErr.getError())
68 report_fatal_error(EC.message());
69 StringRef TargetName = *TargetNameOrErr;
Lang Hames6bfd3982015-01-16 23:13:56 +000070 RTDyldSymbolTable::const_iterator SI =
Lang Hamesa5cd9502014-11-27 05:40:13 +000071 GlobalSymbolTable.find(TargetName.data());
72 if (SI != GlobalSymbolTable.end()) {
Lang Hames6bfd3982015-01-16 23:13:56 +000073 const auto &SymInfo = SI->second;
74 Value.SectionID = SymInfo.getSectionID();
75 Value.Offset = SymInfo.getOffset() + RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000076 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +000077 Value.SymbolName = TargetName.data();
78 Value.Offset = RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000079 }
80 } else {
Keno Fischerc780e8e2015-05-21 21:24:32 +000081 SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
Rafael Espindola80291272014-10-08 15:28:58 +000082 bool IsCode = Sec.isText();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000083 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola80291272014-10-08 15:28:58 +000084 uint64_t Addr = Sec.getAddress();
Lang Hamesca279c22014-09-07 04:03:32 +000085 Value.Offset = RE.Addend - Addr;
Lang Hamesa5216882014-07-17 18:54:50 +000086 }
87
88 return Value;
89}
90
91void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
Lang Hames13163652014-07-30 03:35:05 +000092 const relocation_iterator &RI,
93 unsigned OffsetToNextPC) {
Rafael Espindola76ad2322015-07-06 14:55:37 +000094 auto &O = *cast<MachOObjectFile>(RI->getObject());
95 section_iterator SecI = O.getRelocationRelocatedSection(RI);
96 Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
Lang Hamesa5216882014-07-17 18:54:50 +000097}
98
99void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
100 uint64_t Value) const {
101 const SectionEntry &Section = Sections[RE.SectionID];
102 uint8_t *LocalAddress = Section.Address + RE.Offset;
103 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
104
105 dbgs() << "resolveRelocation Section: " << RE.SectionID
106 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000107 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
108 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000109 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
110 << " Size: " << (1 << RE.Size) << "\n";
111}
112
Lang Hames6f1048f2014-09-11 19:21:14 +0000113section_iterator
114RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
115 uint64_t Addr) {
116 section_iterator SI = Obj.section_begin();
117 section_iterator SE = Obj.section_end();
118
119 for (; SI != SE; ++SI) {
Rafael Espindola80291272014-10-08 15:28:58 +0000120 uint64_t SAddr = SI->getAddress();
121 uint64_t SSize = SI->getSize();
Lang Hames6f1048f2014-09-11 19:21:14 +0000122 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
123 return SI;
124 }
125
126 return SE;
127}
128
129
130// Populate __pointers section.
131void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000132 const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000133 const SectionRef &PTSection,
134 unsigned PTSectionID) {
135 assert(!Obj.is64Bit() &&
136 "Pointer table section not supported in 64-bit MachO.");
137
138 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
139 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
140 uint32_t PTSectionSize = Sec32.size;
141 unsigned FirstIndirectSymbol = Sec32.reserved1;
142 const unsigned PTEntrySize = 4;
143 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
144 unsigned PTEntryOffset = 0;
145
146 assert((PTSectionSize % PTEntrySize) == 0 &&
147 "Pointers section does not contain a whole number of stubs?");
148
149 DEBUG(dbgs() << "Populating pointer table section "
150 << Sections[PTSectionID].Name
151 << ", Section ID " << PTSectionID << ", "
152 << NumPTEntries << " entries, " << PTEntrySize
153 << " bytes each:\n");
154
155 for (unsigned i = 0; i < NumPTEntries; ++i) {
156 unsigned SymbolIndex =
157 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
158 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000159 ErrorOr<StringRef> IndirectSymbolNameOrErr = SI->getName();
160 if (std::error_code EC = IndirectSymbolNameOrErr.getError())
161 report_fatal_error(EC.message());
162 StringRef IndirectSymbolName = *IndirectSymbolNameOrErr;
Lang Hames6f1048f2014-09-11 19:21:14 +0000163 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
164 << ", PT offset: " << PTEntryOffset << "\n");
165 RelocationEntry RE(PTSectionID, PTEntryOffset,
166 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
167 addRelocationForSymbol(RE, IndirectSymbolName);
168 PTEntryOffset += PTEntrySize;
169 }
170}
171
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000172bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
173 return Obj.isMachO();
Lang Hamesa5216882014-07-17 18:54:50 +0000174}
175
Lang Hameseb195f02014-09-04 04:53:03 +0000176template <typename Impl>
Lang Hames38aac642015-04-15 03:39:22 +0000177void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
Lang Hameseb195f02014-09-04 04:53:03 +0000178 ObjSectionToIDMap &SectionMap) {
179 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
180 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
181 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
Lang Hameseb195f02014-09-04 04:53:03 +0000182
Lang Hames38aac642015-04-15 03:39:22 +0000183 for (const auto &Section : Obj.sections()) {
Lang Hameseb195f02014-09-04 04:53:03 +0000184 StringRef Name;
185 Section.getName(Name);
Lang Hames38aac642015-04-15 03:39:22 +0000186
187 // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
188 // if they're present. Otherwise call down to the impl to handle other
189 // sections that have already been emitted.
190 if (Name == "__text")
191 TextSID = findOrEmitSection(Obj, Section, true, SectionMap);
192 else if (Name == "__eh_frame")
193 EHFrameSID = findOrEmitSection(Obj, Section, false, SectionMap);
Lang Hameseb195f02014-09-04 04:53:03 +0000194 else if (Name == "__gcc_except_tab")
Lang Hames38aac642015-04-15 03:39:22 +0000195 ExceptTabSID = findOrEmitSection(Obj, Section, true, SectionMap);
196 else {
197 auto I = SectionMap.find(Section);
198 if (I != SectionMap.end())
199 impl().finalizeSection(Obj, I->second, Section);
200 }
Lang Hameseb195f02014-09-04 04:53:03 +0000201 }
202 UnregisteredEHFrameSections.push_back(
203 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
204}
205
206template <typename Impl>
207unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
208 int64_t DeltaForText,
209 int64_t DeltaForEH) {
210 typedef typename Impl::TargetPtrT TargetPtrT;
211
Lang Hames36072da2014-05-12 21:39:59 +0000212 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
213 << ", Delta for EH: " << DeltaForEH << "\n");
Daniel Sanders523b1712014-11-01 15:52:31 +0000214 uint32_t Length = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000215 P += 4;
216 unsigned char *Ret = P + Length;
Daniel Sanders523b1712014-11-01 15:52:31 +0000217 uint32_t Offset = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000218 if (Offset == 0) // is a CIE
219 return Ret;
220
221 P += 4;
Daniel Sanders523b1712014-11-01 15:52:31 +0000222 TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000223 TargetPtrT NewLocation = FDELocation - DeltaForText;
Daniel Sanders523b1712014-11-01 15:52:31 +0000224 writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
225
Lang Hameseb195f02014-09-04 04:53:03 +0000226 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000227
228 // Skip the FDE address range
Lang Hameseb195f02014-09-04 04:53:03 +0000229 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000230
231 uint8_t Augmentationsize = *P;
232 P += 1;
233 if (Augmentationsize != 0) {
Daniel Sanders523b1712014-11-01 15:52:31 +0000234 TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000235 TargetPtrT NewLSDA = LSDA - DeltaForEH;
Daniel Sanders523b1712014-11-01 15:52:31 +0000236 writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000237 }
238
239 return Ret;
240}
241
Lang Hameseb195f02014-09-04 04:53:03 +0000242static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
Lang Hames042e35c2015-04-15 04:46:01 +0000243 int64_t ObjDistance =
244 static_cast<int64_t>(A->ObjAddress) - static_cast<int64_t>(B->ObjAddress);
Lang Hameseb195f02014-09-04 04:53:03 +0000245 int64_t MemDistance = A->LoadAddress - B->LoadAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000246 return ObjDistance - MemDistance;
247}
248
Lang Hameseb195f02014-09-04 04:53:03 +0000249template <typename Impl>
250void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000251
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000252 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
253 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
254 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
255 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
256 continue;
257 SectionEntry *Text = &Sections[SectionInfo.TextSID];
258 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000259 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000260 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
261 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
262
Lang Hameseb195f02014-09-04 04:53:03 +0000263 int64_t DeltaForText = computeDelta(Text, EHFrame);
264 int64_t DeltaForEH = 0;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000265 if (ExceptTab)
266 DeltaForEH = computeDelta(ExceptTab, EHFrame);
267
268 unsigned char *P = EHFrame->Address;
269 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000270 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000271 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000272 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000273
Lang Hames633fe142015-03-30 03:37:06 +0000274 MemMgr.registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
275 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000276 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000277 UnregisteredEHFrameSections.clear();
278}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000279
Lang Hamesa5216882014-07-17 18:54:50 +0000280std::unique_ptr<RuntimeDyldMachO>
Lang Hames633fe142015-03-30 03:37:06 +0000281RuntimeDyldMachO::create(Triple::ArchType Arch,
282 RuntimeDyld::MemoryManager &MemMgr,
283 RuntimeDyld::SymbolResolver &Resolver) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000284 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000285 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000286 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000287 break;
Lang Hames633fe142015-03-30 03:37:06 +0000288 case Triple::arm:
289 return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
290 case Triple::aarch64:
291 return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
292 case Triple::x86:
293 return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
294 case Triple::x86_64:
295 return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
Danil Malyshev72510f22011-07-13 07:57:58 +0000296 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000297}
298
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000299std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
300RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
301 unsigned SectionStartIdx, SectionEndIdx;
302 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
303 return llvm::make_unique<LoadedMachOObjectInfo>(*this, SectionStartIdx,
304 SectionEndIdx);
305}
306
Danil Malyshev72510f22011-07-13 07:57:58 +0000307} // end namespace llvm