[lld] [lld][WebAssembly] Force-extract archive members with retained segments under --gc-sections (PR #200088)
Guy Bedford via llvm-commits
llvm-commits at lists.llvm.org
Wed May 27 19:13:18 PDT 2026
https://github.com/guybedford updated https://github.com/llvm/llvm-project/pull/200088
>From ec72cb5f9136ae746bdc5cfe0e271298e1218e05 Mon Sep 17 00:00:00 2001
From: Guy Bedford <gbedford at cloudflare.com>
Date: Wed, 27 May 2026 17:02:21 -0700
Subject: [PATCH] [lld][WebAssembly] Force-extract archive members that contain
user custom sections
wasm-ld silently drops user-defined wasm custom sections (wasm-bindgen
descriptors, wit-bindgen component metadata, coverage / sanitizer
sections, __llvm_prf_*, etc.) from archive members that have no
externally-referenced symbol.
Custom sections have no symbol-table entries by construction, so
symbol-driven archive extraction cannot reach them: the archive
member stays lazy and the section is silently dropped along with it.
This affects builds with or without --gc-sections, since the bug is in
archive lazy-extraction, not GC.
The fix teaches ObjFile::parseLazy to scan top-level sections for
WASM_SEC_CUSTOM entries, ignoring custom sections that the linker
synthesizes or owns (linking, name, producers, target_features,
reloc.*, .llvmbc, .llvmcmd, .debug_*, dylink*). If a user custom
section is present, the member is eagerly extracted
(lazy = false; markLive(); addFile(this)).
This generalizes the case-specific approach in #172023 (which added
WASM_SEG_FLAG_PRF for __llvm_prf_* sections), addressing the comment
by @sbc100 on that PR asking for a less special-cased solution. The
PRF special case could later be retired in favor of this general
mechanism.
Tested end-to-end with the wasm-bindgen raytrace-parallel example,
which previously failed with `error: import of __wbg_get_* doesn't
have an adapter listed` and now builds successfully.
Fixes #200083.
Note: this PR was authored by an LLM (Claude/OpenCode) under human
direction, with human review of the diagnosis, implementation, and
tests before submission.
---
lld/test/wasm/no-strip-segment-archive.s | 29 +++++++++++++++++++++
lld/wasm/InputFiles.cpp | 32 ++++++++++++++++++++++++
2 files changed, 61 insertions(+)
create mode 100644 lld/test/wasm/no-strip-segment-archive.s
diff --git a/lld/test/wasm/no-strip-segment-archive.s b/lld/test/wasm/no-strip-segment-archive.s
new file mode 100644
index 0000000000000..57870d191152c
--- /dev/null
+++ b/lld/test/wasm/no-strip-segment-archive.s
@@ -0,0 +1,29 @@
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj --triple=wasm32-unknown-unknown -o %t/main.o %t/main.s
+# RUN: llvm-mc -filetype=obj --triple=wasm32-unknown-unknown -o %t/lib.o %t/lib.s
+# RUN: rm -f %t/lib.a
+# RUN: llvm-ar rcs %t/lib.a %t/lib.o
+# RUN: wasm-ld %t/main.o %t/lib.a --no-entry --allow-undefined -o %t/main.wasm
+# RUN: obj2yaml %t/main.wasm | FileCheck %s
+
+# A user-defined wasm custom section inside an archive member should survive
+# linking even when no symbol in that member is referenced. Custom sections
+# (wasm-bindgen / wit-bindgen / coverage metadata, etc.) have no symbol table
+# entries by construction, so symbol-driven archive extraction cannot reach
+# them. Without explicit handling the archive member is never extracted and
+# the custom section is silently dropped.
+
+#--- main.s
+ .globl _start
+_start:
+ .functype _start () -> ()
+ end_function
+
+#--- lib.s
+ .section .custom_section.my_meta,"",@
+ .asciz "CUSTOM_PAYLOAD"
+
+# The custom section should be present in the linked binary.
+# CHECK: - Type: CUSTOM
+# CHECK: Name: my_meta
+# CHECK: Payload: 435553544F4D5F5041594C4F414400
diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp
index ad7f207259a5b..3c6c88da23fc2 100644
--- a/lld/wasm/InputFiles.cpp
+++ b/lld/wasm/InputFiles.cpp
@@ -395,9 +395,41 @@ static bool shouldMerge(const WasmSegment &seg) {
return true;
}
+// True if `name` is a custom section the linker synthesizes or otherwise owns,
+// rather than user payload that should be propagated to the output.
+// Mirrors the filter in Writer::createCustomSections.
+static bool isLinkerOwnedCustomSection(StringRef name) {
+ return name == "linking" || name == "name" || name == "producers" ||
+ name == "target_features" || name.starts_with("reloc.") ||
+ name == ".llvmbc" || name == ".llvmcmd" ||
+ name.starts_with(".debug_") || name.starts_with("dylink");
+}
+
void ObjFile::parseLazy() {
LLVM_DEBUG(dbgs() << "ObjFile::parseLazy: " << toString(this) << " "
<< wasmObj.get() << "\n");
+
+ // If this object contains any user-defined wasm custom sections (e.g.
+ // wasm-bindgen / wit-bindgen / coverage / sanitizer metadata, or
+ // __llvm_prf_*), force-extract it. Custom sections have no symbol table
+ // entries by construction, so symbol-driven archive extraction cannot reach
+ // them, and the section is silently dropped along with its archive member.
+ // The only place this content can be anchored is the archive member itself,
+ // so we extract eagerly here.
+ for (const SectionRef &secRef : wasmObj->sections()) {
+ const WasmSection &sec = wasmObj->getWasmSection(secRef);
+ if (sec.Type != WASM_SEC_CUSTOM)
+ continue;
+ if (isLinkerOwnedCustomSection(sec.Name))
+ continue;
+ LLVM_DEBUG(dbgs() << " forcing extract for custom section: " << sec.Name
+ << "\n");
+ lazy = false;
+ markLive();
+ symtab->addFile(this, /*symName=*/{});
+ return;
+ }
+
for (const SymbolRef &sym : wasmObj->symbols()) {
const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl());
if (wasmSym.isUndefined() || wasmSym.isBindingLocal())
More information about the llvm-commits
mailing list