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"
17 #include "ObjectImageCommon.h"
18 #include "JITRegistrar.h"
20 using namespace llvm::object;
22 #define DEBUG_TYPE "dyld"
26 class MachOObjectImage : public ObjectImageCommon {
28 typedef SmallVector<uint64_t, 1> SectionAddrList;
29 SectionAddrList OldSectionAddrList;
36 void initOldAddress() {
37 MachOObjectFile *objf = static_cast<MachOObjectFile *>(ObjFile.get());
38 // Unfortunately we need to do this, since there's information encoded
39 // in the original addr of the section that we could not otherwise
40 // recover. The reason for this is that symbols do not actually store
41 // their file offset, but only their vmaddr. This means that in order
42 // to locate the symbol correctly in the object file, we need to know
43 // where the original start of the section was (including any padding,
45 for (section_iterator i = objf->section_begin(), e = objf->section_end();
49 OldSectionAddrList[i->getRawDataRefImpl().d.a] = Addr;
54 MachOObjectImage(ObjectBuffer *Input, bool is64)
55 : ObjectImageCommon(Input),
56 OldSectionAddrList(ObjFile->section_end()->getRawDataRefImpl().d.a, 0),
57 is64(is64), Registered(false) {
61 MachOObjectImage(std::unique_ptr<object::ObjectFile> Input, bool is64)
62 : ObjectImageCommon(std::move(Input)),
63 OldSectionAddrList(ObjFile->section_end()->getRawDataRefImpl().d.a, 0),
64 is64(is64), Registered(false) {
68 virtual ~MachOObjectImage() {
70 deregisterWithDebugger();
73 // Subclasses can override these methods to update the image with loaded
74 // addresses for sections and common symbols
75 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) {
76 MachOObjectFile *objf = static_cast<MachOObjectFile *>(ObjFile.get());
78 const_cast<char *>(objf->getSectionPointer(Sec.getRawDataRefImpl()));
80 uint64_t oldAddr = OldSectionAddrList[Sec.getRawDataRefImpl().d.a];
83 ((MachO::section_64 *)data)->addr = Addr;
85 ((MachO::section *)data)->addr = Addr;
88 for (symbol_iterator i = objf->symbol_begin(), e = objf->symbol_end();
90 section_iterator symSec(objf->section_end());
91 (*i).getSection(symSec);
94 (*i).getAddress(symAddr);
95 updateSymbolAddress(*i, symAddr + Addr - oldAddr);
100 uint64_t getOldSectionAddr(const SectionRef &Sec) const {
101 return OldSectionAddrList[Sec.getRawDataRefImpl().d.a];
104 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) {
105 char *data = const_cast<char *>(
106 reinterpret_cast<const char *>(Sym.getRawDataRefImpl().p));
108 ((MachO::nlist_64 *)data)->n_value = Addr;
110 ((MachO::nlist *)data)->n_value = Addr;
113 virtual void registerWithDebugger() {
114 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
118 virtual void deregisterWithDebugger() {
119 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
123 ObjectImage *RuntimeDyldMachO::createObjectImage(ObjectBuffer *Buffer) {
124 uint32_t magic = *((uint32_t *)Buffer->getBufferStart());
125 bool is64 = (magic == MachO::MH_MAGIC_64);
126 assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
127 "Unrecognized Macho Magic");
128 return new MachOObjectImage(Buffer, is64);
131 ObjectImage *RuntimeDyldMachO::createObjectImageFromFile(
132 std::unique_ptr<object::ObjectFile> ObjFile) {
136 MemoryBuffer *Buffer =
137 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
139 uint32_t magic = *((uint32_t *)Buffer->getBufferStart());
140 bool is64 = (magic == MachO::MH_MAGIC_64);
141 assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
142 "Unrecognized Macho Magic");
143 return new MachOObjectImage(std::move(ObjFile), is64);
146 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
147 intptr_t DeltaForEH) {
148 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
149 << ", Delta for EH: " << DeltaForEH << "\n");
150 uint32_t Length = *((uint32_t *)P);
152 unsigned char *Ret = P + Length;
153 uint32_t Offset = *((uint32_t *)P);
154 if (Offset == 0) // is a CIE
158 intptr_t FDELocation = *((intptr_t *)P);
159 intptr_t NewLocation = FDELocation - DeltaForText;
160 *((intptr_t *)P) = NewLocation;
161 P += sizeof(intptr_t);
163 // Skip the FDE address range
164 P += sizeof(intptr_t);
166 uint8_t Augmentationsize = *P;
168 if (Augmentationsize != 0) {
169 intptr_t LSDA = *((intptr_t *)P);
170 intptr_t NewLSDA = LSDA - DeltaForEH;
171 *((intptr_t *)P) = NewLSDA;
177 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
178 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
179 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
180 return ObjDistance - MemDistance;
183 void RuntimeDyldMachO::registerEHFrames() {
187 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
188 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
189 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
190 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
192 SectionEntry *Text = &Sections[SectionInfo.TextSID];
193 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
194 SectionEntry *ExceptTab = nullptr;
195 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
196 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
198 intptr_t DeltaForText = computeDelta(Text, EHFrame);
199 intptr_t DeltaForEH = 0;
201 DeltaForEH = computeDelta(ExceptTab, EHFrame);
203 unsigned char *P = EHFrame->Address;
204 unsigned char *End = P + EHFrame->Size;
206 P = processFDE(P, DeltaForText, DeltaForEH);
209 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
212 UnregisteredEHFrameSections.clear();
215 void RuntimeDyldMachO::finalizeLoad(ObjectImage &ObjImg,
216 ObjSectionToIDMap &SectionMap) {
217 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
218 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
219 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
220 ObjSectionToIDMap::iterator i, e;
221 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
222 const SectionRef &Section = i->first;
224 Section.getName(Name);
225 if (Name == "__eh_frame")
226 EHFrameSID = i->second;
227 else if (Name == "__text")
229 else if (Name == "__gcc_except_tab")
230 ExceptTabSID = i->second;
231 else if (Name == "__jump_table")
232 populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
234 else if (Name == "__pointers")
235 populatePointersSection(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
238 UnregisteredEHFrameSections.push_back(
239 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
242 // The target location for the relocation is described by RE.SectionID and
243 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
244 // SectionEntry has three members describing its location.
245 // SectionEntry::Address is the address at which the section has been loaded
246 // into memory in the current (host) process. SectionEntry::LoadAddress is the
247 // address that the section will have in the target process.
248 // SectionEntry::ObjAddress is the address of the bits for this section in the
249 // original emitted object image (also in the current address space).
251 // Relocations will be applied as if the section were loaded at
252 // SectionEntry::LoadAddress, but they will be applied at an address based
253 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
254 // Target memory contents if they are required for value calculations.
256 // The Value parameter here is the load address of the symbol for the
257 // relocation to be applied. For relocations which refer to symbols in the
258 // current object Value will be the LoadAddress of the section in which
259 // the symbol resides (RE.Addend provides additional information about the
260 // symbol location). For external symbols, Value will be the address of the
261 // symbol in the target address space.
262 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
265 const SectionEntry &Section = Sections[RE.SectionID];
266 uint8_t* LocalAddress = Section.Address + RE.Offset;
267 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
269 dbgs() << "resolveRelocation Section: " << RE.SectionID
270 << " LocalAddress: " << format("%p", LocalAddress)
271 << " FinalAddress: " << format("%p", FinalAddress)
272 << " Value: " << format("%p", Value)
273 << " Addend: " << RE.Addend
274 << " isPCRel: " << RE.IsPCRel
275 << " MachoType: " << RE.RelType
276 << " Size: " << (1 << RE.Size) << "\n";
279 // This just dispatches to the proper target specific routine.
282 llvm_unreachable("Unsupported CPU type!");
284 resolveX86_64Relocation(RE, Value);
287 resolveI386Relocation(RE, Value);
289 case Triple::arm: // Fall through.
291 resolveARMRelocation(RE, Value);
293 case Triple::aarch64:
295 resolveAArch64Relocation(RE, Value);
300 bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE,
302 const SectionEntry &Section = Sections[RE.SectionID];
303 uint8_t* LocalAddress = Section.Address + RE.Offset;
306 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
307 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
310 switch (RE.RelType) {
312 llvm_unreachable("Invalid relocation type!");
313 case MachO::GENERIC_RELOC_VANILLA:
314 return applyRelocationValue(LocalAddress, Value + RE.Addend,
316 case MachO::GENERIC_RELOC_SECTDIFF:
317 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
318 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
319 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
320 assert((Value == SectionABase || Value == SectionBBase) &&
321 "Unexpected SECTDIFF relocation value.");
322 Value = SectionABase - SectionBBase + RE.Addend;
323 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
325 case MachO::GENERIC_RELOC_PB_LA_PTR:
326 return Error("Relocation type not implemented yet!");
330 bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE,
332 const SectionEntry &Section = Sections[RE.SectionID];
333 uint8_t* LocalAddress = Section.Address + RE.Offset;
335 // If the relocation is PC-relative, the value to be encoded is the
336 // pointer difference.
338 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
339 // address. Is that expected? Only for branches, perhaps?
340 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
341 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
344 switch (RE.RelType) {
346 llvm_unreachable("Invalid relocation type!");
347 case MachO::X86_64_RELOC_SIGNED_1:
348 case MachO::X86_64_RELOC_SIGNED_2:
349 case MachO::X86_64_RELOC_SIGNED_4:
350 case MachO::X86_64_RELOC_SIGNED:
351 case MachO::X86_64_RELOC_UNSIGNED:
352 case MachO::X86_64_RELOC_BRANCH:
353 return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
354 case MachO::X86_64_RELOC_GOT_LOAD:
355 case MachO::X86_64_RELOC_GOT:
356 case MachO::X86_64_RELOC_SUBTRACTOR:
357 case MachO::X86_64_RELOC_TLV:
358 return Error("Relocation type not implemented yet!");
362 bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE,
364 const SectionEntry &Section = Sections[RE.SectionID];
365 uint8_t* LocalAddress = Section.Address + RE.Offset;
367 // If the relocation is PC-relative, the value to be encoded is the
368 // pointer difference.
370 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
371 Value -= FinalAddress;
372 // ARM PCRel relocations have an effective-PC offset of two instructions
373 // (four bytes in Thumb mode, 8 bytes in ARM mode).
374 // FIXME: For now, assume ARM mode.
378 switch (RE.RelType) {
380 llvm_unreachable("Invalid relocation type!");
381 case MachO::ARM_RELOC_VANILLA:
382 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
383 case MachO::ARM_RELOC_BR24: {
384 // Mask the value into the target address. We know instructions are
385 // 32-bit aligned, so we can do it all at once.
386 uint32_t *p = (uint32_t *)LocalAddress;
387 // The low two bits of the value are not encoded.
389 // Mask the value to 24 bits.
390 uint64_t FinalValue = Value & 0xffffff;
391 // Check for overflow.
392 if (Value != FinalValue)
393 return Error("ARM BR24 relocation out of range.");
394 // FIXME: If the destination is a Thumb function (and the instruction
395 // is a non-predicated BL instruction), we need to change it to a BLX
396 // instruction instead.
398 // Insert the value into the instruction.
399 *p = (*p & ~0xffffff) | FinalValue;
402 case MachO::ARM_THUMB_RELOC_BR22:
403 case MachO::ARM_THUMB_32BIT_BRANCH:
404 case MachO::ARM_RELOC_HALF:
405 case MachO::ARM_RELOC_HALF_SECTDIFF:
406 case MachO::ARM_RELOC_PAIR:
407 case MachO::ARM_RELOC_SECTDIFF:
408 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
409 case MachO::ARM_RELOC_PB_LA_PTR:
410 return Error("Relocation type not implemented yet!");
415 bool RuntimeDyldMachO::resolveAArch64Relocation(const RelocationEntry &RE,
417 const SectionEntry &Section = Sections[RE.SectionID];
418 uint8_t* LocalAddress = Section.Address + RE.Offset;
420 // If the relocation is PC-relative, the value to be encoded is the
421 // pointer difference.
423 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
424 Value -= FinalAddress;
427 switch (RE.RelType) {
429 llvm_unreachable("Invalid relocation type!");
430 case MachO::ARM64_RELOC_UNSIGNED:
431 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
432 case MachO::ARM64_RELOC_BRANCH26: {
433 // Mask the value into the target address. We know instructions are
434 // 32-bit aligned, so we can do it all at once.
435 uint32_t *p = (uint32_t *)LocalAddress;
436 // The low two bits of the value are not encoded.
438 // Mask the value to 26 bits.
439 uint64_t FinalValue = Value & 0x3ffffff;
440 // Check for overflow.
441 if (FinalValue != Value)
442 return Error("ARM64 BRANCH26 relocation out of range.");
443 // Insert the value into the instruction.
444 *p = (*p & ~0x3ffffff) | FinalValue;
447 case MachO::ARM64_RELOC_SUBTRACTOR:
448 case MachO::ARM64_RELOC_PAGE21:
449 case MachO::ARM64_RELOC_PAGEOFF12:
450 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
451 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
452 case MachO::ARM64_RELOC_POINTER_TO_GOT:
453 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
454 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
455 case MachO::ARM64_RELOC_ADDEND:
456 return Error("Relocation type not implemented yet!");
461 void RuntimeDyldMachO::populateJumpTable(MachOObjectFile &Obj,
462 const SectionRef &JTSection,
463 unsigned JTSectionID) {
464 assert(!Obj.is64Bit() &&
465 "__jump_table section not supported in 64-bit MachO.");
467 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
468 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
469 uint32_t JTSectionSize = Sec32.size;
470 unsigned FirstIndirectSymbol = Sec32.reserved1;
471 unsigned JTEntrySize = Sec32.reserved2;
472 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
473 uint8_t* JTSectionAddr = getSectionAddress(JTSectionID);
474 unsigned JTEntryOffset = 0;
476 assert((JTSectionSize % JTEntrySize) == 0 &&
477 "Jump-table section does not contain a whole number of stubs?");
479 for (unsigned i = 0; i < NumJTEntries; ++i) {
480 unsigned SymbolIndex =
481 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
482 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
483 StringRef IndirectSymbolName;
484 SI->getName(IndirectSymbolName);
485 uint8_t* JTEntryAddr = JTSectionAddr + JTEntryOffset;
486 createStubFunction(JTEntryAddr);
487 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
488 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
489 addRelocationForSymbol(RE, IndirectSymbolName);
490 JTEntryOffset += JTEntrySize;
494 void RuntimeDyldMachO::populatePointersSection(MachOObjectFile &Obj,
495 const SectionRef &PTSection,
496 unsigned PTSectionID) {
497 assert(!Obj.is64Bit() &&
498 "__pointers section not supported in 64-bit MachO.");
500 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
501 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
502 uint32_t PTSectionSize = Sec32.size;
503 unsigned FirstIndirectSymbol = Sec32.reserved1;
504 const unsigned PTEntrySize = 4;
505 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
506 unsigned PTEntryOffset = 0;
508 assert((PTSectionSize % PTEntrySize) == 0 &&
509 "Pointers section does not contain a whole number of stubs?");
511 DEBUG(dbgs() << "Populating __pointers, Section ID " << PTSectionID
512 << ", " << NumPTEntries << " entries, "
513 << PTEntrySize << " bytes each:\n");
515 for (unsigned i = 0; i < NumPTEntries; ++i) {
516 unsigned SymbolIndex =
517 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
518 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
519 StringRef IndirectSymbolName;
520 SI->getName(IndirectSymbolName);
521 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
522 << ", PT offset: " << PTEntryOffset << "\n");
523 RelocationEntry RE(PTSectionID, PTEntryOffset,
524 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
525 addRelocationForSymbol(RE, IndirectSymbolName);
526 PTEntryOffset += PTEntrySize;
531 section_iterator getSectionByAddress(const MachOObjectFile &Obj,
533 section_iterator SI = Obj.section_begin();
534 section_iterator SE = Obj.section_end();
536 for (; SI != SE; ++SI) {
537 uint64_t SAddr, SSize;
538 SI->getAddress(SAddr);
540 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
547 relocation_iterator RuntimeDyldMachO::processSECTDIFFRelocation(
549 relocation_iterator RelI,
551 ObjSectionToIDMap &ObjSectionToID) {
552 const MachOObjectFile *MachO =
553 static_cast<const MachOObjectFile*>(Obj.getObjectFile());
554 MachO::any_relocation_info RE =
555 MachO->getRelocation(RelI->getRawDataRefImpl());
557 SectionEntry &Section = Sections[SectionID];
558 uint32_t RelocType = MachO->getAnyRelocationType(RE);
559 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
560 unsigned Size = MachO->getAnyRelocationLength(RE);
562 RelI->getOffset(Offset);
563 uint8_t *LocalAddress = Section.Address + Offset;
564 unsigned NumBytes = 1 << Size;
566 memcpy(&Addend, LocalAddress, NumBytes);
569 MachO::any_relocation_info RE2 =
570 MachO->getRelocation(RelI->getRawDataRefImpl());
572 uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
573 section_iterator SAI = getSectionByAddress(*MachO, AddrA);
574 assert(SAI != MachO->section_end() && "Can't find section for address A");
575 uint64_t SectionABase;
576 SAI->getAddress(SectionABase);
577 uint64_t SectionAOffset = AddrA - SectionABase;
578 SectionRef SectionA = *SAI;
580 SectionA.isText(IsCode);
581 uint32_t SectionAID = findOrEmitSection(Obj, SectionA, IsCode,
584 uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
585 section_iterator SBI = getSectionByAddress(*MachO, AddrB);
586 assert(SBI != MachO->section_end() && "Can't find section for address B");
587 uint64_t SectionBBase;
588 SBI->getAddress(SectionBBase);
589 uint64_t SectionBOffset = AddrB - SectionBBase;
590 SectionRef SectionB = *SBI;
591 uint32_t SectionBID = findOrEmitSection(Obj, SectionB, IsCode,
594 if (Addend != AddrA - AddrB)
595 Error("Unexpected SECTDIFF relocation addend.");
597 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
598 << ", Addend: " << Addend << ", SectionA ID: "
599 << SectionAID << ", SectionAOffset: " << SectionAOffset
600 << ", SectionB ID: " << SectionBID << ", SectionBOffset: "
601 << SectionBOffset << "\n");
602 RelocationEntry R(SectionID, Offset, RelocType, 0,
603 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
606 addRelocationForSection(R, SectionAID);
607 addRelocationForSection(R, SectionBID);
612 relocation_iterator RuntimeDyldMachO::processI386ScatteredVANILLA(
614 relocation_iterator RelI,
616 ObjSectionToIDMap &ObjSectionToID) {
617 const MachOObjectFile *MachO =
618 static_cast<const MachOObjectFile*>(Obj.getObjectFile());
619 MachO::any_relocation_info RE =
620 MachO->getRelocation(RelI->getRawDataRefImpl());
622 SectionEntry &Section = Sections[SectionID];
623 uint32_t RelocType = MachO->getAnyRelocationType(RE);
624 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
625 unsigned Size = MachO->getAnyRelocationLength(RE);
627 RelI->getOffset(Offset);
628 uint8_t *LocalAddress = Section.Address + Offset;
629 unsigned NumBytes = 1 << Size;
631 memcpy(&Addend, LocalAddress, NumBytes);
633 unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
634 section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
635 assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
636 uint64_t SectionBaseAddr;
637 TargetSI->getAddress(SectionBaseAddr);
638 SectionRef TargetSection = *TargetSI;
640 TargetSection.isText(IsCode);
641 uint32_t TargetSectionID = findOrEmitSection(Obj, TargetSection, IsCode,
644 Addend -= SectionBaseAddr;
645 RelocationEntry R(SectionID, Offset, RelocType, Addend,
648 addRelocationForSection(R, TargetSectionID);
653 relocation_iterator RuntimeDyldMachO::processRelocationRef(
654 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
655 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
657 const ObjectFile *OF = Obj.getObjectFile();
658 const MachOObjectImage &MachOObj = *static_cast<MachOObjectImage *>(&Obj);
659 const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF);
660 MachO::any_relocation_info RE =
661 MachO->getRelocation(RelI->getRawDataRefImpl());
663 uint32_t RelType = MachO->getAnyRelocationType(RE);
665 // FIXME: Properly handle scattered relocations.
666 // Special case the couple of scattered relocations that we know how
667 // to handle: SECTDIFF relocations, and scattered VANILLA relocations
669 // For all other scattered relocations, just bail out and hope for the
670 // best, since the offsets computed by scattered relocations have often
671 // been optimisticaly filled in by the compiler. This will fail
672 // horribly where the relocations *do* need to be applied, but that was
674 if (MachO->isRelocationScattered(RE)) {
675 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
676 RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
677 return processSECTDIFFRelocation(SectionID, RelI, Obj, ObjSectionToID);
678 else if (Arch == Triple::x86 && RelType == MachO::GENERIC_RELOC_VANILLA)
679 return processI386ScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
684 RelocationValueRef Value;
685 SectionEntry &Section = Sections[SectionID];
687 bool IsExtern = MachO->getPlainRelocationExternal(RE);
688 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
689 unsigned Size = MachO->getAnyRelocationLength(RE);
691 RelI->getOffset(Offset);
692 uint8_t *LocalAddress = Section.Address + Offset;
693 unsigned NumBytes = 1 << Size;
695 memcpy(&Addend, LocalAddress, NumBytes);
698 // Obtain the symbol name which is referenced in the relocation
699 symbol_iterator Symbol = RelI->getSymbol();
700 StringRef TargetName;
701 Symbol->getName(TargetName);
702 // First search for the symbol in the local symbol table
703 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
704 if (lsi != Symbols.end()) {
705 Value.SectionID = lsi->second.first;
706 Value.Addend = lsi->second.second + Addend;
708 // Search for the symbol in the global symbol table
709 SymbolTableMap::const_iterator gsi =
710 GlobalSymbolTable.find(TargetName.data());
711 if (gsi != GlobalSymbolTable.end()) {
712 Value.SectionID = gsi->second.first;
713 Value.Addend = gsi->second.second + Addend;
715 Value.SymbolName = TargetName.data();
716 Value.Addend = Addend;
720 // Addends for external, PC-rel relocations on i386 point back to the zero
721 // offset. Calculate the final offset from the relocation target instead.
722 // This allows us to use the same logic for both external and internal
723 // relocations in resolveI386RelocationRef.
724 if (Arch == Triple::x86 && IsPCRel) {
725 uint64_t RelocAddr = 0;
726 RelI->getAddress(RelocAddr);
727 Value.Addend += RelocAddr + 4;
731 SectionRef Sec = MachO->getRelocationSection(RE);
734 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
735 uint64_t Addr = MachOObj.getOldSectionAddr(Sec);
736 DEBUG(dbgs() << "\nAddr: " << Addr << "\nAddend: " << Addend);
737 Value.Addend = Addend - Addr;
739 Value.Addend += Offset + NumBytes;
742 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
743 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
747 // FIXME: Teach the generic code above not to prematurely conflate
748 // relocation addends and symbol offsets.
749 Value.Addend -= Addend;
750 StubMap::const_iterator i = Stubs.find(Value);
752 if (i != Stubs.end()) {
753 Addr = Section.Address + i->second;
755 Stubs[Value] = Section.StubOffset;
756 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
757 RelocationEntry GOTRE(SectionID, Section.StubOffset,
758 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, false,
760 if (Value.SymbolName)
761 addRelocationForSymbol(GOTRE, Value.SymbolName);
763 addRelocationForSection(GOTRE, Value.SectionID);
764 Section.StubOffset += 8;
767 RelocationEntry TargetRE(SectionID, Offset,
768 MachO::X86_64_RELOC_UNSIGNED, Addend, true,
770 resolveRelocation(TargetRE, (uint64_t)Addr);
771 } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
772 // This is an ARM branch relocation, need to use a stub function.
774 // Look up for existing stub.
775 StubMap::const_iterator i = Stubs.find(Value);
777 if (i != Stubs.end()) {
778 Addr = Section.Address + i->second;
780 // Create a new stub function.
781 Stubs[Value] = Section.StubOffset;
782 uint8_t *StubTargetAddr =
783 createStubFunction(Section.Address + Section.StubOffset);
784 RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address,
785 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
786 if (Value.SymbolName)
787 addRelocationForSymbol(StubRE, Value.SymbolName);
789 addRelocationForSection(StubRE, Value.SectionID);
790 Addr = Section.Address + Section.StubOffset;
791 Section.StubOffset += getMaxStubSize();
793 RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel,
795 resolveRelocation(TargetRE, (uint64_t)Addr);
797 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size);
798 if (Value.SymbolName)
799 addRelocationForSymbol(RE, Value.SymbolName);
801 addRelocationForSection(RE, Value.SectionID);
807 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
808 if (InputBuffer->getBufferSize() < 4)
810 StringRef Magic(InputBuffer->getBufferStart(), 4);
811 if (Magic == "\xFE\xED\xFA\xCE")
813 if (Magic == "\xCE\xFA\xED\xFE")
815 if (Magic == "\xFE\xED\xFA\xCF")
817 if (Magic == "\xCF\xFA\xED\xFE")
822 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
823 return Obj->isMachO();
826 } // end namespace llvm