# Expect script for ld-version tests
#   Copyright (C) 1997 Free Software Foundation
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Written by Eric Youngdale (eric@andante.jic.com)

#

# This test can only be run if ld generates native executables.
if ![isnative] then {return}

# This test can only be run on a couple of ELF platforms.
# Square bracket expressions seem to confuse istarget.
# This is the same test that is used in ld-shared, BTW.
if { ![istarget i386-*-sysv4*] \
     && ![istarget i486-*-sysv4*] \
     && ![istarget i586-*-sysv4*] \
     && ![istarget i386-*-unixware] \
     && ![istarget i486-*-unixware] \
     && ![istarget i586-*-unixware] \
     && ![istarget i386-*-elf*] \
     && ![istarget i486-*-elf*] \
     && ![istarget i586-*-elf*] \
     && ![istarget i386-*-linux*] \
     && ![istarget i486-*-linux*] \
     && ![istarget i586-*-linux*] \
     && ![istarget m68k-*-linux*] \
     && ![istarget mips*-*-irix5*] \
     && ![istarget sparc*-*-elf] \
     && ![istarget sparc*-*-solaris2*] \
     && ![istarget sparc*-*-sunos4*] \
     && ![istarget rs6000*-*-aix*] \
     && ![istarget powerpc*-*-aix*] } {
    return
}

if { [istarget i386-*-linuxaout*] \
     || [istarget i486-*-linuxaout*] \
     || [istarget i586-*-linuxaout*] \
     || [istarget i386-*-linuxoldld*] \
     || [istarget i486-*-linuxoldld*] \
     || [istarget i586-*-linuxoldld*] \
     || [istarget m68k-*-linuxaout*] } {
    return
}

if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
    return
}

set diff diff
set tmpdir tmpdir
set VOBJDUMP_FLAGS --private-headers
set DOBJDUMP_FLAGS --dynamic-syms
set SOBJDUMP_FLAGS --syms
set shared --shared
set script --version-script

proc test_ar { test lib object expect } {
    global ar
    global nm
    global tmpdir
    global srcdir
    global subdir
    global diff

    verbose -log "$ar -cr $tmpdir/$lib $tmpdir/$object"
    catch "exec $ar -cr $tmpdir/$lib $tmpdir/$object" exec_output
    set exec_output [prune_warnings $exec_output]
    if ![string match "" $exec_output] {
	verbose -log "$exec_output"
	unresolved "$test"
	return
    }
    
    verbose -log "$nm --print-armap $tmpdir/$lib | grep \" in \" | grep \"VERS\\|bar\\|foo\" | sort > $tmpdir/nm.out"

    catch "exec $nm --print-armap $tmpdir/$lib | grep \\\ in\\\  | grep VERS\\\\|bar\\\\|foo | sort > $tmpdir/nm.out" exec_output
    if [string match "" $exec_output] then {
	catch "exec $diff -q $tmpdir/nm.out $srcdir/$subdir/$expect" exec_output
	set exec_output [prune_warnings $exec_output]
	if [string match "" $exec_output] then {
	    pass $test
	    return
	} else {
	    verbose -log "$exec_output"
	    fail "$test"
	    return
	}
    } else {
	verbose -log "$exec_output"
	fail "$test"
    }
}

#
# objdump_emptysymstuff
#	Check non-dynamic symbols and make sure there are none with '@'.
#
proc objdump_emptysymstuff { objdump object } {
    global SOBJDUMP_FLAGS
    global version_output
    global diff

    if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }

    verbose -log "$objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p"

    catch "exec $objdump $SOBJDUMP_FLAGS $object | sed -n /\@/p" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
# We shouldn't get anything here.
	return 1
    } else {
# it is not normal to come here - we have no output to compare.
	verbose -log "$exec_output"
	verbose -log "objdump_emptysymstuff: did not expect any output from objdump"
	return 0
    }

}

#
# objdump_emptydynsymstuff
#	Check dynamic symbols and make sure there are none with '@'.
#
proc objdump_emptydynsymstuff { objdump object } {
    global DOBJDUMP_FLAGS
    global version_output
    global diff

    if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }

    verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p"

    catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
# We shouldn't get anything here.
	return 1
    } else { if [string match "*objdump: *: not a dynamic object" $exec_output] then {
	return 1
    } else {
# it is not normal to come here - we have no output to compare.
	verbose -log "$exec_output"
	verbose -log "objdump_emptydynsymstuff: did not expect any output from objdump"
	return 0
    } }
}

#
# objdump_emptyverstuff
#	Make sure there is no version information
#
proc objdump_emptyverstuff { objdump object } {
    global VOBJDUMP_FLAGS
    global version_output
    global diff
    global tmpdir

    if {[which $objdump] == 0} then {
	perror "$objdump does not exist"
	return 0
    }

    if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }

    verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"

    catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
# it is normal to fail here - we have no output to compare.
	return 1
    } else {
	verbose -log "$exec_output"
	verbose -log "objdump_emptyverstuff: did not expect any output from objdump"
	return 0
    }

}

#
# objdump_symstuff
#	Dump non-dynamic symbol stuff and make sure that it is sane.
#
proc objdump_symstuff { objdump object expectfile } {
    global SOBJDUMP_FLAGS
    global version_output
    global diff
    global tmpdir

    if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }

    verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep \@ | sort  > $tmpdir/objdump.out"

    catch "exec $objdump $SOBJDUMP_FLAGS $object | grep \@ | sort  > $tmpdir/objdump.out" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {

# Now do a line-by-line comparison to effectively diff the darned things
# The stuff coming from the expectfile is actually a regex, so we can
# skip over the actual addresses and so forth.  This is currently very
# simpleminded - it expects a one-to-one correspondence in terms of line
# numbers.

	if [file exists $expectfile] then {
	    set file_a [open $expectfile r]
	} else {
	    perror "$expectfile doesn't exist"
	    return 0
	}
	
	if [file exists $tmpdir/objdump.out] then {
	    set file_b [open $tmpdir/objdump.out r]
	} else {
	    perror "$tmpdir/objdump.out doesn't exist"
	    return 0
	}

	verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2

	set eof -1
	set differences 0

	while { [gets $file_a line] != $eof } {
	    if [regexp "^#.*$" $line] then {
		continue
	    } else {
		lappend list_a $line
	    }
	}
	close $file_a

	while { [gets $file_b line] != $eof } {
	    if [regexp "^#.*$" $line] then {
		continue
	    } else {
		lappend list_b $line
	    }
	}
	close $file_b
	
	for { set i 0 } { $i < [llength $list_a] } { incr i } {
	    set line_a [lindex $list_a $i]
	    set line_b [lindex $list_b $i]
	    

	    verbose "\t$expectfile: $i: $line_a" 3
	    verbose "\t/tmp/objdump.out: $i: $line_b" 3
	    if [regexp $line_a $line_b] then {
		continue
	    } else {
		verbose -log "\t$expectfile: $i: $line_a"
		verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
		
		return 0
	    }
	}
	
	if { [llength $list_a] != [llength $list_b] } {
	    verbose -log "Line count"
	    return 0
	}
	
	if $differences<1 then {
	    return 1
	}
	
	return 0
    } else {
	verbose -log "$exec_output"
	return 0
    }

}

#
# objdump_dymsymstuff
#	Dump dynamic symbol stuff and make sure that it is sane.
#
proc objdump_dynsymstuff { objdump object expectfile } {
    global DOBJDUMP_FLAGS
    global version_output
    global diff
    global tmpdir

    if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }

    verbose -log "$objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq  > $tmpdir/objdump.out"

    catch "exec $objdump $DOBJDUMP_FLAGS $object | sed -n /VERS/p\\\;/show/p | sort | uniq  > $tmpdir/objdump.out" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {

# Now do a line-by-line comparison to effectively diff the darned things
# The stuff coming from the expectfile is actually a regex, so we can
# skip over the actual addresses and so forth.  This is currently very
# simpleminded - it expects a one-to-one correspondence in terms of line
# numbers.

	if [file exists $expectfile] then {
	    set file_a [open $expectfile r]
	} else {
	    warning "$expectfile doesn't exist"
	    return 0
	}
	
	if [file exists $tmpdir/objdump.out] then {
	    set file_b [open $tmpdir/objdump.out r]
	} else {
	    fail "$tmpdir/objdump.out doesn't exist"
	    return 0
	}

	verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2

	set eof -1
	set differences 0

	while { [gets $file_a line] != $eof } {
	    if [regexp "^#.*$" $line] then {
		continue
	    } else {
		lappend list_a $line
	    }
	}
	close $file_a

	while { [gets $file_b line] != $eof } {
	    if [regexp "^#.*$" $line] then {
		continue
	    } else {
		lappend list_b $line
	    }
	}
	close $file_b
	
	for { set i 0 } { $i < [llength $list_b] } { incr i } {
	    set line_b [lindex $list_b $i]
	    
# The tests are rigged so that we should never export a symbol with the
# word 'hide' in it.  Thus we just search for it, and bail if we find it.
	    if [regexp "hide" $line_b] then {
		verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
		
		return 0
	    }

	    verbose "\t$expectfile: $i: $line_b" 3

	    # We can't assume that the sort is consistent across
	    # systems, so we must check each regexp.  When we find a
	    # regexp, we null it out, so we don't match it twice.
	    for { set j 0 } { $j < [llength $list_a] } { incr j } {
		set line_a [lindex $list_a $j]

		if [regexp $line_a $line_b] then {
		    lreplace $list_a $j $j "CAN NOT MATCH"
		    break
		}
	    }

	    if { $j >= [llength $list_a] } {
		verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
		
		return 0
	    }
	}
	
	if { [llength $list_a] != [llength $list_b] } {
	    verbose -log "Line count"
	    return 0
	}
	
	if $differences<1 then {
	    return 1
	}
	
	return 0
    } else {
	verbose -log "$exec_output"
	return 0
    }

}

#
# objdump_versionstuff
#	Dump version definitions/references and make sure that it is sane.
#
proc objdump_versionstuff { objdump object expectfile } {
    global VOBJDUMP_FLAGS
    global version_output
    global diff
    global tmpdir

    if {[which $objdump] == 0} then {
	perror "$objdump does not exist"
	return 0
    }

    if ![info exists VOBJDUMP_FLAGS] { set VOBJDUMP_FLAGS "" }

    verbose -log "$objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out"

    catch "exec $objdump $VOBJDUMP_FLAGS $object | sed -n /Version/,\\\$p > $tmpdir/objdump.out" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {

# now perform differences against the expected output.
	verbose -log "$diff -q $tmpdir/objdump.out $expectfile"
	catch "exec $diff -q $tmpdir/objdump.out $expectfile" exec_output
	set exec_output [prune_warnings $exec_output]
	if [string match "" $exec_output] then {
	    return 1
	} else {
	    verbose -log "$exec_output"
	    return 0
	}

	return 1
    } else {
	verbose -log "$exec_output"
	return 0
    }
}

proc build_vers_lib { test source libname other mapfile verexp versymexp symexp } {
    global ld
    global srcdir
    global subdir
    global exec_output
    global host_triplet
    global tmpdir
    global as
    global objdump
    global CC
    global CFLAGS
    global shared
    global script

    if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s]     {
	unresolved "$test"
	return
    }

    if ![ld_assemble $as $tmpdir/$libname.s $tmpdir/$libname.o ]     {
	unresolved "$test"
	return
    }

    if [string match "" $other] then {
	set other_lib ""
    } else {
	set other_lib $tmpdir/$other
    }

    if [string match "" $mapfile] then {
	set script_arg ""
    } else {
	set script_arg "$script $srcdir/$subdir/$mapfile"
    }

    if {![ld_simple_link $ld $tmpdir/$libname.so "$shared $tmpdir/$libname.o $other_lib $script_arg"]}     {
	fail "$test"
	return
    }

    if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]}     {
	fail "$test"
	return
    }

    if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]}     {
	fail "$test"
	return
    }

    if [string match "" $symexp] then {
	if {![objdump_emptysymstuff $objdump $tmpdir/$libname.o ]}     {
	    fail "$test"
	    return
	}
    } else {
	if {![objdump_symstuff $objdump $tmpdir/$libname.o $srcdir/$subdir/$symexp ]}     {
	    fail "$test"
	    return
	}
    }

    pass $test
    
}

proc test_ldfail { test flag source execname other mapfile whyfail } {
    global ld
    global srcdir
    global subdir
    global exec_output
    global host_triplet
    global tmpdir
    global as
    global objdump
    global CC
    global CFLAGS
    global script

    if [string match "" $other] then {
	set other_lib ""
    } else {
	set other_lib $tmpdir/$other
    }

    if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
	unresolved "$test"
	return
    }

    if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ]     {
	unresolved "$test"
	return
    }

    verbose -log "This link should fail because of $whyfail"

    if [string match "" $mapfile] then {
	set script_arg ""
    } else {
	set script_arg "$script $srcdir/$subdir/$mapfile"
    }

    if {![ld_link $ld $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]}     {
	pass "$test"
	return
    }
    fail "$test"
}

proc test_asfail { test flag source execname whyfail } {
    global srcdir
    global subdir
    global tmpdir
    global as
    global CC
    global CFLAGS

    if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
	unresolved "$test"
	return
    }

    verbose -log "This assemble should fail because of $whyfail"
    catch "exec $as -o $tmpdir/$execname.o $tmpdir/$execname.s" exec_output
    set exec_output [prune_warnings $exec_output]
    if [string match "" $exec_output] then {
	fail "$test"
	return
    }
    verbose -log "$exec_output"
    pass "$test"
}

proc test_strip_vers_lib { test srclib libname verexp versymexp } {
    global strip
    global srcdir
    global subdir
    global exec_output
    global host_triplet
    global tmpdir
    global objdump

    verbose -log "cp $tmpdir/$srclib $tmpdir/$libname.so"
    exec cp $tmpdir/$srclib $tmpdir/$libname.so

    verbose -log "$strip $tmpdir/$libname.so"
    catch "exec $strip $tmpdir/$libname.so" exec_output
    if [string match "" $exec_output] then {

# If strip went OK, then run the usual tests on the thing to make sure that
# it is sane.
	if {![objdump_versionstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$verexp ]}     {
	    fail "$test"
	    return
	}
	
	if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$versymexp ]}     {
	    fail "$test"
	    return
	}
	
    } else {
	verbose -log "$exec_output"
	fail "$test"
	return
    }
    pass $test
}


proc build_exec { test source execname flags solibname verexp versymexp symexp } {
    global ld
    global srcdir
    global subdir
    global exec_output
    global host_triplet
    global tmpdir
    global as
    global objdump
    global CC
    global CFLAGS

    set shared --shared
    set script --version-script
    if ![ld_compile "$CC -S $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
	unresolved "$test"
	return
    }

    if ![ld_assemble $as $tmpdir/$execname.s $tmpdir/$execname.o ]     {
	unresolved "$test"
	return
    }

    if [string match "" $solibname] then {
	set solibname_lib ""
    } else {
	set solibname_lib $tmpdir/$solibname
    }

    if {![ld_link $ld $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]}     {
	fail "$test"
	return
    }

    if [string match "" $verexp] then {
#
# Make sure we get nothing back.
#
	if {![objdump_emptyverstuff $objdump $tmpdir/$execname ]}     {
	    fail "$test"
	    return
	}
    } else {
	if {![objdump_versionstuff $objdump $tmpdir/$execname $srcdir/$subdir/$verexp ]}     {
	    fail "$test"
	    return
	}
    }

    if [string match "" $versymexp] then {
	if {![objdump_emptydynsymstuff $objdump $tmpdir/$execname ]}     {
	    fail "$test"
	    return
	}
    } else {
	if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$versymexp ]}     {
	    fail "$test"
	    return
	}
    }

    if [string match "" $symexp] then {
	if {![objdump_emptysymstuff $objdump $tmpdir/$execname.o ]}     {
	    fail "$test"
	    return
	}
    } else {
	if {![objdump_symstuff $objdump $tmpdir/$execname.o $srcdir/$subdir/$symexp ]}     {
	    fail "$test"
	    return
	}
    }

    pass $test
}


#
# Basic test - build a library with versioned symbols.
#
build_vers_lib "vers1" vers1.c vers1 "" vers1.map vers1.ver vers1.dsym vers1.sym


#
# Test #2 - build a library, and link it against the library we built in step
# 1.
#
build_vers_lib "vers2" vers2.c vers2 vers1.so vers2.map vers2.ver vers2.dsym ""

#
# Test #3 - build an executable, and link it against vers1.so.
#
build_exec "vers3" vers3.c vers3 "" vers1.so vers3.ver vers3.dsym ""

#
# Test #4 - Make sure a version implicitly defined in an executable
# causes a version node to be created.  Verify this both with and without
# --export-dynamic.
#

# This test fails on MIPS.  On the MIPS we must put foo in the dynamic
# symbol table, which the test does not expect.
setup_xfail "mips*-*-*"
build_exec "vers4" vers4.c vers4 "" "" "" "" vers4.sym

build_exec "vers4a" vers4.c vers4a "-export-dynamic" "" vers4a.ver vers4a.dsym vers4a.sym


#
# Try multiple definitions foo@BAR and foo@@BAR and make sure the linker
# complains.
#
test_ldfail "vers5" "" vers5.c vers5 "" "" "multiple definition of foo@VERS_1.2"

#
#
# Now build a test that should reference a bunch of versioned symbols.
# All of them should be correctly referenced.
#
build_exec "vers6" vers6.c vers6 "" vers1.so vers6.ver vers6.dsym vers6.sym

#
# Another test to verify that something made local via 'local' is truly not
# accessible.
#
build_vers_lib "vers7a" vers7a.c vers7a "" vers7.map vers7a.ver vers7a.dsym vers7a.sym

test_ldfail "vers7" "" vers7.c vers7 vers7a.so "" "undefined reference to hide_a"


#
# This test is designed to verify that we can pass a linker script on the
# command line as if it were a normal .o file.
#
catch "exec cp $srcdir/$subdir/vers8.map $tmpdir/" ignore_output
build_vers_lib "vers8" vers1.c vers8 vers8.map "" vers8.ver vers1.dsym vers1.sym

#
# This test tries to make sure that version references to versioned symbols
# don't collide with default definitions with the same symbol.
#
build_exec "vers9" vers9.c vers9 "-export-dynamic" "" vers9.ver vers9.dsym vers9.sym


#
# Try and use a non-existant version node.  The linker should fail with
# an error message.
#
test_ldfail "vers10" "-DDO_TEST10" vers1.c vers10 "" "vers1.map --shared" "invalid version"

#
# Try and some things the assembler should complain about.
#
test_asfail "vers11" "-DDO_TEST11" vers1.c vers11 "no @ in symver"

test_asfail "vers12" "-DDO_TEST12" vers1.c vers12 "extern version definition"

#
# Put a shared library in an archive library, and make sure the global
# archive symbol table is sane.
#
test_ar "ar with versioned solib" vers13.a vers1.so vers13.asym

#
# Strip a shared library, and make sure we didn't screw something up in there.
#
test_strip_vers_lib "vers14" vers1.so vers14 vers1.ver vers1.dsym 


#
# Build another test with some versioned symbols.  Here we are going to 
# try and override something from the library, and we shouldn't get
# any errors.
#
build_exec "vers15" vers15.c vers15 "" vers1.so vers15.ver vers15.dsym vers15.sym
