• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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
35do_cleanup()
36{
37	rm -rf $TCtmp
38}
39
40res=0
41CC=${CC:=gcc}
42LD=${LD:=ld}
43TCdat=${TCdat:-`pwd`}
44TCtmp=${TCtmp:-/tmp/ld-$$}
45mkdir $TCtmp
46
47if uname -a | grep -iq x86_64
48then
49	file $TCdat/ldmain.obj | grep 32-bit >/dev/null 2>&1
50	if [ $? -eq 0 ]; then
51		export LDEMULATION="elf_i386"
52	fi
53elif uname -a | grep -iq powerpc
54then
55	file $TCdat/f1.obj | grep 64-bit >/dev/null 2>&1
56	if [ $? -eq 0 ]; then
57		export LDEMULATION="elf64ppc"
58	fi
59fi
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
72grep "$LD:" $TCtmp/errmsg.out | grep [xy].obj | grep -q "No such file or directory"
73if [ $? -eq 0 ]
74then
75	echo  "-)"
76else
77	echo "FAIL -  ld failed to give expected error msg"
78	do_cleanup
79	exit 1
80fi
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
88echo "Assertion 2 .................."
89$CC x.obj y.obj 2> $TCtmp/errmsg.out
90cat <<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
94EOF
95cat <<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
99compilation terminated.
100EOF
101
102diff -iw $TCtmp/errmsg.out $TCtmp/errmsg.exp
103if [ $? -eq 0 ]; then
104	echo  "-)"
105else
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
114fi
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
127cat $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
130if [ $? -eq 0 ]
131then
132	echo  "-)"
133else
134	echo "FAIL -  ld failed to give expected error msg"
135	do_cleanup
136	exit 1
137fi
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
147cat <<EOF > $TCtmp/errmsg.exp
148$CC: bad/x.obj: No such file or directory
149$CC: No input files
150EOF
151cat <<EOF > $TCtmp/errmsg.exp2
152$CC: error: bad/x.obj: No such file or directory
153$CC: fatal error: No input files
154compilation terminated.
155EOF
156
157diff -iw  $TCtmp/errmsg.out $TCtmp/errmsg.exp
158if [ $? -eq 0 ]; then
159	echo  "-)"
160else
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
169fi
170
171## ASSERTION 5
172## Making sure the "shared" option works as designed
173##
174echo "Assertion 5 .................."
175
176$LD -shared $TCdat/f1.obj $TCdat/d1.obj -o $TCtmp/test.lib
177file $TCtmp/test.lib | grep -q shared
178if [ $? -eq 0 ]; then
179	echo "-)"
180else
181	echo "FAIL - ld failed to build a shared object"
182	do_cleanup
183	exit 1
184fi
185
186
187## ASSERTION 6
188## Making sure that the linker ignores the "-Bstatic" option
189## when using a dynamically linked shared object
190##
191echo "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
196rc_code=$?
197
198set -- $($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
200if [ $? -ne 0 ] ; then
201	echo "---"
202elif [ "$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
213else
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
223fi # 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
233echo "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
236cat <<EOF > $TCtmp/errmsg.exp
237$TCtmp/lola: could not read symbols: Invalid operation
238EOF
239
240grep -q  "could not read symbols: Invalid operation" $TCtmp/errmsg.out
241rc_grep=$?
242grep -q "ld: attempted static link of dynamic object" $TCtmp/errmsg.out
243if [ $rc_grep -eq 0  -o $? -eq 0 ]; then
244	echo "-)"
245else
246	echo "FAIL - ld failed to give expected error msg"
247	do_cleanup
248	exit 1
249fi
250
251do_cleanup
252echo "ld: PASS"
253exit 0
254