1 //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #include "RuntimeDyldMachO.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
18 using namespace llvm::object;
20 #define DEBUG_TYPE "dyld"
24 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
25 intptr_t DeltaForEH) {
26 uint32_t Length = *((uint32_t *)P);
28 unsigned char *Ret = P + Length;
29 uint32_t Offset = *((uint32_t *)P);
30 if (Offset == 0) // is a CIE
34 intptr_t FDELocation = *((intptr_t *)P);
35 intptr_t NewLocation = FDELocation - DeltaForText;
36 *((intptr_t *)P) = NewLocation;
37 P += sizeof(intptr_t);
39 // Skip the FDE address range
40 P += sizeof(intptr_t);
42 uint8_t Augmentationsize = *P;
44 if (Augmentationsize != 0) {
45 intptr_t LSDA = *((intptr_t *)P);
46 intptr_t NewLSDA = LSDA - DeltaForEH;
47 *((intptr_t *)P) = NewLSDA;
53 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
54 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
55 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
56 return ObjDistance - MemDistance;
59 void RuntimeDyldMachO::registerEHFrames() {
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)
68 SectionEntry *Text = &Sections[SectionInfo.TextSID];
69 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
70 SectionEntry *ExceptTab = nullptr;
71 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
72 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
74 intptr_t DeltaForText = computeDelta(Text, EHFrame);
75 intptr_t DeltaForEH = 0;
77 DeltaForEH = computeDelta(ExceptTab, EHFrame);
79 unsigned char *P = EHFrame->Address;
80 unsigned char *End = P + EHFrame->Size;
82 P = processFDE(P, DeltaForText, DeltaForEH);
85 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
88 UnregisteredEHFrameSections.clear();
91 void 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;
99 Section.getName(Name);
100 if (Name == "__eh_frame")
101 EHFrameSID = i->second;
102 else if (Name == "__text")
104 else if (Name == "__gcc_except_tab")
105 ExceptTabSID = i->second;
107 UnregisteredEHFrameSections.push_back(
108 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
111 // 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).
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.
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.
131 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
134 const SectionEntry &Section = Sections[RE.SectionID];
135 uint8_t* LocalAddress = Section.Address + RE.Offset;
136 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
138 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";
148 // This just dispatches to the proper target specific routine.
151 llvm_unreachable("Unsupported CPU type!");
153 resolveX86_64Relocation(RE, Value);
156 resolveI386Relocation(RE, Value);
158 case Triple::arm: // Fall through.
160 resolveARMRelocation(RE, Value);
163 resolveARM64Relocation(RE, Value);
168 bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE,
170 const SectionEntry &Section = Sections[RE.SectionID];
171 uint8_t* LocalAddress = Section.Address + RE.Offset;
174 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
175 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
178 switch (RE.RelType) {
180 llvm_unreachable("Invalid relocation type!");
181 case MachO::GENERIC_RELOC_VANILLA:
182 return applyRelocationValue(LocalAddress, Value + RE.Addend,
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!");
191 bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE,
193 const SectionEntry &Section = Sections[RE.SectionID];
194 uint8_t* LocalAddress = Section.Address + RE.Offset;
196 // If the relocation is PC-relative, the value to be encoded is the
197 // pointer difference.
199 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
200 // address. Is that expected? Only for branches, perhaps?
201 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
202 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
205 switch (RE.RelType) {
207 llvm_unreachable("Invalid relocation type!");
208 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:
213 case MachO::X86_64_RELOC_BRANCH:
214 return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
215 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:
219 return Error("Relocation type not implemented yet!");
223 bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE,
225 const SectionEntry &Section = Sections[RE.SectionID];
226 uint8_t* LocalAddress = Section.Address + RE.Offset;
228 // If the relocation is PC-relative, the value to be encoded is the
229 // pointer difference.
231 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
232 Value -= FinalAddress;
233 // 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.
239 switch (RE.RelType) {
241 llvm_unreachable("Invalid relocation type!");
242 case MachO::ARM_RELOC_VANILLA:
243 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
244 case MachO::ARM_RELOC_BR24: {
245 // Mask the value into the target address. We know instructions are
246 // 32-bit aligned, so we can do it all at once.
247 uint32_t *p = (uint32_t *)LocalAddress;
248 // The low two bits of the value are not encoded.
250 // Mask the value to 24 bits.
251 uint64_t FinalValue = Value & 0xffffff;
252 // Check for overflow.
253 if (Value != FinalValue)
254 return Error("ARM BR24 relocation out of range.");
255 // 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.
259 // Insert the value into the instruction.
260 *p = (*p & ~0xffffff) | FinalValue;
263 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:
271 return Error("Relocation type not implemented yet!");
276 bool RuntimeDyldMachO::resolveARM64Relocation(const RelocationEntry &RE,
278 const SectionEntry &Section = Sections[RE.SectionID];
279 uint8_t* LocalAddress = Section.Address + RE.Offset;
281 // If the relocation is PC-relative, the value to be encoded is the
282 // pointer difference.
284 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
285 Value -= FinalAddress;
288 switch (RE.RelType) {
290 llvm_unreachable("Invalid relocation type!");
291 case MachO::ARM64_RELOC_UNSIGNED:
292 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
293 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.
299 // Mask the value to 26 bits.
300 uint64_t FinalValue = Value & 0x3ffffff;
301 // Check for overflow.
302 if (FinalValue != Value)
303 return Error("ARM64 BRANCH26 relocation out of range.");
304 // Insert the value into the instruction.
305 *p = (*p & ~0x3ffffff) | FinalValue;
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!");
322 relocation_iterator RuntimeDyldMachO::processRelocationRef(
323 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
324 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
326 const ObjectFile *OF = Obj.getObjectFile();
327 const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF);
328 MachO::any_relocation_info RE =
329 MachO->getRelocation(RelI->getRawDataRefImpl());
331 uint32_t RelType = MachO->getAnyRelocationType(RE);
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))
342 RelocationValueRef Value;
343 SectionEntry &Section = Sections[SectionID];
345 bool IsExtern = MachO->getPlainRelocationExternal(RE);
346 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
347 unsigned Size = MachO->getAnyRelocationLength(RE);
349 RelI->getOffset(Offset);
350 uint8_t *LocalAddress = Section.Address + Offset;
351 unsigned NumBytes = 1 << Size;
353 memcpy(&Addend, LocalAddress, NumBytes);
356 // Obtain the symbol name which is referenced in the relocation
357 symbol_iterator Symbol = RelI->getSymbol();
358 StringRef TargetName;
359 Symbol->getName(TargetName);
360 // First search for the symbol in the local symbol table
361 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
362 if (lsi != Symbols.end()) {
363 Value.SectionID = lsi->second.first;
364 Value.Addend = lsi->second.second + Addend;
366 // Search for the symbol in the global symbol table
367 SymbolTableMap::const_iterator gsi =
368 GlobalSymbolTable.find(TargetName.data());
369 if (gsi != GlobalSymbolTable.end()) {
370 Value.SectionID = gsi->second.first;
371 Value.Addend = gsi->second.second + Addend;
373 Value.SymbolName = TargetName.data();
374 Value.Addend = Addend;
378 SectionRef Sec = MachO->getRelocationSection(RE);
381 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
383 Sec.getAddress(Addr);
384 Value.Addend = Addend - Addr;
386 Value.Addend += Offset + NumBytes;
389 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
390 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
393 StubMap::const_iterator i = Stubs.find(Value);
395 if (i != Stubs.end()) {
396 Addr = Section.Address + i->second;
398 Stubs[Value] = Section.StubOffset;
399 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
400 RelocationEntry GOTRE(SectionID, Section.StubOffset,
401 MachO::X86_64_RELOC_UNSIGNED, 0, false, 3);
402 if (Value.SymbolName)
403 addRelocationForSymbol(GOTRE, Value.SymbolName);
405 addRelocationForSection(GOTRE, Value.SectionID);
406 Section.StubOffset += 8;
409 RelocationEntry TargetRE(SectionID, Offset,
410 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true,
412 resolveRelocation(TargetRE, (uint64_t)Addr);
413 } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
414 // This is an ARM branch relocation, need to use a stub function.
416 // Look up for existing stub.
417 StubMap::const_iterator i = Stubs.find(Value);
419 if (i != Stubs.end()) {
420 Addr = Section.Address + i->second;
422 // Create a new stub function.
423 Stubs[Value] = Section.StubOffset;
424 uint8_t *StubTargetAddr =
425 createStubFunction(Section.Address + Section.StubOffset);
426 RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address,
427 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
428 if (Value.SymbolName)
429 addRelocationForSymbol(StubRE, Value.SymbolName);
431 addRelocationForSection(StubRE, Value.SectionID);
432 Addr = Section.Address + Section.StubOffset;
433 Section.StubOffset += getMaxStubSize();
435 RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel,
437 resolveRelocation(TargetRE, (uint64_t)Addr);
439 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size);
440 if (Value.SymbolName)
441 addRelocationForSymbol(RE, Value.SymbolName);
443 addRelocationForSection(RE, Value.SectionID);
449 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
450 if (InputBuffer->getBufferSize() < 4)
452 StringRef Magic(InputBuffer->getBufferStart(), 4);
453 if (Magic == "\xFE\xED\xFA\xCE")
455 if (Magic == "\xCE\xFA\xED\xFE")
457 if (Magic == "\xFE\xED\xFA\xCF")
459 if (Magic == "\xCF\xFA\xED\xFE")
464 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
465 return Obj->isMachO();
468 } // end namespace llvm