From: Mark Kettenis Subject: Re: lld/mips64: lazy bindings To: Kirill A. Korinsky Cc: rober@openbsd.org, tech@openbsd.org Date: Tue, 10 Nov 2026 01:45:11 +0100 > Date: Mon, 09 Nov 2026 11:42:05 +0100 > From: Kirill A. Korinsky > > robert@, > > here a diff which implemented a lazy bidning for mips64. > > I had tried to upstream it and upstream rejected it because, let me quote: > > > That is a lot of MIPS-specific machinery for a port with little active > > maintenance in lld, and it changes the output for every MIPS OS, not just > > OpenBSD (hence the updates to existing tests). > > Instead they suggested this hack: > https://gist.github.com/MaskRay/fd946027ac2134ad697c31128c92812c > > I think that lld fix is cleaner, so, shall we go it in and support as part > of our not that small diff? If upstream rejects this, I don't think we should do this. > Index: gnu/llvm/lld/ELF/Config.h > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/Config.h,v > retrieving revision 1.7 > diff -u -p -r1.7 Config.h > --- gnu/llvm/lld/ELF/Config.h 29 May 2026 11:06:20 -0000 1.7 > +++ gnu/llvm/lld/ELF/Config.h 9 Nov 2026 10:34:21 -0000 > @@ -64,6 +64,7 @@ class IgotPltSection; > class InputSection; > class IpltSection; > class MipsGotSection; > +class MipsStubsSection; > class MipsRldMapSection; > class PPC32Got2Section; > class PPC64LongBranchTargetSection; > @@ -592,6 +593,7 @@ struct InStruct { > std::unique_ptr mipsOptions; > std::unique_ptr mipsReginfo; > std::unique_ptr mipsRldMap; > + std::unique_ptr mipsStubs; > std::unique_ptr partEnd; > std::unique_ptr partIndex; > std::unique_ptr plt; > Index: gnu/llvm/lld/ELF/Relocations.cpp > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/Relocations.cpp,v > retrieving revision 1.12 > diff -u -p -r1.12 Relocations.cpp > --- gnu/llvm/lld/ELF/Relocations.cpp 30 Jul 2026 19:28:37 -0000 1.12 > +++ gnu/llvm/lld/ELF/Relocations.cpp 9 Nov 2026 10:34:22 -0000 > @@ -1442,6 +1442,8 @@ void RelocScan::scanEhSection(EhInputSec > if (sym->isUndefined() && > maybeReportUndefined(cast(*sym), offset)) > continue; > + if (ctx.in.mipsStubs) > + ctx.in.mipsStubs->exclude(*sym); > process(r.expr, r.type, offset, *sym, r.addend); > } > } > Index: gnu/llvm/lld/ELF/SyntheticSections.cpp > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/SyntheticSections.cpp,v > retrieving revision 1.11 > diff -u -p -r1.11 SyntheticSections.cpp > --- gnu/llvm/lld/ELF/SyntheticSections.cpp 30 Jul 2026 19:28:37 -0000 1.11 > +++ gnu/llvm/lld/ELF/SyntheticSections.cpp 9 Nov 2026 10:34:22 -0000 > @@ -1098,6 +1098,10 @@ void MipsGotSection::build() { > // Dynamic relocations for "global" entries. > for (const std::pair &p : got.global) { > uint64_t offset = p.second * ctx.arg.wordsize; > + // A lazy stub requires the primary GOT's resolver slots and $gp. > + // Calls through secondary GOTs must be resolved eagerly. > + if (ctx.in.mipsStubs) > + ctx.in.mipsStubs->exclude(*p.first); > ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->relativeRel, *this, > offset, *p.first); > } > @@ -1200,6 +1204,64 @@ void MipsGotSection::writeTo(uint8_t *bu > } > } > } > + > + if (!ctx.in.mipsStubs || gots.empty()) > + return; > + auto writeStubAddresses = [&](const auto &entries) { > + for (const auto &[sym, index] : entries) > + if (std::optional va = ctx.in.mipsStubs->getStubVA(*sym)) > + writeUint(ctx, buf + index * ctx.arg.wordsize, *va); > + }; > + writeStubAddresses(gots.front().global); > + writeStubAddresses(gots.front().relocs); > +} > + > +MipsStubsSection::MipsStubsSection(Ctx &ctx) > + : SyntheticSection(ctx, ".MIPS.stubs", SHT_PROGBITS, > + SHF_ALLOC | SHF_EXECINSTR, ctx.arg.wordsize) {} > + > +void MipsStubsSection::addSymbols() { > + // microMIPS stubs are not implemented. > + if (isMicroMips(ctx)) > + return; > + for (Symbol *sym : ctx.symtab->getSymbols()) { > + if (!sym->isPreemptible || !(sym->isUndefined() || sym->isShared()) || > + !sym->isFunc() || !callOnly.lookup(sym) || sym->hasFlag(NEEDS_PLT)) > + continue; > + entries.push_back(sym); > + } > + > + // Include the full stub size in GOT page counts before GOT layout. > + // Removing secondary GOT candidates later can only shrink this size. > + stubSize = ctx.mainPart->dynSymTab->getNumSymbols() > 0x10000 > + ? stubBigSize > + : stubNormalSize; > +} > + > +std::optional MipsStubsSection::getStubVA(const Symbol &sym) const { > + // The resolver requires a canonical $gp, which custom layouts can change. > + if (!getParent() || > + ctx.in.mipsGot->getGp() != ctx.in.mipsGot->getVA() + 0x7ff0) > + return std::nullopt; > + auto it = indices.find(&sym); > + if (it == indices.end()) > + return std::nullopt; > + return getVA() + it->second * stubSize; > +} > + > +void MipsStubsSection::finalizeContents() { > + llvm::erase_if(entries, [&](const Symbol *sym) { > + return !callOnly.lookup(sym) || !sym->isInGot(ctx); > + }); > + for (size_t i = 0; i != entries.size(); ++i) > + indices[entries[i]] = i; > +} > + > +void MipsStubsSection::writeTo(uint8_t *buf) { > + for (const Symbol *sym : entries) { > + writeMipsStub(ctx, buf, sym->dynsymIndex, stubSize == stubBigSize); > + buf += stubSize; > + } > } > > // On PowerPC the .plt section is used to hold the table of function addresses > @@ -2351,6 +2413,9 @@ template void SymbolTableSe > Symbol *sym = ent.sym; > if (sym->isInPlt(ctx) && sym->hasFlag(NEEDS_COPY)) > eSym->st_other |= STO_MIPS_PLT; > + if (type == SHT_DYNSYM && ctx.in.mipsStubs) > + if (std::optional va = ctx.in.mipsStubs->getStubVA(*sym)) > + eSym->st_value = *va; > if (isMicroMips(ctx)) { > // We already set the less-significant bit for symbols > // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT > @@ -4926,6 +4991,11 @@ template void elf::createSy > add(*ctx.in.plt); > ctx.in.iplt = std::make_unique(ctx); > add(*ctx.in.iplt); > + > + if (ctx.arg.emachine == EM_MIPS && !ctx.arg.relocatable && ctx.hasDynsym) { > + ctx.in.mipsStubs = std::make_unique(ctx); > + add(*ctx.in.mipsStubs); > + } > > if (ctx.arg.andFeatures || ctx.aarch64PauthAbiCoreInfo) { > ctx.in.gnuProperty = std::make_unique(ctx); > Index: gnu/llvm/lld/ELF/SyntheticSections.h > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/SyntheticSections.h,v > retrieving revision 1.6 > diff -u -p -r1.6 SyntheticSections.h > --- gnu/llvm/lld/ELF/SyntheticSections.h 30 Jul 2026 19:28:37 -0000 1.6 > +++ gnu/llvm/lld/ELF/SyntheticSections.h 9 Nov 2026 10:34:22 -0000 > @@ -368,6 +368,29 @@ private: > bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary); > }; > > +class MipsStubsSection final : public SyntheticSection { > + static constexpr size_t stubNormalSize = 16; > + static constexpr size_t stubBigSize = 20; > + > +public: > + MipsStubsSection(Ctx &); > + void addSymbols(); > + void recordCall(const Symbol &sym) { callOnly.try_emplace(&sym, true); } > + void exclude(const Symbol &sym) { callOnly[&sym] = false; } > + std::optional getStubVA(const Symbol &sym) const; > + size_t getSize() const override { return entries.size() * stubSize; } > + bool isNeeded() const override { return !entries.empty(); } > + void finalizeContents() override; > + void writeTo(uint8_t *buf) override; > + > +private: > + SmallVector entries; > + llvm::DenseMap indices; > + // False entries record exclusions that later calls must not override. > + llvm::DenseMap callOnly; > + size_t stubSize = stubNormalSize; > +}; > + > class GotPltSection final : public SyntheticSection { > public: > GotPltSection(Ctx &); > Index: gnu/llvm/lld/ELF/Target.h > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/Target.h,v > retrieving revision 1.2 > diff -u -p -r1.2 Target.h > --- gnu/llvm/lld/ELF/Target.h 30 Jul 2026 19:28:37 -0000 1.2 > +++ gnu/llvm/lld/ELF/Target.h 9 Nov 2026 10:34:22 -0000 > @@ -247,6 +247,7 @@ uint64_t getMipsPageAddr(uint64_t addr); > bool isMipsN32Abi(Ctx &, const InputFile &f); > bool isMicroMips(Ctx &); > bool isMipsR6(Ctx &); > +void writeMipsStub(Ctx &, uint8_t *buf, uint32_t dynsymIndex, bool big); > > void writePPC32GlinkSection(Ctx &, uint8_t *buf, size_t numEntries); > > Index: gnu/llvm/lld/ELF/Writer.cpp > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/Writer.cpp,v > retrieving revision 1.12 > diff -u -p -r1.12 Writer.cpp > --- gnu/llvm/lld/ELF/Writer.cpp 20 Aug 2026 14:39:18 -0000 1.12 > +++ gnu/llvm/lld/ELF/Writer.cpp 9 Nov 2026 10:34:22 -0000 > @@ -2016,8 +2016,11 @@ template void Writer: > } > } > > + if (ctx.in.mipsStubs) > + ctx.in.mipsStubs->addSymbols(); > if (ctx.in.mipsGot) > ctx.in.mipsGot->build(); > + finalizeSynthetic(ctx, ctx.in.mipsStubs.get()); > > removeUnusedSyntheticSections(ctx); > ctx.script->diagnoseOrphanHandling(); > Index: gnu/llvm/lld/ELF/Arch/Mips.cpp > =================================================================== > RCS file: /cvs/src/gnu/llvm/lld/ELF/Arch/Mips.cpp,v > retrieving revision 1.1.1.6 > diff -u -p -r1.1.1.6 Mips.cpp > --- gnu/llvm/lld/ELF/Arch/Mips.cpp 29 May 2026 11:05:55 -0000 1.1.1.6 > +++ gnu/llvm/lld/ELF/Arch/Mips.cpp 9 Nov 2026 10:34:22 -0000 > @@ -48,6 +48,41 @@ uint64_t elf::getMipsPageAddr(uint64_t a > return (addr + 0x8000) & ~0xffff; > } > > +void elf::writeMipsStub(Ctx &ctx, uint8_t *buf, uint32_t dynsymIndex, > + bool big) { > + if (dynsymIndex > 0x7fffffff) { > + ErrAlways(ctx) << "MIPS lazy-binding stub dynamic symbol index " > + << dynsymIndex << " exceeds 31 bits"; > + return; > + } > + > + const uint32_t load = ctx.arg.is64 ? 0xdf998010 // ld $25, -0x7ff0($28) > + : 0x8f998010; // lw $25, -0x7ff0($28) > + const uint32_t lui = 0x3c180000 | ((dynsymIndex >> 16) & 0x7fff); > + const uint32_t jalr = ctx.arg.zHazardplt ? 0x0320fc09 : 0x0320f809; > + uint32_t loadIndex; > + if (big) > + loadIndex = 0x37180000 | (dynsymIndex & 0xffff); // ori $24, $24, idx > + else if (dynsymIndex & ~0x7fff) > + loadIndex = 0x34180000 | (dynsymIndex & 0xffff); // ori $24, $0, idx > + else > + loadIndex = (ctx.arg.is64 ? 0x64180000 : 0x24180000) | // [d]addiu $24, > + dynsymIndex; // $0, idx > + > + if (big) { > + write32(ctx, buf, load); > + write32(ctx, buf + 4, 0x03e07825); // move $15, $31 > + write32(ctx, buf + 8, lui); > + write32(ctx, buf + 12, jalr); // jalr[.hb] $25 > + write32(ctx, buf + 16, loadIndex); // delay slot > + } else { > + write32(ctx, buf, load); > + write32(ctx, buf + 4, 0x03e07825); // move $15, $31 > + write32(ctx, buf + 8, jalr); // jalr[.hb] $25 > + write32(ctx, buf + 12, loadIndex); // delay slot > + } > +} > + > template MIPS::MIPS(Ctx &ctx) : TargetInfo(ctx) { > gotPltHeaderEntriesNum = 2; > defaultMaxPageSize = 65536; > @@ -609,6 +644,12 @@ static RelType getMipsPairType(RelType t > } > } > > +static bool isCallReloc(RelType type) { > + return type == R_MIPS_CALL16 || type == R_MIPS_CALL_HI16 || > + type == R_MIPS_CALL_LO16 || type == R_MICROMIPS_CALL16 || > + type == R_MICROMIPS_CALL_HI16 || type == R_MICROMIPS_CALL_LO16; > +} > + > template > template > void MIPS::scanSectionImpl(InputSectionBase &sec, Relocs rels) { > @@ -639,6 +680,21 @@ void MIPS::scanSectionImpl(InputSe > if (sym.isUndefined() && symIdx != 0 && > rs.maybeReportUndefined(cast(sym), offset)) > continue; > + > + // In a shared object, turn untyped undefined symbols with a call > + // relocation into function symbols so that the dynamic linker handles > + // them correctly. > + if (ctx.arg.shared && sym.isUndefined() && symIdx != 0 && > + sym.type == STT_NOTYPE && isCallReloc(type.v & 0xff)) > + sym.type = STT_FUNC; > + > + if (symIdx != 0 && ctx.in.mipsStubs) { > + uint32_t baseType = type.v & 0xff; > + if (isCallReloc(baseType)) > + ctx.in.mipsStubs->recordCall(sym); > + else if (baseType != R_MIPS_JALR && baseType != R_MICROMIPS_JALR) > + ctx.in.mipsStubs->exclude(sym); > + } > > auto addend = rs.getAddend(rel, type); > if (expr == RE_MIPS_GOTREL && sym.isLocal()) { > > > -- > wbr, Kirill > >