Add ability to change linker warning messages into errors when reporting executable...
[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 "kvx*-*-*"]
203 || [istarget "loongarch*-*-*"]
204 || [istarget "nios2*-*-*"]
205 || [istarget "powerpc64*-*-*"]
206 || [istarget "pru*-*-*"]
207 || [istarget "riscv*-*-*"]
208 || [istarget "tilegx*-*-*"]
209 || [istarget "tilepro*-*-*"] } {
210 return 0
211 }
212 return 1
213 }
214
215 if { [istarget *-*-*linux*]
216 || [istarget *-*-nacl*]
217 || [istarget *-*-gnu*] } {
218 run_ld_link_tests [list \
219 [list "stack exec" \
220 "-z execstack --no-error-execstack" \
221 "" \
222 "" \
223 {stack.s} \
224 {{readelf {-Wl} stack-exec.rd}} \
225 "stack-exec.exe"] \
226 [list "stack noexec" \
227 "-z noexecstack" \
228 "" \
229 "" \
230 {stack.s} \
231 {{readelf {-Wl} stack-noexec.rd}} \
232 "stack-noexec.exe"] \
233 [list "stack size" \
234 "-z stack-size=0x123400 -z noexecstack" \
235 "" \
236 "" \
237 {stack.s} \
238 {{readelf {-Wl} stack-size.rd}} \
239 "stack-size.exe"] \
240 [list "PT_GNU_PROPERTY alignment" \
241 "" \
242 "" \
243 "" \
244 {pr23900-1.s} \
245 [list [list "readelf" {-Wl} $pr23900_1_exp]] \
246 "pr23900-1.exe"] \
247 ]
248
249 # Test the linker's generation of execstack and executable segment warnings.
250 # Since these are normally pruned from the linker's output we temporarily
251 # disable tha action here.
252 rename prune_warnings_extra old_prune_warnings_extra
253 proc prune_warnings_extra { text } {
254 return $text
255 }
256
257 set curr_ldflags $LDFLAGS
258 if { [istarget powerpc*-*-*] && ![istarget powerpc64*-*-*] } {
259 # Don't generate an executable .plt section
260 set LDFLAGS "$LDFLAGS --secure-plt"
261 }
262
263 # Since the warnings can be disabled by configure, ensure consistency
264 # of the first test by forcing the flags.
265 run_ld_link_tests [list \
266 [list "PR ld/29072 (warn about an executable .note.GNU-stack)" \
267 "-e 0 --warn-execstack --warn-rwx-segments --no-error-rwx-segments --no-error-execstack" \
268 "" \
269 "" \
270 {pr29072-a.s} \
271 {{ld pr29072.a.warn}} \
272 "pr29072-a.exe"] \
273 [list "PR 29072 (warn about -z execstack)" \
274 "-z execstack --warn-execstack --no-error-execstack" \
275 "" \
276 "" \
277 {stack.s} \
278 {{ld pr29072.c.warn}} \
279 "pr29072-c.exe"] \
280 [list "PR ld/29072 (suppress warnings about executable stack)" \
281 "-e 0 --no-warn-execstack" \
282 "" \
283 "" \
284 {pr29072-a.s} \
285 {} \
286 "pr29072-d.exe"] \
287 [list "Ensure that a warning issued when creating a segment with RWX permissions" \
288 "-e 0 -Tnobits-1.t --warn-rwx-segments --no-error-rwx-segments" \
289 "" \
290 "" \
291 {nobits-1.s} \
292 {{ld rwx-segments-1.l}} \
293 "rwx-segments-1.exe"] \
294 [list "Ensure that a warning issued when creating a TLS segment with execute permission" \
295 "-e 0 -T rwx-segments-2.t --warn-rwx-segments --no-error-rwx-segments" \
296 "" \
297 "" \
298 {size-2.s} \
299 {{ld rwx-segments-2.l}} \
300 "rwx-segments-2.exe"] \
301 [list "Ensure that the RWX warning can be suppressed" \
302 "-e 0 -Tnobits-1.t --no-warn-rwx-segments" \
303 "" \
304 "" \
305 {nobits-1.s} \
306 {} \
307 "rwx-segments-3.exe"] \
308 ]
309
310 set LDFLAGS $curr_ldflags
311
312 if { [target_defaults_to_execstack] } {
313 run_ld_link_tests [list \
314 [list "PR ld/29072 (warn about absent .note.GNU-stack)" \
315 "-e 0 -z stack-size=0x123400 --warn-execstack --no-error-execstack" \
316 "" \
317 "" \
318 {pr29072-b.s} \
319 {{ld pr29072.b.warn}} \
320 "pr29072-b.exe"] \
321 ]
322 } else {
323 run_ld_link_tests [list \
324 [list "PR ld/29072 (ignore absent .note.GNU-stack)" \
325 "-e 0 -z stack-size=0x123400 --no-error-execstack" \
326 "" \
327 "" \
328 {pr29072-b.s} \
329 {} \
330 "pr29072-b.exe"] \
331 ]
332 }
333
334 # Restore the normal pruning behaviour.
335 rename prune_warnings_extra ""
336 rename old_prune_warnings_extra prune_warnings_extra
337 }
338
339 if [check_gc_sections_available] {
340 run_ld_link_tests [list \
341 [list "__patchable_function_entries section 2" \
342 "--gc-sections -e _start" \
343 "" \
344 "" \
345 {pr25490-2.s} \
346 [list [list "readelf" {-SW} $pr25490_2_exp]] \
347 "pr25490-2.exe"] \
348 [list "__patchable_function_entries section 3" \
349 "--gc-sections -e _start" \
350 "" \
351 "" \
352 {pr25490-3.s} \
353 [list [list "readelf" {-SW} $pr25490_3_exp]] \
354 "pr25490-3.exe"] \
355 [list "__patchable_function_entries section 4" \
356 "--gc-sections -e _start" \
357 "" \
358 "" \
359 {pr25490-4.s} \
360 [list [list "readelf" {-SW} $pr25490_4_exp]] \
361 "pr25490-4.exe"] \
362 [list "__patchable_function_entries section 5" \
363 "--gc-sections -e _start" \
364 "" \
365 "" \
366 {pr25490-5.s} \
367 [list [list "readelf" {-SW} $pr25490_5_exp]] \
368 "pr25490-5.exe"] \
369 [list "__patchable_function_entries section 6" \
370 "--gc-sections -e _start" \
371 "" \
372 "" \
373 {pr25490-6.s} \
374 [list [list "readelf" {-SW} $pr25490_6_exp]] \
375 "pr25490-6.exe"] \
376 ]
377 }
378
379 set LDFLAGS $old_ldflags
380 set ASFLAGS $old_asflags
381
382 # Check to see if the C compiler works
383 if { ![check_compiler_available] } {
384 return
385 }
386
387 if [check_gc_sections_available] {
388 run_cc_link_tests {
389 {"PR ld/13195" "-Wl,--gc-sections" ""
390 {pr13195.c} {} "pr13195"}
391 }
392 }
393
394 set array_tests {
395 {"preinit array" "" ""
396 {preinit.c} "preinit" "preinit.out"}
397 {"init array" "" ""
398 {init.c} "init" "init.out"}
399 {"fini array" "" ""
400 {fini.c} "fini" "fini.out"}
401 {"init array mixed" "" ""
402 {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
403 }
404 set array_tests_pie {
405 {"PIE preinit array" "-pie" ""
406 {preinit.c} "preinit" "preinit.out" "-fPIE"}
407 {"PIE init array" "-pie" ""
408 {init.c} "init" "init.out" "-fPIE"}
409 {"PIE fini array" "-pie" ""
410 {fini.c} "fini" "fini.out" "-fPIE"}
411 {"PIE init array mixed" "-pie" ""
412 {init-mixed.c} "init-mixed" "init-mixed.out" "-I. -fPIE"}
413 {"PIE PR ld/14525" "-pie" ""
414 {pr14525.c} "pr14525" "pr14525.out" "-fPIE"}
415 }
416 set array_tests_static {
417 {"static preinit array" "-static" ""
418 {preinit.c} "preinit" "preinit.out"}
419 {"static init array" "-static" ""
420 {init.c} "init" "init.out"}
421 {"static fini array" "-static" ""
422 {fini.c} "fini" "fini.out"}
423 {"static init array mixed" "-static" ""
424 {init-mixed.c} "init-mixed" "init-mixed.out" "-I."}
425 }
426
427 # NetBSD ELF systems do not currently support the .*_array sections.
428 set xfails "*-*-netbsd*"
429 run_ld_link_exec_tests $array_tests $xfails
430
431 if { [istarget *-*-linux*]
432 || [istarget *-*-nacl*]
433 || [istarget *-*-gnu*] } {
434 run_ld_link_exec_tests $array_tests_pie $xfails
435
436 if { $STATIC_PIE_LDFLAGS != "" } then {
437 run_ld_link_exec_tests [list \
438 [list \
439 "Static PIE preinit array" \
440 "$STATIC_PIE_LDFLAGS" \
441 "" \
442 {preinit.c} \
443 "preinit-static-pie" \
444 "preinit.out" \
445 "-fPIE" \
446 ] \
447 [list \
448 "Static PIE init array" \
449 "$STATIC_PIE_LDFLAGS" \
450 "" \
451 {init.c} \
452 "init-static-pie" \
453 "init.out" \
454 "-fPIE" \
455 ] \
456 [list \
457 "Static PIE fini array" \
458 "$STATIC_PIE_LDFLAGS" \
459 "" \
460 {fini.c} \
461 "fini-static-pie" \
462 "fini.out" \
463 "-fPIE" \
464 ] \
465 [list \
466 "Static PIE init array mixed" \
467 "$STATIC_PIE_LDFLAGS" \
468 "" \
469 {init-mixed.c} \
470 "init-mixed-static-pie" \
471 "init-mixed.out" \
472 "-I. -fPIE" \
473 ] \
474 [list \
475 "Static PIE PR ld/14525" \
476 "$STATIC_PIE_LDFLAGS" \
477 "" \
478 {pr14525.c} \
479 "pr14525-static-pie" \
480 "pr14525.out" \
481 "-fPIE" \
482 ] \
483 ]
484 }
485
486 run_ld_link_exec_tests [list \
487 [list \
488 "Run mbind2a" \
489 "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000 -Wl,-z,noexecstack" \
490 "" \
491 { mbind2a.s mbind2b.c } \
492 "mbind2a" \
493 "pass.out" \
494 "-O2 -I../bfd" \
495 ] \
496 [list \
497 "Run mbind2b" \
498 "-static -Wl,-z,common-page-size=0x4000 -Wl,-z,noexecstack" \
499 "" \
500 { mbind2a.s mbind2b.c } \
501 "mbind2b" \
502 "pass.out" \
503 "-O2 -I../bfd" \
504 ] \
505 ]
506 }
507
508 run_ld_link_exec_tests $array_tests_static $xfails
509
510 run_cc_link_tests [list \
511 [list \
512 "Build pr26391-1" \
513 "-Wl,-z,unique-symbol" \
514 "-fno-function-sections" \
515 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
516 {{nm "" pr26391.nd}} \
517 "pr26391-1" \
518 ] \
519 [list \
520 "Build pr26391-2" \
521 "-Wl,-z,unique-symbol" \
522 "-ffunction-sections" \
523 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
524 {{nm "" pr26391.nd}} \
525 "pr26391-2" \
526 ] \
527 [list \
528 "Build pr26391-3" \
529 "-Wl,-z,unique-symbol,--emit-relocs" \
530 "-fno-function-sections" \
531 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
532 {{nm "" pr26391.nd}} \
533 "pr26391-3" \
534 ] \
535 [list \
536 "Build pr26391-4" \
537 "-Wl,-z,unique-symbol,--emit-relocs" \
538 "-ffunction-sections" \
539 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
540 {{nm "" pr26391.nd}} \
541 "pr26391-4" \
542 ] \
543 [list \
544 "Build pr26391-5.o" \
545 "-Wl,-z,unique-symbol -r -nostdlib -nostartfiles" \
546 "-fno-function-sections" \
547 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
548 [list \
549 [list "nm" "$dep_plug_opt" "pr26391.nd"] \
550 [list "nm" "$dep_plug_opt" "pr26391.fd"] \
551 ] \
552 "pr26391-5.o" \
553 ] \
554 [list \
555 "Build pr26391-6.o" \
556 "-Wl,-z,unique-symbol -r -nostdlib -nostartfiles" \
557 "-ffunction-sections" \
558 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
559 {{nm "" pr26391.nd}} \
560 "pr26391-6.o" \
561 ] \
562 ]
563
564 run_ld_link_exec_tests [list \
565 [list \
566 "Run pr26391-1" \
567 "-Wl,-z,unique-symbol" \
568 "" \
569 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
570 "pr26391-1" \
571 "pr26391.out" \
572 "-fno-function-sections" \
573 ] \
574 [list \
575 "Run pr26391-2" \
576 "-Wl,-z,unique-symbol" \
577 "" \
578 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
579 "pr26391-2" \
580 "pr26391.out" \
581 "-ffunction-sections" \
582 ] \
583 [list \
584 "Run pr26391-3" \
585 "-Wl,-z,unique-symbol,--emit-relocs" \
586 "" \
587 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
588 "pr26391-3" \
589 "pr26391.out" \
590 "-fno-function-sections" \
591 ] \
592 [list \
593 "Run pr26391-4" \
594 "-Wl,-z,unique-symbol,--emit-relocs" \
595 "" \
596 {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
597 "pr26391-4" \
598 "pr26391.out" \
599 "-ffunction-sections" \
600 ] \
601 [list \
602 "Run pr26391-5" \
603 "-Wl,-z,unique-symbol" \
604 "" \
605 {dummy.c} \
606 "pr26391-5" \
607 "pr26391.out" \
608 "" \
609 "c" \
610 "" \
611 "tmpdir/pr26391-5.o" \
612 ] \
613 [list \
614 "Run pr26391-6" \
615 "-Wl,-z,unique-symbol" \
616 "" \
617 {dummy.c} \
618 "pr26391-6" \
619 "pr26391.out" \
620 "" \
621 "c" \
622 "" \
623 "tmpdir/pr26391-6.o" \
624 ] \
625 ]
626
627 set merge_exec_tests {
628 {"merge4" "" ""
629 {merge4a.c merge4b.s} "merge4" "merge4.out"}
630 }
631 run_ld_link_exec_tests $merge_exec_tests
632
633 catch "exec rm -f tmpdir/preinit tmpdir/init tmpdir/fini tmpdir/init-mixed" status