]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/testsuite/ld-undefined/undefined.exp
gold: Properly remove the versioned symbol
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-undefined / undefined.exp
CommitLineData
252b5132
RH
1# Test that the linker reports undefined symbol errors correctly.
2# By Ian Lance Taylor, Cygnus Support
3#
fd67aa11 4# Copyright (C) 1995-2024 Free Software Foundation, Inc.
252b5132 5#
f96b4a7b
NC
6# This file is part of the GNU Binutils.
7#
8# This program is free software; you can redistribute it and/or modify
252b5132 9# it under the terms of the GNU General Public License as published by
f96b4a7b 10# the Free Software Foundation; either version 3 of the License, or
252b5132 11# (at your option) any later version.
f96b4a7b 12#
252b5132
RH
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
f96b4a7b 17#
252b5132
RH
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
f96b4a7b
NC
20# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21# MA 02110-1301, USA.
252b5132
RH
22
23set testund "undefined"
24set testfn "undefined function"
25set testline "undefined line"
26
44ed8092 27if { ![check_compiler_available] } {
252b5132
RH
28 verbose "Could not find C compiler!" 1
29 untested $testund
30 untested $testfn
31 untested $testline
ad77db1c 32} elseif { ![ld_compile "$CC_FOR_TARGET -g $NOLTO_CFLAGS" $srcdir/$subdir/undefined.c tmpdir/undefined.o] } {
252b5132 33 verbose "Unable to compile test file!" 1
5b1f6c95
AM
34 unsupported $testund
35 unsupported $testfn
36 unsupported $testline
647d4de9
AM
37} else {
38 remote_file host delete "tmpdir/undefined"
252b5132 39
647d4de9 40 set flags [big_or_little_endian]
252b5132 41
647d4de9 42 # Using -e start prevents the SunOS linker from trying to build a
fca8e23c
JM
43 # shared library. But don't use an entry point in BPF targets.
44 switch -glob $target_triplet {
45 bpf-*-* { set entry "" }
46 * { set entry "-e start" }
47 }
48
49 send_log "$ld $entry $flags -o tmpdir/undefined tmpdir/undefined.o\n"
50 set exec_output [run_host_cmd "$ld" "$entry $flags -o tmpdir/undefined tmpdir/undefined.o"]
1688b748 51
647d4de9
AM
52 send_log "$exec_output\n"
53 verbose "$exec_output"
252b5132 54
647d4de9
AM
55 proc checkund { string testname } {
56 global exec_output
252b5132 57
647d4de9
AM
58 if [string match "*$string*" $exec_output] {
59 pass $testname
60 } else {
61 fail $testname
62 }
252b5132 63 }
252b5132 64
647d4de9
AM
65 set mu "undefined reference to `*this_function_is_not_defined'"
66 checkund $mu $testund
252b5132 67
647d4de9
AM
68 # ARM PE defaults to using stabs debugging, which we can't handle
69 # for a COFF file.
70 #setup_xfail "arm*-*-pe*"
252b5132 71
647d4de9
AM
72 # For Xtensa on GNU Linux systems (or any other system where PIC
73 # code is always used), the address of the undefined function is
74 # in a literal pool outside the function, so that both the
75 # "undefined function" and "undefined line" tests fail.
76 setup_xfail xtensa*-*-linux*
20eee754 77 setup_xfail loongarch*-*-*
2d312b6b 78
df5f2391 79 set mf "tmpdir/undefined.o* in function `function':"
647d4de9 80 checkund $mf $testfn
252b5132 81
647d4de9
AM
82 if ![is_elf_format] {
83 # COFF SH gets this test wrong--it reports line 10, because
84 # although the jump is at line 9, the function address, and
85 # the reloc, is stored at the end of the function.
86 setup_xfail "sh-*-*"
f7bf754f 87
647d4de9
AM
88 # ARM PE defaults to using stabs debugging, which we can't
89 # handle for a COFF file.
90 #setup_xfail "arm*-*-pe*"
91 }
92
02d2a369 93 set ml {undefined.c:9:(*): undefined reference to `*this_function_is_not_defined'}
647d4de9
AM
94 # With targets that use elf/dwarf2, such as the arm-elf toolchain,
95 # the code in bfd/elf.c:_bfd_elf_find_nearest_line() is called in
96 # order to locate the file name/line number where the undefined
97 # reference occurs. Unfortunately this tries to use the dwarf2
98 # debug information held in the .debug_info section. This section
99 # contains a series of comp_unit structures, each of which has a
100 # low/high address range representing the span of memory locations
101 # covered by that structure. The structures also index into other
102 # structures held in the .debug_line section and together they can
103 # translate memory locations back into file/function/line number
104 # addresses in the source code. Since the information about the
105 # memory region covered by a comp_unit is only determined at link
106 # time, the low/high addresses in the .debug_info section and the
107 # line addresses in the .debug_line section are computed by
108 # generating relocs against known symbols in the object code.
109 #
110 # When the undefined reference is detected, the relocs in the
111 # dwarf2 debug sections have not yet been resolved, so the
112 # low/high addresses and the line number address are all set at
113 # zero. Thus when _bfd_elf_find_nearest_line() calls
114 # _bfd_dwarf2_find_nearest_line() no comp_unit can be found which
115 # actually covers the address where the reference occurred, and so
116 # _bfd_elf_find_nearest_line() fails.
117 #
118 # The upshot of all of this, is that the error message reported by
119 # the linker, instead of having a source file name & line number
120 # as in:
121 #
122 # undefined.c:9: undefined reference to `this_function_is_not_defined'
123 #
124 # has an object file & section address instead:
125 #
126 # undefined.0(.text+0xc): undefined reference to `this_function_is_not_defined'
127 #
128 # hence the xfails below.
129
130 setup_xfail mcore-*-elf
131 setup_xfail mep-*-*
132 setup_xfail mips-sgi-irix6*
647d4de9
AM
133 # Fails for the MSP430 because it uses SYM_DIFF relocs but it does
134 # not provide a special_function for handling them. If
135 # optimization is enabled then this test passes because
136 # function()'s prologue is eliminated.
137 setup_xfail "msp430-*-*"
138
139 # The undefined test fails on 31 bit s/390 because the address of
140 # the function `this_function_is_not_defined' is stored in the
141 # literal pool of the function. Therefore the line number in the
142 # error message is 8 instead of 9. On 64 bit s/390 this works
143 # because of the new brasl instruction that doesn't need a literal
144 # pool entry.
9884676d
AM
145 # FIXME: this test has been an XPASS since 2021-04 (and maybe
146 # earlier, that's just where my logs went back to). Testing the
147 # gcc version is a wild guess at what might have changed.
148 if ![at_least_gcc_version 11 0] {
149 setup_xfail s390-*-*
150 }
647d4de9
AM
151
152 # See comments above for Xtensa.
153 setup_xfail xtensa*-*-linux*
154 setup_xfail hppa*64*-*-*
155
fca8e23c
JM
156 # eBPF doesn't support dwarf yet.
157 setup_xfail bpf-*-*
20eee754 158 setup_xfail loongarch*-*-*
fca8e23c 159
647d4de9 160 checkund $ml $testline
f7bf754f 161}
252b5132 162
647d4de9
AM
163# Undefined symbols should become dynamic when linking a shared lib.
164set testname "undefined symbols in shared lib"
165
166set asflags ""
167switch -glob $target_triplet {
168 aarch64* -
169 arm* -
170 powerpc64* { set asflags "--defsym BL=1" }
171 powerpc* { set asflags "--defsym BLPLT=1" }
172 hppa* { set asflags "--defsym HPPA=1" }
173 i\[3-7\]86* -
174 x86_64* { set asflags "--defsym CALLPLT=1" }
175}
176
177if { ![is_elf_format] || ![check_shared_lib_support]} then {
178 unsupported $testname
179} elseif {![ld_assemble $as "$asflags $srcdir/$subdir/fundef.s" \
180 tmpdir/fundef.o]} then {
181 fail $testname
182} elseif {![ld_link $ld tmpdir/fundef.so \
183 "-shared --allow-shlib-undefined tmpdir/fundef.o"]} then {
184 setup_xfail tic6x-*-*
185 fail $testname
186} else {
5b1f6c95
AM
187 set exec_output [run_host_cmd "$nm" "-D tmpdir/fundef.so"]
188 set exec_output [prune_warnings $exec_output]
189
190 if { ($asflags == ""
191 || ([regexp ".* undef_fun_typed.*" $exec_output]
192 && [regexp ".* undef_fun_notype.*" $exec_output]))
193 && [regexp ".* undef_data.*" $exec_output]
194 && [regexp ".* undef_pfun.*" $exec_output]
195 && [regexp ".* undef_notype.*" $exec_output]} then {
196 pass "$testname (dyn sym)"
647d4de9 197 } else {
5b1f6c95 198 fail "$testname (dyn sym)"
647d4de9
AM
199 }
200
201 global READELF
5b1f6c95
AM
202 set exec_output [run_host_cmd "$READELF" "-r tmpdir/fundef.so"]
203 set exec_output [prune_warnings $exec_output]
204
205 # We ought to get two .rel{a}.plt and three .rel{a}.dyn relocs,
206 # except for MIPS targets whose psABI mandates an extra
207 # R_MIPS_NONE relocation, also used to pad n64 relocation
208 # triplets, and S+core targets using an extra R_SCORE_NONE
209 # relocation, so adjust for that.
210 switch -glob $target_triplet {
29c108c9
YS
211 "mips64*-*-openbsd*" -
212 "mips64*-*-*-gnuabi64" {
5b1f6c95
AM
213 set none_count 6
214 set reloc_count 4
28d810f7 215 }
5b1f6c95
AM
216 "mips*" -
217 "score*" {
218 set none_count 1
219 set reloc_count 4
220 }
221 "*" {
222 set none_count 0
223 set reloc_count 3
647d4de9
AM
224 }
225 }
5b1f6c95
AM
226
227 if { ($asflags == "" || [regexp ".* contains 2 .*" $exec_output])
228 && [regexp ".* contains $reloc_count .*" $exec_output]
229 && [regexp -all "_NONE" $exec_output] == $none_count } then {
230 pass "$testname (dyn reloc)"
231 } else {
232 fail "$testname (dyn reloc)"
233 }
647d4de9 234}