blob: 352d83d0e947e20ca4ef2bcd2f0df086a9a3aa71 [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"
Danil Malyshev72510f22011-07-13 07:57:58 +000017using namespace llvm;
18using namespace llvm::object;
19
Chandler Carruthf58e3762014-04-22 03:04:17 +000020#define DEBUG_TYPE "dyld"
21
Danil Malyshev72510f22011-07-13 07:57:58 +000022namespace llvm {
23
Juergen Ributzka7608dc02014-03-21 20:28:42 +000024static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
25 intptr_t DeltaForEH) {
26 uint32_t Length = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +000027 P += 4;
28 unsigned char *Ret = P + Length;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000029 uint32_t Offset = *((uint32_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +000030 if (Offset == 0) // is a CIE
31 return Ret;
32
33 P += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000034 intptr_t FDELocation = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +000035 intptr_t NewLocation = FDELocation - DeltaForText;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000036 *((intptr_t *)P) = NewLocation;
Rafael Espindolafa5942b2013-05-05 20:43:10 +000037 P += sizeof(intptr_t);
38
39 // Skip the FDE address range
40 P += sizeof(intptr_t);
41
42 uint8_t Augmentationsize = *P;
43 P += 1;
44 if (Augmentationsize != 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000045 intptr_t LSDA = *((intptr_t *)P);
Rafael Espindolafa5942b2013-05-05 20:43:10 +000046 intptr_t NewLSDA = LSDA - DeltaForEH;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000047 *((intptr_t *)P) = NewLSDA;
Rafael Espindolafa5942b2013-05-05 20:43:10 +000048 }
49
50 return Ret;
51}
52
53static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000054 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
Rafael Espindolafa5942b2013-05-05 20:43:10 +000055 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
56 return ObjDistance - MemDistance;
57}
58
Andrew Kaylor7bb13442013-10-11 21:25:48 +000059void RuntimeDyldMachO::registerEHFrames() {
60
61 if (!MemMgr)
62 return;
63 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
64 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
65 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
66 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
67 continue;
68 SectionEntry *Text = &Sections[SectionInfo.TextSID];
69 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
Craig Topper353eda42014-04-24 06:44:33 +000070 SectionEntry *ExceptTab = nullptr;
Andrew Kaylor7bb13442013-10-11 21:25:48 +000071 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
72 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
73
74 intptr_t DeltaForText = computeDelta(Text, EHFrame);
75 intptr_t DeltaForEH = 0;
76 if (ExceptTab)
77 DeltaForEH = computeDelta(ExceptTab, EHFrame);
78
79 unsigned char *P = EHFrame->Address;
80 unsigned char *End = P + EHFrame->Size;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000081 do {
Andrew Kaylor7bb13442013-10-11 21:25:48 +000082 P = processFDE(P, DeltaForText, DeltaForEH);
Juergen Ributzka7608dc02014-03-21 20:28:42 +000083 } while (P != End);
Andrew Kaylor7bb13442013-10-11 21:25:48 +000084
Juergen Ributzka7608dc02014-03-21 20:28:42 +000085 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
Andrew Kaylor7bb13442013-10-11 21:25:48 +000086 EHFrame->Size);
Rafael Espindolafa5942b2013-05-05 20:43:10 +000087 }
Andrew Kaylor7bb13442013-10-11 21:25:48 +000088 UnregisteredEHFrameSections.clear();
89}
Rafael Espindolafa5942b2013-05-05 20:43:10 +000090
Andrew Kaylor7bb13442013-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 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000107 UnregisteredEHFrameSections.push_back(
108 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000109}
110
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000111// The target location for the relocation is described by RE.SectionID and
112// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
113// SectionEntry has three members describing its location.
114// SectionEntry::Address is the address at which the section has been loaded
115// into memory in the current (host) process. SectionEntry::LoadAddress is the
116// address that the section will have in the target process.
117// SectionEntry::ObjAddress is the address of the bits for this section in the
118// original emitted object image (also in the current address space).
119//
120// Relocations will be applied as if the section were loaded at
121// SectionEntry::LoadAddress, but they will be applied at an address based
122// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
123// Target memory contents if they are required for value calculations.
124//
125// The Value parameter here is the load address of the symbol for the
126// relocation to be applied. For relocations which refer to symbols in the
127// current object Value will be the LoadAddress of the section in which
128// the symbol resides (RE.Addend provides additional information about the
129// symbol location). For external symbols, Value will be the address of the
130// symbol in the target address space.
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000131void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
132 uint64_t Value) {
Lang Hamesf35553e2014-05-09 00:11:18 +0000133 DEBUG (
134 const SectionEntry &Section = Sections[RE.SectionID];
135 uint8_t* LocalAddress = Section.Address + RE.Offset;
136 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000137
Lang Hamesf35553e2014-05-09 00:11:18 +0000138 dbgs() << "resolveRelocation Section: " << RE.SectionID
139 << " LocalAddress: " << format("%p", LocalAddress)
140 << " FinalAddress: " << format("%p", FinalAddress)
141 << " Value: " << format("%p", Value)
142 << " Addend: " << RE.Addend
143 << " isPCRel: " << RE.IsPCRel
144 << " MachoType: " << RE.RelType
145 << " Size: " << (1 << RE.Size) << "\n";
146 );
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000147
Danil Malyshev72510f22011-07-13 07:57:58 +0000148 // This just dispatches to the proper target specific routine.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000149 switch (Arch) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000150 default:
151 llvm_unreachable("Unsupported CPU type!");
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000152 case Triple::x86_64:
Lang Hamesf35553e2014-05-09 00:11:18 +0000153 resolveX86_64Relocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000154 break;
155 case Triple::x86:
Lang Hamesf35553e2014-05-09 00:11:18 +0000156 resolveI386Relocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000157 break;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000158 case Triple::arm: // Fall through.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000159 case Triple::thumb:
Lang Hamesf35553e2014-05-09 00:11:18 +0000160 resolveARMRelocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000161 break;
Tim Northover00ed9962014-03-29 10:18:08 +0000162 case Triple::arm64:
Lang Hamesf35553e2014-05-09 00:11:18 +0000163 resolveARM64Relocation(RE, Value);
Tim Northover00ed9962014-03-29 10:18:08 +0000164 break;
Danil Malyshev72510f22011-07-13 07:57:58 +0000165 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000166}
167
Lang Hamesf35553e2014-05-09 00:11:18 +0000168bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE,
169 uint64_t Value) {
170 const SectionEntry &Section = Sections[RE.SectionID];
171 uint8_t* LocalAddress = Section.Address + RE.Offset;
Sean Callanana3759432012-03-26 20:45:52 +0000172
Lang Hamesf35553e2014-05-09 00:11:18 +0000173 if (RE.IsPCRel) {
174 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
175 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
Sean Callanana3759432012-03-26 20:45:52 +0000176 }
Lang Hamesf35553e2014-05-09 00:11:18 +0000177
178 switch (RE.RelType) {
179 default:
180 llvm_unreachable("Invalid relocation type!");
181 case MachO::GENERIC_RELOC_VANILLA:
182 return applyRelocationValue(LocalAddress, Value + RE.Addend,
183 1 << RE.Size);
184 case MachO::GENERIC_RELOC_SECTDIFF:
185 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF:
186 case MachO::GENERIC_RELOC_PB_LA_PTR:
187 return Error("Relocation type not implemented yet!");
Sean Callanana3759432012-03-26 20:45:52 +0000188 }
189}
190
Lang Hamesf35553e2014-05-09 00:11:18 +0000191bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE,
192 uint64_t Value) {
193 const SectionEntry &Section = Sections[RE.SectionID];
194 uint8_t* LocalAddress = Section.Address + RE.Offset;
195
Danil Malyshev72510f22011-07-13 07:57:58 +0000196 // If the relocation is PC-relative, the value to be encoded is the
197 // pointer difference.
Lang Hamesf35553e2014-05-09 00:11:18 +0000198 if (RE.IsPCRel) {
Danil Malyshev72510f22011-07-13 07:57:58 +0000199 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
200 // address. Is that expected? Only for branches, perhaps?
Lang Hamesf35553e2014-05-09 00:11:18 +0000201 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
202 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
203 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000204
Lang Hamesf35553e2014-05-09 00:11:18 +0000205 switch (RE.RelType) {
Danil Malyshev72510f22011-07-13 07:57:58 +0000206 default:
207 llvm_unreachable("Invalid relocation type!");
Charles Davis8bdfafd2013-09-01 04:28:48 +0000208 case MachO::X86_64_RELOC_SIGNED_1:
209 case MachO::X86_64_RELOC_SIGNED_2:
210 case MachO::X86_64_RELOC_SIGNED_4:
211 case MachO::X86_64_RELOC_SIGNED:
212 case MachO::X86_64_RELOC_UNSIGNED:
Lang Hamesf35553e2014-05-09 00:11:18 +0000213 case MachO::X86_64_RELOC_BRANCH:
214 return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000215 case MachO::X86_64_RELOC_GOT_LOAD:
216 case MachO::X86_64_RELOC_GOT:
217 case MachO::X86_64_RELOC_SUBTRACTOR:
218 case MachO::X86_64_RELOC_TLV:
Danil Malyshev72510f22011-07-13 07:57:58 +0000219 return Error("Relocation type not implemented yet!");
220 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000221}
222
Lang Hamesf35553e2014-05-09 00:11:18 +0000223bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE,
224 uint64_t Value) {
225 const SectionEntry &Section = Sections[RE.SectionID];
226 uint8_t* LocalAddress = Section.Address + RE.Offset;
227
Danil Malyshev72510f22011-07-13 07:57:58 +0000228 // If the relocation is PC-relative, the value to be encoded is the
229 // pointer difference.
Lang Hamesf35553e2014-05-09 00:11:18 +0000230 if (RE.IsPCRel) {
231 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
Sean Callananca92a3d2012-03-07 23:05:25 +0000232 Value -= FinalAddress;
Danil Malyshev72510f22011-07-13 07:57:58 +0000233 // ARM PCRel relocations have an effective-PC offset of two instructions
234 // (four bytes in Thumb mode, 8 bytes in ARM mode).
235 // FIXME: For now, assume ARM mode.
236 Value -= 8;
237 }
238
Lang Hamesf35553e2014-05-09 00:11:18 +0000239 switch (RE.RelType) {
Danil Malyshev72510f22011-07-13 07:57:58 +0000240 default:
241 llvm_unreachable("Invalid relocation type!");
Lang Hamesf35553e2014-05-09 00:11:18 +0000242 case MachO::ARM_RELOC_VANILLA:
243 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000244 case MachO::ARM_RELOC_BR24: {
Danil Malyshev72510f22011-07-13 07:57:58 +0000245 // Mask the value into the target address. We know instructions are
246 // 32-bit aligned, so we can do it all at once.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000247 uint32_t *p = (uint32_t *)LocalAddress;
Danil Malyshev72510f22011-07-13 07:57:58 +0000248 // The low two bits of the value are not encoded.
249 Value >>= 2;
250 // Mask the value to 24 bits.
Lang Hamesf35553e2014-05-09 00:11:18 +0000251 uint64_t FinalValue = Value & 0xffffff;
252 // Check for overflow.
253 if (Value != FinalValue)
254 return Error("ARM BR24 relocation out of range.");
Danil Malyshev72510f22011-07-13 07:57:58 +0000255 // FIXME: If the destination is a Thumb function (and the instruction
256 // is a non-predicated BL instruction), we need to change it to a BLX
257 // instruction instead.
258
259 // Insert the value into the instruction.
Lang Hamesf35553e2014-05-09 00:11:18 +0000260 *p = (*p & ~0xffffff) | FinalValue;
Danil Malyshev72510f22011-07-13 07:57:58 +0000261 break;
262 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000263 case MachO::ARM_THUMB_RELOC_BR22:
264 case MachO::ARM_THUMB_32BIT_BRANCH:
265 case MachO::ARM_RELOC_HALF:
266 case MachO::ARM_RELOC_HALF_SECTDIFF:
267 case MachO::ARM_RELOC_PAIR:
268 case MachO::ARM_RELOC_SECTDIFF:
269 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
270 case MachO::ARM_RELOC_PB_LA_PTR:
Danil Malyshev72510f22011-07-13 07:57:58 +0000271 return Error("Relocation type not implemented yet!");
272 }
273 return false;
274}
275
Lang Hamesf35553e2014-05-09 00:11:18 +0000276bool RuntimeDyldMachO::resolveARM64Relocation(const RelocationEntry &RE,
277 uint64_t Value) {
278 const SectionEntry &Section = Sections[RE.SectionID];
279 uint8_t* LocalAddress = Section.Address + RE.Offset;
280
Tim Northover00ed9962014-03-29 10:18:08 +0000281 // If the relocation is PC-relative, the value to be encoded is the
282 // pointer difference.
Lang Hamesf35553e2014-05-09 00:11:18 +0000283 if (RE.IsPCRel) {
284 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
Tim Northover00ed9962014-03-29 10:18:08 +0000285 Value -= FinalAddress;
Lang Hamesf35553e2014-05-09 00:11:18 +0000286 }
Tim Northover00ed9962014-03-29 10:18:08 +0000287
Lang Hamesf35553e2014-05-09 00:11:18 +0000288 switch (RE.RelType) {
Tim Northover00ed9962014-03-29 10:18:08 +0000289 default:
290 llvm_unreachable("Invalid relocation type!");
Lang Hamesf35553e2014-05-09 00:11:18 +0000291 case MachO::ARM64_RELOC_UNSIGNED:
292 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
Tim Northover00ed9962014-03-29 10:18:08 +0000293 case MachO::ARM64_RELOC_BRANCH26: {
294 // Mask the value into the target address. We know instructions are
295 // 32-bit aligned, so we can do it all at once.
296 uint32_t *p = (uint32_t *)LocalAddress;
297 // The low two bits of the value are not encoded.
298 Value >>= 2;
299 // Mask the value to 26 bits.
Lang Hamesf35553e2014-05-09 00:11:18 +0000300 uint64_t FinalValue = Value & 0x3ffffff;
301 // Check for overflow.
302 if (FinalValue != Value)
303 return Error("ARM64 BRANCH26 relocation out of range.");
Tim Northover00ed9962014-03-29 10:18:08 +0000304 // Insert the value into the instruction.
Lang Hamesf35553e2014-05-09 00:11:18 +0000305 *p = (*p & ~0x3ffffff) | FinalValue;
Tim Northover00ed9962014-03-29 10:18:08 +0000306 break;
307 }
308 case MachO::ARM64_RELOC_SUBTRACTOR:
309 case MachO::ARM64_RELOC_PAGE21:
310 case MachO::ARM64_RELOC_PAGEOFF12:
311 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
312 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
313 case MachO::ARM64_RELOC_POINTER_TO_GOT:
314 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
315 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
316 case MachO::ARM64_RELOC_ADDEND:
317 return Error("Relocation type not implemented yet!");
318 }
319 return false;
320}
321
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000322relocation_iterator RuntimeDyldMachO::processRelocationRef(
323 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
324 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
325 StubMap &Stubs) {
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000326 const ObjectFile *OF = Obj.getObjectFile();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000327 const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF);
Juergen Ributzka046709f2014-03-21 07:26:41 +0000328 MachO::any_relocation_info RE =
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000329 MachO->getRelocation(RelI->getRawDataRefImpl());
Danil Malyshev72510f22011-07-13 07:57:58 +0000330
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000331 uint32_t RelType = MachO->getAnyRelocationType(RE);
Lang Hamesfe2833b2013-08-09 00:57:01 +0000332
333 // FIXME: Properly handle scattered relocations.
334 // For now, optimistically skip these: they can often be ignored, as
335 // the static linker will already have applied the relocation, and it
336 // only needs to be reapplied if symbols move relative to one another.
337 // Note: This will fail horribly where the relocations *do* need to be
338 // applied, but that was already the case.
339 if (MachO->isRelocationScattered(RE))
Juergen Ributzka046709f2014-03-21 07:26:41 +0000340 return ++RelI;
Lang Hamesfe2833b2013-08-09 00:57:01 +0000341
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000342 RelocationValueRef Value;
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000343 SectionEntry &Section = Sections[SectionID];
Jim Grosbacheff0a402012-01-16 22:26:39 +0000344
Lang Hamesf35553e2014-05-09 00:11:18 +0000345 bool IsExtern = MachO->getPlainRelocationExternal(RE);
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000346 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
347 unsigned Size = MachO->getAnyRelocationLength(RE);
Rafael Espindolad00c2762013-04-30 01:29:57 +0000348 uint64_t Offset;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000349 RelI->getOffset(Offset);
Rafael Espindola52501032013-04-30 15:40:54 +0000350 uint8_t *LocalAddress = Section.Address + Offset;
351 unsigned NumBytes = 1 << Size;
352 uint64_t Addend = 0;
353 memcpy(&Addend, LocalAddress, NumBytes);
Rafael Espindolad00c2762013-04-30 01:29:57 +0000354
Lang Hamesf35553e2014-05-09 00:11:18 +0000355 if (IsExtern) {
Eli Bendersky667b8792012-05-01 10:41:12 +0000356 // Obtain the symbol name which is referenced in the relocation
Juergen Ributzka046709f2014-03-21 07:26:41 +0000357 symbol_iterator Symbol = RelI->getSymbol();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000358 StringRef TargetName;
Rafael Espindola806f0062013-06-05 01:33:53 +0000359 Symbol->getName(TargetName);
Eli Bendersky667b8792012-05-01 10:41:12 +0000360 // First search for the symbol in the local symbol table
Eli Benderskyfc079082012-05-01 06:58:59 +0000361 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000362 if (lsi != Symbols.end()) {
363 Value.SectionID = lsi->second.first;
Rafael Espindolad00c2762013-04-30 01:29:57 +0000364 Value.Addend = lsi->second.second + Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000365 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000366 // Search for the symbol in the global symbol table
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000367 SymbolTableMap::const_iterator gsi =
368 GlobalSymbolTable.find(TargetName.data());
Eli Benderskyfc079082012-05-01 06:58:59 +0000369 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000370 Value.SectionID = gsi->second.first;
Rafael Espindolad00c2762013-04-30 01:29:57 +0000371 Value.Addend = gsi->second.second + Addend;
372 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000373 Value.SymbolName = TargetName.data();
Rafael Espindolad00c2762013-04-30 01:29:57 +0000374 Value.Addend = Addend;
375 }
Danil Malyshev3548eaf2012-03-29 21:46:18 +0000376 }
Bill Wendling76fdc4b2012-03-29 23:23:59 +0000377 } else {
Rafael Espindola52501032013-04-30 15:40:54 +0000378 SectionRef Sec = MachO->getRelocationSection(RE);
Lang Hames9b2dc932014-02-18 21:46:39 +0000379 bool IsCode = false;
380 Sec.isText(IsCode);
381 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
Rafael Espindola52501032013-04-30 15:40:54 +0000382 uint64_t Addr;
383 Sec.getAddress(Addr);
384 Value.Addend = Addend - Addr;
Lang Hames2cbbdf42014-01-29 18:31:35 +0000385 if (IsPCRel)
386 Value.Addend += Offset + NumBytes;
Bill Wendling76fdc4b2012-03-29 23:23:59 +0000387 }
388
Charles Davis8bdfafd2013-09-01 04:28:48 +0000389 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
390 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000391 assert(IsPCRel);
392 assert(Size == 2);
393 StubMap::const_iterator i = Stubs.find(Value);
394 uint8_t *Addr;
395 if (i != Stubs.end()) {
396 Addr = Section.Address + i->second;
397 } else {
398 Stubs[Value] = Section.StubOffset;
399 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
Lang Hamesf35553e2014-05-09 00:11:18 +0000400 RelocationEntry GOTRE(SectionID, Section.StubOffset,
401 MachO::X86_64_RELOC_UNSIGNED, 0, false, 3);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000402 if (Value.SymbolName)
Lang Hamesf35553e2014-05-09 00:11:18 +0000403 addRelocationForSymbol(GOTRE, Value.SymbolName);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000404 else
Lang Hamesf35553e2014-05-09 00:11:18 +0000405 addRelocationForSection(GOTRE, Value.SectionID);
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000406 Section.StubOffset += 8;
407 Addr = GOTEntry;
408 }
Lang Hamesf35553e2014-05-09 00:11:18 +0000409 RelocationEntry TargetRE(SectionID, Offset,
410 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true,
411 2);
412 resolveRelocation(TargetRE, (uint64_t)Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000413 } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000414 // This is an ARM branch relocation, need to use a stub function.
Bill Wendling76fdc4b2012-03-29 23:23:59 +0000415
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000416 // Look up for existing stub.
417 StubMap::const_iterator i = Stubs.find(Value);
Lang Hamesf35553e2014-05-09 00:11:18 +0000418 uint8_t *Addr;
419 if (i != Stubs.end()) {
420 Addr = Section.Address + i->second;
421 } else {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000422 // Create a new stub function.
423 Stubs[Value] = Section.StubOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000424 uint8_t *StubTargetAddr =
425 createStubFunction(Section.Address + Section.StubOffset);
Lang Hamesf35553e2014-05-09 00:11:18 +0000426 RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address,
427 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
Eli Bendersky667b8792012-05-01 10:41:12 +0000428 if (Value.SymbolName)
Lang Hamesf35553e2014-05-09 00:11:18 +0000429 addRelocationForSymbol(StubRE, Value.SymbolName);
Eli Bendersky667b8792012-05-01 10:41:12 +0000430 else
Lang Hamesf35553e2014-05-09 00:11:18 +0000431 addRelocationForSection(StubRE, Value.SectionID);
432 Addr = Section.Address + Section.StubOffset;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000433 Section.StubOffset += getMaxStubSize();
434 }
Lang Hamesf35553e2014-05-09 00:11:18 +0000435 RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel,
436 Size);
437 resolveRelocation(TargetRE, (uint64_t)Addr);
Eli Bendersky667b8792012-05-01 10:41:12 +0000438 } else {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000439 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size);
Eli Bendersky667b8792012-05-01 10:41:12 +0000440 if (Value.SymbolName)
441 addRelocationForSymbol(RE, Value.SymbolName);
442 else
443 addRelocationForSection(RE, Value.SectionID);
444 }
Juergen Ributzka046709f2014-03-21 07:26:41 +0000445 return ++RelI;
Bill Wendling76fdc4b2012-03-29 23:23:59 +0000446}
447
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000448bool
449RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
Andrew Kayloradc70562012-10-02 21:18:39 +0000450 if (InputBuffer->getBufferSize() < 4)
451 return false;
452 StringRef Magic(InputBuffer->getBufferStart(), 4);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000453 if (Magic == "\xFE\xED\xFA\xCE")
454 return true;
455 if (Magic == "\xCE\xFA\xED\xFE")
456 return true;
457 if (Magic == "\xFE\xED\xFA\xCF")
458 return true;
459 if (Magic == "\xCF\xFA\xED\xFE")
460 return true;
Danil Malyshev72510f22011-07-13 07:57:58 +0000461 return false;
462}
463
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000464bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
Lang Hames173c69f2014-01-08 04:09:09 +0000465 return Obj->isMachO();
466}
467
Danil Malyshev72510f22011-07-13 07:57:58 +0000468} // end namespace llvm