[gdb/testsuite] Fix failure in gdb.python/py-startup-opt.exp
[binutils-gdb.git] / ld / testsuite / ld-elfweak / elfweak.exp
blob18d5058ca41757f36fdeeb6f81e741d162bf40e8
1 # Expect script for ld-weak tests
2 # Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
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.
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
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
21 # Written by H.J. Lu (hjl@gnu.org)
22 # Eric Youngdale (eric@andante.jic.com)
25 # Check to see if the C compiler works
26 if { ![check_compiler_available] } {
27 return
30 # Skip targets where -shared is not supported
31 if { ![check_shared_lib_support] } {
32 return
35 # This test can only be run on a couple of ELF platforms.
36 # Square bracket expressions seem to confuse istarget.
37 # This is similar to the test that is used in ld-shared, BTW.
38 if { ![istarget alpha*-*-linux*]
39 && ![istarget arm*-*-linux*]
40 && ![istarget hppa*64*-*-hpux*]
41 && ![istarget hppa*-*-linux*]
42 && ![istarget i?86-*-elf*]
43 && ![istarget i?86-*-linux*]
44 && ![istarget i?86-*-gnu*]
45 && ![istarget ia64-*-elf*]
46 && ![istarget ia64-*-linux*]
47 && ![istarget m68k-*-linux*]
48 && ![istarget mips*-*-irix5*]
49 && ![istarget mips*-*-linux*]
50 && ![istarget powerpc*-*-elf*]
51 && ![istarget powerpc*-*-linux*]
52 && ![istarget powerpc*-*-sysv4*]
53 && ![istarget sh\[34\]*-*-linux*]
54 && ![istarget sparc*-*-elf]
55 && ![istarget sparc*-*-solaris2*]
56 && ![istarget sparc*-*-linux*]
57 && ![istarget x86_64-*-linux*]
58 && ![istarget *-*-nacl*] } {
59 return
62 set diff diff
63 set tmpdir tmpdir
64 set DOBJDUMP_FLAGS --dynamic-syms
65 set SOBJDUMP_FLAGS --syms
66 set shared "--shared -Wl,--no-as-needed"
69 # objdump_symstuff
70 # Dump non-dynamic symbol stuff and make sure that it is sane.
72 proc objdump_symstuff { objdump object expectfile } {
73 global SOBJDUMP_FLAGS
74 global version_output
75 global diff
76 global tmpdir
78 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
80 set cmd "$objdump $SOBJDUMP_FLAGS $object | sed -n {s/^\\(\[0-9a-f\]* *\\)\\(\[gw\]\\)\\( *\\)\\(\[FO\]\\)/\\1\\2\\4\\3/;/foo$/p} > $tmpdir/objdump.out"
81 verbose -log $cmd
82 catch "exec $cmd" exec_output
83 set exec_output [prune_warnings $exec_output]
84 if [string match "" $exec_output] then {
86 # Now do a line-by-line comparison to effectively diff the darned things
87 # The stuff coming from the expectfile is actually a regex, so we can
88 # skip over the actual addresses and so forth. This is currently very
89 # simpleminded - it expects a one-to-one correspondence in terms of line
90 # numbers.
92 if [file exists $expectfile] then {
93 set file_a [open $expectfile r]
94 } else {
95 perror "$expectfile doesn't exist"
96 return 0
99 if [file exists $tmpdir/objdump.out] then {
100 set file_b [open $tmpdir/objdump.out r]
101 } else {
102 perror "$tmpdir/objdump.out doesn't exist"
103 return 0
106 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
108 set eof -1
109 set list_a {}
110 set list_b {}
112 while { [gets $file_a line] != $eof } {
113 if [regexp "^#.*$" $line] then {
114 continue
115 } else {
116 lappend list_a $line
119 close $file_a
121 while { [gets $file_b line] != $eof } {
122 if [regexp {\.text.* \.[^ ]*$} $line] then {
123 # Discard defined powerpc64 dot-symbols
124 continue
125 } else {
126 lappend list_b $line
129 close $file_b
131 for { set i 0 } { $i < [llength $list_a] } { incr i } {
132 set line_a [lindex $list_a $i]
133 set line_b [lindex $list_b $i]
135 verbose "\t$expectfile: $i: $line_a" 3
136 verbose "\t/tmp/objdump.out: $i: $line_b" 3
137 if [regexp $line_a $line_b] then {
138 continue
139 } else {
140 verbose -log "\t$expectfile: $i: $line_a"
141 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
143 return 0
147 if { [llength $list_a] != [llength $list_b] } {
148 verbose -log "Line count"
149 return 0
151 return 1
153 } else {
154 verbose -log "$exec_output"
155 return 0
160 # objdump_dymsymstuff
161 # Dump dynamic symbol stuff and make sure that it is sane.
163 proc objdump_dynsymstuff { objdump object expectfile } {
164 global DOBJDUMP_FLAGS
165 global version_output
166 global diff
167 global tmpdir
169 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
171 set cmd "$objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
172 verbose -log $cmd
173 catch "exec $cmd" exec_output
174 set exec_output [prune_warnings $exec_output]
175 if [string match "" $exec_output] then {
177 # Now do a line-by-line comparison to effectively diff the darned things
178 # The stuff coming from the expectfile is actually a regex, so we can
179 # skip over the actual addresses and so forth. This is currently very
180 # simpleminded - it expects a one-to-one correspondence in terms of line
181 # numbers.
183 if [file exists $expectfile] then {
184 set file_a [open $expectfile r]
185 } else {
186 warning "$expectfile doesn't exist"
187 return 0
190 if [file exists $tmpdir/objdump.out] then {
191 set file_b [open $tmpdir/objdump.out r]
192 } else {
193 fail "$tmpdir/objdump.out doesn't exist"
194 return 0
197 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
199 set eof -1
200 set list_a {}
201 set list_b {}
203 while { [gets $file_a line] != $eof } {
204 if [regexp "^#.*$" $line] then {
205 continue
206 } else {
207 lappend list_a $line
210 close $file_a
212 while { [gets $file_b line] != $eof } {
213 if [regexp {\.text.* \.[^ ]*$} $line] then {
214 # Discard defined powerpc64 dot-symbols
215 continue
216 } else {
217 lappend list_b $line
220 close $file_b
222 for { set i 0 } { $i < [llength $list_b] } { incr i } {
223 set line_b [lindex $list_b $i]
225 # The tests are rigged so that we should never export a symbol with the
226 # word 'hide' in it. Thus we just search for it, and bail if we find it.
227 if [regexp "hide" $line_b] then {
228 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
230 return 0
233 verbose "\t$expectfile: $i: $line_b" 3
235 # We can't assume that the sort is consistent across
236 # systems, so we must check each regexp. When we find a
237 # regexp, we null it out, so we don't match it twice.
238 for { set j 0 } { $j < [llength $list_a] } { incr j } {
239 set line_a [lindex $list_a $j]
241 if [regexp $line_a $line_b] then {
242 lreplace $list_a $j $j "CAN NOT MATCH"
243 break
247 if { $j >= [llength $list_a] } {
248 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
250 return 0
254 if { [llength $list_a] != [llength $list_b] } {
255 verbose -log "Line count"
256 return 0
258 return 1
260 } else {
261 verbose -log "$exec_output"
262 return 0
266 proc build_lib {test libname objs dynsymexp} {
267 global CC_FOR_TARGET
268 global objdump
269 global tmpdir
270 global shared
271 global srcdir
272 global subdir
273 global NOSANITIZE_CFLAGS
274 global NOLTO_CFLAGS
276 set files ""
277 foreach obj $objs {
278 set files "$files $tmpdir/$obj"
281 if {![ld_link "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $tmpdir/$libname.so "$shared $files"]} {
282 fail $test
283 return
286 if {![string match "" $dynsymexp]
287 && ![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$dynsymexp]} {
288 fail $test
289 return
291 pass $test
294 proc build_exec { test execname objs flags dat dynsymexp symexp} {
295 global CC_FOR_TARGET
296 global objdump
297 global tmpdir
298 global srcdir
299 global subdir
300 global exec_output
301 global NOSANITIZE_CFLAGS
302 global NOLTO_CFLAGS
304 set files ""
305 foreach obj $objs {
306 set files "$files $tmpdir/$obj"
309 if {![ld_link $CC_FOR_TARGET $tmpdir/$execname "$flags $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $files"]} {
310 fail "$test"
311 return
314 if {![string match "" $dynsymexp]} then {
315 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$dynsymexp]} {
316 fail $test
317 return
321 if {![string match "" $symexp]} then {
322 if {![objdump_symstuff $objdump $tmpdir/$execname $srcdir/$subdir/$symexp]} {
323 fail $test
324 return
328 if ![isnative] {
329 unsupported $test
330 return
332 # Run the resulting program
333 send_log "$tmpdir/$execname >$tmpdir/$execname.out\n"
334 verbose "$tmpdir/$execname >$tmpdir/$execname.out"
335 catch "exec $tmpdir/$execname >$tmpdir/$execname.out" exec_output
336 if ![string match "" $exec_output] then {
337 send_log "$exec_output\n"
338 verbose "$exec_output"
339 fail $test
340 return
343 send_log "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat\n"
344 verbose "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat"
345 catch "exec diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat" exec_output
346 set exec_output [prune_warnings $exec_output]
348 if {![string match "" $exec_output]} then {
349 send_log "$exec_output\n"
350 verbose "$exec_output"
351 fail $test
352 return
355 pass $test
358 # Disable LTO for these tests.
359 set cc_cmd "$CC_FOR_TARGET"
360 if {[check_lto_available]} {
361 append cc_cmd " -fno-lto"
364 # Disable all sanitizers and LTO.
365 set saved_CFLAGS "$CFLAGS_FOR_TARGET"
366 append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
368 # Old version of GCC for MIPS default to enabling -fpic
369 # and get confused if it is used on the command line.
370 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
371 set picflag ""
372 } else {
373 # Unfortunately, the gcc argument is -fpic and the cc argument is
374 # -KPIC. We have to try both.
375 set picflag "-fpic"
376 send_log "$CC_FOR_TARGET $picflag\n"
377 verbose "$CC_FOR_TARGET $picflag"
378 catch "exec $CC_FOR_TARGET $picflag" exec_output
379 send_log "$exec_output\n"
380 verbose "--" "$exec_output"
381 if { [string match "*illegal option*" $exec_output]
382 || [string match "*option ignored*" $exec_output]
383 || [string match "*unrecognized option*" $exec_output]
384 || [string match "*passed to ld*" $exec_output] } {
385 set picflag "-KPIC"
388 verbose "Using $picflag to compile PIC code"
390 if {![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o]
391 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o]
392 || ![ld_compile "$cc_cmd" $srcdir/$subdir/main.c $tmpdir/main.o]
393 || ![ld_link $cc_cmd $tmpdir/libbar.so "$shared $tmpdir/bar.o"]
394 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o]
395 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o]
396 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o]
397 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o]
398 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o]
399 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o]} then {
400 unsupported "ELF weak"
401 } elseif {![ld_link $cc_cmd $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]
402 || ![ld_link $cc_cmd $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]
403 || ![ld_link $cc_cmd $tmpdir/libbar1a.so "$shared $tmpdir/bar1a.o $tmpdir/libfoo1a.so"]} then {
404 fail "ELF weak"
405 } else {
406 build_lib "ELF DSO weak func first" libfoo "foo.o bar.o" dso.dsym
407 build_lib "ELF DSO weak func last" libfoo "bar.o foo.o" dso.dsym
408 build_lib "ELF DSO weak func first DSO" libfoo "foo.o libbar.so" dsow.dsym
409 build_lib "ELF DSO weak func last DSO" libfoo "libbar.so foo.o" dsow.dsym
410 build_exec "ELF weak func first" foo "main.o bar.o" "" strong "" strong.sym
411 build_exec "ELF weak func last" foo "bar.o main.o" "" strong "" strong.sym
412 build_exec "ELF weak func first DSO" foo "main.o libbar.so" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
413 build_exec "ELF weak func last DSO" foo "libbar.so main.o" "-Wl,-rpath,.,--no-as-needed" weak weak.dsym ""
414 build_lib "ELF DSO weak data first" libfoo "bar1a.o foo1a.o" dsodata.dsym
415 build_lib "ELF DSO weak data last" libfoo "foo1a.o bar1a.o" dsodata.dsym
416 build_lib "ELF DSO weak data first DSO" libfoo "main1.o libfoo1a.so" dsowdata.dsym
417 build_lib "ELF DSO weak data last DSO" libfoo "libfoo1a.so main1.o" dsowdata.dsym
418 build_lib "ELF DSO weak data first DSO common" libfoo "main1.o libfoo1b.so" dsowdata.dsym
419 build_lib "ELF DSO weak data last DSO common" libfoo "libfoo1b.so main1.o" dsowdata.dsym
420 build_exec "ELF weak data first" foo "main1.o bar1a.o foo1a.o" "" strongdata "" strongdata.sym
421 build_exec "ELF weak data last" foo "foo1a.o main1.o bar1a.o" "" strongdata "" strongdata.sym
422 build_exec "ELF weak data first common" foo "main1.o bar1a.o foo1b.o" "" strongdata "" strongcomm.sym
423 build_exec "ELF weak data last common" foo "foo1b.o main1.o bar1a.o" "" strongdata "" strongcomm.sym
424 build_exec "ELF weak data first DSO" foo "main1.o libbar1a.so libfoo1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
425 build_exec "ELF weak data last DSO" foo "libfoo1a.so main1.o libbar1a.so" "-Wl,-rpath,.,--no-as-needed" weakdata weakdata.dsym ""
426 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 ""
427 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 ""
430 if {![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o]
431 || ![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar_s.o]
432 || ![ld_compile "$cc_cmd $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o]
433 || ![ld_compile "$cc_cmd" $srcdir/$subdir/size_main.c $tmpdir/size_main.o]} then {
434 unsupported "ELF weak (size)"
435 } else {
436 build_lib "ELF DSO small bar (size)" libsize_bar_s "size_bar_s.o" ""
437 build_lib "ELF DSO foo with small bar (size)" libsize_foo "size_foo.o libsize_bar_s.so" ""
438 build_lib "ELF DSO big bar (size)" libsize_bar "size_bar.o" ""
439 build_exec "ELF weak size" size_main "size_main.o libsize_foo.so libsize_bar.so" "-Wl,-rpath=.,-rpath-link=.,--no-as-needed" size "" ""
442 verbose "size2"
443 run_dump_test $srcdir/$subdir/size2
445 if {![ld_compile "$cc_cmd $picflag" $srcdir/$subdir/alias.c $tmpdir/alias.o]
446 || ![ld_link $cc_cmd $tmpdir/alias.so "$shared $tmpdir/alias.o"]
447 || ![ld_compile $cc_cmd $srcdir/$subdir/aliasmain.c $tmpdir/aliasmain.o]
448 || ![ld_compile $cc_cmd $srcdir/$subdir/weakref1.c $tmpdir/weakref1.o]
449 || ![ld_compile $cc_cmd $srcdir/$subdir/weakref2.c $tmpdir/weakref2.o]} then {
450 unsupported "ELF weak (alias)"
451 } else {
452 build_exec "ELF weak (alias)" alias "aliasmain.o weakref1.o weakref2.o alias.so" "-Wl,-rpath=.,--no-as-needed" alias "" ""
455 set CFLAGS_FOR_TARGET "$saved_CFLAGS"