fsf address update, but not in COPYING files
[binutils-gdb.git] / ld / testsuite / ld-shared / shared.exp
1 # Expect script for ld-shared tests
2 # Copyright (C) 1994,1995 Free Software Foundation
3 #
4 # This file is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 #
18 # Written by Ian Lance Taylor (ian@cygnus.com)
19 #
20
21 # Make sure that ld can generate ELF shared libraries.
22 # Note that linking against ELF shared libraries is tested by the
23 # bootstrap test.
24
25 # This test can only be run if ld generates native executables.
26 if ![isnative] then {return}
27
28 # This test can only be run on a couple of ELF platforms.
29 # Square bracket expressions seem to confuse istarget.
30 if { ![istarget i386-*-sysv4*] \
31 && ![istarget i486-*-sysv4*] \
32 && ![istarget i586-*-sysv4*] \
33 && ![istarget i386-*-unixware] \
34 && ![istarget i486-*-unixware] \
35 && ![istarget i586-*-unixware] \
36 && ![istarget i386-*-elf*] \
37 && ![istarget i486-*-elf*] \
38 && ![istarget i586-*-elf*] \
39 && ![istarget sparc*-*-elf] \
40 && ![istarget sparc*-*-solaris2*]} then {
41 return
42 }
43
44 # The test procedure.
45 proc shared_test { progname testname main sh1 sh2 } {
46 global ld
47 global srcdir
48 global subdir
49 global exec_output
50 global host_triplet
51
52 # Build the shared library.
53 if {![ld_simple_link $ld tmpdir/$progname.so "-shared tmpdir/$sh1 tmpdir/$sh2"]} {
54 fail "$testname"
55 return
56 }
57
58 # Link against the shared library. Use -rpath so that the
59 # dynamic linker can locate the shared library at runtime.
60 if ![ld_link $ld tmpdir/$progname "-rpath tmpdir tmpdir/$main tmpdir/$progname.so"] {
61 fail "$testname"
62 return
63 }
64
65 # Run the resulting program
66 send_log "tmpdir/$progname >tmpdir/$progname.out\n"
67 verbose "tmpdir/$progname >tmpdir/$progname.out"
68 catch "exec tmpdir/$progname >tmpdir/$progname.out" exec_output
69 if ![string match "" $exec_output] then {
70 send_log "$exec_output\n"
71 verbose "$exec_output"
72 fail "$testname"
73 return
74 }
75
76 send_log "diff tmpdir/$progname.out $srcdir$subdir/shared.dat\n"
77 verbose "diff tmpdir/$progname.out $srcdir$subdir/shared.dat"
78 catch "exec diff tmpdir/$progname.out $srcdir$subdir/shared.dat" exec_output
79 set exec_output [prune_system_crud $host_triplet $exec_output]
80
81 if {![string match "" $exec_output]} then {
82 send_log "$exec_output\n"
83 verbose "$exec_output"
84 fail "$testname"
85 return
86 }
87
88 pass "$testname"
89 }
90
91 # Unfortunately, the gcc argument is -fpic and the cc argument is
92 # -KPIC. We have to try both.
93
94 set picflag "-fpic"
95 send_log "$CC $picflag\n"
96 verbose "$CC $picflag"
97 catch "exec $CC $picflag" exec_output
98 send_log "$exec_output\n"
99 verbose "--" "$exec_output"
100 if { [string match "*illegal option*" $exec_output] \
101 || [string match "*option ignored*" $exec_output] \
102 || [string match "*unrecognized option*" $exec_output] } then {
103 set picflag "-KPIC"
104 }
105 verbose "Using $picflag to compile PIC code"
106
107 # Compile the main program.
108 if ![ld_compile "$CC $CFLAGS" $srcdir$subdir/main.c tmpdir/mainnp.o] {
109 unresolved "shared (non PIC)"
110 unresolved "shared"
111 } else {
112 # The shared library is composed of two files. First compile them
113 # without using -fpic. That should work on an ELF system,
114 # although it will be less efficient because the dynamic linker
115 # will need to do more relocation work. However, note that not
116 # using -fpic will cause some of the tests to return different
117 # results.
118 if { ![ld_compile "$CC $CFLAGS" $srcdir$subdir/sh1.c tmpdir/sh1np.o]
119 || ![ld_compile "$CC $CFLAGS" $srcdir$subdir/sh2.c tmpdir/sh2np.o] } {
120 unresolved "shared (non PIC)"
121 } else {
122 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o
123 }
124
125 # Now compile the code using -fpic.
126
127 if { ![ld_compile "$CC $CFLAGS $picflag" $srcdir$subdir/sh1.c tmpdir/sh1p.o]
128 || ![ld_compile "$CC $CFLAGS $picflag" $srcdir$subdir/sh2.c tmpdir/sh2p.o] } {
129 unresolved "shared"
130 } else {
131 shared_test shp "shared" mainnp.o sh1p.o sh2p.o
132 }
133 }
134
135 # Now do the same tests again, but this time compile main.c PIC.
136 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir$subdir/main.c tmpdir/mainp.o] {
137 unresolved "shared (PIC main, non PIC so)"
138 unresolved "shared (PIC main)"
139 } else {
140 if { [file exists tmpdir/sh1np.o ] && [ file exists tmpdir/sh2np.o ] } {
141 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o
142 } else {
143 unresolved "shared (PIC main, non PIC so)"
144 }
145
146 if { [file exists tmpdir/sh1p.o ] && [ file exists tmpdir/sh2p.o ] } {
147 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o
148 } else {
149 unresolved "shared (PIC main)"
150 }
151 }