]> 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
250d07de 2# Copyright (C) 2001-2021 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
bf174910 26if { ![is_elf_format] && ![is_pecoff_format] } then {
977cdf5a 27 unsupported $testname
bf174910
AM
28} elseif {![ld_assemble $as $srcdir/$subdir/weak-undef.s \
29 tmpdir/weak-undef.o]} then {
30 # It's OK if .weak doesn't work on this target.
31 unresolved $testname
d9816402 32} elseif {![ld_link $ld tmpdir/weak-undef \
bf174910
AM
33 "tmpdir/weak-undef.o -T $srcdir/$subdir/weak-undef.t"]} then {
34 # Weak symbols are broken for non-i386 PE targets.
35 if {! [istarget i?86-*-*]} {
36 setup_xfail *-*-pe*
37 }
bf174910
AM
38 fail $testname
39} elseif {![is_remote host] && [which $objdump] == 0} then {
40 unresolved $testname
41} else {
42 set exec_output [run_host_cmd "$objdump" "-s tmpdir/weak-undef"]
43 set exec_output [prune_warnings $exec_output]
44 verbose -log $exec_output
7dc542b2 45
bf174910 46 set output_regexp ".*Contents of section .data:.*0000 00000000 11111111.*"
7dc542b2 47
bf174910
AM
48 if {[regexp $output_regexp $exec_output]} then {
49 pass $testname
50 } else {
51 fail $testname
52 }
977cdf5a
NC
53}
54
954b63d4
AM
55proc undef_weak_so { testname opts passval } {
56 global ld
57 global nm
58
59 if {![ld_link $ld tmpdir/weak-fundef.so \
60 "$opts tmpdir/weak-fundef.o"]} then {
61 fail $testname
62 } elseif {![is_remote host] && [which $nm] == 0} then {
63 unresolved $testname
64 } else {
65 set exec_output [run_host_cmd "$nm" "-D tmpdir/weak-fundef.so"]
66 set exec_output [prune_warnings $exec_output]
67
68 set output_regexp ".*w undef_weak_fun.*"
69 if {[regexp $output_regexp $exec_output] == $passval} then {
70 pass $testname
71 } else {
72 fail $testname
73 }
74 return 1
75 }
76 return 0
77}
78
79proc undef_weak_exe { testname opts passval } {
80 global ld
81 global nm
82
83 if {![ld_link $ld tmpdir/weak-fundef \
84 "$opts tmpdir/weak-fundef.o tmpdir/weak-fundef.so"]} then {
85 fail $testname
86 } elseif {![is_remote host] && [which $nm] == 0} then {
87 unresolved $testname
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}