Home | History | Annotate | Download | only in ld
      1 #!/bin/sh
      2 #
      3 #   Copyright (c) International Business Machines  Corp., 2000
      4 #
      5 #   This program is free software;  you can redistribute it and/or modify
      6 #   it under the terms of the GNU General Public License as published by
      7 #   the Free Software Foundation; either version 2 of the License, or
      8 #   (at your option) any later version.
      9 #
     10 #   This program is distributed in the hope that it will be useful,
     11 #   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13 #   the GNU General Public License for more details.
     14 #
     15 #   You should have received a copy of the GNU General Public License
     16 #   along with this program;  if not, write to the Free Software
     17 #   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18 #
     19 #
     20 #
     21 #  FILE   : ld01
     22 #
     23 #  PURPOSE: To test the basic functionality of the `ld` command.
     24 #
     25 #  HISTORY:
     26 #    06/01 Robbie Williamson (robbiew (at] us.ibm.com)
     27 #     -Ported
     28 #
     29 #
     30 #---------------------------------------------------------------------------
     31 #Uncomment line below for debug output
     32 #trace_logic=${trace_logic:-"set -x"}
     33 $trace_logic
     34 
     35 do_cleanup()
     36 {
     37 	rm -rf $TCtmp
     38 }
     39 
     40 res=0
     41 CC=${CC:=gcc}
     42 LD=${LD:=ld}
     43 TCdat=${TCdat:-`pwd`}
     44 TCtmp=${TCtmp:-/tmp/ld-$$}
     45 mkdir $TCtmp
     46 
     47 if uname -a | grep -iq x86_64
     48 then
     49 	file $TCdat/ldmain.obj | grep 32-bit >/dev/null 2>&1
     50 	if [ $? -eq 0 ]; then
     51 		export LDEMULATION="elf_i386"
     52 	fi
     53 elif uname -a | grep -iq powerpc
     54 then
     55 	file $TCdat/f1.obj | grep 64-bit >/dev/null 2>&1
     56 	if [ $? -eq 0 ]; then
     57 		export LDEMULATION="elf64ppc"
     58 	fi
     59 fi
     60 
     61 #ASSERTION 1
     62 #Test for graceful failure when ld can't find a fileTest calling ld directly
     63 #
     64 #CODE
     65 	echo "Assertion 1 .................."
     66 	ld x.obj y.obj 2> $TCtmp/errmsg.out
     67 #cat <<EOF > $TCtmp/errmsg.exp
     68 #$LD: cannot open x.obj: No such file or directory
     69 #EOF
     70 
     71 #diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
     72 grep "$LD:" $TCtmp/errmsg.out | grep [xy].obj | grep -q "No such file or directory"
     73 if [ $? -eq 0 ]
     74 then
     75 	echo  "-)"
     76 else
     77 	echo "FAIL -  ld failed to give expected error msg"
     78 	do_cleanup
     79 	exit 1
     80 fi
     81 
     82 
     83 
     84 ##ASSERTION 2
     85 #Test for graceful failure when ld can't find a fileTest calling ld through cc
     86 #
     87 #CODE
     88 echo "Assertion 2 .................."
     89 $CC x.obj y.obj 2> $TCtmp/errmsg.out
     90 cat <<EOF > $TCtmp/errmsg.exp
     91 $CC: x.obj: No such file or directory
     92 $CC: y.obj: No such file or directory
     93 $CC: No input files
     94 EOF
     95 cat <<EOF > $TCtmp/errmsg.exp2
     96 $CC: error: x.obj: No such file or directory
     97 $CC: error: y.obj: No such file or directory
     98 $CC: fatal error: No input files
     99 compilation terminated.
    100 EOF
    101 
    102 diff -iw $TCtmp/errmsg.out $TCtmp/errmsg.exp
    103 if [ $? -eq 0 ]; then
    104 	echo  "-)"
    105 else
    106 	diff -iw $TCtmp/errmsg.out $TCtmp/errmsg.exp2
    107 	if [ $? -eq 0 ]; then
    108 		echo "-)"
    109 	else
    110 		echo "FAIL -  ld failed to give expected error msg"
    111 		do_cleanup
    112 		exit 1
    113 	fi
    114 fi
    115 
    116 
    117 
    118 ##ASSERTION 3
    119 #Test for graceful failure when ld can't find a fileTest calling ld directly, with a non-existent path
    120 #
    121 #CODE
    122 	echo "Assertion 3 .................."
    123 	$LD bad/x.obj 2> $TCtmp/errmsg.out
    124 #cat <<EOF > $TCtmp/errmsg.exp
    125 #$LD: cannot open bad/x.obj: No such file or directory
    126 #EOF
    127 cat $TCtmp/errmsg.out | grep "$LD:" | grep "bad/[xy].obj:" | grep -q "No such file or directory"
    128 
    129 #diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
    130 if [ $? -eq 0 ]
    131 then
    132 	echo  "-)"
    133 else
    134 	echo "FAIL -  ld failed to give expected error msg"
    135 	do_cleanup
    136 	exit 1
    137 fi
    138 
    139 
    140 
    141 ##ASSERTION 4
    142 #Test for graceful failure when ld can't find a fileTest calling ld through cc, with a non-existent path
    143 #
    144 #CODE
    145 	echo "Assertion 4 .................."
    146 	$CC bad/x.obj 2> $TCtmp/errmsg.out
    147 cat <<EOF > $TCtmp/errmsg.exp
    148 $CC: bad/x.obj: No such file or directory
    149 $CC: No input files
    150 EOF
    151 cat <<EOF > $TCtmp/errmsg.exp2
    152 $CC: error: bad/x.obj: No such file or directory
    153 $CC: fatal error: No input files
    154 compilation terminated.
    155 EOF
    156 
    157 diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
    158 if [ $? -eq 0 ]; then
    159 	echo  "-)"
    160 else
    161 	diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp2
    162 	if [ $? -eq 0 ]; then
    163 		echo "-)"
    164 	else
    165 		echo "FAIL -  ld failed to give expected error msg"
    166 		do_cleanup
    167 		exit 1
    168 	fi
    169 fi
    170 
    171 ## ASSERTION 5
    172 ## Making sure the "shared" option works as designed
    173 ##
    174 echo "Assertion 5 .................."
    175 
    176 $LD -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/test.lib
    177 file $TCtmp/test.lib | grep -q shared
    178 if [ $? -eq 0 ]; then
    179 	echo "-)"
    180 else
    181 	echo "FAIL - ld failed to build a shared object"
    182 	do_cleanup
    183 	exit 1
    184 fi
    185 
    186 
    187 ## ASSERTION 6
    188 ## Making sure that the linker ignores the "-Bstatic" option
    189 ## when using a dynamically linked shared object
    190 ##
    191 echo "Assertion 6 .................."
    192 
    193 $LD -Bdynamic -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/lola 2>$TCtmp/errmsg.out
    194 $LD -Bstatic  -L. $TCdat/ldmain.obj $TCdat/rd1.obj $TCtmp/lola -o $TCtmp/a.out 2> $TCtmp/errmsg.out
    195 #nm $TCtmp/a.out | grep -q DYNAMIC
    196 rc_code=$?
    197 
    198 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; } }')
    199 
    200 if [ $? -ne 0 ] ; then
    201 	echo "---"
    202 elif [ "$1" -le "2"  -a "$2" -le "15" ]; then
    203 	if [ $rc_code -eq 0 ]
    204 	then
    205 		echo "-)"
    206 	else
    207 		echo "FAIL - ld 9 failed ignore the -Bstatic option with \
    208 		      shared objects"
    209 		do_cleanup
    210 		exit 1
    211 	fi
    212 
    213 else
    214 	if [ $rc_code -eq 0 ]
    215 	then
    216 		echo "FAIL - ld failed to  reject the -Bstatic option \
    217 		      with shared objects"
    218 		do_cleanup
    219 		exit 1
    220 	else
    221 		echo "-)"
    222 	fi
    223 fi # end of checking for ld version less than 2.16
    224 
    225 ##
    226 ## ASSERTION 7
    227 ## Making sure that the linker does not accepts shared object
    228 ## when doing static linking try to link with a with and
    229 ## without -r*repeat with -r
    230 ##
    231 
    232 
    233 echo "Assertion 7 .................."
    234 $LD -Bdynamic -shared $TCdat/ldmain.obj $TCdat/f1.obj $TCdat/rf1.obj -o $TCtmp/lola -L/usr/lib/
    235 $LD -Bstatic -r  $TCdat/ldmain.obj $TCdat/f1.obj $TCdat/rf1.obj $TCtmp/lola -L/usr/lib/ 2> $TCtmp/errmsg.out
    236 cat <<EOF > $TCtmp/errmsg.exp
    237 $TCtmp/lola: could not read symbols: Invalid operation
    238 EOF
    239 
    240 grep -q  "could not read symbols: Invalid operation" $TCtmp/errmsg.out
    241 rc_grep=$?
    242 grep -q "ld: attempted static link of dynamic object" $TCtmp/errmsg.out
    243 if [ $rc_grep -eq 0  -o $? -eq 0 ]; then
    244 	echo "-)"
    245 else
    246 	echo "FAIL - ld failed to give expected error msg"
    247 	do_cleanup
    248 	exit 1
    249 fi
    250 
    251 do_cleanup
    252 echo "ld: PASS"
    253 exit 0
    254