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