blob: 35736a4df2c9d92c99401cfb56b7c951898dae03 [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];
32 uint8_t *LocalAddress = Section.Address + RE.Offset;
33 unsigned NumBytes = 1 << RE.Size;
Juergen Ributzka175b78b2014-07-22 21:42:46 +000034 int64_t Addend = 0;
Lang Hamesa5216882014-07-17 18:54:50 +000035 memcpy(&Addend, LocalAddress, NumBytes);
Lang Hames3fda7d82014-07-19 00:19:17 +000036 return Addend;
Lang Hamesa5216882014-07-17 18:54:50 +000037}
38
39RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
40 ObjectImage &ObjImg, const relocation_iterator &RI,
41 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
42 const SymbolTableMap &Symbols) {
43
44 const MachOObjectFile &Obj =
45 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
46 MachO::any_relocation_info RelInfo =
47 Obj.getRelocation(RI->getRawDataRefImpl());
48 RelocationValueRef Value;
49
50 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
51 if (IsExternal) {
52 symbol_iterator Symbol = RI->getSymbol();
53 StringRef TargetName;
54 Symbol->getName(TargetName);
55 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
56 if (SI != Symbols.end()) {
57 Value.SectionID = SI->second.first;
58 Value.Addend = SI->second.second + RE.Addend;
59 } else {
60 SI = GlobalSymbolTable.find(TargetName.data());
61 if (SI != GlobalSymbolTable.end()) {
62 Value.SectionID = SI->second.first;
63 Value.Addend = SI->second.second + RE.Addend;
64 } else {
65 Value.SymbolName = TargetName.data();
66 Value.Addend = RE.Addend;
67 }
68 }
69 } else {
70 SectionRef Sec = Obj.getRelocationSection(RelInfo);
71 bool IsCode = false;
72 Sec.isText(IsCode);
73 Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
74 uint64_t Addr;
75 Sec.getAddress(Addr);
76 Value.Addend = RE.Addend - Addr;
77 }
78
79 return Value;
80}
81
82void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
83 ObjectImage &ObjImg,
Lang Hames13163652014-07-30 03:35:05 +000084 const relocation_iterator &RI,
85 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000086 const MachOObjectFile &Obj =
87 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
88 MachO::any_relocation_info RelInfo =
89 Obj.getRelocation(RI->getRawDataRefImpl());
90
91 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
92 if (IsPCRel) {
93 uint64_t RelocAddr = 0;
94 RI->getAddress(RelocAddr);
Lang Hames13163652014-07-30 03:35:05 +000095 Value.Addend += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +000096 }
97}
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 Hames86b08f02014-08-25 18:37:38 +0000107 << " FinalAddress: " << format("0x%x", FinalAddress)
108 << " Value: " << format("0x%x", 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 Hames4f867bf2014-08-18 21:43:16 +0000113bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Dst, uint64_t Value,
Lang Hamesa5216882014-07-17 18:54:50 +0000114 unsigned Size) {
Lang Hames4f867bf2014-08-18 21:43:16 +0000115
116
117 // If host and target endianness match use memcpy, otherwise copy in reverse
118 // order.
119 if (IsTargetLittleEndian == sys::IsLittleEndianHost)
120 memcpy(Dst, &Value, Size);
121 else {
122 uint8_t *Src = reinterpret_cast<uint8_t*>(&Value) + Size - 1;
123 for (unsigned i = 0; i < Size; ++i)
124 *Dst++ = *Src--;
Lang Hamesa5216882014-07-17 18:54:50 +0000125 }
126
127 return false;
128}
129
130bool
131RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
132 if (InputBuffer->getBufferSize() < 4)
133 return false;
134 StringRef Magic(InputBuffer->getBufferStart(), 4);
135 if (Magic == "\xFE\xED\xFA\xCE")
136 return true;
137 if (Magic == "\xCE\xFA\xED\xFE")
138 return true;
139 if (Magic == "\xFE\xED\xFA\xCF")
140 return true;
141 if (Magic == "\xCF\xFA\xED\xFE")
142 return true;
143 return false;
144}
145
146bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
147 return Obj->isMachO();
148}
149
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000150static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
151 intptr_t DeltaForEH) {
Lang Hames36072da2014-05-12 21:39:59 +0000152 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
153 << ", Delta for EH: " << DeltaForEH << "\n");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000154 uint32_t Length = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000155 P += 4;
156 unsigned char *Ret = P + Length;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000157 uint32_t Offset = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000158 if (Offset == 0) // is a CIE
159 return Ret;
160
161 P += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000162 intptr_t FDELocation = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000163 intptr_t NewLocation = FDELocation - DeltaForText;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000164 *((intptr_t *)P) = NewLocation;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000165 P += sizeof(intptr_t);
166
167 // Skip the FDE address range
168 P += sizeof(intptr_t);
169
170 uint8_t Augmentationsize = *P;
171 P += 1;
172 if (Augmentationsize != 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000173 intptr_t LSDA = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000174 intptr_t NewLSDA = LSDA - DeltaForEH;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000175 *((intptr_t *)P) = NewLSDA;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000176 }
177
178 return Ret;
179}
180
181static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000182 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000183 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
184 return ObjDistance - MemDistance;
185}
186
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000187void RuntimeDyldMachO::registerEHFrames() {
188
189 if (!MemMgr)
190 return;
191 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
192 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
193 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
194 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
195 continue;
196 SectionEntry *Text = &Sections[SectionInfo.TextSID];
197 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000198 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000199 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
200 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
201
202 intptr_t DeltaForText = computeDelta(Text, EHFrame);
203 intptr_t DeltaForEH = 0;
204 if (ExceptTab)
205 DeltaForEH = computeDelta(ExceptTab, EHFrame);
206
207 unsigned char *P = EHFrame->Address;
208 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000209 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000210 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000211 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000212
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000213 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000214 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000215 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000216 UnregisteredEHFrameSections.clear();
217}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000218
Lang Hamesa5216882014-07-17 18:54:50 +0000219std::unique_ptr<RuntimeDyldMachO>
220llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000221 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000222 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000223 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000224 break;
Lang Hamesa5216882014-07-17 18:54:50 +0000225 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
Tim Northovere19bed72014-07-23 12:32:47 +0000226 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
Lang Hamesa5216882014-07-17 18:54:50 +0000227 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
228 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
Danil Malyshev72510f22011-07-13 07:57:58 +0000229 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000230}
231
Danil Malyshev72510f22011-07-13 07:57:58 +0000232} // end namespace llvm