Index: lld/ELF/SyntheticSections.cpp
--- lld/ELF/SyntheticSections.cpp.orig
+++ lld/ELF/SyntheticSections.cpp
@@ -934,9 +934,16 @@ void MipsGotSection::build() {
       uint64_t offset = p.second * ctx.arg.wordsize;
       if (&got == primGot)
         addConstant({R_ABS, ctx.target->relativeRel, offset, 0, p.first});
-      else
+      else {
+#if defined(__OpenBSD__)
+        // 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);
+#endif
         ctx.in.relaDyn->addSymbolReloc(ctx.target->relativeRel, *this, offset,
                                        *p.first);
+      }
     }
     // Relocation-only entries exist as dummy entries for dynamic symbols that
     // aren't otherwise in the primary GOT, as the ABI requires an entry for
@@ -1013,8 +1020,69 @@ void MipsGotSection::writeTo(uint8_t *buf) {
   writeUint(ctx, buf + ctx.arg.wordsize,
             (uint64_t)1 << (ctx.arg.wordsize * 8 - 1));
   ctx.target->relocateAlloc(*this, buf);
+
+#if defined(__OpenBSD__)
+  if (ctx.in.mipsStubs && !gots.empty()) {
+    auto writeStubAddresses = [&](const auto &entries) {
+      for (const auto &[sym, index] : entries)
+        if (std::optional<uint64_t> va = ctx.in.mipsStubs->getStubVA(*sym))
+          writeUint(ctx, buf + index * ctx.arg.wordsize, *va);
+    };
+    writeStubAddresses(gots.front().global);
+    writeStubAddresses(gots.front().relocs);
+  }
+#endif
 }
 
+#if defined(__OpenBSD__)
+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);
+  }
+
+  // The dynsym is complete. Later steps can only shrink it.
+  // Choose stub size that can encode the dynsym indices.
+  stubSize = ctx.in.dynSymTab->getNumSymbols() > 0x10000 ? stubBigSize
+                                                         : stubNormalSize;
+}
+
+std::optional<uint64_t> MipsStubsSection::getStubVA(const Symbol &sym) const {
+  // The stub's fixed GOT[0] load requires _gp == .got + 0x7ff0.
+  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;
+  }
+}
+#endif
+
 // On PowerPC the .plt section is used to hold the table of function addresses
 // instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
 // section. I don't know why we have a BSS style type for the section but it is
@@ -1310,6 +1378,7 @@ DynamicSection<ELFT>::computeContents() {
         addInt(DT_RISCV_VARIANT_CC, 0);
       [[fallthrough]];
     default:
+      assert(ctx.target->usesGotPlt);
       addInSec(DT_PLTGOT, *ctx.in.gotPlt);
       break;
     }
@@ -1521,14 +1590,20 @@ void RelocationBaseSection::finalizeContents() {
   else
     getParent()->link = 0;
 
-  if (ctx.in.relaPlt.get() == this && ctx.in.gotPlt->getParent()) {
-    getParent()->flags |= ELF::SHF_INFO_LINK;
-    getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
+  if (ctx.in.relaPlt.get() == this) {
+    if (ctx.target->usesGotPlt && ctx.in.gotPlt->getParent()) {
+      getParent()->flags |= ELF::SHF_INFO_LINK;
+      getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
+    } else if (ctx.in.plt->getParent()) {
+      getParent()->flags |= ELF::SHF_INFO_LINK;
+      getParent()->info = ctx.in.plt->getParent()->sectionIndex;
+    }
   }
 }
 
 void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
   r_offset = getOffset();
+  ctx.target->finalizeDynamicReloc(*this);
   r_sym = getSymIndex(symt);
   addend = computeAddend(ctx);
   isFinal = true; // Catch errors
@@ -1981,12 +2056,12 @@ void SymbolTableBaseSection::finalizeContents() {
     return;
   }
 
-  // If it is a .dynsym, there should be no local symbols, but we need
-  // to do a few things for the dynamic linker.
+  // If it is a .dynsym, we need to do a few things for the dynamic linker.
 
   // Section's Info field has the index of the first non-local symbol.
-  // Because the first symbol entry is a null entry, 1 is the first.
-  getParent()->info = 1;
+  // Because the first symbol entry is a null entry, add one to the number of
+  // local symbols.
+  getParent()->info = numLocalDynamicSymbols + 1;
 
   if (ctx.in.gnuHashTab) {
     // NB: It also sorts Symbols to meet the GNU hash table requirements.
@@ -2005,7 +2080,7 @@ void SymbolTableBaseSection::finalizeContents() {
 
 // The ELF spec requires that all local symbols precede global symbols, so we
 // sort symbol entries in this function. (For .dynsym, we don't do that because
-// symbols for dynamic linking are inherently all globals.)
+// local symbols are inserted before global symbols.)
 //
 // Aside from above, we put local symbols in groups starting with the STT_FILE
 // symbol. That is convenient for purpose of identifying where are local symbols
@@ -2039,6 +2114,13 @@ void SymbolTableBaseSection::addSymbol(Symbol *b) {
   symbols.push_back({b, strTabSec.addString(b->getName(), false)});
 }
 
+void SymbolTableBaseSection::addLocalSectionSymbol(Symbol *b) {
+  assert(this->type == SHT_DYNSYM && b->isLocal() && b->isSection());
+  symbols.insert(symbols.begin() + numLocalDynamicSymbols,
+                 {b, strTabSec.addString(b->getName(), false)});
+  ++numLocalDynamicSymbols;
+}
+
 size_t SymbolTableBaseSection::getSymbolIndex(const Symbol &sym) {
   if (this == ctx.in.dynSymTab.get())
     return sym.dynsymIndex;
@@ -2133,6 +2215,11 @@ template <class ELFT> void SymbolTableSection<ELFT>::w
       Symbol *sym = ent.sym;
       if (sym->isInPlt(ctx) && sym->hasFlag(NEEDS_COPY))
         eSym->st_other |= STO_MIPS_PLT;
+#if defined(__OpenBSD__)
+      if (type == SHT_DYNSYM && ctx.in.mipsStubs)
+        if (std::optional<uint64_t> va = ctx.in.mipsStubs->getStubVA(*sym))
+          eSym->st_value = *va;
+#endif
       if (isMicroMips(ctx)) {
         // We already set the less-significant bit for symbols
         // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
@@ -2303,7 +2390,7 @@ void GnuHashTableSection::addSymbols(SmallVectorImpl<S
   // its type correctly.
   auto mid =
       std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) {
-        return !s.sym->isDefined();
+        return s.sym->isLocal() || !s.sym->isDefined();
       });
 
   // We chose load factor 4 for the on-disk hash table. For each hash
@@ -2369,6 +2456,8 @@ void HashTableSection::writeTo(uint8_t *buf) {
 
   for (const SymbolTableEntry &s : symTab->getSymbols()) {
     Symbol *sym = s.sym;
+    if (sym->isLocal())
+      continue;
     StringRef name = sym->getName();
     unsigned i = sym->dynsymIndex;
     uint32_t hash = hashSysV(name) % numSymbols;
@@ -2397,23 +2486,29 @@ PltSection::PltSection(Ctx &ctx)
   if ((ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) &&
       (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT))
     name = ".plt.sec";
+#ifdef __OpenBSD__
+  else if (ctx.arg.emachine == EM_X86_64)
+    name = ".plt.sec";
+#endif
 
   // The PLT needs to be writable on SPARC as the dynamic linker will
   // modify the instructions in the PLT entries.
-  if (ctx.arg.emachine == EM_SPARCV9)
+  if (ctx.arg.emachine == EM_SPARCV9) {
     this->flags |= SHF_WRITE;
+    addralign = 256;
+  }
 }
 
 void PltSection::writeTo(uint8_t *buf) {
   // At beginning of PLT, we have code to call the dynamic
   // linker to resolve dynsyms at runtime. Write such code.
   ctx.target->writePltHeader(buf);
-  size_t off = headerSize;
 
   for (const Symbol *sym : entries) {
+    size_t off = sym->getPltOffset(ctx);
     ctx.target->writePlt(buf + off, *sym, getVA() + off);
-    off += ctx.target->pltEntrySize;
   }
+  ctx.target->finalizePlt(buf);
 }
 
 void PltSection::addEntry(Symbol &sym) {
@@ -2436,11 +2531,8 @@ bool PltSection::isNeeded() const {
 void PltSection::addSymbols() {
   ctx.target->addPltHeaderSymbols(*this);
 
-  size_t off = headerSize;
-  for (size_t i = 0; i < entries.size(); ++i) {
-    ctx.target->addPltSymbols(*this, off);
-    off += ctx.target->pltEntrySize;
-  }
+  for (const Symbol *sym : entries)
+    ctx.target->addPltSymbols(*this, sym->getPltOffset(ctx));
 }
 
 IpltSection::IpltSection(Ctx &ctx)
@@ -4527,10 +4619,12 @@ template <class ELFT> void elf::createSyntheticSection
   // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
   // it as a relocation and ensure the referenced section is created.
   if (ctx.sym.globalOffsetTable && ctx.arg.emachine != EM_MIPS) {
-    if (ctx.target->gotBaseSymInGotPlt)
+    if (ctx.target->gotBaseSymInGotPlt) {
+      assert(ctx.target->usesGotPlt);
       ctx.in.gotPlt->hasGotPltOffRel = true;
-    else
+    } else {
       ctx.in.got->hasGotOffRel = true;
+    }
   }
 
   // We always need to add rel[a].plt to output if it has entries.
@@ -4548,10 +4642,23 @@ template <class ELFT> void elf::createSyntheticSection
   ctx.in.iplt = std::make_unique<IpltSection>(ctx);
   add(*ctx.in.iplt);
 
+#if defined(__OpenBSD__)
+  if (ctx.arg.emachine == EM_MIPS && !ctx.arg.relocatable && ctx.hasDynsym) {
+    ctx.in.mipsStubs = std::make_unique<MipsStubsSection>(ctx);
+    add(*ctx.in.mipsStubs);
+  }
+#endif
+
   if (ctx.arg.andFeatures || ctx.aarch64PauthAbiCoreInfo) {
     ctx.in.gnuProperty = std::make_unique<GnuPropertySection>(ctx);
     add(*ctx.in.gnuProperty);
   }
+#ifdef __OpenBSD__
+  else if (ctx.arg.emachine == EM_X86_64) {
+    ctx.in.ibtPlt = std::make_unique<IBTPltSection>(ctx);
+    add(*ctx.in.ibtPlt);
+  }
+#endif
 
   if (ctx.arg.debugNames) {
     ctx.in.debugNames = std::make_unique<DebugNamesSection<ELFT>>(ctx);
