1# Expect script for simple native Linux/x86 tests.
2#   Copyright (C) 2018-2020 Free Software Foundation, Inc.
3#
4# This file is part of the GNU Binutils.
5#
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.
10#
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.
15#
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,
19# MA 02110-1301, USA.
20#
21
22# Linux/x86 tests.
23if { ![istarget "i?86-*-linux*"] \
24       && ![istarget "x86_64-*-linux*"] \
25       && ![istarget "amd64-*-linux*"] } {
26    return
27}
28
29run_ld_link_tests [list \
30    [list \
31	"Build pr24920.so" \
32	"-shared" \
33	"" \
34	"" \
35	{dummy.s} \
36	{} \
37	"pr24920.so" \
38    ] \
39    [list \
40	"Build pr24920" \
41	"-static " \
42	"-Bdynamic tmpdir/pr24920.so" \
43	"" \
44	{start.s} \
45	{{ld pr24920.err}} \
46	"pr24920" \
47    ] \
48]
49
50# Test very simple native Linux/x86 programs with linux-x86.S.
51run_ld_link_exec_tests [list \
52    [list \
53	"Run PR ld/23428 test" \
54	"--no-dynamic-linker -z separate-code" \
55	"" \
56	{ linux-x86.S pr23428.c dummy.s } \
57	"pr23428" \
58	"pass.out" \
59	"$NOPIE_CFLAGS $NOSANITIZE_CFLAGS -fno-asynchronous-unwind-tables" \
60	"asm" \
61    ] \
62]
63
64run_ld_link_tests [list \
65    [list \
66	"Build x86-feature-1" \
67	"-z separate-code -z shstk" \
68	"" \
69	"-mx86-used-note=yes" \
70	{ start.s } \
71	{{readelf -n x86-feature-1a.rd}} \
72	"x86-feature-1" \
73    ] \
74]
75
76proc elfedit_test { options test output } {
77    global ELFEDIT
78    global READELF
79    global srcdir
80    global subdir
81
82    set test_name "elfedit $options"
83    send_log "$ELFEDIT $options tmpdir/$test\n"
84    set got [remote_exec host "$ELFEDIT $options tmpdir/$test" "" "/dev/null"]
85    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
86	send_log "$got\n"
87	unresolved "$test_name"
88    }
89    send_log "$READELF -n $options tmpdir/$test > tmpdir/$output.out\n"
90    set got [remote_exec host "$READELF -n tmpdir/$test" "" "/dev/null" "tmpdir/$output.out"]
91    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
92	send_log "$got\n"
93	unresolved "$test_name"
94}
95    if { [regexp_diff tmpdir/$output.out $srcdir/$subdir/$output.rd] } then {
96	fail "$test_name"
97    } else {
98	pass "$test_name"
99    }
100}
101
102elfedit_test "--enable-x86-feature ibt --disable-x86-feature shstk" \
103		x86-feature-1 x86-feature-1b
104elfedit_test "--enable-x86-feature ibt" x86-feature-1 x86-feature-1b
105elfedit_test "--disable-x86-feature shstk" x86-feature-1 x86-feature-1c
106elfedit_test "--disable-x86-feature ibt" x86-feature-1 x86-feature-1d
107elfedit_test "--enable-x86-feature ibt --enable-x86-feature shstk" \
108		x86-feature-1 x86-feature-1e
109
110proc check_pr25749a {testname srcfilea srcfileb cflags ldflags lderror} {
111    global objcopy
112    global srcdir
113    global subdir
114
115    if { [istarget "i?86-*-linux*"] } {
116	set output_arch "i386:i386"
117	set output_target "elf32-i386"
118    } else {
119	set output_arch "i386:x86-64"
120	if {[istarget "x86_64-*-linux*-gnux32"]} {
121	    set output_target "elf32-x86-64"
122	} else {
123	    set output_target "elf64-x86-64"
124	}
125    }
126
127    # Suppress warning for unsupported attribute from older GCC.
128    append cflags " -w"
129
130    exec cp $srcdir/$subdir/$srcfilea $srcfilea
131    exec chmod +w $srcfilea
132    set pr25749_bin "$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
133    send_log "$pr25749_bin\n"
134    set got [remote_exec host "$pr25749_bin"]
135    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
136	send_log "$got\n"
137	fail "Convert $srcfilea to $output_target"
138	return
139    }
140
141    if {"$lderror" == ""} {
142	run_cc_link_tests [list \
143	    [list \
144		"Build $testname ($ldflags $cflags)" \
145		"$ldflags tmpdir/pr25749-bin.o" \
146		"$cflags -I../bfd" \
147		[list $srcfilea $srcfileb]\
148		{{readelf {-Wr} pr25749.rd}}  \
149		"${testname}a" \
150	    ] \
151	]
152	run_ld_link_exec_tests [list \
153	    [list \
154		"Run ${testname}a ($ldflags $cflags)" \
155		"$ldflags tmpdir/pr25749-bin.o" \
156		"" \
157		[list $srcfilea $srcfileb]\
158		"${testname}a" \
159		"pass.out" \
160		"$cflags -I../bfd" \
161	    ] \
162	]
163    } else {
164	run_cc_link_tests [list \
165	    [list \
166		"Build $testname ($ldflags $cflags)" \
167		"$ldflags tmpdir/pr25749-bin.o" \
168		"$cflags -I../bfd" \
169		[list $srcfilea $srcfileb]\
170		[list [list error_output $lderror]] \
171		"$testname" \
172	    ] \
173	]
174    }
175}
176
177check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
178check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
179check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
180if { [istarget "i?86-*-linux*"] || ![at_least_gcc_version 5 1] } {
181    check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" ""
182} else {
183    check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "pr25749-1b.err"
184}
185check_pr25749a "pr25749-1c" "pr25749-1.c" "pr25749-1c.c" "-fPIC" "-shared" "pr25749-1b.err"
186check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
187check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" ""
188check_pr25749a "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
189check_pr25749a "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""
190check_pr25749a "pr25754-1a" "pr25754-1a.c" "pr25754-1b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
191check_pr25749a "pr25754-1b" "pr25754-1a.c" "pr25754-1b.s" "-fPIE" "$NOPIE_LDFLAGS" ""
192check_pr25749a "pr25754-1c" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
193check_pr25749a "pr25754-1d" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "-pie" ""
194if { [istarget "i?86-*-linux*"] || [istarget "x86_64-*-linux*-gnux32"]} {
195    check_pr25749a "pr25754-2a" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
196    check_pr25749a "pr25754-2b" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "-pie" ""
197} else {
198    check_pr25749a "pr25754-3a" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
199    check_pr25749a "pr25754-3b" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "-pie" ""
200}
201if { [istarget "i?86-*-linux*"] } {
202    check_pr25749a "pr25754-4a" "pr25754-4a.c" "pr25754-4b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
203    check_pr25749a "pr25754-4b" "pr25754-4a.c" "pr25754-4b.s" "-fpie" "-pie" ""
204    check_pr25749a "pr25754-5a" "pr25754-5a.c" "pr25754-5b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
205    check_pr25749a "pr25754-5b" "pr25754-5a.c" "pr25754-5b.s" "-fpie" "-pie" ""
206} else {
207    check_pr25749a "pr25754-4a" "pr25754-4a.c" "pr25754-4c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
208    check_pr25749a "pr25754-4b" "pr25754-4a.c" "pr25754-4c.s" "-fpie" "-pie" ""
209    check_pr25749a "pr25754-5a" "pr25754-5a.c" "pr25754-5c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
210    check_pr25749a "pr25754-5b" "pr25754-5a.c" "pr25754-5c.s" "-fpie" "-pie" ""
211    if { ![istarget "x86_64-*-linux*-gnux32"]} {
212	check_pr25749a "pr25754-6a" "pr25754-6a.c" "pr25754-6b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
213	check_pr25749a "pr25754-6b" "pr25754-6a.c" "pr25754-6b.s" "-fpie" "-pie" ""
214    }
215}
216
217proc check_pr25749b {testname srcfilea srcfileb cflags ldflags dsoldflags args} {
218    global objcopy
219    global srcdir
220    global subdir
221
222    if { [istarget "i?86-*-linux*"] } {
223	set output_arch "i386:i386"
224	set output_target "elf32-i386"
225    } else {
226	set output_arch "i386:x86-64"
227	if {[istarget "x86_64-*-linux*-gnux32"]} {
228	    set output_target "elf32-x86-64"
229	} else {
230	    set output_target "elf64-x86-64"
231	}
232    }
233
234    exec cp $srcdir/$subdir/$srcfilea $srcfilea
235    exec chmod +w $srcfilea
236    set pr25749_bin "$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
237    send_log "$pr25749_bin\n"
238    set got [remote_exec host "$pr25749_bin"]
239    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
240	send_log "$got\n"
241	fail "Convert $srcfilea to $output_target"
242	return
243    }
244
245    run_cc_link_tests [list \
246	[list \
247	    "Build lib${testname}.so ($dsoldflags)" \
248	    "-shared $dsoldflags tmpdir/pr25749-bin.o" \
249	    "-fPIC -I../bfd" \
250	    [list $srcfileb] \
251	    {{readelf {-Wr} pr25749.rd}}  \
252	    "lib${testname}.so" \
253	] \
254    ]
255
256    set pass "pass.out"
257    if { [llength $args] > 0 } {
258	set pass [lindex $args 0]
259    }
260
261    run_ld_link_exec_tests [list \
262	[list \
263	    "Run ${testname}b ($ldflags $cflags)" \
264	    "$ldflags -Wl,--no-as-needed tmpdir/lib${testname}.so" \
265	    "" \
266	    [list $srcfilea]\
267	    "${testname}b" \
268	    "$pass" \
269	    "$cflags -I../bfd" \
270	] \
271    ]
272}
273
274check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
275check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
276check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
277check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
278check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "" "passall.out"
279check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
280check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "-Wl,-defsym=_begin=0"
281check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-defsym=_begin=0"
282check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-Bsymbolic -Wl,-defsym=_begin=0"
283check_pr25749b "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
284check_pr25749b "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" "" "passall.out"
285check_pr25749b "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
286check_pr25749b "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""
287