1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2020 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 proc load_common_lib { name } {
23 load_file $srcdir/../../binutils/testsuite/lib/$name
26 load_common_lib binutils-common.exp
28 # Returns 1 if the gcc for the target is at least version MAJOR.MINOR
29 # Returns 0 otherwise.
31 proc at_least_gcc_version { major minor } {
34 if {![info exists CC]} {
40 set state [remote_exec host $CC --version]
41 if { [lindex $state 0] != 0 } {
44 set tmp "[lindex $state 1]\n"
45 # Look for (eg) 4.6.1 in the version output.
46 set ver_re "\[^\\.0-9\]+(\[1-9\]\[0-9\]*)\\.(\[0-9\]+)(?:\\.\[0-9\]+)?"
47 regexp $ver_re $tmp fred maj min
48 verbose "gcc version: $tmp"
49 if { ![info exists maj] || ![info exists min] } then {
50 perror "can't decipher gcc version number, fix the framework!"
53 verbose "major gcc version is $maj, want at least $major"
54 if { $maj == $major } then {
55 verbose "minor gcc version is $min, want at least $minor"
56 return [expr $min >= $minor]
58 return [expr $maj > $major]
62 # Extract and print the version number of ld.
64 proc default_ld_version { ld } {
67 if { ![is_remote host] && [which $ld] == 0 } then {
68 perror "$ld does not exist"
72 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
73 remote_upload host "ld.version"
74 set tmp [prune_warnings [file_contents "ld.version"]]
75 remote_file build delete "ld.version"
76 remote_file host delete "ld.version"
78 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
79 if [info exists number] then {
80 clone_output "$ld $number\n"
84 proc run_host_cmd { prog command } {
88 global gcc_ld_B_opt_tested
91 if { ![is_remote host] && [which "$prog"] == 0 } then {
92 perror "$prog does not exist"
96 # If we are compiling with gcc, we want to add gcc_B_opt and
97 # ld_L_opt to flags. However, if $prog already has -B options,
98 # which might be the case when running gcc out of a build
99 # directory, we want our -B options to come first.
101 set gccparm [string first " " $gccexe]
103 if { $gccparm > 0 } then {
104 set gccflags [string range $gccexe $gccparm end]
105 set gccexe [string range $gccexe 0 $gccparm]
108 set gccexe [string replace $gccexe 0 [string last "/" $gccexe] ""]
109 if {[string match "*cc*" $gccexe] || [string match "*++*" $gccexe]} then {
110 set gccflags "$gcc_B_opt $gccflags $ld_L_opt"
111 if {![info exists gcc_ld_B_opt_tested]} {
112 set gcc_ld_B_opt_tested 1
113 set ld_version_message [run_host_cmd "$ld" "--version"]
114 set gcc_ld_version_message [run_host_cmd "$prog" "$gccflags -Wl,--version"]
115 if {[string first $ld_version_message $gcc_ld_version_message] < 0} {
116 perror "************************************************************************"
117 perror "Your compiler driver ignores -B when choosing ld."
118 perror "You will not be testing the new ld in many of the following tests."
119 set gcc_ld_version [run_host_cmd "$prog" "$gccflags --print-prog-name=ld"]
120 if {![string match "" $gcc_ld_version] && ![string match "ld" $gcc_ld_version]} {
122 perror "It seems you will be testing $gcc_ld_version instead."
124 perror "************************************************************************"
129 verbose -log "$prog $gccflags $command"
130 set status [remote_exec host [concat sh -c [list "$prog $gccflags $command 2>&1"]] "" "/dev/null" "ld.tmp"]
131 remote_upload host "ld.tmp"
132 set link_output [file_contents "ld.tmp"]
133 regsub "\n$" $link_output "" link_output
134 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
135 append link_output "child process exited abnormally"
137 remote_file build delete ld.tmp
138 remote_file host delete ld.tmp
140 if [string match "" $link_output] then {
144 verbose -log "$link_output"
145 return "$link_output"
148 proc run_host_cmd_yesno { prog command } {
150 global errcnt warncnt
152 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
153 # Ignore error and warning.
156 if [string match "" $exec_output] then {
162 # Link an object using relocation.
164 proc default_ld_relocate { ld target objects } {
167 remote_file host delete $target
168 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
171 # Check to see if ld is being invoked with a non-endian output format
173 proc is_endian_output_format { object_flags } {
175 if {[string match "*-oformat binary*" $object_flags] || \
176 [string match "*-oformat ieee*" $object_flags] || \
177 [string match "*-oformat ihex*" $object_flags] || \
178 [string match "*-oformat netbsd-core*" $object_flags] || \
179 [string match "*-oformat srec*" $object_flags] || \
180 [string match "*-oformat tekhex*" $object_flags] || \
181 [string match "*-oformat trad-core*" $object_flags] } then {
188 # Link a program using ld
190 proc default_ld_link { ld target objects } {
195 if [is_endian_output_format $objects] then {
196 set flags [big_or_little_endian]
199 remote_file host delete $target
200 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
201 set exec_output [prune_warnings $exec_output]
203 # We don't care if we get a warning about a non-existent start
204 # symbol, since the default linker script might use ENTRY.
205 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
207 return [string match "" $exec_output]
210 # Compile an object using cc.
212 proc default_ld_compile { cc source object } {
221 if {[llength $cc_prog] > 1} then {
222 set cc_prog [lindex $cc_prog 0]
224 if {![is_remote host] && [which $cc_prog] == 0} then {
225 perror "$cc_prog does not exist"
229 remote_file build delete "$object"
230 remote_file host delete "$object"
232 set flags "$gcc_B_opt -I$srcdir/$subdir"
234 # If we are compiling with gcc, we want to add gcc_B_opt to flags.
235 # However, if $prog already has -B options, which might be the
236 # case when running gcc out of a build directory, we want our -B
237 # options to come first.
239 set ccparm [string first " " $cc]
241 if { $ccparm > 0 } then {
242 set ccflags [string range $cc $ccparm end]
243 set ccexe [string range $cc 0 $ccparm]
247 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
248 if {[string match "*++*" $ccexe]} {
249 append flags " $CXXFLAGS"
251 append flags " $CFLAGS"
254 if [board_info [target_info name] exists cflags] {
255 append flags " [board_info [target_info name] cflags]"
258 if [board_info [target_info name] exists multilib_flags] {
259 append flags " [board_info [target_info name] multilib_flags]"
262 set cmd "$cc $flags $ccflags -c $source -o $object"
265 set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
266 remote_upload host "ld.tmp"
267 set exec_output [file_contents "ld.tmp"]
268 remote_file build delete "ld.tmp"
269 remote_file host delete "ld.tmp"
270 set exec_output [prune_warnings $exec_output]
271 # Versions of gcc up to and including pre-release gcc-7, at least on
272 # some targets, generate .section directives with incorrect type.
273 # Ignore warnings from the assembler about this.
274 regsub -all "(^|\n)\[^\n\]*: ignoring incorrect section type \[^\n\]*" $exec_output "" exec_output
275 regsub -all "^\[^\n\]*: Assembler messages:\n" $exec_output "" exec_output
276 if [string match "" $exec_output] then {
277 if {![file exists $object]} then {
278 regexp ".*/(\[^/\]*)$" $source all dobj
279 regsub "\\.c" $dobj ".o" realobj
280 verbose "looking for $realobj"
281 if {[remote_file host exists $realobj]} then {
282 verbose -log "mv $realobj $object"
283 remote_upload "$realobj" "$object"
285 perror "$object not found after compilation"
291 verbose -log "$exec_output"
292 perror "$source: compilation failed"
299 proc default_ld_assemble { as in_flags source object } {
305 if ![info exists ASFLAGS] { set ASFLAGS "" }
307 set flags "[big_or_little_endian] -I$srcdir/$subdir"
308 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
309 set exec_output [prune_warnings $exec_output]
310 if [string match "" $exec_output] then {
313 perror "$source: assembly failed"
318 # Run nm on a file, putting the result in the array nm_output.
320 proc default_ld_nm { nm nmflags object } {
325 if {[info exists nm_output]} {
329 if ![info exists NMFLAGS] { set NMFLAGS "" }
331 # Ensure consistent sorting of symbols
332 if {[info exists env(LC_ALL)]} {
333 set old_lc_all $env(LC_ALL)
337 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
339 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
340 if {[info exists old_lc_all]} {
341 set env(LC_ALL) $old_lc_all
345 remote_upload host "ld.stderr"
346 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
347 set exec_output [prune_warnings [file_contents "ld.stderr"]]
348 remote_file host delete "ld.stderr"
349 remote_file build delete "ld.stderr"
350 if [string match "" $exec_output] then {
351 set file [open tmpdir/nm.out r]
352 while { [gets $file line] != -1 } {
354 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
355 set name [string trimleft $name "_"]
356 verbose "Setting nm_output($name) to 0x$value" 2
357 set nm_output($name) 0x$value
363 verbose -log "$exec_output"
364 perror "$object: nm failed"
369 # Define various symbols needed when not linking against all
371 proc ld_link_defsyms {} {
373 set flags "--defsym __stack_chk_fail=0"
375 # ARM targets call __gccmain
376 if {[istarget arm*-*-*]} {
377 append flags " --defsym __gccmain=0"
380 # Windows targets need __main, some prefixed with underscore.
381 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
382 append flags " --defsym __main=0 --defsym ___main=0"
385 # PowerPC EABI code calls __eabi.
386 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
387 append flags " --defsym __eabi=0"
390 # mn10200 code calls __truncsipsi2_d0_d2.
391 if {[istarget mn10200*-*-*]} then {
392 append flags " --defsym __truncsipsi2_d0_d2=0"
395 # m6811/m6812 code has references to soft registers.
396 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
397 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
398 append flags " --defsym _.d3=0 --defsym _.d4=0"
399 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
402 # Some OpenBSD targets have ProPolice and reference __guard and
403 # __stack_smash_handler.
404 if [istarget *-*-openbsd*] {
405 append flags " --defsym __guard=0"
406 append flags " --defsym __stack_smash_handler=0"
412 # Create an archive using ar
414 proc ar_simple_create { ar aropts target objects } {
415 remote_file host delete $target
417 set exec_output [run_host_cmd "$ar" "-rc $aropts $target $objects"]
418 set exec_output [prune_warnings $exec_output]
420 if [string match "" $exec_output] then {
421 send_log "$exec_output\n"
428 # List contains test-items with 3 items followed by 2 lists, one item and
431 # 1:ld/ar leading options, placed before object files
432 # 2:ld/ar trailing options, placed after object files
433 # 3:assembler options
434 # 4:filenames of assembler files
435 # 5:list of actions, options and expected outputs.
436 # 6:name of output file
437 # 7:compiler flags (optional)
439 # Actions: { command command-line-options file-containg-expected-output-regexps }
441 # objdump: Apply objdump options on result.
442 # nm: Apply nm options on result.
443 # readelf: Apply readelf options on result.
444 # ld: Don't apply anything on result. Compare output during linking with
445 # the file containing regexps (which is the second arg, not the third).
446 # Note that this *must* be the first action if it is to be used at all;
447 # in all other cases, any output from the linker during linking is
448 # treated as a sign of an error and FAILs the test.
450 # args is an optional list of target triplets to be xfailed.
452 proc run_ld_link_tests { ldtests args } {
466 global ld_elf_shared_opt
468 if { [is_elf_format] && [check_shared_lib_support] } {
469 set ld_extra_opt "$ld_elf_shared_opt"
474 foreach testitem $ldtests {
475 set testname [lindex $testitem 0]
477 if ![runtest_file_p $runtests $testname] then {
481 foreach target $args {
482 if [match_target $target] {
488 set ld_options [lindex $testitem 1]
489 set ld_after [lindex $testitem 2]
490 set as_options [lindex $testitem 3]
491 set src_files [lindex $testitem 4]
492 set actions [lindex $testitem 5]
493 set binfile tmpdir/[lindex $testitem 6]
494 set cflags [lindex $testitem 7]
501 # verbose -log "Testname is $testname"
502 # verbose -log "ld_options is $ld_options"
503 # verbose -log "ld_after is $ld_after"
504 # verbose -log "as_options is $as_options"
505 # verbose -log "src_files is $src_files"
506 # verbose -log "actions is $actions"
507 # verbose -log "binfile is $binfile"
509 # Assemble each file in the test.
510 foreach src_file $src_files {
511 set fileroot "[file rootname [file tail $src_file]]"
512 set objfile "tmpdir/$fileroot.o"
513 lappend objfiles $objfile
515 if { [file extension $src_file] == ".c" } {
516 set as_file "tmpdir/$fileroot.s"
517 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
522 set as_file "$srcdir/$subdir/$src_file"
524 if ![ld_assemble $as "$as_options $as_file" $objfile] {
530 # Catch assembler errors.
531 if { $is_unresolved } {
536 if { $binfile eq "tmpdir/" } {
538 } elseif { [regexp ".*\\.a$" $binfile] } {
539 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
542 } elseif { ![ld_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
544 set ld_output "$exec_output"
548 foreach actionlist $actions {
549 set action [lindex $actionlist 0]
550 set progopts [lindex $actionlist 1]
552 # There are actions where we run regexp_diff on the
553 # output, and there are other actions (presumably).
554 # Handling of the former look the same.
558 { set dump_prog $objdump }
560 { set dump_prog $nm }
562 { set dump_prog $READELF }
564 { set dump_prog "ld" }
567 perror "Unrecognized action $action"
573 if { $action == "ld" } {
574 set regexpfile $progopts
575 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
576 set_file_contents "tmpdir/ld.messages" "$ld_output"
577 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
578 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
579 verbose "output is $ld_output" 2
584 } elseif { !$maybe_failed && $dump_prog != "" } {
585 set dumpfile [lindex $actionlist 2]
586 set binary $dump_prog
588 # Ensure consistent sorting of symbols
589 if {[info exists env(LC_ALL)]} {
590 set old_lc_all $env(LC_ALL)
593 set cmd "$binary $progopts $binfile"
594 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
596 remote_upload host "ld.stderr"
597 set comp_output [prune_warnings [file_contents "ld.stderr"]]
598 remote_file host delete "ld.stderr"
599 remote_file build delete "ld.stderr"
601 if {[info exists old_lc_all]} {
602 set env(LC_ALL) $old_lc_all
607 if ![string match "" $comp_output] then {
608 send_log "$comp_output\n"
613 remote_upload host "dump.out"
615 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
616 verbose "output is [file_contents "dump.out"]" 2
618 remote_file build delete "dump.out"
619 remote_file host delete "dump.out"
622 remote_file build delete "dump.out"
623 remote_file host delete "dump.out"
628 if { $is_unresolved } {
630 } elseif { $maybe_failed || $failed } {
638 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
639 # and 3 optional items:
641 # 1:ld leading options, placed before object files
642 # 2:assembler options
643 # 3:filenames of source files
644 # 4:name of output file
646 # 6:compiler flags (optional)
647 # 7:language (optional)
648 # 8:linker warning (optional)
649 # 9:ld trailing options, placed after object files (optional)
650 # args is an optional list of target triplets to be xfailed.
652 proc run_ld_link_exec_tests { ldtests args } {
665 global STATIC_LDFLAGS
667 # When using GCC as the linker driver, we need to specify board cflags when
668 # linking because cflags may contain linker options. For example when
669 # linker options are included in GCC spec files then we need the -specs
671 if [board_info [target_info name] exists cflags] {
672 set board_cflags " [board_info [target_info name] cflags]"
677 foreach testitem $ldtests {
678 set testname [lindex $testitem 0]
679 set ld_options [lindex $testitem 1]
680 set as_options [lindex $testitem 2]
681 set src_files [lindex $testitem 3]
682 set binfile tmpdir/[lindex $testitem 4]
683 set expfile [lindex $testitem 5]
684 set cflags [lindex $testitem 6]
685 set lang [lindex $testitem 7]
686 set warning [lindex $testitem 8]
687 set ld_after [lindex $testitem 9]
691 if { ![check_compiler_available] } {
692 unsupported $testname
696 foreach target $args {
697 if [match_target $target] {
703 # verbose -log "Testname is $testname"
704 # verbose -log "ld_options is $ld_options"
705 # verbose -log "as_options is $as_options"
706 # verbose -log "src_files is $src_files"
707 # verbose -log "binfile is $binfile"
709 # Assemble each file in the test.
710 foreach src_file $src_files {
711 set fileroot "[file rootname [file tail $src_file]]"
712 set objfile "tmpdir/$fileroot.o"
713 lappend objfiles $objfile
715 if { [ string match "c++" $lang ] } {
716 set cmd "$CXX -c $CXXFLAGS $cflags"
718 set cmd "$CC -c $CFLAGS $cflags"
720 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
725 if { $failed != 0 } {
730 if { [ string match "asm" $lang ] } {
731 set link_proc ld_link
733 } elseif { [ string match "c++" $lang ] } {
734 set link_proc ld_link
737 set link_proc ld_link
741 if { $binfile eq "tmpdir/" } {
746 if { [string match "" $STATIC_LDFLAGS] \
747 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ld_options $objfiles $ld_after "] } {
751 if ![$link_proc $link_cmd $binfile "$board_cflags -L$srcdir/$subdir $ld_options $objfiles $ld_after"] {
756 # Check if exec_output is expected.
757 if { $warning != "" } then {
758 verbose -log "returned with: <$exec_output>, expected: <$warning>"
759 if { [regexp $warning $exec_output] } then {
766 if { $failed == 0 && [isnative] } {
767 send_log "Running: $binfile > $binfile.out\n"
768 verbose "Running: $binfile > $binfile.out"
769 catch "exec $binfile > $binfile.out" exec_output
771 if ![string match "" $exec_output] then {
772 send_log "$exec_output\n"
773 verbose "$exec_output" 1
776 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
777 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
778 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
779 set exec_output [prune_warnings $exec_output]
781 if ![string match "" $exec_output] then {
782 send_log "$exec_output\n"
783 verbose "$exec_output" 1
789 if { $failed != 0 } {
791 } elseif ![isnative] {
792 unsupported $testname
800 # List contains test-items with 3 items followed by 2 lists, one item and
805 # 3:filenames of source files
806 # 4:action and options.
807 # 5:name of output file
808 # 6:language (optional)
811 # objdump: Apply objdump options on result. Compare with regex (last arg).
812 # nm: Apply nm options on result. Compare with regex (last arg).
813 # readelf: Apply readelf options on result. Compare with regex (last arg).
814 # warning: Check linker output against regex (last arg).
815 # error: Like 'warning' but checking output in error case.
816 # warning_output: Check linker output against regex in a file (last arg).
817 # error_output: Like 'warning_output' but checking output in error case.
819 proc run_cc_link_tests { ldtests } {
833 global STATIC_LDFLAGS
835 if [board_info [target_info name] exists cflags] {
836 set board_cflags " [board_info [target_info name] cflags]"
841 foreach testitem $ldtests {
842 set testname [lindex $testitem 0]
843 set ldflags [lindex $testitem 1]
844 set cflags [lindex $testitem 2]
845 set src_files [lindex $testitem 3]
846 set actions [lindex $testitem 4]
847 set binfile tmpdir/[lindex $testitem 5]
848 set lang [lindex $testitem 6]
852 set check_ld(terminal) 0
853 set check_ld(source) ""
855 if { ![check_compiler_available] } {
856 unsupported $testname
860 #verbose -log "testname is $testname"
861 #verbose -log "ldflags is $ldflags"
862 #verbose -log "cflags is $cflags"
863 #verbose -log "src_files is $src_files"
864 #verbose -log "actions is $actions"
865 #verbose -log "binfile is $binfile"
866 #verbose -log "lang is $lang"
868 foreach actionlist $actions {
869 set action [lindex $actionlist 0]
870 set progopts [lindex $actionlist 1]
872 # Find actions related to error/warning processing.
876 set check_ld(source) "regexp"
877 set check_ld(regexp) $progopts
878 set check_ld(terminal) 1
882 set check_ld(source) "regexp"
883 set check_ld(regexp) $progopts
887 set check_ld(source) "file"
888 set check_ld(file) $progopts
889 set check_ld(terminal) 1
893 set check_ld(source) "file"
894 set check_ld(file) $progopts
899 # Compile each file in the test.
900 foreach src_file $src_files {
901 set fileroot "[file rootname [file tail $src_file]]"
902 set objfile "tmpdir/$fileroot.o"
903 lappend objfiles $objfile
905 if { [ string match "c++" $lang ] } {
906 set cmd "$CXX -c $CXXFLAGS $cflags"
908 set cmd "$CC -c $CFLAGS $cflags"
910 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
915 if { $failed != 0 } {
920 # Clear error and warning counts.
923 if { [ string match "c++" $lang ] } {
929 if { $binfile eq "tmpdir/" } {
932 } elseif { [regexp ".*\\.a$" $binfile] } {
933 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
937 if { [string match "" $STATIC_LDFLAGS] \
938 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ldflags $objfiles "] } {
942 ld_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"
943 set ld_output "$exec_output"
945 if { $check_ld(source) == "regexp" } then {
946 # Match output against regexp argument.
947 verbose -log "returned with: <$ld_output>, expected: <$check_ld(regexp)>"
948 if { ![regexp $check_ld(regexp) $ld_output] } then {
951 } elseif { $check_ld(source) == "file" } then {
952 # Match output against patterns in a file.
953 set_file_contents "tmpdir/ld.messages" "$ld_output"
954 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
955 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"] } then {
956 verbose "output is $ld_output" 2
961 if { $check_ld(source) != "" } then {
962 if { $ld_output == "" } then {
963 verbose -log "Linker was expected to give error or warning"
967 if { $ld_output != "" } then {
968 verbose -log "Unexpected linker warning or error"
974 if { $failed == 0 } {
975 foreach actionlist $actions {
976 set action [lindex $actionlist 0]
977 set progopts [lindex $actionlist 1]
979 # There are actions where we run regexp_diff on the
980 # output, and there are other actions (presumably).
981 # Handling of the former look the same.
985 { set dump_prog $objdump }
987 { set dump_prog $nm }
989 { set dump_prog $READELF }
996 perror "Unrecognized action $action"
1002 if { $dump_prog != "" } {
1003 set dumpfile [lindex $actionlist 2]
1004 set binary $dump_prog
1006 # Ensure consistent sorting of symbols
1007 if {[info exists env(LC_ALL)]} {
1008 set old_lc_all $env(LC_ALL)
1011 set cmd "$binary $progopts $binfile > dump.out"
1013 catch "exec $cmd" comp_output
1014 if {[info exists old_lc_all]} {
1015 set env(LC_ALL) $old_lc_all
1019 set comp_output [prune_warnings $comp_output]
1021 if ![string match "" $comp_output] then {
1022 send_log "$comp_output\n"
1027 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1028 verbose "output is [file_contents "dump.out"]" 2
1038 } elseif { $is_unresolved } {
1039 unresolved $testname
1046 # Returns true if --gc-sections is supported on the target.
1048 proc check_gc_sections_available { } {
1049 global gc_sections_available_saved
1052 if {![info exists gc_sections_available_saved]} {
1053 # Some targets don't support gc-sections despite whatever's
1054 # advertised by ld's options.
1055 if { [istarget alpha-*-*]
1056 || [istarget bpf-*-*]
1057 || [istarget d30v-*-*]
1058 || [istarget dlx-*-*]
1059 || [istarget hppa*64-*-*]
1060 || [istarget ia64-*-*]
1061 || [istarget mep-*-*]
1062 || [istarget mn10200-*-*]
1063 || [istarget pj*-*-*]
1064 || [istarget pru*-*-*]
1065 || [istarget s12z-*-*]
1066 || [istarget xgate-*-*]
1067 || [istarget z80-*-*] } {
1068 set gc_sections_available_saved 0
1072 # elf2flt uses -q (--emit-relocs), which is incompatible with
1074 if { [board_info target exists ldflags]
1075 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1076 set gc_sections_available_saved 0
1080 # Check if the ld used by gcc supports --gc-sections.
1081 # FIXME: this test is useless since ld --help always says
1082 # --gc-sections is available
1083 set ld_output [remote_exec host $ld "--help"]
1084 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1085 set gc_sections_available_saved 1
1087 set gc_sections_available_saved 0
1090 return $gc_sections_available_saved
1093 # Return true if target uses genelf.em.
1094 proc uses_genelf { } {
1095 if { [istarget "d30v-*-*"]
1096 || [istarget "dlx-*-*"]
1097 || [istarget "fr30-*-*"]
1098 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1099 || [istarget "ft32-*-*"]
1100 || [istarget "iq2000-*-*"]
1101 || [istarget "mn10200-*-*"]
1102 || [istarget "msp430-*-*"]
1103 || [istarget "mt-*-*"]
1104 || [istarget "pj*-*-*"]
1105 || [istarget "s12z-*-*"]
1106 || [istarget "xgate-*-*"] } {
1112 proc is_underscore_target { } {
1113 global is_underscore_target_saved
1114 global target_triplet
1117 if { ![info exists is_underscore_target_saved] } {
1118 set cmd "targ=$target_triplet . $srcdir/../../bfd/config.bfd &&"
1119 append cmd { echo "$targ_underscore"}
1121 set status [catch {exec sh -c $cmd} result]
1122 if { $status == 0 && [string match "yes" $result] } {
1123 set is_underscore_target_saved 1
1125 set is_underscore_target_saved 0
1128 return $is_underscore_target_saved
1131 # Returns true if the target ld supports the plugin API.
1132 proc check_plugin_api_available { } {
1133 global plugin_api_available_saved
1135 if {![info exists plugin_api_available_saved]} {
1136 # Check if the ld used by gcc supports --plugin.
1137 set ld_output [remote_exec host $ld "--help"]
1138 if { [regexp -- "-plugin PLUGIN \[^\n\r\]*" $ld_output line]
1139 && ![regexp "ignored" $line] } {
1140 set plugin_api_available_saved 1
1142 set plugin_api_available_saved 0
1145 return $plugin_api_available_saved
1148 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1149 # returns true if the target ld supports sysroot.
1150 proc check_sysroot_available { } {
1151 global ld_sysroot_available_saved ld ld_sysroot
1152 if {![info exists ld_sysroot_available_saved]} {
1153 # Check if ld supports --sysroot *other* than empty.
1154 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1155 if { $ld_sysroot == "" } {
1156 set ld_sysroot_available_saved 0
1158 set ld_sysroot_available_saved 1
1161 return $ld_sysroot_available_saved
1164 # Return true if we can build a program with the compiler.
1165 # On some targets, CC might be defined, but libraries and startup
1166 # code might be missing or require special options that the ld test
1167 # harness doesn't know about.
1169 proc check_compiler_available { } {
1170 global compiler_available_saved
1173 if {![info exists compiler_available_saved]} {
1174 if { [which $CC] == 0 } {
1175 set compiler_available_saved 0
1180 if [board_info [target_info name] exists cflags] {
1181 append flags " [board_info [target_info name] cflags]"
1183 if [board_info [target_info name] exists ldflags] {
1184 append flags " [board_info [target_info name] ldflags]"
1187 set basename "tmpdir/compiler[pid]"
1188 set src ${basename}.c
1189 set output ${basename}.out
1190 set f [open $src "w"]
1191 puts $f "int main (void)"
1193 puts $f " return 0; "
1196 if [is_remote host] {
1197 set src [remote_download host $src]
1199 set compiler_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1200 remote_file host delete $src
1201 remote_file host delete $output
1204 return $compiler_available_saved
1207 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1208 proc check_gcc_plugin_enabled { } {
1211 if {![info exists CC]} {
1217 set state [remote_exec host $CC -v]
1218 if { [lindex $state 0] != 0 } {
1221 for { set i 1 } { $i < [llength $state] } { incr i } {
1222 set v [lindex $state $i]
1223 if { [ string match "*--disable-plugin*" $v ] } {
1224 verbose "plugin is disabled by $v"
1232 # Returns true if the target compiler supports LTO
1233 proc check_lto_available { } {
1234 global lto_available_saved
1237 if {![info exists lto_available_saved]} {
1238 if { ![check_gcc_plugin_enabled] } {
1239 set lto_available_saved 0
1242 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1243 # -ffat-lto-objects, we always run LTO tests on Linux with
1245 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1246 set lto_available_saved 1
1249 # Check if gcc supports -flto -fuse-linker-plugin
1251 if [board_info [target_info name] exists cflags] {
1252 append flags " [board_info [target_info name] cflags]"
1254 if [board_info [target_info name] exists ldflags] {
1255 append flags " [board_info [target_info name] ldflags]"
1258 set basename "tmpdir/lto[pid]"
1259 set src ${basename}.c
1260 set output ${basename}.out
1261 set f [open $src "w"]
1262 puts $f "int main() { return 0; }"
1264 if [is_remote host] {
1265 set src [remote_download host $src]
1267 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1268 remote_file host delete $src
1269 remote_file host delete $output
1272 return $lto_available_saved
1275 # Returns true if the target compiler supports LTO -ffat-lto-objects
1276 proc check_lto_fat_available { } {
1277 global lto_fat_available_saved
1280 if {![info exists lto_fat_available_saved]} {
1281 if { ![check_gcc_plugin_enabled] } {
1282 set lto_fat_available_saved 0
1285 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1286 # -ffat-lto-objects, we always run LTO tests on Linux with
1288 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1289 set lto_fat_available_saved 1
1292 # Check if gcc supports -flto -fuse-linker-plugin
1294 if [board_info [target_info name] exists cflags] {
1295 append flags " [board_info [target_info name] cflags]"
1297 if [board_info [target_info name] exists ldflags] {
1298 append flags " [board_info [target_info name] ldflags]"
1301 set basename "tmpdir/lto[pid]"
1302 set src ${basename}.c
1303 set output ${basename}.out
1304 set f [open $src "w"]
1305 puts $f "int main() { return 0; }"
1307 if [is_remote host] {
1308 set src [remote_download host $src]
1310 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
1311 remote_file host delete $src
1312 remote_file host delete $output
1315 return $lto_fat_available_saved
1318 # Returns true if the target compiler supports LTO and -shared
1319 proc check_lto_shared_available { } {
1320 global lto_shared_available_saved
1323 if {![info exists lto_shared_available_saved]} {
1324 if { ![check_gcc_plugin_enabled] } {
1325 set lto_shared_available_saved 0
1328 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1329 # -ffat-lto-objects, we always run LTO tests on Linux with
1331 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1332 set lto_shared_available_saved 1
1335 # Check if gcc supports -flto -fuse-linker-plugin -shared
1337 if [board_info [target_info name] exists cflags] {
1338 append flags " [board_info [target_info name] cflags]"
1340 if [board_info [target_info name] exists ldflags] {
1341 append flags " [board_info [target_info name] ldflags]"
1344 set basename "tmpdir/lto_shared[pid]"
1345 set src ${basename}.c
1346 set output ${basename}.so
1347 set f [open $src "w"]
1350 if [is_remote host] {
1351 set src [remote_download host $src]
1353 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
1354 remote_file host delete $src
1355 remote_file host delete $output
1358 return $lto_shared_available_saved
1361 # Check if the assembler supports CFI statements.
1363 proc check_as_cfi { } {
1364 global check_as_cfi_result
1366 if [info exists check_as_cfi_result] {
1367 return $check_as_cfi_result
1369 set as_file "tmpdir/check_as_cfi.s"
1370 set as_fh [open $as_file w 0666]
1371 puts $as_fh "# Generated file. DO NOT EDIT"
1372 puts $as_fh "\t.cfi_startproc"
1373 puts $as_fh "\t.cfi_endproc"
1375 remote_download host $as_file
1376 verbose -log "Checking CFI support:"
1377 rename "perror" "check_as_cfi_perror"
1378 proc perror { args } { }
1379 set success [ld_assemble $as $as_file "/dev/null"]
1381 rename "check_as_cfi_perror" "perror"
1382 #remote_file host delete $as_file
1383 set check_as_cfi_result $success
1387 # Returns true if IFUNC works.
1389 proc check_ifunc_available { } {
1390 global ifunc_available_saved
1393 if {![info exists ifunc_available_saved]} {
1394 if { ![check_compiler_available] } {
1395 set ifunc_available_saved 0
1398 # Check if gcc supports -flto -fuse-linker-plugin
1400 if [board_info [target_info name] exists cflags] {
1401 append flags " [board_info [target_info name] cflags]"
1403 if [board_info [target_info name] exists ldflags] {
1404 append flags " [board_info [target_info name] ldflags]"
1407 set basename "tmpdir/ifunc[pid]"
1408 set src ${basename}.c
1409 set output ${basename}.out
1410 set f [open $src "w"]
1411 puts $f "extern int library_func2 (void);"
1412 puts $f "int main (void)"
1414 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1415 puts $f " return 0; "
1417 puts $f "static int library_func1 (void) {return 2; }"
1418 puts $f "void *foo (void) __asm__ (\"library_func2\");"
1419 puts $f "void *foo (void) { return library_func1; }"
1420 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
1422 if [is_remote host] {
1423 set src [remote_download host $src]
1425 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1426 if { [isnative] && $ifunc_available_saved == 1 } {
1427 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
1429 remote_file host delete $src
1430 remote_file host delete $output
1433 return $ifunc_available_saved
1436 # Returns true if ifunc attribute works.
1438 proc check_ifunc_attribute_available { } {
1439 global ifunc_attribute_available_saved
1442 if {![info exists ifunc_attribute_available_saved]} {
1443 if { ![check_compiler_available] } {
1444 set ifunc_attribute_available_saved 0
1447 # Check if gcc supports -flto -fuse-linker-plugin
1449 if [board_info [target_info name] exists cflags] {
1450 append flags " [board_info [target_info name] cflags]"
1452 if [board_info [target_info name] exists ldflags] {
1453 append flags " [board_info [target_info name] ldflags]"
1456 set basename "tmpdir/ifunc[pid]"
1457 set src ${basename}.c
1458 set output ${basename}.out
1459 set f [open $src "w"]
1460 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
1461 puts $f "int main (void)"
1463 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1464 puts $f " return 0; "
1466 puts $f "static int library_func1 (void) {return 2; }"
1467 puts $f "void *foo (void) { return library_func1; }"
1469 if [is_remote host] {
1470 set src [remote_download host $src]
1472 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1473 if { [isnative] && $ifunc_attribute_available_saved == 1 } {
1474 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
1476 remote_file host delete $src
1477 remote_file host delete $output
1480 return $ifunc_attribute_available_saved
1483 # Return true if libdl is supported.
1485 proc check_libdl_available { } {
1486 global libdl_available_saved
1489 if {![info exists libdl_available_saved]} {
1490 if { ![check_compiler_available] } {
1491 set libdl_available_saved 0
1495 set basename "tmpdir/dl_avail_test[pid]"
1496 set src ${basename}.c
1497 set output ${basename}.out
1498 set f [open $src "w"]
1500 puts $f "#include <dlfcn.h>"
1501 puts $f "int main (void)"
1503 puts $f " dlopen (\"dummy.so\", RTLD_NOW);"
1504 puts $f " return 0; "
1507 if [is_remote host] {
1508 set src [remote_download host $src]
1510 set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
1511 remote_file host delete $src
1512 remote_file host delete $output
1515 return $libdl_available_saved
1518 # Returns true if GNU2 TLS works.
1520 proc check_gnu2_tls_available { } {
1521 global gnu2_tls_available_saved
1525 if {![info exists gnu2_tls_available_saved]} {
1526 if { ![check_compiler_available] || "$GNU2_CFLAGS" == "" } {
1527 set gnu2_tls_available_saved 0
1530 # Check if GNU2 TLS works.
1531 set flags "$GNU2_CFLAGS"
1532 if [board_info [target_info name] exists cflags] {
1533 append flags " [board_info [target_info name] cflags]"
1535 if [board_info [target_info name] exists ldflags] {
1536 append flags " [board_info [target_info name] ldflags]"
1539 set basename "tmpdir/gnu2_tls[pid]"
1540 set src1 ${basename}1.c
1541 set output1 ${basename}.so
1542 set f [open $src1 "w"]
1543 puts $f "extern __thread int zzz;"
1544 puts $f "int foo (void)"
1546 puts $f " return zzz;"
1549 if [is_remote host] {
1550 set src1 [remote_download host $src1]
1552 set src2 ${basename}2.c
1553 set output2 ${basename}.exe
1554 set f [open $src2 "w"]
1555 puts $f "__thread int zzz = 20;"
1556 puts $f "extern int foo (void);"
1557 puts $f "int main (void)"
1559 puts $f " if (foo () != 20) __builtin_abort ();"
1560 puts $f " return 0; "
1563 if [is_remote host] {
1564 set src2 [remote_download host $src2]
1566 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "-fPIC -shared $flags $src1 -o $output1"]
1567 if { $gnu2_tls_available_saved == 1 } {
1568 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "$flags $src2 $output1 -o $output2"]
1569 if { $gnu2_tls_available_saved == 1 } {
1570 set gnu2_tls_available_saved [run_host_cmd_yesno "$output2" ""]
1573 remote_file host delete $src1
1574 remote_file host delete $output1
1575 remote_file host delete $src2
1576 remote_file host delete $output2
1577 file delete $src1 $src2
1579 return $gnu2_tls_available_saved