* ld-shared: New directory, with new files to test generating ELF
[binutils-gdb.git] / ld / testsuite / ld-shared / shared.exp
1 # Expect script for ld-shared tests
2 # Copyright (C) 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 # Compile the main program.
45 if ![ld_compile "$CC $CFLAGS" $srcdir$subdir/main.c tmpdir/main.o] {
46 return
47 }
48
49 # The shared library is composed of two files. First compile them
50 # without using -fpic. That should work on an ELF system, although it
51 # will be less efficient because the dynamic linker will need to do
52 # more relocation work. However, note that not using -fpic will cause
53 # some of the tests to return different results.
54 if ![ld_compile "$CC $CFLAGS" $srcdir$subdir/sh1.c tmpdir/sh1.o] {
55 return
56 }
57 if ![ld_compile "$CC $CFLAGS" $srcdir$subdir/sh2.c tmpdir/sh2.o] {
58 return
59 }
60
61 # Build the shared library.
62 if ![ld_simple_link $ld tmpdir/shnonpic.so {-shared tmpdir/sh1.o tmpdir/sh2.o}] {
63 fail "shared (non PIC)"
64 } else {
65 # Link against the shared library. Use -rpath so that the dynamic
66 # linker can locate the shared library at runtime.
67 if ![ld_link $ld tmpdir/shnonpic {-rpath tmpdir tmpdir/main.o tmpdir/shnonpic.so}] {
68 fail "shared (non PIC)"
69 } else {
70 # Run the resulting program
71 send_log "tmpdir/shnonpic >tmpdir/shnonpic.out\n"
72 verbose "tmpdir/shnonpic >tmpdir/shnonpic.out"
73 catch "exec tmpdir/shnonpic >tmpdir/shnonpic.out" exec_output
74 if ![string match "" $exec_output] then {
75 send_log "$exec_output\n"
76 verbose "$exec_output"
77 fail "shared (non PIC)"
78 } else {
79 send_log "diff tmpdir/shnonpic.out $srcdir$subdir/shared.dat\n"
80 verbose "diff tmpdir/shnonpic.out $srcdir$subdir/shared.dat"
81 catch "exec diff tmpdir/shnonpic.out $srcdir$subdir/shared.dat" exec_output
82 if [string match "" $exec_output] then {
83 pass "shared (non PIC)"
84 } else {
85 send_log "$exec_output\n"
86 verbose "$exec_output"
87 fail "shared (non PIC)"
88 }
89 }
90 }
91 }
92
93 # Now compile the code using -fpic. Unfortunately, the gcc argument
94 # is -fpic and the cc argument is -KPIC. We have to try both.
95
96 set picflag "-fpic"
97 send_log "$CC $picflag\n"
98 verbose "$CC $picflag"
99 catch "exec $CC $picflag" exec_output
100 send_log "$exec_output\n"
101 verbose "--" "$exec_output"
102 if { [string match "*illegal option*" $exec_output] \
103 || [string match "*option ignored*" $exec_output] \
104 || [string match "*unrecognized option*" $exec_output] } then {
105 set picflag "-KPIC"
106 }
107 verbose "Using $picflag to compile PIC code"
108
109 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir$subdir/sh1.c tmpdir/sh1.o] {
110 return
111 }
112 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir$subdir/sh2.c tmpdir/sh2.o] {
113 return
114 }
115
116 # Build the shared library.
117 if ![ld_simple_link $ld tmpdir/shpic.so {-shared tmpdir/sh1.o tmpdir/sh2.o}] {
118 fail "shared"
119 } else {
120 # Link against the shared library. Use -rpath so that the dynamic
121 # linker can locate the shared library at runtime.
122 if ![ld_link $ld tmpdir/shpic {-rpath tmpdir tmpdir/main.o tmpdir/shpic.so}] {
123 fail "shared"
124 } else {
125 # Run the resulting program
126 send_log "tmpdir/shpic >tmpdir/shpic.out\n"
127 verbose "tmpdir/shpic >tmpdir/shpic.out"
128 catch "exec tmpdir/shpic >tmpdir/shpic.out" exec_output
129 if ![string match "" $exec_output] then {
130 send_log "$exec_output\n"
131 verbose "$exec_output"
132 fail "shared"
133 } else {
134 send_log "diff tmpdir/shpic.out $srcdir$subdir/shared.dat\n"
135 verbose "diff tmpdir/shpic.out $srcdir$subdir/shared.dat"
136 catch "exec diff tmpdir/shpic.out $srcdir$subdir/shared.dat" exec_output
137 if [string match "" $exec_output] then {
138 pass "shared"
139 } else {
140 send_log "$exec_output\n"
141 verbose "$exec_output"
142 fail "shared"
143 }
144 }
145 }
146 }