Initial revision
[binutils.git] / ld / testsuite / ld-elfvers / vers.exp
blob5dfaa93346ef2bfe05bd79ad2dcfdda1452fa457
1 # Expect script for ld-version tests
2 # Copyright (C) 1997, 1998 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.
8 #
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*] } {
40 return
43 if { [istarget i?86-*-linuxaout*] \
44 || [istarget i?86-*-linuxoldld*] \
45 || [istarget m68k-*-linuxaout*] } {
46 return
49 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
50 return
53 set diff diff
54 set tmpdir tmpdir
55 set VOBJDUMP_FLAGS --private-headers
56 set DOBJDUMP_FLAGS --dynamic-syms
57 set SOBJDUMP_FLAGS --syms
58 set shared --shared
59 set script --version-script
61 proc test_ar { test lib object expect } {
62 global ar
63 global nm
64 global tmpdir
65 global srcdir
66 global subdir
67 global diff
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"
74 unresolved "$test"
75 return
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 {
85 pass $test
86 return
87 } else {
88 verbose -log "$exec_output"
89 fail "$test"
90 return
92 } else {
93 verbose -log "$exec_output"
94 fail "$test"
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
105 global diff
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.
115 return 1
116 } else {
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"
120 return 0
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
132 global diff
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.
142 return 1
143 } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
144 return 1
145 } else {
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"
149 return 0
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
160 global diff
161 global tmpdir
163 if {[which $objdump] == 0} then {
164 perror "$objdump does not exist"
165 return 0
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.
176 return 1
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.
180 return 1
181 } else {
182 verbose -log "$exec_output"
183 verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
184 return 0
190 # objdump_symstuff
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
196 global diff
197 global tmpdir
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
211 # numbers.
213 if [file exists $expectfile] then {
214 set file_a [open $expectfile r]
215 } else {
216 perror "$expectfile doesn't exist"
217 return 0
220 if [file exists $tmpdir/objdump.out] then {
221 set file_b [open $tmpdir/objdump.out r]
222 } else {
223 perror "$tmpdir/objdump.out doesn't exist"
224 return 0
227 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
229 set eof -1
230 set differences 0
232 while { [gets $file_a line] != $eof } {
233 if [regexp "^#.*$" $line] then {
234 continue
235 } else {
236 lappend list_a $line
239 close $file_a
241 while { [gets $file_b line] != $eof } {
242 if [regexp "^#.*$" $line] then {
243 continue
244 } else {
245 lappend list_b $line
248 close $file_b
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 {
258 continue
259 } else {
260 verbose -log "\t$expectfile: $i: $line_a"
261 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
263 return 0
267 if { [llength $list_a] != [llength $list_b] } {
268 verbose -log "Line count"
269 return 0
272 if $differences<1 then {
273 return 1
276 return 0
277 } else {
278 verbose -log "$exec_output"
279 return 0
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
291 global diff
292 global tmpdir
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
306 # numbers.
308 if [file exists $expectfile] then {
309 set file_a [open $expectfile r]
310 } else {
311 warning "$expectfile doesn't exist"
312 return 0
315 if [file exists $tmpdir/objdump.out] then {
316 set file_b [open $tmpdir/objdump.out r]
317 } else {
318 fail "$tmpdir/objdump.out doesn't exist"
319 return 0
322 verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
324 set eof -1
325 set differences 0
327 while { [gets $file_a line] != $eof } {
328 if [regexp "^#.*$" $line] then {
329 continue
330 } else {
331 lappend list_a $line
334 close $file_a
336 while { [gets $file_b line] != $eof } {
337 if [regexp "^#.*$" $line] then {
338 continue
339 } else {
340 lappend list_b $line
343 close $file_b
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"
353 return 0
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"
366 break
370 if { $j >= [llength $list_a] } {
371 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
373 return 0
377 if { [llength $list_a] != [llength $list_b] } {
378 verbose -log "Line count"
379 return 0
382 if $differences<1 then {
383 return 1
386 return 0
387 } else {
388 verbose -log "$exec_output"
389 return 0
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
401 global diff
402 global tmpdir
404 if {[which $objdump] == 0} then {
405 perror "$objdump does not exist"
406 return 0
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]
423 gets $f2 l2
424 while { [gets $f1 l1] != -1 } {
425 if { [string match $l2 $l1] } then {
426 if { [gets $f2 l2] == -1 } then {
427 close $f1
428 close $f2
429 return 1
434 # We reached the end of the output without seeing the line we
435 # expected. This is a test failure.
437 close $f1
438 close $f2
440 verbose -log "Did not find \"$l2\""
441 set f1 [open $tmpdir/objdump.out r]
442 while { [gets $f1 l1] != -1 } {
443 verbose -log $l1
446 verbose -log "$exec_output"
447 return 0
448 } else {
449 verbose -log "$exec_output"
450 return 0
454 proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
455 global ld
456 global srcdir
457 global subdir
458 global exec_output
459 global host_triplet
460 global tmpdir
461 global as
462 global objdump
463 global CC
464 global CFLAGS
465 global shared
466 global script
468 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s] {
469 unresolved "$test"
470 return
473 if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ] {
474 unresolved "$test"
475 return
478 if [string match "" $other] then {
479 set other_lib ""
480 } else {
481 set other_lib $tmpdir/$other
484 if [string match "" $mapfile] then {
485 set script_arg ""
486 } else {
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"]} {
491 fail "$test"
492 return
495 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
496 fail "$test"
497 return
500 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
501 fail "$test"
502 return
505 if [string match "" $symexp] then {
506 if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]} {
507 fail "$test"
508 return
510 } else {
511 if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]} {
512 fail "$test"
513 return
517 pass $test
521 proc test_ldfail { test flag source execname other mapfile whyfail } {
522 global ld
523 global srcdir
524 global subdir
525 global exec_output
526 global host_triplet
527 global tmpdir
528 global as
529 global objdump
530 global CC
531 global CFLAGS
532 global script
534 if [string match "" $other] then {
535 set other_lib ""
536 } else {
537 set other_lib $tmpdir/$other
540 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
541 unresolved "$test"
542 return
545 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
546 unresolved "$test"
547 return
550 verbose -log "This link should fail because of $whyfail"
552 if [string match "" $mapfile] then {
553 set script_arg ""
554 } else {
555 set script_arg "$script $srcdir/$subdir/$mapfile"
558 if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]} {
559 pass "$test"
560 return
562 fail "$test"
565 proc test_asfail { test flag source execname whyfail } {
566 global srcdir
567 global subdir
568 global tmpdir
569 global as
570 global CC
571 global CFLAGS
573 if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
574 unresolved "$test"
575 return
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 {
582 fail "$test"
583 return
585 verbose -log "$exec_output"
586 pass "$test"
589 proc test_strip_vers_lib { test srclib libname verexp versymexp } {
590 global strip
591 global srcdir
592 global subdir
593 global exec_output
594 global host_triplet
595 global tmpdir
596 global objdump
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
606 # it is sane.
607 if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]} {
608 fail "$test"
609 return
612 if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]} {
613 fail "$test"
614 return
617 } else {
618 verbose -log "$exec_output"
619 fail "$test"
620 return
622 pass $test
626 proc build_exec { test source execname flags solibname verexp versymexp symexp } {
627 global ld
628 global srcdir
629 global subdir
630 global exec_output
631 global host_triplet
632 global tmpdir
633 global as
634 global objdump
635 global CC
636 global CFLAGS
638 set shared --shared
639 set script --version-script
640 if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s] {
641 unresolved "$test"
642 return
645 if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ] {
646 unresolved "$test"
647 return
650 if [string match "" $solibname] then {
651 set solibname_lib ""
652 } else {
653 set solibname_lib $tmpdir/$solibname
656 if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]} {
657 fail "$test"
658 return
661 if [string match "" $verexp] then {
663 # Make sure we get nothing back.
665 if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]} {
666 fail "$test"
667 return
669 } else {
670 if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]} {
671 fail "$test"
672 return
676 if [string match "" $versymexp] then {
677 if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]} {
678 fail "$test"
679 return
681 } else {
682 if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]} {
683 fail "$test"
684 return
688 if [string match "" $symexp] then {
689 if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]} {
690 fail "$test"
691 return
693 } else {
694 if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]} {
695 fail "$test"
696 return
700 pass $test
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
712 # 1.
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
724 # --export-dynamic.
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
737 # complains.
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
750 # accessible.
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
773 # an error message.
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
799 # any errors.
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 ""