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 // Subclasses can override these methods to update the image with loaded
71 // addresses for sections and common symbols
72 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) {
73 MachOObjectFile *objf = static_cast<MachOObjectFile *>(ObjFile.get());
75 const_cast<char *>(objf->getSectionPointer(Sec.getRawDataRefImpl()));
77 uint64_t oldAddr = OldSectionAddrList[Sec.getRawDataRefImpl().d.a];
80 ((MachO::section_64 *)data)->addr = Addr;
82 ((MachO::section *)data)->addr = Addr;
85 for (symbol_iterator i = objf->symbol_begin(), e = objf->symbol_end();
87 section_iterator symSec(objf->section_end());
88 (*i).getSection(symSec);
91 (*i).getAddress(symAddr);
92 updateSymbolAddress(*i, symAddr + Addr - oldAddr);
97 uint64_t getOldSectionAddr(const SectionRef &Sec) const {
98 return OldSectionAddrList[Sec.getRawDataRefImpl().d.a];
101 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) {
102 char *data = const_cast<char *>(
103 reinterpret_cast<const char *>(Sym.getRawDataRefImpl().p));
105 ((MachO::nlist_64 *)data)->n_value = Addr;
107 ((MachO::nlist *)data)->n_value = Addr;
110 virtual void registerWithDebugger() {
111 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
115 virtual void deregisterWithDebugger() {
116 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
120 ObjectImage *RuntimeDyldMachO::createObjectImage(ObjectBuffer *Buffer) {
121 uint32_t magic = *((uint32_t *)Buffer->getBufferStart());
122 bool is64 = (magic == MachO::MH_MAGIC_64);
123 assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
124 "Unrecognized Macho Magic");
125 return new MachOObjectImage(Buffer, is64);
128 ObjectImage *RuntimeDyldMachO::createObjectImageFromFile(
129 std::unique_ptr<object::ObjectFile> ObjFile) {
133 MemoryBuffer *Buffer =
134 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
136 uint32_t magic = *((uint32_t *)Buffer->getBufferStart());
137 bool is64 = (magic == MachO::MH_MAGIC_64);
138 assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
139 "Unrecognized Macho Magic");
140 return new MachOObjectImage(std::move(ObjFile), is64);
143 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
144 intptr_t DeltaForEH) {
145 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
146 << ", Delta for EH: " << DeltaForEH << "\n");
147 uint32_t Length = *((uint32_t *)P);
149 unsigned char *Ret = P + Length;
150 uint32_t Offset = *((uint32_t *)P);
151 if (Offset == 0) // is a CIE
155 intptr_t FDELocation = *((intptr_t *)P);
156 intptr_t NewLocation = FDELocation - DeltaForText;
157 *((intptr_t *)P) = NewLocation;
158 P += sizeof(intptr_t);
160 // Skip the FDE address range
161 P += sizeof(intptr_t);
163 uint8_t Augmentationsize = *P;
165 if (Augmentationsize != 0) {
166 intptr_t LSDA = *((intptr_t *)P);
167 intptr_t NewLSDA = LSDA - DeltaForEH;
168 *((intptr_t *)P) = NewLSDA;
174 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
175 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
176 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
177 return ObjDistance - MemDistance;
180 void RuntimeDyldMachO::registerEHFrames() {
184 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
185 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
186 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
187 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
189 SectionEntry *Text = &Sections[SectionInfo.TextSID];
190 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
191 SectionEntry *ExceptTab = nullptr;
192 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
193 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
195 intptr_t DeltaForText = computeDelta(Text, EHFrame);
196 intptr_t DeltaForEH = 0;
198 DeltaForEH = computeDelta(ExceptTab, EHFrame);
200 unsigned char *P = EHFrame->Address;
201 unsigned char *End = P + EHFrame->Size;
203 P = processFDE(P, DeltaForText, DeltaForEH);
206 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
209 UnregisteredEHFrameSections.clear();
212 void RuntimeDyldMachO::finalizeLoad(ObjectImage &ObjImg,
213 ObjSectionToIDMap &SectionMap) {
214 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
215 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
216 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
217 ObjSectionToIDMap::iterator i, e;
218 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
219 const SectionRef &Section = i->first;
221 Section.getName(Name);
222 if (Name == "__eh_frame")
223 EHFrameSID = i->second;
224 else if (Name == "__text")
226 else if (Name == "__gcc_except_tab")
227 ExceptTabSID = i->second;
228 else if (Name == "__jump_table")
229 populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
231 else if (Name == "__pointers")
232 populatePointersSection(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
235 UnregisteredEHFrameSections.push_back(
236 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
239 // The target location for the relocation is described by RE.SectionID and
240 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
241 // SectionEntry has three members describing its location.
242 // SectionEntry::Address is the address at which the section has been loaded
243 // into memory in the current (host) process. SectionEntry::LoadAddress is the
244 // address that the section will have in the target process.
245 // SectionEntry::ObjAddress is the address of the bits for this section in the
246 // original emitted object image (also in the current address space).
248 // Relocations will be applied as if the section were loaded at
249 // SectionEntry::LoadAddress, but they will be applied at an address based
250 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
251 // Target memory contents if they are required for value calculations.
253 // The Value parameter here is the load address of the symbol for the
254 // relocation to be applied. For relocations which refer to symbols in the
255 // current object Value will be the LoadAddress of the section in which
256 // the symbol resides (RE.Addend provides additional information about the
257 // symbol location). For external symbols, Value will be the address of the
258 // symbol in the target address space.
259 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
262 const SectionEntry &Section = Sections[RE.SectionID];
263 uint8_t* LocalAddress = Section.Address + RE.Offset;
264 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
266 dbgs() << "resolveRelocation Section: " << RE.SectionID
267 << " LocalAddress: " << format("%p", LocalAddress)
268 << " FinalAddress: " << format("%p", FinalAddress)
269 << " Value: " << format("%p", Value)
270 << " Addend: " << RE.Addend
271 << " isPCRel: " << RE.IsPCRel
272 << " MachoType: " << RE.RelType
273 << " Size: " << (1 << RE.Size) << "\n";
276 // This just dispatches to the proper target specific routine.
279 llvm_unreachable("Unsupported CPU type!");
281 resolveX86_64Relocation(RE, Value);
284 resolveI386Relocation(RE, Value);
286 case Triple::arm: // Fall through.
288 resolveARMRelocation(RE, Value);
290 case Triple::aarch64:
292 resolveAArch64Relocation(RE, Value);
297 bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE,
299 const SectionEntry &Section = Sections[RE.SectionID];
300 uint8_t* LocalAddress = Section.Address + RE.Offset;
303 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
304 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
307 switch (RE.RelType) {
309 llvm_unreachable("Invalid relocation type!");
310 case MachO::GENERIC_RELOC_VANILLA:
311 return applyRelocationValue(LocalAddress, Value + RE.Addend,
313 case MachO::GENERIC_RELOC_SECTDIFF:
314 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
315 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
316 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
317 assert((Value == SectionABase || Value == SectionBBase) &&
318 "Unexpected SECTDIFF relocation value.");
319 Value = SectionABase - SectionBBase + RE.Addend;
320 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
322 case MachO::GENERIC_RELOC_PB_LA_PTR:
323 return Error("Relocation type not implemented yet!");
327 bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE,
329 const SectionEntry &Section = Sections[RE.SectionID];
330 uint8_t* LocalAddress = Section.Address + RE.Offset;
332 // If the relocation is PC-relative, the value to be encoded is the
333 // pointer difference.
335 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
336 // address. Is that expected? Only for branches, perhaps?
337 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
338 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
341 switch (RE.RelType) {
343 llvm_unreachable("Invalid relocation type!");
344 case MachO::X86_64_RELOC_SIGNED_1:
345 case MachO::X86_64_RELOC_SIGNED_2:
346 case MachO::X86_64_RELOC_SIGNED_4:
347 case MachO::X86_64_RELOC_SIGNED:
348 case MachO::X86_64_RELOC_UNSIGNED:
349 case MachO::X86_64_RELOC_BRANCH:
350 return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
351 case MachO::X86_64_RELOC_GOT_LOAD:
352 case MachO::X86_64_RELOC_GOT:
353 case MachO::X86_64_RELOC_SUBTRACTOR:
354 case MachO::X86_64_RELOC_TLV:
355 return Error("Relocation type not implemented yet!");
359 bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE,
361 const SectionEntry &Section = Sections[RE.SectionID];
362 uint8_t* LocalAddress = Section.Address + RE.Offset;
364 // If the relocation is PC-relative, the value to be encoded is the
365 // pointer difference.
367 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
368 Value -= FinalAddress;
369 // ARM PCRel relocations have an effective-PC offset of two instructions
370 // (four bytes in Thumb mode, 8 bytes in ARM mode).
371 // FIXME: For now, assume ARM mode.
375 switch (RE.RelType) {
377 llvm_unreachable("Invalid relocation type!");
378 case MachO::ARM_RELOC_VANILLA:
379 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
380 case MachO::ARM_RELOC_BR24: {
381 // Mask the value into the target address. We know instructions are
382 // 32-bit aligned, so we can do it all at once.
383 uint32_t *p = (uint32_t *)LocalAddress;
384 // The low two bits of the value are not encoded.
386 // Mask the value to 24 bits.
387 uint64_t FinalValue = Value & 0xffffff;
388 // Check for overflow.
389 if (Value != FinalValue)
390 return Error("ARM BR24 relocation out of range.");
391 // FIXME: If the destination is a Thumb function (and the instruction
392 // is a non-predicated BL instruction), we need to change it to a BLX
393 // instruction instead.
395 // Insert the value into the instruction.
396 *p = (*p & ~0xffffff) | FinalValue;
399 case MachO::ARM_THUMB_RELOC_BR22:
400 case MachO::ARM_THUMB_32BIT_BRANCH:
401 case MachO::ARM_RELOC_HALF:
402 case MachO::ARM_RELOC_HALF_SECTDIFF:
403 case MachO::ARM_RELOC_PAIR:
404 case MachO::ARM_RELOC_SECTDIFF:
405 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
406 case MachO::ARM_RELOC_PB_LA_PTR:
407 return Error("Relocation type not implemented yet!");
412 bool RuntimeDyldMachO::resolveAArch64Relocation(const RelocationEntry &RE,
414 const SectionEntry &Section = Sections[RE.SectionID];
415 uint8_t* LocalAddress = Section.Address + RE.Offset;
417 // If the relocation is PC-relative, the value to be encoded is the
418 // pointer difference.
420 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
421 Value -= FinalAddress;
424 switch (RE.RelType) {
426 llvm_unreachable("Invalid relocation type!");
427 case MachO::ARM64_RELOC_UNSIGNED:
428 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
429 case MachO::ARM64_RELOC_BRANCH26: {
430 // Mask the value into the target address. We know instructions are
431 // 32-bit aligned, so we can do it all at once.
432 uint32_t *p = (uint32_t *)LocalAddress;
433 // The low two bits of the value are not encoded.
435 // Mask the value to 26 bits.
436 uint64_t FinalValue = Value & 0x3ffffff;
437 // Check for overflow.
438 if (FinalValue != Value)
439 return Error("ARM64 BRANCH26 relocation out of range.");
440 // Insert the value into the instruction.
441 *p = (*p & ~0x3ffffff) | FinalValue;
444 case MachO::ARM64_RELOC_SUBTRACTOR:
445 case MachO::ARM64_RELOC_PAGE21:
446 case MachO::ARM64_RELOC_PAGEOFF12:
447 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
448 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
449 case MachO::ARM64_RELOC_POINTER_TO_GOT:
450 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
451 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
452 case MachO::ARM64_RELOC_ADDEND:
453 return Error("Relocation type not implemented yet!");
458 void RuntimeDyldMachO::populateJumpTable(MachOObjectFile &Obj,
459 const SectionRef &JTSection,
460 unsigned JTSectionID) {
461 assert(!Obj.is64Bit() &&
462 "__jump_table section not supported in 64-bit MachO.");
464 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
465 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
466 uint32_t JTSectionSize = Sec32.size;
467 unsigned FirstIndirectSymbol = Sec32.reserved1;
468 unsigned JTEntrySize = Sec32.reserved2;
469 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
470 uint8_t* JTSectionAddr = getSectionAddress(JTSectionID);
471 unsigned JTEntryOffset = 0;
473 assert((JTSectionSize % JTEntrySize) == 0 &&
474 "Jump-table section does not contain a whole number of stubs?");
476 for (unsigned i = 0; i < NumJTEntries; ++i) {
477 unsigned SymbolIndex =
478 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
479 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
480 StringRef IndirectSymbolName;
481 SI->getName(IndirectSymbolName);
482 uint8_t* JTEntryAddr = JTSectionAddr + JTEntryOffset;
483 createStubFunction(JTEntryAddr);
484 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
485 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
486 addRelocationForSymbol(RE, IndirectSymbolName);
487 JTEntryOffset += JTEntrySize;
491 void RuntimeDyldMachO::populatePointersSection(MachOObjectFile &Obj,
492 const SectionRef &PTSection,
493 unsigned PTSectionID) {
494 assert(!Obj.is64Bit() &&
495 "__pointers section not supported in 64-bit MachO.");
497 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
498 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
499 uint32_t PTSectionSize = Sec32.size;
500 unsigned FirstIndirectSymbol = Sec32.reserved1;
501 const unsigned PTEntrySize = 4;
502 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
503 unsigned PTEntryOffset = 0;
505 assert((PTSectionSize % PTEntrySize) == 0 &&
506 "Pointers section does not contain a whole number of stubs?");
508 DEBUG(dbgs() << "Populating __pointers, Section ID " << PTSectionID
509 << ", " << NumPTEntries << " entries, "
510 << PTEntrySize << " bytes each:\n");
512 for (unsigned i = 0; i < NumPTEntries; ++i) {
513 unsigned SymbolIndex =
514 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
515 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
516 StringRef IndirectSymbolName;
517 SI->getName(IndirectSymbolName);
518 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
519 << ", PT offset: " << PTEntryOffset << "\n");
520 RelocationEntry RE(PTSectionID, PTEntryOffset,
521 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
522 addRelocationForSymbol(RE, IndirectSymbolName);
523 PTEntryOffset += PTEntrySize;
528 section_iterator getSectionByAddress(const MachOObjectFile &Obj,
530 section_iterator SI = Obj.section_begin();
531 section_iterator SE = Obj.section_end();
533 for (; SI != SE; ++SI) {
534 uint64_t SAddr, SSize;
535 SI->getAddress(SAddr);
537 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
544 relocation_iterator RuntimeDyldMachO::processSECTDIFFRelocation(
546 relocation_iterator RelI,
548 ObjSectionToIDMap &ObjSectionToID) {
549 const MachOObjectFile *MachO =
550 static_cast<const MachOObjectFile*>(Obj.getObjectFile());
551 MachO::any_relocation_info RE =
552 MachO->getRelocation(RelI->getRawDataRefImpl());
554 SectionEntry &Section = Sections[SectionID];
555 uint32_t RelocType = MachO->getAnyRelocationType(RE);
556 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
557 unsigned Size = MachO->getAnyRelocationLength(RE);
559 RelI->getOffset(Offset);
560 uint8_t *LocalAddress = Section.Address + Offset;
561 unsigned NumBytes = 1 << Size;
563 memcpy(&Addend, LocalAddress, NumBytes);
566 MachO::any_relocation_info RE2 =
567 MachO->getRelocation(RelI->getRawDataRefImpl());
569 uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
570 section_iterator SAI = getSectionByAddress(*MachO, AddrA);
571 assert(SAI != MachO->section_end() && "Can't find section for address A");
572 uint64_t SectionABase;
573 SAI->getAddress(SectionABase);
574 uint64_t SectionAOffset = AddrA - SectionABase;
575 SectionRef SectionA = *SAI;
577 SectionA.isText(IsCode);
578 uint32_t SectionAID = findOrEmitSection(Obj, SectionA, IsCode,
581 uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
582 section_iterator SBI = getSectionByAddress(*MachO, AddrB);
583 assert(SBI != MachO->section_end() && "Can't find section for address B");
584 uint64_t SectionBBase;
585 SBI->getAddress(SectionBBase);
586 uint64_t SectionBOffset = AddrB - SectionBBase;
587 SectionRef SectionB = *SBI;
588 uint32_t SectionBID = findOrEmitSection(Obj, SectionB, IsCode,
591 if (Addend != AddrA - AddrB)
592 Error("Unexpected SECTDIFF relocation addend.");
594 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
595 << ", Addend: " << Addend << ", SectionA ID: "
596 << SectionAID << ", SectionAOffset: " << SectionAOffset
597 << ", SectionB ID: " << SectionBID << ", SectionBOffset: "
598 << SectionBOffset << "\n");
599 RelocationEntry R(SectionID, Offset, RelocType, 0,
600 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
603 addRelocationForSection(R, SectionAID);
604 addRelocationForSection(R, SectionBID);
609 relocation_iterator RuntimeDyldMachO::processI386ScatteredVANILLA(
611 relocation_iterator RelI,
613 ObjSectionToIDMap &ObjSectionToID) {
614 const MachOObjectFile *MachO =
615 static_cast<const MachOObjectFile*>(Obj.getObjectFile());
616 MachO::any_relocation_info RE =
617 MachO->getRelocation(RelI->getRawDataRefImpl());
619 SectionEntry &Section = Sections[SectionID];
620 uint32_t RelocType = MachO->getAnyRelocationType(RE);
621 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
622 unsigned Size = MachO->getAnyRelocationLength(RE);
624 RelI->getOffset(Offset);
625 uint8_t *LocalAddress = Section.Address + Offset;
626 unsigned NumBytes = 1 << Size;
628 memcpy(&Addend, LocalAddress, NumBytes);
630 unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
631 section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
632 assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
633 uint64_t SectionBaseAddr;
634 TargetSI->getAddress(SectionBaseAddr);
635 SectionRef TargetSection = *TargetSI;
637 TargetSection.isText(IsCode);
638 uint32_t TargetSectionID = findOrEmitSection(Obj, TargetSection, IsCode,
641 Addend -= SectionBaseAddr;
642 RelocationEntry R(SectionID, Offset, RelocType, Addend,
645 addRelocationForSection(R, TargetSectionID);
650 relocation_iterator RuntimeDyldMachO::processRelocationRef(
651 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
652 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
654 const ObjectFile *OF = Obj.getObjectFile();
655 const MachOObjectImage &MachOObj = *static_cast<MachOObjectImage *>(&Obj);
656 const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF);
657 MachO::any_relocation_info RE =
658 MachO->getRelocation(RelI->getRawDataRefImpl());
660 uint32_t RelType = MachO->getAnyRelocationType(RE);
662 // FIXME: Properly handle scattered relocations.
663 // Special case the couple of scattered relocations that we know how
664 // to handle: SECTDIFF relocations, and scattered VANILLA relocations
666 // For all other scattered relocations, just bail out and hope for the
667 // best, since the offsets computed by scattered relocations have often
668 // been optimisticaly filled in by the compiler. This will fail
669 // horribly where the relocations *do* need to be applied, but that was
671 if (MachO->isRelocationScattered(RE)) {
672 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
673 RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
674 return processSECTDIFFRelocation(SectionID, RelI, Obj, ObjSectionToID);
675 else if (Arch == Triple::x86 && RelType == MachO::GENERIC_RELOC_VANILLA)
676 return processI386ScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
681 RelocationValueRef Value;
682 SectionEntry &Section = Sections[SectionID];
684 bool IsExtern = MachO->getPlainRelocationExternal(RE);
685 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
686 unsigned Size = MachO->getAnyRelocationLength(RE);
688 RelI->getOffset(Offset);
689 uint8_t *LocalAddress = Section.Address + Offset;
690 unsigned NumBytes = 1 << Size;
692 memcpy(&Addend, LocalAddress, NumBytes);
695 // Obtain the symbol name which is referenced in the relocation
696 symbol_iterator Symbol = RelI->getSymbol();
697 StringRef TargetName;
698 Symbol->getName(TargetName);
699 // First search for the symbol in the local symbol table
700 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
701 if (lsi != Symbols.end()) {
702 Value.SectionID = lsi->second.first;
703 Value.Addend = lsi->second.second + Addend;
705 // Search for the symbol in the global symbol table
706 SymbolTableMap::const_iterator gsi =
707 GlobalSymbolTable.find(TargetName.data());
708 if (gsi != GlobalSymbolTable.end()) {
709 Value.SectionID = gsi->second.first;
710 Value.Addend = gsi->second.second + Addend;
712 Value.SymbolName = TargetName.data();
713 Value.Addend = Addend;
717 // Addends for external, PC-rel relocations on i386 point back to the zero
718 // offset. Calculate the final offset from the relocation target instead.
719 // This allows us to use the same logic for both external and internal
720 // relocations in resolveI386RelocationRef.
721 if (Arch == Triple::x86 && IsPCRel) {
722 uint64_t RelocAddr = 0;
723 RelI->getAddress(RelocAddr);
724 Value.Addend += RelocAddr + 4;
728 SectionRef Sec = MachO->getRelocationSection(RE);
731 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
732 uint64_t Addr = MachOObj.getOldSectionAddr(Sec);
733 DEBUG(dbgs() << "\nAddr: " << Addr << "\nAddend: " << Addend);
734 Value.Addend = Addend - Addr;
736 Value.Addend += Offset + NumBytes;
739 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
740 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
744 // FIXME: Teach the generic code above not to prematurely conflate
745 // relocation addends and symbol offsets.
746 Value.Addend -= Addend;
747 StubMap::const_iterator i = Stubs.find(Value);
749 if (i != Stubs.end()) {
750 Addr = Section.Address + i->second;
752 Stubs[Value] = Section.StubOffset;
753 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
754 RelocationEntry GOTRE(SectionID, Section.StubOffset,
755 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, false,
757 if (Value.SymbolName)
758 addRelocationForSymbol(GOTRE, Value.SymbolName);
760 addRelocationForSection(GOTRE, Value.SectionID);
761 Section.StubOffset += 8;
764 RelocationEntry TargetRE(SectionID, Offset,
765 MachO::X86_64_RELOC_UNSIGNED, Addend, true,
767 resolveRelocation(TargetRE, (uint64_t)Addr);
768 } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
769 // This is an ARM branch relocation, need to use a stub function.
771 // Look up for existing stub.
772 StubMap::const_iterator i = Stubs.find(Value);
774 if (i != Stubs.end()) {
775 Addr = Section.Address + i->second;
777 // Create a new stub function.
778 Stubs[Value] = Section.StubOffset;
779 uint8_t *StubTargetAddr =
780 createStubFunction(Section.Address + Section.StubOffset);
781 RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address,
782 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
783 if (Value.SymbolName)
784 addRelocationForSymbol(StubRE, Value.SymbolName);
786 addRelocationForSection(StubRE, Value.SectionID);
787 Addr = Section.Address + Section.StubOffset;
788 Section.StubOffset += getMaxStubSize();
790 RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel,
792 resolveRelocation(TargetRE, (uint64_t)Addr);
794 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size);
795 if (Value.SymbolName)
796 addRelocationForSymbol(RE, Value.SymbolName);
798 addRelocationForSection(RE, Value.SectionID);
804 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
805 if (InputBuffer->getBufferSize() < 4)
807 StringRef Magic(InputBuffer->getBufferStart(), 4);
808 if (Magic == "\xFE\xED\xFA\xCE")
810 if (Magic == "\xCE\xFA\xED\xFE")
812 if (Magic == "\xFE\xED\xFA\xCF")
814 if (Magic == "\xCF\xFA\xED\xFE")
819 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
820 return Obj->isMachO();
823 } // end namespace llvm