1 # Expect script for ld-version tests
2 # Copyright (C) 1997 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 the same test that is used in ld-shared, BTW.
28 if { ![istarget i386-*-sysv4*] \
29 && ![istarget i486-*-sysv4*] \
30 && ![istarget i586-*-sysv4*] \
31 && ![istarget i386-*-unixware] \
32 && ![istarget i486-*-unixware] \
33 && ![istarget i586-*-unixware] \
34 && ![istarget i386-*-elf*] \
35 && ![istarget i486-*-elf*] \
36 && ![istarget i586-*-elf*] \
37 && ![istarget i386-*-linux*] \
38 && ![istarget i486-*-linux*] \
39 && ![istarget i586-*-linux*] \
40 && ![istarget m68k-*-linux*] \
41 && ![istarget mips*-*-irix5*] \
42 && ![istarget sparc*-*-elf] \
43 && ![istarget sparc*-*-solaris2*] \
44 && ![istarget sparc*-*-sunos4*] \
45 && ![istarget rs6000*-*-aix*] \
46 && ![istarget powerpc*-*-aix*] } {
50 if { [istarget i386-*-linuxaout*] \
51 || [istarget i486-*-linuxaout*] \
52 || [istarget i586-*-linuxaout*] \
53 || [istarget i386-*-linuxoldld*] \
54 || [istarget i486-*-linuxoldld*] \
55 || [istarget i586-*-linuxoldld*] \
56 || [istarget m68k-*-linuxaout*] } {
60 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
66 set VOBJDUMP_FLAGS --private-headers
67 set DOBJDUMP_FLAGS --dynamic-syms
68 set SOBJDUMP_FLAGS --syms
70 set script --version-script
72 proc test_ar { test lib object expect } {
80 verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
81 catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
82 set exec_output [prune_warnings $exec_output]
83 if ![string match "" $exec_output] {
84 verbose -log "$exec_output"
89 verbose -log "$nm --print-armap $tmpdir/$lib | grep \" in \" | grep \"VERS\\|bar\\|foo\" | sort > $tmpdir/nm.out"
91 catch "exec $nm --print-armap $tmpdir/$lib | grep \\\ in\\\ | grep VERS\\\\|bar\\\\|foo | sort > $tmpdir/nm.out" exec_output
92 if [string match "" $exec_output] then {
93 catch "exec $diff -q $tmpdir/nm.out $srcdir/$subdir/$expect" exec_output
94 set exec_output [prune_warnings $exec_output]
95 if [string match "" $exec_output] then {
99 verbose -log "$exec_output"
104 verbose -log "$exec_output"
110 # objdump_emptysymstuff
111 # Check non-dynamic symbols and make sure there are none with '@'.
113 proc objdump_emptysymstuff { objdump object } {
114 global SOBJDUMP_FLAGS
115 global version_output
118 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
120 verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"
122 catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
123 set exec_output [prune_warnings $exec_output]
124 if [string match "" $exec_output] then {
125 # We shouldn't get anything here.
128 # it is not normal to come here - we have no output to compare.
129 verbose -log "$exec_output"
130 verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
137 # objdump_emptydynsymstuff
138 # Check dynamic symbols and make sure there are none with '@'.
140 proc objdump_emptydynsymstuff { objdump object } {
141 global DOBJDUMP_FLAGS
142 global version_output
145 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
147 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"
149 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
150 set exec_output [prune_warnings $exec_output]
151 if [string match "" $exec_output] then {
152 # We shouldn't get anything here.
154 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
157 # it is not normal to come here - we have no output to compare.
158 verbose -log "$exec_output"
159 verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
165 # objdump_emptyverstuff
166 # Make sure there is no version information
168 proc objdump_emptyverstuff { objdump object } {
169 global VOBJDUMP_FLAGS
170 global version_output
174 if {[which $objdump] == 0} then {
175 perror "$objdump does not exist"
179 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
181 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
183 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
184 set exec_output [prune_warnings $exec_output]
185 if [string match "" $exec_output] then {
186 # it is normal to fail here - we have no output to compare.
189 verbose -log "$exec_output"
190 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
198 # Dump non-dynamic symbol stuff and make sure that it is sane.
200 proc objdump_symstuff { objdump object expectfile } {
201 global SOBJDUMP_FLAGS
202 global version_output
206 if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
208 verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out"
210 catch "exec $objdump $SOBJDUMP_FLAGS $object | grep \@ | sort > $tmpdir/objdump.out" exec_output
211 set exec_output [prune_warnings $exec_output]
212 if [string match "" $exec_output] then {
214 # Now do a line-by-line comparison to effectively diff the darned things
215 # The stuff coming from the expectfile is actually a regex, so we can
216 # skip over the actual addresses and so forth. This is currently very
217 # simpleminded - it expects a one-to-one correspondence in terms of line
220 if [file exists $expectfile] then {
221 set file_a [open $expectfile r]
223 perror "$expectfile doesn't exist"
227 if [file exists $tmpdir/objdump.out] then {
228 set file_b [open $tmpdir/objdump.out r]
230 perror "$tmpdir/objdump.out doesn't exist"
234 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
239 while { [gets $file_a line] != $eof } {
240 if [regexp "^#.*$" $line] then {
248 while { [gets $file_b line] != $eof } {
249 if [regexp "^#.*$" $line] then {
257 for { set i 0 } { $i < [llength $list_a] } { incr i } {
258 set line_a [lindex $list_a $i]
259 set line_b [lindex $list_b $i]
262 verbose "\t$expectfile: $i: $line_a" 3
263 verbose "\t/tmp/objdump.out: $i: $line_b" 3
264 if [regexp $line_a $line_b] then {
267 verbose -log "\t$expectfile: $i: $line_a"
268 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
274 if { [llength $list_a] != [llength $list_b] } {
275 verbose -log "Line count"
279 if $differences<1 then {
285 verbose -log "$exec_output"
292 # objdump_dymsymstuff
293 # Dump dynamic symbol stuff and make sure that it is sane.
295 proc objdump_dynsymstuff { objdump object expectfile } {
296 global DOBJDUMP_FLAGS
297 global version_output
301 if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
303 verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out"
305 catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq > $tmpdir/objdump.out" exec_output
306 set exec_output [prune_warnings $exec_output]
307 if [string match "" $exec_output] then {
309 # Now do a line-by-line comparison to effectively diff the darned things
310 # The stuff coming from the expectfile is actually a regex, so we can
311 # skip over the actual addresses and so forth. This is currently very
312 # simpleminded - it expects a one-to-one correspondence in terms of line
315 if [file exists $expectfile] then {
316 set file_a [open $expectfile r]
318 warning "$expectfile doesn't exist"
322 if [file exists $tmpdir/objdump.out] then {
323 set file_b [open $tmpdir/objdump.out r]
325 fail "$tmpdir/objdump.out doesn't exist"
329 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
334 while { [gets $file_a line] != $eof } {
335 if [regexp "^#.*$" $line] then {
343 while { [gets $file_b line] != $eof } {
344 if [regexp "^#.*$" $line] then {
352 for { set i 0 } { $i < [llength $list_b] } { incr i } {
353 set line_b [lindex $list_b $i]
355 # The tests are rigged so that we should never export a symbol with the
356 # word 'hide' in it. Thus we just search for it, and bail if we find it.
357 if [regexp "hide" $line_b] then {
358 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
363 verbose "\t$expectfile: $i: $line_b" 3
365 # We can't assume that the sort is consistent across
366 # systems, so we must check each regexp. When we find a
367 # regexp, we null it out, so we don't match it twice.
368 for { set j 0 } { $j < [llength $list_a] } { incr j } {
369 set line_a [lindex $list_a $j]
371 if [regexp $line_a $line_b] then {
372 lreplace $list_a $j $j "CAN NOT MATCH"
377 if { $j >= [llength $list_a] } {
378 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
384 if { [llength $list_a] != [llength $list_b] } {
385 verbose -log "Line count"
389 if $differences<1 then {
395 verbose -log "$exec_output"
402 # objdump_versionstuff
403 # Dump version definitions/references and make sure that it is sane.
405 proc objdump_versionstuff { objdump object expectfile } {
406 global VOBJDUMP_FLAGS
407 global version_output
411 if {[which $objdump] == 0} then {
412 perror "$objdump does not exist"
416 if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }
418 verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"
420 catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
421 set exec_output [prune_warnings $exec_output]
422 if [string match "" $exec_output] then {
424 # now perform differences against the expected output.
425 verbose -log "$diff -q $tmpdir/objdump.out $expectfile"
426 catch "exec $diff -q $tmpdir/objdump.out $expectfile" exec_output
427 set exec_output [prune_warnings $exec_output]
428 if [string match "" $exec_output] then {
431 verbose -log "$exec_output"
437 verbose -log "$exec_output"
442 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
456 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
461 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
466 if [string match "" $other] then {
469 set other_lib $tmpdir/$other
472 if [string match "" $mapfile] then {
475 set script_arg "$script $srcdir/$subdir/$mapfile"
478 if {![ld_simple_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg"]} {
483 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
488 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
493 if [string match "" $symexp] then {
494 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
499 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
509 proc test_ldfail { test flag source execname other mapfile whyfail } {
522 if [string match "" $other] then {
525 set other_lib $tmpdir/$other
528 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
533 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
538 verbose -log "This link should fail because of $whyfail"
540 if [string match "" $mapfile] then {
543 set script_arg "$script $srcdir/$subdir/$mapfile"
546 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
553 proc test_asfail { test flag source execname whyfail } {
561 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
566 verbose -log "This assemble should fail because of $whyfail"
567 catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
568 set exec_output [prune_warnings $exec_output]
569 if [string match "" $exec_output] then {
573 verbose -log "$exec_output"
577 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
586 verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
587 exec cp $tmpdir/$srclib $tmpdir/$libname.so
589 verbose -log "$strip $tmpdir/$libname.so"
590 catch "exec $strip $tmpdir/$libname.so" exec_output
591 if [string match "" $exec_output] then {
593 # If strip went OK, then run the usual tests on the thing to make sure that
595 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
600 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
606 verbose -log "$exec_output"
614 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
627 set script --version-script
628 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
633 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
638 if [string match "" $solibname] then {
641 set solibname_lib $tmpdir/$solibname
644 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
649 if [string match "" $verexp] then {
651 # Make sure we get nothing back.
653 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
658 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
664 if [string match "" $versymexp] then {
665 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
670 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
676 if [string match "" $symexp] then {
677 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
682 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
693 # Basic test - build a library with versioned symbols.
695 build_vers_lib "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym
699 # Test #2 - build a library, and link it against the library we built in step
702 build_vers_lib "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""
705 # Test #3 - build an executable, and link it against vers1.so.
707 build_exec "vers3" vers3.c vers3 "" vers1.so vers3.ver vers3.dsym ""
710 # Test #4 - Make sure a version implicitly defined in an executable
711 # causes a version node to be created. Verify this both with and without
715 # This test fails on MIPS. On the MIPS we must put foo in the dynamic
716 # symbol table, which the test does not expect.
717 setup_xfail "mips*-*-*"
718 build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym
720 build_exec "vers4a" vers4.c vers4a "-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym
724 # Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
727 test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"
731 # Now build a test that should reference a bunch of versioned symbols.
732 # All of them should be correctly referenced.
734 build_exec "vers6" vers6.c vers6 "" vers1.so vers6.ver vers6.dsym vers6.sym
737 # Another test to verify that something made local via 'local' is truly not
740 build_vers_lib "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym
742 test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"
746 # This test is designed to verify that we can pass a linker script on the
747 # command line as if it were a normal .o file.
749 catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
750 build_vers_lib "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym
753 # This test tries to make sure that version references to versioned symbols
754 # don't collide with default definitions with the same symbol.
756 build_exec "vers9" vers9.c vers9 "-export-dynamic" "" vers9.ver vers9.dsym vers9.sym
760 # Try and use a non-existant version node. The linker should fail with
763 test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"
766 # Try and some things the assembler should complain about.
768 test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"
770 test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"
773 # Put a shared library in an archive library, and make sure the global
774 # archive symbol table is sane.
776 test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym
779 # Strip a shared library, and make sure we didn't screw something up in there.
781 test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym
785 # Build another test with some versioned symbols. Here we are going to
786 # try and override something from the library, and we shouldn't get
789 build_exec "vers15" vers15.c vers15 "" vers1.so vers15.ver vers15.dsym vers15.sym