From afdc4b1526d41ff48ae4aa5efd230cd4f9d1c430 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 28 Apr 2025 15:30:35 -0700 Subject: [PATCH] [lld][WebAssembly] Don't mark `--start-lib`/`--end-lib` files as live (#137714) Without this change files in `--start-lib`/`--end-lib` groups were being marked as live, which means there static constructors were being included in the link. --- lld/test/wasm/Inputs/start-lib1.s | 7 +++++++ lld/wasm/Driver.cpp | 4 ---- lld/wasm/InputFiles.cpp | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lld/test/wasm/Inputs/start-lib1.s b/lld/test/wasm/Inputs/start-lib1.s index 229f67a4bd89..9ebfdbc2f61e 100644 --- a/lld/test/wasm/Inputs/start-lib1.s +++ b/lld/test/wasm/Inputs/start-lib1.s @@ -5,3 +5,10 @@ foo: .functype foo () -> () call bar end_function + +# Static constructor inserted here to ensure the object file is not +# being processed as "live". Live object files have their static constructors +# preserved even if no symbol within is used. +.section .init_array,"",@ + .p2align 2 + .int32 foo diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index 80e9a3ead2a4..d22261574f85 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -318,10 +318,6 @@ void LinkerDriver::addFile(StringRef path) { if (inWholeArchive) { for (const auto &[m, offset] : members) { auto *object = createObjectFile(m, path, offset); - // Mark object as live; object members are normally not - // live by default but -whole-archive is designed to treat - // them as such. - object->markLive(); files.push_back(object); } diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp index 614cddddd1b1..1d1b82c9879b 100644 --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -423,8 +423,10 @@ ObjFile::ObjFile(MemoryBufferRef m, StringRef archiveName, bool lazy) // https://github.com/llvm/llvm-project/issues/98778 checkArch(wasmObj->getArch()); - // If this isn't part of an archive, it's eagerly linked, so mark it live. - if (archiveName.empty()) + // Unless we are processing this as a lazy object file (e.g. part of an + // archive file or within `--start-lib`/`--end-lib`, it's eagerly linked, so + // mark it live. + if (!lazy) markLive(); }