]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/testsuite/ld-undefined/weak-undef.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-undefined / weak-undef.exp
CommitLineData
7dc542b2 1# Test handling of weak undefined symbols
a2c58332 2# Copyright (C) 2001-2022 Free Software Foundation, Inc.
a2b64bed 3#
f96b4a7b
NC
4# This file is part of the GNU Binutils.
5#
6# This program is free software; you can redistribute it and/or modify
a2b64bed 7# it under the terms of the GNU General Public License as published by
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
a2b64bed 9# (at your option) any later version.
f96b4a7b 10#
a2b64bed
NC
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
f96b4a7b 15#
a2b64bed
NC
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
7dc542b2 20
bf174910
AM
21# The linker should accept references to undefined weaks without error,
22# and resolve them to zero in a static executable. Ought to work for
23# some a.out targets too.
24set testname "weak undefined data symbols"
7dc542b2 25
87fa7d56
JB
26if { [is_pecoff_format] } then {
27 set IMAGE_BASE "--image-base 0"
28} else {
29 set IMAGE_BASE ""
30}
31
bf174910 32if { ![is_elf_format] && ![is_pecoff_format] } then {
977cdf5a 33 unsupported $testname
bf174910
AM
34} elseif {![ld_assemble $as $srcdir/$subdir/weak-undef.s \
35 tmpdir/weak-undef.o]} then {
36 # It's OK if .weak doesn't work on this target.
5b1f6c95 37 unsupported $testname
87fa7d56
JB
38} elseif {![ld_link $ld tmpdir/weak-undef "tmpdir/weak-undef.o \
39 -T $srcdir/$subdir/weak-undef.t $IMAGE_BASE"]} then {
bf174910
AM
40 # Weak symbols are broken for non-i386 PE targets.
41 if {! [istarget i?86-*-*]} {
42 setup_xfail *-*-pe*
43 }
bf174910 44 fail $testname
bf174910
AM
45} else {
46 set exec_output [run_host_cmd "$objdump" "-s tmpdir/weak-undef"]
47 set exec_output [prune_warnings $exec_output]
48 verbose -log $exec_output
7dc542b2 49
bf174910 50 set output_regexp ".*Contents of section .data:.*0000 00000000 11111111.*"
7dc542b2 51
bf174910
AM
52 if {[regexp $output_regexp $exec_output]} then {
53 pass $testname
54 } else {
55 fail $testname
56 }
977cdf5a
NC
57}
58
954b63d4
AM
59proc undef_weak_so { testname opts passval } {
60 global ld
61 global nm
62
63 if {![ld_link $ld tmpdir/weak-fundef.so \
64 "$opts tmpdir/weak-fundef.o"]} then {
65 fail $testname
954b63d4
AM
66 } else {
67 set exec_output [run_host_cmd "$nm" "-D tmpdir/weak-fundef.so"]
68 set exec_output [prune_warnings $exec_output]
69
70 set output_regexp ".*w undef_weak_fun.*"
71 if {[regexp $output_regexp $exec_output] == $passval} then {
72 pass $testname
73 } else {
74 fail $testname
75 }
76 return 1
77 }
78 return 0
79}
80
81proc undef_weak_exe { testname opts passval } {
82 global ld
83 global nm
84
85 if {![ld_link $ld tmpdir/weak-fundef \
86 "$opts tmpdir/weak-fundef.o tmpdir/weak-fundef.so"]} then {
87 fail $testname
954b63d4
AM
88 } else {
89 set exec_output [run_host_cmd "$nm" "-D tmpdir/weak-fundef"]
90 set exec_output [prune_warnings $exec_output]
91
92 set output_regexp ".*w undef_weak_fun.*"
93 if {[regexp $output_regexp $exec_output] == $passval} then {
94 pass $testname
95 } else {
96 fail $testname
97 }
98 }
99}
100
bf174910
AM
101# When linking a shared lib, weak undefined symbols should become dynamic.
102set testname "weak undefined function symbols in shared lib"
48733062 103
bf174910
AM
104set asflags ""
105switch -glob $target_triplet {
106 aarch64* -
107 arm* -
7cc34005
AM
108 powerpc64* { set asflags "--defsym BL=1" }
109 powerpc* { set asflags "--defsym BLPLT=1" }
bf174910
AM
110 hppa* { set asflags "--defsym HPPA=1" }
111 i\[3-7\]86* -
112 x86_64* { set asflags "--defsym CALLPLT=1" }
7dc542b2
PB
113}
114
bf174910 115if { $asflags == "" || ![is_elf_format] || ![check_shared_lib_support]} then {
626c539f 116 unsupported $testname
bf174910
AM
117} elseif {![ld_assemble $as "$asflags $srcdir/$subdir/weak-fundef.s" \
118 tmpdir/weak-fundef.o]} then {
119 fail $testname
954b63d4 120} elseif { [undef_weak_so $testname "--shared" 1] } then {
7dc542b2 121
954b63d4
AM
122 # When linking a dynamic executable, weak undefined symbols become dynamic.
123 set testname "weak undefined function symbols in dynamic exe"
124 undef_weak_exe $testname "--no-as-needed" 1
bf174910 125
954b63d4
AM
126 # Find -z options supported by the default emulation
127 set emul [get_target_emul]
128 set cmd [list "$ld --help 2>&1 | sed -e '1,/^$emul:/d;/^\[^ \]*:/,\$d'"]
129 set status [remote_exec host [concat sh -c $cmd]]
130 if { [lindex $status 0] != 0 } {
131 verbose -log "$cmd exited with status [lindex $status 0]"
7dc542b2 132 }
954b63d4 133 set emulopt [lindex $status 1]
bf174910 134
954b63d4
AM
135 if { [string first "dynamic-undefined-weak" $emulopt] >= 0 } {
136 # -z dynamic-undefined-weak is supported. Let's see if it works.
bf174910 137
954b63d4
AM
138 set testname "weak undefined functions in shared lib, no dyn undef weak"
139 undef_weak_so $testname "--shared -z nodynamic-undefined-weak" 0
bf174910 140
954b63d4
AM
141 set testname "weak undefined functions in shared lib, dyn undef weak"
142 undef_weak_so $testname "--shared -z dynamic-undefined-weak" 1
143
144 set testname "weak undefined functions in dynamic exe, no dyn undef weak"
145 undef_weak_exe $testname "-z nodynamic-undefined-weak" 0
146
147 set testname "weak undefined functions in dynamic exe, dyn undef weak"
148 undef_weak_exe $testname "-z dynamic-undefined-weak" 1
149
150 set testname "weak undefined functions in pie, no dyn undef weak"
151 undef_weak_exe $testname "-pie -z nodynamic-undefined-weak" 0
152
153 set testname "weak undefined functions in pie, dyn undef weak"
154 undef_weak_exe $testname "-pie -z dynamic-undefined-weak" 1
bf174910 155 }
7dc542b2 156}