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