1 # Expect script
for simple native Linux
/x86 tests.
2 # Copyright
(C
) 2018-2025 Free Software Foundation
, Inc.
4 # This file is part of the GNU Binutils.
6 # This
program is free software
; you can redistribute it and
/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation
; either version
3 of the License
, or
9 #
(at your option
) any later version.
11 # This
program is distributed in the hope that it will be useful
,
12 # but WITHOUT
ANY WARRANTY
; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License
for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this
program; if not
, write to the Free Software
18 # Foundation
, Inc.
, 51 Franklin Street
- Fifth Floor
, Boston
,
23 if { ![istarget
"i?86-*-linux*"] \
24 && ![istarget
"x86_64-*-linux*"] \
25 && ![istarget
"amd64-*-linux*"] } {
29 run_ld_link_tests
[list \
42 "-Bdynamic tmpdir/pr24920.so" \
50 # Test very simple native Linux
/x86 programs with linux
-x86.S.
51 run_ld_link_exec_tests
[list \
53 "Run PR ld/23428 test" \
54 "--no-dynamic-linker -z separate-code -z noexecstack" \
56 { linux
-x86.S pr23428.c dummy.s
} \
59 "$NOPIE_CFLAGS $NOSANITIZE_CFLAGS -fno-asynchronous-unwind-tables" \
64 run_ld_link_tests
[list \
66 "Build x86-feature-1" \
67 "-z separate-code -z shstk" \
69 "-mx86-used-note=yes" \
71 {{readelf
-n x86
-feature
-1a.rd
}} \
76 run_cc_link_tests
[list \
78 "Build indirect-extern-access-1.so" \
79 "-shared -z noexecstack" \
81 { indirect
-extern
-access
-1a.c
} \
83 "indirect-extern-access-1.so" \
86 "Build indirect-extern-access-1a without PIE" \
87 "$NOPIE_LDFLAGS -Wl,--no-as-needed -z noexecstack \
88 tmpdir
/indirect
-extern
-access
-1.so
" \
90 { indirect
-extern
-access.S indirect
-extern
-access
-1b.c
} \
91 {{readelf
-rn indirect
-extern
-access
-1.rd
}} \
92 "indirect-extern-access-1a" \
95 "Build indirect-extern-access-2.so" \
98 { indirect
-extern
-access
-2a.c
} \
100 "indirect-extern-access-2.so" \
103 "Build indirect-extern-access-2a without PIE" \
104 "$NOPIE_LDFLAGS -Wl,--no-as-needed -z noexecstack \
105 tmpdir
/indirect
-extern
-access
-2.so
" \
107 { indirect
-extern
-access.S indirect
-extern
-access
-1b.c
} \
108 {{readelf
-rn indirect
-extern
-access
-2.rd
}} \
109 "indirect-extern-access-2a" \
112 "Build indirect-extern-access-2b with PIE" \
113 "-pie -Wl,--no-as-needed -z noexecstack \
114 tmpdir
/indirect
-extern
-access
-2.so
" \
116 { indirect
-extern
-access.S indirect
-extern
-access
-2b.c
} \
117 {{readelf
-rn indirect
-extern
-access
-3.rd
}} \
118 "indirect-extern-access-2b" \
122 "-shared -Wl,-version-script,pr27128.t" \
130 "$NOPIE_LDFLAGS -Wl,--no-as-needed \
134 {{readelf
{--dyn
-syms
-W
} pr28158.rd
} {nm
-D pr28158.nd
}} \
139 "-r -nostdlib -Wl,-z,noexecstack" \
141 {pr29377a.c indirect
-extern
-access.S
} \
147 "$NOPIE_LDFLAGS tmpdir/pr29377.o" \
155 run_ld_link_exec_tests
[list \
157 "Run indirect-extern-access-1a without PIE" \
158 "$NOPIE_LDFLAGS -z noexecstack" \
160 { indirect
-extern
-access.S indirect
-extern
-access
-1b.c
} \
161 "indirect-extern-access-1a" \
166 "tmpdir/indirect-extern-access-1.so" \
169 "Run indirect-extern-access-1b with PIE" \
170 "-pie -z noexecstack" \
172 { indirect
-extern
-access.S indirect
-extern
-access
-1b.c
} \
173 "indirect-extern-access-1b" \
178 "tmpdir/indirect-extern-access-1.so" \
181 "Run indirect-extern-access-2a without PIE" \
182 "$NOPIE_LDFLAGS -z noexecstack" \
184 { indirect
-extern
-access.S indirect
-extern
-access
-2b.c
} \
185 "indirect-extern-access-2a" \
190 "tmpdir/indirect-extern-access-2.so" \
193 "Run indirect-extern-access-2b with PIE" \
194 "-pie -z noexecstack" \
196 { indirect
-extern
-access.S indirect
-extern
-access
-2b.c
} \
197 "indirect-extern-access-2b" \
202 "tmpdir/indirect-extern-access-2.so" \
206 # Run
-time tests which require working ifunc attribute support.
207 if { [check_ifunc_attribute_available
] } {
208 run_ld_link_exec_tests
[list \
221 # Old gcc silently ignores __attribute__
((aligned
())) with too big alignment.
222 proc compiler_honours_aligned
{ } {
223 global CC_FOR_TARGET READELF srcdir subdir
224 ld_compile $CC_FOR_TARGET $srcdir
/$subdir
/p_align
-1.c tmpdir
/p_align
-1.o
225 set output
[run_host_cmd
"$READELF" "-SW tmpdir/p_align-1.o"]
226 if { [regexp
{ [.
]data
*PROGBITS .
* 8388608[\n]} $output
] } {
232 if { [check_compiler_available
] && [compiler_honours_aligned
] } {
233 run_ld_link_exec_tests
[list \
235 "Run p_align-1a without PIE" \
244 "Run p_align-1b with PIE" \
253 "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
254 "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
262 "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
263 "-pie -Wl,-z,max-page-size=0x1000" \
272 unsupported
"p_align-1"
275 proc elfedit_test
{ options test output
} {
281 set test_name
"elfedit $options"
282 send_log
"$ELFEDIT $options tmpdir/$test\n"
283 set got
[remote_exec host
"$ELFEDIT $options tmpdir/$test" "" "/dev/null"]
284 if { [lindex $got
0] != 0 ||
![string match
"" [lindex $got 1]] } then {
288 send_log
"$READELF -n $options tmpdir/$test > tmpdir/$output.out\n"
289 set got
[remote_exec host
"$READELF -n tmpdir/$test" "" "/dev/null" "tmpdir/$output.out"]
290 if { [lindex $got
0] != 0 ||
![string match
"" [lindex $got 1]] } then {
294 if { [regexp_diff tmpdir
/$output.out $srcdir
/$subdir
/$output.rd
] } then {
301 elfedit_test
"--enable-x86-feature ibt --disable-x86-feature shstk" \
302 x86
-feature
-1 x86
-feature
-1b
303 elfedit_test
"--enable-x86-feature ibt" x86-feature-1 x86-feature-1b
304 elfedit_test
"--disable-x86-feature shstk" x86-feature-1 x86-feature-1c
305 elfedit_test
"--disable-x86-feature ibt" x86-feature-1 x86-feature-1d
306 elfedit_test
"--enable-x86-feature ibt --enable-x86-feature shstk" \
307 x86
-feature
-1 x86
-feature
-1e
309 proc check_pr25749a
{testname srcfilea srcfileb cflags ldflags lderror
} {
314 if { [istarget
"i?86-*-linux*"] } {
315 set output_arch
"i386:i386"
316 set output_target
"elf32-i386"
318 set output_arch
"i386:x86-64"
319 if {[istarget
"x86_64-*-linux*-gnux32"]} {
320 set output_target
"elf32-x86-64"
322 set output_target
"elf64-x86-64"
326 # Suppress warning
for unsupported attribute from older GCC.
329 exec cp $srcdir
/$subdir
/$srcfilea $srcfilea
330 exec chmod
+w $srcfilea
331 set pr25749_bin
"$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
332 send_log
"$pr25749_bin\n"
333 set got
[remote_exec host
"$pr25749_bin"]
334 if { [lindex $got
0] != 0 ||
![string match
"" [lindex $got 1]] } then {
336 fail
"Convert $srcfilea to $output_target"
340 if {"$lderror" == ""} {
341 run_cc_link_tests
[list \
343 "Build $testname ($ldflags $cflags)" \
344 "$ldflags tmpdir/pr25749-bin.o -z noexecstack" \
346 [list $srcfilea $srcfileb
]\
347 {{readelf
{-Wr
} pr25749.rd
}} \
351 run_ld_link_exec_tests
[list \
353 "Run ${testname}a ($ldflags $cflags)" \
354 "$ldflags tmpdir/pr25749-bin.o -z noexecstack" \
356 [list $srcfilea $srcfileb
]\
363 run_cc_link_tests
[list \
365 "Build $testname ($ldflags $cflags)" \
366 "$ldflags tmpdir/pr25749-bin.o -z noexecstack" \
368 [list $srcfilea $srcfileb
]\
369 [list
[list error_output $lderror
]] \
376 check_pr25749a
"pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
377 check_pr25749a
"pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
378 check_pr25749a
"pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
379 if { [istarget
"i?86-*-linux*"] || ![at_least_gcc_version 5 1] } {
380 check_pr25749a
"pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" ""
382 check_pr25749a
"pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "pr25749-1b.err"
384 check_pr25749a
"pr25749-1c" "pr25749-1.c" "pr25749-1c.c" "-fPIC" "-shared" "pr25749-1b.err"
385 check_pr25749a
"pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
386 check_pr25749a
"pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" ""
387 check_pr25749a
"pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
388 check_pr25749a
"pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""
389 check_pr25749a
"pr25754-1a" "pr25754-1a.c" "pr25754-1b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
390 check_pr25749a
"pr25754-1b" "pr25754-1a.c" "pr25754-1b.s" "-fPIE" "$NOPIE_LDFLAGS" ""
391 check_pr25749a
"pr25754-1c" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
392 check_pr25749a
"pr25754-1d" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "-pie" ""
393 if { [istarget
"i?86-*-linux*"] || [istarget "x86_64-*-linux*-gnux32"]} {
394 check_pr25749a
"pr25754-2a" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
395 check_pr25749a
"pr25754-2b" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "-pie" ""
397 check_pr25749a
"pr25754-3a" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
398 check_pr25749a
"pr25754-3b" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "-pie" ""
400 if { [istarget
"i?86-*-linux*"] } {
401 check_pr25749a
"pr25754-4a" "pr25754-4a.c" "pr25754-4b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
402 check_pr25749a
"pr25754-4b" "pr25754-4a.c" "pr25754-4b.s" "-fpie" "-pie" ""
403 check_pr25749a
"pr25754-5a" "pr25754-5a.c" "pr25754-5b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
404 check_pr25749a
"pr25754-5b" "pr25754-5a.c" "pr25754-5b.s" "-fpie" "-pie" ""
406 check_pr25749a
"pr25754-4a" "pr25754-4a.c" "pr25754-4c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
407 check_pr25749a
"pr25754-4b" "pr25754-4a.c" "pr25754-4c.s" "-fpie" "-pie" ""
408 check_pr25749a
"pr25754-5a" "pr25754-5a.c" "pr25754-5c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
409 check_pr25749a
"pr25754-5b" "pr25754-5a.c" "pr25754-5c.s" "-fpie" "-pie" ""
410 if { ![istarget
"x86_64-*-linux*-gnux32"]} {
411 check_pr25749a
"pr25754-6a" "pr25754-6a.c" "pr25754-6b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
412 check_pr25749a
"pr25754-6b" "pr25754-6a.c" "pr25754-6b.s" "-fpie" "-pie" ""
416 proc check_pr25749b
{testname srcfilea srcfileb cflags ldflags dsoldflags
args} {
421 if { [istarget
"i?86-*-linux*"] } {
422 set output_arch
"i386:i386"
423 set output_target
"elf32-i386"
425 set output_arch
"i386:x86-64"
426 if {[istarget
"x86_64-*-linux*-gnux32"]} {
427 set output_target
"elf32-x86-64"
429 set output_target
"elf64-x86-64"
433 exec cp $srcdir
/$subdir
/$srcfilea $srcfilea
434 exec chmod
+w $srcfilea
435 set pr25749_bin
"$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
436 send_log
"$pr25749_bin\n"
437 set got
[remote_exec host
"$pr25749_bin"]
438 if { [lindex $got
0] != 0 ||
![string match
"" [lindex $got 1]] } then {
440 fail
"Convert $srcfilea to $output_target"
444 run_cc_link_tests
[list \
446 "Build lib${testname}.so ($dsoldflags)" \
447 "-shared $dsoldflags tmpdir/pr25749-bin.o -z noexecstack" \
450 {{readelf
{-Wr
} pr25749.rd
}} \
451 "lib${testname}.so" \
456 if { [llength $
args] > 0 } {
457 set pass
[lindex $
args 0]
460 run_ld_link_exec_tests
[list \
462 "Run ${testname}b ($ldflags $cflags)" \
463 "$ldflags -Wl,--no-as-needed tmpdir/lib${testname}.so -z noexecstack" \
473 check_pr25749b
"pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
474 check_pr25749b
"pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
475 check_pr25749b
"pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
476 check_pr25749b
"pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
477 check_pr25749b
"pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "" "passall.out"
478 check_pr25749b
"pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
479 check_pr25749b
"pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "-Wl,-defsym=_begin=0"
480 check_pr25749b
"pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-defsym=_begin=0"
481 check_pr25749b
"pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-Bsymbolic -Wl,-defsym=_begin=0"
482 check_pr25749b
"pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
483 check_pr25749b
"pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" "" "passall.out"
484 check_pr25749b
"pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
485 check_pr25749b
"pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""