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