#!/bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program 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 pronram;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE   : ld01
#
#  PURPOSE: To test the basic functionality of the `ld` command.
#
#  HISTORY:
#    06/01 Robbie Williamson (robbiew@us.ibm.com)
#     -Ported
#
#
#---------------------------------------------------------------------------
#Uncomment line below for debug output
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

do_cleanup()
{
	rm -rf $TCtmp
}

res=0
CC=${CC:=gcc}
LD=${LD:=ld}
TCdat=${TCdat:-`pwd`}
TCtmp=${TCtmp:-/tmp/ld-$$}
mkdir $TCtmp

#ASSERTION 1 
#Test for graceful failure when ld can't find a fileTest calling ld directly
#
#CODE
	echo "Assertion 1 .................."
	ld x.obj y.obj 2> $TCtmp/errmsg.out
#cat <<EOF > $TCtmp/errmsg.exp
#$LD: cannot open x.obj: No such file or directory
#EOF

#diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
grep "$LD:" $TCtmp/errmsg.out | grep [xy].obj | grep -q "No such file or directory"
if [ $? -eq 0 ]
then
	echo  "-)"
else
	echo "FAIL -  ld failed to give expected error msg"
	do_cleanup
	exit 1
fi



##ASSERTION 2 
#Test for graceful failure when ld can't find a fileTest calling ld through cc
#
#CODE
echo "Assertion 2 .................."
$CC x.obj y.obj 2> $TCtmp/errmsg.out
cat <<EOF > $TCtmp/errmsg.exp
$CC: x.obj: No such file or directory
$CC: y.obj: No such file or directory
$CC: No input files
EOF

diff -iw $TCtmp/errmsg.out $TCtmp/errmsg.exp
if [ $? -eq 0 ]
then
	echo  "-)"
else
	echo "FAIL -  ld failed to give expected error msg"
	do_cleanup
	exit 1
fi



##ASSERTION 3
#Test for graceful failure when ld can't find a fileTest calling ld directly, with a non-existent path
#
#CODE
	echo "Assertion 3 .................."
	$LD bad/x.obj 2> $TCtmp/errmsg.out
#cat <<EOF > $TCtmp/errmsg.exp
#$LD: cannot open bad/x.obj: No such file or directory
#EOF
cat $TCtmp/errmsg.out | grep "$LD:" | grep "bad/[xy].obj:" | grep -q "No such file or directory"

#diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
if [ $? -eq 0 ]
then
	echo  "-)"
else
	echo "FAIL -  ld failed to give expected error msg"
	do_cleanup
	exit 1
fi



##ASSERTION 4
#Test for graceful failure when ld can't find a fileTest calling ld through cc, with a non-existent path
#
#CODE
	echo "Assertion 4 .................."
	$CC bad/x.obj 2> $TCtmp/errmsg.out
cat <<EOF > $TCtmp/errmsg.exp
$CC: bad/x.obj: No such file or directory
$CC: No input files
EOF

diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
if [ $? -eq 0 ]
then
	echo  "-)"
else
	echo "FAIL -  ld failed to give expected error msg"
	do_cleanup
	exit 1
fi

## ASSERTION 5
## Making sure the "shared" option works as designed
##
echo "Assertion 5 .................."
# Check for ppc64 architecture
file f1.obj | grep PowerPC | grep 64-bit >/dev/null 2>&1
if [ $? -eq 0 ]; then
	$LD -m elf64ppc -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/test.lib
else
	$LD -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/test.lib
fi
file $TCtmp/test.lib | grep -q shared
if [ $? -eq 0 ]; then
	echo "-)"
else
	echo "FAIL - ld failed to build a shared object"
	do_cleanup
	exit 1
fi


## ASSERTION 6
## Making sure that the linker ignores the "-Bstatic" option 
## when using a dynamically linked shared object
##
echo "Assertion 6 .................."

# Check for ppc64 architecture
file f1.obj | grep PowerPC | grep 64-bit >/dev/null 2>&1
if [ $? -eq 0 ]; then
	$LD -m elf64ppc -Bdynamic -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/lola 2>$TCtmp/errmsg.out
	$LD -m elf64ppc -Bstatic  -L. $TCdat/ldmain.obj $TCdat/rd1.obj $TCtmp/lola -o $TCtmp/a.out 2> $TCtmp/errmsg.out
 else
	$LD -Bdynamic -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/lola 2>$TCtmp/errmsg.out
	$LD -Bstatic  -L. $TCdat/ldmain.obj $TCdat/rd1.obj $TCtmp/lola -o $TCtmp/a.out 2> $TCtmp/errmsg.out
fi
#nm $TCtmp/a.out | grep -q DYNAMIC
rc_code=$?

set -- $($LD --version | awk 'BEGIN { version = "-1"; } /^GNU ld/ { for (i = 1; i <= NF; i++) { if ($i ~ /^[[:digit:]]+\.[[:digit:]]+/) { split ($i, arr, "."); version = sprintf ("%d %d", arr[1], arr[2]); } } } END { if (version == "-1") { exit 1; } else { print version; } }')

if [ $? -ne 0 ] ; then
	echo "---"
elif [ "$1" -le "2"  -a "$2" -le "15" ]; then
	if [ $rc_code -eq 0 ]
	then
		echo "-)"
	else
		echo "FAIL - ld 9 failed ignore the -Bstatic option with \
		      shared objects"
		do_cleanup
		exit 1
	fi

else
	if [ $rc_code -eq 0 ]
	then
		echo "FAIL - ld failed to  reject the -Bstatic option \
		      with shared objects"
		do_cleanup
		exit 1
	else
		echo "-)"
	fi
fi # end of checking for ld version less than 2.16	

##
## ASSERTION 7
## Making sure that the linker does not accepts shared object
## when doing static linking try to link with a with and
## without -r*repeat with -r
##


echo "Assertion 7 .................."
$LD -Bdynamic -shared $TCdat/ldmain.obj $TCdat/f1.obj $TCdat/rf1.obj -o $TCtmp/lola -L/usr/lib/ 
$LD -Bstatic -r  $TCdat/ldmain.obj $TCdat/f1.obj $TCdat/rf1.obj $TCtmp/lola -L/usr/lib/ 2> $TCtmp/errmsg.out
cat <<EOF > $TCtmp/errmsg.exp
$TCtmp/lola: could not read symbols: Invalid operation
EOF

grep -q  "could not read symbols: Invalid operation" $TCtmp/errmsg.out
rc_grep=$?
grep -q "ld: attempted static link of dynamic object" $TCtmp/errmsg.out
if [ $rc_grep -eq 0  -o $? -eq 0 ]; then 
	echo "-)"
else
	echo "FAIL - ld failed to give expected error msg"
	do_cleanup
	exit 1
fi

do_cleanup
echo "ld: PASS"
exit 0
