[PATCH 3/3] ld/testsuite: Add shared test logic for undefined symbol retention

Hakan Candar hakan@envs.net
Wed Jul 16 16:03:23 GMT 2025


Move shared procedures and target configuration logic for testing
undefined symbols into a new helper file, `undef-common.tcl`. This
consolidates logic across `weak-undef.exp` and `undefined.exp`,
reducing duplication and enabling stronger coverage of ELF targets.

Add new tests for strong undefined symbols, including dynamic and
PIE executables. These are now verified to behave correctly under
`-z dynamic-undefined-weak` and `-z nodynamic-undefined-weak`.

Mark HPPA and MIPS test failures under `-z nodynamic-undefined-weak`
as expected failures due to current backend limitations. These
will be addressed in follow-up patches.

ld/testsuite/
	* ld-undefined/undef-common.tcl: New file with
	shared helpers for undefined symbol tests.
	* ld-undefined/fundef.s: Add MIPS-specific
	undefined function calls for coverage.
	* ld-undefined/undefined.exp: Use common logic,
	add dynamic/PIE tests for strong undefineds, and mark
	HPPA/MIPS failures as expected where appropriate.
	* ld-undefined/weak-undef.exp: Source common
	logic and remove duplicated test procedures.

Signed-off-by: Hakan Candar <hakan@envs.net>
---
 ld/testsuite/ld-undefined/fundef.s         |  4 +
 ld/testsuite/ld-undefined/undef-common.tcl | 96 ++++++++++++++++++++++
 ld/testsuite/ld-undefined/undefined.exp    | 73 ++++++++++++----
 ld/testsuite/ld-undefined/weak-undef.exp   | 58 +------------
 4 files changed, 160 insertions(+), 71 deletions(-)
 create mode 100644 ld/testsuite/ld-undefined/undef-common.tcl

diff --git a/ld/testsuite/ld-undefined/fundef.s b/ld/testsuite/ld-undefined/fundef.s
index 96eb668ac6e..5e747fabd94 100644
--- a/ld/testsuite/ld-undefined/fundef.s
+++ b/ld/testsuite/ld-undefined/fundef.s
@@ -22,6 +22,10 @@
 	bl undef_fun_notype,%r2
 	nop
  .endif
+ .ifdef JAL
+	jal undef_fun_typed
+	jal undef_fun_notype
+ .endif
 
 	.data
 	.type undef_data %object
diff --git a/ld/testsuite/ld-undefined/undef-common.tcl b/ld/testsuite/ld-undefined/undef-common.tcl
new file mode 100644
index 00000000000..fa07e044041
--- /dev/null
+++ b/ld/testsuite/ld-undefined/undef-common.tcl
@@ -0,0 +1,96 @@
+# Helper file for test of weak and strong undefined symbols
+#   Copyright (C) 2001-2025 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+# The linker should accept references to undefined weaks without error,
+# and resolve them to zero in a static executable.  Ought to work for
+# some a.out targets too.
+
+proc undef_so { testname opts passval objname symname regex } {
+    global ld
+    global nm
+
+    if {![ld_link $ld tmpdir/$objname.so \
+	  "$opts tmpdir/$objname.o"]} then {
+	fail $testname
+    } else {
+	set exec_output [run_host_cmd "$nm" "-D tmpdir/$objname.so"]
+	set exec_output [prune_warnings $exec_output]
+
+	set output_regexp "$regex $symname.*"
+	if {[regexp $output_regexp $exec_output] == $passval} then {
+	    pass $testname
+	} else {
+	    fail $testname
+	}
+	return 1
+    }
+    return 0
+}
+
+proc undef_exe { testname opts passval objname symname regex } {
+    global ld
+    global nm
+
+    if {![ld_link $ld tmpdir/$objname \
+	  "$opts tmpdir/$objname.o tmpdir/$objname.so"]} then {
+	fail $testname
+    } else {
+	set exec_output [run_host_cmd "$nm" "-D tmpdir/$objname"]
+	set exec_output [prune_warnings $exec_output]
+
+	set output_regexp "$regex $symname.*"
+	if {[regexp $output_regexp $exec_output] == $passval} then {
+	    pass $testname
+	} else {
+	    fail $testname
+	}
+	return 1
+    }
+    return 0
+}
+
+proc undef_weak_so { testname opts passval } {
+	return [undef_so $testname $opts $passval weak-fundef undef_weak_fun ".*w"]
+}
+
+proc undef_weak_exe { testname opts passval } {
+	return [undef_exe $testname $opts $passval weak-fundef undef_weak_fun ".*w"]
+}
+
+proc undef_strong_so { testname opts passval } {
+	return [undef_so $testname $opts $passval fundef undef_fun_notype ".*U"]
+}
+
+proc undef_strong_exe { testname opts passval } {
+	return [undef_exe $testname $opts $passval fundef undef_fun_notype ".*U"]
+}
+
+set asflags ""
+switch -glob $target_triplet {
+    aarch64* -
+    arm* -
+    powerpc64* { set asflags "--defsym BL=1" }
+    powerpc* { set asflags "--defsym BLPLT=1" }
+    hppa* { set asflags "--defsym HPPA=1" }
+    i\[3-7\]86* -
+    x86_64* { set asflags "--defsym CALLPLT=1" }
+    riscv* { set asflags "--defsym CALLPLT=1" }
+    mips* { set asflags "--defsym JAL=1 -KPIC" }
+}
diff --git a/ld/testsuite/ld-undefined/undefined.exp b/ld/testsuite/ld-undefined/undefined.exp
index 9cdb900c49d..c84e146929f 100644
--- a/ld/testsuite/ld-undefined/undefined.exp
+++ b/ld/testsuite/ld-undefined/undefined.exp
@@ -163,18 +163,10 @@ if { ![check_compiler_available] } {
 # Undefined symbols should become dynamic when linking a shared lib.
 set testname "undefined symbols in shared lib"
 
-set asflags ""
-switch -glob $target_triplet {
-    aarch64* -
-    arm* -
-    powerpc64* { set asflags "--defsym BL=1" }
-    powerpc* { set asflags "--defsym BLPLT=1" }
-    hppa* { set asflags "--defsym HPPA=1" }
-    i\[3-7\]86* -
-    x86_64* { set asflags "--defsym CALLPLT=1" }
-}
+# $asflags, undef_weak_so and undef_weak_exe comes from here
+source "$srcdir/ld-undefined/undef-common.tcl"
 
-if { ![is_elf_format] || ![check_shared_lib_support]} then {
+if { $asflags == "" || ![is_elf_format] || ![check_shared_lib_support]} then {
     unsupported $testname
 } elseif {![ld_assemble $as "$asflags $srcdir/$subdir/fundef.s" \
 		tmpdir/fundef.o]} then {
@@ -187,9 +179,8 @@ if { ![is_elf_format] || ![check_shared_lib_support]} then {
     set exec_output [run_host_cmd "$nm" "-D tmpdir/fundef.so"]
     set exec_output [prune_warnings $exec_output]
 
-    if { ($asflags == ""
-	  || ([regexp ".* undef_fun_typed.*" $exec_output]
-	      && [regexp ".* undef_fun_notype.*" $exec_output]))
+    if { (([regexp ".* undef_fun_typed.*" $exec_output]
+	    && [regexp ".* undef_fun_notype.*" $exec_output]))
 	 && [regexp ".* undef_data.*" $exec_output]
 	 && [regexp ".* undef_pfun.*" $exec_output]
 	 && [regexp ".* undef_notype.*" $exec_output]} then {
@@ -201,6 +192,7 @@ if { ![is_elf_format] || ![check_shared_lib_support]} then {
     global READELF
     set exec_output [run_host_cmd "$READELF" "-r tmpdir/fundef.so"]
     set exec_output [prune_warnings $exec_output]
+    set noplt 0
 
     # We ought to get two .rel{a}.plt and three .rel{a}.dyn relocs,
     # except for MIPS targets whose psABI mandates an extra
@@ -213,7 +205,13 @@ if { ![is_elf_format] || ![check_shared_lib_support]} then {
 	    set none_count 6
 	    set reloc_count 4
 	}
-	"mips*" -
+	"mips*" {
+	    set none_count 1
+	    set reloc_count 4
+	    # MIPS does not put plt relocations into .rel.plt
+	    # but into MIPS-specific global entries
+	    set noplt 1
+	}
 	"score*" {
 	    set none_count 1
 	    set reloc_count 4
@@ -224,7 +222,7 @@ if { ![is_elf_format] || ![check_shared_lib_support]} then {
 	}
     }
 
-    if { ($asflags == "" || [regexp ".* contains 2 .*" $exec_output])
+    if { ($noplt || [regexp ".* contains 2 .*" $exec_output])
 	 && [regexp ".* contains $reloc_count .*" $exec_output]
 	 && [regexp -all "_NONE" $exec_output] == $none_count } then {
 	pass "$testname (dyn reloc)"
@@ -232,3 +230,46 @@ if { ![is_elf_format] || ![check_shared_lib_support]} then {
 	fail "$testname (dyn reloc)"
     }
 }
+
+# When linking a dynamic executable, strong undefined symbols become dynamic.
+# This behaviour is currently only guaranteed when -z dynamic-undefined-weak
+# is passed.
+set testname "undefined function symbols in dynamic exe, dyn undef weak"
+set undefignore "--unresolved-symbols=ignore-all"
+
+if { $asflags == "" || ![is_elf_format] || ![check_shared_lib_support]} then {
+    unsupported $testname
+} elseif {![ld_assemble $as "$asflags $srcdir/$subdir/fundef.s" \
+                tmpdir/fundef.o]} then {
+    fail $testname
+} elseif { [undef_strong_exe $testname "-z dynamic-undefined-weak $undefignore" 1] } then {
+
+    set testname "undefined functions in pie, dyn undef weak"
+    undef_strong_exe $testname "-pie -z dynamic-undefined-weak $undefignore" 1
+
+    # HPPA backend errors on relocation against a non-dynamic undefined
+    # symbol under -z nodynamic-undefined-weak. It should instead resolve
+    # to 0 at link time. This will be fixed in a follow-up patch.
+    setup_xfail "hppa*-*-*"
+
+    set testname "undefined functions in dynamic exe, no dyn undef weak"
+    undef_strong_exe $testname "-z nodynamic-undefined-weak $undefignore" 0
+
+    setup_xfail "hppa*-*-*"
+
+    # MIPS backend fails to handle undefined data symbols under
+    # -z nodynamic-undefined-weak; an internal assert triggers
+    # when resolving them to 0 at link time.
+    # Function symbols are handled correctly. Investigation pending.
+    setup_xfail "mips*-*-*"
+
+    set testname "undefined functions in pie, no dyn undef weak"
+    undef_strong_exe $testname "-pie -z nodynamic-undefined-weak $undefignore" 0
+
+    setup_xfail "hppa*-*-*"
+    setup_xfail "mips*-*-*"
+
+    set testname "undefined functions in shared lib, no dyn undef weak"
+    undef_strong_so $testname "--shared -z nodynamic-undefined-weak" 0
+}
+
diff --git a/ld/testsuite/ld-undefined/weak-undef.exp b/ld/testsuite/ld-undefined/weak-undef.exp
index 2d6f8f2c573..c7757c966bf 100644
--- a/ld/testsuite/ld-undefined/weak-undef.exp
+++ b/ld/testsuite/ld-undefined/weak-undef.exp
@@ -56,67 +56,15 @@ if { ![is_elf_format] && ![is_pecoff_format] } then {
     }
 }
 
-proc undef_weak_so { testname opts passval } {
-    global ld
-    global nm
-
-    if {![ld_link $ld tmpdir/weak-fundef.so \
-	  "$opts tmpdir/weak-fundef.o"]} then {
-	fail $testname
-    } else {
-	set exec_output [run_host_cmd "$nm" "-D tmpdir/weak-fundef.so"]
-	set exec_output [prune_warnings $exec_output]
-
-	set output_regexp ".*w undef_weak_fun.*"
-	if {[regexp $output_regexp $exec_output] == $passval} then {
-	    pass $testname
-	} else {
-	    fail $testname
-	}
-	return 1
-    }
-    return 0
-}
-
-proc undef_weak_exe { testname opts passval } {
-    global ld
-    global nm
-
-    if {![ld_link $ld tmpdir/weak-fundef \
-	  "$opts tmpdir/weak-fundef.o tmpdir/weak-fundef.so"]} then {
-	fail $testname
-    } else {
-	set exec_output [run_host_cmd "$nm" "-D tmpdir/weak-fundef"]
-	set exec_output [prune_warnings $exec_output]
-
-	set output_regexp ".*w undef_weak_fun.*"
-	if {[regexp $output_regexp $exec_output] == $passval} then {
-	    pass $testname
-	} else {
-	    fail $testname
-	}
-    }
-}
-
 # When linking a shared lib, weak undefined symbols should become dynamic.
 set testname "weak undefined function symbols in shared lib"
 
-set asflags ""
-switch -glob $target_triplet {
-    aarch64* { set asflags "--defsym BL=1" }
-    arm* -
-    powerpc64* { set asflags "--defsym BL=1" }
-    powerpc* { set asflags "--defsym BLPLT=1" }
-    hppa* { set asflags "--defsym HPPA=1" }
-    i\[3-7\]86* -
-    x86_64* { set asflags "--defsym CALLPLT=1" }
-    riscv* { set asflags "--defsym CALLPLT=1" }
-    mips* { set asflags "--defsym JAL=1 -KPIC" }
-}
+# $asflags, undef_weak_so and undef_weak_exe comes from here
+source "$srcdir/ld-undefined/undef-common.tcl"
 
 if { $asflags == "" || ![is_elf_format] || ![check_shared_lib_support]} then {
     unsupported $testname
-} elseif {![ld_assemble $as "$asflags $srcdir/$subdir/weak-fundef.s" \
+} elseif {![ld_assemble $as "$asflags --defsym WEAK=1 $srcdir/$subdir/weak-fundef.s" \
 		tmpdir/weak-fundef.o]} then {
     fail $testname
 } elseif { [undef_weak_so $testname "--shared" 1] } then {
-- 
2.47.0



More information about the Binutils mailing list