elf: Treat undefined version as hidden
[binutils-gdb.git] / ld / testsuite / ld-elf / linux-x86.exp
1 # Expect script for simple native Linux/x86 tests.
2 # Copyright (C) 2018-2021 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.
23 if { ![istarget "i?86-*-linux*"] \
24 && ![istarget "x86_64-*-linux*"] \
25 && ![istarget "amd64-*-linux*"] } {
26 return
27 }
28
29 run_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.
51 run_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
64 run_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
76 run_cc_link_tests [list \
77 [list \
78 "Build indirect-extern-access-1.so" \
79 "-shared" \
80 "-fPIC" \
81 { indirect-extern-access-1a.c } \
82 {} \
83 "indirect-extern-access-1.so" \
84 ] \
85 [list \
86 "Build indirect-extern-access-1a without PIE" \
87 "$NOPIE_LDFLAGS -Wl,--no-as-needed \
88 tmpdir/indirect-extern-access-1.so" \
89 "$NOPIE_CFLAGS" \
90 { indirect-extern-access.S indirect-extern-access-1b.c } \
91 {{readelf -rn indirect-extern-access-1.rd}} \
92 "indirect-extern-access-1a" \
93 ] \
94 [list \
95 "Build indirect-extern-access-2.so" \
96 "-shared" \
97 "-fPIC" \
98 { indirect-extern-access-2a.c } \
99 {} \
100 "indirect-extern-access-2.so" \
101 ] \
102 [list \
103 "Build indirect-extern-access-2a without PIE" \
104 "$NOPIE_LDFLAGS -Wl,--no-as-needed \
105 tmpdir/indirect-extern-access-2.so" \
106 "$NOPIE_CFLAGS" \
107 { indirect-extern-access.S indirect-extern-access-1b.c } \
108 {{readelf -rn indirect-extern-access-2.rd}} \
109 "indirect-extern-access-2a" \
110 ] \
111 [list \
112 "Build indirect-extern-access-2b with PIE" \
113 "-pie -Wl,--no-as-needed \
114 tmpdir/indirect-extern-access-2.so" \
115 "-fpie" \
116 { indirect-extern-access.S indirect-extern-access-2b.c } \
117 {{readelf -rn indirect-extern-access-3.rd}} \
118 "indirect-extern-access-2b" \
119 ] \
120 [list \
121 "Build pr28158.so" \
122 "-shared -Wl,-version-script,pr27128.t" \
123 "-fPIC" \
124 { pr28158-1.c } \
125 {} \
126 "pr28158.so" \
127 ] \
128 [list \
129 "Build pr28158" \
130 "$NOPIE_LDFLAGS -Wl,--no-as-needed \
131 tmpdir/pr28158.so" \
132 "" \
133 { pr28158-2.S } \
134 {{readelf {--dyn-syms -W} pr28158.rd} {nm -D pr28158.nd}} \
135 "pr28158" \
136 ] \
137 ]
138
139 run_ld_link_exec_tests [list \
140 [list \
141 "Run indirect-extern-access-1a without PIE" \
142 "$NOPIE_LDFLAGS" \
143 "" \
144 { indirect-extern-access.S indirect-extern-access-1b.c } \
145 "indirect-extern-access-1a" \
146 "pass.out" \
147 "$NOPIE_CFLAGS" \
148 "" \
149 "" \
150 "tmpdir/indirect-extern-access-1.so" \
151 ] \
152 [list \
153 "Run indirect-extern-access-1b with PIE" \
154 "-pie" \
155 "" \
156 { indirect-extern-access.S indirect-extern-access-1b.c } \
157 "indirect-extern-access-1b" \
158 "pass.out" \
159 "-fpie" \
160 "" \
161 "" \
162 "tmpdir/indirect-extern-access-1.so" \
163 ] \
164 [list \
165 "Run indirect-extern-access-2a without PIE" \
166 "$NOPIE_LDFLAGS" \
167 "" \
168 { indirect-extern-access.S indirect-extern-access-2b.c } \
169 "indirect-extern-access-2a" \
170 "pass.out" \
171 "$NOPIE_CFLAGS" \
172 "" \
173 "" \
174 "tmpdir/indirect-extern-access-2.so" \
175 ] \
176 [list \
177 "Run indirect-extern-access-2b with PIE" \
178 "-pie" \
179 "" \
180 { indirect-extern-access.S indirect-extern-access-2b.c } \
181 "indirect-extern-access-2b" \
182 "pass.out" \
183 "-fpie" \
184 "" \
185 "" \
186 "tmpdir/indirect-extern-access-2.so" \
187 ] \
188 ]
189
190 proc elfedit_test { options test output } {
191 global ELFEDIT
192 global READELF
193 global srcdir
194 global subdir
195
196 set test_name "elfedit $options"
197 send_log "$ELFEDIT $options tmpdir/$test\n"
198 set got [remote_exec host "$ELFEDIT $options tmpdir/$test" "" "/dev/null"]
199 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
200 send_log "$got\n"
201 fail "$test_name"
202 }
203 send_log "$READELF -n $options tmpdir/$test > tmpdir/$output.out\n"
204 set got [remote_exec host "$READELF -n tmpdir/$test" "" "/dev/null" "tmpdir/$output.out"]
205 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
206 send_log "$got\n"
207 fail "$test_name"
208 }
209 if { [regexp_diff tmpdir/$output.out $srcdir/$subdir/$output.rd] } then {
210 fail "$test_name"
211 } else {
212 pass "$test_name"
213 }
214 }
215
216 elfedit_test "--enable-x86-feature ibt --disable-x86-feature shstk" \
217 x86-feature-1 x86-feature-1b
218 elfedit_test "--enable-x86-feature ibt" x86-feature-1 x86-feature-1b
219 elfedit_test "--disable-x86-feature shstk" x86-feature-1 x86-feature-1c
220 elfedit_test "--disable-x86-feature ibt" x86-feature-1 x86-feature-1d
221 elfedit_test "--enable-x86-feature ibt --enable-x86-feature shstk" \
222 x86-feature-1 x86-feature-1e
223
224 proc check_pr25749a {testname srcfilea srcfileb cflags ldflags lderror} {
225 global objcopy
226 global srcdir
227 global subdir
228
229 if { [istarget "i?86-*-linux*"] } {
230 set output_arch "i386:i386"
231 set output_target "elf32-i386"
232 } else {
233 set output_arch "i386:x86-64"
234 if {[istarget "x86_64-*-linux*-gnux32"]} {
235 set output_target "elf32-x86-64"
236 } else {
237 set output_target "elf64-x86-64"
238 }
239 }
240
241 # Suppress warning for unsupported attribute from older GCC.
242 append cflags " -w"
243
244 exec cp $srcdir/$subdir/$srcfilea $srcfilea
245 exec chmod +w $srcfilea
246 set pr25749_bin "$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
247 send_log "$pr25749_bin\n"
248 set got [remote_exec host "$pr25749_bin"]
249 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
250 send_log "$got\n"
251 fail "Convert $srcfilea to $output_target"
252 return
253 }
254
255 if {"$lderror" == ""} {
256 run_cc_link_tests [list \
257 [list \
258 "Build $testname ($ldflags $cflags)" \
259 "$ldflags tmpdir/pr25749-bin.o" \
260 "$cflags -I../bfd" \
261 [list $srcfilea $srcfileb]\
262 {{readelf {-Wr} pr25749.rd}} \
263 "${testname}a" \
264 ] \
265 ]
266 run_ld_link_exec_tests [list \
267 [list \
268 "Run ${testname}a ($ldflags $cflags)" \
269 "$ldflags tmpdir/pr25749-bin.o" \
270 "" \
271 [list $srcfilea $srcfileb]\
272 "${testname}a" \
273 "pass.out" \
274 "$cflags -I../bfd" \
275 ] \
276 ]
277 } else {
278 run_cc_link_tests [list \
279 [list \
280 "Build $testname ($ldflags $cflags)" \
281 "$ldflags tmpdir/pr25749-bin.o" \
282 "$cflags -I../bfd" \
283 [list $srcfilea $srcfileb]\
284 [list [list error_output $lderror]] \
285 "$testname" \
286 ] \
287 ]
288 }
289 }
290
291 check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
292 check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
293 check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
294 if { [istarget "i?86-*-linux*"] || ![at_least_gcc_version 5 1] } {
295 check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" ""
296 } else {
297 check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "pr25749-1b.err"
298 }
299 check_pr25749a "pr25749-1c" "pr25749-1.c" "pr25749-1c.c" "-fPIC" "-shared" "pr25749-1b.err"
300 check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
301 check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" ""
302 check_pr25749a "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
303 check_pr25749a "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""
304 check_pr25749a "pr25754-1a" "pr25754-1a.c" "pr25754-1b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
305 check_pr25749a "pr25754-1b" "pr25754-1a.c" "pr25754-1b.s" "-fPIE" "$NOPIE_LDFLAGS" ""
306 check_pr25749a "pr25754-1c" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
307 check_pr25749a "pr25754-1d" "pr25754-1a.c" "pr25754-1b.s" "-fPIC" "-pie" ""
308 if { [istarget "i?86-*-linux*"] || [istarget "x86_64-*-linux*-gnux32"]} {
309 check_pr25749a "pr25754-2a" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
310 check_pr25749a "pr25754-2b" "pr25754-2a.c" "pr25754-2b.s" "-fPIC" "-pie" ""
311 } else {
312 check_pr25749a "pr25754-3a" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "$NOPIE_LDFLAGS" ""
313 check_pr25749a "pr25754-3b" "pr25754-3a.c" "pr25754-3b.s" "-fPIC" "-pie" ""
314 }
315 if { [istarget "i?86-*-linux*"] } {
316 check_pr25749a "pr25754-4a" "pr25754-4a.c" "pr25754-4b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
317 check_pr25749a "pr25754-4b" "pr25754-4a.c" "pr25754-4b.s" "-fpie" "-pie" ""
318 check_pr25749a "pr25754-5a" "pr25754-5a.c" "pr25754-5b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
319 check_pr25749a "pr25754-5b" "pr25754-5a.c" "pr25754-5b.s" "-fpie" "-pie" ""
320 } else {
321 check_pr25749a "pr25754-4a" "pr25754-4a.c" "pr25754-4c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
322 check_pr25749a "pr25754-4b" "pr25754-4a.c" "pr25754-4c.s" "-fpie" "-pie" ""
323 check_pr25749a "pr25754-5a" "pr25754-5a.c" "pr25754-5c.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
324 check_pr25749a "pr25754-5b" "pr25754-5a.c" "pr25754-5c.s" "-fpie" "-pie" ""
325 if { ![istarget "x86_64-*-linux*-gnux32"]} {
326 check_pr25749a "pr25754-6a" "pr25754-6a.c" "pr25754-6b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
327 check_pr25749a "pr25754-6b" "pr25754-6a.c" "pr25754-6b.s" "-fpie" "-pie" ""
328 }
329 }
330
331 proc check_pr25749b {testname srcfilea srcfileb cflags ldflags dsoldflags args} {
332 global objcopy
333 global srcdir
334 global subdir
335
336 if { [istarget "i?86-*-linux*"] } {
337 set output_arch "i386:i386"
338 set output_target "elf32-i386"
339 } else {
340 set output_arch "i386:x86-64"
341 if {[istarget "x86_64-*-linux*-gnux32"]} {
342 set output_target "elf32-x86-64"
343 } else {
344 set output_target "elf64-x86-64"
345 }
346 }
347
348 exec cp $srcdir/$subdir/$srcfilea $srcfilea
349 exec chmod +w $srcfilea
350 set pr25749_bin "$objcopy -B $output_arch -I binary -O $output_target $srcfilea tmpdir/pr25749-bin.o"
351 send_log "$pr25749_bin\n"
352 set got [remote_exec host "$pr25749_bin"]
353 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
354 send_log "$got\n"
355 fail "Convert $srcfilea to $output_target"
356 return
357 }
358
359 run_cc_link_tests [list \
360 [list \
361 "Build lib${testname}.so ($dsoldflags)" \
362 "-shared $dsoldflags tmpdir/pr25749-bin.o" \
363 "-fPIC -I../bfd" \
364 [list $srcfileb] \
365 {{readelf {-Wr} pr25749.rd}} \
366 "lib${testname}.so" \
367 ] \
368 ]
369
370 set pass "pass.out"
371 if { [llength $args] > 0 } {
372 set pass [lindex $args 0]
373 }
374
375 run_ld_link_exec_tests [list \
376 [list \
377 "Run ${testname}b ($ldflags $cflags)" \
378 "$ldflags -Wl,--no-as-needed tmpdir/lib${testname}.so" \
379 "" \
380 [list $srcfilea]\
381 "${testname}b" \
382 "$pass" \
383 "$cflags -I../bfd" \
384 ] \
385 ]
386 }
387
388 check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
389 check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
390 check_pr25749b "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
391 check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
392 check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "" "passall.out"
393 check_pr25749b "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "-Wl,-Bsymbolic"
394 check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "-Wl,-defsym=_begin=0"
395 check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-defsym=_begin=0"
396 check_pr25749b "pr25749-1d" "pr25749-1.c" "pr25749-1d.c" "-fPIE" "-pie" "-Wl,-Bsymbolic -Wl,-defsym=_begin=0"
397 check_pr25749b "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" "" "passall.out"
398 check_pr25749b "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "-fPIE" "-pie" "" "passall.out"
399 check_pr25749b "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
400 check_pr25749b "pr25749-2b" "pr25749-2.c" "pr25749-2b.s" "-fPIE" "-pie" ""