ld: Pass -fno-sanitize=all to tests with linker
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfweak / elfweak.exp
CommitLineData
16a57284 1# Expect script for ld-weak tests
b3adc24a 2# Copyright (C) 2001-2020 Free Software Foundation, Inc.
16a57284 3#
f96b4a7b
NC
4# This file is part of the GNU Binutils.
5#
6# This program is free software; you can redistribute it and/or modify
16a57284 7# it under the terms of the GNU General Public License as published by
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
16a57284 9# (at your option) any later version.
f96b4a7b 10#
16a57284
L
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
f96b4a7b 15#
16a57284
L
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
16a57284
L
20#
21# Written by H.J. Lu (hjl@gnu.org)
22# Eric Youngdale (eric@andante.jic.com)
23#
24
d9816402 25# Check to see if the C compiler works
44ed8092 26if { ![check_compiler_available] } {
d9816402
AM
27 return
28}
16a57284
L
29
30# This test can only be run on a couple of ELF platforms.
31# Square bracket expressions seem to confuse istarget.
32# This is similar to the test that is used in ld-shared, BTW.
99753d9d
AM
33if { ![istarget alpha*-*-linux*]
34 && ![istarget arm*-*-linux*]
35 && ![istarget hppa*64*-*-hpux*]
36 && ![istarget hppa*-*-linux*]
37 && ![istarget i?86-*-sysv4*]
38 && ![istarget i?86-*-unixware]
39 && ![istarget i?86-*-elf*]
40 && ![istarget i?86-*-linux*]
41 && ![istarget i?86-*-gnu*]
42 && ![istarget ia64-*-elf*]
43 && ![istarget ia64-*-linux*]
44 && ![istarget m68k-*-linux*]
45 && ![istarget mips*-*-irix5*]
46 && ![istarget mips*-*-linux*]
47 && ![istarget powerpc*-*-elf*]
48 && ![istarget powerpc*-*-linux*]
49 && ![istarget powerpc*-*-sysv4*]
50 && ![istarget sh\[34\]*-*-linux*]
51 && ![istarget sparc*-*-elf]
52 && ![istarget sparc*-*-solaris2*]
5a68afcf 53 && ![istarget sparc*-*-linux*]
75c8d84d 54 && ![istarget x86_64-*-linux*]
5a68afcf 55 && ![istarget *-*-nacl*] } {
16a57284
L
56 return
57}
16a57284
L
58if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
59 return
60}
61
62set diff diff
63set tmpdir tmpdir
64set DOBJDUMP_FLAGS --dynamic-syms
65set SOBJDUMP_FLAGS --syms
75c8d84d 66set shared "--shared -Wl,--no-as-needed"
16a57284 67
1949ad04 68global NOSANTIZE_CFLAGS
5940a93c 69
16a57284
L
70#
71# objdump_symstuff
72# Dump non-dynamic symbol stuff and make sure that it is sane.
73#
74proc objdump_symstuff { objdump object expectfile } {
75 global SOBJDUMP_FLAGS
76 global version_output
77 global diff
78 global tmpdir
79
80 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
81
4e95fbcd
AM
82 set cmd "$objdump $SOBJDUMP_FLAGS $object | sed -n {s/^\\(\[0-9a-f\]* *\\)\\(\[gw\]\\)\\( *\\)\\(\[FO\]\\)/\\1\\2\\4\\3/;/foo$/p} > $tmpdir/objdump.out"
83 verbose -log $cmd
84 catch "exec $cmd" exec_output
16a57284
L
85 set exec_output [prune_warnings $exec_output]
86 if [string match "" $exec_output] then {
87
88# Now do a line-by-line comparison to effectively diff the darned things
89# The stuff coming from the expectfile is actually a regex, so we can
90# skip over the actual addresses and so forth. This is currently very
91# simpleminded - it expects a one-to-one correspondence in terms of line
92# numbers.
93
94 if [file exists $expectfile] then {
95 set file_a [open $expectfile r]
96 } else {
97 perror "$expectfile doesn't exist"
98 return 0
99 }
5a68afcf 100
16a57284
L
101 if [file exists $tmpdir/objdump.out] then {
102 set file_b [open $tmpdir/objdump.out r]
103 } else {
104 perror "$tmpdir/objdump.out doesn't exist"
105 return 0
106 }
107
108 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
109
110 set eof -1
111 set differences 0
112
113 while { [gets $file_a line] != $eof } {
114 if [regexp "^#.*$" $line] then {
115 continue
116 } else {
117 lappend list_a $line
118 }
119 }
120 close $file_a
121
122 while { [gets $file_b line] != $eof } {
4e95fbcd
AM
123 if [regexp {\.text.* \.[^ ]*$} $line] then {
124 # Discard defined powerpc64 dot-symbols
16a57284
L
125 continue
126 } else {
127 lappend list_b $line
128 }
129 }
130 close $file_b
5a68afcf 131
16a57284
L
132 for { set i 0 } { $i < [llength $list_a] } { incr i } {
133 set line_a [lindex $list_a $i]
134 set line_b [lindex $list_b $i]
5a68afcf 135
16a57284
L
136
137 verbose "\t$expectfile: $i: $line_a" 3
138 verbose "\t/tmp/objdump.out: $i: $line_b" 3
139 if [regexp $line_a $line_b] then {
140 continue
141 } else {
142 verbose -log "\t$expectfile: $i: $line_a"
143 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
5a68afcf 144
16a57284
L
145 return 0
146 }
147 }
5a68afcf 148
16a57284
L
149 if { [llength $list_a] != [llength $list_b] } {
150 verbose -log "Line count"
151 return 0
152 }
5a68afcf 153
16a57284
L
154 if $differences<1 then {
155 return 1
156 }
5a68afcf 157
16a57284
L
158 return 0
159 } else {
160 verbose -log "$exec_output"
161 return 0
162 }
163
164}
165
166#
167# objdump_dymsymstuff
168# Dump dynamic symbol stuff and make sure that it is sane.
169#
170proc objdump_dynsymstuff { objdump object expectfile } {
171 global DOBJDUMP_FLAGS
172 global version_output
173 global diff
174 global tmpdir
175
176 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
177
4e95fbcd
AM
178 set cmd "$objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
179 verbose -log $cmd
180 catch "exec $cmd" exec_output
16a57284
L
181 set exec_output [prune_warnings $exec_output]
182 if [string match "" $exec_output] then {
183
184# Now do a line-by-line comparison to effectively diff the darned things
185# The stuff coming from the expectfile is actually a regex, so we can
186# skip over the actual addresses and so forth. This is currently very
187# simpleminded - it expects a one-to-one correspondence in terms of line
188# numbers.
189
190 if [file exists $expectfile] then {
191 set file_a [open $expectfile r]
192 } else {
193 warning "$expectfile doesn't exist"
194 return 0
195 }
5a68afcf 196
16a57284
L
197 if [file exists $tmpdir/objdump.out] then {
198 set file_b [open $tmpdir/objdump.out r]
199 } else {
200 fail "$tmpdir/objdump.out doesn't exist"
201 return 0
202 }
203
204 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
205
206 set eof -1
207 set differences 0
208
209 while { [gets $file_a line] != $eof } {
210 if [regexp "^#.*$" $line] then {
211 continue
212 } else {
213 lappend list_a $line
214 }
215 }
216 close $file_a
217
218 while { [gets $file_b line] != $eof } {
4e95fbcd
AM
219 if [regexp {\.text.* \.[^ ]*$} $line] then {
220 # Discard defined powerpc64 dot-symbols
16a57284
L
221 continue
222 } else {
223 lappend list_b $line
224 }
225 }
226 close $file_b
5a68afcf 227
16a57284
L
228 for { set i 0 } { $i < [llength $list_b] } { incr i } {
229 set line_b [lindex $list_b $i]
5a68afcf 230
16a57284
L
231# The tests are rigged so that we should never export a symbol with the
232# word 'hide' in it. Thus we just search for it, and bail if we find it.
233 if [regexp "hide" $line_b] then {
234 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
5a68afcf 235
16a57284
L
236 return 0
237 }
238
239 verbose "\t$expectfile: $i: $line_b" 3
240
241 # We can't assume that the sort is consistent across
242 # systems, so we must check each regexp. When we find a
243 # regexp, we null it out, so we don't match it twice.
244 for { set j 0 } { $j < [llength $list_a] } { incr j } {
245 set line_a [lindex $list_a $j]
246
247 if [regexp $line_a $line_b] then {
248 lreplace $list_a $j $j "CAN NOT MATCH"
249 break
250 }
251 }
252
253 if { $j >= [llength $list_a] } {
254 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
5a68afcf 255
16a57284
L
256 return 0
257 }
258 }
5a68afcf 259
16a57284
L
260 if { [llength $list_a] != [llength $list_b] } {
261 verbose -log "Line count"
262 return 0
263 }
5a68afcf 264
16a57284
L
265 if $differences<1 then {
266 return 1
267 }
5a68afcf 268
16a57284
L
269 return 0
270 } else {
271 verbose -log "$exec_output"
272 return 0
273 }
274
275}
276
bd7c9df6 277proc build_lib {test libname objs dynsymexp} {
b765d4e3 278 global CC
16a57284
L
279 global objdump
280 global tmpdir
281 global shared
282 global srcdir
283 global subdir
284
bd7c9df6
L
285 set files ""
286 foreach obj $objs {
287 set files "$files $tmpdir/$obj"
288 }
289
d9816402 290 if {![ld_link $CC $tmpdir/$libname.so "$shared $files"]} {
16a57284
L
291 fail $test
292 return
293 }
294
99753d9d 295 if {![string match "" $dynsymexp]
a664545d 296 && ![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$dynsymexp]} {
16a57284
L
297 fail $test
298 return
299 }
300 pass $test
301}
302
bd7c9df6 303proc build_exec { test execname objs flags dat dynsymexp symexp} {
b765d4e3 304 global CC
16a57284
L
305 global objdump
306 global tmpdir
16a57284
L
307 global srcdir
308 global subdir
309 global exec_output
1949ad04 310 global NOSANTIZE_CFLAGS
16a57284 311
bd7c9df6
L
312 set files ""
313 foreach obj $objs {
314 set files "$files $tmpdir/$obj"
315 }
316
1949ad04 317 if {![ld_link $CC $tmpdir/$execname "$flags $NOSANTIZE_CFLAGS $files"]} {
16a57284
L
318 fail "$test"
319 return
320 }
321
322 if {![string match "" $dynsymexp]} then {
323 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$dynsymexp]} {
324 fail $test
325 return
326 }
327 }
328
329 if {![string match "" $symexp]} then {
330 if {![objdump_symstuff $objdump $tmpdir/$execname $srcdir/$subdir/$symexp]} {
331 fail $test
332 return
333 }
334 }
335
d9816402
AM
336 if ![isnative] {
337 unsupported $test
338 return
339 }
16a57284
L
340 # Run the resulting program
341 send_log "$tmpdir/$execname >$tmpdir/$execname.out\n"
342 verbose "$tmpdir/$execname >$tmpdir/$execname.out"
343 catch "exec $tmpdir/$execname >$tmpdir/$execname.out" exec_output
344 if ![string match "" $exec_output] then {
345 send_log "$exec_output\n"
346 verbose "$exec_output"
347 fail $test
348 return
349 }
350
351 send_log "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat\n"
352 verbose "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat"
353 catch "exec diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat" exec_output
354 set exec_output [prune_warnings $exec_output]
355
356 if {![string match "" $exec_output]} then {
357 send_log "$exec_output\n"
358 verbose "$exec_output"
359 fail $test
360 return
361 }
362
363 pass $test
364}
365
fb35d3d8
DD
366# Old version of GCC for MIPS default to enabling -fpic
367# and get confused if it is used on the command line.
368if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
16a57284
L
369 set picflag ""
370} else {
371 # Unfortunately, the gcc argument is -fpic and the cc argument is
372 # -KPIC. We have to try both.
373 set picflag "-fpic"
374 send_log "$CC $picflag\n"
375 verbose "$CC $picflag"
376 catch "exec $CC $picflag" exec_output
377 send_log "$exec_output\n"
378 verbose "--" "$exec_output"
99753d9d
AM
379 if { [string match "*illegal option*" $exec_output]
380 || [string match "*option ignored*" $exec_output]
381 || [string match "*unrecognized option*" $exec_output]
16a57284 382 || [string match "*passed to ld*" $exec_output] } {
c9098af4 383 set picflag "-KPIC"
16a57284
L
384 }
385}
386verbose "Using $picflag to compile PIC code"
387
40363a91
AM
388if {![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o]
389 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o]
390 || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c $tmpdir/main.o]
391 || ![ld_link $CC $tmpdir/libbar.so "$shared $tmpdir/bar.o"]
392 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o]
393 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o]
394 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o]
395 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o]
396 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o]
397 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o]
398 || ![ld_link $CC $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]
399 || ![ld_link $CC $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]
400 || ![ld_link $CC $tmpdir/libbar1a.so "$shared $tmpdir/bar1a.o $tmpdir/libfoo1a.so"]} then {
bd7c9df6 401 fail "ELF weak"
40363a91
AM
402} else {
403 build_lib "ELF DSO weak func first" libfoo "foo.o bar.o" dso.dsym
404 build_lib "ELF DSO weak func last" libfoo "bar.o foo.o" dso.dsym
405 build_lib "ELF DSO weak func first DSO" libfoo "foo.o libbar.so" dsow.dsym
406 build_lib "ELF DSO weak func last DSO" libfoo "libbar.so foo.o" dsow.dsym
407 build_exec "ELF weak func first" foo "main.o bar.o" "" strong "" strong.sym
408 build_exec "ELF weak func last" foo "bar.o main.o" "" strong "" strong.sym
409 build_exec "ELF weak func first DSO" foo "main.o libbar.so" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
410 build_exec "ELF weak func last DSO" foo "libbar.so main.o" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
411 build_lib "ELF DSO weak data first" libfoo "bar1a.o foo1a.o" dsodata.dsym
412 build_lib "ELF DSO weak data last" libfoo "foo1a.o bar1a.o" dsodata.dsym
413 build_lib "ELF DSO weak data first DSO" libfoo "main1.o libfoo1a.so" dsowdata.dsym
414 build_lib "ELF DSO weak data last DSO" libfoo "libfoo1a.so main1.o" dsowdata.dsym
415 build_lib "ELF DSO weak data first DSO common" libfoo "main1.o libfoo1b.so" dsowdata.dsym
416 build_lib "ELF DSO weak data last DSO common" libfoo "libfoo1b.so main1.o" dsowdata.dsym
417 build_exec "ELF weak data first" foo "main1.o bar1a.o foo1a.o" "" strongdata "" strongdata.sym
418 build_exec "ELF weak data last" foo "foo1a.o main1.o bar1a.o" "" strongdata "" strongdata.sym
419 build_exec "ELF weak data first common" foo "main1.o bar1a.o foo1b.o" "" strongdata "" strongcomm.sym
420 build_exec "ELF weak data last common" foo "foo1b.o main1.o bar1a.o" "" strongdata "" strongcomm.sym
421 build_exec "ELF weak data first DSO" foo "main1.o libbar1a.so libfoo1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
422 build_exec "ELF weak data last DSO" foo "libfoo1a.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
423 build_exec "ELF weak data first DSO common" foo "main1.o libbar1a.so libfoo1b.so" "-Wl,--no-as-needed,-rpath,.,-rpath-link,." weakdata weakdata.dsym ""
424 build_exec "ELF weak data last DSO common" foo "libfoo1b.so main1.o libbar1a.so" "-Wl,--no-as-needed,-rpath,.,-rpath-link,." weakdata weakdata.dsym ""
a664545d
L
425}
426
40363a91
AM
427if {![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o]
428 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar_s.o]
429 || ![ld_compile "$CC $CFLAGS $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o]
430 || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/size_main.c $tmpdir/size_main.o]} then {
a664545d 431 unresolved "ELF weak (size)"
40363a91
AM
432} else {
433 build_lib "ELF DSO small bar (size)" libsize_bar_s "size_bar_s.o" ""
434 build_lib "ELF DSO foo with small bar (size)" libsize_foo "size_foo.o libsize_bar_s.so" ""
435 build_lib "ELF DSO big bar (size)" libsize_bar "size_bar.o" ""
436 build_exec "ELF weak size" size_main "size_main.o libsize_foo.so libsize_bar.so" "-Wl,-rpath=.,-rpath-link=.,--no-as-needed" size "" ""
a664545d
L
437}
438
5cfd5a0c
PB
439verbose "size2"
440run_dump_test $srcdir/$subdir/size2
40363a91
AM
441
442if {![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/alias.c $tmpdir/alias.o]
443 || ![ld_link $CC $tmpdir/alias.so "$shared $tmpdir/alias.o"]
1949ad04 444 || ![ld_compile "$CC $CFLAGS $NOSANTIZE_CFLAGS" $srcdir/$subdir/aliasmain.c $tmpdir/aliasmain.o]
40363a91
AM
445 || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/weakref1.c $tmpdir/weakref1.o]
446 || ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/weakref2.c $tmpdir/weakref2.o]} then {
447 unresolved "ELF weak (alias)"
448} else {
449 build_exec "ELF weak (alias)" alias "aliasmain.o weakref1.o weakref2.o alias.so" "-Wl,-rpath=.,--no-as-needed" alias "" ""
450}
This page took 0.82418 seconds and 4 git commands to generate.