2001-05-17 H.J. Lu <hjl@gnu.org>
[binutils.git] / ld / testsuite / ld-elfvers / vers.exp
blobe300e0373165cb3af003e76c748d49f513d9327c
1 # Expect script for ld-version tests
2 # Copyright 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
4 # This file is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Written by Eric Youngdale (eric@andante.jic.com)
22 # This test can only be run if ld generates native executables.
23 if ![isnative] then {return}
25 # This test can only be run on a couple of ELF platforms.
26 # Square bracket expressions seem to confuse istarget.
27 # This is similar to the test that is used in ld-shared, BTW.
28 if { ![istarget i?86-*-sysv4*] \
29 && ![istarget i?86-*-unixware] \
30 && ![istarget i?86-*-elf*] \
31 && ![istarget i?86-*-linux*] \
32 && ![istarget ia64-*-elf*] \
33 && ![istarget ia64-*-linux*] \
34 && ![istarget m68k-*-linux*] \
35 && ![istarget mips*-*-irix5*] \
36 && ![istarget powerpc-*-elf*] \
37 && ![istarget powerpc-*-linux*] \
38 && ![istarget powerpc-*-sysv4*] \
39 && ![istarget sparc*-*-elf] \
40 && ![istarget sparc*-*-solaris2*] \
41 && ![istarget sparc*-*-linux*] \
42 && ![istarget arm*-*-linux*] \
43 && ![istarget alpha*-*-linux*] } {
44 return
47 if { [istarget i?86-*-linuxaout*] \
48 || [istarget i?86-*-linuxoldld*] \
49 || [istarget m68k-*-linuxaout*] } {
50 return
53 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
54 return
57 set diff diff
58 set tmpdir tmpdir
59 set VOBJDUMP_FLAGS --private-headers
60 set DOBJDUMP_FLAGS --dynamic-syms
61 set SOBJDUMP_FLAGS --syms
62 set shared --shared
63 set script --version-script
65 proc test_ar { test lib object expect } {
66 global ar
67 global nm
68 global tmpdir
69 global srcdir
70 global subdir
71 global diff
73 verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
74 catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
75 set exec_output [prune_warnings $exec_output]
76 if ![string match "" $exec_output] {
77 verbose -log "$exec_output"
78 unresolved "$test"
79 return
82 verbose -log "$nm --print-armap $tmpdir/$lib | grep \" in \" | egrep \"VERS\\|bar\\|foo\" | sort > $tmpdir/nm.out"
84 catch "exec $nm --print-armap $tmpdir/$lib | grep \\\ in\\\ | egrep VERS\\\|bar\\\|foo | sort > $tmpdir/nm.out" exec_output
85 if [string match "" $exec_output] then {
86 catch "exec $diff -q $tmpdir/nm.out $srcdir/$subdir/$expect" exec_output
87 set exec_output [prune_warnings $exec_output]
88 if [string match "" $exec_output] then {
89 pass $test
90 return
91 } else {
92 verbose -log "$exec_output"
93 fail "$test"
94 return
96 } else {
97 verbose -log "$exec_output"
98 fail "$test"
103 # objdump_emptysymstuff
104 # Check non-dynamic symbols and make sure there are none with '@'.
106 proc objdump_emptysymstuff { objdump object } {
107 global SOBJDUMP_FLAGS
108 global version_output
109 global diff
111 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
113 verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"
115 catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
116 set exec_output [prune_warnings $exec_output]
117 if [string match "" $exec_output] then {
118 # We shouldn't get anything here.
119 return 1
120 } else {
121 # it is not normal to come here - we have no output to compare.
122 verbose -log "$exec_output"
123 verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
124 return 0
130 # objdump_emptydynsymstuff
131 # Check dynamic symbols and make sure there are none with '@'.
133 proc objdump_emptydynsymstuff { objdump object } {
134 global DOBJDUMP_FLAGS
135 global version_output
136 global diff
138 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
140 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"
142 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
143 set exec_output [prune_warnings $exec_output]
144 if [string match "" $exec_output] then {
145 # We shouldn't get anything here.
146 return 1
147 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
148 return 1
149 } else {
150 # it is not normal to come here - we have no output to compare.
151 verbose -log "$exec_output"
152 verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
153 return 0
158 # objdump_emptyverstuff
159 # Make sure there is no version information
161 proc objdump_emptyverstuff { objdump object } {
162 global VOBJDUMP_FLAGS
163 global version_output
164 global diff
165 global tmpdir
167 if {[which $objdump] == 0} then {
168 perror "$objdump does not exist"
169 return 0
172 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
174 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
176 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
177 set exec_output [prune_warnings $exec_output]
178 if [string match "" $exec_output] then {
179 # it is normal to fail here - we have no output to compare.
180 return 1
181 } else { if { [string match "*libc*" $exec_output] } then {
182 # this probably means that there is version information in libc, so we
183 # can't really perform this test.
184 return 1
185 } else {
186 verbose -log "$exec_output"
187 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
188 return 0
194 # objdump_symstuff
195 # Dump non-dynamic symbol stuff and make sure that it is sane.
197 proc objdump_symstuff { objdump object expectfile } {
198 global SOBJDUMP_FLAGS
199 global version_output
200 global diff
201 global tmpdir
203 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
205 verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out"
207 catch "exec $objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out" exec_output
208 set exec_output [prune_warnings $exec_output]
209 if [string match "" $exec_output] then {
211 # Now do a line-by-line comparison to effectively diff the darned things
212 # The stuff coming from the expectfile is actually a regex, so we can
213 # skip over the actual addresses and so forth. This is currently very
214 # simpleminded - it expects a one-to-one correspondence in terms of line
215 # numbers.
217 if [file exists $expectfile] then {
218 set file_a [open $expectfile r]
219 } else {
220 perror "$expectfile doesn't exist"
221 return 0
224 if [file exists $tmpdir/objdump.out] then {
225 set file_b [open $tmpdir/objdump.out r]
226 } else {
227 perror "$tmpdir/objdump.out doesn't exist"
228 return 0
231 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
233 set eof -1
234 set differences 0
236 while { [gets $file_a line] != $eof } {
237 if [regexp "^#.*$" $line] then {
238 continue
239 } else {
240 lappend list_a $line
243 close $file_a
245 while { [gets $file_b line] != $eof } {
246 if [regexp "^#.*$" $line] then {
247 continue
248 } else {
249 lappend list_b $line
252 close $file_b
254 for { set i 0 } { $i < [llength $list_a] } { incr i } {
255 set line_a [lindex $list_a $i]
256 set line_b [lindex $list_b $i]
259 verbose "\t$expectfile: $i: $line_a" 3
260 verbose "\t/tmp/objdump.out: $i: $line_b" 3
261 if [regexp $line_a $line_b] then {
262 continue
263 } else {
264 verbose -log "\t$expectfile: $i: $line_a"
265 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
267 return 0
271 if { [llength $list_a] != [llength $list_b] } {
272 verbose -log "Line count"
273 return 0
276 if $differences<1 then {
277 return 1
280 return 0
281 } else {
282 verbose -log "$exec_output"
283 return 0
289 # objdump_dymsymstuff
290 # Dump dynamic symbol stuff and make sure that it is sane.
292 proc objdump_dynsymstuff { objdump object expectfile } {
293 global DOBJDUMP_FLAGS
294 global version_output
295 global diff
296 global tmpdir
298 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
300 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out"
302 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out" exec_output
303 set exec_output [prune_warnings $exec_output]
304 if [string match "" $exec_output] then {
306 # Now do a line-by-line comparison to effectively diff the darned things
307 # The stuff coming from the expectfile is actually a regex, so we can
308 # skip over the actual addresses and so forth. This is currently very
309 # simpleminded - it expects a one-to-one correspondence in terms of line
310 # numbers.
312 if [file exists $expectfile] then {
313 set file_a [open $expectfile r]
314 } else {
315 warning "$expectfile doesn't exist"
316 return 0
319 if [file exists $tmpdir/objdump.out] then {
320 set file_b [open $tmpdir/objdump.out r]
321 } else {
322 fail "$tmpdir/objdump.out doesn't exist"
323 return 0
326 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
328 set eof -1
329 set differences 0
331 while { [gets $file_a line] != $eof } {
332 if [regexp "^#.*$" $line] then {
333 continue
334 } else {
335 lappend list_a $line
338 close $file_a
340 while { [gets $file_b line] != $eof } {
341 if [regexp "^#.*$" $line] then {
342 continue
343 } else {
344 lappend list_b $line
347 close $file_b
349 for { set i 0 } { $i < [llength $list_b] } { incr i } {
350 set line_b [lindex $list_b $i]
352 # The tests are rigged so that we should never export a symbol with the
353 # word 'hide' in it. Thus we just search for it, and bail if we find it.
354 if [regexp "hide" $line_b] then {
355 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
357 return 0
360 verbose "\t$expectfile: $i: $line_b" 3
362 # We can't assume that the sort is consistent across
363 # systems, so we must check each regexp. When we find a
364 # regexp, we null it out, so we don't match it twice.
365 for { set j 0 } { $j < [llength $list_a] } { incr j } {
366 set line_a [lindex $list_a $j]
368 if [regexp $line_a $line_b] then {
369 lreplace $list_a $j $j "CAN NOT MATCH"
370 break
374 if { $j >= [llength $list_a] } {
375 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
377 return 0
381 if { [llength $list_a] != [llength $list_b] } {
382 verbose -log "Line count"
383 return 0
386 if $differences<1 then {
387 return 1
390 return 0
391 } else {
392 verbose -log "$exec_output"
393 return 0
399 # objdump_versionstuff
400 # Dump version definitions/references and make sure that it is sane.
402 proc objdump_versionstuff { objdump object expectfile } {
403 global VOBJDUMP_FLAGS
404 global version_output
405 global diff
406 global tmpdir
408 if {[which $objdump] == 0} then {
409 perror "$objdump does not exist"
410 return 0
413 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
415 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
417 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
418 set exec_output [prune_warnings $exec_output]
419 if [string match "" $exec_output] then {
421 # It's OK if there are extra lines in the actual output; they
422 # may come from version information in libc. We require that
423 # every line in EXPECTFILE appear in the output in order.
425 set f1 [open $tmpdir/objdump.out r]
426 set f2 [open $expectfile r]
427 gets $f2 l2
428 while { [gets $f1 l1] != -1 } {
429 if { [string match $l2 $l1] } then {
430 if { [gets $f2 l2] == -1 } then {
431 close $f1
432 close $f2
433 return 1
438 # We reached the end of the output without seeing the line we
439 # expected. This is a test failure.
441 close $f1
442 close $f2
444 verbose -log "Did not find \"$l2\""
445 set f1 [open $tmpdir/objdump.out r]
446 while { [gets $f1 l1] != -1 } {
447 verbose -log $l1
450 verbose -log "$exec_output"
451 return 0
452 } else {
453 verbose -log "$exec_output"
454 return 0
458 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
459 global ld
460 global srcdir
461 global subdir
462 global exec_output
463 global host_triplet
464 global tmpdir
465 global as
466 global objdump
467 global CC
468 global CFLAGS
469 global shared
470 global script
472 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
473 unresolved "$test"
474 return
477 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
478 unresolved "$test"
479 return
482 if [string match "" $other] then {
483 set other_lib ""
484 } else {
485 set other_lib $tmpdir/$other
488 if [string match "" $mapfile] then {
489 set script_arg ""
490 } else {
491 set script_arg "$script $srcdir/$subdir/$mapfile"
494 if {![ld_simple_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg"]} {
495 fail "$test"
496 return
499 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
500 fail "$test"
501 return
504 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
505 fail "$test"
506 return
509 if [string match "" $symexp] then {
510 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
511 fail "$test"
512 return
514 } else {
515 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
516 fail "$test"
517 return
521 pass $test
525 proc test_ldfail { test flag source execname other mapfile whyfail } {
526 global ld
527 global srcdir
528 global subdir
529 global exec_output
530 global host_triplet
531 global tmpdir
532 global as
533 global objdump
534 global CC
535 global CFLAGS
536 global script
538 if [string match "" $other] then {
539 set other_lib ""
540 } else {
541 set other_lib $tmpdir/$other
544 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
545 unresolved "$test"
546 return
549 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
550 unresolved "$test"
551 return
554 verbose -log "This link should fail because of $whyfail"
556 if [string match "" $mapfile] then {
557 set script_arg ""
558 } else {
559 set script_arg "$script $srcdir/$subdir/$mapfile"
562 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
563 pass "$test"
564 return
566 fail "$test"
569 proc test_asfail { test flag source execname whyfail } {
570 global srcdir
571 global subdir
572 global tmpdir
573 global as
574 global CC
575 global CFLAGS
577 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
578 unresolved "$test"
579 return
582 verbose -log "This assemble should fail because of $whyfail"
583 catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
584 set exec_output [prune_warnings $exec_output]
585 if [string match "" $exec_output] then {
586 fail "$test"
587 return
589 verbose -log "$exec_output"
590 pass "$test"
593 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
594 global strip
595 global srcdir
596 global subdir
597 global exec_output
598 global host_triplet
599 global tmpdir
600 global objdump
602 verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
603 exec cp $tmpdir/$srclib $tmpdir/$libname.so
605 verbose -log "$strip $tmpdir/$libname.so"
606 catch "exec $strip $tmpdir/$libname.so" exec_output
607 if [string match "" $exec_output] then {
609 # If strip went OK, then run the usual tests on the thing to make sure that
610 # it is sane.
611 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
612 fail "$test"
613 return
616 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
617 fail "$test"
618 return
621 } else {
622 verbose -log "$exec_output"
623 fail "$test"
624 return
626 pass $test
630 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
631 global ld
632 global srcdir
633 global subdir
634 global exec_output
635 global host_triplet
636 global tmpdir
637 global as
638 global objdump
639 global CC
640 global CFLAGS
642 set shared --shared
643 set script --version-script
644 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
645 unresolved "$test"
646 return
649 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
650 unresolved "$test"
651 return
654 if [string match "" $solibname] then {
655 set solibname_lib ""
656 } else {
657 set solibname_lib $tmpdir/$solibname
660 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
661 fail "$test"
662 return
665 if [string match "" $verexp] then {
667 # Make sure we get nothing back.
669 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
670 fail "$test"
671 return
673 } else {
674 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
675 fail "$test"
676 return
680 if [string match "" $versymexp] then {
681 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
682 fail "$test"
683 return
685 } else {
686 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
687 fail "$test"
688 return
692 if [string match "" $symexp] then {
693 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
694 fail "$test"
695 return
697 } else {
698 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
699 fail "$test"
700 return
704 pass $test
709 # Basic test - build a library with versioned symbols.
711 build_vers_lib "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym
715 # Test #2 - build a library, and link it against the library we built in step
716 # 1.
718 build_vers_lib "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""
721 # Test #3 - build an executable, and link it against vers1.so.
723 build_exec "vers3" vers3.c vers3 "" vers1.so vers3.ver vers3.dsym ""
726 # Test #4 - Make sure a version implicitly defined in an executable
727 # causes a version node to be created. Verify this both with and without
728 # --export-dynamic.
731 # This test fails on MIPS. On the MIPS we must put foo in the dynamic
732 # symbol table, which the test does not expect.
733 setup_xfail "mips*-*-*"
734 build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym
736 build_exec "vers4a" vers4.c vers4a "-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym
740 # Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
741 # complains.
743 test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"
747 # Now build a test that should reference a bunch of versioned symbols.
748 # All of them should be correctly referenced.
750 build_exec "vers6" vers6.c vers6 "" vers1.so vers6.ver vers6.dsym vers6.sym
753 # Another test to verify that something made local via 'local' is truly not
754 # accessible.
756 build_vers_lib "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym
758 test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"
762 # This test is designed to verify that we can pass a linker script on the
763 # command line as if it were a normal .o file.
765 catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
766 build_vers_lib "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym
769 # This test tries to make sure that version references to versioned symbols
770 # don't collide with default definitions with the same symbol.
772 build_exec "vers9" vers9.c vers9 "-export-dynamic" "" vers9.ver vers9.dsym vers9.sym
776 # Try and use a non-existant version node. The linker should fail with
777 # an error message.
779 test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"
782 # Try and some things the assembler should complain about.
784 test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"
786 test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"
789 # Put a shared library in an archive library, and make sure the global
790 # archive symbol table is sane.
792 test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym
795 # Strip a shared library, and make sure we didn't screw something up in there.
797 test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym
801 # Build another test with some versioned symbols. Here we are going to
802 # try and override something from the library, and we shouldn't get
803 # any errors.
805 build_exec "vers15" vers15.c vers15 "" vers1.so vers15.ver vers15.dsym vers15.sym
808 # Test that when we override a versioned symbol from the library this
809 # symbol appears in the dynamic symbol table of the executable.
811 build_vers_lib "vers16a" vers16a.c vers16a "" vers16.map vers16a.ver vers16a.dsym ""
812 build_exec "vers16" vers16.c vers16 "" vers16a.so "" vers16.dsym ""
814 # Test a weak versioned symbol.
815 build_vers_lib "vers17" vers17.c vers17 "" vers17.map vers17.ver vers17.dsym ""
816 build_vers_lib "vers18" vers18.c vers18 vers17.so vers18.map vers18.ver vers18.dsym vers18.sym
817 build_exec "vers19" vers19.c vers19 "-rpath .:$tmpdir" vers18.so vers19.ver vers19.dsym ""