blob: a2320584d194b484e4a8975d18d806cec7f37721 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
Danil Malyshevcf852dc2011-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
14#define DEBUG_TYPE "dyld"
Eli Bendersky76463fd2012-01-22 07:05:02 +000015#include "RuntimeDyldMachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000019using namespace llvm;
20using namespace llvm::object;
21
22namespace llvm {
23
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +000024static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText, intptr_t DeltaForEH) {
25 uint32_t Length = *((uint32_t*)P);
26 P += 4;
27 unsigned char *Ret = P + Length;
28 uint32_t Offset = *((uint32_t*)P);
29 if (Offset == 0) // is a CIE
30 return Ret;
31
32 P += 4;
33 intptr_t FDELocation = *((intptr_t*)P);
34 intptr_t NewLocation = FDELocation - DeltaForText;
35 *((intptr_t*)P) = NewLocation;
36 P += sizeof(intptr_t);
37
38 // Skip the FDE address range
39 P += sizeof(intptr_t);
40
41 uint8_t Augmentationsize = *P;
42 P += 1;
43 if (Augmentationsize != 0) {
44 intptr_t LSDA = *((intptr_t*)P);
45 intptr_t NewLSDA = LSDA - DeltaForEH;
46 *((intptr_t*)P) = NewLSDA;
47 }
48
49 return Ret;
50}
51
52static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
53 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
54 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
55 return ObjDistance - MemDistance;
56}
57
58StringRef RuntimeDyldMachO::getEHFrameSection() {
59 SectionEntry *Text = NULL;
60 SectionEntry *EHFrame = NULL;
61 SectionEntry *ExceptTab = NULL;
62 for (int i = 0, e = Sections.size(); i != e; ++i) {
63 if (Sections[i].Name == "__eh_frame")
64 EHFrame = &Sections[i];
65 else if (Sections[i].Name == "__text")
66 Text = &Sections[i];
67 else if (Sections[i].Name == "__gcc_except_tab")
68 ExceptTab = &Sections[i];
69 }
70 if (Text == NULL || EHFrame == NULL)
71 return StringRef();
72
73 intptr_t DeltaForText = computeDelta(Text, EHFrame);
74 intptr_t DeltaForEH = 0;
75 if (ExceptTab)
76 DeltaForEH = computeDelta(ExceptTab, EHFrame);
77
78 unsigned char *P = EHFrame->Address;
79 unsigned char *End = P + EHFrame->Size;
80 do {
81 P = processFDE(P, DeltaForText, DeltaForEH);
82 } while(P != End);
83
84 return StringRef((char*)EHFrame->Address, EHFrame->Size);
85}
86
Rafael Espindola87b50172013-04-29 17:24:34 +000087void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
88 uint64_t Value) {
89 const SectionEntry &Section = Sections[RE.SectionID];
90 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
91 RE.IsPCRel, RE.Size);
92}
93
Andrew Kaylora307a1c2012-11-02 19:45:23 +000094void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
95 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000096 uint64_t Value,
97 uint32_t Type,
Rafael Espindola87b50172013-04-29 17:24:34 +000098 int64_t Addend,
99 bool isPCRel,
100 unsigned LogSize) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000101 uint8_t *LocalAddress = Section.Address + Offset;
102 uint64_t FinalAddress = Section.LoadAddress + Offset;
Rafael Espindola87b50172013-04-29 17:24:34 +0000103 unsigned MachoType = Type;
104 unsigned Size = 1 << LogSize;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000105
Eli Bendersky6d15e872012-04-30 10:06:27 +0000106 DEBUG(dbgs() << "resolveRelocation LocalAddress: "
107 << format("%p", LocalAddress)
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000108 << " FinalAddress: " << format("%p", FinalAddress)
109 << " Value: " << format("%p", Value)
110 << " Addend: " << Addend
111 << " isPCRel: " << isPCRel
112 << " MachoType: " << MachoType
113 << " Size: " << Size
114 << "\n");
115
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000116 // This just dispatches to the proper target specific routine.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000117 switch (Arch) {
Craig Topper85814382012-02-07 05:05:23 +0000118 default: llvm_unreachable("Unsupported CPU type!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000119 case Triple::x86_64:
120 resolveX86_64Relocation(LocalAddress,
121 FinalAddress,
122 (uintptr_t)Value,
123 isPCRel,
124 MachoType,
125 Size,
126 Addend);
127 break;
128 case Triple::x86:
129 resolveI386Relocation(LocalAddress,
Eli Bendersky6d15e872012-04-30 10:06:27 +0000130 FinalAddress,
131 (uintptr_t)Value,
132 isPCRel,
Jim Grosbachba9ba9f2012-09-13 01:24:32 +0000133 MachoType,
Eli Bendersky6d15e872012-04-30 10:06:27 +0000134 Size,
135 Addend);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000136 break;
137 case Triple::arm: // Fall through.
138 case Triple::thumb:
139 resolveARMRelocation(LocalAddress,
140 FinalAddress,
141 (uintptr_t)Value,
142 isPCRel,
143 MachoType,
144 Size,
145 Addend);
146 break;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000147 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000148}
149
Eli Bendersky6d15e872012-04-30 10:06:27 +0000150bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
151 uint64_t FinalAddress,
152 uint64_t Value,
153 bool isPCRel,
154 unsigned Type,
155 unsigned Size,
156 int64_t Addend) {
Sean Callananb38aae42012-03-26 20:45:52 +0000157 if (isPCRel)
158 Value -= FinalAddress + 4; // see resolveX86_64Relocation
159
160 switch (Type) {
161 default:
162 llvm_unreachable("Invalid relocation type!");
163 case macho::RIT_Vanilla: {
164 uint8_t *p = LocalAddress;
165 uint64_t ValueToWrite = Value + Addend;
166 for (unsigned i = 0; i < Size; ++i) {
167 *p++ = (uint8_t)(ValueToWrite & 0xff);
168 ValueToWrite >>= 8;
169 }
Jim Grosbach6f6f1712013-01-31 19:46:28 +0000170 return false;
Sean Callananb38aae42012-03-26 20:45:52 +0000171 }
172 case macho::RIT_Difference:
173 case macho::RIT_Generic_LocalDifference:
174 case macho::RIT_Generic_PreboundLazyPointer:
175 return Error("Relocation type not implemented yet!");
176 }
177}
178
Eli Bendersky6d15e872012-04-30 10:06:27 +0000179bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
180 uint64_t FinalAddress,
181 uint64_t Value,
182 bool isPCRel,
183 unsigned Type,
184 unsigned Size,
185 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000186 // If the relocation is PC-relative, the value to be encoded is the
187 // pointer difference.
188 if (isPCRel)
189 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
190 // address. Is that expected? Only for branches, perhaps?
Sean Callanan61dfa772012-03-07 23:05:25 +0000191 Value -= FinalAddress + 4;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000192
193 switch(Type) {
194 default:
195 llvm_unreachable("Invalid relocation type!");
Jim Grosbach652ca2f2012-01-16 23:50:49 +0000196 case macho::RIT_X86_64_Signed1:
197 case macho::RIT_X86_64_Signed2:
198 case macho::RIT_X86_64_Signed4:
199 case macho::RIT_X86_64_Signed:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000200 case macho::RIT_X86_64_Unsigned:
201 case macho::RIT_X86_64_Branch: {
Jim Grosbach652ca2f2012-01-16 23:50:49 +0000202 Value += Addend;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000203 // Mask in the target value a byte at a time (we don't have an alignment
204 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000205 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000206 for (unsigned i = 0; i < Size; ++i) {
207 *p++ = (uint8_t)Value;
208 Value >>= 8;
209 }
210 return false;
211 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000212 case macho::RIT_X86_64_GOTLoad:
213 case macho::RIT_X86_64_GOT:
214 case macho::RIT_X86_64_Subtractor:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000215 case macho::RIT_X86_64_TLV:
216 return Error("Relocation type not implemented yet!");
217 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000218}
219
Eli Bendersky6d15e872012-04-30 10:06:27 +0000220bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
221 uint64_t FinalAddress,
222 uint64_t Value,
223 bool isPCRel,
224 unsigned Type,
225 unsigned Size,
226 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000227 // If the relocation is PC-relative, the value to be encoded is the
228 // pointer difference.
229 if (isPCRel) {
Sean Callanan61dfa772012-03-07 23:05:25 +0000230 Value -= FinalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000231 // ARM PCRel relocations have an effective-PC offset of two instructions
232 // (four bytes in Thumb mode, 8 bytes in ARM mode).
233 // FIXME: For now, assume ARM mode.
234 Value -= 8;
235 }
236
237 switch(Type) {
238 default:
239 llvm_unreachable("Invalid relocation type!");
240 case macho::RIT_Vanilla: {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000241 // Mask in the target value a byte at a time (we don't have an alignment
242 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000243 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000244 for (unsigned i = 0; i < Size; ++i) {
245 *p++ = (uint8_t)Value;
246 Value >>= 8;
247 }
248 break;
249 }
250 case macho::RIT_ARM_Branch24Bit: {
251 // Mask the value into the target address. We know instructions are
252 // 32-bit aligned, so we can do it all at once.
Sean Callanan61dfa772012-03-07 23:05:25 +0000253 uint32_t *p = (uint32_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000254 // The low two bits of the value are not encoded.
255 Value >>= 2;
256 // Mask the value to 24 bits.
257 Value &= 0xffffff;
258 // FIXME: If the destination is a Thumb function (and the instruction
259 // is a non-predicated BL instruction), we need to change it to a BLX
260 // instruction instead.
261
262 // Insert the value into the instruction.
263 *p = (*p & ~0xffffff) | Value;
264 break;
265 }
266 case macho::RIT_ARM_ThumbBranch22Bit:
267 case macho::RIT_ARM_ThumbBranch32Bit:
268 case macho::RIT_ARM_Half:
269 case macho::RIT_ARM_HalfDifference:
270 case macho::RIT_Pair:
271 case macho::RIT_Difference:
272 case macho::RIT_ARM_LocalDifference:
273 case macho::RIT_ARM_PreboundLazyPointer:
274 return Error("Relocation type not implemented yet!");
275 }
276 return false;
277}
278
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000279void RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000280 RelocationRef RelI,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000281 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000282 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000283 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000284 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000285 const ObjectFile *OF = Obj.getObjectFile();
286 const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000287 macho::RelocationEntry RE = MachO->getRelocation(RelI.getRawDataRefImpl());
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000288
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000289 uint32_t RelType = MachO->getAnyRelocationType(RE);
Lang Hames623f2022013-08-09 00:57:01 +0000290
291 // FIXME: Properly handle scattered relocations.
292 // For now, optimistically skip these: they can often be ignored, as
293 // the static linker will already have applied the relocation, and it
294 // only needs to be reapplied if symbols move relative to one another.
295 // Note: This will fail horribly where the relocations *do* need to be
296 // applied, but that was already the case.
297 if (MachO->isRelocationScattered(RE))
298 return;
299
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000300 RelocationValueRef Value;
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000301 SectionEntry &Section = Sections[SectionID];
Jim Grosbach61425c02012-01-16 22:26:39 +0000302
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000303 bool isExtern = MachO->getPlainRelocationExternal(RE);
Rafael Espindola87b50172013-04-29 17:24:34 +0000304 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
305 unsigned Size = MachO->getAnyRelocationLength(RE);
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000306 uint64_t Offset;
307 RelI.getOffset(Offset);
Rafael Espindolae87dadc2013-04-30 15:40:54 +0000308 uint8_t *LocalAddress = Section.Address + Offset;
309 unsigned NumBytes = 1 << Size;
310 uint64_t Addend = 0;
311 memcpy(&Addend, LocalAddress, NumBytes);
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000312
Rafael Espindolae87dadc2013-04-30 15:40:54 +0000313 if (isExtern) {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000314 // Obtain the symbol name which is referenced in the relocation
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000315 symbol_iterator Symbol = RelI.getSymbol();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000316 StringRef TargetName;
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000317 Symbol->getName(TargetName);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000318 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000319 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000320 if (lsi != Symbols.end()) {
321 Value.SectionID = lsi->second.first;
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000322 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000323 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000324 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000325 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
326 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000327 Value.SectionID = gsi->second.first;
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000328 Value.Addend = gsi->second.second + Addend;
329 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000330 Value.SymbolName = TargetName.data();
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000331 Value.Addend = Addend;
332 }
Danil Malyshev4b0b8ef2012-03-29 21:46:18 +0000333 }
Bill Wendling288967d2012-03-29 23:23:59 +0000334 } else {
Rafael Espindolae87dadc2013-04-30 15:40:54 +0000335 SectionRef Sec = MachO->getRelocationSection(RE);
336 Value.SectionID = findOrEmitSection(Obj, Sec, true, ObjSectionToID);
337 uint64_t Addr;
338 Sec.getAddress(Addr);
339 Value.Addend = Addend - Addr;
Bill Wendling288967d2012-03-29 23:23:59 +0000340 }
341
Lang Hamesae7ac392013-08-15 22:31:40 +0000342 if (Arch == Triple::x86_64 && (RelType == macho::RIT_X86_64_GOT ||
343 RelType == macho::RIT_X86_64_GOTLoad)) {
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000344 assert(IsPCRel);
345 assert(Size == 2);
346 StubMap::const_iterator i = Stubs.find(Value);
347 uint8_t *Addr;
348 if (i != Stubs.end()) {
349 Addr = Section.Address + i->second;
350 } else {
351 Stubs[Value] = Section.StubOffset;
352 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
353 RelocationEntry RE(SectionID, Section.StubOffset,
Lang Hamesae7ac392013-08-15 22:31:40 +0000354 macho::RIT_X86_64_Unsigned, 0, false, 3);
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000355 if (Value.SymbolName)
356 addRelocationForSymbol(RE, Value.SymbolName);
357 else
358 addRelocationForSection(RE, Value.SectionID);
359 Section.StubOffset += 8;
360 Addr = GOTEntry;
361 }
362 resolveRelocation(Section, Offset, (uint64_t)Addr,
Lang Hamesae7ac392013-08-15 22:31:40 +0000363 macho::RIT_X86_64_Unsigned, Value.Addend, true, 2);
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000364 } else if (Arch == Triple::arm &&
365 (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000366 // This is an ARM branch relocation, need to use a stub function.
Bill Wendling288967d2012-03-29 23:23:59 +0000367
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000368 // Look up for existing stub.
369 StubMap::const_iterator i = Stubs.find(Value);
370 if (i != Stubs.end())
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000371 resolveRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000372 (uint64_t)Section.Address + i->second,
Rafael Espindola87b50172013-04-29 17:24:34 +0000373 RelType, 0, IsPCRel, Size);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000374 else {
375 // Create a new stub function.
376 Stubs[Value] = Section.StubOffset;
377 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
378 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000379 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000380 macho::RIT_Vanilla, Value.Addend);
381 if (Value.SymbolName)
382 addRelocationForSymbol(RE, Value.SymbolName);
383 else
384 addRelocationForSection(RE, Value.SectionID);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000385 resolveRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000386 (uint64_t)Section.Address + Section.StubOffset,
Rafael Espindola87b50172013-04-29 17:24:34 +0000387 RelType, 0, IsPCRel, Size);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000388 Section.StubOffset += getMaxStubSize();
389 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000390 } else {
Rafael Espindola87b50172013-04-29 17:24:34 +0000391 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend,
392 IsPCRel, Size);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000393 if (Value.SymbolName)
394 addRelocationForSymbol(RE, Value.SymbolName);
395 else
396 addRelocationForSection(RE, Value.SectionID);
397 }
Bill Wendling288967d2012-03-29 23:23:59 +0000398}
399
Bill Wendling288967d2012-03-29 23:23:59 +0000400
Eli Bendersky6d15e872012-04-30 10:06:27 +0000401bool RuntimeDyldMachO::isCompatibleFormat(
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000402 const ObjectBuffer *InputBuffer) const {
403 if (InputBuffer->getBufferSize() < 4)
404 return false;
405 StringRef Magic(InputBuffer->getBufferStart(), 4);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000406 if (Magic == "\xFE\xED\xFA\xCE") return true;
407 if (Magic == "\xCE\xFA\xED\xFE") return true;
408 if (Magic == "\xFE\xED\xFA\xCF") return true;
409 if (Magic == "\xCF\xFA\xED\xFE") return true;
410 return false;
411}
412
413} // end namespace llvm