bfd: use the ELF linker to perform relocations in BPF targets
[external/binutils.git] / ld / testsuite / ld-undefined / undefined.exp
1 # Test that the linker reports undefined symbol errors correctly.
2 # By Ian Lance Taylor, Cygnus Support
3 #
4 #   Copyright (C) 1995-2019 Free Software Foundation, Inc.
5 #
6 # This file is part of the GNU Binutils.
7 #
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.
12 #
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.
17 #
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,
21 # MA 02110-1301, USA.
22
23 set testund "undefined"
24 set testfn "undefined function"
25 set testline "undefined line"
26
27 if { ![check_compiler_available] } {
28     verbose "Could not find C compiler!" 1
29     untested $testund
30     untested $testfn
31     untested $testline
32 } elseif { ![ld_compile "$CC -g" $srcdir/$subdir/undefined.c tmpdir/undefined.o] } {
33     verbose "Unable to compile test file!" 1
34     unresolved $testund
35     unresolved $testfn
36     unresolved $testline
37 } else {
38     remote_file host delete "tmpdir/undefined"
39
40     set flags [big_or_little_endian]
41
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"]
46
47     send_log "$exec_output\n"
48     verbose "$exec_output"
49
50     proc checkund { string testname } {
51         global exec_output
52
53         if [string match "*$string*" $exec_output] {
54             pass $testname
55         } else {
56             fail $testname
57         }
58     }
59
60     set mu "undefined reference to `*this_function_is_not_defined'"
61     checkund $mu $testund
62
63     # ARM PE defaults to using stabs debugging, which we can't handle
64     # for a COFF file.
65     #setup_xfail "arm*-*-pe*"
66
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*
72
73     set mf "tmpdir/undefined.o* in function `function':"
74     checkund $mf $testfn
75
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-*-*"
81
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     # 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-*-*"
132
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
138     # pool entry.
139     setup_xfail s390-*-*
140
141     # See comments above for Xtensa.
142     setup_xfail xtensa*-*-linux*
143     setup_xfail hppa*64*-*-*
144
145     checkund $ml $testline
146 }
147
148 # Undefined symbols should become dynamic when linking a shared lib.
149 set testname "undefined symbols in shared lib"
150
151 set asflags ""
152 switch -glob $target_triplet {
153     aarch64* -
154     arm* -
155     powerpc64* { set asflags "--defsym BL=1" }
156     powerpc* { set asflags "--defsym BLPLT=1" }
157     hppa* { set asflags "--defsym HPPA=1" }
158     i\[3-7\]86* -
159     x86_64* { set asflags "--defsym CALLPLT=1" }
160 }
161
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 {
166     fail $testname
167 } elseif {![ld_link $ld tmpdir/fundef.so \
168                 "-shared --allow-shlib-undefined tmpdir/fundef.o"]} then {
169     setup_xfail tic6x-*-*
170     fail $testname
171 } else {
172     if {![is_remote host] && [which $nm] == 0} then {
173         unresolved "$testname (dyn sym)"
174     } else {
175         set exec_output [run_host_cmd "$nm" "-D tmpdir/fundef.so"]
176         set exec_output [prune_warnings $exec_output]
177
178         if { ($asflags == ""
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)"
185         } else {
186             fail "$testname (dyn sym)"
187         }
188     }
189
190     global READELF
191     if {![is_remote host] && [which $READELF] == 0} then {
192         unresolved "$testname (dyn reloc)"
193     } else {
194         set exec_output [run_host_cmd "$READELF" "-r tmpdir/fundef.so"]
195         set exec_output [prune_warnings $exec_output]
196
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*" {
204                 set none_count 6
205                 set reloc_count 4
206             }
207             "mips*" -
208             "score*" {
209                 set none_count 1
210                 set reloc_count 4
211             }
212             "*" {
213                 set none_count 0
214                 set reloc_count 3
215             }
216         }
217
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)"
222         } else {
223             fail "$testname (dyn reloc)"
224         }
225     }
226 }