blob: 81020dc771273cf2bd293541a4e94c370068ebfb [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 Hamesa5216882014-07-17 18:54:50 +000055 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
56 const SymbolTableMap &Symbols) {
57
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);
69 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
70 if (SI != Symbols.end()) {
71 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 {
74 SI = GlobalSymbolTable.find(TargetName.data());
75 if (SI != GlobalSymbolTable.end()) {
76 Value.SectionID = SI->second.first;
Lang Hamesca279c22014-09-07 04:03:32 +000077 Value.Offset = SI->second.second + RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000078 } else {
79 Value.SymbolName = TargetName.data();
Lang Hamesca279c22014-09-07 04:03:32 +000080 Value.Offset = RE.Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000081 }
82 }
83 } else {
84 SectionRef Sec = Obj.getRelocationSection(RelInfo);
Rafael Espindola80291272014-10-08 15:28:58 +000085 bool IsCode = Sec.isText();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000086 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola80291272014-10-08 15:28:58 +000087 uint64_t Addr = Sec.getAddress();
Lang Hamesca279c22014-09-07 04:03:32 +000088 Value.Offset = RE.Addend - Addr;
Lang Hamesa5216882014-07-17 18:54:50 +000089 }
90
91 return Value;
92}
93
94void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000095 const ObjectFile &BaseTObj,
Lang Hames13163652014-07-30 03:35:05 +000096 const relocation_iterator &RI,
97 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000098 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000099 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hamesa5216882014-07-17 18:54:50 +0000100 MachO::any_relocation_info RelInfo =
101 Obj.getRelocation(RI->getRawDataRefImpl());
102
103 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
104 if (IsPCRel) {
105 uint64_t RelocAddr = 0;
106 RI->getAddress(RelocAddr);
Lang Hamesca279c22014-09-07 04:03:32 +0000107 Value.Offset += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +0000108 }
109}
110
111void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
112 uint64_t Value) const {
113 const SectionEntry &Section = Sections[RE.SectionID];
114 uint8_t *LocalAddress = Section.Address + RE.Offset;
115 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
116
117 dbgs() << "resolveRelocation Section: " << RE.SectionID
118 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000119 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
120 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000121 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
122 << " Size: " << (1 << RE.Size) << "\n";
123}
124
Lang Hames6f1048f2014-09-11 19:21:14 +0000125section_iterator
126RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
127 uint64_t Addr) {
128 section_iterator SI = Obj.section_begin();
129 section_iterator SE = Obj.section_end();
130
131 for (; SI != SE; ++SI) {
Rafael Espindola80291272014-10-08 15:28:58 +0000132 uint64_t SAddr = SI->getAddress();
133 uint64_t SSize = SI->getSize();
Lang Hames6f1048f2014-09-11 19:21:14 +0000134 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
135 return SI;
136 }
137
138 return SE;
139}
140
141
142// Populate __pointers section.
143void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000144 const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000145 const SectionRef &PTSection,
146 unsigned PTSectionID) {
147 assert(!Obj.is64Bit() &&
148 "Pointer table section not supported in 64-bit MachO.");
149
150 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
151 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
152 uint32_t PTSectionSize = Sec32.size;
153 unsigned FirstIndirectSymbol = Sec32.reserved1;
154 const unsigned PTEntrySize = 4;
155 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
156 unsigned PTEntryOffset = 0;
157
158 assert((PTSectionSize % PTEntrySize) == 0 &&
159 "Pointers section does not contain a whole number of stubs?");
160
161 DEBUG(dbgs() << "Populating pointer table section "
162 << Sections[PTSectionID].Name
163 << ", Section ID " << PTSectionID << ", "
164 << NumPTEntries << " entries, " << PTEntrySize
165 << " bytes each:\n");
166
167 for (unsigned i = 0; i < NumPTEntries; ++i) {
168 unsigned SymbolIndex =
169 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
170 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
171 StringRef IndirectSymbolName;
172 SI->getName(IndirectSymbolName);
173 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
174 << ", PT offset: " << PTEntryOffset << "\n");
175 RelocationEntry RE(PTSectionID, PTEntryOffset,
176 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
177 addRelocationForSymbol(RE, IndirectSymbolName);
178 PTEntryOffset += PTEntrySize;
179 }
180}
181
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000182bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
183 return Obj.isMachO();
Lang Hamesa5216882014-07-17 18:54:50 +0000184}
185
Lang Hameseb195f02014-09-04 04:53:03 +0000186template <typename Impl>
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000187void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &ObjImg,
Lang Hameseb195f02014-09-04 04:53:03 +0000188 ObjSectionToIDMap &SectionMap) {
189 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
190 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
191 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
192 ObjSectionToIDMap::iterator i, e;
193
194 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
195 const SectionRef &Section = i->first;
196 StringRef Name;
197 Section.getName(Name);
198 if (Name == "__eh_frame")
199 EHFrameSID = i->second;
200 else if (Name == "__text")
201 TextSID = i->second;
202 else if (Name == "__gcc_except_tab")
203 ExceptTabSID = i->second;
204 else
205 impl().finalizeSection(ObjImg, i->second, Section);
206 }
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) {
248 int64_t ObjDistance = A->ObjAddress - B->ObjAddress;
249 int64_t MemDistance = A->LoadAddress - B->LoadAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000250 return ObjDistance - MemDistance;
251}
252
Lang Hameseb195f02014-09-04 04:53:03 +0000253template <typename Impl>
254void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000255
256 if (!MemMgr)
257 return;
258 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
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000280 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000281 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 Hamesb5c7b1f2014-11-26 16:54:40 +0000287RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000288 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000289 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000290 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000291 break;
Lang Hamesa5216882014-07-17 18:54:50 +0000292 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
Tim Northovere19bed72014-07-23 12:32:47 +0000293 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
Lang Hamesa5216882014-07-17 18:54:50 +0000294 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
295 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
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