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 static uint32_t readMachOMagic(const char *InputBuffer, unsigned BufferSize) {
126 StringRef Magic(InputBuffer, 4);
127 if (Magic == "\xFE\xED\xFA\xCE" || Magic == "\xCE\xFA\xED\xFE")
129 else if (Magic == "\xFE\xED\xFA\xCF" || Magic == "\xCF\xFA\xED\xFE")
135 ObjectImage *RuntimeDyldMachO::createObjectImage(ObjectBuffer *Buffer) {
136 uint32_t magic = readMachOMagic(Buffer->getBufferStart(),
137 Buffer->getBufferSize());
138 bool is64 = (magic == MachO::MH_MAGIC_64);
139 assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
140 "Unrecognized Macho Magic");
141 return new MachOObjectImage(Buffer, is64);
144 ObjectImage *RuntimeDyldMachO::createObjectImageFromFile(
145 std::unique_ptr<object::ObjectFile> ObjFile) {
149 MemoryBuffer *Buffer =
150 MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
152 uint32_t magic = readMachOMagic(Buffer->getBufferStart(),
153 Buffer->getBufferSize());
154 bool is64 = (magic == MachO::MH_MAGIC_64);
155 assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
156 "Unrecognized Macho Magic");
157 return new MachOObjectImage(std::move(ObjFile), is64);
160 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
161 intptr_t DeltaForEH) {
162 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
163 << ", Delta for EH: " << DeltaForEH << "\n");
164 uint32_t Length = *((uint32_t *)P);
166 unsigned char *Ret = P + Length;
167 uint32_t Offset = *((uint32_t *)P);
168 if (Offset == 0) // is a CIE
172 intptr_t FDELocation = *((intptr_t *)P);
173 intptr_t NewLocation = FDELocation - DeltaForText;
174 *((intptr_t *)P) = NewLocation;
175 P += sizeof(intptr_t);
177 // Skip the FDE address range
178 P += sizeof(intptr_t);
180 uint8_t Augmentationsize = *P;
182 if (Augmentationsize != 0) {
183 intptr_t LSDA = *((intptr_t *)P);
184 intptr_t NewLSDA = LSDA - DeltaForEH;
185 *((intptr_t *)P) = NewLSDA;
191 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
192 intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
193 intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
194 return ObjDistance - MemDistance;
197 void RuntimeDyldMachO::registerEHFrames() {
201 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
202 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
203 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
204 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
206 SectionEntry *Text = &Sections[SectionInfo.TextSID];
207 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
208 SectionEntry *ExceptTab = nullptr;
209 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
210 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
212 intptr_t DeltaForText = computeDelta(Text, EHFrame);
213 intptr_t DeltaForEH = 0;
215 DeltaForEH = computeDelta(ExceptTab, EHFrame);
217 unsigned char *P = EHFrame->Address;
218 unsigned char *End = P + EHFrame->Size;
220 P = processFDE(P, DeltaForText, DeltaForEH);
223 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
226 UnregisteredEHFrameSections.clear();
229 void RuntimeDyldMachO::finalizeLoad(ObjectImage &ObjImg,
230 ObjSectionToIDMap &SectionMap) {
231 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
232 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
233 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
234 ObjSectionToIDMap::iterator i, e;
235 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
236 const SectionRef &Section = i->first;
238 Section.getName(Name);
239 if (Name == "__eh_frame")
240 EHFrameSID = i->second;
241 else if (Name == "__text")
243 else if (Name == "__gcc_except_tab")
244 ExceptTabSID = i->second;
245 else if (Name == "__jump_table")
246 populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
248 else if (Name == "__pointers")
249 populatePointersSection(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
252 UnregisteredEHFrameSections.push_back(
253 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
256 // The target location for the relocation is described by RE.SectionID and
257 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
258 // SectionEntry has three members describing its location.
259 // SectionEntry::Address is the address at which the section has been loaded
260 // into memory in the current (host) process. SectionEntry::LoadAddress is the
261 // address that the section will have in the target process.
262 // SectionEntry::ObjAddress is the address of the bits for this section in the
263 // original emitted object image (also in the current address space).
265 // Relocations will be applied as if the section were loaded at
266 // SectionEntry::LoadAddress, but they will be applied at an address based
267 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
268 // Target memory contents if they are required for value calculations.
270 // The Value parameter here is the load address of the symbol for the
271 // relocation to be applied. For relocations which refer to symbols in the
272 // current object Value will be the LoadAddress of the section in which
273 // the symbol resides (RE.Addend provides additional information about the
274 // symbol location). For external symbols, Value will be the address of the
275 // symbol in the target address space.
276 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
279 const SectionEntry &Section = Sections[RE.SectionID];
280 uint8_t* LocalAddress = Section.Address + RE.Offset;
281 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
283 dbgs() << "resolveRelocation Section: " << RE.SectionID
284 << " LocalAddress: " << format("%p", LocalAddress)
285 << " FinalAddress: " << format("%p", FinalAddress)
286 << " Value: " << format("%p", Value)
287 << " Addend: " << RE.Addend
288 << " isPCRel: " << RE.IsPCRel
289 << " MachoType: " << RE.RelType
290 << " Size: " << (1 << RE.Size) << "\n";
293 // This just dispatches to the proper target specific routine.
296 llvm_unreachable("Unsupported CPU type!");
298 resolveX86_64Relocation(RE, Value);
301 resolveI386Relocation(RE, Value);
303 case Triple::arm: // Fall through.
305 resolveARMRelocation(RE, Value);
307 case Triple::aarch64:
309 resolveAArch64Relocation(RE, Value);
314 bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE,
316 const SectionEntry &Section = Sections[RE.SectionID];
317 uint8_t* LocalAddress = Section.Address + RE.Offset;
320 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
321 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
324 switch (RE.RelType) {
326 llvm_unreachable("Invalid relocation type!");
327 case MachO::GENERIC_RELOC_VANILLA:
328 return applyRelocationValue(LocalAddress, Value + RE.Addend,
330 case MachO::GENERIC_RELOC_SECTDIFF:
331 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
332 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
333 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
334 assert((Value == SectionABase || Value == SectionBBase) &&
335 "Unexpected SECTDIFF relocation value.");
336 Value = SectionABase - SectionBBase + RE.Addend;
337 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
339 case MachO::GENERIC_RELOC_PB_LA_PTR:
340 return Error("Relocation type not implemented yet!");
344 bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE,
346 const SectionEntry &Section = Sections[RE.SectionID];
347 uint8_t* LocalAddress = Section.Address + RE.Offset;
349 // If the relocation is PC-relative, the value to be encoded is the
350 // pointer difference.
352 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
353 // address. Is that expected? Only for branches, perhaps?
354 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
355 Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
358 switch (RE.RelType) {
360 llvm_unreachable("Invalid relocation type!");
361 case MachO::X86_64_RELOC_SIGNED_1:
362 case MachO::X86_64_RELOC_SIGNED_2:
363 case MachO::X86_64_RELOC_SIGNED_4:
364 case MachO::X86_64_RELOC_SIGNED:
365 case MachO::X86_64_RELOC_UNSIGNED:
366 case MachO::X86_64_RELOC_BRANCH:
367 return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
368 case MachO::X86_64_RELOC_GOT_LOAD:
369 case MachO::X86_64_RELOC_GOT:
370 case MachO::X86_64_RELOC_SUBTRACTOR:
371 case MachO::X86_64_RELOC_TLV:
372 return Error("Relocation type not implemented yet!");
376 bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE,
378 const SectionEntry &Section = Sections[RE.SectionID];
379 uint8_t* LocalAddress = Section.Address + RE.Offset;
381 // If the relocation is PC-relative, the value to be encoded is the
382 // pointer difference.
384 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
385 Value -= FinalAddress;
386 // ARM PCRel relocations have an effective-PC offset of two instructions
387 // (four bytes in Thumb mode, 8 bytes in ARM mode).
388 // FIXME: For now, assume ARM mode.
392 switch (RE.RelType) {
394 llvm_unreachable("Invalid relocation type!");
395 case MachO::ARM_RELOC_VANILLA:
396 return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
397 case MachO::ARM_RELOC_BR24: {
398 // Mask the value into the target address. We know instructions are
399 // 32-bit aligned, so we can do it all at once.
400 uint32_t *p = (uint32_t *)LocalAddress;
401 // The low two bits of the value are not encoded.
403 // Mask the value to 24 bits.
404 uint64_t FinalValue = Value & 0xffffff;
405 // Check for overflow.
406 if (Value != FinalValue)
407 return Error("ARM BR24 relocation out of range.");
408 // FIXME: If the destination is a Thumb function (and the instruction
409 // is a non-predicated BL instruction), we need to change it to a BLX
410 // instruction instead.
412 // Insert the value into the instruction.
413 *p = (*p & ~0xffffff) | FinalValue;
416 case MachO::ARM_THUMB_RELOC_BR22:
417 case MachO::ARM_THUMB_32BIT_BRANCH:
418 case MachO::ARM_RELOC_HALF:
419 case MachO::ARM_RELOC_HALF_SECTDIFF:
420 case MachO::ARM_RELOC_PAIR:
421 case MachO::ARM_RELOC_SECTDIFF:
422 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
423 case MachO::ARM_RELOC_PB_LA_PTR:
424 return Error("Relocation type not implemented yet!");
429 bool RuntimeDyldMachO::resolveAArch64Relocation(const RelocationEntry &RE,
431 const SectionEntry &Section = Sections[RE.SectionID];
432 uint8_t* LocalAddress = Section.Address + RE.Offset;
434 switch (RE.RelType) {
436 llvm_unreachable("Invalid relocation type!");
437 case MachO::ARM64_RELOC_UNSIGNED: {
438 assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_UNSIGNED not supported");
439 // Mask in the target value a byte at a time (we don't have an alignment
440 // guarantee for the target address, so this is safest).
442 llvm_unreachable("Invalid size for ARM64_RELOC_UNSIGNED");
444 applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
447 case MachO::ARM64_RELOC_BRANCH26: {
448 assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported");
449 // Mask the value into the target address. We know instructions are
450 // 32-bit aligned, so we can do it all at once.
451 uint32_t *p = (uint32_t*)LocalAddress;
452 // Check if the addend is encoded in the instruction.
453 uint32_t EncodedAddend = *p & 0x03FFFFFF;
454 if (EncodedAddend != 0 ) {
456 llvm_unreachable("branch26 instruction has embedded addend.");
458 llvm_unreachable("branch26 instruction has embedded addend and" \
459 "ARM64_RELOC_ADDEND.");
461 // Check if branch is in range.
462 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
463 uint64_t PCRelVal = Value - FinalAddress + RE.Addend;
464 assert(isInt<26>(PCRelVal) && "Branch target out of range!");
465 // Insert the value into the instruction.
466 *p = (*p & 0xFC000000) | ((uint32_t)(PCRelVal >> 2) & 0x03FFFFFF);
469 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
470 case MachO::ARM64_RELOC_PAGE21: {
471 assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported");
472 // Mask the value into the target address. We know instructions are
473 // 32-bit aligned, so we can do it all at once.
474 uint32_t *p = (uint32_t*)LocalAddress;
475 // Check if the addend is encoded in the instruction.
476 uint32_t EncodedAddend = ((*p & 0x60000000) >> 29) |
477 ((*p & 0x01FFFFE0) >> 3);
478 if (EncodedAddend != 0) {
480 llvm_unreachable("adrp instruction has embedded addend.");
482 llvm_unreachable("adrp instruction has embedded addend and" \
483 "ARM64_RELOC_ADDEND.");
485 // Adjust for PC-relative relocation and offset.
486 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
487 uint64_t PCRelVal = ((Value + RE.Addend) & (-4096)) -
488 (FinalAddress & (-4096));
489 // Check that the value fits into 21 bits (+ 12 lower bits).
490 assert(isInt<33>(PCRelVal) && "Invalid page reloc value!");
491 // Insert the value into the instruction.
492 uint32_t ImmLoValue = (uint32_t)(PCRelVal << 17) & 0x60000000;
493 uint32_t ImmHiValue = (uint32_t)(PCRelVal >> 9) & 0x00FFFFE0;
494 *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue;
497 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
498 case MachO::ARM64_RELOC_PAGEOFF12: {
499 assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_PAGEOFF21 not supported");
500 // Mask the value into the target address. We know instructions are
501 // 32-bit aligned, so we can do it all at once.
502 uint32_t *p = (uint32_t*)LocalAddress;
503 // Check if the addend is encoded in the instruction.
504 uint32_t EncodedAddend = *p & 0x003FFC00;
505 if (EncodedAddend != 0) {
507 llvm_unreachable("adrp instruction has embedded addend.");
509 llvm_unreachable("adrp instruction has embedded addend and" \
510 "ARM64_RELOC_ADDEND.");
512 // Add the offset from the symbol.
514 // Mask out the page address and only use the lower 12 bits.
516 // Check which instruction we are updating to obtain the implicit shift
517 // factor from LDR/STR instructions.
518 if (*p & 0x08000000) {
519 uint32_t ImplicitShift = ((*p >> 30) & 0x3);
520 switch (ImplicitShift) {
522 // Check if this a vector op.
523 if ((*p & 0x04800000) == 0x04800000) {
525 assert(((Value & 0xF) == 0) &&
526 "128-bit LDR/STR not 16-byte aligned.");
530 assert(((Value & 0x1) == 0) && "16-bit LDR/STR not 2-byte aligned.");
532 assert(((Value & 0x3) == 0) && "32-bit LDR/STR not 4-byte aligned.");
534 assert(((Value & 0x7) == 0) && "64-bit LDR/STR not 8-byte aligned.");
536 // Compensate for implicit shift.
537 Value >>= ImplicitShift;
539 // Insert the value into the instruction.
540 *p = (*p & 0xFFC003FF) | ((uint32_t)(Value << 10) & 0x003FFC00);
543 case MachO::ARM64_RELOC_SUBTRACTOR:
544 case MachO::ARM64_RELOC_POINTER_TO_GOT:
545 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
546 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
547 llvm_unreachable("Relocation type not implemented yet!");
548 return Error("Relocation type not implemented yet!");
549 case MachO::ARM64_RELOC_ADDEND:
550 llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by " \
551 "processRelocationRef!");
556 void RuntimeDyldMachO::populateJumpTable(MachOObjectFile &Obj,
557 const SectionRef &JTSection,
558 unsigned JTSectionID) {
559 assert(!Obj.is64Bit() &&
560 "__jump_table section not supported in 64-bit MachO.");
562 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
563 MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
564 uint32_t JTSectionSize = Sec32.size;
565 unsigned FirstIndirectSymbol = Sec32.reserved1;
566 unsigned JTEntrySize = Sec32.reserved2;
567 unsigned NumJTEntries = JTSectionSize / JTEntrySize;
568 uint8_t* JTSectionAddr = getSectionAddress(JTSectionID);
569 unsigned JTEntryOffset = 0;
571 assert((JTSectionSize % JTEntrySize) == 0 &&
572 "Jump-table section does not contain a whole number of stubs?");
574 for (unsigned i = 0; i < NumJTEntries; ++i) {
575 unsigned SymbolIndex =
576 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
577 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
578 StringRef IndirectSymbolName;
579 SI->getName(IndirectSymbolName);
580 uint8_t* JTEntryAddr = JTSectionAddr + JTEntryOffset;
581 createStubFunction(JTEntryAddr);
582 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
583 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
584 addRelocationForSymbol(RE, IndirectSymbolName);
585 JTEntryOffset += JTEntrySize;
589 void RuntimeDyldMachO::populatePointersSection(MachOObjectFile &Obj,
590 const SectionRef &PTSection,
591 unsigned PTSectionID) {
592 assert(!Obj.is64Bit() &&
593 "__pointers section not supported in 64-bit MachO.");
595 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
596 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
597 uint32_t PTSectionSize = Sec32.size;
598 unsigned FirstIndirectSymbol = Sec32.reserved1;
599 const unsigned PTEntrySize = 4;
600 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
601 unsigned PTEntryOffset = 0;
603 assert((PTSectionSize % PTEntrySize) == 0 &&
604 "Pointers section does not contain a whole number of stubs?");
606 DEBUG(dbgs() << "Populating __pointers, Section ID " << PTSectionID
607 << ", " << NumPTEntries << " entries, "
608 << PTEntrySize << " bytes each:\n");
610 for (unsigned i = 0; i < NumPTEntries; ++i) {
611 unsigned SymbolIndex =
612 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
613 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
614 StringRef IndirectSymbolName;
615 SI->getName(IndirectSymbolName);
616 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
617 << ", PT offset: " << PTEntryOffset << "\n");
618 RelocationEntry RE(PTSectionID, PTEntryOffset,
619 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
620 addRelocationForSymbol(RE, IndirectSymbolName);
621 PTEntryOffset += PTEntrySize;
626 section_iterator getSectionByAddress(const MachOObjectFile &Obj,
628 section_iterator SI = Obj.section_begin();
629 section_iterator SE = Obj.section_end();
631 for (; SI != SE; ++SI) {
632 uint64_t SAddr, SSize;
633 SI->getAddress(SAddr);
635 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
642 relocation_iterator RuntimeDyldMachO::processSECTDIFFRelocation(
644 relocation_iterator RelI,
646 ObjSectionToIDMap &ObjSectionToID) {
647 const MachOObjectFile *MachO =
648 static_cast<const MachOObjectFile*>(Obj.getObjectFile());
649 MachO::any_relocation_info RE =
650 MachO->getRelocation(RelI->getRawDataRefImpl());
652 SectionEntry &Section = Sections[SectionID];
653 uint32_t RelocType = MachO->getAnyRelocationType(RE);
654 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
655 unsigned Size = MachO->getAnyRelocationLength(RE);
657 RelI->getOffset(Offset);
658 uint8_t *LocalAddress = Section.Address + Offset;
659 unsigned NumBytes = 1 << Size;
661 memcpy(&Addend, LocalAddress, NumBytes);
664 MachO::any_relocation_info RE2 =
665 MachO->getRelocation(RelI->getRawDataRefImpl());
667 uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
668 section_iterator SAI = getSectionByAddress(*MachO, AddrA);
669 assert(SAI != MachO->section_end() && "Can't find section for address A");
670 uint64_t SectionABase;
671 SAI->getAddress(SectionABase);
672 uint64_t SectionAOffset = AddrA - SectionABase;
673 SectionRef SectionA = *SAI;
675 SectionA.isText(IsCode);
676 uint32_t SectionAID = findOrEmitSection(Obj, SectionA, IsCode,
679 uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
680 section_iterator SBI = getSectionByAddress(*MachO, AddrB);
681 assert(SBI != MachO->section_end() && "Can't find section for address B");
682 uint64_t SectionBBase;
683 SBI->getAddress(SectionBBase);
684 uint64_t SectionBOffset = AddrB - SectionBBase;
685 SectionRef SectionB = *SBI;
686 uint32_t SectionBID = findOrEmitSection(Obj, SectionB, IsCode,
689 if (Addend != AddrA - AddrB)
690 Error("Unexpected SECTDIFF relocation addend.");
692 DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
693 << ", Addend: " << Addend << ", SectionA ID: "
694 << SectionAID << ", SectionAOffset: " << SectionAOffset
695 << ", SectionB ID: " << SectionBID << ", SectionBOffset: "
696 << SectionBOffset << "\n");
697 RelocationEntry R(SectionID, Offset, RelocType, 0,
698 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
701 addRelocationForSection(R, SectionAID);
702 addRelocationForSection(R, SectionBID);
707 relocation_iterator RuntimeDyldMachO::processI386ScatteredVANILLA(
709 relocation_iterator RelI,
711 ObjSectionToIDMap &ObjSectionToID) {
712 const MachOObjectFile *MachO =
713 static_cast<const MachOObjectFile*>(Obj.getObjectFile());
714 MachO::any_relocation_info RE =
715 MachO->getRelocation(RelI->getRawDataRefImpl());
717 SectionEntry &Section = Sections[SectionID];
718 uint32_t RelocType = MachO->getAnyRelocationType(RE);
719 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
720 unsigned Size = MachO->getAnyRelocationLength(RE);
722 RelI->getOffset(Offset);
723 uint8_t *LocalAddress = Section.Address + Offset;
724 unsigned NumBytes = 1 << Size;
726 memcpy(&Addend, LocalAddress, NumBytes);
728 unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
729 section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
730 assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
731 uint64_t SectionBaseAddr;
732 TargetSI->getAddress(SectionBaseAddr);
733 SectionRef TargetSection = *TargetSI;
735 TargetSection.isText(IsCode);
736 uint32_t TargetSectionID = findOrEmitSection(Obj, TargetSection, IsCode,
739 Addend -= SectionBaseAddr;
740 RelocationEntry R(SectionID, Offset, RelocType, Addend,
743 addRelocationForSection(R, TargetSectionID);
748 relocation_iterator RuntimeDyldMachO::processRelocationRef(
749 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
750 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
752 const ObjectFile *OF = Obj.getObjectFile();
753 const MachOObjectImage &MachOObj = *static_cast<MachOObjectImage *>(&Obj);
754 const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF);
755 MachO::any_relocation_info RE =
756 MachO->getRelocation(RelI->getRawDataRefImpl());
757 int64_t RelocAddendValue = 0;
758 bool HasRelocAddendValue = false;
760 uint32_t RelType = MachO->getAnyRelocationType(RE);
761 if (Arch == Triple::arm64) {
762 // ARM64_RELOC_ADDEND provides the offset (addend) that will be used by the
763 // next relocation entry. Save the value and advance to the next relocation
765 if (RelType == MachO::ARM64_RELOC_ADDEND) {
766 assert(!MachO->getPlainRelocationExternal(RE));
767 assert(!MachO->getAnyRelocationPCRel(RE));
768 assert(MachO->getAnyRelocationLength(RE) == 2);
769 uint64_t RawAddend = MachO->getPlainRelocationSymbolNum(RE);
770 // Sign-extend the 24-bit to 64-bit.
771 RelocAddendValue = RawAddend << 40;
772 RelocAddendValue >>= 40;
773 HasRelocAddendValue = true;
775 // Get the next entry.
776 RE = MachO->getRelocation((++RelI)->getRawDataRefImpl());
777 RelType = MachO->getAnyRelocationType(RE);
778 assert(RelType == MachO::ARM64_RELOC_BRANCH26 ||
779 RelType == MachO::ARM64_RELOC_PAGE21 ||
780 RelType == MachO::ARM64_RELOC_PAGEOFF12);
782 } else if (RelType == MachO::ARM64_RELOC_BRANCH26 ||
783 RelType == MachO::ARM64_RELOC_PAGE21 ||
784 RelType == MachO::ARM64_RELOC_PAGEOFF12 ||
785 RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 ||
786 RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12) {
787 RelocAddendValue = 0;
788 HasRelocAddendValue = true;
792 // FIXME: Properly handle scattered relocations.
793 // Special case the couple of scattered relocations that we know how
794 // to handle: SECTDIFF relocations, and scattered VANILLA relocations
796 // For all other scattered relocations, just bail out and hope for the
797 // best, since the offsets computed by scattered relocations have often
798 // been optimisticaly filled in by the compiler. This will fail
799 // horribly where the relocations *do* need to be applied, but that was
801 if (MachO->isRelocationScattered(RE)) {
802 if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
803 RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
804 return processSECTDIFFRelocation(SectionID, RelI, Obj, ObjSectionToID);
805 else if (Arch == Triple::x86 && RelType == MachO::GENERIC_RELOC_VANILLA)
806 return processI386ScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
811 RelocationValueRef Value;
812 SectionEntry &Section = Sections[SectionID];
814 bool IsExtern = MachO->getPlainRelocationExternal(RE);
815 bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
816 unsigned Size = MachO->getAnyRelocationLength(RE);
818 RelI->getOffset(Offset);
819 uint8_t *LocalAddress = Section.Address + Offset;
820 unsigned NumBytes = 1 << Size;
822 if (HasRelocAddendValue)
823 Addend = RelocAddendValue;
825 memcpy(&Addend, LocalAddress, NumBytes);
828 // Obtain the symbol name which is referenced in the relocation
829 symbol_iterator Symbol = RelI->getSymbol();
830 StringRef TargetName;
831 Symbol->getName(TargetName);
832 // First search for the symbol in the local symbol table
833 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
834 if (lsi != Symbols.end()) {
835 Value.SectionID = lsi->second.first;
836 Value.Addend = lsi->second.second + Addend;
838 // Search for the symbol in the global symbol table
839 SymbolTableMap::const_iterator gsi =
840 GlobalSymbolTable.find(TargetName.data());
841 if (gsi != GlobalSymbolTable.end()) {
842 Value.SectionID = gsi->second.first;
843 Value.Addend = gsi->second.second + Addend;
845 Value.SymbolName = TargetName.data();
846 Value.Addend = Addend;
850 // Addends for external, PC-rel relocations on i386 point back to the zero
851 // offset. Calculate the final offset from the relocation target instead.
852 // This allows us to use the same logic for both external and internal
853 // relocations in resolveI386RelocationRef.
854 if (Arch == Triple::x86 && IsPCRel) {
855 uint64_t RelocAddr = 0;
856 RelI->getAddress(RelocAddr);
857 Value.Addend += RelocAddr + 4;
861 SectionRef Sec = MachO->getRelocationSection(RE);
864 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
865 uint64_t Addr = MachOObj.getOldSectionAddr(Sec);
866 DEBUG(dbgs() << "\nAddr: " << Addr << "\nAddend: " << Addend);
867 Value.Addend = Addend - Addr;
869 Value.Addend += Offset + NumBytes;
872 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
873 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
877 // FIXME: Teach the generic code above not to prematurely conflate
878 // relocation addends and symbol offsets.
879 Value.Addend -= Addend;
880 StubMap::const_iterator i = Stubs.find(Value);
882 if (i != Stubs.end()) {
883 Addr = Section.Address + i->second;
885 Stubs[Value] = Section.StubOffset;
886 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
887 RelocationEntry GOTRE(SectionID, Section.StubOffset,
888 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, false,
890 if (Value.SymbolName)
891 addRelocationForSymbol(GOTRE, Value.SymbolName);
893 addRelocationForSection(GOTRE, Value.SectionID);
894 Section.StubOffset += 8;
897 RelocationEntry TargetRE(SectionID, Offset,
898 MachO::X86_64_RELOC_UNSIGNED, Addend, true,
900 resolveRelocation(TargetRE, (uint64_t)Addr);
901 } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
902 // This is an ARM branch relocation, need to use a stub function.
904 // Look up for existing stub.
905 StubMap::const_iterator i = Stubs.find(Value);
907 if (i != Stubs.end()) {
908 Addr = Section.Address + i->second;
910 // Create a new stub function.
911 Stubs[Value] = Section.StubOffset;
912 uint8_t *StubTargetAddr =
913 createStubFunction(Section.Address + Section.StubOffset);
914 RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address,
915 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
916 if (Value.SymbolName)
917 addRelocationForSymbol(StubRE, Value.SymbolName);
919 addRelocationForSection(StubRE, Value.SectionID);
920 Addr = Section.Address + Section.StubOffset;
921 Section.StubOffset += getMaxStubSize();
923 RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel,
925 resolveRelocation(TargetRE, (uint64_t)Addr);
926 } else if (Arch == Triple::arm64 &&
927 (RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 ||
928 RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12)) {
930 StubMap::const_iterator i = Stubs.find(Value);
932 if (i != Stubs.end())
933 Addr = Section.Address + i->second;
935 // FIXME: There must be a better way to do this then to check and fix the
936 // alignment every time!!!
937 uintptr_t BaseAddress = uintptr_t(Section.Address);
938 uintptr_t StubAlignment = getStubAlignment();
939 uintptr_t StubAddress
940 = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
942 unsigned StubOffset = StubAddress - BaseAddress;
943 Stubs[Value] = StubOffset;
944 assert(((StubAddress % getStubAlignment()) == 0) &&
945 "GOT entry not aligned");
946 RelocationEntry GOTRE(SectionID, StubOffset, MachO::ARM64_RELOC_UNSIGNED,
947 Value.Addend, /*IsPCRel=*/false, /*Size=*/3);
948 if (Value.SymbolName)
949 addRelocationForSymbol(GOTRE, Value.SymbolName);
951 addRelocationForSection(GOTRE, Value.SectionID);
952 Section.StubOffset = StubOffset + getMaxStubSize();
954 Addr = (uint8_t *)StubAddress;
956 RelocationEntry TargetRE(SectionID, Offset, RelType, /*Addend=*/0, IsPCRel,
958 resolveRelocation(TargetRE, (uint64_t)Addr);
961 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size);
962 if (Value.SymbolName)
963 addRelocationForSymbol(RE, Value.SymbolName);
965 addRelocationForSection(RE, Value.SectionID);
971 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
972 uint32_t Magic = readMachOMagic(InputBuffer->getBufferStart(),
973 InputBuffer->getBufferSize());
974 return (Magic == 0xFEEDFACE || Magic == 0xFEEDFACF);
977 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
978 return Obj->isMachO();
981 } // end namespace llvm