1 # Expect script for simple native Linux/x86 tests.
2 # Copyright (C) 2018-2021 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" \
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" \
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 \
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 \
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 \
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 run_ld_link_exec_tests [list \
124 "Run indirect-extern-access-1a without PIE" \
127 { indirect-extern-access.S indirect-extern-access-1b.c } \
128 "indirect-extern-access-1a" \
133 "tmpdir/indirect-extern-access-1.so" \
136 "Run indirect-extern-access-1b with PIE" \
139 { indirect-extern-access.S indirect-extern-access-1b.c } \
140 "indirect-extern-access-1b" \
145 "tmpdir/indirect-extern-access-1.so" \
148 "Run indirect-extern-access-2a without PIE" \
151 { indirect-extern-access.S indirect-extern-access-2b.c } \
152 "indirect-extern-access-2a" \
157 "tmpdir/indirect-extern-access-2.so" \
160 "Run indirect-extern-access-2b with PIE" \
163 { indirect-extern-access.S indirect-extern-access-2b.c } \
164 "indirect-extern-access-2b" \
169 "tmpdir/indirect-extern-access-2.so" \
173 proc elfedit_test { options test output } {
179 set test_name "elfedit $options"
180 send_log "$ELFEDIT $options tmpdir/$test\n"
181 set got [remote_exec host "$ELFEDIT $options tmpdir/$test" "" "/dev/null"]
182 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
186 send_log "$READELF -n $options tmpdir/$test > tmpdir/$output.out\n"
187 set got [remote_exec host "$READELF -n tmpdir/$test" "" "/dev/null" "tmpdir/$output.out"]
188 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
192 if { [regexp_diff tmpdir/$output.out $srcdir/$subdir/$output.rd] } then {
199 elfedit_test "--enable-x86-feature ibt --disable-x86-feature shstk" \
200 x86-feature-1 x86-feature-1b
201 elfedit_test "--enable-x86-feature ibt" x86-feature-1 x86-feature-1b
202 elfedit_test "--disable-x86-feature shstk" x86-feature-1 x86-feature-1c
203 elfedit_test "--disable-x86-feature ibt" x86-feature-1 x86-feature-1d
204 elfedit_test "--enable-x86-feature ibt --enable-x86-feature shstk" \
205 x86-feature-1 x86-feature-1e
207 proc check_pr25749a {testname srcfilea srcfileb cflags ldflags lderror} {
212 if { [istarget "i?86-*-linux*"] } {
213 set output_arch "i386:i386"
214 set output_target "elf32-i386"
216 set output_arch "i386:x86-64"
217 if {[istarget "x86_64-*-linux*-gnux32"]} {
218 set output_target "elf32-x86-64"
220 set output_target "elf64-x86-64"
224 # Suppress warning for unsupported attribute from older GCC.
227 exec cp $srcdir/$subdir/$srcfilea $srcfilea
228 exec chmod +w $srcfilea
229 set pr25749_bin "$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
230 send_log "$pr25749_bin\n"
231 set got [remote_exec host "$pr25749_bin"]
232 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
234 fail "Convert $srcfilea to $output_target"
238 if {"$lderror" == ""} {
239 run_cc_link_tests [list \
241 "Build $testname ($ldflags $cflags)" \
242 "$ldflags tmpdir/pr25749-bin.o" \
244 [list $srcfilea $srcfileb]\
245 {{readelf {-Wr} pr25749.rd}} \
249 run_ld_link_exec_tests [list \
251 "Run ${testname}a ($ldflags $cflags)" \
252 "$ldflags tmpdir/pr25749-bin.o" \
254 [list $srcfilea $srcfileb]\
261 run_cc_link_tests [list \
263 "Build $testname ($ldflags $cflags)" \
264 "$ldflags tmpdir/pr25749-bin.o" \
266 [list $srcfilea $srcfileb]\
267 [list [list error_output $lderror]] \
274 check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
275 check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
276 check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
277 if { [istarget "i?86-*-linux*"] || ![at_least_gcc_version 5 1] } {
278 check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" ""
280 check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "pr25749-1b.err"
282 check_pr25749a "pr25749-1c" "pr25749-1.c" "pr25749-1c.c" "-fPIC" "-shared" "pr25749-1b.err"
283 check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
284 check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" ""
285 check_pr25749a "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
286 check_pr25749a "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""
287 check_pr25749a "pr25754-1a" "pr25754-1a.c" "pr25754-1b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
288 check_pr25749a "pr25754-1b" "pr25754-1a.c" "pr25754-1b.s" "-fPIE" "$NOPIE_LDFLAGS" ""
289 check_pr25749a "pr25754-1c" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
290 check_pr25749a "pr25754-1d" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "-pie" ""
291 if { [istarget "i?86-*-linux*"] || [istarget "x86_64-*-linux*-gnux32"]} {
292 check_pr25749a "pr25754-2a" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
293 check_pr25749a "pr25754-2b" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "-pie" ""
295 check_pr25749a "pr25754-3a" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
296 check_pr25749a "pr25754-3b" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "-pie" ""
298 if { [istarget "i?86-*-linux*"] } {
299 check_pr25749a "pr25754-4a" "pr25754-4a.c" "pr25754-4b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
300 check_pr25749a "pr25754-4b" "pr25754-4a.c" "pr25754-4b.s" "-fpie" "-pie" ""
301 check_pr25749a "pr25754-5a" "pr25754-5a.c" "pr25754-5b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
302 check_pr25749a "pr25754-5b" "pr25754-5a.c" "pr25754-5b.s" "-fpie" "-pie" ""
304 check_pr25749a "pr25754-4a" "pr25754-4a.c" "pr25754-4c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
305 check_pr25749a "pr25754-4b" "pr25754-4a.c" "pr25754-4c.s" "-fpie" "-pie" ""
306 check_pr25749a "pr25754-5a" "pr25754-5a.c" "pr25754-5c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
307 check_pr25749a "pr25754-5b" "pr25754-5a.c" "pr25754-5c.s" "-fpie" "-pie" ""
308 if { ![istarget "x86_64-*-linux*-gnux32"]} {
309 check_pr25749a "pr25754-6a" "pr25754-6a.c" "pr25754-6b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
310 check_pr25749a "pr25754-6b" "pr25754-6a.c" "pr25754-6b.s" "-fpie" "-pie" ""
314 proc check_pr25749b {testname srcfilea srcfileb cflags ldflags dsoldflags args} {
319 if { [istarget "i?86-*-linux*"] } {
320 set output_arch "i386:i386"
321 set output_target "elf32-i386"
323 set output_arch "i386:x86-64"
324 if {[istarget "x86_64-*-linux*-gnux32"]} {
325 set output_target "elf32-x86-64"
327 set output_target "elf64-x86-64"
331 exec cp $srcdir/$subdir/$srcfilea $srcfilea
332 exec chmod +w $srcfilea
333 set pr25749_bin "$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
334 send_log "$pr25749_bin\n"
335 set got [remote_exec host "$pr25749_bin"]
336 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
338 fail "Convert $srcfilea to $output_target"
342 run_cc_link_tests [list \
344 "Build lib${testname}.so ($dsoldflags)" \
345 "-shared $dsoldflags tmpdir/pr25749-bin.o" \
348 {{readelf {-Wr} pr25749.rd}} \
349 "lib${testname}.so" \
354 if { [llength $args] > 0 } {
355 set pass [lindex $args 0]
358 run_ld_link_exec_tests [list \
360 "Run ${testname}b ($ldflags $cflags)" \
361 "$ldflags -Wl,--no-as-needed tmpdir/lib${testname}.so" \
371 check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
372 check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
373 check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
374 check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
375 check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "" "passall.out"
376 check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
377 check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "-Wl,-defsym=_begin=0"
378 check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-defsym=_begin=0"
379 check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-Bsymbolic -Wl,-defsym=_begin=0"
380 check_pr25749b "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
381 check_pr25749b "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" "" "passall.out"
382 check_pr25749b "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
383 check_pr25749b "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""