aboutsummaryrefslogtreecommitdiff
path: root/ld/testsuite/ld-shared/shared.exp
blob: 0e45f5c042310c3a56505f1adaf8adc3646200df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Expect script for ld-shared tests
#   Copyright (C) 1994-2021 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Written by Ian Lance Taylor (ian@cygnus.com)
#

# Make sure that ld can generate ELF shared libraries.
# Note that linking against ELF shared libraries is tested by the
# bootstrap test.

# Check to see if the C compiler works
if { ![check_compiler_available] } {
    return
}

# This test can only be run on a couple of ELF platforms.
# Square bracket expressions seem to confuse istarget.
if { ![istarget hppa*64*-*-hpux*] \
     && ![istarget hppa*-*-linux*] \
     && ![istarget i?86-*-sysv4*] \
     && ![istarget i?86-*-unixware] \
     && ![istarget i?86-*-elf*] \
     && ![istarget i?86-*-linux*] \
     && ![istarget i?86-*-gnu*] \
     && ![istarget *-*-nacl*] \
     && ![istarget ia64-*-elf*] \
     && ![istarget ia64-*-linux*] \
     && ![istarget m68k-*-linux*] \
     && ![istarget mips*-*-irix5*] \
     && ![istarget mips*-*-linux*] \
     && ![istarget powerpc*-*-elf*] \
     && ![istarget powerpc*-*-linux*] \
     && ![istarget powerpc*-*-sysv4*] \
     && ![istarget sparc*-*-elf] \
     && ![istarget sparc*-*-solaris2*] \
     && ![istarget sparc*-*-linux*] \
     && ![istarget arm*-*-linux*] \
     && ![istarget alpha*-*-linux*] \
     && ![istarget rs6000*-*-aix*] \
     && ![istarget powerpc*-*-aix*] \
     && ![istarget s390*-*-linux*] \
     && ![istarget aarch64*-*-linux*] \
     && ![istarget x86_64-*-linux*] } {
    return
}

set tmpdir tmpdir
set SHCFLAG ""
set shared_needs_pic "no"

# Disable all sanitizers.
set old_CFLAGS "$CFLAGS"
append CFLAGS " $NOSANITIZE_CFLAGS"

if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {

    # AIX shared libraries do not seem to support useful features,
    # like overriding the shared library function or letting the
    # shared library refer to objects defined in the main program.  We
    # avoid testing those features.
    set SHCFLAG "-DXCOFF_TEST"

    # The AIX 3.2.5 loader appears to randomly fail when loading
    # shared libraries from NSF mounted partitions, so we avoid any
    # potential problems by using a local directory.
    catch {exec /bin/sh -c "echo $$"} pid
    set tmpdir /usr/tmp/ld.$pid
    catch "exec mkdir $tmpdir" exec_status

    # On AIX, we need to explicitly export the symbols the shared
    # library is going to provide, and need.
    set file [open $tmpdir/xcoff.exp w]
    puts $file shlibvar1
    puts $file shlibvar2
    puts $file shlib_shlibvar1
    puts $file shlib_shlibvar2
    puts $file shlib_shlibcall
    puts $file shlib_shlibcalled
    puts $file shlib_checkfunptr1
    puts $file shlib_getfunptr1
    puts $file shlib_check
    close $file
}

if [istarget arm*-*-linux*] {
    # On ARM section anchors can change the symbol pre-emptability for
    # non-PIC shared libraries, causing these tests to fail.  Turn section
    # anchors off.
    set SHCFLAG "-fno-section-anchors"

    # On targets that have MOVW the compiler will emit relocations which
    # the linker doesn't support when compiling -shared without -fpic.  The
    # test to find out whether we want to XFAIL the non-PIC tests requires
    # a compile - so we pre-calculate it here.  We also note that this can
    # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
    if [istarget arm*-*-*eabi*] {
	set file [open $tmpdir/movw-detect.c w]
	puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
	close $file
	if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
	    set shared_needs_pic "yes"
	}
    }
}

# The test procedure.
proc shared_test { progname testname main sh1 sh2 dat args } {
    global CC
    global srcdir
    global subdir
    global exec_output
    global host_triplet
    global tmpdir

    if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }

    # Build the shared library.
    # On AIX, we need to use an export file.
    set shared -shared
    if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
	set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
    }
    if { [is_elf_format] && [check_shared_lib_support] } {
	append shared " -Wl,-z,notext"
    }
    if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
        fail "$testname"
    	return
    }

    # Link against the shared library.  Use -rpath so that the
    # dynamic linker can locate the shared library at runtime.
    # On AIX, we must include /lib in -rpath, as otherwise the loader
    # can not find -lc.
    set rpath $tmpdir
    if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
	set rpath /lib:$tmpdir
    }
    if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
    	fail "$testname"
    	return
    }

    if ![isnative] {
	unsupported $testname
	return
    }

    # Run the resulting program
    send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
    verbose "$tmpdir/$progname >$tmpdir/$progname.out"
    catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
    if ![string match "" $exec_output] then {
	send_log "$exec_output\n"
	verbose "$exec_output"
	fail "$testname"
    	return
    }

    send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
    verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
    catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
    set exec_output [prune_warnings $exec_output]

    if {![string match "" $exec_output]} then {
	send_log "$exec_output\n"
	verbose "$exec_output"
	fail "$testname"
        return
    }

    pass "$testname"
}

# Old version of GCC for MIPS default to enabling -fpic
# and get confused if it is used on the command line.
if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
    set picflag ""
} else {
    # Unfortunately, the gcc argument is -fpic and the cc argument is
    # -KPIC.  We have to try both.
    set picflag "-fpic"
    send_log "$CC $picflag\n"
    verbose "$CC $picflag"
    catch "exec $CC $picflag" exec_output
    send_log "$exec_output\n"
    verbose "--" "$exec_output"
    if { [string match "*illegal option*" $exec_output] \
	 || [string match "*option ignored*" $exec_output] \
	 || [string match "*unrecognized option*" $exec_output] \
	 || [string match "*passed to ld*" $exec_output] } {
	set picflag "-KPIC"
    }
}
verbose "Using $picflag to compile PIC code"

# Compile the main program.
if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
    unsupported "shared (non PIC)"
    unsupported "shared"
} else {
    # The shared library is composed of two files.  First compile them
    # without using -fpic.  That should work on an ELF system,
    # although it will be less efficient because the dynamic linker
    # will need to do more relocation work.  However, note that not
    # using -fpic will cause some of the tests to return different
    # results.  Make sure that PLT is used since PLT is expected.
    global PLT_CFLAGS NOPIE_CFLAGS
    if { ![ld_compile "$CC $PLT_CFLAGS $NOPIE_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
	 || ![ld_compile "$CC $PLT_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
	unsupported "shared (non PIC)"
    } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
	shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
    } else {
	# Solaris defaults to -z text.
	setup_xfail "*-*-solaris2*"
	setup_xfail "ia64-*-linux*"
	setup_xfail "alpha*-*-linux*"
	setup_xfail "powerpc64*-*-*"
	if { ![istarget hppa*64*-*-linux*] } {
	    setup_xfail "hppa*-*-linux*"
	}
	if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
	    setup_xfail "sparc*-*-linux*"
	}
	if { [is_elf64 $tmpdir/mainnp.o] } {
	    setup_xfail "x86_64-*-linux*"
	}
	setup_xfail "x86_64-*-linux-gnux32"
	setup_xfail "s390x-*-linux*"
	if [ string match $shared_needs_pic "yes" ] {
	    setup_xfail "arm*-*-linux*"
	}
	setup_xfail "aarch64*-*-linux*"
	shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared

	# Test ELF shared library relocations with a non-zero load
	# address for the library.  Near as I can tell, the R_*_RELATIVE
	# relocations for various targets are broken in the case where
	# the load address is not zero (which is the default).
	setup_xfail "*-*-linux*libc1"
	setup_xfail "powerpc*-*-linux*"
	setup_xfail "ia64-*-linux*"
	setup_xfail "alpha*-*-linux*"
	setup_xfail "mips*-*-linux*"
	if { ![istarget hppa*64*-*-linux*] } {
	    setup_xfail "hppa*-*-linux*"
	}
	if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
	    setup_xfail "sparc*-*-linux*"
	}
	if { [is_elf64 $tmpdir/mainnp.o] } {
	    setup_xfail "x86_64-*-linux*"
	}
	setup_xfail "x86_64-*-linux-gnux32"
	setup_xfail "s390x-*-linux*"
	if [ string match $shared_needs_pic "yes" ] {
	    setup_xfail "arm*-*-linux*"
	}
	setup_xfail "aarch64*-*-linux*"
	# Solaris defaults to -z text.
	setup_xfail "*-*-solaris2*"
	shared_test shnp "shared (non PIC, load offset)" \
		mainnp.o sh1np.o sh2np.o shared \
		"-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
    } }

    # Now compile the code using -fpic.

    if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
	 || ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
	unsupported "shared"
    } else {
	if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
	    shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
	} else {
	    shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
	    ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
	    ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
	    shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
	    ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
	    ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
	}
    }
}

# Now do the same tests again, but this time compile main.c PIC.
if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
    unsupported "shared (PIC main, non PIC so)"
    unsupported "shared (PIC main)"
} else {
    if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
        if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
	    shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
	} else {
	    # Solaris defaults to -z text.
	    setup_xfail "*-*-solaris2*"
	    setup_xfail "ia64-*-linux*"
	    setup_xfail "alpha*-*-linux*"
	    setup_xfail "powerpc64*-*-*"
	    if { ![istarget hppa*64*-*-linux*] } {
		setup_xfail "hppa*-*-linux*"
	    }
	    if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
		setup_xfail "sparc*-*-linux*"
	    }
	    if { [is_elf64 $tmpdir/mainp.o] } {
		setup_xfail "x86_64-*-linux*"
	    }
	    setup_xfail "x86_64-*-linux-gnux32"
	    setup_xfail "s390x-*-linux*"
	    if [ string match $shared_needs_pic "yes" ] {
		setup_xfail "arm*-*-linux*"
	    }
	    setup_xfail "aarch64*-*-linux*"
	    shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
	}
    } else {
	unsupported "shared (PIC main, non PIC so)"
    }

    if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
        if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
	    shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
	} else {
	    shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
	}
    } else {
	unsupported "shared (PIC main)"
    }
}

set CFLAGS "$old_CFLAGS"

if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
    # Remove the temporary directory.
    catch "exec rm -rf $tmpdir" exec_status
}