#!/bin/ksh

# Copyright (C) 1992 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.

# The GNU C Library 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
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB.  If
# not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# A constant env variable to test env access (see testcase1l.c)
export MYSTUFF="hi there"

if [ $# -ge 1 ]
then
  CC=$1
else
  CC="gcc"
fi

if [ $# -ge 2 ]
then
  F77=$2
else
  F77="g77"
fi

if [ $# -ge 3 ]
then
  UND=$3
else
  UND="_"
fi

LD_LIBRARY_PATH=$X/local_bin

# The makefile implicitly tests a lot of compile/link time things, so
# look there for details.

echo "Minimal test"
echo '#include <stdio.h>
      main() {
	  fprintf(stderr, "hello\\n"); 
	  return 0;
      }' >hello.c
$CC -Wl,-Bdynamic hello.c -Wl,--rpath=.. -o hello -Wl,--dynamic-linker,../ld.so
./hello
echo Exit code $?
echo -----------------------
echo "PIC in main that isn't dynamic"
$CC -fpic --static hello.c -o hello2
./hello2
echo Exit code $?
echo -----------------------
echo "Minimal test of dynamic case"
echo ./testcase1
./testcase1 
echo Exit code $?
echo -----------------------
echo "Multiple dynamic case"
echo ./testcase2
./testcase2 >tf 2>&1
xc=$?
sed -e 's/_dynLoadedEntryPoint/dynLoadedEntryPoint/g' -e 's/_noSuchName/noSuchName/' <tf
echo Exit code $xc
echo -----------------------
echo "Test ld.so as command; arg passing (can fail randomly; see comments)"
#On Win2k sometimes the image will load at other than ImageBase, and since
#the image has neither .reloc or RELATIVE entries, it won't run correctly.
#This is detected by ld.so and a message printed (rather than allowing
#a memory fault).  If/when we care, this can be fixed by creating .reloc
#or turning on RELATIVE entries (the latter at a fairly high cost for
#main programs).  This appears to be unique to ld.so being used to run
#a program.
sleep 1  # See if we can tweak the odds a bit.
echo ../ld.so ./testcase1 xxx
../ld.so ./testcase1 xxx
echo Exit code $?
echo -----------------------
echo "ld.so as command, --verify"
echo ../ld.so --verify ./testcase1
../ld.so --verify ./testcase1
echo Exit code $?
echo -----------------------
echo "ld.so as command, --list"
echo ../ld.so --list ./testcase1
../ld.so --list ./testcase1 >tf
xc=$?
sed -e 's/0x......../0xxxxxxxxx/g' \
	-e 's,$X,\$X,' -e 's/lib.*\.so\.[^ ]*/libc.so.version/' <tf
echo Exit code $xc
echo -----------------------
echo "ld.so as command, --list with arg"
echo ../ld.so --list ./testcase1 fprintf
../ld.so --list ./testcase1 ${UND}fprintf >tf
xc=$?
sed -e 's/0x......../0xxxxxxxxx/g' -e 's/_fprintf/fprintf/' \
	-e 's,$X,\$X,' -e 's/lib.*\.so\.[^ ]*/libc.so.version/' <tf
echo Exit code $xc
echo -----------------------
echo "ld.so as command on non-shared executable"
echo 'main(){printf("hello\\n");}' > trun.c
$CC -static trun.c
echo ../ld.so ./a.out
../ld.so ./a.out
echo Exit code $?
echo -----------------------
echo "ld.so as command --verify on non-shared executable"
echo ../ld.so --verify ./a.out
../ld.so --verify ./a.out
echo Exit code $?
echo -----------------------
echo "ld.so with no args"
echo ../ld.so
../ld.so
echo Exit code $?
echo -----------------------
echo "Test LD_TRACE_LOADED_OBJECTS"
echo LD_TRACE_LOADED_OBJECTS=1 ./testcase2
LD_TRACE_LOADED_OBJECTS=1 ./testcase2  >tf
xc=$?
sed -e 's/0x......../0xxxxxxxxx/g' \
	-e 's,$X,\$X,' -e 's/lib.*\.so\.[^ ]*/libc.so.version/' <tf
echo Exit code $xc
echo -----------------------
echo "Test error when lib not found"
# This is an intentional failure to check out the message.
echo ./testcase3
./testcase3 >tf 2>&1 
xc=$?
sed -e 's/random number .*/random number /' <tf
echo Exit code $xc
echo -----------------------
echo "Test LD_LIBRARY_PATH"
echo LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:. ./testcase3
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. ./testcase3 >tf 2>&1 
xc=$?
sed -e 's/random number .*/random number /' <tf
echo Exit code $xc
echo -----------------------
echo "Test whether PIC in main works"
echo LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:. ./testcase3pic
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. ./testcase3pic >tf 2>&1 
xc=$?
sed -e 's/random number .*/random number /' <tf
echo Exit code $xc
echo -----------------------
echo "Test whether LD_BIND_NOW works"
# This test only works right on PIC in the main program on Intel
# (Non-PIC calls go direct, not thru PLT.)
echo LD_BIND_NOW=1 LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:. ./testcase3pic
LD_BIND_NOW=1 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. ./testcase3 >tf 2>&1 
xc=$?
sed -e 's/random number .*/random number /' <tf
echo Exit code $xc
echo -----------------------
echo "Test whether lazy binding is lazy"
# This test only works right on PIC in the main program on Intel
# (Non-PIC calls go direct, not thru PLT.)
echo LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:bogus ./testcase3pic
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:bogus ./testcase3pic >tf 2>&1 
xc=$?
sed -e 's/random number .*/random number /' -e 's/_callrand/callrand/g' <tf
echo Exit code $xc
echo -----------------------
echo "Test whether LD_BIND_NOW fails as expected"
# This test only works right on PIC in the main program on Intel
# (Non-PIC calls go direct, not thru PLT.)
echo LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:bogus LD_BIND_NOW=1 ./testcase3
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:bogus LD_BIND_NOW=1 ./testcase3 >tf 2>&1 
sed -e 's/random number .*/random number /' -e 's/_callrand/callrand/g' <tf
echo Exit code $?
echo -----------------------
echo "Test that leaf only and data only shared libs work"
echo testcase4
./testcase4
echo Exit code $?

echo -----------------------
echo "Test that fortran main and basic shared libs work"
$F77 -Wl,-Bdynamic hello.f  -Wl,--dynamic-linker,../ld.so
./a.out

echo -----------------------
echo "Test that fortran PIC main and basic shared libs work"
$F77 -fpic -Wl,-Bdynamic hello.f  -Wl,--dynamic-linker,../ld.so
./a.out
echo Exit code $?

echo -----------------------
echo "Fortran as a shared lib"
# Also incidentally tests that actually moving an entry works, particularly
# dlls.  (A failed call to fstat() before reaching MAIN__ indicates that DLL
# relocation is incorrect.)
./fortmain
echo Exit code $?

echo -----------------------
echo "Fortran common when .so loaded before .o"
# If some values are zero, initialized common is not overriding uninit.
./fortmainsw
echo Exit code $?

echo -----------------------
echo "C++ with class crossing between shared lib and main"
# tests initializers for static global data objects (and libc!)
./cppmain
echo Exit code $?

echo -----------------------
echo "Reversed load, C++"
./cppmainsw
echo Exit code $?

echo -----------------------
echo "Function pointers, duplicate name masking, and symbolic name hiding"
./fpmain
echo Exit code $?

echo -----------------------
echo "library chaining"
./lcmain
echo Exit code $?

echo -----------------------
echo "library locking"
cp fortlib.so fortliblock.so
$X/local_bin/liblock fortliblock.so
($CC -dynamic fortmain.o fortliblock.so; echo Exit code $?) 2>&1 | sed -e 's/^.*ld:/ld:/'

echo -----------------------
echo "weak symbols"
./weakmain

echo -----------------------
echo "archive libdl"
./artestcase

exit 0
