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.
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*] } {
47 if { [istarget i?86-*-linux*aout*] \
48 || [istarget i?86-*-linux*oldld*] \
49 || [istarget m68k-*-linux*aout*] } {
53 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
59 set VOBJDUMP_FLAGS --private-headers
60 set DOBJDUMP_FLAGS --dynamic-syms
61 set SOBJDUMP_FLAGS --syms
63 set script --version-script
65 proc test_ar { test lib object expect } {
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"
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 {
92 verbose -log "$exec_output"
97 verbose -log "$exec_output"
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
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.
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"
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
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.
147 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
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"
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
167 if {[which $objdump] == 0} then {
168 perror "$objdump does not exist"
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.
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.
186 verbose -log "$exec_output"
187 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
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
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
217 if [file exists $expectfile] then {
218 set file_a [open $expectfile r]
220 perror "$expectfile doesn't exist"
224 if [file exists $tmpdir/objdump.out] then {
225 set file_b [open $tmpdir/objdump.out r]
227 perror "$tmpdir/objdump.out doesn't exist"
231 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
236 while { [gets $file_a line] != $eof } {
237 if [regexp "^#.*$" $line] then {
245 while { [gets $file_b line] != $eof } {
246 if [regexp "^#.*$" $line] then {
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 {
264 verbose -log "\t$expectfile: $i: $line_a"
265 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
271 if { [llength $list_a] != [llength $list_b] } {
272 verbose -log "Line count"
276 if $differences<1 then {
282 verbose -log "$exec_output"
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
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
312 if [file exists $expectfile] then {
313 set file_a [open $expectfile r]
315 warning "$expectfile doesn't exist"
319 if [file exists $tmpdir/objdump.out] then {
320 set file_b [open $tmpdir/objdump.out r]
322 fail "$tmpdir/objdump.out doesn't exist"
326 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
331 while { [gets $file_a line] != $eof } {
332 if [regexp "^#.*$" $line] then {
340 while { [gets $file_b line] != $eof } {
341 if [regexp "^#.*$" $line] then {
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"
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"
374 if { $j >= [llength $list_a] } {
375 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
381 if { [llength $list_a] != [llength $list_b] } {
382 verbose -log "Line count"
386 if $differences<1 then {
392 verbose -log "$exec_output"
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
408 if {[which $objdump] == 0} then {
409 perror "$objdump does not exist"
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]
428 while { [gets $f1 l1] != -1 } {
429 if { [string match $l2 $l1] } then {
430 if { [gets $f2 l2] == -1 } then {
438 # We reached the end of the output without seeing the line we
439 # expected. This is a test failure.
444 verbose -log "Did not find \"$l2\""
445 set f1 [open $tmpdir/objdump.out r]
446 while { [gets $f1 l1] != -1 } {
450 verbose -log "$exec_output"
453 verbose -log "$exec_output"
458 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
472 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
477 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
482 if [string match "" $other] then {
485 set other_lib $tmpdir/$other
488 if [string match "" $mapfile] then {
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"]} {
499 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
504 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
509 if [string match "" $symexp] then {
510 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
515 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
525 proc test_ldfail { test flag source execname other mapfile whyfail } {
538 if [string match "" $other] then {
541 set other_lib $tmpdir/$other
544 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
549 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
554 verbose -log "This link should fail because of $whyfail"
556 if [string match "" $mapfile] then {
559 set script_arg "$script $srcdir/$subdir/$mapfile"
562 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
569 proc test_asfail { test flag source execname whyfail } {
577 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
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 {
589 verbose -log "$exec_output"
593 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
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
611 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
616 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
622 verbose -log "$exec_output"
630 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
643 set script --version-script
644 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
649 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
654 if [string match "" $solibname] then {
657 set solibname_lib $tmpdir/$solibname
660 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
665 if [string match "" $verexp] then {
667 # Make sure we get nothing back.
669 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
674 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
680 if [string match "" $versymexp] then {
681 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
686 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
692 if [string match "" $symexp] then {
693 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
698 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
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
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
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
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
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
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
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 ." vers18.so vers19.ver vers19.dsym ""