blob: 5b92867b4778b3dd76f7889b0d5e68c99ae241f3 [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
Andrew Kaylor528f6d72013-10-11 21:25:48 +000058void RuntimeDyldMachO::registerEHFrames() {
59
60 if (!MemMgr)
61 return;
62 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
63 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
64 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
65 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
66 continue;
67 SectionEntry *Text = &Sections[SectionInfo.TextSID];
68 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
69 SectionEntry *ExceptTab = NULL;
70 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
71 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
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 MemMgr->registerEHFrames(EHFrame->Address,
85 EHFrame->LoadAddress,
86 EHFrame->Size);
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +000087 }
Andrew Kaylor528f6d72013-10-11 21:25:48 +000088 UnregisteredEHFrameSections.clear();
89}
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +000090
Andrew Kaylor528f6d72013-10-11 21:25:48 +000091void RuntimeDyldMachO::finalizeLoad(ObjSectionToIDMap &SectionMap) {
92 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
93 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
94 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
95 ObjSectionToIDMap::iterator i, e;
96 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
97 const SectionRef &Section = i->first;
98 StringRef Name;
99 Section.getName(Name);
100 if (Name == "__eh_frame")
101 EHFrameSID = i->second;
102 else if (Name == "__text")
103 TextSID = i->second;
104 else if (Name == "__gcc_except_tab")
105 ExceptTabSID = i->second;
106 }
107 UnregisteredEHFrameSections.push_back(EHFrameRelatedSections(EHFrameSID,
108 TextSID,
109 ExceptTabSID));
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000110}
111
Andrew Kaylor32bd10b2013-08-19 19:38:06 +0000112// The target location for the relocation is described by RE.SectionID and
113// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
114// SectionEntry has three members describing its location.
115// SectionEntry::Address is the address at which the section has been loaded
116// into memory in the current (host) process. SectionEntry::LoadAddress is the
117// address that the section will have in the target process.
118// SectionEntry::ObjAddress is the address of the bits for this section in the
119// original emitted object image (also in the current address space).
120//
121// Relocations will be applied as if the section were loaded at
122// SectionEntry::LoadAddress, but they will be applied at an address based
123// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
124// Target memory contents if they are required for value calculations.
125//
126// The Value parameter here is the load address of the symbol for the
127// relocation to be applied. For relocations which refer to symbols in the
128// current object Value will be the LoadAddress of the section in which
129// the symbol resides (RE.Addend provides additional information about the
130// symbol location). For external symbols, Value will be the address of the
131// symbol in the target address space.
Rafael Espindola87b50172013-04-29 17:24:34 +0000132void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
133 uint64_t Value) {
134 const SectionEntry &Section = Sections[RE.SectionID];
135 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
136 RE.IsPCRel, RE.Size);
137}
138
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000139void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
140 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000141 uint64_t Value,
142 uint32_t Type,
Rafael Espindola87b50172013-04-29 17:24:34 +0000143 int64_t Addend,
144 bool isPCRel,
145 unsigned LogSize) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000146 uint8_t *LocalAddress = Section.Address + Offset;
147 uint64_t FinalAddress = Section.LoadAddress + Offset;
Rafael Espindola87b50172013-04-29 17:24:34 +0000148 unsigned MachoType = Type;
149 unsigned Size = 1 << LogSize;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000150
Eli Bendersky6d15e872012-04-30 10:06:27 +0000151 DEBUG(dbgs() << "resolveRelocation LocalAddress: "
152 << format("%p", LocalAddress)
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000153 << " FinalAddress: " << format("%p", FinalAddress)
154 << " Value: " << format("%p", Value)
155 << " Addend: " << Addend
156 << " isPCRel: " << isPCRel
157 << " MachoType: " << MachoType
158 << " Size: " << Size
159 << "\n");
160
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000161 // This just dispatches to the proper target specific routine.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000162 switch (Arch) {
Craig Topper85814382012-02-07 05:05:23 +0000163 default: llvm_unreachable("Unsupported CPU type!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000164 case Triple::x86_64:
165 resolveX86_64Relocation(LocalAddress,
166 FinalAddress,
167 (uintptr_t)Value,
168 isPCRel,
169 MachoType,
170 Size,
171 Addend);
172 break;
173 case Triple::x86:
174 resolveI386Relocation(LocalAddress,
Eli Bendersky6d15e872012-04-30 10:06:27 +0000175 FinalAddress,
176 (uintptr_t)Value,
177 isPCRel,
Jim Grosbachba9ba9f2012-09-13 01:24:32 +0000178 MachoType,
Eli Bendersky6d15e872012-04-30 10:06:27 +0000179 Size,
180 Addend);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000181 break;
182 case Triple::arm: // Fall through.
183 case Triple::thumb:
184 resolveARMRelocation(LocalAddress,
185 FinalAddress,
186 (uintptr_t)Value,
187 isPCRel,
188 MachoType,
189 Size,
190 Addend);
191 break;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000192 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000193}
194
Eli Bendersky6d15e872012-04-30 10:06:27 +0000195bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
196 uint64_t FinalAddress,
197 uint64_t Value,
198 bool isPCRel,
199 unsigned Type,
200 unsigned Size,
201 int64_t Addend) {
Sean Callananb38aae42012-03-26 20:45:52 +0000202 if (isPCRel)
203 Value -= FinalAddress + 4; // see resolveX86_64Relocation
204
205 switch (Type) {
206 default:
207 llvm_unreachable("Invalid relocation type!");
Charles Davis55107282013-09-01 04:28:48 +0000208 case MachO::GENERIC_RELOC_VANILLA: {
Sean Callananb38aae42012-03-26 20:45:52 +0000209 uint8_t *p = LocalAddress;
210 uint64_t ValueToWrite = Value + Addend;
211 for (unsigned i = 0; i < Size; ++i) {
212 *p++ = (uint8_t)(ValueToWrite & 0xff);
213 ValueToWrite >>= 8;
214 }
Jim Grosbach6f6f1712013-01-31 19:46:28 +0000215 return false;
Sean Callananb38aae42012-03-26 20:45:52 +0000216 }
Charles Davis55107282013-09-01 04:28:48 +0000217 case MachO::GENERIC_RELOC_SECTDIFF:
218 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF:
219 case MachO::GENERIC_RELOC_PB_LA_PTR:
Sean Callananb38aae42012-03-26 20:45:52 +0000220 return Error("Relocation type not implemented yet!");
221 }
222}
223
Eli Bendersky6d15e872012-04-30 10:06:27 +0000224bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
225 uint64_t FinalAddress,
226 uint64_t Value,
227 bool isPCRel,
228 unsigned Type,
229 unsigned Size,
230 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000231 // If the relocation is PC-relative, the value to be encoded is the
232 // pointer difference.
233 if (isPCRel)
234 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
235 // address. Is that expected? Only for branches, perhaps?
Sean Callanan61dfa772012-03-07 23:05:25 +0000236 Value -= FinalAddress + 4;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000237
238 switch(Type) {
239 default:
240 llvm_unreachable("Invalid relocation type!");
Charles Davis55107282013-09-01 04:28:48 +0000241 case MachO::X86_64_RELOC_SIGNED_1:
242 case MachO::X86_64_RELOC_SIGNED_2:
243 case MachO::X86_64_RELOC_SIGNED_4:
244 case MachO::X86_64_RELOC_SIGNED:
245 case MachO::X86_64_RELOC_UNSIGNED:
246 case MachO::X86_64_RELOC_BRANCH: {
Jim Grosbach652ca2f2012-01-16 23:50:49 +0000247 Value += Addend;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000248 // Mask in the target value a byte at a time (we don't have an alignment
249 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000250 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000251 for (unsigned i = 0; i < Size; ++i) {
252 *p++ = (uint8_t)Value;
253 Value >>= 8;
254 }
255 return false;
256 }
Charles Davis55107282013-09-01 04:28:48 +0000257 case MachO::X86_64_RELOC_GOT_LOAD:
258 case MachO::X86_64_RELOC_GOT:
259 case MachO::X86_64_RELOC_SUBTRACTOR:
260 case MachO::X86_64_RELOC_TLV:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000261 return Error("Relocation type not implemented yet!");
262 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000263}
264
Eli Bendersky6d15e872012-04-30 10:06:27 +0000265bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
266 uint64_t FinalAddress,
267 uint64_t Value,
268 bool isPCRel,
269 unsigned Type,
270 unsigned Size,
271 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000272 // If the relocation is PC-relative, the value to be encoded is the
273 // pointer difference.
274 if (isPCRel) {
Sean Callanan61dfa772012-03-07 23:05:25 +0000275 Value -= FinalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000276 // ARM PCRel relocations have an effective-PC offset of two instructions
277 // (four bytes in Thumb mode, 8 bytes in ARM mode).
278 // FIXME: For now, assume ARM mode.
279 Value -= 8;
280 }
281
282 switch(Type) {
283 default:
284 llvm_unreachable("Invalid relocation type!");
Charles Davis55107282013-09-01 04:28:48 +0000285 case MachO::ARM_RELOC_VANILLA: {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000286 // Mask in the target value a byte at a time (we don't have an alignment
287 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000288 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000289 for (unsigned i = 0; i < Size; ++i) {
Charles Davisf69a29b2013-08-27 05:38:30 +0000290 *p++ = (uint8_t)Value;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000291 Value >>= 8;
292 }
293 break;
294 }
Charles Davis55107282013-09-01 04:28:48 +0000295 case MachO::ARM_RELOC_BR24: {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000296 // Mask the value into the target address. We know instructions are
297 // 32-bit aligned, so we can do it all at once.
Sean Callanan61dfa772012-03-07 23:05:25 +0000298 uint32_t *p = (uint32_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000299 // The low two bits of the value are not encoded.
300 Value >>= 2;
301 // Mask the value to 24 bits.
302 Value &= 0xffffff;
303 // FIXME: If the destination is a Thumb function (and the instruction
304 // is a non-predicated BL instruction), we need to change it to a BLX
305 // instruction instead.
306
307 // Insert the value into the instruction.
308 *p = (*p & ~0xffffff) | Value;
309 break;
310 }
Charles Davis55107282013-09-01 04:28:48 +0000311 case MachO::ARM_THUMB_RELOC_BR22:
312 case MachO::ARM_THUMB_32BIT_BRANCH:
313 case MachO::ARM_RELOC_HALF:
314 case MachO::ARM_RELOC_HALF_SECTDIFF:
315 case MachO::ARM_RELOC_PAIR:
316 case MachO::ARM_RELOC_SECTDIFF:
317 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
318 case MachO::ARM_RELOC_PB_LA_PTR:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000319 return Error("Relocation type not implemented yet!");
320 }
321 return false;
322}
323
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000324void RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000325 RelocationRef RelI,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000326 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000327 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000328 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000329 StubMap &Stubs) {
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000330 const ObjectFile *OF = Obj.getObjectFile();
331 const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
Charles Davis55107282013-09-01 04:28:48 +0000332 MachO::any_relocation_info RE= MachO->getRelocation(RelI.getRawDataRefImpl());
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000333
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000334 uint32_t RelType = MachO->getAnyRelocationType(RE);
Lang Hames623f2022013-08-09 00:57:01 +0000335
336 // FIXME: Properly handle scattered relocations.
337 // For now, optimistically skip these: they can often be ignored, as
338 // the static linker will already have applied the relocation, and it
339 // only needs to be reapplied if symbols move relative to one another.
340 // Note: This will fail horribly where the relocations *do* need to be
341 // applied, but that was already the case.
342 if (MachO->isRelocationScattered(RE))
343 return;
344
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000345 RelocationValueRef Value;
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000346 SectionEntry &Section = Sections[SectionID];
Jim Grosbach61425c02012-01-16 22:26:39 +0000347
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000348 bool isExtern = MachO->getPlainRelocationExternal(RE);
Rafael Espindola87b50172013-04-29 17:24:34 +0000349 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
350 unsigned Size = MachO->getAnyRelocationLength(RE);
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000351 uint64_t Offset;
352 RelI.getOffset(Offset);
Rafael Espindolae87dadc2013-04-30 15:40:54 +0000353 uint8_t *LocalAddress = Section.Address + Offset;
354 unsigned NumBytes = 1 << Size;
355 uint64_t Addend = 0;
356 memcpy(&Addend, LocalAddress, NumBytes);
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000357
Rafael Espindolae87dadc2013-04-30 15:40:54 +0000358 if (isExtern) {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000359 // Obtain the symbol name which is referenced in the relocation
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000360 symbol_iterator Symbol = RelI.getSymbol();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000361 StringRef TargetName;
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000362 Symbol->getName(TargetName);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000363 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000364 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000365 if (lsi != Symbols.end()) {
366 Value.SectionID = lsi->second.first;
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000367 Value.Addend = lsi->second.second + Addend;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000368 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000369 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000370 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
371 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000372 Value.SectionID = gsi->second.first;
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000373 Value.Addend = gsi->second.second + Addend;
374 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000375 Value.SymbolName = TargetName.data();
Rafael Espindola8e6e02a2013-04-30 01:29:57 +0000376 Value.Addend = Addend;
377 }
Danil Malyshev4b0b8ef2012-03-29 21:46:18 +0000378 }
Bill Wendling288967d2012-03-29 23:23:59 +0000379 } else {
Rafael Espindolae87dadc2013-04-30 15:40:54 +0000380 SectionRef Sec = MachO->getRelocationSection(RE);
381 Value.SectionID = findOrEmitSection(Obj, Sec, true, ObjSectionToID);
382 uint64_t Addr;
383 Sec.getAddress(Addr);
384 Value.Addend = Addend - Addr;
Bill Wendling288967d2012-03-29 23:23:59 +0000385 }
386
Charles Davis55107282013-09-01 04:28:48 +0000387 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
388 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000389 assert(IsPCRel);
390 assert(Size == 2);
391 StubMap::const_iterator i = Stubs.find(Value);
392 uint8_t *Addr;
393 if (i != Stubs.end()) {
394 Addr = Section.Address + i->second;
395 } else {
396 Stubs[Value] = Section.StubOffset;
397 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
398 RelocationEntry RE(SectionID, Section.StubOffset,
Charles Davis55107282013-09-01 04:28:48 +0000399 MachO::X86_64_RELOC_UNSIGNED, 0, false, 3);
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000400 if (Value.SymbolName)
401 addRelocationForSymbol(RE, Value.SymbolName);
402 else
403 addRelocationForSection(RE, Value.SectionID);
404 Section.StubOffset += 8;
405 Addr = GOTEntry;
406 }
407 resolveRelocation(Section, Offset, (uint64_t)Addr,
Charles Davis55107282013-09-01 04:28:48 +0000408 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2);
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000409 } else if (Arch == Triple::arm &&
Charles Davis55107282013-09-01 04:28:48 +0000410 (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000411 // This is an ARM branch relocation, need to use a stub function.
Bill Wendling288967d2012-03-29 23:23:59 +0000412
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000413 // Look up for existing stub.
414 StubMap::const_iterator i = Stubs.find(Value);
415 if (i != Stubs.end())
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000416 resolveRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000417 (uint64_t)Section.Address + i->second,
Rafael Espindola87b50172013-04-29 17:24:34 +0000418 RelType, 0, IsPCRel, Size);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000419 else {
420 // Create a new stub function.
421 Stubs[Value] = Section.StubOffset;
422 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
423 Section.StubOffset);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000424 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
Charles Davis55107282013-09-01 04:28:48 +0000425 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000426 if (Value.SymbolName)
427 addRelocationForSymbol(RE, Value.SymbolName);
428 else
429 addRelocationForSection(RE, Value.SectionID);
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000430 resolveRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000431 (uint64_t)Section.Address + Section.StubOffset,
Rafael Espindola87b50172013-04-29 17:24:34 +0000432 RelType, 0, IsPCRel, Size);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000433 Section.StubOffset += getMaxStubSize();
434 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000435 } else {
Rafael Espindola87b50172013-04-29 17:24:34 +0000436 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend,
437 IsPCRel, Size);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000438 if (Value.SymbolName)
439 addRelocationForSymbol(RE, Value.SymbolName);
440 else
441 addRelocationForSection(RE, Value.SectionID);
442 }
Bill Wendling288967d2012-03-29 23:23:59 +0000443}
444
Bill Wendling288967d2012-03-29 23:23:59 +0000445
Eli Bendersky6d15e872012-04-30 10:06:27 +0000446bool RuntimeDyldMachO::isCompatibleFormat(
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000447 const ObjectBuffer *InputBuffer) const {
448 if (InputBuffer->getBufferSize() < 4)
449 return false;
450 StringRef Magic(InputBuffer->getBufferStart(), 4);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000451 if (Magic == "\xFE\xED\xFA\xCE") return true;
452 if (Magic == "\xCE\xFA\xED\xFE") return true;
453 if (Magic == "\xFE\xED\xFA\xCF") return true;
454 if (Magic == "\xCF\xFA\xED\xFE") return true;
455 return false;
456}
457
458} // end namespace llvm