[PATCH 09/27] gdb/testsuite: Restrict --no-as-needed to ELF targets

Pedro Alves <[email protected]> Thu, 23 Jul 2026 14:01:00 +0100
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
gdb_compile passes -Wl,--no-as-needed when linking a test executable
against a shared library, to keep the shared library's DT_NEEDED entry
even when no symbol from it is referenced at link time.

--as-needed/--no-as-needed is an ELF concept, though: it controls
whether the linker emits a DT_NEEDED tag in the .dynamic section for a
shared library that ends up unreferenced.  Other object formats have
no DT_NEEDED, so the flag does nothing there.  E.g., PE records a DLL
dependency in the import table, driven by which symbols are actually
imported.  GNU ld accepts the option and silently ignores it for a PE
target (to easy portability), but lld-link, the linker clang drives on
windows-msvc, warns about it:

 lld-link: warning: ignoring unknown argument '--no-as-needed'

Fix this by restricting the flag to ELF targets, using the existing
is_elf_target predicate.

Change-Id: Icb472ba2e9e38c25c179e14648340acaea2f463d
---
 gdb/testsuite/lib/gdb.exp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index b3588f0edec..dccafbb637a 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6819,7 +6819,18 @@ proc gdb_compile {source dest type options} {
 		if { [is_pecoff_target] && ![istarget "*-*-windows-msvc*"] } {
 		    lappend new_options "ldflags=-Wl,--enable-auto-import"
 		}
-		if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } {
+		# --as-needed/--no-as-needed control whether the
+		# linker emits a DT_NEEDED entry for an unreferenced
+		# shared library, which is an ELF .dynamic-section
+		# concept.  It does nothing on other object formats
+		# (PE records DLL dependencies via the import table,
+		# Mach-O via LC_LOAD_DYLIB load commands, XCOFF via
+		# the loader section), and lld-link doesn't even
+		# recognize the option, warning about it.  So only
+		# pass it when producing ELF.
+		if { [is_elf_target]
+		     && ([test_compiler_info "gcc-*"]
+			 || [test_compiler_info "clang-*"]) } {
 		    # Undo debian's change in the default.
 		    # Put it at the front to not override any user-provided
 		    # value, and to make sure it appears in front of all the
-- 
2.54.0