1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2019 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 {
485 set ld_options [lindex $testitem 1]
486 set ld_after [lindex $testitem 2]
487 set as_options [lindex $testitem 3]
488 set src_files [lindex $testitem 4]
489 set actions [lindex $testitem 5]
490 set binfile tmpdir/[lindex $testitem 6]
491 set cflags [lindex $testitem 7]
498 # verbose -log "Testname is $testname"
499 # verbose -log "ld_options is $ld_options"
500 # verbose -log "ld_after is $ld_after"
501 # verbose -log "as_options is $as_options"
502 # verbose -log "src_files is $src_files"
503 # verbose -log "actions is $actions"
504 # verbose -log "binfile is $binfile"
506 # Assemble each file in the test.
507 foreach src_file $src_files {
508 set fileroot "[file rootname [file tail $src_file]]"
509 set objfile "tmpdir/$fileroot.o"
510 lappend objfiles $objfile
512 if { [file extension $src_file] == ".c" } {
513 set as_file "tmpdir/$fileroot.s"
514 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
519 set as_file "$srcdir/$subdir/$src_file"
521 if ![ld_assemble $as "$as_options $as_file" $objfile] {
527 # Catch assembler errors.
528 if { $is_unresolved } {
533 if { $binfile eq "tmpdir/" } {
535 } elseif { [regexp ".*\\.a$" $binfile] } {
536 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
539 } elseif { ![ld_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
541 set ld_output "$exec_output"
545 foreach actionlist $actions {
546 set action [lindex $actionlist 0]
547 set progopts [lindex $actionlist 1]
549 # There are actions where we run regexp_diff on the
550 # output, and there are other actions (presumably).
551 # Handling of the former look the same.
555 { set dump_prog $objdump }
557 { set dump_prog $nm }
559 { set dump_prog $READELF }
561 { set dump_prog "ld" }
564 perror "Unrecognized action $action"
570 if { $action == "ld" } {
571 set regexpfile $progopts
572 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
573 set_file_contents "tmpdir/ld.messages" "$ld_output"
574 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
575 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
576 verbose "output is $ld_output" 2
581 } elseif { !$maybe_failed && $dump_prog != "" } {
582 set dumpfile [lindex $actionlist 2]
583 set binary $dump_prog
585 # Ensure consistent sorting of symbols
586 if {[info exists env(LC_ALL)]} {
587 set old_lc_all $env(LC_ALL)
590 set cmd "$binary $progopts $binfile"
591 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
593 remote_upload host "ld.stderr"
594 set comp_output [prune_warnings [file_contents "ld.stderr"]]
595 remote_file host delete "ld.stderr"
596 remote_file build delete "ld.stderr"
598 if {[info exists old_lc_all]} {
599 set env(LC_ALL) $old_lc_all
604 if ![string match "" $comp_output] then {
605 send_log "$comp_output\n"
610 remote_upload host "dump.out"
612 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
613 verbose "output is [file_contents "dump.out"]" 2
615 remote_file build delete "dump.out"
616 remote_file host delete "dump.out"
619 remote_file build delete "dump.out"
620 remote_file host delete "dump.out"
625 if { $is_unresolved } {
627 } elseif { $maybe_failed || $failed } {
635 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
636 # and 3 optional items:
638 # 1:ld leading options, placed before object files
639 # 2:assembler options
640 # 3:filenames of source files
641 # 4:name of output file
643 # 6:compiler flags (optional)
644 # 7:language (optional)
645 # 8:linker warning (optional)
646 # 9:ld trailing options, placed after object files (optional)
647 # args is an optional list of target triplets to be xfailed.
649 proc run_ld_link_exec_tests { ldtests args } {
662 global STATIC_LDFLAGS
664 # When using GCC as the linker driver, we need to specify board cflags when
665 # linking because cflags may contain linker options. For example when
666 # linker options are included in GCC spec files then we need the -specs
668 if [board_info [target_info name] exists cflags] {
669 set board_cflags " [board_info [target_info name] cflags]"
674 foreach testitem $ldtests {
675 foreach target $args {
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 # verbose -log "Testname is $testname"
692 # verbose -log "ld_options is $ld_options"
693 # verbose -log "as_options is $as_options"
694 # verbose -log "src_files is $src_files"
695 # verbose -log "binfile is $binfile"
697 # Assemble each file in the test.
698 foreach src_file $src_files {
699 set fileroot "[file rootname [file tail $src_file]]"
700 set objfile "tmpdir/$fileroot.o"
701 lappend objfiles $objfile
703 if { [ string match "c++" $lang ] } {
704 set cmd "$CXX -c $CXXFLAGS $cflags"
706 set cmd "$CC -c $CFLAGS $cflags"
708 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
713 if { $failed != 0 } {
718 if { [ string match "asm" $lang ] } {
719 set link_proc ld_link
721 } elseif { [ string match "c++" $lang ] } {
722 set link_proc ld_link
725 set link_proc ld_link
729 if { $binfile eq "tmpdir/" } {
734 if { [string match "" $STATIC_LDFLAGS] \
735 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ld_options $objfiles $ld_after "] } {
739 if ![$link_proc $link_cmd $binfile "$board_cflags -L$srcdir/$subdir $ld_options $objfiles $ld_after"] {
744 # Check if exec_output is expected.
745 if { $warning != "" } then {
746 verbose -log "returned with: <$exec_output>, expected: <$warning>"
747 if { [regexp $warning $exec_output] } then {
754 if { $failed == 0 && [isnative] } {
755 send_log "Running: $binfile > $binfile.out\n"
756 verbose "Running: $binfile > $binfile.out"
757 catch "exec $binfile > $binfile.out" exec_output
759 if ![string match "" $exec_output] then {
760 send_log "$exec_output\n"
761 verbose "$exec_output" 1
764 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
765 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
766 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
767 set exec_output [prune_warnings $exec_output]
769 if ![string match "" $exec_output] then {
770 send_log "$exec_output\n"
771 verbose "$exec_output" 1
777 if { $failed != 0 } {
779 } elseif ![isnative] {
780 unsupported $testname
788 # List contains test-items with 3 items followed by 2 lists, one item and
793 # 3:filenames of source files
794 # 4:action and options.
795 # 5:name of output file
796 # 6:language (optional)
799 # objdump: Apply objdump options on result. Compare with regex (last arg).
800 # nm: Apply nm options on result. Compare with regex (last arg).
801 # readelf: Apply readelf options on result. Compare with regex (last arg).
802 # warning: Check linker output against regex (last arg).
803 # error: Like 'warning' but checking output in error case.
804 # warning_output: Check linker output against regex in a file (last arg).
805 # error_output: Like 'warning_output' but checking output in error case.
807 proc run_cc_link_tests { ldtests } {
821 global STATIC_LDFLAGS
823 if [board_info [target_info name] exists cflags] {
824 set board_cflags " [board_info [target_info name] cflags]"
829 foreach testitem $ldtests {
830 set testname [lindex $testitem 0]
831 set ldflags [lindex $testitem 1]
832 set cflags [lindex $testitem 2]
833 set src_files [lindex $testitem 3]
834 set actions [lindex $testitem 4]
835 set binfile tmpdir/[lindex $testitem 5]
836 set lang [lindex $testitem 6]
840 set check_ld(terminal) 0
841 set check_ld(source) ""
843 #verbose -log "testname is $testname"
844 #verbose -log "ldflags is $ldflags"
845 #verbose -log "cflags is $cflags"
846 #verbose -log "src_files is $src_files"
847 #verbose -log "actions is $actions"
848 #verbose -log "binfile is $binfile"
849 #verbose -log "lang is $lang"
851 foreach actionlist $actions {
852 set action [lindex $actionlist 0]
853 set progopts [lindex $actionlist 1]
855 # Find actions related to error/warning processing.
859 set check_ld(source) "regexp"
860 set check_ld(regexp) $progopts
861 set check_ld(terminal) 1
865 set check_ld(source) "regexp"
866 set check_ld(regexp) $progopts
870 set check_ld(source) "file"
871 set check_ld(file) $progopts
872 set check_ld(terminal) 1
876 set check_ld(source) "file"
877 set check_ld(file) $progopts
882 # Compile each file in the test.
883 foreach src_file $src_files {
884 set fileroot "[file rootname [file tail $src_file]]"
885 set objfile "tmpdir/$fileroot.o"
886 lappend objfiles $objfile
888 if { [ string match "c++" $lang ] } {
889 set cmd "$CXX -c $CXXFLAGS $cflags"
891 set cmd "$CC -c $CFLAGS $cflags"
893 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
898 if { $failed != 0 } {
903 # Clear error and warning counts.
906 if { [ string match "c++" $lang ] } {
912 if { $binfile eq "tmpdir/" } {
914 } elseif { [regexp ".*\\.a$" $binfile] } {
915 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
919 if { [string match "" $STATIC_LDFLAGS] \
920 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ldflags $objfiles "] } {
924 ld_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"
925 set ld_output "$exec_output"
927 if { $check_ld(source) == "regexp" } then {
928 # Match output against regexp argument.
929 verbose -log "returned with: <$ld_output>, expected: <$check_ld(regexp)>"
930 if { ![regexp $check_ld(regexp) $ld_output] } then {
933 } elseif { $check_ld(source) == "file" } then {
934 # Match output against patterns in a file.
935 set_file_contents "tmpdir/ld.messages" "$ld_output"
936 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
937 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"] } then {
938 verbose "output is $ld_output" 2
943 if { $check_ld(source) != "" } then {
944 if { $ld_output == "" } then {
945 verbose -log "Linker was expected to give error or warning"
949 if { $ld_output != "" } then {
950 verbose -log "Unexpected linker warning or error"
956 if { $failed == 0 } {
957 foreach actionlist $actions {
958 set action [lindex $actionlist 0]
959 set progopts [lindex $actionlist 1]
961 # There are actions where we run regexp_diff on the
962 # output, and there are other actions (presumably).
963 # Handling of the former look the same.
967 { set dump_prog $objdump }
969 { set dump_prog $nm }
971 { set dump_prog $READELF }
978 perror "Unrecognized action $action"
984 if { $dump_prog != "" } {
985 set dumpfile [lindex $actionlist 2]
986 set binary $dump_prog
988 # Ensure consistent sorting of symbols
989 if {[info exists env(LC_ALL)]} {
990 set old_lc_all $env(LC_ALL)
993 set cmd "$binary $progopts $binfile > dump.out"
995 catch "exec $cmd" comp_output
996 if {[info exists old_lc_all]} {
997 set env(LC_ALL) $old_lc_all
1001 set comp_output [prune_warnings $comp_output]
1003 if ![string match "" $comp_output] then {
1004 send_log "$comp_output\n"
1009 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1010 verbose "output is [file_contents "dump.out"]" 2
1020 } elseif { $is_unresolved } {
1021 unresolved $testname
1028 # Returns true if --gc-sections is supported on the target.
1030 proc check_gc_sections_available { } {
1031 global gc_sections_available_saved
1034 if {![info exists gc_sections_available_saved]} {
1035 # Some targets don't support gc-sections despite whatever's
1036 # advertised by ld's options.
1037 if { [istarget alpha-*-*]
1038 || [istarget d30v-*-*]
1039 || [istarget dlx-*-*]
1040 || [istarget hppa*64-*-*]
1041 || [istarget ia64-*-*]
1042 || [istarget mep-*-*]
1043 || [istarget mn10200-*-*]
1044 || [istarget pj*-*-*]
1045 || [istarget pru*-*-*]
1046 || [istarget s12z-*-*]
1047 || [istarget xgate-*-*] } {
1048 set gc_sections_available_saved 0
1052 # elf2flt uses -q (--emit-relocs), which is incompatible with
1054 if { [board_info target exists ldflags]
1055 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1056 set gc_sections_available_saved 0
1060 # Check if the ld used by gcc supports --gc-sections.
1061 # FIXME: this test is useless since ld --help always says
1062 # --gc-sections is available
1063 set ld_output [remote_exec host $ld "--help"]
1064 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1065 set gc_sections_available_saved 1
1067 set gc_sections_available_saved 0
1070 return $gc_sections_available_saved
1073 # Return true if target uses genelf.em (assuming it is ELF).
1074 proc is_generic_elf { } {
1075 if { [istarget "d30v-*-*"]
1076 || [istarget "dlx-*-*"]
1077 || [istarget "fr30-*-*"]
1078 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1079 || [istarget "ft32-*-*"]
1080 || [istarget "iq2000-*-*"]
1081 || [istarget "mn10200-*-*"]
1082 || [istarget "moxie-*-*"]
1083 || [istarget "msp430-*-*"]
1084 || [istarget "mt-*-*"]
1085 || [istarget "pj*-*-*"]
1086 || [istarget "xgate-*-*"] } {
1092 proc is_underscore_target { } {
1093 global is_underscore_target_saved
1094 global target_triplet
1097 if { ![info exists is_underscore_target_saved] } {
1098 set cmd "targ=$target_triplet . $srcdir/../../bfd/config.bfd &&"
1099 append cmd { echo "$targ_underscore"}
1101 set status [catch {exec sh -c $cmd} result]
1102 if { $status == 0 && [string match "yes" $result] } {
1103 set is_underscore_target_saved 1
1105 set is_underscore_target_saved 0
1108 return $is_underscore_target_saved
1111 # Returns true if the target ld supports the plugin API.
1112 proc check_plugin_api_available { } {
1113 global plugin_api_available_saved
1115 if {![info exists plugin_api_available_saved]} {
1116 # Check if the ld used by gcc supports --plugin.
1117 set ld_output [remote_exec host $ld "--help"]
1118 if { [ string first "-plugin PLUGIN" $ld_output ] >= 0 } {
1119 set plugin_api_available_saved 1
1121 set plugin_api_available_saved 0
1124 return $plugin_api_available_saved
1127 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1128 # returns true if the target ld supports sysroot.
1129 proc check_sysroot_available { } {
1130 global ld_sysroot_available_saved ld ld_sysroot
1131 if {![info exists ld_sysroot_available_saved]} {
1132 # Check if ld supports --sysroot *other* than empty.
1133 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1134 if { $ld_sysroot == "" } {
1135 set ld_sysroot_available_saved 0
1137 set ld_sysroot_available_saved 1
1140 return $ld_sysroot_available_saved
1143 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1144 proc check_gcc_plugin_enabled { } {
1147 if {![info exists CC]} {
1153 set state [remote_exec host $CC -v]
1154 if { [lindex $state 0] != 0 } {
1157 for { set i 1 } { $i < [llength $state] } { incr i } {
1158 set v [lindex $state $i]
1159 if { [ string match "*--disable-plugin*" $v ] } {
1160 verbose "plugin is disabled by $v"
1168 # Returns true if the target compiler supports LTO
1169 proc check_lto_available { } {
1170 global lto_available_saved
1173 if {![info exists lto_available_saved]} {
1174 if { ![check_gcc_plugin_enabled] } {
1175 set lto_available_saved 0
1178 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1179 # -ffat-lto-objects, we always run LTO tests on Linux with
1181 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1182 set lto_available_saved 1
1185 # Check if gcc supports -flto -fuse-linker-plugin
1187 if [board_info [target_info name] exists cflags] {
1188 append flags " [board_info [target_info name] cflags]"
1190 if [board_info [target_info name] exists ldflags] {
1191 append flags " [board_info [target_info name] ldflags]"
1194 set basename "tmpdir/lto[pid]"
1195 set src ${basename}.c
1196 set output ${basename}.out
1197 set f [open $src "w"]
1198 puts $f "int main() { return 0; }"
1200 if [is_remote host] {
1201 set src [remote_download host $src]
1203 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1204 remote_file host delete $src
1205 remote_file host delete $output
1208 return $lto_available_saved
1211 # Returns true if the target compiler supports LTO -ffat-lto-objects
1212 proc check_lto_fat_available { } {
1213 global lto_fat_available_saved
1216 if {![info exists lto_fat_available_saved]} {
1217 if { ![check_gcc_plugin_enabled] } {
1218 set lto_fat_available_saved 0
1221 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1222 # -ffat-lto-objects, we always run LTO tests on Linux with
1224 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1225 set lto_fat_available_saved 1
1228 # Check if gcc supports -flto -fuse-linker-plugin
1230 if [board_info [target_info name] exists cflags] {
1231 append flags " [board_info [target_info name] cflags]"
1233 if [board_info [target_info name] exists ldflags] {
1234 append flags " [board_info [target_info name] ldflags]"
1237 set basename "tmpdir/lto[pid]"
1238 set src ${basename}.c
1239 set output ${basename}.out
1240 set f [open $src "w"]
1241 puts $f "int main() { return 0; }"
1243 if [is_remote host] {
1244 set src [remote_download host $src]
1246 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
1247 remote_file host delete $src
1248 remote_file host delete $output
1251 return $lto_fat_available_saved
1254 # Returns true if the target compiler supports LTO and -shared
1255 proc check_lto_shared_available { } {
1256 global lto_shared_available_saved
1259 if {![info exists lto_shared_available_saved]} {
1260 if { ![check_gcc_plugin_enabled] } {
1261 set lto_shared_available_saved 0
1264 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1265 # -ffat-lto-objects, we always run LTO tests on Linux with
1267 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1268 set lto_shared_available_saved 1
1271 # Check if gcc supports -flto -fuse-linker-plugin -shared
1273 if [board_info [target_info name] exists cflags] {
1274 append flags " [board_info [target_info name] cflags]"
1276 if [board_info [target_info name] exists ldflags] {
1277 append flags " [board_info [target_info name] ldflags]"
1280 set basename "tmpdir/lto_shared[pid]"
1281 set src ${basename}.c
1282 set output ${basename}.so
1283 set f [open $src "w"]
1286 if [is_remote host] {
1287 set src [remote_download host $src]
1289 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
1290 remote_file host delete $src
1291 remote_file host delete $output
1294 return $lto_shared_available_saved
1297 # Check if the assembler supports CFI statements.
1299 proc check_as_cfi { } {
1300 global check_as_cfi_result
1302 if [info exists check_as_cfi_result] {
1303 return $check_as_cfi_result
1305 set as_file "tmpdir/check_as_cfi.s"
1306 set as_fh [open $as_file w 0666]
1307 puts $as_fh "# Generated file. DO NOT EDIT"
1308 puts $as_fh "\t.cfi_startproc"
1309 puts $as_fh "\t.cfi_endproc"
1311 remote_download host $as_file
1312 verbose -log "Checking CFI support:"
1313 rename "perror" "check_as_cfi_perror"
1314 proc perror { args } { }
1315 set success [ld_assemble $as $as_file "/dev/null"]
1317 rename "check_as_cfi_perror" "perror"
1318 #remote_file host delete $as_file
1319 set check_as_cfi_result $success
1323 # Returns true if IFUNC works.
1325 proc check_ifunc_available { } {
1326 global ifunc_available_saved
1329 if {![info exists ifunc_available_saved]} {
1330 if { [which $CC] == 0 } {
1331 set ifunc_available_saved 0
1334 # Check if gcc supports -flto -fuse-linker-plugin
1336 if [board_info [target_info name] exists cflags] {
1337 append flags " [board_info [target_info name] cflags]"
1339 if [board_info [target_info name] exists ldflags] {
1340 append flags " [board_info [target_info name] ldflags]"
1343 set basename "tmpdir/ifunc[pid]"
1344 set src ${basename}.c
1345 set output ${basename}.out
1346 set f [open $src "w"]
1347 puts $f "extern int library_func2 (void);"
1348 puts $f "int main (void)"
1350 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1351 puts $f " return 0; "
1353 puts $f "static int library_func1 (void) {return 2; }"
1354 puts $f "void *foo (void) __asm__ (\"library_func2\");"
1355 puts $f "void *foo (void) { return library_func1; }"
1356 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
1358 if [is_remote host] {
1359 set src [remote_download host $src]
1361 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1362 if { [isnative] && $ifunc_available_saved == 1 } {
1363 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
1365 remote_file host delete $src
1366 remote_file host delete $output
1369 return $ifunc_available_saved
1372 # Returns true if ifunc attribute works.
1374 proc check_ifunc_attribute_available { } {
1375 global ifunc_attribute_available_saved
1378 if {![info exists ifunc_attribute_available_saved]} {
1379 if { [which $CC] == 0 } {
1380 set ifunc_attribute_available_saved 0
1383 # Check if gcc supports -flto -fuse-linker-plugin
1385 if [board_info [target_info name] exists cflags] {
1386 append flags " [board_info [target_info name] cflags]"
1388 if [board_info [target_info name] exists ldflags] {
1389 append flags " [board_info [target_info name] ldflags]"
1392 set basename "tmpdir/ifunc[pid]"
1393 set src ${basename}.c
1394 set output ${basename}.out
1395 set f [open $src "w"]
1396 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
1397 puts $f "int main (void)"
1399 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1400 puts $f " return 0; "
1402 puts $f "static int library_func1 (void) {return 2; }"
1403 puts $f "void *foo (void) { return library_func1; }"
1405 if [is_remote host] {
1406 set src [remote_download host $src]
1408 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1409 if { [isnative] && $ifunc_attribute_available_saved == 1 } {
1410 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
1412 remote_file host delete $src
1413 remote_file host delete $output
1416 return $ifunc_attribute_available_saved
1419 # Return true if libdl is supported.
1421 proc check_libdl_available { } {
1422 global libdl_available_saved
1425 if {![info exists libdl_available_saved]} {
1426 if { [which $CC] == 0 } {
1427 set libdl_available_saved 0
1431 set basename "tmpdir/dl_avail_test[pid]"
1432 set src ${basename}.c
1433 set output ${basename}.out
1434 set f [open $src "w"]
1436 puts $f "#include <dlfcn.h>"
1437 puts $f "int main (void)"
1439 puts $f " dlopen (\"dummy.so\", RTLD_NOW);"
1440 puts $f " return 0; "
1443 if [is_remote host] {
1444 set src [remote_download host $src]
1446 set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
1447 remote_file host delete $src
1448 remote_file host delete $output
1451 return $libdl_available_saved
1454 # Returns true if GNU2 TLS works.
1456 proc check_gnu2_tls_available { } {
1457 global gnu2_tls_available_saved
1461 if {![info exists gnu2_tls_available_saved]} {
1462 if { [which $CC] == 0 || "$GNU2_CFLAGS" == "" } {
1463 set gnu2_tls_available_saved 0
1466 # Check if GNU2 TLS works.
1467 set flags "$GNU2_CFLAGS"
1468 if [board_info [target_info name] exists cflags] {
1469 append flags " [board_info [target_info name] cflags]"
1471 if [board_info [target_info name] exists ldflags] {
1472 append flags " [board_info [target_info name] ldflags]"
1475 set basename "tmpdir/gnu2_tls[pid]"
1476 set src1 ${basename}1.c
1477 set output1 ${basename}.so
1478 set f [open $src1 "w"]
1479 puts $f "extern __thread int zzz;"
1480 puts $f "int foo (void)"
1482 puts $f " return zzz;"
1485 if [is_remote host] {
1486 set src1 [remote_download host $src1]
1488 set src2 ${basename}2.c
1489 set output2 ${basename}.exe
1490 set f [open $src2 "w"]
1491 puts $f "__thread int zzz = 20;"
1492 puts $f "extern int foo (void);"
1493 puts $f "int main (void)"
1495 puts $f " if (foo () != 20) __builtin_abort ();"
1496 puts $f " return 0; "
1499 if [is_remote host] {
1500 set src2 [remote_download host $src2]
1502 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "-fPIC -shared $flags $src1 -o $output1"]
1503 if { $gnu2_tls_available_saved == 1 } {
1504 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "$flags $src2 $output1 -o $output2"]
1505 if { $gnu2_tls_available_saved == 1 } {
1506 set gnu2_tls_available_saved [run_host_cmd_yesno "$output2" ""]
1509 remote_file host delete $src1
1510 remote_file host delete $output1
1511 remote_file host delete $src2
1512 remote_file host delete $output2
1513 file delete $src1 $src2
1515 return $gnu2_tls_available_saved