Commit | Line | Data |
---|---|---|
6fc49d28 | 1 | # Expect script for ld-visibility tests |
4f38fc1c | 2 | # Copyright (C) 2000, 2001 Free Software Foundation |
6fc49d28 L |
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 | # and H.J. Lu (hjl@gnu.org) | |
20 | # | |
21 | ||
22 | # Make sure that ld can generate ELF shared libraries with visibility. | |
23 | ||
24 | # This test can only be run if ld generates native executables. | |
25 | if ![isnative] then {return} | |
26 | ||
27 | # This test can only be run on a couple of ELF platforms. | |
28 | # Square bracket expressions seem to confuse istarget. | |
29 | if { ![istarget i386-*-linux*] \ | |
30 | && ![istarget i486-*-linux*] \ | |
31 | && ![istarget i586-*-linux*] \ | |
32 | && ![istarget i686-*-linux*] \ | |
33 | && ![istarget m68k-*-linux*] \ | |
34 | && ![istarget powerpc-*-linux*] \ | |
4f38fc1c | 35 | && ![istarget arm*-*-linux*] \ |
6fc49d28 L |
36 | && ![istarget sparc*-*-linux*] } { |
37 | return | |
38 | } | |
39 | ||
40 | if { [istarget *-*-linux*aout*] \ | |
41 | || [istarget *-*-linux*oldld*] } { | |
42 | return | |
43 | } | |
44 | ||
45 | set tmpdir tmpdir | |
46 | set SHCFLAG "" | |
47 | ||
48 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
49 | ||
50 | # AIX shared libraries do not seem to support useful features, | |
51 | # like overriding the shared library function or letting the | |
52 | # shared library refer to objects defined in the main program. We | |
53 | # avoid testing those features. | |
54 | set SHCFLAG "-DXCOFF_TEST" | |
55 | ||
56 | # The AIX 3.2.5 loader appears to randomly fail when loading | |
57 | # shared libraries from NSF mounted partitions, so we avoid any | |
58 | # potential problems by using a local directory. | |
59 | catch {exec /bin/sh -c "echo $$"} pid | |
60 | set tmpdir /usr/tmp/ld.$pid | |
61 | catch "exec mkdir $tmpdir" exec_status | |
62 | ||
63 | # On AIX, we need to explicitly export the symbols the shared | |
64 | # library is going to provide, and need. | |
65 | set file [open $tmpdir/xcoff.exp w] | |
66 | puts $file shlibvar1 | |
67 | puts $file shlibvar2 | |
68 | puts $file shlib_shlibvar1 | |
69 | puts $file shlib_shlibvar2 | |
70 | puts $file shlib_shlibcall | |
71 | puts $file shlib_shlibcalled | |
72 | puts $file shlib_checkfunptr1 | |
73 | puts $file shlib_getfunptr1 | |
74 | puts $file shlib_check | |
75 | close $file | |
76 | } | |
77 | ||
08c44e65 L |
78 | set support_protected "no" |
79 | ||
80 | if [istarget *-*-linux*] { | |
360e9586 | 81 | if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] { |
08c44e65 L |
82 | if [ld_link $ld $tmpdir/main "$tmpdir/main.o"] { |
83 | catch "exec $tmpdir/main" support_protected | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
6fc49d28 L |
88 | # The test procedure. |
89 | proc visibility_test { visibility progname testname main sh1 sh2 dat args } { | |
90 | global ld | |
91 | global srcdir | |
92 | global subdir | |
93 | global exec_output | |
94 | global link_output | |
95 | global host_triplet | |
96 | global tmpdir | |
97 | ||
98 | if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" } | |
99 | ||
100 | # Build the shared library. | |
101 | # On AIX, we need to use an export file. | |
102 | set shared -shared | |
103 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
104 | set shared "-bM:SRE -bE:$tmpdir/xcoff.exp" | |
105 | } | |
106 | if {![ld_simple_link $ld $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} { | |
7cda33a1 L |
107 | if { [ string match $visibility "hidden_undef" ] |
108 | && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output] | |
109 | && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } { | |
110 | pass "$testname" | |
111 | } else { if { [ string match $visibility "protected_undef" ] | |
112 | && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output] | |
113 | && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } { | |
114 | pass "$testname" | |
115 | } else { | |
116 | fail "$testname" | |
117 | }} | |
6fc49d28 L |
118 | return |
119 | } | |
120 | ||
121 | # Link against the shared library. Use -rpath so that the | |
122 | # dynamic linker can locate the shared library at runtime. | |
123 | # On AIX, we must include /lib in -rpath, as otherwise the loader | |
124 | # can not find -lc. | |
125 | set rpath $tmpdir | |
126 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
127 | set rpath /lib:$tmpdir | |
128 | } | |
129 | if ![ld_link $ld $tmpdir/$progname "-rpath $rpath $tmpdir/$main $tmpdir/$progname.so"] { | |
130 | if { [ string match $visibility "hidden" ] | |
7cda33a1 L |
131 | && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output] |
132 | && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } { | |
133 | pass "$testname" | |
134 | } else { if { [ string match $visibility "hidden_undef_def" ] | |
135 | && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output] | |
136 | && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } { | |
6fc49d28 L |
137 | pass "$testname" |
138 | } else { | |
139 | fail "$testname" | |
7cda33a1 | 140 | }} |
6fc49d28 L |
141 | return |
142 | } | |
143 | ||
7cda33a1 L |
144 | if { [ string match $visibility "hidden" ] |
145 | || [ string match $visibility "hidden_undef" ] | |
146 | || [ string match $visibility "protected_undef" ] } { | |
6fc49d28 L |
147 | fail "$testname" |
148 | } | |
149 | ||
150 | # Run the resulting program | |
151 | send_log "$tmpdir/$progname >$tmpdir/$progname.out\n" | |
152 | verbose "$tmpdir/$progname >$tmpdir/$progname.out" | |
153 | catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output | |
154 | if ![string match "" $exec_output] then { | |
155 | send_log "$exec_output\n" | |
156 | verbose "$exec_output" | |
157 | fail "$testname" | |
158 | return | |
159 | } | |
160 | ||
161 | send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n" | |
162 | verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" | |
163 | catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output | |
164 | set exec_output [prune_warnings $exec_output] | |
165 | ||
166 | if {![string match "" $exec_output]} then { | |
167 | send_log "$exec_output\n" | |
168 | verbose "$exec_output" | |
169 | fail "$testname" | |
170 | return | |
171 | } | |
172 | ||
173 | pass "$testname" | |
174 | } | |
175 | ||
176 | proc visibility_run {visibility} { | |
177 | global CC | |
178 | global CFLAGS | |
179 | global SHCFLAG | |
180 | global srcdir | |
181 | global subdir | |
182 | global tmpdir | |
183 | global picflag | |
184 | global target_triplet | |
08c44e65 | 185 | global support_protected |
6fc49d28 L |
186 | |
187 | if [ string match $visibility "hidden" ] { | |
188 | set VSBCFLAG "-DHIDDEN_TEST" | |
189 | } else { if [ string match $visibility "hidden_normal" ] { | |
190 | set VSBCFLAG "-DHIDDEN_NORMAL_TEST" | |
7cda33a1 L |
191 | } else { if [ string match $visibility "hidden_undef" ] { |
192 | set VSBCFLAG "-DHIDDEN_UNDEF_TEST" | |
193 | } else { if [ string match $visibility "hidden_undef_def" ] { | |
194 | set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST" | |
195 | } else { if [ string match $visibility "hidden_weak" ] { | |
196 | set VSBCFLAG "-DHIDDEN_WEAK_TEST" | |
6fc49d28 L |
197 | } else { if [ string match $visibility "protected" ] { |
198 | set VSBCFLAG "-DPROTECTED_TEST" | |
7cda33a1 L |
199 | } else { if [ string match $visibility "protected_undef" ] { |
200 | set VSBCFLAG "-DPROTECTED_UNDEF_TEST" | |
201 | } else { if [ string match $visibility "protected_undef_def" ] { | |
202 | set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST" | |
203 | } else { if [ string match $visibility "protected_weak" ] { | |
204 | set VSBCFLAG "-DPROTECTED_WEAK_TEST" | |
6fc49d28 L |
205 | } else { |
206 | set VSBCFLAG "" | |
7cda33a1 | 207 | }}}}}}}}} |
6fc49d28 L |
208 | |
209 | # Compile the main program. | |
360e9586 | 210 | if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] { |
6fc49d28 L |
211 | unresolved "visibility ($visibility) (non PIC)" |
212 | unresolved "visibility ($visibility)" | |
213 | } else { | |
214 | # The shared library is composed of two files. First compile them | |
215 | # without using -fpic. That should work on an ELF system, | |
216 | # although it will be less efficient because the dynamic linker | |
217 | # will need to do more relocation work. However, note that not | |
218 | # using -fpic will cause some of the tests to return different | |
219 | # results. | |
360e9586 L |
220 | if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o] |
221 | || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } { | |
6fc49d28 L |
222 | unresolved "visibility ($visibility) (non PIC)" |
223 | } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
224 | visibility_test $visibility vnp "visibility ($visibility) (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff | |
225 | } else { | |
226 | # SunOS non PIC shared libraries don't permit some cases of | |
227 | # overriding. | |
1345a0c0 L |
228 | if { [ string match $visibility "protected" ] |
229 | || [ string match $visibility "protected_undef_def" ] } { | |
08c44e65 L |
230 | if [ string match $support_protected "no" ] { |
231 | setup_xfail $target_triplet | |
232 | } | |
6fc49d28 L |
233 | } else { |
234 | setup_xfail "*-*-sunos4*" | |
235 | } | |
d1d8dddf L |
236 | if { [ string match $visibility "hidden_weak" ] |
237 | || [ string match $visibility "protected_weak" ] } { | |
238 | setup_xfail "powerpc-*-linux*" | |
239 | } | |
6fc49d28 L |
240 | visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb |
241 | ||
242 | # Test ELF shared library relocations with a non-zero load | |
243 | # address for the library. Near as I can tell, the R_*_RELATIVE | |
244 | # relocations for various targets are broken in the case where | |
245 | # the load address is not zero (which is the default). | |
1345a0c0 L |
246 | if { [ string match $visibility "protected" ] |
247 | || [ string match $visibility "protected_undef_def" ] } { | |
08c44e65 L |
248 | if [ string match $support_protected "no" ] { |
249 | setup_xfail $target_triplet | |
250 | } | |
6fc49d28 L |
251 | } else { |
252 | setup_xfail "*-*-sunos4*" | |
253 | setup_xfail "*-*-linux*libc1" | |
254 | } | |
d1d8dddf L |
255 | if { [ string match $visibility "hidden_normal" ] |
256 | || [ string match $visibility "hidden_weak" ] | |
257 | || [ string match $visibility "protected" ] | |
258 | || [ string match $visibility "protected_undef_def" ] | |
259 | || [ string match $visibility "protected_weak" ] | |
260 | || [ string match $visibility "normal" ] } { | |
261 | setup_xfail "powerpc-*-linux*" | |
262 | } | |
6fc49d28 L |
263 | visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \ |
264 | mainnp.o sh1np.o sh2np.o elfvsb \ | |
265 | "-T $srcdir/$subdir/elf-offset.ld" | |
266 | } } | |
267 | ||
268 | # Now compile the code using -fpic. | |
269 | ||
360e9586 L |
270 | if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o] |
271 | || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } { | |
6fc49d28 L |
272 | unresolved "visibility ($visibility)" |
273 | } else { | |
1345a0c0 L |
274 | if { [ string match $visibility "protected" ] |
275 | || [ string match $visibility "protected_undef_def" ] } { | |
08c44e65 L |
276 | if [ string match $support_protected "no" ] { |
277 | setup_xfail $target_triplet | |
278 | } | |
6fc49d28 L |
279 | } |
280 | # SunOS can not compare function pointers correctly | |
281 | if [istarget "*-*-sunos4*"] { | |
282 | visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4 | |
283 | } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
284 | visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff | |
285 | } else { | |
286 | visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb | |
287 | } } | |
288 | } | |
289 | } | |
290 | ||
291 | # Now do the same tests again, but this time compile main.c PIC. | |
360e9586 | 292 | if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] { |
6fc49d28 L |
293 | unresolved "visibility ($visibility) (PIC main, non PIC so)" |
294 | unresolved "visibility ($visibility) (PIC main)" | |
295 | } else { | |
296 | if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } { | |
297 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
298 | visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff | |
299 | } else { | |
300 | # SunOS non PIC shared libraries don't permit some cases of | |
301 | # overriding. | |
1345a0c0 L |
302 | if { [ string match $visibility "protected" ] |
303 | || [ string match $visibility "protected_undef_def" ] } { | |
08c44e65 L |
304 | if [ string match $support_protected "no" ] { |
305 | setup_xfail $target_triplet | |
306 | } | |
6fc49d28 L |
307 | } else { |
308 | setup_xfail "*-*-sunos4*" | |
309 | } | |
d1d8dddf L |
310 | if { [ string match $visibility "hidden_weak" ] |
311 | || [ string match $visibility "protected_weak" ] } { | |
312 | setup_xfail "powerpc-*-linux*" | |
313 | } | |
6fc49d28 L |
314 | visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb |
315 | } | |
316 | } else { | |
317 | unresolved "visibility (PIC main, non PIC so)" | |
318 | } | |
319 | ||
320 | if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } { | |
1345a0c0 L |
321 | if { [ string match $visibility "protected" ] |
322 | || [ string match $visibility "protected_undef_def" ] } { | |
08c44e65 L |
323 | if [ string match $support_protected "no" ] { |
324 | setup_xfail $target_triplet | |
325 | } | |
6fc49d28 L |
326 | } |
327 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
328 | visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff | |
329 | } else { | |
330 | visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb | |
331 | } | |
332 | } else { | |
333 | unresolved "visibility ($visibility) (PIC main)" | |
334 | } | |
335 | } | |
336 | } | |
337 | ||
338 | if [istarget mips*-*-*] { | |
339 | set picflag "" | |
340 | } else { | |
341 | # Unfortunately, the gcc argument is -fpic and the cc argument is | |
342 | # -KPIC. We have to try both. | |
343 | set picflag "-fpic" | |
344 | send_log "$CC $picflag\n" | |
345 | verbose "$CC $picflag" | |
346 | catch "exec $CC $picflag" exec_output | |
347 | send_log "$exec_output\n" | |
348 | verbose "--" "$exec_output" | |
349 | if { [string match "*illegal option*" $exec_output] \ | |
350 | || [string match "*option ignored*" $exec_output] \ | |
351 | || [string match "*unrecognized option*" $exec_output] \ | |
352 | || [string match "*passed to ld*" $exec_output] } { | |
353 | if [istarget *-*-sunos4*] { | |
354 | set picflag "-pic" | |
355 | } else { | |
356 | set picflag "-KPIC" | |
357 | } | |
358 | } | |
359 | } | |
360 | verbose "Using $picflag to compile PIC code" | |
361 | ||
362 | visibility_run hidden | |
363 | visibility_run hidden_normal | |
7cda33a1 L |
364 | visibility_run hidden_undef |
365 | visibility_run hidden_undef_def | |
366 | visibility_run hidden_weak | |
6fc49d28 | 367 | visibility_run protected |
7cda33a1 L |
368 | visibility_run protected_undef |
369 | visibility_run protected_undef_def | |
370 | visibility_run protected_weak | |
6fc49d28 L |
371 | visibility_run normal |
372 | ||
373 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
374 | # Remove the temporary directory. | |
375 | catch "exec rm -rf $tmpdir" exec_status | |
376 | } |