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 hppa*64*-*-hpux*] \
29 && ![istarget hppa*-*-linux*] \
30 && ![istarget i?86-*-sysv4*] \
31 && ![istarget i?86-*-unixware] \
32 && ![istarget i?86-*-elf*] \
33 && ![istarget i?86-*-linux*] \
34 && ![istarget ia64-*-elf*] \
35 && ![istarget ia64-*-linux*] \
36 && ![istarget m68k-*-linux*] \
37 && ![istarget mips*-*-irix5*] \
38 && ![istarget powerpc-*-elf*] \
39 && ![istarget powerpc-*-linux*] \
40 && ![istarget powerpc-*-sysv4*] \
41 && ![istarget sparc*-*-elf] \
42 && ![istarget sparc*-*-solaris2*] \
43 && ![istarget sparc*-*-linux*] \
44 && ![istarget arm*-*-linux*] \
45 && ![istarget mips*-*-linux*] \
46 && ![istarget alpha*-*-linux*] \
47 && ![istarget s390*-*-linux*] \
48 && ![istarget x86_64-*-linux*] } {
52 if { [istarget i?86-*-linux*aout*] \
53 || [istarget i?86-*-linux*oldld*] \
54 || [istarget m68k-*-linux*aout*] } {
58 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
64 set VOBJDUMP_FLAGS --private-headers
65 set DOBJDUMP_FLAGS --dynamic-syms
66 set SOBJDUMP_FLAGS --syms
67 set shared "--shared --no-undefined-version"
68 set script --version-script
70 proc test_ar { test lib object expect } {
78 verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
79 catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
80 set exec_output [prune_warnings $exec_output]
81 if ![string match "" $exec_output] {
82 verbose -log "$exec_output"
87 verbose -log "$nm --print-armap $tmpdir/$lib | grep \" in \" | egrep \"VERS\\|bar\\|foo\" | sort > $tmpdir/nm.out"
89 catch "exec $nm --print-armap $tmpdir/$lib | grep \\\ in\\\ | egrep VERS\\\|bar\\\|foo | sort > $tmpdir/nm.out" exec_output
90 if [string match "" $exec_output] then {
91 catch "exec $diff $tmpdir/nm.out $srcdir/$subdir/$expect" exec_output
92 set exec_output [prune_warnings $exec_output]
93 if [string match "" $exec_output] then {
97 verbose -log "$exec_output"
102 verbose -log "$exec_output"
108 # objdump_emptysymstuff
109 # Check non-dynamic symbols and make sure there are none with '@'.
111 proc objdump_emptysymstuff { objdump object } {
112 global SOBJDUMP_FLAGS
113 global version_output
116 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
118 verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"
120 catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
121 set exec_output [prune_warnings $exec_output]
122 if [string match "" $exec_output] then {
123 # We shouldn't get anything here.
126 # it is not normal to come here - we have no output to compare.
127 verbose -log "$exec_output"
128 verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
135 # objdump_emptydynsymstuff
136 # Check dynamic symbols and make sure there are none with '@'.
138 proc objdump_emptydynsymstuff { objdump object } {
139 global DOBJDUMP_FLAGS
140 global version_output
143 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
145 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"
147 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
148 set exec_output [prune_warnings $exec_output]
149 if [string match "" $exec_output] then {
150 # We shouldn't get anything here.
152 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
155 # it is not normal to come here - we have no output to compare.
156 verbose -log "$exec_output"
157 verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
163 # objdump_emptyverstuff
164 # Make sure there is no version information
166 proc objdump_emptyverstuff { objdump object } {
167 global VOBJDUMP_FLAGS
168 global version_output
172 if {[which $objdump] == 0} then {
173 perror "$objdump does not exist"
177 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
179 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
181 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
182 set exec_output [prune_warnings $exec_output]
183 if [string match "" $exec_output] then {
184 # it is normal to fail here - we have no output to compare.
186 } else { if { [string match "*libc*" $exec_output] } then {
187 # this probably means that there is version information in libc, so we
188 # can't really perform this test.
191 verbose -log "$exec_output"
192 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
200 # Dump non-dynamic symbol stuff and make sure that it is sane.
202 proc objdump_symstuff { objdump object expectfile } {
203 global SOBJDUMP_FLAGS
204 global version_output
208 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
210 verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out"
212 catch "exec $objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out" exec_output
213 set exec_output [prune_warnings $exec_output]
214 if [string match "" $exec_output] then {
216 # Now do a line-by-line comparison to effectively diff the darned things
217 # The stuff coming from the expectfile is actually a regex, so we can
218 # skip over the actual addresses and so forth. This is currently very
219 # simpleminded - it expects a one-to-one correspondence in terms of line
222 if [file exists $expectfile] then {
223 set file_a [open $expectfile r]
225 perror "$expectfile doesn't exist"
229 if [file exists $tmpdir/objdump.out] then {
230 set file_b [open $tmpdir/objdump.out r]
232 perror "$tmpdir/objdump.out doesn't exist"
236 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
241 while { [gets $file_a line] != $eof } {
242 if [regexp "^#.*$" $line] then {
250 while { [gets $file_b line] != $eof } {
251 if [regexp "^#.*$" $line] then {
259 for { set i 0 } { $i < [llength $list_a] } { incr i } {
260 set line_a [lindex $list_a $i]
261 set line_b [lindex $list_b $i]
264 verbose "\t$expectfile: $i: $line_a" 3
265 verbose "\t/tmp/objdump.out: $i: $line_b" 3
266 if [regexp $line_a $line_b] then {
269 verbose -log "\t$expectfile: $i: $line_a"
270 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
276 if { [llength $list_a] != [llength $list_b] } {
277 verbose -log "Line count"
281 if $differences<1 then {
287 verbose -log "$exec_output"
294 # objdump_dymsymstuff
295 # Dump dynamic symbol stuff and make sure that it is sane.
297 proc objdump_dynsymstuff { objdump object expectfile } {
298 global DOBJDUMP_FLAGS
299 global version_output
303 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
305 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out"
307 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out" exec_output
308 set exec_output [prune_warnings $exec_output]
309 if [string match "" $exec_output] then {
311 # Now do a line-by-line comparison to effectively diff the darned things
312 # The stuff coming from the expectfile is actually a regex, so we can
313 # skip over the actual addresses and so forth. This is currently very
314 # simpleminded - it expects a one-to-one correspondence in terms of line
317 if [file exists $expectfile] then {
318 set file_a [open $expectfile r]
320 warning "$expectfile doesn't exist"
324 if [file exists $tmpdir/objdump.out] then {
325 set file_b [open $tmpdir/objdump.out r]
327 fail "$tmpdir/objdump.out doesn't exist"
331 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
336 while { [gets $file_a line] != $eof } {
337 if [regexp "^#.*$" $line] then {
345 while { [gets $file_b line] != $eof } {
346 if [regexp "^#.*$" $line] then {
354 # Support empty files.
355 if { ![info exists list_a] && ![info exists list_b] } then {
359 for { set i 0 } { $i < [llength $list_b] } { incr i } {
360 set line_b [lindex $list_b $i]
362 # The tests are rigged so that we should never export a symbol with the
363 # word 'hide' in it. Thus we just search for it, and bail if we find it.
364 if [regexp "hide" $line_b] then {
365 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
370 verbose "\t$expectfile: $i: $line_b" 3
372 # We can't assume that the sort is consistent across
373 # systems, so we must check each regexp. When we find a
374 # regexp, we null it out, so we don't match it twice.
375 for { set j 0 } { $j < [llength $list_a] } { incr j } {
376 set line_a [lindex $list_a $j]
378 if [regexp $line_a $line_b] then {
379 lreplace $list_a $j $j "CAN NOT MATCH"
384 if { $j >= [llength $list_a] } {
385 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
391 if { [llength $list_a] != [llength $list_b] } {
392 verbose -log "Line count"
396 if $differences<1 then {
402 verbose -log "$exec_output"
409 # objdump_versionstuff
410 # Dump version definitions/references and make sure that it is sane.
412 proc objdump_versionstuff { objdump object expectfile } {
413 global VOBJDUMP_FLAGS
414 global version_output
418 if {[which $objdump] == 0} then {
419 perror "$objdump does not exist"
423 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
425 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
427 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
428 set exec_output [prune_warnings $exec_output]
429 if [string match "" $exec_output] then {
431 # It's OK if there are extra lines in the actual output; they
432 # may come from version information in libc. We require that
433 # every line in EXPECTFILE appear in the output in order.
435 set f1 [open $tmpdir/objdump.out r]
436 set f2 [open $expectfile r]
438 while { [gets $f1 l1] != -1 } {
439 if { [string match $l2 $l1] } then {
440 if { [gets $f2 l2] == -1 } then {
448 # We reached the end of the output without seeing the line we
449 # expected. This is a test failure.
454 # Support empty expected file.
455 if [string match "" $l2] then {
459 verbose -log "Did not find \"$l2\""
460 set f1 [open $tmpdir/objdump.out r]
461 while { [gets $f1 l1] != -1 } {
465 verbose -log "$exec_output"
468 verbose -log "$exec_output"
473 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
487 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
492 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
498 if ![string match "" $other] then {
500 set other_lib "$other_lib $tmpdir/$o"
504 if [string match "" $mapfile] then {
507 set script_arg "$script $srcdir/$subdir/$mapfile"
510 if {![ld_simple_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg"]} {
515 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
520 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
525 if [string match "" $symexp] then {
526 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
531 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
541 proc test_ldfail { test flag source execname other mapfile whyfail } {
554 if [string match "" $other] then {
557 set other_lib $tmpdir/$other
560 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
565 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
570 verbose -log "This link should fail because of $whyfail"
572 if [string match "" $mapfile] then {
575 set script_arg "$script $srcdir/$subdir/$mapfile"
578 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
585 proc test_asfail { test flag source execname whyfail } {
593 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
598 verbose -log "This assemble should fail because of $whyfail"
599 catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
600 set exec_output [prune_warnings $exec_output]
601 if [string match "" $exec_output] then {
605 verbose -log "$exec_output"
609 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
618 verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
619 exec cp $tmpdir/$srclib $tmpdir/$libname.so
621 verbose -log "$strip $tmpdir/$libname.so"
622 catch "exec $strip $tmpdir/$libname.so" exec_output
623 if [string match "" $exec_output] then {
625 # If strip went OK, then run the usual tests on the thing to make sure that
627 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
632 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
638 verbose -log "$exec_output"
646 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
658 set shared "--shared --no-undefined-version"
659 set script --version-script
660 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
665 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
670 if [string match "" $solibname] then {
673 set solibname_lib $tmpdir/$solibname
676 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
681 if [string match "" $verexp] then {
683 # Make sure we get nothing back.
685 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
690 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
696 if [string match "" $versymexp] then {
697 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
702 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
708 if [string match "" $symexp] then {
709 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
714 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
725 # Basic test - build a library with versioned symbols.
727 build_vers_lib "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym
731 # Test #2 - build a library, and link it against the library we built in step
734 if [istarget x86_64-*-linux*] {
735 # x86_64 doesn't like non-pic shared libraries
738 build_vers_lib "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""
742 # Test #3 - build an executable, and link it against vers1.so.
744 build_exec "vers3" vers3.c vers3 "" vers1.so vers3.ver vers3.dsym ""
747 # Test #4 - Make sure a version implicitly defined in an executable
748 # causes a version node to be created. Verify this both with and without
752 # This test fails on MIPS. On the MIPS we must put foo in the dynamic
753 # symbol table, which the test does not expect.
754 setup_xfail "mips*-*-*"
755 build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym
757 build_exec "vers4a" vers4.c vers4a "-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym
761 # Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
764 test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"
768 # Now build a test that should reference a bunch of versioned symbols.
769 # All of them should be correctly referenced.
771 build_exec "vers6" vers6.c vers6 "" vers1.so vers6.ver vers6.dsym vers6.sym
774 # Another test to verify that something made local via 'local' is truly not
777 build_vers_lib "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym
779 test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"
783 # This test is designed to verify that we can pass a linker script on the
784 # command line as if it were a normal .o file.
786 catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
787 build_vers_lib "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym
790 # This test tries to make sure that version references to versioned symbols
791 # don't collide with default definitions with the same symbol.
793 build_exec "vers9" vers9.c vers9 "-export-dynamic" "" vers9.ver vers9.dsym vers9.sym
797 # Try and use a non-existant version node. The linker should fail with
800 test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"
803 # Try and some things the assembler should complain about.
805 test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"
807 test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"
810 # Put a shared library in an archive library, and make sure the global
811 # archive symbol table is sane.
813 test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym
816 # Strip a shared library, and make sure we didn't screw something up in there.
818 test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym
822 # Build another test with some versioned symbols. Here we are going to
823 # try and override something from the library, and we shouldn't get
826 build_exec "vers15" vers15.c vers15 "" vers1.so vers15.ver vers15.dsym vers15.sym
829 # Test that when we override a versioned symbol from the library this
830 # symbol appears in the dynamic symbol table of the executable.
832 build_vers_lib "vers16a" vers16a.c vers16a "" vers16.map vers16a.ver vers16a.dsym ""
833 build_exec "vers16" vers16.c vers16 "" vers16a.so "" vers16.dsym ""
835 # Test a weak versioned symbol.
836 build_vers_lib "vers17" vers17.c vers17 "" vers17.map vers17.ver vers17.dsym ""
837 build_vers_lib "vers18" vers18.c vers18 vers17.so vers18.map vers18.ver vers18.dsym vers18.sym
838 build_exec "vers19" vers19.c vers19 "-rpath ." vers18.so vers19.ver vers19.dsym ""
840 build_vers_lib "vers20a" vers20.c vers20a "" vers20.map vers20a.ver vers20.dsym ""
841 exec cp $tmpdir/vers20a.so $tmpdir/vers20b.so
842 build_vers_lib "vers20" vers20.c vers20 "vers20a.so vers20b.so" vers20.map vers20.ver vers20.dsym ""
844 # Test .symver override.
845 build_vers_lib "vers21" vers21.c vers21 "" vers21.map vers21.ver vers21.dsym vers21.sym
847 # Test moving default definition from one DSO to another.
848 build_vers_lib "vers22a" vers22a.c vers22a "" vers22.map vers22a.ver vers22a.dsym vers22a.sym
849 build_vers_lib "vers22b" vers22b.c vers22b "" vers22.map vers22b.ver vers22b.dsym ""
850 build_vers_lib "vers22" vers22.c vers22 "vers22a.so vers22b.so" "" vers22.ver vers22.dsym ""
852 # Test versioned definitions in different files.
853 build_vers_lib "vers23a" vers23a.c vers23a "" vers23a.map vers23a.ver vers23a.dsym vers23a.sym
854 build_vers_lib "vers23b" vers23b.c vers23b "" vers23b.map vers23b.ver vers23b.dsym ""
855 build_vers_lib "vers23c" vers23b.c vers23c "vers23a.so" vers23b.map vers23c.ver vers23b.dsym ""
856 build_exec "vers23d" vers23.c vers23d "tmpdir/vers23a.so tmpdir/vers23c.so" "" vers23.ver vers23d.dsym ""
857 build_exec "vers23" vers23.c vers23 "tmpdir/vers23a.so tmpdir/vers23b.o tmpdir/vers23b.so" "" vers23.ver vers23.dsym ""
859 # Test .symver x,x@VERS.0
861 if [istarget sparc*-*-*] {
862 set as_pic_flags "-K PIC"
864 run_ld_link_tests [list "\"vers24a\"
865 \"-shared --version-script $srcdir/$subdir/vers24.map\"
866 \"$as_pic_flags\" {vers24a.c vers24b.c} { { readelf -Wrs vers24.rd } }
867 \"libvers24a.so\" \"-fpic\""]
868 run_ld_link_tests [list "\"vers24b\"
869 \"-shared --version-script $srcdir/$subdir/vers24.map\"
870 \"$as_pic_flags\" {vers24b.c vers24a.c} { { readelf -Wrs vers24.rd } }
871 \"libvers24b.so\" \"-fpic\""]
872 run_ld_link_tests [list "\"vers24c\"
873 \"-shared --version-script $srcdir/$subdir/vers24.map\"
874 \"$as_pic_flags\" {vers24c.c} { { readelf -Wrs vers24.rd } }
875 \"libvers24c.so\" \"-fpic\""]
877 # Test versioned definition vs. normal definition in different files.
878 build_vers_lib "vers25a" vers25a.c vers25a "" vers25a.map vers25a.ver vers25a.dsym ""
879 build_vers_lib "vers25b1" vers25b.c vers25b1 "vers25a.o vers25a.so" "" vers25b.ver vers25b.dsym ""
880 build_vers_lib "vers25b2" vers25b.c vers25b2 "vers25a.so vers25a.o" "" vers25b.ver vers25b.dsym ""