blob: c89978411ddbd804ae8b3405fd2d2af48e9dc2d3 [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 {
Lang Hames25d93092014-08-08 23:12:22 +000031 unsigned NumBytes = 1 << RE.Size;
Lang Hamese1287c02014-08-29 23:17:47 +000032 uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset;
Lang Hamesdc77feb2014-08-27 17:41:06 +000033
Lang Hamese1287c02014-08-29 23:17:47 +000034 return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
Lang Hamesa5216882014-07-17 18:54:50 +000035}
36
37RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
38 ObjectImage &ObjImg, const relocation_iterator &RI,
39 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
40 const SymbolTableMap &Symbols) {
41
42 const MachOObjectFile &Obj =
43 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
44 MachO::any_relocation_info RelInfo =
45 Obj.getRelocation(RI->getRawDataRefImpl());
46 RelocationValueRef Value;
47
48 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
49 if (IsExternal) {
50 symbol_iterator Symbol = RI->getSymbol();
51 StringRef TargetName;
52 Symbol->getName(TargetName);
53 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
54 if (SI != Symbols.end()) {
55 Value.SectionID = SI->second.first;
56 Value.Addend = SI->second.second + RE.Addend;
57 } else {
58 SI = GlobalSymbolTable.find(TargetName.data());
59 if (SI != GlobalSymbolTable.end()) {
60 Value.SectionID = SI->second.first;
61 Value.Addend = SI->second.second + RE.Addend;
62 } else {
63 Value.SymbolName = TargetName.data();
64 Value.Addend = RE.Addend;
65 }
66 }
67 } else {
68 SectionRef Sec = Obj.getRelocationSection(RelInfo);
69 bool IsCode = false;
70 Sec.isText(IsCode);
71 Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
72 uint64_t Addr;
73 Sec.getAddress(Addr);
74 Value.Addend = RE.Addend - Addr;
75 }
76
77 return Value;
78}
79
80void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
81 ObjectImage &ObjImg,
Lang Hames13163652014-07-30 03:35:05 +000082 const relocation_iterator &RI,
83 unsigned OffsetToNextPC) {
Lang Hamesa5216882014-07-17 18:54:50 +000084 const MachOObjectFile &Obj =
85 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
86 MachO::any_relocation_info RelInfo =
87 Obj.getRelocation(RI->getRawDataRefImpl());
88
89 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
90 if (IsPCRel) {
91 uint64_t RelocAddr = 0;
92 RI->getAddress(RelocAddr);
Lang Hames13163652014-07-30 03:35:05 +000093 Value.Addend += RelocAddr + OffsetToNextPC;
Lang Hamesa5216882014-07-17 18:54:50 +000094 }
95}
96
97void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
98 uint64_t Value) const {
99 const SectionEntry &Section = Sections[RE.SectionID];
100 uint8_t *LocalAddress = Section.Address + RE.Offset;
101 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
102
103 dbgs() << "resolveRelocation Section: " << RE.SectionID
104 << " LocalAddress: " << format("%p", LocalAddress)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000105 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
106 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
Lang Hamesa5216882014-07-17 18:54:50 +0000107 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
108 << " Size: " << (1 << RE.Size) << "\n";
109}
110
Lang Hamesa5216882014-07-17 18:54:50 +0000111bool
112RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
113 if (InputBuffer->getBufferSize() < 4)
114 return false;
115 StringRef Magic(InputBuffer->getBufferStart(), 4);
116 if (Magic == "\xFE\xED\xFA\xCE")
117 return true;
118 if (Magic == "\xCE\xFA\xED\xFE")
119 return true;
120 if (Magic == "\xFE\xED\xFA\xCF")
121 return true;
122 if (Magic == "\xCF\xFA\xED\xFE")
123 return true;
124 return false;
125}
126
127bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
128 return Obj->isMachO();
129}
130
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000131static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
132 intptr_t DeltaForEH) {
Lang Hames36072da2014-05-12 21:39:59 +0000133 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
134 << ", Delta for EH: " << DeltaForEH << "\n");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000135 uint32_t Length = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000136 P += 4;
137 unsigned char *Ret = P + Length;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000138 uint32_t Offset = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000139 if (Offset == 0) // is a CIE
140 return Ret;
141
142 P += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000143 intptr_t FDELocation = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000144 intptr_t NewLocation = FDELocation - DeltaForText;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000145 *((intptr_t *)P) = NewLocation;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000146 P += sizeof(intptr_t);
147
148 // Skip the FDE address range
149 P += sizeof(intptr_t);
150
151 uint8_t Augmentationsize = *P;
152 P += 1;
153 if (Augmentationsize != 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000154 intptr_t LSDA = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000155 intptr_t NewLSDA = LSDA - DeltaForEH;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000156 *((intptr_t *)P) = NewLSDA;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000157 }
158
159 return Ret;
160}
161
162static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000163 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000164 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
165 return ObjDistance - MemDistance;
166}
167
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000168void RuntimeDyldMachO::registerEHFrames() {
169
170 if (!MemMgr)
171 return;
172 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
173 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
174 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
175 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
176 continue;
177 SectionEntry *Text = &Sections[SectionInfo.TextSID];
178 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +0000179 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000180 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
181 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
182
183 intptr_t DeltaForText = computeDelta(Text, EHFrame);
184 intptr_t DeltaForEH = 0;
185 if (ExceptTab)
186 DeltaForEH = computeDelta(ExceptTab, EHFrame);
187
188 unsigned char *P = EHFrame->Address;
189 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000190 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000191 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000192 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000193
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000194 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000195 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000196 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000197 UnregisteredEHFrameSections.clear();
198}
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000199
Lang Hamesa5216882014-07-17 18:54:50 +0000200std::unique_ptr<RuntimeDyldMachO>
201llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000202 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000203 default:
Lang Hamesa5216882014-07-17 18:54:50 +0000204 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000205 break;
Lang Hamesa5216882014-07-17 18:54:50 +0000206 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
Tim Northovere19bed72014-07-23 12:32:47 +0000207 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
Lang Hamesa5216882014-07-17 18:54:50 +0000208 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
209 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
Danil Malyshev72510f22011-07-13 07:57:58 +0000210 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000211}
212
Danil Malyshev72510f22011-07-13 07:57:58 +0000213} // end namespace llvm