blob: 9e4d3ac82afb3d21cd86cf60a244d572fc580b34 [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
Danil Malyshev72510f22011-07-13 07:57:58 +000028namespace llvm {
29
Lang Hames25d93092014-08-08 23:12:22 +000030int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
31 const SectionEntry &Section = Sections[RE.SectionID];
Lang Hames25d93092014-08-08 23:12:22 +000032 unsigned NumBytes = 1 << RE.Size;
Juergen Ributzka175b78b2014-07-22 21:42:46 +000033 int64_t Addend = 0;
Lang Hamesdc77feb2014-08-27 17:41:06 +000034 uint8_t *LocalAddress = Section.Address + RE.Offset;
35 uint8_t *Dst = reinterpret_cast<uint8_t*>(&Addend);
36
37 if (IsTargetLittleEndian == sys::IsLittleEndianHost) {
38 if (!sys::IsLittleEndianHost)
39 Dst += sizeof(Addend) - NumBytes;
40 memcpy(Dst, LocalAddress, NumBytes);
41 } else {
42 Dst += NumBytes - 1;
43 for (unsigned i = 0; i < NumBytes; ++i)
44 *Dst-- = *LocalAddress++;
45 }
46
Lang Hames3fda7d82014-07-19 00:19:17 +000047 return Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000048}
49
50RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
51 ObjectImage &ObjImg, const relocation_iterator &RI,
52 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
53 const SymbolTableMap &Symbols) {
54
55 const MachOObjectFile &Obj =
56 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
57 MachO::any_relocation_info RelInfo =
58 Obj.getRelocation(RI->getRawDataRefImpl());
59 RelocationValueRef Value;
60
61 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
62 if (IsExternal) {
63 symbol_iterator Symbol = RI->getSymbol();
64 StringRef TargetName;
65 Symbol->getName(TargetName);
66 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
67 if (SI != Symbols.end()) {
68 Value.SectionID = SI->second.first;
69 Value.Addend = SI->second.second + RE.Addend;
70 } else {
71 SI = GlobalSymbolTable.find(TargetName.data());
72 if (SI != GlobalSymbolTable.end()) {
73 Value.SectionID = SI->second.first;
74 Value.Addend = SI->second.second + RE.Addend;
75 } else {
76 Value.SymbolName = TargetName.data();
77 Value.Addend = RE.Addend;
78 }
79 }
80 } else {
81 SectionRef Sec = Obj.getRelocationSection(RelInfo);
82 bool IsCode = false;
83 Sec.isText(IsCode);
84 Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
85 uint64_t Addr;
86 Sec.getAddress(Addr);
87 Value.Addend = RE.Addend - Addr;
88 }
89
90 return Value;
91}
92
93void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
94 ObjectImage &ObjImg,
Lang Hames13163652014-07-30 03:35:05 +000095 const relocation_iterator &RI,
96 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000097 const MachOObjectFile &Obj =
98 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
99 MachO::any_relocation_info RelInfo =
100 Obj.getRelocation(RI->getRawDataRefImpl());
101
102 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
103 if (IsPCRel) {
104 uint64_t RelocAddr = 0;
105 RI->getAddress(RelocAddr);
Lang Hames13163652014-07-30 03:35:05 +0000106 Value.Addend += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +0000107 }
108}
109
110void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
111 uint64_t Value) const {
112 const SectionEntry &Section = Sections[RE.SectionID];
113 uint8_t *LocalAddress = Section.Address + RE.Offset;
114 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
115
116 dbgs() << "resolveRelocation Section: " << RE.SectionID
117 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hames86b08f02014-08-25 18:37:38 +0000118 << " FinalAddress: " << format("0x%x", FinalAddress)
119 << " Value: " << format("0x%x", Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000120 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
121 << " Size: " << (1 << RE.Size) << "\n";
122}
123
Lang Hames4f867bf2014-08-18 21:43:16 +0000124bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Dst, uint64_t Value,
Lang Hamesa5216882014-07-17 18:54:50 +0000125 unsigned Size) {
Lang Hames4f867bf2014-08-18 21:43:16 +0000126
Lang Hamesdc77feb2014-08-27 17:41:06 +0000127 uint8_t *Src = reinterpret_cast<uint8_t*>(&Value);
Lang Hames4f867bf2014-08-18 21:43:16 +0000128 // If host and target endianness match use memcpy, otherwise copy in reverse
129 // order.
Lang Hamesdc77feb2014-08-27 17:41:06 +0000130 if (IsTargetLittleEndian == sys::IsLittleEndianHost) {
131 if (!sys::IsLittleEndianHost)
132 Src += sizeof(Value) - Size;
133 memcpy(Dst, Src, Size);
134 } else {
135 Src += Size - 1;
Lang Hames4f867bf2014-08-18 21:43:16 +0000136 for (unsigned i = 0; i < Size; ++i)
137 *Dst++ = *Src--;
Lang Hamesa5216882014-07-17 18:54:50 +0000138 }
139
140 return false;
141}
142
143bool
144RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
145 if (InputBuffer->getBufferSize() < 4)
146 return false;
147 StringRef Magic(InputBuffer->getBufferStart(), 4);
148 if (Magic == "\xFE\xED\xFA\xCE")
149 return true;
150 if (Magic == "\xCE\xFA\xED\xFE")
151 return true;
152 if (Magic == "\xFE\xED\xFA\xCF")
153 return true;
154 if (Magic == "\xCF\xFA\xED\xFE")
155 return true;
156 return false;
157}
158
159bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
160 return Obj->isMachO();
161}
162
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000163static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
164 intptr_t DeltaForEH) {
Lang Hames36072da2014-05-12 21:39:59 +0000165 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
166 << ", Delta for EH: " << DeltaForEH << "\n");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000167 uint32_t Length = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000168 P += 4;
169 unsigned char *Ret = P + Length;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000170 uint32_t Offset = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000171 if (Offset == 0) // is a CIE
172 return Ret;
173
174 P += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000175 intptr_t FDELocation = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000176 intptr_t NewLocation = FDELocation - DeltaForText;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000177 *((intptr_t *)P) = NewLocation;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000178 P += sizeof(intptr_t);
179
180 // Skip the FDE address range
181 P += sizeof(intptr_t);
182
183 uint8_t Augmentationsize = *P;
184 P += 1;
185 if (Augmentationsize != 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000186 intptr_t LSDA = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000187 intptr_t NewLSDA = LSDA - DeltaForEH;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000188 *((intptr_t *)P) = NewLSDA;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000189 }
190
191 return Ret;
192}
193
194static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000195 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000196 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
197 return ObjDistance - MemDistance;
198}
199
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000200void RuntimeDyldMachO::registerEHFrames() {
201
202 if (!MemMgr)
203 return;
204 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
205 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
206 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
207 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
208 continue;
209 SectionEntry *Text = &Sections[SectionInfo.TextSID];
210 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000211 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000212 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
213 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
214
215 intptr_t DeltaForText = computeDelta(Text, EHFrame);
216 intptr_t DeltaForEH = 0;
217 if (ExceptTab)
218 DeltaForEH = computeDelta(ExceptTab, EHFrame);
219
220 unsigned char *P = EHFrame->Address;
221 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000222 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000223 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000224 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000225
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000226 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000227 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000228 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000229 UnregisteredEHFrameSections.clear();
230}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000231
Lang Hamesa5216882014-07-17 18:54:50 +0000232std::unique_ptr<RuntimeDyldMachO>
233llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000234 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000235 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000236 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000237 break;
Lang Hamesa5216882014-07-17 18:54:50 +0000238 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
Tim Northovere19bed72014-07-23 12:32:47 +0000239 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
Lang Hamesa5216882014-07-17 18:54:50 +0000240 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
241 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
Danil Malyshev72510f22011-07-13 07:57:58 +0000242 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000243}
244
Danil Malyshev72510f22011-07-13 07:57:58 +0000245} // end namespace llvm