1 # Test that the linker reports undefined symbol errors correctly.
2 # By Ian Lance Taylor, Cygnus Support
4 # Copyright (C) 1995-2019 Free Software Foundation, Inc.
6 # This file is part of the GNU Binutils.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
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.
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
20 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 set testund "undefined"
24 set testfn "undefined function"
25 set testline "undefined line"
27 if { ![check_compiler_available] } {
28 verbose "Could not find C compiler!" 1
32 } elseif { ![ld_compile "$CC -g" $srcdir/$subdir/undefined.c tmpdir/undefined.o] } {
33 verbose "Unable to compile test file!" 1
38 remote_file host delete "tmpdir/undefined"
40 set flags [big_or_little_endian]
42 # Using -e start prevents the SunOS linker from trying to build a
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"]
47 send_log "$exec_output\n"
48 verbose "$exec_output"
50 proc checkund { string testname } {
53 if [string match "*$string*" $exec_output] {
60 set mu "undefined reference to `*this_function_is_not_defined'"
63 # ARM PE defaults to using stabs debugging, which we can't handle
65 #setup_xfail "arm*-*-pe*"
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*
73 set mf "tmpdir/undefined.o* in function `function':"
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.
82 # ARM PE defaults to using stabs debugging, which we can't
83 # handle for a COFF file.
84 #setup_xfail "arm*-*-pe*"
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.
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.
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
116 # undefined.c:9: undefined reference to `this_function_is_not_defined'
118 # has an object file & section address instead:
120 # undefined.0(.text+0xc): undefined reference to `this_function_is_not_defined'
122 # hence the xfails below.
124 setup_xfail mcore-*-elf
126 setup_xfail mips-sgi-irix6*
127 # Fails for the MSP430 because it uses SYM_DIFF relocs but it does
128 # not provide a special_function for handling them. If
129 # optimization is enabled then this test passes because
130 # function()'s prologue is eliminated.
131 setup_xfail "msp430-*-*"
133 # The undefined test fails on 31 bit s/390 because the address of
134 # the function `this_function_is_not_defined' is stored in the
135 # literal pool of the function. Therefore the line number in the
136 # error message is 8 instead of 9. On 64 bit s/390 this works
137 # because of the new brasl instruction that doesn't need a literal
141 # See comments above for Xtensa.
142 setup_xfail xtensa*-*-linux*
143 setup_xfail hppa*64*-*-*
145 checkund $ml $testline
148 # Undefined symbols should become dynamic when linking a shared lib.
149 set testname "undefined symbols in shared lib"
152 switch -glob $target_triplet {
155 powerpc64* { set asflags "--defsym BL=1" }
156 powerpc* { set asflags "--defsym BLPLT=1" }
157 hppa* { set asflags "--defsym HPPA=1" }
159 x86_64* { set asflags "--defsym CALLPLT=1" }
162 if { ![is_elf_format] || ![check_shared_lib_support]} then {
163 unsupported $testname
164 } elseif {![ld_assemble $as "$asflags $srcdir/$subdir/fundef.s" \
165 tmpdir/fundef.o]} then {
167 } elseif {![ld_link $ld tmpdir/fundef.so \
168 "-shared --allow-shlib-undefined tmpdir/fundef.o"]} then {
169 setup_xfail tic6x-*-*
172 if {![is_remote host] && [which $nm] == 0} then {
173 unresolved "$testname (dyn sym)"
175 set exec_output [run_host_cmd "$nm" "-D tmpdir/fundef.so"]
176 set exec_output [prune_warnings $exec_output]
179 || ([regexp ".* undef_fun_typed.*" $exec_output]
180 && [regexp ".* undef_fun_notype.*" $exec_output]))
181 && [regexp ".* undef_data.*" $exec_output]
182 && [regexp ".* undef_pfun.*" $exec_output]
183 && [regexp ".* undef_notype.*" $exec_output]} then {
184 pass "$testname (dyn sym)"
186 fail "$testname (dyn sym)"
191 if {![is_remote host] && [which $READELF] == 0} then {
192 unresolved "$testname (dyn reloc)"
194 set exec_output [run_host_cmd "$READELF" "-r tmpdir/fundef.so"]
195 set exec_output [prune_warnings $exec_output]
197 # We ought to get two .rel{a}.plt and three .rel{a}.dyn relocs,
198 # except for MIPS targets whose psABI mandates an extra
199 # R_MIPS_NONE relocation, also used to pad n64 relocation
200 # triplets, and S+core targets using an extra R_SCORE_NONE
201 # relocation, so adjust for that.
202 switch -glob $target_triplet {
203 "mips64*-*-openbsd*" {
218 if { ($asflags == "" || [regexp ".* contains 2 .*" $exec_output])
219 && [regexp ".* contains $reloc_count .*" $exec_output]
220 && [regexp -all "_NONE" $exec_output] == $none_count } then {
221 pass "$testname (dyn reloc)"
223 fail "$testname (dyn reloc)"