]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - 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
1 # Test handling of weak undefined symbols
2 # Copyright (C) 2001-2021 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
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.
15 #
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
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20
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.
24 set testname "weak undefined data symbols"
25
26 if { ![is_elf_format] && ![is_pecoff_format] } then {
27 unsupported $testname
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
32 } elseif {![ld_link $ld tmpdir/weak-undef \
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 }
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
45
46 set output_regexp ".*Contents of section .data:.*0000 00000000 11111111.*"
47
48 if {[regexp $output_regexp $exec_output]} then {
49 pass $testname
50 } else {
51 fail $testname
52 }
53 }
54
55 proc 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
79 proc 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
101 # When linking a shared lib, weak undefined symbols should become dynamic.
102 set testname "weak undefined function symbols in shared lib"
103
104 set asflags ""
105 switch -glob $target_triplet {
106 aarch64* -
107 arm* -
108 powerpc64* { set asflags "--defsym BL=1" }
109 powerpc* { set asflags "--defsym BLPLT=1" }
110 hppa* { set asflags "--defsym HPPA=1" }
111 i\[3-7\]86* -
112 x86_64* { set asflags "--defsym CALLPLT=1" }
113 }
114
115 if { $asflags == "" || ![is_elf_format] || ![check_shared_lib_support]} then {
116 unsupported $testname
117 } elseif {![ld_assemble $as "$asflags $srcdir/$subdir/weak-fundef.s" \
118 tmpdir/weak-fundef.o]} then {
119 fail $testname
120 } elseif { [undef_weak_so $testname "--shared" 1] } then {
121
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
125
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]"
132 }
133 set emulopt [lindex $status 1]
134
135 if { [string first "dynamic-undefined-weak" $emulopt] >= 0 } {
136 # -z dynamic-undefined-weak is supported. Let's see if it works.
137
138 set testname "weak undefined functions in shared lib, no dyn undef weak"
139 undef_weak_so $testname "--shared -z nodynamic-undefined-weak" 0
140
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
155 }
156 }