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