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