Commit | Line | Data |
---|---|---|
7dc542b2 | 1 | # Test handling of weak undefined symbols |
b3adc24a | 2 | # Copyright (C) 2001-2020 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. | |
24 | set testname "weak undefined data symbols" | |
7dc542b2 | 25 | |
bf174910 | 26 | if { ![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 |
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 | ||
bf174910 AM |
101 | # When linking a shared lib, weak undefined symbols should become dynamic. |
102 | set testname "weak undefined function symbols in shared lib" | |
48733062 | 103 | |
bf174910 AM |
104 | set asflags "" |
105 | switch -glob $target_triplet { | |
106 | aarch64* - | |
107 | arm* - | |
108 | powerpc* { set asflags "--defsym BL=1" } | |
109 | hppa* { set asflags "--defsym HPPA=1" } | |
110 | i\[3-7\]86* - | |
111 | x86_64* { set asflags "--defsym CALLPLT=1" } | |
7dc542b2 PB |
112 | } |
113 | ||
bf174910 | 114 | if { $asflags == "" || ![is_elf_format] || ![check_shared_lib_support]} then { |
626c539f | 115 | unsupported $testname |
bf174910 AM |
116 | } elseif {![ld_assemble $as "$asflags $srcdir/$subdir/weak-fundef.s" \ |
117 | tmpdir/weak-fundef.o]} then { | |
118 | fail $testname | |
954b63d4 | 119 | } elseif { [undef_weak_so $testname "--shared" 1] } then { |
7dc542b2 | 120 | |
954b63d4 AM |
121 | # When linking a dynamic executable, weak undefined symbols become dynamic. |
122 | set testname "weak undefined function symbols in dynamic exe" | |
123 | undef_weak_exe $testname "--no-as-needed" 1 | |
bf174910 | 124 | |
954b63d4 AM |
125 | # Find -z options supported by the default emulation |
126 | set emul [get_target_emul] | |
127 | set cmd [list "$ld --help 2>&1 | sed -e '1,/^$emul:/d;/^\[^ \]*:/,\$d'"] | |
128 | set status [remote_exec host [concat sh -c $cmd]] | |
129 | if { [lindex $status 0] != 0 } { | |
130 | verbose -log "$cmd exited with status [lindex $status 0]" | |
7dc542b2 | 131 | } |
954b63d4 | 132 | set emulopt [lindex $status 1] |
bf174910 | 133 | |
954b63d4 AM |
134 | if { [string first "dynamic-undefined-weak" $emulopt] >= 0 } { |
135 | # -z dynamic-undefined-weak is supported. Let's see if it works. | |
bf174910 | 136 | |
954b63d4 AM |
137 | set testname "weak undefined functions in shared lib, no dyn undef weak" |
138 | undef_weak_so $testname "--shared -z nodynamic-undefined-weak" 0 | |
bf174910 | 139 | |
954b63d4 AM |
140 | set testname "weak undefined functions in shared lib, dyn undef weak" |
141 | undef_weak_so $testname "--shared -z dynamic-undefined-weak" 1 | |
142 | ||
143 | set testname "weak undefined functions in dynamic exe, no dyn undef weak" | |
144 | undef_weak_exe $testname "-z nodynamic-undefined-weak" 0 | |
145 | ||
146 | set testname "weak undefined functions in dynamic exe, dyn undef weak" | |
147 | undef_weak_exe $testname "-z dynamic-undefined-weak" 1 | |
148 | ||
149 | set testname "weak undefined functions in pie, no dyn undef weak" | |
150 | undef_weak_exe $testname "-pie -z nodynamic-undefined-weak" 0 | |
151 | ||
152 | set testname "weak undefined functions in pie, dyn undef weak" | |
153 | undef_weak_exe $testname "-pie -z dynamic-undefined-weak" 1 | |
bf174910 | 154 | } |
7dc542b2 | 155 | } |