Test undefined symbols in shared libraries
[deliverable/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#
2571583a 4# Copyright (C) 1995-2017 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
7f6a71ff 27if { ![is_remote host] && [which $CC] == 0 } {
252b5132
RH
28 verbose "Could not find C compiler!" 1
29 untested $testund
30 untested $testfn
31 untested $testline
647d4de9 32} elseif { ![ld_compile "$CC -g" $srcdir/$subdir/undefined.c tmpdir/undefined.o] } {
252b5132
RH
33 verbose "Unable to compile test file!" 1
34 unresolved $testund
35 unresolved $testfn
36 unresolved $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
AM
42 # Using -e start prevents the SunOS linker from trying to build a
43 # shared library.
44 send_log "$ld -e start $flags -o tmpdir/undefined tmpdir/undefined.o\n"
45 set exec_output [run_host_cmd "$ld" "-e start $flags -o tmpdir/undefined tmpdir/undefined.o"]
1688b748 46
647d4de9
AM
47 send_log "$exec_output\n"
48 verbose "$exec_output"
252b5132 49
647d4de9
AM
50 proc checkund { string testname } {
51 global exec_output
252b5132 52
647d4de9
AM
53 if [string match "*$string*" $exec_output] {
54 pass $testname
55 } else {
56 fail $testname
57 }
252b5132 58 }
252b5132 59
647d4de9
AM
60 set mu "undefined reference to `*this_function_is_not_defined'"
61 checkund $mu $testund
252b5132 62
647d4de9
AM
63 # ARM PE defaults to using stabs debugging, which we can't handle
64 # for a COFF file.
65 #setup_xfail "arm*-*-pe*"
252b5132 66
647d4de9
AM
67 # For Xtensa on GNU Linux systems (or any other system where PIC
68 # code is always used), the address of the undefined function is
69 # in a literal pool outside the function, so that both the
70 # "undefined function" and "undefined line" tests fail.
71 setup_xfail xtensa*-*-linux*
2d312b6b 72
647d4de9
AM
73 set mf "tmpdir/undefined.o* In function `function':"
74 checkund $mf $testfn
252b5132 75
647d4de9
AM
76 if ![is_elf_format] {
77 # COFF SH gets this test wrong--it reports line 10, because
78 # although the jump is at line 9, the function address, and
79 # the reloc, is stored at the end of the function.
80 setup_xfail "sh-*-*"
f7bf754f 81
647d4de9
AM
82 # ARM PE defaults to using stabs debugging, which we can't
83 # handle for a COFF file.
84 #setup_xfail "arm*-*-pe*"
85 }
86
87 set ml "undefined.c:9: undefined reference to `*this_function_is_not_defined'"
88 # With targets that use elf/dwarf2, such as the arm-elf toolchain,
89 # the code in bfd/elf.c:_bfd_elf_find_nearest_line() is called in
90 # order to locate the file name/line number where the undefined
91 # reference occurs. Unfortunately this tries to use the dwarf2
92 # debug information held in the .debug_info section. This section
93 # contains a series of comp_unit structures, each of which has a
94 # low/high address range representing the span of memory locations
95 # covered by that structure. The structures also index into other
96 # structures held in the .debug_line section and together they can
97 # translate memory locations back into file/function/line number
98 # addresses in the source code. Since the information about the
99 # memory region covered by a comp_unit is only determined at link
100 # time, the low/high addresses in the .debug_info section and the
101 # line addresses in the .debug_line section are computed by
102 # generating relocs against known symbols in the object code.
103 #
104 # When the undefined reference is detected, the relocs in the
105 # dwarf2 debug sections have not yet been resolved, so the
106 # low/high addresses and the line number address are all set at
107 # zero. Thus when _bfd_elf_find_nearest_line() calls
108 # _bfd_dwarf2_find_nearest_line() no comp_unit can be found which
109 # actually covers the address where the reference occurred, and so
110 # _bfd_elf_find_nearest_line() fails.
111 #
112 # The upshot of all of this, is that the error message reported by
113 # the linker, instead of having a source file name & line number
114 # as in:
115 #
116 # undefined.c:9: undefined reference to `this_function_is_not_defined'
117 #
118 # has an object file & section address instead:
119 #
120 # undefined.0(.text+0xc): undefined reference to `this_function_is_not_defined'
121 #
122 # hence the xfails below.
123
124 setup_xfail mcore-*-elf
125 setup_xfail mep-*-*
126 setup_xfail mips-sgi-irix6*
127 setup_xfail "sh64-*-*"
128 # Fails for the MSP430 because it uses SYM_DIFF relocs but it does
129 # not provide a special_function for handling them. If
130 # optimization is enabled then this test passes because
131 # function()'s prologue is eliminated.
132 setup_xfail "msp430-*-*"
133
134 # The undefined test fails on 31 bit s/390 because the address of
135 # the function `this_function_is_not_defined' is stored in the
136 # literal pool of the function. Therefore the line number in the
137 # error message is 8 instead of 9. On 64 bit s/390 this works
138 # because of the new brasl instruction that doesn't need a literal
139 # pool entry.
140 setup_xfail s390-*-*
141
142 # See comments above for Xtensa.
143 setup_xfail xtensa*-*-linux*
144 setup_xfail hppa*64*-*-*
145
146 checkund $ml $testline
f7bf754f 147}
252b5132 148
647d4de9
AM
149# Undefined symbols should become dynamic when linking a shared lib.
150set testname "undefined symbols in shared lib"
151
152set asflags ""
153switch -glob $target_triplet {
154 aarch64* -
155 arm* -
156 powerpc64* { set asflags "--defsym BL=1" }
157 powerpc* { set asflags "--defsym BLPLT=1" }
158 hppa* { set asflags "--defsym HPPA=1" }
159 i\[3-7\]86* -
160 x86_64* { set asflags "--defsym CALLPLT=1" }
161}
162
163if { ![is_elf_format] || ![check_shared_lib_support]} then {
164 unsupported $testname
165} elseif {![ld_assemble $as "$asflags $srcdir/$subdir/fundef.s" \
166 tmpdir/fundef.o]} then {
167 fail $testname
168} elseif {![ld_link $ld tmpdir/fundef.so \
169 "-shared --allow-shlib-undefined tmpdir/fundef.o"]} then {
170 setup_xfail tic6x-*-*
171 fail $testname
172} else {
173 if {![is_remote host] && [which $nm] == 0} then {
174 unresolved "$testname (dyn sym)"
175 } else {
176 set exec_output [run_host_cmd "$nm" "-D tmpdir/fundef.so"]
177 set exec_output [prune_warnings $exec_output]
178
179 if { ($asflags == ""
180 || ([regexp ".* undef_fun_typed.*" $exec_output]
181 && [regexp ".* undef_fun_notype.*" $exec_output]))
182 && [regexp ".* undef_data.*" $exec_output]
183 && [regexp ".* undef_pfun.*" $exec_output]
184 && [regexp ".* undef_notype.*" $exec_output]} then {
185 pass "$testname (dyn sym)"
186 } else {
187 fail "$testname (dyn sym)"
188 }
189 }
190
191 global READELF
192 if {![is_remote host] && [which $READELF] == 0} then {
193 unresolved "$testname (dyn reloc)"
194 } else {
195 set exec_output [run_host_cmd "$READELF" "-r tmpdir/fundef.so"]
196 set exec_output [prune_warnings $exec_output]
197
198 # we ought to get two .rel{a}.plt and three .rel{a}.dyn relocs
199 if { ($asflags == "" || [regexp ".* contains 2 .*" $exec_output])
200 && [regexp ".* contains 3 .*" $exec_output]
201 && ![regexp "_NONE" $exec_output]} then {
202 pass "$testname (dyn reloc)"
203 } else {
204 fail "$testname (dyn reloc)"
205 }
206 }
207}
This page took 0.812835 seconds and 4 git commands to generate.