1 # Expect script for ld-version tests
2 # Copyright (C) 1997, 1998, 1999 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 alpha*-*-linux*] } {
43 if { [istarget i?86-*-linuxaout*] \
44 || [istarget i?86-*-linuxoldld*] \
45 || [istarget m68k-*-linuxaout*] } {
49 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
55 set VOBJDUMP_FLAGS --private-headers
56 set DOBJDUMP_FLAGS --dynamic-syms
57 set SOBJDUMP_FLAGS --syms
59 set script --version-script
61 proc test_ar { test lib object expect } {
69 verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
70 catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
71 set exec_output [prune_warnings $exec_output]
72 if ![string match "" $exec_output] {
73 verbose -log "$exec_output"
78 verbose -log "$nm --print-armap $tmpdir/$lib | grep \" in \" | egrep \"VERS\\|bar\\|foo\" | sort > $tmpdir/nm.out"
80 catch "exec $nm --print-armap $tmpdir/$lib | grep \\\ in\\\ | egrep VERS\\\|bar\\\|foo | sort > $tmpdir/nm.out" exec_output
81 if [string match "" $exec_output] then {
82 catch "exec $diff -q $tmpdir/nm.out $srcdir/$subdir/$expect" exec_output
83 set exec_output [prune_warnings $exec_output]
84 if [string match "" $exec_output] then {
88 verbose -log "$exec_output"
93 verbose -log "$exec_output"
99 # objdump_emptysymstuff
100 # Check non-dynamic symbols and make sure there are none with '@'.
102 proc objdump_emptysymstuff { objdump object } {
103 global SOBJDUMP_FLAGS
104 global version_output
107 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
109 verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"
111 catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
112 set exec_output [prune_warnings $exec_output]
113 if [string match "" $exec_output] then {
114 # We shouldn't get anything here.
117 # it is not normal to come here - we have no output to compare.
118 verbose -log "$exec_output"
119 verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
126 # objdump_emptydynsymstuff
127 # Check dynamic symbols and make sure there are none with '@'.
129 proc objdump_emptydynsymstuff { objdump object } {
130 global DOBJDUMP_FLAGS
131 global version_output
134 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
136 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"
138 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
139 set exec_output [prune_warnings $exec_output]
140 if [string match "" $exec_output] then {
141 # We shouldn't get anything here.
143 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
146 # it is not normal to come here - we have no output to compare.
147 verbose -log "$exec_output"
148 verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
154 # objdump_emptyverstuff
155 # Make sure there is no version information
157 proc objdump_emptyverstuff { objdump object } {
158 global VOBJDUMP_FLAGS
159 global version_output
163 if {[which $objdump] == 0} then {
164 perror "$objdump does not exist"
168 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
170 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
172 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
173 set exec_output [prune_warnings $exec_output]
174 if [string match "" $exec_output] then {
175 # it is normal to fail here - we have no output to compare.
177 } else { if { [string match "*libc*" $exec_output] } then {
178 # this probably means that there is version information in libc, so we
179 # can't really perform this test.
182 verbose -log "$exec_output"
183 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
191 # Dump non-dynamic symbol stuff and make sure that it is sane.
193 proc objdump_symstuff { objdump object expectfile } {
194 global SOBJDUMP_FLAGS
195 global version_output
199 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
201 verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out"
203 catch "exec $objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out" exec_output
204 set exec_output [prune_warnings $exec_output]
205 if [string match "" $exec_output] then {
207 # Now do a line-by-line comparison to effectively diff the darned things
208 # The stuff coming from the expectfile is actually a regex, so we can
209 # skip over the actual addresses and so forth. This is currently very
210 # simpleminded - it expects a one-to-one correspondence in terms of line
213 if [file exists $expectfile] then {
214 set file_a [open $expectfile r]
216 perror "$expectfile doesn't exist"
220 if [file exists $tmpdir/objdump.out] then {
221 set file_b [open $tmpdir/objdump.out r]
223 perror "$tmpdir/objdump.out doesn't exist"
227 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
232 while { [gets $file_a line] != $eof } {
233 if [regexp "^#.*$" $line] then {
241 while { [gets $file_b line] != $eof } {
242 if [regexp "^#.*$" $line] then {
250 for { set i 0 } { $i < [llength $list_a] } { incr i } {
251 set line_a [lindex $list_a $i]
252 set line_b [lindex $list_b $i]
255 verbose "\t$expectfile: $i: $line_a" 3
256 verbose "\t/tmp/objdump.out: $i: $line_b" 3
257 if [regexp $line_a $line_b] then {
260 verbose -log "\t$expectfile: $i: $line_a"
261 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
267 if { [llength $list_a] != [llength $list_b] } {
268 verbose -log "Line count"
272 if $differences<1 then {
278 verbose -log "$exec_output"
285 # objdump_dymsymstuff
286 # Dump dynamic symbol stuff and make sure that it is sane.
288 proc objdump_dynsymstuff { objdump object expectfile } {
289 global DOBJDUMP_FLAGS
290 global version_output
294 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
296 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out"
298 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out" exec_output
299 set exec_output [prune_warnings $exec_output]
300 if [string match "" $exec_output] then {
302 # Now do a line-by-line comparison to effectively diff the darned things
303 # The stuff coming from the expectfile is actually a regex, so we can
304 # skip over the actual addresses and so forth. This is currently very
305 # simpleminded - it expects a one-to-one correspondence in terms of line
308 if [file exists $expectfile] then {
309 set file_a [open $expectfile r]
311 warning "$expectfile doesn't exist"
315 if [file exists $tmpdir/objdump.out] then {
316 set file_b [open $tmpdir/objdump.out r]
318 fail "$tmpdir/objdump.out doesn't exist"
322 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
327 while { [gets $file_a line] != $eof } {
328 if [regexp "^#.*$" $line] then {
336 while { [gets $file_b line] != $eof } {
337 if [regexp "^#.*$" $line] then {
345 for { set i 0 } { $i < [llength $list_b] } { incr i } {
346 set line_b [lindex $list_b $i]
348 # The tests are rigged so that we should never export a symbol with the
349 # word 'hide' in it. Thus we just search for it, and bail if we find it.
350 if [regexp "hide" $line_b] then {
351 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
356 verbose "\t$expectfile: $i: $line_b" 3
358 # We can't assume that the sort is consistent across
359 # systems, so we must check each regexp. When we find a
360 # regexp, we null it out, so we don't match it twice.
361 for { set j 0 } { $j < [llength $list_a] } { incr j } {
362 set line_a [lindex $list_a $j]
364 if [regexp $line_a $line_b] then {
365 lreplace $list_a $j $j "CAN NOT MATCH"
370 if { $j >= [llength $list_a] } {
371 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
377 if { [llength $list_a] != [llength $list_b] } {
378 verbose -log "Line count"
382 if $differences<1 then {
388 verbose -log "$exec_output"
395 # objdump_versionstuff
396 # Dump version definitions/references and make sure that it is sane.
398 proc objdump_versionstuff { objdump object expectfile } {
399 global VOBJDUMP_FLAGS
400 global version_output
404 if {[which $objdump] == 0} then {
405 perror "$objdump does not exist"
409 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
411 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
413 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
414 set exec_output [prune_warnings $exec_output]
415 if [string match "" $exec_output] then {
417 # It's OK if there are extra lines in the actual output; they
418 # may come from version information in libc. We require that
419 # every line in EXPECTFILE appear in the output in order.
421 set f1 [open $tmpdir/objdump.out r]
422 set f2 [open $expectfile r]
424 while { [gets $f1 l1] != -1 } {
425 if { [string match $l2 $l1] } then {
426 if { [gets $f2 l2] == -1 } then {
434 # We reached the end of the output without seeing the line we
435 # expected. This is a test failure.
440 verbose -log "Did not find \"$l2\""
441 set f1 [open $tmpdir/objdump.out r]
442 while { [gets $f1 l1] != -1 } {
446 verbose -log "$exec_output"
449 verbose -log "$exec_output"
454 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
468 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
473 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
478 if [string match "" $other] then {
481 set other_lib $tmpdir/$other
484 if [string match "" $mapfile] then {
487 set script_arg "$script $srcdir/$subdir/$mapfile"
490 if {![ld_simple_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg"]} {
495 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
500 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
505 if [string match "" $symexp] then {
506 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
511 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
521 proc test_ldfail { test flag source execname other mapfile whyfail } {
534 if [string match "" $other] then {
537 set other_lib $tmpdir/$other
540 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
545 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
550 verbose -log "This link should fail because of $whyfail"
552 if [string match "" $mapfile] then {
555 set script_arg "$script $srcdir/$subdir/$mapfile"
558 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
565 proc test_asfail { test flag source execname whyfail } {
573 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
578 verbose -log "This assemble should fail because of $whyfail"
579 catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
580 set exec_output [prune_warnings $exec_output]
581 if [string match "" $exec_output] then {
585 verbose -log "$exec_output"
589 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
598 verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
599 exec cp $tmpdir/$srclib $tmpdir/$libname.so
601 verbose -log "$strip $tmpdir/$libname.so"
602 catch "exec $strip $tmpdir/$libname.so" exec_output
603 if [string match "" $exec_output] then {
605 # If strip went OK, then run the usual tests on the thing to make sure that
607 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
612 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
618 verbose -log "$exec_output"
626 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
639 set script --version-script
640 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
645 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
650 if [string match "" $solibname] then {
653 set solibname_lib $tmpdir/$solibname
656 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
661 if [string match "" $verexp] then {
663 # Make sure we get nothing back.
665 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
670 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
676 if [string match "" $versymexp] then {
677 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
682 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
688 if [string match "" $symexp] then {
689 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
694 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
705 # Basic test - build a library with versioned symbols.
707 build_vers_lib "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym
711 # Test #2 - build a library, and link it against the library we built in step
714 build_vers_lib "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""
717 # Test #3 - build an executable, and link it against vers1.so.
719 build_exec "vers3" vers3.c vers3 "" vers1.so vers3.ver vers3.dsym ""
722 # Test #4 - Make sure a version implicitly defined in an executable
723 # causes a version node to be created. Verify this both with and without
727 # This test fails on MIPS. On the MIPS we must put foo in the dynamic
728 # symbol table, which the test does not expect.
729 setup_xfail "mips*-*-*"
730 build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym
732 build_exec "vers4a" vers4.c vers4a "-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym
736 # Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
739 test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"
743 # Now build a test that should reference a bunch of versioned symbols.
744 # All of them should be correctly referenced.
746 build_exec "vers6" vers6.c vers6 "" vers1.so vers6.ver vers6.dsym vers6.sym
749 # Another test to verify that something made local via 'local' is truly not
752 build_vers_lib "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym
754 test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"
758 # This test is designed to verify that we can pass a linker script on the
759 # command line as if it were a normal .o file.
761 catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
762 build_vers_lib "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym
765 # This test tries to make sure that version references to versioned symbols
766 # don't collide with default definitions with the same symbol.
768 build_exec "vers9" vers9.c vers9 "-export-dynamic" "" vers9.ver vers9.dsym vers9.sym
772 # Try and use a non-existant version node. The linker should fail with
775 test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"
778 # Try and some things the assembler should complain about.
780 test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"
782 test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"
785 # Put a shared library in an archive library, and make sure the global
786 # archive symbol table is sane.
788 test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym
791 # Strip a shared library, and make sure we didn't screw something up in there.
793 test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym
797 # Build another test with some versioned symbols. Here we are going to
798 # try and override something from the library, and we shouldn't get
801 build_exec "vers15" vers15.c vers15 "" vers1.so vers15.ver vers15.dsym vers15.sym
804 # Test that when we override a versioned symbol from the library this
805 # symbol appears in the dynamic symbol table of the executable.
807 build_vers_lib "vers16a" vers16a.c vers16a "" vers16.map vers16a.ver vers16a.dsym ""
808 build_exec "vers16" vers16.c vers16 "" vers16a.so "" vers16.dsym ""
810 # Test a weak versioned symbol.
811 build_vers_lib "vers17" vers17.c vers17 "" vers17.map vers17.ver vers17.dsym ""
812 build_vers_lib "vers18" vers18.c vers18 vers17.so vers18.map vers18.ver vers18.dsym vers18.sym
813 build_exec "vers19" vers19.c vers19 "-rpath ." vers18.so vers19.ver vers19.dsym ""