blob: d4a680d749a163e95a2aa33a727ea6965b0187ab [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
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 Hames6bfd3982015-01-16 23:13:56 +000068 RTDyldSymbolTable::const_iterator SI =
Lang Hamesa5cd9502014-11-27 05:40:13 +000069 GlobalSymbolTable.find(TargetName.data());
70 if (SI != GlobalSymbolTable.end()) {
Lang Hames6bfd3982015-01-16 23:13:56 +000071 const auto &SymInfo = SI->second;
72 Value.SectionID = SymInfo.getSectionID();
73 Value.Offset = SymInfo.getOffset() + RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000074 } else {
Lang Hamesa5cd9502014-11-27 05:40:13 +000075 Value.SymbolName = TargetName.data();
76 Value.Offset = RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000077 }
78 } else {
Keno Fischerc780e8e2015-05-21 21:24:32 +000079 SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
Rafael Espindola80291272014-10-08 15:28:58 +000080 bool IsCode = Sec.isText();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000081 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola80291272014-10-08 15:28:58 +000082 uint64_t Addr = Sec.getAddress();
Lang Hamesca279c22014-09-07 04:03:32 +000083 Value.Offset = RE.Addend - Addr;
Lang Hamesa5216882014-07-17 18:54:50 +000084 }
85
86 return Value;
87}
88
89void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000090 const ObjectFile &BaseTObj,
Lang Hames13163652014-07-30 03:35:05 +000091 const relocation_iterator &RI,
92 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000093 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
98 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
99 if (IsPCRel) {
100 uint64_t RelocAddr = 0;
101 RI->getAddress(RelocAddr);
Lang Hamesca279c22014-09-07 04:03:32 +0000102 Value.Offset += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +0000103 }
104}
105
106void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
107 uint64_t Value) const {
108 const SectionEntry &Section = Sections[RE.SectionID];
109 uint8_t *LocalAddress = Section.Address + RE.Offset;
110 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
111
112 dbgs() << "resolveRelocation Section: " << RE.SectionID
113 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000114 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
115 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000116 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
117 << " Size: " << (1 << RE.Size) << "\n";
118}
119
Lang Hames6f1048f2014-09-11 19:21:14 +0000120section_iterator
121RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
122 uint64_t Addr) {
123 section_iterator SI = Obj.section_begin();
124 section_iterator SE = Obj.section_end();
125
126 for (; SI != SE; ++SI) {
Rafael Espindola80291272014-10-08 15:28:58 +0000127 uint64_t SAddr = SI->getAddress();
128 uint64_t SSize = SI->getSize();
Lang Hames6f1048f2014-09-11 19:21:14 +0000129 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
130 return SI;
131 }
132
133 return SE;
134}
135
136
137// Populate __pointers section.
138void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000139 const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000140 const SectionRef &PTSection,
141 unsigned PTSectionID) {
142 assert(!Obj.is64Bit() &&
143 "Pointer table section not supported in 64-bit MachO.");
144
145 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
146 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
147 uint32_t PTSectionSize = Sec32.size;
148 unsigned FirstIndirectSymbol = Sec32.reserved1;
149 const unsigned PTEntrySize = 4;
150 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
151 unsigned PTEntryOffset = 0;
152
153 assert((PTSectionSize % PTEntrySize) == 0 &&
154 "Pointers section does not contain a whole number of stubs?");
155
156 DEBUG(dbgs() << "Populating pointer table section "
157 << Sections[PTSectionID].Name
158 << ", Section ID " << PTSectionID << ", "
159 << NumPTEntries << " entries, " << PTEntrySize
160 << " bytes each:\n");
161
162 for (unsigned i = 0; i < NumPTEntries; ++i) {
163 unsigned SymbolIndex =
164 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
165 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
166 StringRef IndirectSymbolName;
167 SI->getName(IndirectSymbolName);
168 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
169 << ", PT offset: " << PTEntryOffset << "\n");
170 RelocationEntry RE(PTSectionID, PTEntryOffset,
171 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
172 addRelocationForSymbol(RE, IndirectSymbolName);
173 PTEntryOffset += PTEntrySize;
174 }
175}
176
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000177bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
178 return Obj.isMachO();
Lang Hamesa5216882014-07-17 18:54:50 +0000179}
180
Lang Hameseb195f02014-09-04 04:53:03 +0000181template <typename Impl>
Lang Hames38aac642015-04-15 03:39:22 +0000182void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
Lang Hameseb195f02014-09-04 04:53:03 +0000183 ObjSectionToIDMap &SectionMap) {
184 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
185 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
186 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
Lang Hameseb195f02014-09-04 04:53:03 +0000187
Lang Hames38aac642015-04-15 03:39:22 +0000188 for (const auto &Section : Obj.sections()) {
Lang Hameseb195f02014-09-04 04:53:03 +0000189 StringRef Name;
190 Section.getName(Name);
Lang Hames38aac642015-04-15 03:39:22 +0000191
192 // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
193 // if they're present. Otherwise call down to the impl to handle other
194 // sections that have already been emitted.
195 if (Name == "__text")
196 TextSID = findOrEmitSection(Obj, Section, true, SectionMap);
197 else if (Name == "__eh_frame")
198 EHFrameSID = findOrEmitSection(Obj, Section, false, SectionMap);
Lang Hameseb195f02014-09-04 04:53:03 +0000199 else if (Name == "__gcc_except_tab")
Lang Hames38aac642015-04-15 03:39:22 +0000200 ExceptTabSID = findOrEmitSection(Obj, Section, true, SectionMap);
201 else {
202 auto I = SectionMap.find(Section);
203 if (I != SectionMap.end())
204 impl().finalizeSection(Obj, I->second, Section);
205 }
Lang Hameseb195f02014-09-04 04:53:03 +0000206 }
207 UnregisteredEHFrameSections.push_back(
208 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
209}
210
211template <typename Impl>
212unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
213 int64_t DeltaForText,
214 int64_t DeltaForEH) {
215 typedef typename Impl::TargetPtrT TargetPtrT;
216
Lang Hames36072da2014-05-12 21:39:59 +0000217 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
218 << ", Delta for EH: " << DeltaForEH << "\n");
Daniel Sanders523b1712014-11-01 15:52:31 +0000219 uint32_t Length = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000220 P += 4;
221 unsigned char *Ret = P + Length;
Daniel Sanders523b1712014-11-01 15:52:31 +0000222 uint32_t Offset = readBytesUnaligned(P, 4);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000223 if (Offset == 0) // is a CIE
224 return Ret;
225
226 P += 4;
Daniel Sanders523b1712014-11-01 15:52:31 +0000227 TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000228 TargetPtrT NewLocation = FDELocation - DeltaForText;
Daniel Sanders523b1712014-11-01 15:52:31 +0000229 writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
230
Lang Hameseb195f02014-09-04 04:53:03 +0000231 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000232
233 // Skip the FDE address range
Lang Hameseb195f02014-09-04 04:53:03 +0000234 P += sizeof(TargetPtrT);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000235
236 uint8_t Augmentationsize = *P;
237 P += 1;
238 if (Augmentationsize != 0) {
Daniel Sanders523b1712014-11-01 15:52:31 +0000239 TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
Lang Hameseb195f02014-09-04 04:53:03 +0000240 TargetPtrT NewLSDA = LSDA - DeltaForEH;
Daniel Sanders523b1712014-11-01 15:52:31 +0000241 writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000242 }
243
244 return Ret;
245}
246
Lang Hameseb195f02014-09-04 04:53:03 +0000247static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
Lang Hames042e35c2015-04-15 04:46:01 +0000248 int64_t ObjDistance =
249 static_cast<int64_t>(A->ObjAddress) - static_cast<int64_t>(B->ObjAddress);
Lang Hameseb195f02014-09-04 04:53:03 +0000250 int64_t MemDistance = A->LoadAddress - B->LoadAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000251 return ObjDistance - MemDistance;
252}
253
Lang Hameseb195f02014-09-04 04:53:03 +0000254template <typename Impl>
255void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000256
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000257 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
258 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
259 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
260 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
261 continue;
262 SectionEntry *Text = &Sections[SectionInfo.TextSID];
263 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000264 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000265 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
266 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
267
Lang Hameseb195f02014-09-04 04:53:03 +0000268 int64_t DeltaForText = computeDelta(Text, EHFrame);
269 int64_t DeltaForEH = 0;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000270 if (ExceptTab)
271 DeltaForEH = computeDelta(ExceptTab, EHFrame);
272
273 unsigned char *P = EHFrame->Address;
274 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000275 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000276 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000277 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000278
Lang Hames633fe142015-03-30 03:37:06 +0000279 MemMgr.registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
280 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000281 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000282 UnregisteredEHFrameSections.clear();
283}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000284
Lang Hamesa5216882014-07-17 18:54:50 +0000285std::unique_ptr<RuntimeDyldMachO>
Lang Hames633fe142015-03-30 03:37:06 +0000286RuntimeDyldMachO::create(Triple::ArchType Arch,
287 RuntimeDyld::MemoryManager &MemMgr,
288 RuntimeDyld::SymbolResolver &Resolver) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000289 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000290 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000291 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000292 break;
Lang Hames633fe142015-03-30 03:37:06 +0000293 case Triple::arm:
294 return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
295 case Triple::aarch64:
296 return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
297 case Triple::x86:
298 return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
299 case Triple::x86_64:
300 return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
Danil Malyshev72510f22011-07-13 07:57:58 +0000301 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000302}
303
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000304std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
305RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
306 unsigned SectionStartIdx, SectionEndIdx;
307 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
308 return llvm::make_unique<LoadedMachOObjectInfo>(*this, SectionStartIdx,
309 SectionEndIdx);
310}
311
Danil Malyshev72510f22011-07-13 07:57:58 +0000312} // end namespace llvm