bfd: don't silently wrap or truncate PE image section RVAs
[deliverable/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_pecoff_format] } then {
27 set IMAGE_BASE "--image-base 0"
28 } else {
29 set IMAGE_BASE ""
30 }
31
32 if { ![is_elf_format] && ![is_pecoff_format] } then {
33 unsupported $testname
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.
37 unsupported $testname
38 } elseif {![ld_link $ld tmpdir/weak-undef "tmpdir/weak-undef.o \
39 -T $srcdir/$subdir/weak-undef.t $IMAGE_BASE"]} then {
40 # Weak symbols are broken for non-i386 PE targets.
41 if {! [istarget i?86-*-*]} {
42 setup_xfail *-*-pe*
43 }
44 fail $testname
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
49
50 set output_regexp ".*Contents of section .data:.*0000 00000000 11111111.*"
51
52 if {[regexp $output_regexp $exec_output]} then {
53 pass $testname
54 } else {
55 fail $testname
56 }
57 }
58
59 proc 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
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
81 proc 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
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 }
This page took 0.048362 seconds and 4 git commands to generate.