Index: lld/MachO/SyntheticSections.h =================================================================== --- lld/MachO/SyntheticSections.h +++ lld/MachO/SyntheticSections.h @@ -281,7 +281,7 @@ const llvm::SetVector &getEntries() const { return entries; } // Returns whether the symbol was added. Note that every stubs entry will // have a corresponding entry in the LazyPointerSection. - bool addEntry(Symbol *); + void addEntry(Symbol *); uint64_t getVA(uint32_t stubsIndex) const { assert(isFinal || target->usesThunks()); // ConcatOutputSection::finalize() can seek the address of a Index: lld/MachO/SyntheticSections.cpp =================================================================== --- lld/MachO/SyntheticSections.cpp +++ lld/MachO/SyntheticSections.cpp @@ -659,11 +659,36 @@ void StubsSection::finalize() { isFinal = true; } -bool StubsSection::addEntry(Symbol *sym) { +static void addBindingsForStub(Symbol *sym) { + if (auto *dysym = dyn_cast(sym)) { + if (sym->isWeakDef()) { + in.binding->addEntry(dysym, in.lazyPointers->isec, + sym->stubsIndex * target->wordSize); + in.weakBinding->addEntry(sym, in.lazyPointers->isec, + sym->stubsIndex * target->wordSize); + } else { + in.lazyBinding->addEntry(dysym); + } + } else if (auto *defined = dyn_cast(sym)) { + if (defined->isExternalWeakDef()) { + in.rebase->addEntry(in.lazyPointers->isec, + sym->stubsIndex * target->wordSize); + in.weakBinding->addEntry(sym, in.lazyPointers->isec, + sym->stubsIndex * target->wordSize); + } else if (defined->interposable) { + in.lazyBinding->addEntry(sym); + } + } else { + llvm_unreachable("invalid stub target symbol type"); + } +} + +void StubsSection::addEntry(Symbol *sym) { bool inserted = entries.insert(sym); - if (inserted) + if (inserted) { sym->stubsIndex = entries.size() - 1; - return inserted; + addBindingsForStub(sym); + } } StubHelperSection::StubHelperSection() Index: lld/MachO/Writer.cpp =================================================================== --- lld/MachO/Writer.cpp +++ lld/MachO/Writer.cpp @@ -575,32 +575,13 @@ } } -// Add stubs and bindings where necessary (e.g. if the symbol is a -// DylibSymbol.) +// Add stubs where necessary (e.g. if the symbol is a DylibSymbol.) static void prepareBranchTarget(Symbol *sym) { if (auto *dysym = dyn_cast(sym)) { - if (in.stubs->addEntry(dysym)) { - if (sym->isWeakDef()) { - in.binding->addEntry(dysym, in.lazyPointers->isec, - sym->stubsIndex * target->wordSize); - in.weakBinding->addEntry(sym, in.lazyPointers->isec, - sym->stubsIndex * target->wordSize); - } else { - in.lazyBinding->addEntry(dysym); - } - } + in.stubs->addEntry(dysym); } else if (auto *defined = dyn_cast(sym)) { - if (defined->isExternalWeakDef()) { - if (in.stubs->addEntry(sym)) { - in.rebase->addEntry(in.lazyPointers->isec, - sym->stubsIndex * target->wordSize); - in.weakBinding->addEntry(sym, in.lazyPointers->isec, - sym->stubsIndex * target->wordSize); - } - } else if (defined->interposable) { - if (in.stubs->addEntry(sym)) - in.lazyBinding->addEntry(sym); - } + if (defined->isExternalWeakDef() || defined->interposable) + in.stubs->addEntry(defined); } else { llvm_unreachable("invalid branch target symbol type"); }