]>
Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | # Test that the linker reports undefined symbol errors correctly. |
2 | # By Ian Lance Taylor, Cygnus Support | |
3 | # | |
250d07de | 4 | # Copyright (C) 1995-2021 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 | |
23 | set testund "undefined" | |
24 | set testfn "undefined function" | |
25 | set testline "undefined line" | |
26 | ||
44ed8092 | 27 | if { ![check_compiler_available] } { |
252b5132 RH |
28 | verbose "Could not find C compiler!" 1 |
29 | untested $testund | |
30 | untested $testfn | |
31 | untested $testline | |
c6d47bff | 32 | } elseif { ![ld_compile "$CC -g $NOLTO_CFLAGS" $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 | 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* | |
2d312b6b | 77 | |
df5f2391 | 78 | set mf "tmpdir/undefined.o* in function `function':" |
647d4de9 | 79 | checkund $mf $testfn |
252b5132 | 80 | |
647d4de9 AM |
81 | if ![is_elf_format] { |
82 | # COFF SH gets this test wrong--it reports line 10, because | |
83 | # although the jump is at line 9, the function address, and | |
84 | # the reloc, is stored at the end of the function. | |
85 | setup_xfail "sh-*-*" | |
f7bf754f | 86 | |
647d4de9 AM |
87 | # ARM PE defaults to using stabs debugging, which we can't |
88 | # handle for a COFF file. | |
89 | #setup_xfail "arm*-*-pe*" | |
90 | } | |
91 | ||
92 | set ml "undefined.c:9: undefined reference to `*this_function_is_not_defined'" | |
93 | # With targets that use elf/dwarf2, such as the arm-elf toolchain, | |
94 | # the code in bfd/elf.c:_bfd_elf_find_nearest_line() is called in | |
95 | # order to locate the file name/line number where the undefined | |
96 | # reference occurs. Unfortunately this tries to use the dwarf2 | |
97 | # debug information held in the .debug_info section. This section | |
98 | # contains a series of comp_unit structures, each of which has a | |
99 | # low/high address range representing the span of memory locations | |
100 | # covered by that structure. The structures also index into other | |
101 | # structures held in the .debug_line section and together they can | |
102 | # translate memory locations back into file/function/line number | |
103 | # addresses in the source code. Since the information about the | |
104 | # memory region covered by a comp_unit is only determined at link | |
105 | # time, the low/high addresses in the .debug_info section and the | |
106 | # line addresses in the .debug_line section are computed by | |
107 | # generating relocs against known symbols in the object code. | |
108 | # | |
109 | # When the undefined reference is detected, the relocs in the | |
110 | # dwarf2 debug sections have not yet been resolved, so the | |
111 | # low/high addresses and the line number address are all set at | |
112 | # zero. Thus when _bfd_elf_find_nearest_line() calls | |
113 | # _bfd_dwarf2_find_nearest_line() no comp_unit can be found which | |
114 | # actually covers the address where the reference occurred, and so | |
115 | # _bfd_elf_find_nearest_line() fails. | |
116 | # | |
117 | # The upshot of all of this, is that the error message reported by | |
118 | # the linker, instead of having a source file name & line number | |
119 | # as in: | |
120 | # | |
121 | # undefined.c:9: undefined reference to `this_function_is_not_defined' | |
122 | # | |
123 | # has an object file & section address instead: | |
124 | # | |
125 | # undefined.0(.text+0xc): undefined reference to `this_function_is_not_defined' | |
126 | # | |
127 | # hence the xfails below. | |
128 | ||
129 | setup_xfail mcore-*-elf | |
130 | setup_xfail mep-*-* | |
131 | setup_xfail mips-sgi-irix6* | |
647d4de9 AM |
132 | # Fails for the MSP430 because it uses SYM_DIFF relocs but it does |
133 | # not provide a special_function for handling them. If | |
134 | # optimization is enabled then this test passes because | |
135 | # function()'s prologue is eliminated. | |
136 | setup_xfail "msp430-*-*" | |
137 | ||
138 | # The undefined test fails on 31 bit s/390 because the address of | |
139 | # the function `this_function_is_not_defined' is stored in the | |
140 | # literal pool of the function. Therefore the line number in the | |
141 | # error message is 8 instead of 9. On 64 bit s/390 this works | |
142 | # because of the new brasl instruction that doesn't need a literal | |
143 | # pool entry. | |
144 | setup_xfail s390-*-* | |
145 | ||
146 | # See comments above for Xtensa. | |
147 | setup_xfail xtensa*-*-linux* | |
148 | setup_xfail hppa*64*-*-* | |
149 | ||
fca8e23c JM |
150 | # eBPF doesn't support dwarf yet. |
151 | setup_xfail bpf-*-* | |
152 | ||
647d4de9 | 153 | checkund $ml $testline |
f7bf754f | 154 | } |
252b5132 | 155 | |
647d4de9 AM |
156 | # Undefined symbols should become dynamic when linking a shared lib. |
157 | set testname "undefined symbols in shared lib" | |
158 | ||
159 | set asflags "" | |
160 | switch -glob $target_triplet { | |
161 | aarch64* - | |
162 | arm* - | |
163 | powerpc64* { set asflags "--defsym BL=1" } | |
164 | powerpc* { set asflags "--defsym BLPLT=1" } | |
165 | hppa* { set asflags "--defsym HPPA=1" } | |
166 | i\[3-7\]86* - | |
167 | x86_64* { set asflags "--defsym CALLPLT=1" } | |
168 | } | |
169 | ||
170 | if { ![is_elf_format] || ![check_shared_lib_support]} then { | |
171 | unsupported $testname | |
172 | } elseif {![ld_assemble $as "$asflags $srcdir/$subdir/fundef.s" \ | |
173 | tmpdir/fundef.o]} then { | |
174 | fail $testname | |
175 | } elseif {![ld_link $ld tmpdir/fundef.so \ | |
176 | "-shared --allow-shlib-undefined tmpdir/fundef.o"]} then { | |
177 | setup_xfail tic6x-*-* | |
178 | fail $testname | |
179 | } else { | |
180 | if {![is_remote host] && [which $nm] == 0} then { | |
181 | unresolved "$testname (dyn sym)" | |
182 | } else { | |
183 | set exec_output [run_host_cmd "$nm" "-D tmpdir/fundef.so"] | |
184 | set exec_output [prune_warnings $exec_output] | |
185 | ||
186 | if { ($asflags == "" | |
187 | || ([regexp ".* undef_fun_typed.*" $exec_output] | |
188 | && [regexp ".* undef_fun_notype.*" $exec_output])) | |
189 | && [regexp ".* undef_data.*" $exec_output] | |
190 | && [regexp ".* undef_pfun.*" $exec_output] | |
191 | && [regexp ".* undef_notype.*" $exec_output]} then { | |
192 | pass "$testname (dyn sym)" | |
193 | } else { | |
194 | fail "$testname (dyn sym)" | |
195 | } | |
196 | } | |
197 | ||
198 | global READELF | |
199 | if {![is_remote host] && [which $READELF] == 0} then { | |
200 | unresolved "$testname (dyn reloc)" | |
201 | } else { | |
202 | set exec_output [run_host_cmd "$READELF" "-r tmpdir/fundef.so"] | |
203 | set exec_output [prune_warnings $exec_output] | |
204 | ||
28d810f7 MR |
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 { | |
211 | "mips64*-*-openbsd*" { | |
212 | set none_count 6 | |
213 | set reloc_count 4 | |
214 | } | |
215 | "mips*" - | |
216 | "score*" { | |
217 | set none_count 1 | |
218 | set reloc_count 4 | |
219 | } | |
220 | "*" { | |
221 | set none_count 0 | |
222 | set reloc_count 3 | |
223 | } | |
224 | } | |
225 | ||
647d4de9 | 226 | if { ($asflags == "" || [regexp ".* contains 2 .*" $exec_output]) |
28d810f7 MR |
227 | && [regexp ".* contains $reloc_count .*" $exec_output] |
228 | && [regexp -all "_NONE" $exec_output] == $none_count } then { | |
647d4de9 AM |
229 | pass "$testname (dyn reloc)" |
230 | } else { | |
231 | fail "$testname (dyn reloc)" | |
232 | } | |
233 | } | |
234 | } |