]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/testsuite/ld-elf/elf.exp
For test for PR 29072 when the linker is configured with --enable-default-execstack=no.
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-elf / elf.exp
1 # Expect script for various ELF tests.
2 # Copyright (C) 2002-2023 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 # Exclude non-ELF targets.
23
24 if ![is_elf_format] {
25 return
26 }
27
28 # Return true if target is riscv little endian.
29 # xfail the riscv little endain targets for the compressed1d1 test;
30 # The riscv big endian targets and others should pass.
31 proc riscv_little_endian { } {
32 if { [istarget "riscv32-*-*"]
33 || [istarget "riscv64-*-*"]
34 || [istarget "riscv32le-*-*"]
35 || [istarget "riscv64le-*-*"] } {
36 return 1
37 }
38 return 0
39 }
40
41 set old_ldflags $LDFLAGS
42 if { [istarget spu*-*-*] } {
43 set LDFLAGS "$LDFLAGS --local-store 0:0"
44 }
45
46 # hpux .comm differs from everyone else
47 set hpux ""
48 set old_asflags $ASFLAGS
49 if [istarget "*-*-hpux*"] {
50 set hpux "--defsym HPUX=1"
51 set ASFLAGS "$ASFLAGS --defsym HPUX=1"
52 }
53
54 if { [istarget alpha*-*-* ] } {
55 # The compress1 test is written expecting 32-bit addresses; force the
56 # executable down into the low address space to match.
57 # ??? How can we adjust just the one testcase?
58 set LDFLAGS "$LDFLAGS -Ttext-segment 0x1000000"
59 set ASFLAGS "$ASFLAGS --defsym NO_SET=1"
60 }
61
62 if [istarget "tic6x-*-*"] {
63 append ASFLAGS " -mpic -mpid=near"
64 }
65
66 if { [istarget "*-*-solaris*"] } {
67 # Same for Solaris
68 set options_regsub(ld) {-melf_x86_64 -melf_x86_64_sol2}
69 }
70
71 if { [is_remote host] } then {
72 remote_download host merge.ld
73 }
74
75 # Note - the output file from the second test (symbol3w.a) is
76 # used in the proc is_elf64 test below...
77 run_ld_link_tests [list \
78 [list "Build symbol3.a" \
79 "" "" $hpux \
80 {symbol3.s} {} "symbol3.a" ] \
81 [list "Build symbol3w.a" \
82 "" "" "" \
83 {symbol3w.s} {} "symbol3w.a" ] \
84 ]
85
86
87 if [is_elf64 tmpdir/symbol3w.a] {
88 set ASFLAGS "$ASFLAGS --defsym ALIGN=3"
89 set pr23900_1_exp "pr23900-1-64.rd"
90 set pr25490_2_exp "pr25490-2-64.rd"
91 set pr25490_3_exp "pr25490-3-64.rd"
92 set pr25490_4_exp "pr25490-4-64.rd"
93 set pr25490_5_exp "pr25490-5-64.rd"
94 set pr25490_6_exp "pr25490-6-64.rd"
95 } else {
96 set ASFLAGS "$ASFLAGS --defsym ALIGN=2"
97 set pr23900_1_exp "pr23900-1-32.rd"
98 if { [istarget avr-*-*]
99 || [istarget h8300-*-*]
100 || [istarget ip2k-*-*]
101 || [istarget m68hc11-*]
102 || [istarget "z80-*-*"] } {
103 set pr25490_2_exp "pr25490-2-16.rd"
104 set pr25490_3_exp "pr25490-3-16.rd"
105 set pr25490_4_exp "pr25490-4-16.rd"
106 set pr25490_5_exp "pr25490-5-16.rd"
107 set pr25490_6_exp "pr25490-6-16.rd"
108 } else {
109 set pr25490_2_exp "pr25490-2-32.rd"
110 set pr25490_3_exp "pr25490-3-32.rd"
111 set pr25490_4_exp "pr25490-4-32.rd"
112 set pr25490_5_exp "pr25490-5-32.rd"
113 set pr25490_6_exp "pr25490-6-32.rd"
114 }
115 }
116
117
118
119 # Targets that use _bfd_generic_link_add_symbols won't pass pr21703 tests
120 run_ld_link_tests {
121 {"PR ld/21703"
122 "--allow-multiple-definition tmpdir/pr21703-1.o tmpdir/pr21703-2.o" "" "" \
123 {pr21703-1.s pr21703-2.s} {{readelf {-s} pr21703.sd}} "pr21703" }
124 {"PR ld/21703 -r"
125 "-r --allow-multiple-definition tmpdir/pr21703-3.o tmpdir/pr21703-4.o" "" "" \
126 {pr21703-3.s pr21703-4.s} {{readelf {-s} pr21703-r.sd}} "pr21703.o" }
127 } \[is_generic\]
128
129 if [is_underscore_target] {
130 set ASFLAGS "$ASFLAGS --defsym UNDERSCORE=1"
131 }
132
133 set saved_ASFLAGS "$ASFLAGS"
134 if { [istarget "i?86-*-*"] || [istarget "x86_64-*-*"] } {
135 set ASFLAGS "$ASFLAGS -mx86-used-note=no"
136 }
137
138 # Build libraries required for SHF_GNU_RETAIN tests.
139 if { [check_gc_sections_available] && [supports_gnu_osabi] } {
140 run_ld_link_tests [list \
141 [list "Build libretain5.a" "" "" "" \
142 {retain5lib.s} {} "libretain5.a"] \
143 [list "Build libretain6.a" "" "" "" \
144 {retain6lib.s} {} "libretain6.a"] \
145 ]
146 }
147
148 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
149 foreach t $test_list {
150 # We need to strip the ".d", but can leave the dirname.
151 verbose [file rootname $t]
152 run_dump_test [file rootname $t]
153 }
154
155 set ASFLAGS "$saved_ASFLAGS"
156
157 # Check that the --out-implib option work correctly.
158 # Targets that don't use elf.em won't support this.
159 run_ld_link_tests [list \
160 [list "Generate empty import library" \
161 "--out-implib=tmpdir/implib.lib" "" \
162 [concat "--defsym NO_GLOBAL=1" $hpux] \
163 {implib.s} \
164 {{ld empty-implib.out}} \
165 "implib" ] \
166 [list "Generate import library" \
167 "--out-implib=tmpdir/implib.lib" "" \
168 $hpux \
169 {implib.s} \
170 {{readelf {-s tmpdir/implib.lib} implib.rd}} \
171 "implib" ] \
172 ] \[uses_genelf\]
173
174 #v850 gas complains about .tbss.var section attributes.
175 if { [check_gc_sections_available] && ![istarget "v850-*-*"] } {
176 run_ld_link_tests {
177 {"--gc-sections on tls variable"
178 "--gc-section" "" "" {tls_gc.s} {} "tls_gc"}
179 }
180 }
181
182 proc target_defaults_to_execstack {} {
183 global base_dir
184
185 # If the linker has been configured with --enable-default-execstack=no then
186 # this proc should always return 0.
187 if { [file exists $base_dir/config.status] } {
188 set status [remote_exec host grep "enable-default-execstack=no" $base_dir/config.status]
189 if { [lindex $status 0] == 0 } {
190 return 0
191 } else {
192 verbose -log "$base_dir/config.status does not contain enable-default-execstack=no"
193 }
194 } else {
195 verbose -log "there is no file $base_dir/config.status"
196 }
197
198 if { [istarget "aarch64*-*-*"]
199 || [istarget "arc*-*-*"]
200 || [istarget "cris*-*-*"]
201 || [istarget "ia64*-*-*"]
202 || [istarget "loongarch*-*-*"]
203 || [istarget "nios2*-*-*"]
204 || [istarget "powerpc64*-*-*"]
205 || [istarget "pru*-*-*"]
206 || [istarget "riscv*-*-*"]
207 || [istarget "tilegx*-*-*"]
208 || [istarget "tilepro*-*-*"] } {
209 return 0
210 }
211 return 1
212 }
213
214 if { [istarget *-*-*linux*]
215 || [istarget *-*-nacl*]
216 || [istarget *-*-gnu*] } {
217 run_ld_link_tests [list \
218 [list "stack exec" \
219 "-z execstack" \
220 "" \
221 "" \
222 {stack.s} \
223 {{readelf {-Wl} stack-exec.rd}} \
224 "stack-exec.exe"] \
225 [list "stack noexec" \
226 "-z noexecstack" \
227 "" \
228 "" \
229 {stack.s} \
230 {{readelf {-Wl} stack-noexec.rd}} \
231 "stack-noexec.exe"] \
232 [list "stack size" \
233 "-z stack-size=0x123400 -z noexecstack" \
234 "" \
235 "" \
236 {stack.s} \
237 {{readelf {-Wl} stack-size.rd}} \
238 "stack-size.exe"] \
239 [list "PT_GNU_PROPERTY alignment" \
240 "" \
241 "" \
242 "" \
243 {pr23900-1.s} \
244 [list [list "readelf" {-Wl} $pr23900_1_exp]] \
245 "pr23900-1.exe"] \
246 ]
247
248 # Test the linker's generation of execstack and executable segment warnings.
249 # Since these are normally pruned from the linker's output we temporarily
250 # disable tha action here.
251 rename prune_warnings_extra old_prune_warnings_extra
252 proc prune_warnings_extra { text } {
253 return $text
254 }
255
256 set curr_ldflags $LDFLAGS
257 if { [istarget powerpc*-*-*] && ![istarget powerpc64*-*-*] } {
258 # Don't generate an executable .plt section
259 set LDFLAGS "$LDFLAGS --secure-plt"
260 }
261
262 # Since the warnings can be disabled by configure, ensure consistency
263 # of the first test by forcing the flags.
264 run_ld_link_tests [list \
265 [list "PR ld/29072 (warn about an executable .note.GNU-stack)" \
266 "-e 0 --warn-execstack --warn-rwx-segments" \
267 "" \
268 "" \
269 {pr29072-a.s} \
270 {{ld pr29072.a.warn}} \
271 "pr29072-a.exe"] \
272 [list "PR 29072 (warn about -z execstack)" \
273 "-z execstack --warn-execstack" \
274 "" \
275 "" \
276 {stack.s} \
277 {{ld pr29072.c.warn}} \
278 "pr29072-c.exe"] \
279 [list "PR ld/29072 (suppress warnings about executable stack)" \
280 "-e 0 --no-warn-execstack" \
281 "" \
282 "" \
283 {pr29072-a.s} \
284 {} \
285 "pr29072-d.exe"] \
286 [list "Ensure that a warning issued when creating a segment with RWX permissions" \
287 "-e 0 -Tnobits-1.t --warn-rwx-segments" \
288 "" \
289 "" \
290 {nobits-1.s} \
291 {{ld rwx-segments-1.l}} \
292 "rwx-segments-1.exe"] \
293 [list "Ensure that a warning issued when creating a TLS segment with execute permission" \
294 "-e 0 -T rwx-segments-2.t --warn-rwx-segments" \
295 "" \
296 "" \
297 {size-2.s} \
298 {{ld rwx-segments-2.l}} \
299 "rwx-segments-2.exe"] \
300 [list "Ensure that the RWX warning can be suppressed" \
301 "-e 0 -Tnobits-1.t --no-warn-rwx-segments" \
302 "" \
303 "" \
304 {nobits-1.s} \
305 {} \
306 "rwx-segments-3.exe"] \
307 ]
308
309 set LDFLAGS $curr_ldflags
310
311 if { [target_defaults_to_execstack] } {
312 run_ld_link_tests [list \
313 [list "PR ld/29072 (warn about absent .note.GNU-stack)" \
314 "-e 0 -z stack-size=0x123400 --warn-execstack" \
315 "" \
316 "" \
317 {pr29072-b.s} \
318 {{ld pr29072.b.warn}} \
319 "pr29072-b.exe"] \
320 ]
321 } else {
322 run_ld_link_tests [list \
323 [list "PR ld/29072 (ignore absent .note.GNU-stack)" \
324 "-e 0 -z stack-size=0x123400" \
325 "" \
326 "" \
327 {pr29072-b.s} \
328 {} \
329 "pr29072-b.exe"] \
330 ]
331 }
332
333 # Restore the normal pruning behaviour.
334 rename prune_warnings_extra ""
335 rename old_prune_warnings_extra prune_warnings_extra
336 }
337
338 if [check_gc_sections_available] {
339 run_ld_link_tests [list \
340 [list "__patchable_function_entries section 2" \
341 "--gc-sections -e _start" \
342 "" \
343 "" \
344 {pr25490-2.s} \
345 [list [list "readelf" {-SW} $pr25490_2_exp]] \
346 "pr25490-2.exe"] \
347 [list "__patchable_function_entries section 3" \
348 "--gc-sections -e _start" \
349 "" \
350 "" \
351 {pr25490-3.s} \
352 [list [list "readelf" {-SW} $pr25490_3_exp]] \
353 "pr25490-3.exe"] \
354 [list "__patchable_function_entries section 4" \
355 "--gc-sections -e _start" \
356 "" \
357 "" \
358 {pr25490-4.s} \
359 [list [list "readelf" {-SW} $pr25490_4_exp]] \
360 "pr25490-4.exe"] \
361 [list "__patchable_function_entries section 5" \
362 "--gc-sections -e _start" \
363 "" \
364 "" \
365 {pr25490-5.s} \
366 [list [list "readelf" {-SW} $pr25490_5_exp]] \
367 "pr25490-5.exe"] \
368 [list "__patchable_function_entries section 6" \
369 "--gc-sections -e _start" \
370 "" \
371 "" \
372 {pr25490-6.s} \
373 [list [list "readelf" {-SW} $pr25490_6_exp]] \
374 "pr25490-6.exe"] \
375 ]
376 }
377
378 set LDFLAGS $old_ldflags
379 set ASFLAGS $old_asflags
380
381 # Check to see if the C compiler works
382 if { ![check_compiler_available] } {
383 return
384 }
385
386 if [check_gc_sections_available] {
387 run_cc_link_tests {
388 {"PR ld/13195" "-Wl,--gc-sections" ""
389 {pr13195.c} {} "pr13195"}
390 }
391 }
392
393 set array_tests {
394 {"preinit array" "" ""
395 {preinit.c} "preinit" "preinit.out"}
396 {"init array" "" ""
397 {init.c} "init" "init.out"}
398 {"fini array" "" ""
399 {fini.c} "fini" "fini.out"}
400 {"init array mixed" "" ""
401 {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
402 }
403 set array_tests_pie {
404 {"PIE preinit array" "-pie" ""
405 {preinit.c} "preinit" "preinit.out" "-fPIE"}
406 {"PIE init array" "-pie" ""
407 {init.c} "init" "init.out" "-fPIE"}
408 {"PIE fini array" "-pie" ""
409 {fini.c} "fini" "fini.out" "-fPIE"}
410 {"PIE init array mixed" "-pie" ""
411 {init-mixed.c} "init-mixed" "init-mixed.out" "-I. -fPIE"}
412 {"PIE PR ld/14525" "-pie" ""
413 {pr14525.c} "pr14525" "pr14525.out" "-fPIE"}
414 }
415 set array_tests_static {
416 {"static preinit array" "-static" ""
417 {preinit.c} "preinit" "preinit.out"}
418 {"static init array" "-static" ""
419 {init.c} "init" "init.out"}
420 {"static fini array" "-static" ""
421 {fini.c} "fini" "fini.out"}
422 {"static init array mixed" "-static" ""
423 {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
424 }
425
426 # NetBSD ELF systems do not currently support the .*_array sections.
427 set xfails "*-*-netbsd*"
428 run_ld_link_exec_tests $array_tests $xfails
429
430 if { [istarget *-*-linux*]
431 || [istarget *-*-nacl*]
432 || [istarget *-*-gnu*] } {
433 run_ld_link_exec_tests $array_tests_pie $xfails
434
435 if { $STATIC_PIE_LDFLAGS != "" } then {
436 run_ld_link_exec_tests [list \
437 [list \
438 "Static PIE preinit array" \
439 "$STATIC_PIE_LDFLAGS" \
440 "" \
441 {preinit.c} \
442 "preinit-static-pie" \
443 "preinit.out" \
444 "-fPIE" \
445 ] \
446 [list \
447 "Static PIE init array" \
448 "$STATIC_PIE_LDFLAGS" \
449 "" \
450 {init.c} \
451 "init-static-pie" \
452 "init.out" \
453 "-fPIE" \
454 ] \
455 [list \
456 "Static PIE fini array" \
457 "$STATIC_PIE_LDFLAGS" \
458 "" \
459 {fini.c} \
460 "fini-static-pie" \
461 "fini.out" \
462 "-fPIE" \
463 ] \
464 [list \
465 "Static PIE init array mixed" \
466 "$STATIC_PIE_LDFLAGS" \
467 "" \
468 {init-mixed.c} \
469 "init-mixed-static-pie" \
470 "init-mixed.out" \
471 "-I. -fPIE" \
472 ] \
473 [list \
474 "Static PIE PR ld/14525" \
475 "$STATIC_PIE_LDFLAGS" \
476 "" \
477 {pr14525.c} \
478 "pr14525-static-pie" \
479 "pr14525.out" \
480 "-fPIE" \
481 ] \
482 ]
483 }
484
485 run_ld_link_exec_tests [list \
486 [list \
487 "Run mbind2a" \
488 "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000 -Wl,-z,noexecstack" \
489 "" \
490 { mbind2a.s mbind2b.c } \
491 "mbind2a" \
492 "pass.out" \
493 "-O2 -I../bfd" \
494 ] \
495 [list \
496 "Run mbind2b" \
497 "-static -Wl,-z,common-page-size=0x4000 -Wl,-z,noexecstack" \
498 "" \
499 { mbind2a.s mbind2b.c } \
500 "mbind2b" \
501 "pass.out" \
502 "-O2 -I../bfd" \
503 ] \
504 ]
505 }
506
507 run_ld_link_exec_tests $array_tests_static $xfails
508
509 run_cc_link_tests [list \
510 [list \
511 "Build pr26391-1" \
512 "-Wl,-z,unique-symbol" \
513 "-fno-function-sections" \
514 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
515 {{nm "" pr26391.nd}} \
516 "pr26391-1" \
517 ] \
518 [list \
519 "Build pr26391-2" \
520 "-Wl,-z,unique-symbol" \
521 "-ffunction-sections" \
522 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
523 {{nm "" pr26391.nd}} \
524 "pr26391-2" \
525 ] \
526 [list \
527 "Build pr26391-3" \
528 "-Wl,-z,unique-symbol,--emit-relocs" \
529 "-fno-function-sections" \
530 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
531 {{nm "" pr26391.nd}} \
532 "pr26391-3" \
533 ] \
534 [list \
535 "Build pr26391-4" \
536 "-Wl,-z,unique-symbol,--emit-relocs" \
537 "-ffunction-sections" \
538 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
539 {{nm "" pr26391.nd}} \
540 "pr26391-4" \
541 ] \
542 [list \
543 "Build pr26391-5.o" \
544 "-Wl,-z,unique-symbol -r -nostdlib -nostartfiles" \
545 "-fno-function-sections" \
546 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
547 [list \
548 [list "nm" "$dep_plug_opt" "pr26391.nd"] \
549 [list "nm" "$dep_plug_opt" "pr26391.fd"] \
550 ] \
551 "pr26391-5.o" \
552 ] \
553 [list \
554 "Build pr26391-6.o" \
555 "-Wl,-z,unique-symbol -r -nostdlib -nostartfiles" \
556 "-ffunction-sections" \
557 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
558 {{nm "" pr26391.nd}} \
559 "pr26391-6.o" \
560 ] \
561 ]
562
563 run_ld_link_exec_tests [list \
564 [list \
565 "Run pr26391-1" \
566 "-Wl,-z,unique-symbol" \
567 "" \
568 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
569 "pr26391-1" \
570 "pr26391.out" \
571 "-fno-function-sections" \
572 ] \
573 [list \
574 "Run pr26391-2" \
575 "-Wl,-z,unique-symbol" \
576 "" \
577 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
578 "pr26391-2" \
579 "pr26391.out" \
580 "-ffunction-sections" \
581 ] \
582 [list \
583 "Run pr26391-3" \
584 "-Wl,-z,unique-symbol,--emit-relocs" \
585 "" \
586 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
587 "pr26391-3" \
588 "pr26391.out" \
589 "-fno-function-sections" \
590 ] \
591 [list \
592 "Run pr26391-4" \
593 "-Wl,-z,unique-symbol,--emit-relocs" \
594 "" \
595 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
596 "pr26391-4" \
597 "pr26391.out" \
598 "-ffunction-sections" \
599 ] \
600 [list \
601 "Run pr26391-5" \
602 "-Wl,-z,unique-symbol" \
603 "" \
604 {dummy.c} \
605 "pr26391-5" \
606 "pr26391.out" \
607 "" \
608 "c" \
609 "" \
610 "tmpdir/pr26391-5.o" \
611 ] \
612 [list \
613 "Run pr26391-6" \
614 "-Wl,-z,unique-symbol" \
615 "" \
616 {dummy.c} \
617 "pr26391-6" \
618 "pr26391.out" \
619 "" \
620 "c" \
621 "" \
622 "tmpdir/pr26391-6.o" \
623 ] \
624 ]
625
626 set merge_exec_tests {
627 {"merge4" "" ""
628 {merge4a.c merge4b.s} "merge4" "merge4.out"}
629 }
630 run_ld_link_exec_tests $merge_exec_tests
631
632 catch "exec rm -f tmpdir/preinit tmpdir/init tmpdir/fini tmpdir/init-mixed" status