blob: 7d4dea630d0741c8cc2f25a110b653b3ac239ab0 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringRef.h"
Lang Hamesa5216882014-07-17 18:54:50 +000017
18#include "Targets/RuntimeDyldMachOARM.h"
19#include "Targets/RuntimeDyldMachOAArch64.h"
20#include "Targets/RuntimeDyldMachOI386.h"
21#include "Targets/RuntimeDyldMachOX86_64.h"
22
Danil Malyshev72510f22011-07-13 07:57:58 +000023using namespace llvm;
24using namespace llvm::object;
25
Chandler Carruthf58e3762014-04-22 03:04:17 +000026#define DEBUG_TYPE "dyld"
27
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000028namespace {
29
30class LoadedMachOObjectInfo : public RuntimeDyld::LoadedObjectInfo {
31public:
32 LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
33 unsigned EndIdx)
34 : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
35
36 OwningBinary<ObjectFile>
37 getObjectForDebug(const ObjectFile &Obj) const override {
38 return OwningBinary<ObjectFile>();
39 }
40};
41
42}
43
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();
66 StringRef TargetName;
67 Symbol->getName(TargetName);
Lang Hamesa5cd9502014-11-27 05:40:13 +000068 SymbolTableMap::const_iterator SI =
69 GlobalSymbolTable.find(TargetName.data());
70 if (SI != GlobalSymbolTable.end()) {
Lang Hamesa5216882014-07-17 18:54:50 +000071 Value.SectionID = SI->second.first;
Lang Hamesca279c22014-09-07 04:03:32 +000072 Value.Offset = SI->second.second + RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000073 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +000074 Value.SymbolName = TargetName.data();
75 Value.Offset = RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000076 }
77 } else {
78 SectionRef Sec = Obj.getRelocationSection(RelInfo);
Rafael Espindola80291272014-10-08 15:28:58 +000079 bool IsCode = Sec.isText();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000080 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola80291272014-10-08 15:28:58 +000081 uint64_t Addr = Sec.getAddress();
Lang Hamesca279c22014-09-07 04:03:32 +000082 Value.Offset = RE.Addend - Addr;
Lang Hamesa5216882014-07-17 18:54:50 +000083 }
84
85 return Value;
86}
87
88void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000089 const ObjectFile &BaseTObj,
Lang Hames13163652014-07-30 03:35:05 +000090 const relocation_iterator &RI,
91 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000092 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000093 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hamesa5216882014-07-17 18:54:50 +000094 MachO::any_relocation_info RelInfo =
95 Obj.getRelocation(RI->getRawDataRefImpl());
96
97 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
98 if (IsPCRel) {
99 uint64_t RelocAddr = 0;
100 RI->getAddress(RelocAddr);
Lang Hamesca279c22014-09-07 04:03:32 +0000101 Value.Offset += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +0000102 }
103}
104
105void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
106 uint64_t Value) const {
107 const SectionEntry &Section = Sections[RE.SectionID];
108 uint8_t *LocalAddress = Section.Address + RE.Offset;
109 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
110
111 dbgs() << "resolveRelocation Section: " << RE.SectionID
112 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000113 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
114 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000115 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
116 << " Size: " << (1 << RE.Size) << "\n";
117}
118
Lang Hames6f1048f2014-09-11 19:21:14 +0000119section_iterator
120RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
121 uint64_t Addr) {
122 section_iterator SI = Obj.section_begin();
123 section_iterator SE = Obj.section_end();
124
125 for (; SI != SE; ++SI) {
Rafael Espindola80291272014-10-08 15:28:58 +0000126 uint64_t SAddr = SI->getAddress();
127 uint64_t SSize = SI->getSize();
Lang Hames6f1048f2014-09-11 19:21:14 +0000128 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
129 return SI;
130 }
131
132 return SE;
133}
134
135
136// Populate __pointers section.
137void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000138 const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000139 const SectionRef &PTSection,
140 unsigned PTSectionID) {
141 assert(!Obj.is64Bit() &&
142 "Pointer table section not supported in 64-bit MachO.");
143
144 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
145 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
146 uint32_t PTSectionSize = Sec32.size;
147 unsigned FirstIndirectSymbol = Sec32.reserved1;
148 const unsigned PTEntrySize = 4;
149 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
150 unsigned PTEntryOffset = 0;
151
152 assert((PTSectionSize % PTEntrySize) == 0 &&
153 "Pointers section does not contain a whole number of stubs?");
154
155 DEBUG(dbgs() << "Populating pointer table section "
156 << Sections[PTSectionID].Name
157 << ", Section ID " << PTSectionID << ", "
158 << NumPTEntries << " entries, " << PTEntrySize
159 << " bytes each:\n");
160
161 for (unsigned i = 0; i < NumPTEntries; ++i) {
162 unsigned SymbolIndex =
163 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
164 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
165 StringRef IndirectSymbolName;
166 SI->getName(IndirectSymbolName);
167 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
168 << ", PT offset: " << PTEntryOffset << "\n");
169 RelocationEntry RE(PTSectionID, PTEntryOffset,
170 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
171 addRelocationForSymbol(RE, IndirectSymbolName);
172 PTEntryOffset += PTEntrySize;
173 }
174}
175
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000176bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
177 return Obj.isMachO();
Lang Hamesa5216882014-07-17 18:54:50 +0000178}
179
Lang Hameseb195f02014-09-04 04:53:03 +0000180template <typename Impl>
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000181void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &ObjImg,
Lang Hameseb195f02014-09-04 04:53:03 +0000182 ObjSectionToIDMap &SectionMap) {
183 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
184 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
185 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
186 ObjSectionToIDMap::iterator i, e;
187
188 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
189 const SectionRef &Section = i->first;
190 StringRef Name;
191 Section.getName(Name);
192 if (Name == "__eh_frame")
193 EHFrameSID = i->second;
194 else if (Name == "__text")
195 TextSID = i->second;
196 else if (Name == "__gcc_except_tab")
197 ExceptTabSID = i->second;
198 else
199 impl().finalizeSection(ObjImg, i->second, Section);
200 }
201 UnregisteredEHFrameSections.push_back(
202 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
203}
204
205template <typename Impl>
206unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
207 int64_t DeltaForText,
208 int64_t DeltaForEH) {
209 typedef typename Impl::TargetPtrT TargetPtrT;
210
Lang Hames36072da2014-05-12 21:39:59 +0000211 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
212 << ", Delta for EH: " << DeltaForEH << "\n");
Daniel Sanders523b1712014-11-01 15:52:31 +0000213 uint32_t Length = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000214 P += 4;
215 unsigned char *Ret = P + Length;
Daniel Sanders523b1712014-11-01 15:52:31 +0000216 uint32_t Offset = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000217 if (Offset == 0) // is a CIE
218 return Ret;
219
220 P += 4;
Daniel Sanders523b1712014-11-01 15:52:31 +0000221 TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000222 TargetPtrT NewLocation = FDELocation - DeltaForText;
Daniel Sanders523b1712014-11-01 15:52:31 +0000223 writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
224
Lang Hameseb195f02014-09-04 04:53:03 +0000225 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000226
227 // Skip the FDE address range
Lang Hameseb195f02014-09-04 04:53:03 +0000228 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000229
230 uint8_t Augmentationsize = *P;
231 P += 1;
232 if (Augmentationsize != 0) {
Daniel Sanders523b1712014-11-01 15:52:31 +0000233 TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000234 TargetPtrT NewLSDA = LSDA - DeltaForEH;
Daniel Sanders523b1712014-11-01 15:52:31 +0000235 writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000236 }
237
238 return Ret;
239}
240
Lang Hameseb195f02014-09-04 04:53:03 +0000241static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
242 int64_t ObjDistance = A->ObjAddress - B->ObjAddress;
243 int64_t MemDistance = A->LoadAddress - B->LoadAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000244 return ObjDistance - MemDistance;
245}
246
Lang Hameseb195f02014-09-04 04:53:03 +0000247template <typename Impl>
248void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000249
250 if (!MemMgr)
251 return;
252 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
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000274 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000275 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 Hamesb5c7b1f2014-11-26 16:54:40 +0000281RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000282 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000283 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000284 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000285 break;
Lang Hamesa5216882014-07-17 18:54:50 +0000286 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
Tim Northovere19bed72014-07-23 12:32:47 +0000287 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
Lang Hamesa5216882014-07-17 18:54:50 +0000288 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
289 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
Danil Malyshev72510f22011-07-13 07:57:58 +0000290 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000291}
292
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000293std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
294RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
295 unsigned SectionStartIdx, SectionEndIdx;
296 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
297 return llvm::make_unique<LoadedMachOObjectInfo>(*this, SectionStartIdx,
298 SectionEndIdx);
299}
300
Danil Malyshev72510f22011-07-13 07:57:58 +0000301} // end namespace llvm