Index: lib/ReaderWriter/MachO/ArchHandler.h =================================================================== --- lib/ReaderWriter/MachO/ArchHandler.h +++ lib/ReaderWriter/MachO/ArchHandler.h @@ -112,6 +112,10 @@ /// info in final executables. virtual bool isLazyPointer(const Reference &); + /// Reference from an __stub_helper entry to the required offset of the + /// lazy bind commands. + virtual Reference::KindValue lazyImmediateLocationKind() = 0; + /// Returns true if the specified relocation is paired to the next relocation. virtual bool isPairedReloc(const normalized::Relocation &) = 0; Index: lib/ReaderWriter/MachO/ArchHandler_arm.cpp =================================================================== --- lib/ReaderWriter/MachO/ArchHandler_arm.cpp +++ lib/ReaderWriter/MachO/ArchHandler_arm.cpp @@ -67,6 +67,10 @@ return invalid; } + Reference::KindValue lazyImmediateLocationKind() override { + return lazyImmediateLocation; + } + Reference::KindValue pointerKind() override { return invalid; } Index: lib/ReaderWriter/MachO/ArchHandler_arm64.cpp =================================================================== --- lib/ReaderWriter/MachO/ArchHandler_arm64.cpp +++ lib/ReaderWriter/MachO/ArchHandler_arm64.cpp @@ -127,6 +127,10 @@ return pointer64; } + Reference::KindValue lazyImmediateLocationKind() override { + return lazyImmediateLocation; + } + uint32_t dwarfCompactUnwindType() override { return 0x03000000; } Index: lib/ReaderWriter/MachO/ArchHandler_x86.cpp =================================================================== --- lib/ReaderWriter/MachO/ArchHandler_x86.cpp +++ lib/ReaderWriter/MachO/ArchHandler_x86.cpp @@ -70,6 +70,10 @@ return delta32; } + Reference::KindValue lazyImmediateLocationKind() override{ + return lazyImmediateLocation; + } + Reference::KindValue unwindRefToEhFrameKind() override { return invalid; } Index: lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp =================================================================== --- lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp +++ lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp @@ -116,6 +116,10 @@ return unwindFDEToFunction; } + Reference::KindValue lazyImmediateLocationKind() override{ + return lazyImmediateLocation; + } + Reference::KindValue unwindRefToEhFrameKind() override { return unwindInfoToEhFrame; } Index: lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp =================================================================== --- lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp +++ lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp @@ -172,6 +172,7 @@ SymbolScope &symbolScope); void appendSection(SectionInfo *si, NormalizedFile &file); uint32_t sectionIndexForAtom(const Atom *atom); + void fixLazyReferenceImm(const DefinedAtom *atom, uint32_t offset, NormalizedFile &file); typedef llvm::DenseMap AtomToIndex; struct AtomAndIndex { const Atom *atom; uint32_t index; SymbolScope scope; }; @@ -1423,6 +1424,8 @@ uint8_t segmentIndex; uint64_t segmentStartAddr; + uint32_t offsetInBindInfo = 0; + for (SectionInfo *sect : _sectionInfos) { segIndexForSection(sect, segmentIndex, segmentStartAddr); for (const AtomInfo &info : sect->atomsAndOffsets) { @@ -1467,12 +1470,54 @@ bind.symbolName = targ->name(); bind.addend = ref->addend(); nFile.lazyBindingInfo.push_back(bind); + + // Now that we know the segmentOffset and the ordinal attribute, + // we can fix the helper's code + + fixLazyReferenceImm(atom, offsetInBindInfo, nFile); + + // 5 bytes for opcodes + variable sizes (target name + \0 and offset encode's size) + offsetInBindInfo += 6 + targ->name().size() + llvm::getULEB128Size(bind.segOffset); + if (bind.ordinal > BIND_IMMEDIATE_MASK) + offsetInBindInfo += llvm::getULEB128Size(bind.ordinal); } } } } } +void Util::fixLazyReferenceImm(const DefinedAtom *atom, uint32_t offset, NormalizedFile &file) { + for (const auto &ref: *atom) { + const DefinedAtom *da = dyn_cast(ref->target()); + if (da == nullptr) + return; + + auto helperRef = std::find_if(*da->begin(), *da->end(), [&](const Reference &hr) -> bool { + return hr.kindValue() == _archHandler.lazyImmediateLocationKind(); + }); + + if (helperRef == *da->end()) + continue; + + // TODO: maybe get the fixed atom content from _archHandler ? + for (SectionInfo *sectInfo : _sectionInfos) { + for (const AtomInfo &atomInfo : sectInfo->atomsAndOffsets) { + if (atomInfo.atom == helperRef->target()) { + auto sectionContent = file.sections[sectInfo->normalizedSectionIndex].content; + uint8_t *rawb = file.ownedAllocations.Allocate(sectionContent.size()); + llvm::MutableArrayRef newContent{rawb, sectionContent.size()}; + std::copy(sectionContent.begin(), sectionContent.end(), newContent.begin()); + llvm::support::ulittle32_t *loc = + reinterpret_cast(&newContent[atomInfo.offsetInSection + + helperRef->offsetInAtom()]); + *loc = offset; + file.sections[sectInfo->normalizedSectionIndex].content = newContent; + } + } + } + } +} + void Util::addExportInfo(const lld::File &atomFile, NormalizedFile &nFile) { if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) return;