1 # Expect script for ld-version tests
2 # Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation
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 m68k-*-linux*] \
33 && ![istarget mips*-*-irix5*] \
34 && ![istarget powerpc-*-elf*] \
35 && ![istarget powerpc-*-linux*] \
36 && ![istarget powerpc-*-sysv4*] \
37 && ![istarget sparc*-*-elf] \
38 && ![istarget sparc*-*-solaris2*] \
39 && ![istarget sparc*-*-linux*] \
40 && ![istarget arm*-*-linux*] \
41 && ![istarget alpha*-*-linux*] } {
45 if { [istarget i?86-*-linuxaout*] \
46 || [istarget i?86-*-linuxoldld*] \
47 || [istarget m68k-*-linuxaout*] } {
51 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
57 set VOBJDUMP_FLAGS --private-headers
58 set DOBJDUMP_FLAGS --dynamic-syms
59 set SOBJDUMP_FLAGS --syms
61 set script --version-script
63 proc test_ar { test lib object expect } {
71 verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
72 catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
73 set exec_output [prune_warnings $exec_output]
74 if ![string match "" $exec_output] {
75 verbose -log "$exec_output"
80 verbose -log "$nm --print-armap $tmpdir/$lib | grep \" in \" | egrep \"VERS\\|bar\\|foo\" | sort > $tmpdir/nm.out"
82 catch "exec $nm --print-armap $tmpdir/$lib | grep \\\ in\\\ | egrep VERS\\\|bar\\\|foo | sort > $tmpdir/nm.out" exec_output
83 if [string match "" $exec_output] then {
84 catch "exec $diff -q $tmpdir/nm.out $srcdir/$subdir/$expect" exec_output
85 set exec_output [prune_warnings $exec_output]
86 if [string match "" $exec_output] then {
90 verbose -log "$exec_output"
95 verbose -log "$exec_output"
101 # objdump_emptysymstuff
102 # Check non-dynamic symbols and make sure there are none with '@'.
104 proc objdump_emptysymstuff { objdump object } {
105 global SOBJDUMP_FLAGS
106 global version_output
109 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
111 verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"
113 catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
114 set exec_output [prune_warnings $exec_output]
115 if [string match "" $exec_output] then {
116 # We shouldn't get anything here.
119 # it is not normal to come here - we have no output to compare.
120 verbose -log "$exec_output"
121 verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
128 # objdump_emptydynsymstuff
129 # Check dynamic symbols and make sure there are none with '@'.
131 proc objdump_emptydynsymstuff { objdump object } {
132 global DOBJDUMP_FLAGS
133 global version_output
136 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
138 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"
140 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
141 set exec_output [prune_warnings $exec_output]
142 if [string match "" $exec_output] then {
143 # We shouldn't get anything here.
145 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
148 # it is not normal to come here - we have no output to compare.
149 verbose -log "$exec_output"
150 verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
156 # objdump_emptyverstuff
157 # Make sure there is no version information
159 proc objdump_emptyverstuff { objdump object } {
160 global VOBJDUMP_FLAGS
161 global version_output
165 if {[which $objdump] == 0} then {
166 perror "$objdump does not exist"
170 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
172 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
174 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
175 set exec_output [prune_warnings $exec_output]
176 if [string match "" $exec_output] then {
177 # it is normal to fail here - we have no output to compare.
179 } else { if { [string match "*libc*" $exec_output] } then {
180 # this probably means that there is version information in libc, so we
181 # can't really perform this test.
184 verbose -log "$exec_output"
185 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
193 # Dump non-dynamic symbol stuff and make sure that it is sane.
195 proc objdump_symstuff { objdump object expectfile } {
196 global SOBJDUMP_FLAGS
197 global version_output
201 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
203 verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out"
205 catch "exec $objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out" exec_output
206 set exec_output [prune_warnings $exec_output]
207 if [string match "" $exec_output] then {
209 # Now do a line-by-line comparison to effectively diff the darned things
210 # The stuff coming from the expectfile is actually a regex, so we can
211 # skip over the actual addresses and so forth. This is currently very
212 # simpleminded - it expects a one-to-one correspondence in terms of line
215 if [file exists $expectfile] then {
216 set file_a [open $expectfile r]
218 perror "$expectfile doesn't exist"
222 if [file exists $tmpdir/objdump.out] then {
223 set file_b [open $tmpdir/objdump.out r]
225 perror "$tmpdir/objdump.out doesn't exist"
229 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
234 while { [gets $file_a line] != $eof } {
235 if [regexp "^#.*$" $line] then {
243 while { [gets $file_b line] != $eof } {
244 if [regexp "^#.*$" $line] then {
252 for { set i 0 } { $i < [llength $list_a] } { incr i } {
253 set line_a [lindex $list_a $i]
254 set line_b [lindex $list_b $i]
257 verbose "\t$expectfile: $i: $line_a" 3
258 verbose "\t/tmp/objdump.out: $i: $line_b" 3
259 if [regexp $line_a $line_b] then {
262 verbose -log "\t$expectfile: $i: $line_a"
263 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
269 if { [llength $list_a] != [llength $list_b] } {
270 verbose -log "Line count"
274 if $differences<1 then {
280 verbose -log "$exec_output"
287 # objdump_dymsymstuff
288 # Dump dynamic symbol stuff and make sure that it is sane.
290 proc objdump_dynsymstuff { objdump object expectfile } {
291 global DOBJDUMP_FLAGS
292 global version_output
296 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
298 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out"
300 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out" exec_output
301 set exec_output [prune_warnings $exec_output]
302 if [string match "" $exec_output] then {
304 # Now do a line-by-line comparison to effectively diff the darned things
305 # The stuff coming from the expectfile is actually a regex, so we can
306 # skip over the actual addresses and so forth. This is currently very
307 # simpleminded - it expects a one-to-one correspondence in terms of line
310 if [file exists $expectfile] then {
311 set file_a [open $expectfile r]
313 warning "$expectfile doesn't exist"
317 if [file exists $tmpdir/objdump.out] then {
318 set file_b [open $tmpdir/objdump.out r]
320 fail "$tmpdir/objdump.out doesn't exist"
324 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
329 while { [gets $file_a line] != $eof } {
330 if [regexp "^#.*$" $line] then {
338 while { [gets $file_b line] != $eof } {
339 if [regexp "^#.*$" $line] then {
347 for { set i 0 } { $i < [llength $list_b] } { incr i } {
348 set line_b [lindex $list_b $i]
350 # The tests are rigged so that we should never export a symbol with the
351 # word 'hide' in it. Thus we just search for it, and bail if we find it.
352 if [regexp "hide" $line_b] then {
353 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
358 verbose "\t$expectfile: $i: $line_b" 3
360 # We can't assume that the sort is consistent across
361 # systems, so we must check each regexp. When we find a
362 # regexp, we null it out, so we don't match it twice.
363 for { set j 0 } { $j < [llength $list_a] } { incr j } {
364 set line_a [lindex $list_a $j]
366 if [regexp $line_a $line_b] then {
367 lreplace $list_a $j $j "CAN NOT MATCH"
372 if { $j >= [llength $list_a] } {
373 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
379 if { [llength $list_a] != [llength $list_b] } {
380 verbose -log "Line count"
384 if $differences<1 then {
390 verbose -log "$exec_output"
397 # objdump_versionstuff
398 # Dump version definitions/references and make sure that it is sane.
400 proc objdump_versionstuff { objdump object expectfile } {
401 global VOBJDUMP_FLAGS
402 global version_output
406 if {[which $objdump] == 0} then {
407 perror "$objdump does not exist"
411 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
413 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
415 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
416 set exec_output [prune_warnings $exec_output]
417 if [string match "" $exec_output] then {
419 # It's OK if there are extra lines in the actual output; they
420 # may come from version information in libc. We require that
421 # every line in EXPECTFILE appear in the output in order.
423 set f1 [open $tmpdir/objdump.out r]
424 set f2 [open $expectfile r]
426 while { [gets $f1 l1] != -1 } {
427 if { [string match $l2 $l1] } then {
428 if { [gets $f2 l2] == -1 } then {
436 # We reached the end of the output without seeing the line we
437 # expected. This is a test failure.
442 verbose -log "Did not find \"$l2\""
443 set f1 [open $tmpdir/objdump.out r]
444 while { [gets $f1 l1] != -1 } {
448 verbose -log "$exec_output"
451 verbose -log "$exec_output"
456 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
470 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
475 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
480 if [string match "" $other] then {
483 set other_lib $tmpdir/$other
486 if [string match "" $mapfile] then {
489 set script_arg "$script $srcdir/$subdir/$mapfile"
492 if {![ld_simple_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg"]} {
497 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
502 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
507 if [string match "" $symexp] then {
508 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
513 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
523 proc test_ldfail { test flag source execname other mapfile whyfail } {
536 if [string match "" $other] then {
539 set other_lib $tmpdir/$other
542 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
547 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
552 verbose -log "This link should fail because of $whyfail"
554 if [string match "" $mapfile] then {
557 set script_arg "$script $srcdir/$subdir/$mapfile"
560 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
567 proc test_asfail { test flag source execname whyfail } {
575 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
580 verbose -log "This assemble should fail because of $whyfail"
581 catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
582 set exec_output [prune_warnings $exec_output]
583 if [string match "" $exec_output] then {
587 verbose -log "$exec_output"
591 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
600 verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
601 exec cp $tmpdir/$srclib $tmpdir/$libname.so
603 verbose -log "$strip $tmpdir/$libname.so"
604 catch "exec $strip $tmpdir/$libname.so" exec_output
605 if [string match "" $exec_output] then {
607 # If strip went OK, then run the usual tests on the thing to make sure that
609 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
614 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
620 verbose -log "$exec_output"
628 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
641 set script --version-script
642 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
647 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
652 if [string match "" $solibname] then {
655 set solibname_lib $tmpdir/$solibname
658 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
663 if [string match "" $verexp] then {
665 # Make sure we get nothing back.
667 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
672 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
678 if [string match "" $versymexp] then {
679 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
684 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
690 if [string match "" $symexp] then {
691 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
696 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
707 # Basic test - build a library with versioned symbols.
709 build_vers_lib "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym
713 # Test #2 - build a library, and link it against the library we built in step
716 build_vers_lib "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""
719 # Test #3 - build an executable, and link it against vers1.so.
721 build_exec "vers3" vers3.c vers3 "" vers1.so vers3.ver vers3.dsym ""
724 # Test #4 - Make sure a version implicitly defined in an executable
725 # causes a version node to be created. Verify this both with and without
729 # This test fails on MIPS. On the MIPS we must put foo in the dynamic
730 # symbol table, which the test does not expect.
731 setup_xfail "mips*-*-*"
732 build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym
734 build_exec "vers4a" vers4.c vers4a "-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym
738 # Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
741 test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"
745 # Now build a test that should reference a bunch of versioned symbols.
746 # All of them should be correctly referenced.
748 build_exec "vers6" vers6.c vers6 "" vers1.so vers6.ver vers6.dsym vers6.sym
751 # Another test to verify that something made local via 'local' is truly not
754 build_vers_lib "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym
756 test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"
760 # This test is designed to verify that we can pass a linker script on the
761 # command line as if it were a normal .o file.
763 catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
764 build_vers_lib "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym
767 # This test tries to make sure that version references to versioned symbols
768 # don't collide with default definitions with the same symbol.
770 build_exec "vers9" vers9.c vers9 "-export-dynamic" "" vers9.ver vers9.dsym vers9.sym
774 # Try and use a non-existant version node. The linker should fail with
777 test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"
780 # Try and some things the assembler should complain about.
782 test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"
784 test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"
787 # Put a shared library in an archive library, and make sure the global
788 # archive symbol table is sane.
790 test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym
793 # Strip a shared library, and make sure we didn't screw something up in there.
795 test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym
799 # Build another test with some versioned symbols. Here we are going to
800 # try and override something from the library, and we shouldn't get
803 build_exec "vers15" vers15.c vers15 "" vers1.so vers15.ver vers15.dsym vers15.sym
806 # Test that when we override a versioned symbol from the library this
807 # symbol appears in the dynamic symbol table of the executable.
809 build_vers_lib "vers16a" vers16a.c vers16a "" vers16.map vers16a.ver vers16a.dsym ""
810 build_exec "vers16" vers16.c vers16 "" vers16a.so "" vers16.dsym ""
812 # Test a weak versioned symbol.
813 build_vers_lib "vers17" vers17.c vers17 "" vers17.map vers17.ver vers17.dsym ""
814 build_vers_lib "vers18" vers18.c vers18 vers17.so vers18.map vers18.ver vers18.dsym vers18.sym
815 build_exec "vers19" vers19.c vers19 "-rpath ." vers18.so vers19.ver vers19.dsym ""