1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2021 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
6 # This file 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.
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.
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,
21 load_file $srcdir/../../binutils/testsuite/lib/binutils-common.exp
23 # Returns 1 if the gcc for the target is at least version MAJOR.MINOR
24 # Returns 0 otherwise.
26 proc at_least_gcc_version { major minor } {
29 if {![info exists CC]} {
35 # Filter out -Wl, options.
36 regsub -all -- "-Wl,\[^ ^\t\]+" $CC "" cc_cmd
37 set state [remote_exec host $cc_cmd --version]
38 if { [lindex $state 0] != 0 } {
41 set tmp "[lindex $state 1]\n"
42 # Look for (eg) 4.6.1 in the version output.
43 set ver_re "\[^\\.0-9\]+(\[1-9\]\[0-9\]*)\\.(\[0-9\]+)(?:\\.\[0-9\]+)?"
44 regexp $ver_re $tmp fred maj min
45 verbose "gcc version: $tmp"
46 if { ![info exists maj] || ![info exists min] } then {
47 perror "can't decipher gcc version number, fix the framework!"
50 verbose "major gcc version is $maj, want at least $major"
51 if { $maj == $major } then {
52 verbose "minor gcc version is $min, want at least $minor"
53 return [expr $min >= $minor]
55 return [expr $maj > $major]
59 # Extract and print the version number of ld.
61 proc default_ld_version { ld } {
64 if { ![is_remote host] && [which $ld] == 0 } then {
65 perror "$ld does not exist"
69 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
70 remote_upload host "ld.version"
71 set tmp [prune_warnings [file_contents "ld.version"]]
72 remote_file build delete "ld.version"
73 remote_file host delete "ld.version"
75 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
76 if [info exists number] then {
77 clone_output "$ld $number\n"
81 proc run_host_cmd { prog command } {
85 global gcc_ld_B_opt_tested
88 if { ![is_remote host] && [which "$prog"] == 0 } then {
89 perror "$prog does not exist"
93 # If we are compiling with gcc, we want to add gcc_B_opt and
94 # ld_L_opt to flags. However, if $prog already has -B options,
95 # which might be the case when running gcc out of a build
96 # directory, we want our -B options to come first.
98 set gccparm [string first " " $gccexe]
100 if { $gccparm > 0 } then {
101 set gccflags [string range $gccexe $gccparm end]
102 set gccexe [string range $gccexe 0 $gccparm]
105 set gccexe [string replace $gccexe 0 [string last "/" $gccexe] ""]
106 if {[string match "*cc*" $gccexe] ||
107 [string match "*++*" $gccexe] ||
108 [string match "clang*" $gccexe]} then {
109 set gccflags "$gcc_B_opt $gccflags $ld_L_opt"
110 if {![info exists gcc_ld_B_opt_tested]} {
111 set gcc_ld_B_opt_tested 1
112 set ld_version_message [run_host_cmd "$ld" "--version"]
113 set ver "-Wl,--version"
114 if [check_lto_available] {
115 set ver "-fno-lto $ver"
117 set gcc_ld_version_message [run_host_cmd "$prog" "$gccflags $ver"]
118 if {[string first $ld_version_message $gcc_ld_version_message] < 0} {
119 perror "************************************************************************"
120 perror "Your compiler driver ignores -B when choosing ld."
121 perror "You will not be testing the new ld in many of the following tests."
122 set gcc_ld_version [run_host_cmd "$prog" "$gccflags --print-prog-name=ld"]
123 if {![string match "" $gcc_ld_version] && ![string match "ld" $gcc_ld_version]} {
124 perror "It seems you will be testing $gcc_ld_version instead."
126 perror "************************************************************************"
131 verbose -log "$prog $gccflags $command"
132 set status [remote_exec host [concat sh -c [list "$prog $gccflags $command 2>&1"]] "" "/dev/null" "ld.tmp"]
133 remote_upload host "ld.tmp"
134 set link_output [file_contents "ld.tmp"]
135 regsub "\n$" $link_output "" link_output
136 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
137 append link_output "child process exited abnormally"
139 remote_file build delete ld.tmp
140 remote_file host delete ld.tmp
142 if [string match "" $link_output] then {
146 verbose -log "$link_output"
147 return "$link_output"
150 proc run_host_cmd_yesno { prog command } {
152 global errcnt warncnt
154 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
155 # Ignore error and warning.
158 if [string match "" $exec_output] then {
164 # Link an object using relocation.
166 proc default_ld_relocate { ld target objects } {
169 remote_file host delete $target
170 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
173 # Check to see if ld is being invoked with a non-endian output format
175 proc is_endian_output_format { object_flags } {
177 if {[string match "*-oformat binary*" $object_flags] || \
178 [string match "*-oformat ieee*" $object_flags] || \
179 [string match "*-oformat ihex*" $object_flags] || \
180 [string match "*-oformat netbsd-core*" $object_flags] || \
181 [string match "*-oformat srec*" $object_flags] || \
182 [string match "*-oformat tekhex*" $object_flags] || \
183 [string match "*-oformat trad-core*" $object_flags] } then {
190 # Link a program using ld
192 proc default_ld_link { ld target objects } {
197 if [is_endian_output_format $objects] then {
198 set flags [big_or_little_endian]
201 remote_file host delete $target
202 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
203 set exec_output [prune_warnings $exec_output]
205 # We don't care if we get a warning about a non-existent start
206 # symbol, since the default linker script might use ENTRY.
207 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
209 return [string match "" $exec_output]
212 # Compile an object using cc.
214 proc default_ld_compile { cc source object } {
223 if {[llength $cc_prog] > 1} then {
224 set cc_prog [lindex $cc_prog 0]
226 if {![is_remote host] && [which $cc_prog] == 0} then {
227 perror "$cc_prog does not exist"
231 remote_file build delete "$object"
232 remote_file host delete "$object"
234 set flags "$gcc_B_opt -I$srcdir/$subdir"
236 # If we are compiling with gcc, we want to add gcc_B_opt to flags.
237 # However, if $prog already has -B options, which might be the
238 # case when running gcc out of a build directory, we want our -B
239 # options to come first.
241 set ccparm [string first " " $cc]
243 if { $ccparm > 0 } then {
244 set ccflags [string range $cc $ccparm end]
245 set ccexe [string range $cc 0 $ccparm]
249 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
250 if {[string match "*++*" $ccexe]} {
251 append flags " $CXXFLAGS"
253 append flags " $CFLAGS"
256 if [board_info [target_info name] exists cflags] {
257 append flags " [board_info [target_info name] cflags]"
260 if [board_info [target_info name] exists multilib_flags] {
261 append flags " [board_info [target_info name] multilib_flags]"
264 set cmd "$cc $flags $ccflags -c $source -o $object"
267 set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
268 remote_upload host "ld.tmp"
269 set exec_output [file_contents "ld.tmp"]
270 remote_file build delete "ld.tmp"
271 remote_file host delete "ld.tmp"
272 set exec_output [prune_warnings $exec_output]
273 # Versions of gcc up to and including pre-release gcc-7, at least on
274 # some targets, generate .section directives with incorrect type.
275 # Ignore warnings from the assembler about this.
276 regsub -all "(^|\n)\[^\n\]*: ignoring incorrect section type \[^\n\]*" $exec_output "" exec_output
277 regsub -all "^\[^\n\]*: Assembler messages:\n" $exec_output "" exec_output
278 if [string match "" $exec_output] then {
279 if {![file exists $object]} then {
280 regexp ".*/(\[^/\]*)$" $source all dobj
281 regsub "\\.c" $dobj ".o" realobj
282 verbose "looking for $realobj"
283 if {[remote_file host exists $realobj]} then {
284 verbose -log "mv $realobj $object"
285 remote_upload "$realobj" "$object"
287 perror "$object not found after compilation"
293 verbose -log "$exec_output"
300 proc default_ld_assemble { as in_flags source object } {
306 if ![info exists ASFLAGS] { set ASFLAGS "" }
308 set flags [big_or_little_endian]
309 if [info exists subdir] {
310 append flags " -I$srcdir/$subdir"
312 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
313 set exec_output [prune_warnings $exec_output]
314 if [string match "" $exec_output] then {
321 # Run nm on a file, putting the result in the array nm_output.
323 proc default_ld_nm { nm nmflags object } {
328 if {[info exists nm_output]} {
332 if ![info exists NMFLAGS] { set NMFLAGS "" }
334 # Ensure consistent sorting of symbols
335 if {[info exists env(LC_ALL)]} {
336 set old_lc_all $env(LC_ALL)
340 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
342 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
343 if {[info exists old_lc_all]} {
344 set env(LC_ALL) $old_lc_all
348 remote_upload host "ld.stderr"
349 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
350 set exec_output [prune_warnings [file_contents "ld.stderr"]]
351 remote_file host delete "ld.stderr"
352 remote_file build delete "ld.stderr"
353 if [string match "" $exec_output] then {
354 set file [open tmpdir/nm.out r]
355 while { [gets $file line] != -1 } {
357 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
358 set name [string trimleft $name "_"]
359 verbose "Setting nm_output($name) to 0x$value" 2
360 set nm_output($name) 0x$value
366 verbose -log "$exec_output"
371 # Define various symbols needed when not linking against all
373 proc ld_link_defsyms {} {
375 set flags "--defsym __stack_chk_fail=0"
377 # ARM targets call __gccmain
378 if {[istarget arm*-*-*]} {
379 append flags " --defsym __gccmain=0"
382 # Windows targets need __main, some prefixed with underscore.
383 if [is_pecoff_format] {
384 append flags " --defsym __main=main --defsym ___main=main"
387 # PowerPC EABI code calls __eabi.
388 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
389 append flags " --defsym __eabi=0"
392 # mn10200 code calls __truncsipsi2_d0_d2.
393 if {[istarget mn10200*-*-*]} then {
394 append flags " --defsym __truncsipsi2_d0_d2=0"
397 # m6811/m6812 code has references to soft registers.
398 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
399 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
400 append flags " --defsym _.d3=0 --defsym _.d4=0"
401 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
404 # Some OpenBSD targets have ProPolice and reference __guard and
405 # __stack_smash_handler.
406 if [istarget *-*-openbsd*] {
407 append flags " --defsym __guard=0"
408 append flags " --defsym __stack_smash_handler=0"
414 # Create an archive using ar
416 proc ar_simple_create { ar aropts target objects } {
417 remote_file host delete $target
419 set exec_output [run_host_cmd "$ar" "$aropts rc $target $objects"]
420 set exec_output [prune_warnings $exec_output]
422 if [string match "" $exec_output] then {
423 send_log "$exec_output\n"
430 # List contains test-items with 3 items followed by 2 lists, one item and
433 # 1:ld/ar leading options, placed before object files
434 # 2:ld/ar trailing options, placed after object files
435 # 3:assembler options
436 # 4:filenames of assembler files
437 # 5:list of actions, options and expected outputs.
438 # 6:name of output file
439 # 7:compiler flags (optional)
441 # Actions: { command command-line-options file-containg-expected-output-regexps }
443 # objdump: Apply objdump options on result.
444 # nm: Apply nm options on result.
445 # readelf: Apply readelf options on result.
446 # ld: Don't apply anything on result. Compare output during linking with
447 # the file containing regexps (which is the second arg, not the third).
448 # Note that this *must* be the first action if it is to be used at all;
449 # in all other cases, any output from the linker during linking is
450 # treated as a sign of an error and FAILs the test.
452 # args is an optional list of target triplets to be xfailed.
454 proc run_ld_link_tests { ldtests args } {
470 set ld_extra_opt $LDFLAGS
471 if [check_relro_support] {
472 append ld_extra_opt " -z norelro"
475 foreach testitem $ldtests {
476 set testname [lindex $testitem 0]
478 if ![runtest_file_p $runtests $testname] then {
482 foreach target $args {
483 if [match_target $target] {
489 set ld_options [lindex $testitem 1]
490 set ld_after [lindex $testitem 2]
491 set as_options [lindex $testitem 3]
492 set src_files [lindex $testitem 4]
493 set actions [lindex $testitem 5]
494 set binfile tmpdir/[lindex $testitem 6]
495 set cflags [lindex $testitem 7]
502 # Add -fno-lto. LTO should be tested explicitly by $cflags.
503 if {[check_lto_available]} {
504 set cflags "-fno-lto $cflags"
507 # verbose -log "Testname is $testname"
508 # verbose -log "ld_options is $ld_options"
509 # verbose -log "ld_after is $ld_after"
510 # verbose -log "as_options is $as_options"
511 # verbose -log "src_files is $src_files"
512 # verbose -log "actions is $actions"
513 # verbose -log "binfile is $binfile"
515 # Assemble each file in the test.
516 foreach src_file $src_files {
517 set fileroot "[file rootname [file tail $src_file]]"
519 if { [file extension $src_file] == ".bz2" } {
520 set objfile tmpdir/[file rootname $src_file]
521 set unbzip2 "system \"bzip2 -dc $srcdir/$subdir/$src_file > $objfile\""
522 send_log "$unbzip2\n"
523 catch "$unbzip2" exec_output
524 if ![string match "" $exec_output] then {
525 send_log "$exec_output\n"
530 set objfile "tmpdir/$fileroot.o"
531 if { [file extension $src_file] == ".c" } {
532 set as_file "tmpdir/$fileroot.s"
533 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
538 set as_file "$srcdir/$subdir/$src_file"
540 if ![ld_assemble $as "$as_options $as_file" $objfile] {
545 lappend objfiles $objfile
548 # Catch assembler errors.
553 # Catch compiler errors.
554 if { $is_unsupported } {
555 unsupported $testname
559 if { $binfile eq "tmpdir/" } {
561 } elseif { [regexp ".*\\.a$" $binfile] } {
562 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
565 } elseif { ![ld_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
567 set ld_output "$exec_output"
572 foreach actionlist $actions {
573 set action [lindex $actionlist 0]
574 set progopts [lindex $actionlist 1]
576 # There are actions where we run regexp_diff on the
577 # output, and there are other actions (presumably).
578 # Handling of the former look the same.
582 { set dump_prog $objdump }
584 { set dump_prog $nm }
586 { set dump_prog $READELF }
588 { set dump_prog "ld" }
591 perror "Unrecognized action $action"
597 if { $action == "ld" } {
598 set regexpfile $progopts
599 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
600 set_file_contents "tmpdir/ld.messages" "$ld_output"
601 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
602 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
603 verbose "output is $ld_output" 2
608 } elseif { !$maybe_failed && $dump_prog != "" } {
609 set dumpfile [lindex $actionlist 2]
610 set binary $dump_prog
612 # Ensure consistent sorting of symbols
613 if {[info exists env(LC_ALL)]} {
614 set old_lc_all $env(LC_ALL)
617 set cmd "$binary $progopts $binfile"
618 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
620 remote_upload host "ld.stderr"
621 set comp_output [prune_warnings [file_contents "ld.stderr"]]
622 remote_file host delete "ld.stderr"
623 remote_file build delete "ld.stderr"
625 if {[info exists old_lc_all]} {
626 set env(LC_ALL) $old_lc_all
631 if ![string match "" $comp_output] then {
632 send_log "$comp_output\n"
637 remote_upload host "dump.out"
639 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
640 verbose "output is [file_contents "dump.out"]" 2
642 remote_file build delete "dump.out"
643 remote_file host delete "dump.out"
646 remote_file build delete "dump.out"
647 remote_file host delete "dump.out"
652 if { $is_unresolved } {
654 } elseif { $maybe_failed || $failed } {
662 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
663 # and 3 optional items:
665 # 1:ld leading options, placed before object files
666 # 2:assembler options
667 # 3:filenames of source files
668 # 4:name of output file
670 # 6:compiler flags (optional)
671 # 7:language (optional)
672 # 8:linker warning (optional)
673 # 9:ld trailing options, placed after object files (optional)
674 # args is an optional list of target triplets to be xfailed.
676 proc run_ld_link_exec_tests { ldtests args } {
689 global STATIC_LDFLAGS
691 # When using GCC as the linker driver, we need to specify board cflags when
692 # linking because cflags may contain linker options. For example when
693 # linker options are included in GCC spec files then we need the -specs
695 if [board_info [target_info name] exists cflags] {
696 set board_cflags " [board_info [target_info name] cflags]"
701 foreach testitem $ldtests {
702 set testname [lindex $testitem 0]
703 set ld_options [lindex $testitem 1]
704 set as_options [lindex $testitem 2]
705 set src_files [lindex $testitem 3]
706 set binfile tmpdir/[lindex $testitem 4]
707 set expfile [lindex $testitem 5]
708 set cflags [lindex $testitem 6]
709 set lang [lindex $testitem 7]
710 set warning [lindex $testitem 8]
711 set ld_after [lindex $testitem 9]
715 if { ![check_compiler_available] } {
716 unsupported $testname
720 # Add -fno-lto. LTO should be tested explicitly by $cflags.
721 if {[check_lto_available]} {
722 set cflags "-fno-lto $cflags"
725 foreach target $args {
726 if [match_target $target] {
732 # verbose -log "Testname is $testname"
733 # verbose -log "ld_options is $ld_options"
734 # verbose -log "as_options is $as_options"
735 # verbose -log "src_files is $src_files"
736 # verbose -log "binfile is $binfile"
738 # Assemble each file in the test.
739 foreach src_file $src_files {
740 set fileroot "[file rootname [file tail $src_file]]"
741 set objfile "tmpdir/$fileroot.o"
742 lappend objfiles $objfile
744 if { [ string match "c++" $lang ] } {
745 set cmd "$CXX -c $CXXFLAGS $cflags"
747 set cmd "$CC -c $CFLAGS $cflags"
749 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
754 if { $failed != 0 } {
755 unsupported $testname
759 if { [ string match "asm" $lang ] } {
760 set link_proc ld_link
762 } elseif { [ string match "c++" $lang ] } {
763 set link_proc ld_link
766 set link_proc ld_link
770 if { $binfile eq "tmpdir/" } {
775 if { [string match "" $STATIC_LDFLAGS] \
776 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ld_options $objfiles $ld_after "] } {
780 if ![$link_proc $link_cmd $binfile "$board_cflags -L$srcdir/$subdir $ld_options $objfiles $ld_after"] {
785 # Check if exec_output is expected.
786 if { $warning != "" } then {
787 verbose -log "returned with: <$exec_output>, expected: <$warning>"
788 if { [regexp $warning $exec_output] } then {
795 if { $failed == 0 && [isnative] } {
796 send_log "Running: $binfile > $binfile.out\n"
797 verbose "Running: $binfile > $binfile.out"
798 catch "exec $binfile > $binfile.out" exec_output
800 if ![string match "" $exec_output] then {
801 send_log "$exec_output\n"
802 verbose "$exec_output" 1
805 send_log [file_contents $binfile.out]
806 verbose [file_contents $binfile.out] 2
807 if [regexp_diff "$binfile.out" "$srcdir/$subdir/$expfile"] {
813 if { $failed != 0 } {
815 } elseif ![isnative] {
816 unsupported $testname
824 # List contains test-items with 3 items followed by 2 lists, one item and
829 # 3:filenames of source files
830 # 4:action and options.
831 # 5:name of output file
832 # 6:language (optional)
835 # objdump: Apply objdump options on result. Compare with regex (last arg).
836 # nm: Apply nm options on result. Compare with regex (last arg).
837 # readelf: Apply readelf options on result. Compare with regex (last arg).
838 # warning: Check linker output against regex (last arg).
839 # error: Like 'warning' but checking output in error case.
840 # warning_output: Check linker output against regex in a file (last arg).
841 # error_output: Like 'warning_output' but checking output in error case.
843 proc run_cc_link_tests { ldtests } {
857 global STATIC_LDFLAGS
859 if [board_info [target_info name] exists cflags] {
860 set board_cflags " [board_info [target_info name] cflags]"
865 foreach testitem $ldtests {
866 set testname [lindex $testitem 0]
867 set ldflags [lindex $testitem 1]
868 set cflags [lindex $testitem 2]
869 set src_files [lindex $testitem 3]
870 set actions [lindex $testitem 4]
871 set binfile tmpdir/[lindex $testitem 5]
872 set lang [lindex $testitem 6]
876 set check_ld(terminal) 0
877 set check_ld(source) ""
879 if { ![check_compiler_available] } {
880 unsupported $testname
884 # Add -fno-lto. LTO should be tested explicitly by $cflags.
885 if {[check_lto_available]} {
886 set cflags "-fno-lto $cflags"
889 #verbose -log "testname is $testname"
890 #verbose -log "ldflags is $ldflags"
891 #verbose -log "cflags is $cflags"
892 #verbose -log "src_files is $src_files"
893 #verbose -log "actions is $actions"
894 #verbose -log "binfile is $binfile"
895 #verbose -log "lang is $lang"
897 foreach actionlist $actions {
898 set action [lindex $actionlist 0]
899 set progopts [lindex $actionlist 1]
901 # Find actions related to error/warning processing.
905 set check_ld(source) "regexp"
906 set check_ld(regexp) $progopts
907 set check_ld(terminal) 1
911 set check_ld(source) "regexp"
912 set check_ld(regexp) $progopts
916 set check_ld(source) "file"
917 set check_ld(file) $progopts
918 set check_ld(terminal) 1
922 set check_ld(source) "file"
923 set check_ld(file) $progopts
928 # Compile each file in the test.
929 foreach src_file $src_files {
930 set fileroot "[file rootname [file tail $src_file]]"
931 set objfile "tmpdir/$fileroot.o"
932 lappend objfiles $objfile
934 if { [ string match "c++" $lang ] } {
935 set cmd "$CXX -c $CXXFLAGS $cflags"
937 set cmd "$CC -c $CFLAGS $cflags"
939 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
944 if { $failed != 0 } {
945 unsupported $testname
949 # Clear error and warning counts.
952 if { [ string match "c++" $lang ] } {
958 if { $binfile eq "tmpdir/" } {
961 } elseif { [regexp ".*\\.a$" $binfile] } {
962 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
966 if { [string match "" $STATIC_LDFLAGS] \
967 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ldflags $objfiles "] } {
971 ld_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"
972 set ld_output "$exec_output"
974 if { $check_ld(source) == "regexp" } then {
975 # Match output against regexp argument.
976 verbose -log "returned with: <$ld_output>, expected: <$check_ld(regexp)>"
977 if { ![regexp $check_ld(regexp) $ld_output] } then {
980 } elseif { $check_ld(source) == "file" } then {
981 # Match output against patterns in a file.
982 set_file_contents "tmpdir/ld.messages" "$ld_output"
983 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
984 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"] } then {
985 verbose "output is $ld_output" 2
990 if { $check_ld(source) != "" } then {
991 if { $ld_output == "" } then {
992 verbose -log "Linker was expected to give error or warning"
996 if { $ld_output != "" } then {
997 verbose -log "Unexpected linker warning or error"
1003 if { $failed == 0 } {
1004 foreach actionlist $actions {
1005 set action [lindex $actionlist 0]
1006 set progopts [lindex $actionlist 1]
1008 # There are actions where we run regexp_diff on the
1009 # output, and there are other actions (presumably).
1010 # Handling of the former look the same.
1014 { set dump_prog $objdump }
1016 { set dump_prog $nm }
1018 { set dump_prog $READELF }
1025 perror "Unrecognized action $action"
1031 if { $dump_prog != "" } {
1032 set dumpfile [lindex $actionlist 2]
1033 set binary $dump_prog
1035 # Ensure consistent sorting of symbols
1036 if {[info exists env(LC_ALL)]} {
1037 set old_lc_all $env(LC_ALL)
1040 set cmd "$binary $progopts $binfile > dump.out"
1042 catch "exec $cmd" comp_output
1043 if {[info exists old_lc_all]} {
1044 set env(LC_ALL) $old_lc_all
1048 set comp_output [prune_warnings $comp_output]
1050 if ![string match "" $comp_output] then {
1051 send_log "$comp_output\n"
1056 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1057 verbose "output is [file_contents "dump.out"]" 2
1067 } elseif { $is_unresolved } {
1068 unresolved $testname
1075 # Returns true if --gc-sections is supported on the target.
1077 proc check_gc_sections_available { } {
1078 global gc_sections_available_saved
1081 if {![info exists gc_sections_available_saved]} {
1082 # Some targets don't support gc-sections despite whatever's
1083 # advertised by ld's options.
1084 if { [istarget alpha-*-*]
1085 || [istarget bpf-*-*]
1086 || [istarget d30v-*-*]
1087 || [istarget dlx-*-*]
1088 || [istarget hppa*64-*-*]
1089 || [istarget ia64-*-*]
1090 || [istarget mep-*-*]
1091 || [istarget mn10200-*-*]
1092 || [istarget pj*-*-*]
1093 || [istarget pru*-*-*]
1094 || [istarget s12z-*-*]
1095 || [istarget xgate-*-*]
1096 || [istarget z80-*-*] } {
1097 set gc_sections_available_saved 0
1101 # elf2flt uses -q (--emit-relocs), which is incompatible with
1103 if { [board_info target exists ldflags]
1104 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1105 set gc_sections_available_saved 0
1109 # Check if the ld used by gcc supports --gc-sections.
1110 # FIXME: this test is useless since ld --help always says
1111 # --gc-sections is available
1112 set ld_output [remote_exec host $ld "--help"]
1113 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1114 set gc_sections_available_saved 1
1116 set gc_sections_available_saved 0
1119 return $gc_sections_available_saved
1122 # Return true if target uses genelf.em.
1123 proc uses_genelf { } {
1124 if { [istarget "d30v-*-*"]
1125 || [istarget "dlx-*-*"]
1126 || [istarget "fr30-*-*"]
1127 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1128 || [istarget "ft32-*-*"]
1129 || [istarget "iq2000-*-*"]
1130 || [istarget "mn10200-*-*"]
1131 || [istarget "msp430-*-*"]
1132 || [istarget "mt-*-*"]
1133 || [istarget "pj*-*-*"]
1134 || [istarget "s12z-*-*"]
1135 || [istarget "xgate-*-*"] } {
1141 proc is_underscore_target { } {
1142 global is_underscore_target_saved
1143 global target_triplet
1146 if { ![info exists is_underscore_target_saved] } {
1147 set cmd "targ=$target_triplet . $srcdir/../../bfd/config.bfd &&"
1148 append cmd { echo "$targ_underscore"}
1150 set status [catch {exec sh -c $cmd} result]
1151 if { $status == 0 && [string match "yes" $result] } {
1152 set is_underscore_target_saved 1
1154 set is_underscore_target_saved 0
1157 return $is_underscore_target_saved
1160 # Returns true if the target ld supports the plugin API.
1161 proc check_plugin_api_available { } {
1162 global plugin_api_available_saved
1164 if {![info exists plugin_api_available_saved]} {
1165 # Check if the ld used by gcc supports --plugin.
1166 set ld_output [remote_exec host $ld "--help"]
1167 if { [regexp -- "-plugin PLUGIN \[^\n\r\]*" $ld_output line]
1168 && ![regexp "ignored" $line] } {
1169 set plugin_api_available_saved 1
1171 set plugin_api_available_saved 0
1174 return $plugin_api_available_saved
1177 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1178 # returns true if the target ld supports sysroot.
1179 proc check_sysroot_available { } {
1180 global ld_sysroot_available_saved ld ld_sysroot
1181 if {![info exists ld_sysroot_available_saved]} {
1182 # Check if ld supports --sysroot *other* than empty.
1183 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1184 if { $ld_sysroot == "" } {
1185 set ld_sysroot_available_saved 0
1187 set ld_sysroot_available_saved 1
1190 return $ld_sysroot_available_saved
1193 # Return true if we can build a program with the compiler.
1194 # On some targets, CC might be defined, but libraries and startup
1195 # code might be missing or require special options that the ld test
1196 # harness doesn't know about.
1198 proc check_compiler_available { } {
1199 global compiler_available_saved
1202 if {![info exists compiler_available_saved]} {
1203 if { [which $CC] == 0 } {
1204 set compiler_available_saved 0
1209 if [board_info [target_info name] exists cflags] {
1210 append flags " [board_info [target_info name] cflags]"
1212 if [board_info [target_info name] exists ldflags] {
1213 append flags " [board_info [target_info name] ldflags]"
1216 set basename "tmpdir/compiler[pid]"
1217 set src ${basename}.c
1218 set output ${basename}.out
1219 set f [open $src "w"]
1220 puts $f "int main (void)"
1222 puts $f " return 0; "
1225 if [is_remote host] {
1226 set src [remote_download host $src]
1228 set compiler_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1229 remote_file host delete $src
1230 remote_file host delete $output
1233 return $compiler_available_saved
1236 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1237 proc check_gcc_plugin_enabled { } {
1240 if {![info exists CC]} {
1246 # Filter out -Wl, options.
1247 regsub -all -- "-Wl,\[^ ^\t\]+" $CC "" cc_cmd
1248 set state [remote_exec host $cc_cmd -v]
1249 if { [lindex $state 0] != 0 } {
1252 for { set i 1 } { $i < [llength $state] } { incr i } {
1253 set v [lindex $state $i]
1254 if { [ string match "*--disable-plugin*" $v ] } {
1255 verbose "plugin is disabled by $v"
1263 # Returns true if the target compiler supports LTO
1264 proc check_lto_available { } {
1265 global lto_available_saved
1268 if {![info exists lto_available_saved]} {
1269 if { ![check_gcc_plugin_enabled] } {
1270 set lto_available_saved 0
1273 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1274 # -ffat-lto-objects, we always run LTO tests on Linux with
1276 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1277 set lto_available_saved 1
1280 # Check if gcc supports -flto -fuse-linker-plugin
1282 if [board_info [target_info name] exists cflags] {
1283 append flags " [board_info [target_info name] cflags]"
1285 if [board_info [target_info name] exists ldflags] {
1286 append flags " [board_info [target_info name] ldflags]"
1289 set basename "tmpdir/lto[pid]"
1290 set src ${basename}.c
1291 set output ${basename}.out
1292 set f [open $src "w"]
1293 puts $f "int main() { return 0; }"
1295 if [is_remote host] {
1296 set src [remote_download host $src]
1298 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1299 remote_file host delete $src
1300 remote_file host delete $output
1303 return $lto_available_saved
1306 # Returns true if the target compiler supports LTO -ffat-lto-objects
1307 proc check_lto_fat_available { } {
1308 global lto_fat_available_saved
1311 if {![info exists lto_fat_available_saved]} {
1312 if { ![check_gcc_plugin_enabled] } {
1313 set lto_fat_available_saved 0
1316 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1317 # -ffat-lto-objects, we always run LTO tests on Linux with
1319 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1320 set lto_fat_available_saved 1
1323 # Check if gcc supports -flto -fuse-linker-plugin
1325 if [board_info [target_info name] exists cflags] {
1326 append flags " [board_info [target_info name] cflags]"
1328 if [board_info [target_info name] exists ldflags] {
1329 append flags " [board_info [target_info name] ldflags]"
1332 set basename "tmpdir/lto[pid]"
1333 set src ${basename}.c
1334 set output ${basename}.out
1335 set f [open $src "w"]
1336 puts $f "int main() { return 0; }"
1338 if [is_remote host] {
1339 set src [remote_download host $src]
1341 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
1342 remote_file host delete $src
1343 remote_file host delete $output
1346 return $lto_fat_available_saved
1349 # Returns true if the target compiler supports LTO and -shared
1350 proc check_lto_shared_available { } {
1351 global lto_shared_available_saved
1354 if {![info exists lto_shared_available_saved]} {
1355 if { ![check_gcc_plugin_enabled] } {
1356 set lto_shared_available_saved 0
1359 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1360 # -ffat-lto-objects, we always run LTO tests on Linux with
1362 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1363 set lto_shared_available_saved 1
1366 # Check if gcc supports -flto -fuse-linker-plugin -shared
1368 if [board_info [target_info name] exists cflags] {
1369 append flags " [board_info [target_info name] cflags]"
1371 if [board_info [target_info name] exists ldflags] {
1372 append flags " [board_info [target_info name] ldflags]"
1375 set basename "tmpdir/lto_shared[pid]"
1376 set src ${basename}.c
1377 set output ${basename}.so
1378 set f [open $src "w"]
1381 if [is_remote host] {
1382 set src [remote_download host $src]
1384 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
1385 remote_file host delete $src
1386 remote_file host delete $output
1389 return $lto_shared_available_saved
1392 # Check if the assembler supports CFI statements.
1394 proc check_as_cfi { } {
1395 global check_as_cfi_result
1397 if [info exists check_as_cfi_result] {
1398 return $check_as_cfi_result
1400 set as_file "tmpdir/check_as_cfi.s"
1401 set as_fh [open $as_file w 0666]
1402 puts $as_fh "# Generated file. DO NOT EDIT"
1403 puts $as_fh "\t.cfi_startproc"
1404 puts $as_fh "\t.cfi_endproc"
1406 remote_download host $as_file
1407 verbose -log "Checking CFI support:"
1408 set success [ld_assemble $as $as_file "/dev/null"]
1409 #remote_file host delete $as_file
1410 set check_as_cfi_result $success
1414 # Returns true if IFUNC works.
1416 proc check_ifunc_available { } {
1417 global ifunc_available_saved
1420 if {![info exists ifunc_available_saved]} {
1421 if { ![check_compiler_available] } {
1422 set ifunc_available_saved 0
1425 # Check if gcc supports -flto -fuse-linker-plugin
1427 if [board_info [target_info name] exists cflags] {
1428 append flags " [board_info [target_info name] cflags]"
1430 if [board_info [target_info name] exists ldflags] {
1431 append flags " [board_info [target_info name] ldflags]"
1434 set basename "tmpdir/ifunc[pid]"
1435 set src ${basename}.c
1436 set output ${basename}.out
1437 set f [open $src "w"]
1438 puts $f "extern int library_func2 (void);"
1439 puts $f "int main (void)"
1441 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1442 puts $f " return 0; "
1444 puts $f "static int library_func1 (void) {return 2; }"
1445 puts $f "void *foo (void) __asm__ (\"library_func2\");"
1446 puts $f "void *foo (void) { return library_func1; }"
1447 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
1449 if [is_remote host] {
1450 set src [remote_download host $src]
1452 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1453 if { [isnative] && $ifunc_available_saved == 1 } {
1454 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
1456 remote_file host delete $src
1457 remote_file host delete $output
1460 return $ifunc_available_saved
1463 # Returns true if ifunc attribute works.
1465 proc check_ifunc_attribute_available { } {
1466 global ifunc_attribute_available_saved
1469 if {![info exists ifunc_attribute_available_saved]} {
1470 if { ![check_compiler_available] } {
1471 set ifunc_attribute_available_saved 0
1474 # Check if gcc supports -flto -fuse-linker-plugin
1476 if [board_info [target_info name] exists cflags] {
1477 append flags " [board_info [target_info name] cflags]"
1479 if [board_info [target_info name] exists ldflags] {
1480 append flags " [board_info [target_info name] ldflags]"
1483 set basename "tmpdir/ifunc[pid]"
1484 set src ${basename}.c
1485 set output ${basename}.out
1486 set f [open $src "w"]
1487 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
1488 puts $f "int main (void)"
1490 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1491 puts $f " return 0; "
1493 puts $f "static int library_func1 (void) {return 2; }"
1494 puts $f "void *foo (void) { return library_func1; }"
1496 if [is_remote host] {
1497 set src [remote_download host $src]
1499 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1500 if { [isnative] && $ifunc_attribute_available_saved == 1 } {
1501 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
1503 remote_file host delete $src
1504 remote_file host delete $output
1507 return $ifunc_attribute_available_saved
1510 # Return true if libdl is supported.
1512 proc check_libdl_available { } {
1513 global libdl_available_saved
1516 if {![info exists libdl_available_saved]} {
1517 if { ![check_compiler_available] } {
1518 set libdl_available_saved 0
1522 set basename "tmpdir/dl_avail_test[pid]"
1523 set src ${basename}.c
1524 set output ${basename}.out
1525 set f [open $src "w"]
1527 puts $f "#include <dlfcn.h>"
1528 puts $f "int main (void)"
1530 puts $f " dlopen (\"dummy.so\", RTLD_NOW);"
1531 puts $f " return 0; "
1534 if [is_remote host] {
1535 set src [remote_download host $src]
1537 set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
1538 remote_file host delete $src
1539 remote_file host delete $output
1542 return $libdl_available_saved
1545 # Returns true if GNU2 TLS works.
1547 proc check_gnu2_tls_available { } {
1548 global gnu2_tls_available_saved
1552 if {![info exists gnu2_tls_available_saved]} {
1553 if { ![check_compiler_available] || "$GNU2_CFLAGS" == "" } {
1554 set gnu2_tls_available_saved 0
1557 # Check if GNU2 TLS works.
1558 set flags "$GNU2_CFLAGS"
1559 if [board_info [target_info name] exists cflags] {
1560 append flags " [board_info [target_info name] cflags]"
1562 if [board_info [target_info name] exists ldflags] {
1563 append flags " [board_info [target_info name] ldflags]"
1566 set basename "tmpdir/gnu2_tls[pid]"
1567 set src1 ${basename}1.c
1568 set output1 ${basename}.so
1569 set f [open $src1 "w"]
1570 puts $f "extern __thread int zzz;"
1571 puts $f "int foo (void)"
1573 puts $f " return zzz;"
1576 if [is_remote host] {
1577 set src1 [remote_download host $src1]
1579 set src2 ${basename}2.c
1580 set output2 ${basename}.exe
1581 set f [open $src2 "w"]
1582 puts $f "__thread int zzz = 20;"
1583 puts $f "extern int foo (void);"
1584 puts $f "int main (void)"
1586 puts $f " if (foo () != 20) __builtin_abort ();"
1587 puts $f " return 0; "
1590 if [is_remote host] {
1591 set src2 [remote_download host $src2]
1593 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "-fPIC -shared $flags $src1 -o $output1"]
1594 if { $gnu2_tls_available_saved == 1 } {
1595 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "$flags $src2 $output1 -o $output2"]
1596 if { $gnu2_tls_available_saved == 1 } {
1597 set gnu2_tls_available_saved [run_host_cmd_yesno "$output2" ""]
1600 remote_file host delete $src1
1601 remote_file host delete $output1
1602 remote_file host delete $src2
1603 remote_file host delete $output2
1604 file delete $src1 $src2
1606 return $gnu2_tls_available_saved
1609 # Compile a C source file, with the specified additional_flags.
1610 proc compile_one_cc { src output additional_flags } {
1615 if [board_info [target_info name] exists cflags] {
1616 append flags " [board_info [target_info name] cflags]"
1618 if [board_info [target_info name] exists ldflags] {
1619 append flags " [board_info [target_info name] ldflags]"
1622 if [is_remote host] {
1623 set src [remote_download host $src]
1625 return [run_host_cmd_yesno "$CC" "$flags $CFLAGS $additional_flags $src -o $output"]
1628 # Returns true if the target compiler supports -gctf
1629 proc check_ctf_available { } {
1630 global ctf_available_saved
1632 if {![info exists ctf_available_saved]} {
1633 if { ![check_compiler_available] } {
1634 set ctf_available_saved 0
1636 set basename "tmpdir/ctf_available[pid]"
1637 set src ${basename}.c
1638 set output ${basename}.o
1639 set f [open $src "w"]
1640 puts $f "int main() { return 0; }"
1642 set ctf_available_saved [compile_one_cc $src $output "-gctf -c"]
1643 remote_file host delete $src
1644 remote_file host delete $output
1648 return $ctf_available_saved
1651 proc skip_ctf_tests { } {
1652 global enable_libctf
1654 if {$enable_libctf eq "no"} {
1658 if [check_ctf_available] {