1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2016 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 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 } {
86 if { ![is_remote host] && [which "$prog"] == 0 } then {
87 perror "$prog does not exist"
91 # If we are compiling with gcc, we want to add gcc_B_opt and
92 # ld_L_opt to flags. However, if $prog already has -B options,
93 # which might be the case when running gcc out of a build
94 # directory, we want our -B options to come first.
96 set gccparm [string first " " $gccexe]
98 if { $gccparm > 0 } then {
99 set gccflags [string range $gccexe $gccparm end]
100 set gccexe [string range $gccexe 0 $gccparm]
103 set gccexe [string replace $gccexe 0 [string last "/" $gccexe] ""]
104 if {[string match "*cc*" $gccexe] || [string match "*++*" $gccexe]} then {
105 set gccflags "$gcc_B_opt $gccflags $ld_L_opt"
108 verbose -log "$prog $gccflags $command"
109 set status [remote_exec host [concat sh -c [list "$prog $gccflags $command 2>&1"]] "" "/dev/null" "ld.tmp"]
110 remote_upload host "ld.tmp"
111 set link_output [file_contents "ld.tmp"]
112 regsub "\n$" $link_output "" link_output
113 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
114 append link_output "child process exited abnormally"
116 remote_file build delete ld.tmp
117 remote_file host delete ld.tmp
119 if [string match "" $link_output] then {
123 verbose -log "$link_output"
124 return "$link_output"
127 proc run_host_cmd_yesno { prog command } {
129 global errcnt warncnt
131 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
132 # Ignore error and warning.
135 if [string match "" $exec_output] then {
141 # Link an object using relocation.
143 proc default_ld_relocate { ld target objects } {
146 remote_file host delete $target
147 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
150 # Check to see if ld is being invoked with a non-endian output format
152 proc is_endian_output_format { object_flags } {
154 if {[string match "*-oformat binary*" $object_flags] || \
155 [string match "*-oformat ieee*" $object_flags] || \
156 [string match "*-oformat ihex*" $object_flags] || \
157 [string match "*-oformat netbsd-core*" $object_flags] || \
158 [string match "*-oformat srec*" $object_flags] || \
159 [string match "*-oformat tekhex*" $object_flags] || \
160 [string match "*-oformat trad-core*" $object_flags] } then {
167 # Look for big-endian or little-endian switches in the multlib
168 # options and translate these into a -EB or -EL switch. Note
169 # we cannot rely upon proc process_multilib_options to do this
170 # for us because for some targets the compiler does not support
171 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
172 # the site.exp file will include the switch "-mbig-endian"
173 # (rather than "big-endian") which is not detected by proc
174 # process_multilib_options.
176 proc big_or_little_endian {} {
178 if [board_info [target_info name] exists multilib_flags] {
179 set tmp_flags " [board_info [target_info name] multilib_flags]"
181 foreach x $tmp_flags {
183 {*big*endian eb EB -eb -EB -mb -meb} {
187 {*little*endian el EL -el -EL -ml -mel} {
199 # Link a program using ld.
201 proc default_ld_link { ld target objects } {
212 if { [ string match "* -pie *" $objects ] } {
213 set objs "$HOSTING_SCRT0 $objects"
214 set libs "$LIBS $HOSTING_SLIBS"
216 set objs "$HOSTING_CRT0 $objects"
217 set libs "$LIBS $HOSTING_LIBS"
220 if [is_endian_output_format $objects] then {
221 set flags [big_or_little_endian]
226 remote_file host delete $target
228 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU $flags -o $target $objs $libs"]
231 # Link a program using ld, without including any libraries.
233 proc default_ld_simple_link { ld target objects } {
238 if [is_endian_output_format $objects] then {
239 set flags [big_or_little_endian]
242 remote_file host delete $target
243 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
244 set exec_output [prune_warnings $exec_output]
246 # We don't care if we get a warning about a non-existent start
247 # symbol, since the default linker script might use ENTRY.
248 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
250 return [string match "" $exec_output]
253 # Compile an object using cc.
255 proc default_ld_compile { cc source object } {
264 if {[llength $cc_prog] > 1} then {
265 set cc_prog [lindex $cc_prog 0]
267 if {![is_remote host] && [which $cc_prog] == 0} then {
268 perror "$cc_prog does not exist"
272 remote_file build delete "$object"
273 remote_file host delete "$object"
275 set flags "$gcc_B_opt -I$srcdir/$subdir"
277 # If we are compiling with gcc, we want to add gcc_B_opt to flags.
278 # However, if $prog already has -B options, which might be the
279 # case when running gcc out of a build directory, we want our -B
280 # options to come first.
282 set ccparm [string first " " $cc]
284 if { $ccparm > 0 } then {
285 set ccflags [string range $cc $ccparm end]
286 set ccexe [string range $cc 0 $ccparm]
290 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
291 if {[string match "*++*" $ccexe]} {
292 append flags " $CXXFLAGS"
294 append flags " $CFLAGS"
297 if [board_info [target_info name] exists cflags] {
298 append flags " [board_info [target_info name] cflags]"
301 if [board_info [target_info name] exists multilib_flags] {
302 append flags " [board_info [target_info name] multilib_flags]"
305 set cmd "$cc $flags $ccflags -c $source -o $object"
308 set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
309 remote_upload host "ld.tmp"
310 set exec_output [file_contents "ld.tmp"]
311 remote_file build delete "ld.tmp"
312 remote_file host delete "ld.tmp"
313 set exec_output [prune_warnings $exec_output]
314 if [string match "" $exec_output] then {
315 if {![file exists $object]} then {
316 regexp ".*/(\[^/\]*)$" $source all dobj
317 regsub "\\.c" $dobj ".o" realobj
318 verbose "looking for $realobj"
319 if {[remote_file host exists $realobj]} then {
320 verbose -log "mv $realobj $object"
321 remote_upload "$realobj" "$object"
323 perror "$object not found after compilation"
329 verbose -log "$exec_output"
330 perror "$source: compilation failed"
337 proc default_ld_assemble { as in_flags source object } {
343 if ![info exists ASFLAGS] { set ASFLAGS "" }
345 set flags "[big_or_little_endian] -I$srcdir/$subdir"
346 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
347 set exec_output [prune_warnings $exec_output]
348 if [string match "" $exec_output] then {
351 perror "$source: assembly failed"
356 # Run nm on a file, putting the result in the array nm_output.
358 proc default_ld_nm { nm nmflags object } {
363 if {[info exists nm_output]} {
367 if ![info exists NMFLAGS] { set NMFLAGS "" }
369 # Ensure consistent sorting of symbols
370 if {[info exists env(LC_ALL)]} {
371 set old_lc_all $env(LC_ALL)
375 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
377 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
378 if {[info exists old_lc_all]} {
379 set env(LC_ALL) $old_lc_all
383 remote_upload host "ld.stderr"
384 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
385 set exec_output [prune_warnings [file_contents "ld.stderr"]]
386 remote_file host delete "ld.stderr"
387 remote_file build delete "ld.stderr"
388 if [string match "" $exec_output] then {
389 set file [open tmpdir/nm.out r]
390 while { [gets $file line] != -1 } {
392 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
393 set name [string trimleft $name "_"]
394 verbose "Setting nm_output($name) to 0x$value" 2
395 set nm_output($name) 0x$value
401 verbose -log "$exec_output"
402 perror "$object: nm failed"
407 # Define various symbols needed when not linking against all
409 proc ld_simple_link_defsyms {} {
411 set flags "--defsym __stack_chk_fail=0"
413 # ARM targets call __gccmain
414 if {[istarget arm*-*-*]} {
415 append flags " --defsym __gccmain=0"
418 # Windows targets need __main, some prefixed with underscore.
419 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
420 append flags " --defsym __main=0 --defsym ___main=0"
423 # PowerPC EABI code calls __eabi.
424 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
425 append flags " --defsym __eabi=0"
428 # mn10200 code calls __truncsipsi2_d0_d2.
429 if {[istarget mn10200*-*-*]} then {
430 append flags " --defsym __truncsipsi2_d0_d2=0"
433 # m6811/m6812 code has references to soft registers.
434 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
435 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
436 append flags " --defsym _.d3=0 --defsym _.d4=0"
437 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
440 # Some OpenBSD targets have ProPolice and reference __guard and
441 # __stack_smash_handler.
442 if [istarget *-*-openbsd*] {
443 append flags " --defsym __guard=0"
444 append flags " --defsym __stack_smash_handler=0"
450 # run_dump_test FILE (optional:) EXTRA_OPTIONS
451 # Copied from gas testsuite, tweaked and further extended.
453 # Assemble a .s file, then run some utility on it and check the output.
455 # There should be an assembly language file named FILE.s in the test
456 # suite directory, and a pattern file called FILE.d. `run_dump_test'
457 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
458 # `nm' on the .o file to produce textual output, and then analyze that
459 # with regexps. The FILE.d file specifies what program to run, and
460 # what to expect in its output.
462 # The FILE.d file begins with zero or more option lines, which specify
463 # flags to pass to the assembler, the program to run to dump the
464 # assembler's output, and the options it wants. The option lines have
469 # OPTION is the name of some option, like "name" or "objdump", and
470 # VALUE is OPTION's value. The valid options are described below.
471 # Whitespace is ignored everywhere, except within VALUE. The option
472 # list ends with the first line that doesn't match the above syntax
473 # (hmm, not great for error detection).
475 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
476 # two-element lists. The first element of each is an option name, and
477 # the second additional arguments to be added on to the end of the
478 # option list as given in FILE.d. (If omitted, no additional options
481 # The interesting options are:
484 # The name of this test, passed to DejaGNU's `pass' and `fail'
485 # commands. If omitted, this defaults to FILE, the root of the
486 # .s and .d files' names.
489 # When assembling, pass FLAGS to the assembler.
490 # If assembling several files, you can pass different assembler
491 # options in the "source" directives. See below.
494 # Link assembled files using FLAGS, in the order of the "source"
495 # directives, when using multiple files.
497 # ld_after_inputfiles: FLAGS
498 # Similar to "ld", but put after all input files.
500 # objcopy_objects: FLAGS
501 # Run objcopy with the specified flags after assembling any source
502 # that has the special marker RUN_OBJCOPY in the source specific
505 # objcopy_linked_file: FLAGS
506 # Run objcopy on the linked file with the specified flags.
507 # This lets you transform the linked file using objcopy, before the
508 # result is analyzed by an analyzer program specified below (which
509 # may in turn *also* be objcopy).
512 # The name of the program to run to analyze the .o file produced
513 # by the assembler or the linker output. This can be omitted;
514 # run_dump_test will guess which program to run by seeing which of
515 # the flags options below is present.
521 # Use the specified program to analyze the assembler or linker
522 # output file, and pass it FLAGS, in addition to the output name.
523 # Note that they are run with LC_ALL=C in the environment to give
524 # consistent sorting of symbols.
526 # source: SOURCE [FLAGS]
527 # Assemble the file SOURCE.s using the flags in the "as" directive
528 # and the (optional) FLAGS. If omitted, the source defaults to
530 # This is useful if several .d files want to share a .s file.
531 # More than one "source" directive can be given, which is useful
532 # when testing linking.
535 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
536 # is useful if several .d files differ by options only. Options are
537 # always read from FILE.d.
540 # The test is expected to fail on TARGET. This may occur more than
544 # Only run the test for TARGET. This may occur more than once; the
545 # target being tested must match at least one. You may provide target
546 # name "cfi" for any target supporting the CFI statements.
549 # Do not run the test for TARGET. This may occur more than once;
550 # the target being tested must not match any of them.
553 # An error with message matching REGEX must be emitted for the test
554 # to pass. The PROG, readelf, objdump, nm and objcopy options have
555 # no meaning and need not be supplied if this is present. Multiple
556 # "error" directives append to the expected linker error message.
559 # Means the same as 'error', except the regular expression lines
560 # are contains in FILE.
563 # Expect a linker warning matching REGEX. It is an error to issue
564 # both "error" and "warning". Multiple "warning" directives
565 # append to the expected linker warning message.
567 # warning_output: FILE
568 # Means the same as 'warning', except the regular expression
569 # lines are contains in FILE.
572 # Adding this option will cause the linker to generate a linker
573 # map file, using the -Map=MAPFILE command line option. If
574 # there is no -Map=MAPFILE in the 'ld: FLAGS' then one will be
575 # added to the linker command line. The contents of the
576 # generated MAPFILE are then compared against the regexp lines
577 # in FILE using `regexp_diff' (see below for details).
579 # Each option may occur at most once unless otherwise mentioned.
581 # After the option lines come regexp lines. `run_dump_test' calls
582 # `regexp_diff' to compare the output of the dumping tool against the
583 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
584 # see further comments there.
586 proc run_dump_test { name {extra_options {}} } {
588 global OBJDUMP NM AS OBJCOPY READELF LD
589 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
590 global host_triplet runtests
593 if [string match "*/*" $name] {
595 set name [file tail $name]
597 set file "$srcdir/$subdir/$name"
600 if ![runtest_file_p $runtests $name] then {
604 set opt_array [slurp_options "${file}.d"]
605 if { $opt_array == -1 } {
606 perror "error reading options from $file.d"
607 unresolved $subdir/$name
610 set dumpfile tmpdir/dump.out
615 set opts(ld_after_inputfiles) {}
618 set opts(notarget) {}
629 set opts(error_output) {}
630 set opts(warning_output) {}
631 set opts(objcopy_linked_file) {}
632 set opts(objcopy_objects) {}
635 foreach i $opt_array {
636 set opt_name [lindex $i 0]
637 set opt_val [lindex $i 1]
638 if ![info exists opts($opt_name)] {
639 perror "unknown option $opt_name in file $file.d"
640 unresolved $subdir/$name
644 switch -- $opt_name {
651 # Move any source-specific as-flags to a separate list to
652 # simplify processing.
653 if { [llength $opt_val] > 1 } {
654 lappend asflags [lrange $opt_val 1 end]
655 set opt_val [lindex $opt_val 0]
661 if [string length $opts($opt_name)] {
662 perror "option $opt_name multiply set in $file.d"
663 unresolved $subdir/$name
667 # A single "# ld:" with no options should do the right thing.
668 if { $opt_name == "ld" } {
671 # Likewise objcopy_linked_file.
672 if { $opt_name == "objcopy_linked_file" } {
677 if { $opt_name == "as" || $opt_name == "ld" } {
678 set opt_val [subst $opt_val]
681 # Append differently whether it's a message (without space) or
682 # an option or list (with space).
683 switch -- $opt_name {
686 append opts($opt_name) $opt_val
689 set opts($opt_name) [concat $opts($opt_name) $opt_val]
694 foreach i $extra_options {
695 set opt_name [lindex $i 0]
696 set opt_val [lindex $i 1]
697 if ![info exists opts($opt_name)] {
698 perror "unknown option $opt_name given in extra_opts"
699 unresolved $subdir/$name
702 # Add extra option to end of existing option, adding space
704 if { ![regexp "warning|error" $opt_name]
705 && [string length $opts($opt_name)] } {
706 append opts($opt_name) " "
708 append opts($opt_name) $opt_val
711 foreach opt { as ld } {
712 regsub {\[big_or_little_endian\]} $opts($opt) \
713 [big_or_little_endian] opts($opt)
716 # Decide early whether we should run the test for this target.
717 if { [llength $opts(target)] > 0 } {
719 foreach targ $opts(target) {
720 if [istarget $targ] {
725 if { $targmatch == 0 } {
729 foreach targ $opts(notarget) {
730 if [istarget $targ] {
736 # It's meaningless to require an output-testing method when we
738 if { $opts(error) == "" && $opts(error_output) == "" } {
739 if {$opts(PROG) != ""} {
740 switch -- $opts(PROG) {
741 objdump { set program objdump }
742 nm { set program nm }
743 objcopy { set program objcopy }
744 readelf { set program readelf }
746 { perror "unrecognized program option $opts(PROG) in $file.d"
747 unresolved $subdir/$name
751 # Guess which program to run, by seeing which option was specified.
752 foreach p {objdump objcopy nm readelf} {
753 if {$opts($p) != ""} {
754 if {$program != ""} {
755 perror "ambiguous dump program in $file.d"
756 unresolved $subdir/$name
764 if { $program == "" \
765 && $opts(map) == "" \
766 && $opts(warning) == "" \
767 && $opts(warning_output) == "" \
768 && $opts(error) == "" \
769 && $opts(error_output) == "" } {
770 perror "dump program unspecified in $file.d"
771 unresolved $subdir/$name
776 if { $opts(name) == "" } {
777 set testname "$subdir/$name"
779 set testname $opts(name)
782 if { $opts(source) == "" } {
783 set sourcefiles [list ${file}.s]
784 set asflags [list ""]
787 foreach sf $opts(source) {
788 if { [string match "/*" $sf] } {
789 lappend sourcefiles "$sf"
791 lappend sourcefiles "$srcdir/$subdir/$sf"
796 if { $opts(dump) == "" } {
799 set dfile $srcdir/$subdir/$opts(dump)
802 # Time to setup xfailures.
803 foreach targ $opts(xfail) {
807 # Assemble each file.
809 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
810 set sourcefile [lindex $sourcefiles $i]
811 set sourceasflags [lindex $asflags $i]
812 set run_objcopy_objects 0
814 if { [string match "*RUN_OBJCOPY*" $sourceasflags] } {
815 set run_objcopy_objects 1
817 regsub "RUN_OBJCOPY" $sourceasflags "" sourceasflags
819 set objfile "tmpdir/dump$i.o"
820 catch "exec rm -f $objfile" exec_output
821 lappend objfiles $objfile
822 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
825 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
826 remote_upload host "ld.tmp"
827 set comp_output [prune_warnings [file_contents "ld.tmp"]]
828 remote_file host delete "ld.tmp"
829 remote_file build delete "ld.tmp"
831 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
832 send_log "$comp_output\n"
833 verbose "$comp_output" 3
835 set exitstat "succeeded"
836 if { $cmdret != 0 } { set exitstat "failed" }
837 verbose -log "$exitstat with: <$comp_output>"
842 if { $run_objcopy_objects } {
843 set cmd "$OBJCOPY $opts(objcopy_objects) $objfile"
846 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] \
847 "" "/dev/null" "objcopy.tmp"]
848 remote_upload host "objcopy.tmp"
849 set comp_output [prune_warnings [file_contents "objcopy.tmp"]]
850 remote_file host delete "objcopy.tmp"
851 remote_file build delete "objcopy.tmp"
853 if { [lindex $cmdret 0] != 0 \
854 || ![string match "" $comp_output] } {
855 send_log "$comp_output\n"
856 verbose "$comp_output" 3
858 set exitstat "succeeded"
859 if { $cmdret != 0 } { set exitstat "failed" }
860 verbose -log "$exitstat with: <$comp_output>"
867 if { (($opts(warning) != "") && ($opts(error) != "")) \
868 || (($opts(warning) != "") && ($opts(error_output) != "")) \
869 || (($opts(warning) != "") && ($opts(warning_output) != "")) \
870 || (($opts(error) != "") && ($opts(warning_output) != "")) \
871 || (($opts(error) != "") && ($opts(error_output) != "")) \
872 || (($opts(warning_output) != "") && ($opts(error_output) != "")) } {
873 perror "$testname: bad mix of warning, error, warning_output, and error_output test-directives"
878 set check_ld(source) ""
879 set check_ld(terminal) 0
880 if { $opts(error) != "" \
881 || $opts(warning) != "" \
882 || $opts(error_output) != "" \
883 || $opts(warning_output) != "" } {
885 if { $opts(error) != "" || $opts(error_output) != "" } {
886 set check_ld(terminal) 1
888 set check_ld(terminal) 0
891 if { $opts(error) != "" || $opts(warning) != "" } {
892 set check_ld(source) "regex"
893 if { $opts(error) != "" } {
894 set check_ld(regex) $opts(error)
896 set check_ld(regex) $opts(warning)
899 set check_ld(source) "file"
900 if { $opts(error_output) != "" } {
901 set check_ld(file) $opts(error_output)
903 set check_ld(file) $opts(warning_output)
908 # Perhaps link the file(s).
910 set objfile "tmpdir/dump"
911 catch "exec rm -f $objfile" exec_output
913 # Add -L$srcdir/$subdir so that the linker command can use
914 # linker scripts in the source directory.
915 set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
916 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
918 # If needed then check for, or add a -Map option.
920 if { $opts(map) != "" } then {
921 if { [regexp -- "-Map=(\[^ \]+)" $cmd all mapfile] } then {
922 # Found existing mapfile option
923 verbose -log "Existing mapfile '$mapfile' found"
926 set mapfile "tmpdir/dump.map"
927 verbose -log "Adding mapfile '$mapfile'"
928 set cmd "$cmd -Map=$mapfile"
933 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
934 remote_upload host "ld.tmp"
935 set comp_output [file_contents "ld.tmp"]
936 remote_file host delete "ld.tmp"
937 remote_file build delete "ld.tmp"
938 set cmdret [lindex $cmdret 0]
940 if { $cmdret == 0 && $run_objcopy } {
942 set objfile "tmpdir/dump1"
943 remote_file host delete $objfile
945 # Note that we don't use OBJCOPYFLAGS here; any flags must be
946 # explicitly specified.
947 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
950 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
951 remote_upload host "ld.tmp"
952 append comp_output [file_contents "ld.tmp"]
953 remote_file host delete "ld.tmp"
954 remote_file build delete "ld.tmp"
955 set cmdret [lindex $cmdret 0]
958 regsub "\n$" $comp_output "" comp_output
959 if { $cmdret != 0 || $comp_output != "" || $check_ld(source) != "" } then {
960 set exitstat "succeeded"
961 if { $cmdret != 0 } { set exitstat "failed" }
963 if { $check_ld(source) == "regexp" } {
964 verbose -log "$exitstat with: <$comp_output>, expected: <$check_ld(regex)>"
965 } elseif { $check_ld(source) == "file" } {
966 verbose -log "$exitstat with: <$comp_output>, expected in file $check_ld(file)"
967 set_file_contents "tmpdir/ld.messages" "$comp_output"
969 verbose -log "$exitstat with: <$comp_output>, no expected output"
971 send_log "$comp_output\n"
972 verbose "$comp_output" 3
974 if { (($check_ld(source) == "") == ($comp_output == "")) \
975 && (($cmdret == 0) == ($check_ld(terminal) == 0)) \
976 && ((($check_ld(source) == "regex") \
977 && ($check_ld(regex) == "") == ($comp_output == "") \
978 && [regexp $check_ld(regex) $comp_output]) \
979 || (($check_ld(source) == "file") \
980 && (![regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"]))) } {
981 # We have the expected output from ld.
982 if { $check_ld(terminal) || $program == "" } {
992 if { $opts(map) != "" } then {
993 # Check the map file matches.
994 set map_pattern_file $srcdir/$subdir/$opts(map)
995 verbose -log "Compare '$mapfile' against '$map_pattern_file'"
996 if { [regexp_diff $mapfile $map_pattern_file] } then {
997 fail "$testname (map file check)"
999 pass "$testname (map file check)"
1002 if { $program == "" } then {
1007 set objfile "tmpdir/dump0.o"
1010 # We must not have expected failure if we get here.
1011 if { $opts(error) != "" } {
1016 set progopts1 $opts($program)
1017 eval set progopts \$[string toupper $program]FLAGS
1018 eval set binary \$[string toupper $program]
1020 if { ![is_remote host] && [which $binary] == 0 } {
1025 if { $progopts1 == "" } { set $progopts1 "-r" }
1026 verbose "running $binary $progopts $progopts1" 3
1028 # Objcopy, unlike the other two, won't send its output to stdout,
1029 # so we have to run it specially.
1030 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
1031 if { $program == "objcopy" } {
1032 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
1035 # Ensure consistent sorting of symbols
1036 if {[info exists env(LC_ALL)]} {
1037 set old_lc_all $env(LC_ALL)
1041 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
1042 set cmdret [lindex $cmdret 0]
1043 remote_upload host "ld.tmp"
1044 set comp_output [prune_warnings [file_contents "ld.tmp"]]
1045 remote_file host delete "ld.tmp"
1046 remote_file build delete "ld.tmp"
1047 if {[info exists old_lc_all]} {
1048 set env(LC_ALL) $old_lc_all
1052 if { $cmdret != 0 || $comp_output != "" } {
1053 send_log "exited abnormally with $cmdret, output:$comp_output\n"
1058 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
1059 if { [regexp_diff $dumpfile "${dfile}"] } then {
1061 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
1068 proc slurp_options { file } {
1069 # If options_regsub(foo) is set to {a b}, then the contents of a
1070 # "#foo:" line will have regsub -all applied to replace a with b.
1071 global options_regsub
1073 if [catch { set f [open $file r] } x] {
1074 #perror "couldn't open `$file': $x"
1079 # whitespace expression
1082 # whitespace is ignored anywhere except within the options list;
1083 # option names are alphabetic plus underscore only.
1084 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
1085 while { [gets $f line] != -1 } {
1086 set line [string trim $line]
1087 # Whitespace here is space-tab.
1088 if [regexp $pat $line xxx opt_name opt_val] {
1090 if [info exists options_regsub($opt_name)] {
1091 set subst $options_regsub($opt_name)
1092 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
1095 lappend opt_array [list $opt_name $opt_val]
1104 proc file_contents { filename } {
1105 set file [open $filename r]
1106 set contents [read $file]
1111 proc set_file_contents { filename contents } {
1112 set file [open $filename w]
1113 puts $file "$contents"
1117 # Create an archive using ar
1119 proc ar_simple_create { ar aropts target objects } {
1120 remote_file host delete $target
1122 set exec_output [run_host_cmd "$ar" "-rc $aropts $target $objects"]
1123 set exec_output [prune_warnings $exec_output]
1125 if [string match "" $exec_output] then {
1126 send_log "$exec_output\n"
1133 # List contains test-items with 3 items followed by 2 lists, one item and
1134 # one optional item:
1136 # 1:ld/ar leading options, placed before object files
1137 # 2:ld/ar trailing options, placed after object files
1138 # 3:assembler options
1139 # 4:filenames of assembler files
1140 # 5:list of actions, options and expected outputs.
1141 # 6:name of output file
1142 # 7:compiler flags (optional)
1144 # Actions: { command command-line-options file-containg-expected-output-regexps }
1146 # objdump: Apply objdump options on result.
1147 # nm: Apply nm options on result.
1148 # readelf: Apply readelf options on result.
1149 # ld: Don't apply anything on result. Compare output during linking with
1150 # the file containing regexps (which is the second arg, not the third).
1151 # Note that this *must* be the first action if it is to be used at all;
1152 # in all other cases, any output from the linker during linking is
1153 # treated as a sign of an error and FAILs the test.
1155 proc run_ld_link_tests { ldtests } {
1170 foreach testitem $ldtests {
1171 set testname [lindex $testitem 0]
1173 if ![runtest_file_p $runtests $testname] then {
1177 set ld_options [lindex $testitem 1]
1178 set ld_after [lindex $testitem 2]
1179 set as_options [lindex $testitem 3]
1180 set src_files [lindex $testitem 4]
1181 set actions [lindex $testitem 5]
1182 set binfile tmpdir/[lindex $testitem 6]
1183 set cflags [lindex $testitem 7]
1190 # verbose -log "Testname is $testname"
1191 # verbose -log "ld_options is $ld_options"
1192 # verbose -log "ld_after is $ld_after"
1193 # verbose -log "as_options is $as_options"
1194 # verbose -log "src_files is $src_files"
1195 # verbose -log "actions is $actions"
1196 # verbose -log "binfile is $binfile"
1198 # Assemble each file in the test.
1199 foreach src_file $src_files {
1200 set fileroot "[file rootname [file tail $src_file]]"
1201 set objfile "tmpdir/$fileroot.o"
1202 lappend objfiles $objfile
1204 if { [file extension $src_file] == ".c" } {
1205 set as_file "tmpdir/$fileroot.s"
1206 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1211 set as_file "$srcdir/$subdir/$src_file"
1213 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1219 # Catch assembler errors.
1220 if { $is_unresolved } {
1221 unresolved $testname
1225 if { $binfile eq "tmpdir/" } {
1227 } elseif { [regexp ".*\\.a$" $binfile] } {
1228 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
1231 } elseif { ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
1233 set ld_output "$exec_output"
1237 foreach actionlist $actions {
1238 set action [lindex $actionlist 0]
1239 set progopts [lindex $actionlist 1]
1241 # There are actions where we run regexp_diff on the
1242 # output, and there are other actions (presumably).
1243 # Handling of the former look the same.
1247 { set dump_prog $objdump }
1249 { set dump_prog $nm }
1251 { set dump_prog $READELF }
1253 { set dump_prog "ld" }
1256 perror "Unrecognized action $action"
1262 if { $action == "ld" } {
1263 set regexpfile $progopts
1264 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1265 set_file_contents "tmpdir/ld.messages" "$ld_output"
1266 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1267 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1268 verbose "output is $ld_output" 2
1273 } elseif { !$maybe_failed && $dump_prog != "" } {
1274 set dumpfile [lindex $actionlist 2]
1275 set binary $dump_prog
1277 # Ensure consistent sorting of symbols
1278 if {[info exists env(LC_ALL)]} {
1279 set old_lc_all $env(LC_ALL)
1282 set cmd "$binary $progopts $binfile"
1283 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1285 remote_upload host "ld.stderr"
1286 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1287 remote_file host delete "ld.stderr"
1288 remote_file build delete "ld.stderr"
1290 if {[info exists old_lc_all]} {
1291 set env(LC_ALL) $old_lc_all
1296 if ![string match "" $comp_output] then {
1297 send_log "$comp_output\n"
1302 remote_upload host "dump.out"
1304 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1305 verbose "output is [file_contents "dump.out"]" 2
1307 remote_file build delete "dump.out"
1308 remote_file host delete "dump.out"
1311 remote_file build delete "dump.out"
1312 remote_file host delete "dump.out"
1317 if { $is_unresolved } {
1318 unresolved $testname
1319 } elseif { $maybe_failed || $failed } {
1327 # This definition is taken from an unreleased version of DejaGnu. Once
1328 # that version gets released, and has been out in the world for a few
1329 # months at least, it may be safe to delete this copy.
1330 if ![string length [info proc prune_warnings]] {
1332 # prune_warnings -- delete various system verbosities from TEXT
1335 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1337 # Sites with particular verbose os's may wish to override this in site.exp.
1339 proc prune_warnings { text } {
1340 # This is from sun4's. Do it for all machines for now.
1341 # The "\\1" is to try to preserve a "\n" but only if necessary.
1342 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1344 # It might be tempting to get carried away and delete blank lines, etc.
1345 # Just delete *exactly* what we're ask to, and that's it.
1350 # targets_to_xfail is a list of target triplets to be xfailed.
1351 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1352 # and 3 optional items:
1355 # 2:assembler options
1356 # 3:filenames of source files
1357 # 4:name of output file
1359 # 6:compiler flags (optional)
1360 # 7:language (optional)
1361 # 8:linker warning (optional)
1363 proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
1376 foreach testitem $ldtests {
1377 foreach target $targets_to_xfail {
1380 set testname [lindex $testitem 0]
1381 set ld_options [lindex $testitem 1]
1382 set as_options [lindex $testitem 2]
1383 set src_files [lindex $testitem 3]
1384 set binfile tmpdir/[lindex $testitem 4]
1385 set expfile [lindex $testitem 5]
1386 set cflags [lindex $testitem 6]
1387 set lang [lindex $testitem 7]
1388 set warning [lindex $testitem 8]
1392 # verbose -log "Testname is $testname"
1393 # verbose -log "ld_options is $ld_options"
1394 # verbose -log "as_options is $as_options"
1395 # verbose -log "src_files is $src_files"
1396 # verbose -log "binfile is $binfile"
1398 # Assemble each file in the test.
1399 foreach src_file $src_files {
1400 set fileroot "[file rootname [file tail $src_file]]"
1401 set objfile "tmpdir/$fileroot.o"
1402 lappend objfiles $objfile
1404 # We ignore warnings since some compilers may generate
1405 # incorrect section attributes and the assembler will warn
1407 if { [ string match "c++" $lang ] } {
1408 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1410 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1414 # We have to use $CC to build PIE and shared library.
1415 if { [ string match "c" $lang ] } {
1416 set link_proc ld_simple_link
1418 } elseif { [ string match "c++" $lang ] } {
1419 set link_proc ld_simple_link
1421 } elseif { [ string match "-shared" $ld_options ] \
1422 || [ string match "-pie" $ld_options ] } {
1423 set link_proc ld_simple_link
1426 set link_proc ld_link
1430 if { $binfile eq "tmpdir/" } {
1434 } elseif ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1438 # Check if exec_output is expected.
1439 if { $warning != "" } then {
1440 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1441 if { [regexp $warning $exec_output] } then {
1448 if { $failed == 0 } {
1449 send_log "Running: $binfile > $binfile.out\n"
1450 verbose "Running: $binfile > $binfile.out"
1451 catch "exec $binfile > $binfile.out" exec_output
1453 if ![string match "" $exec_output] then {
1454 send_log "$exec_output\n"
1455 verbose "$exec_output" 1
1458 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1459 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1460 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1461 set exec_output [prune_warnings $exec_output]
1463 if ![string match "" $exec_output] then {
1464 send_log "$exec_output\n"
1465 verbose "$exec_output" 1
1471 if { $failed != 0 } {
1480 # List contains test-items with 3 items followed by 2 lists, one item and
1481 # one optional item:
1483 # 1:ld or ar options
1485 # 3:filenames of source files
1486 # 4:action and options.
1487 # 5:name of output file
1488 # 6:language (optional)
1489 # 7:linker warnings (optional)
1492 # objdump: Apply objdump options on result. Compare with regex (last arg).
1493 # nm: Apply nm options on result. Compare with regex (last arg).
1494 # readelf: Apply readelf options on result. Compare with regex (last arg).
1496 proc run_cc_link_tests { ldtests } {
1511 if [board_info [target_info name] exists cflags] {
1512 set board_cflags " [board_info [target_info name] cflags]"
1517 foreach testitem $ldtests {
1518 set testname [lindex $testitem 0]
1519 set ldflags [lindex $testitem 1]
1520 set cflags [lindex $testitem 2]
1521 set src_files [lindex $testitem 3]
1522 set actions [lindex $testitem 4]
1523 set binfile tmpdir/[lindex $testitem 5]
1524 set lang [lindex $testitem 6]
1525 set warnings [lindex $testitem 7]
1530 #verbose -log "testname is $testname"
1531 #verbose -log "ldflags is $ldflags"
1532 #verbose -log "cflags is $cflags"
1533 #verbose -log "src_files is $src_files"
1534 #verbose -log "actions is $actions"
1535 #verbose -log "binfile is $binfile"
1536 #verbose -log "lang is $lang"
1537 #verbose -log "warnings is $warnings"
1539 # Compile each file in the test.
1540 foreach src_file $src_files {
1541 set fileroot "[file rootname [file tail $src_file]]"
1542 set objfile "tmpdir/$fileroot.o"
1543 lappend objfiles $objfile
1545 # We ignore warnings since some compilers may generate
1546 # incorrect section attributes and the assembler will warn
1548 if { [ string match "c++" $lang ] } {
1549 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1551 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1555 # Clear error and warning counts.
1558 if { [ string match "c++" $lang ] } {
1564 if { $binfile eq "tmpdir/" } {
1566 } elseif { [regexp ".*\\.a$" $binfile] } {
1567 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1572 if { ![ld_simple_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"] } {
1576 # Check if exec_output is expected.
1577 if { $warnings != "" } then {
1578 verbose -log "returned with: <$exec_output>, expected: <$warnings>"
1579 if { [regexp $warnings $exec_output] } then {
1586 if { $failed == 1 } {
1591 if { $failed == 0 } {
1592 foreach actionlist $actions {
1593 set action [lindex $actionlist 0]
1594 set progopts [lindex $actionlist 1]
1596 # There are actions where we run regexp_diff on the
1597 # output, and there are other actions (presumably).
1598 # Handling of the former look the same.
1602 { set dump_prog $objdump }
1604 { set dump_prog $nm }
1606 { set dump_prog $READELF }
1609 perror "Unrecognized action $action"
1615 if { $dump_prog != "" } {
1616 set dumpfile [lindex $actionlist 2]
1617 set binary $dump_prog
1619 # Ensure consistent sorting of symbols
1620 if {[info exists env(LC_ALL)]} {
1621 set old_lc_all $env(LC_ALL)
1624 set cmd "$binary $progopts $binfile > dump.out"
1626 catch "exec $cmd" comp_output
1627 if {[info exists old_lc_all]} {
1628 set env(LC_ALL) $old_lc_all
1632 set comp_output [prune_warnings $comp_output]
1634 if ![string match "" $comp_output] then {
1635 send_log "$comp_output\n"
1640 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1641 verbose "output is [file_contents "dump.out"]" 2
1649 if { $failed != 0 } {
1651 } elseif { $is_unresolved == 0 } {
1654 unresolved $testname
1660 # Returns true if --gc-sections is supported on the target.
1662 proc check_gc_sections_available { } {
1663 global gc_sections_available_saved
1666 if {![info exists gc_sections_available_saved]} {
1667 # Some targets don't support gc-sections despite whatever's
1668 # advertised by ld's options.
1669 if { [istarget arc*-*-*]
1670 || [istarget d30v-*-*]
1671 || [istarget dlx-*-*]
1672 || [istarget i960-*-*]
1673 || [istarget pj*-*-*]
1674 || [istarget alpha-*-*]
1675 || [istarget hppa*64-*-*]
1676 || [istarget i370-*-*]
1677 || [istarget i860-*-*]
1678 || [istarget ia64-*-*]
1679 || [istarget mep-*-*]
1680 || [istarget mn10200-*-*] } {
1681 set gc_sections_available_saved 0
1685 # elf2flt uses -q (--emit-relocs), which is incompatible with
1687 if { [board_info target exists ldflags]
1688 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1689 set gc_sections_available_saved 0
1693 # Check if the ld used by gcc supports --gc-sections.
1694 # FIXME: this test is useless since ld --help always says
1695 # --gc-sections is available
1696 set ld_output [remote_exec host $ld "--help"]
1697 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1698 set gc_sections_available_saved 1
1700 set gc_sections_available_saved 0
1703 return $gc_sections_available_saved
1706 # Returns true if -shared is supported on the target
1707 # Only used and accurate for ELF targets at the moment
1709 proc check_shared_lib_support { } {
1710 if {![istarget aarch64*-*-elf]
1711 && ![istarget arc*-*-*]
1712 && ![istarget arm*-*-elf]
1713 && ![istarget avr-*-*]
1714 && ![istarget cr16-*-*]
1715 && ![istarget cris*-*-elf]
1716 && ![istarget crx-*-*]
1717 && ![istarget d10v-*-*]
1718 && ![istarget d30v-*-*]
1719 && ![istarget dlx-*-*]
1720 && ![istarget epiphany-*-*]
1721 && ![istarget fr30-*-*]
1722 && ![istarget frv-*-*]
1723 && ![istarget h8300-*-*]
1724 && ![istarget i860-*-*]
1725 && ![istarget i960-*-*]
1726 && ![istarget ip2k-*-*]
1727 && ![istarget iq2000-*-*]
1728 && ![istarget lm32-*-*]
1729 && ![istarget m32c-*-*]
1730 && ![istarget m32r-*-*]
1731 && ![istarget m6811-*-*]
1732 && ![istarget m6812-*-*]
1733 && ![istarget m68hc1*-*-*]
1734 && ![istarget mcore*-*-*]
1735 && ![istarget mep-*-*]
1736 && ![istarget microblaze-*-*]
1737 && ![istarget mips*-*-elf]
1738 && ![istarget mn10200-*-*]
1739 && ![istarget moxie-*-*]
1740 && ![istarget msp430-*-*]
1741 && ![istarget mt-*-*]
1742 && ![istarget nds32*-*-*]
1743 && ![istarget or1k*-*-*]
1744 && ![istarget pj-*-*]
1745 && ![istarget rl78-*-*]
1746 && ![istarget rx-*-*]
1747 && ![istarget spu-*-*]
1748 && ![istarget v850*-*-*]
1749 && ![istarget visium-*-*]
1750 && ![istarget xgate-*-*]
1751 && ![istarget xstormy16-*-*]
1752 && ![istarget *-*-irix*]
1753 && ![istarget *-*-rtems] } {
1759 # Returns true if the target ld supports the plugin API.
1760 proc check_plugin_api_available { } {
1761 global plugin_api_available_saved
1763 if {![info exists plugin_api_available_saved]} {
1764 # Check if the ld used by gcc supports --plugin.
1765 set ld_output [remote_exec host $ld "--help"]
1766 if { [ string first "-plugin PLUGIN" $ld_output ] >= 0 } {
1767 set plugin_api_available_saved 1
1769 set plugin_api_available_saved 0
1772 return $plugin_api_available_saved
1775 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1776 # returns true if the target ld supports sysroot.
1777 proc check_sysroot_available { } {
1778 global ld_sysroot_available_saved ld ld_sysroot
1779 if {![info exists ld_sysroot_available_saved]} {
1780 # Check if ld supports --sysroot *other* than empty.
1781 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1782 if { $ld_sysroot == "" } {
1783 set ld_sysroot_available_saved 0
1785 set ld_sysroot_available_saved 1
1788 return $ld_sysroot_available_saved
1791 # Returns true if the target compiler supports LTO
1792 proc check_lto_available { } {
1793 global lto_available_saved
1796 if {![info exists lto_available_saved]} {
1797 if { [which $CC] == 0 } {
1798 set lto_available_saved 0
1801 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1802 # -ffat-lto-objects, we always run LTO tests on Linux with
1804 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1805 set lto_available_saved 1
1808 # Check if gcc supports -flto -fuse-linker-plugin
1810 if [board_info [target_info name] exists cflags] {
1811 append flags " [board_info [target_info name] cflags]"
1813 if [board_info [target_info name] exists ldflags] {
1814 append flags " [board_info [target_info name] ldflags]"
1817 set basename "tmpdir/lto[pid]"
1818 set src ${basename}.c
1819 set output ${basename}.out
1820 set f [open $src "w"]
1821 puts $f "int main() { return 0; }"
1823 if [is_remote host] {
1824 set src [remote_download host $src]
1826 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1827 remote_file host delete $src
1828 remote_file host delete $output
1831 return $lto_available_saved
1834 # Returns true if the target compiler supports LTO -ffat-lto-objects
1835 proc check_lto_fat_available { } {
1836 global lto_fat_available_saved
1839 if {![info exists lto_fat_available_saved]} {
1840 if { [which $CC] == 0 } {
1841 set lto_fat_available_saved 0
1844 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1845 # -ffat-lto-objects, we always run LTO tests on Linux with
1847 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1848 set lto_fat_available_saved 1
1851 # Check if gcc supports -flto -fuse-linker-plugin
1853 if [board_info [target_info name] exists cflags] {
1854 append flags " [board_info [target_info name] cflags]"
1856 if [board_info [target_info name] exists ldflags] {
1857 append flags " [board_info [target_info name] ldflags]"
1860 set basename "tmpdir/lto[pid]"
1861 set src ${basename}.c
1862 set output ${basename}.out
1863 set f [open $src "w"]
1864 puts $f "int main() { return 0; }"
1866 if [is_remote host] {
1867 set src [remote_download host $src]
1869 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
1870 remote_file host delete $src
1871 remote_file host delete $output
1874 return $lto_fat_available_saved
1877 # Returns true if the target compiler supports LTO and -shared
1878 proc check_lto_shared_available { } {
1879 global lto_shared_available_saved
1882 if {![info exists lto_shared_available_saved]} {
1883 if { [which $CC] == 0 } {
1884 set lto_shared_available_saved 0
1887 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1888 # -ffat-lto-objects, we always run LTO tests on Linux with
1890 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1891 set lto_shared_available_saved 1
1894 # Check if gcc supports -flto -fuse-linker-plugin -shared
1896 if [board_info [target_info name] exists cflags] {
1897 append flags " [board_info [target_info name] cflags]"
1899 if [board_info [target_info name] exists ldflags] {
1900 append flags " [board_info [target_info name] ldflags]"
1903 set basename "tmpdir/lto_shared[pid]"
1904 set src ${basename}.c
1905 set output ${basename}.so
1906 set f [open $src "w"]
1909 if [is_remote host] {
1910 set src [remote_download host $src]
1912 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
1913 remote_file host delete $src
1914 remote_file host delete $output
1917 return $lto_shared_available_saved
1920 # Check if the assembler supports CFI statements.
1922 proc check_as_cfi { } {
1923 global check_as_cfi_result
1925 if [info exists check_as_cfi_result] {
1926 return $check_as_cfi_result
1928 set as_file "tmpdir/check_as_cfi.s"
1929 set as_fh [open $as_file w 0666]
1930 puts $as_fh "# Generated file. DO NOT EDIT"
1931 puts $as_fh "\t.cfi_startproc"
1932 puts $as_fh "\t.cfi_endproc"
1934 remote_download host $as_file
1935 verbose -log "Checking CFI support:"
1936 rename "perror" "check_as_cfi_perror"
1937 proc perror { args } { }
1938 set success [ld_assemble $as $as_file "/dev/null"]
1940 rename "check_as_cfi_perror" "perror"
1941 #remote_file host delete $as_file
1942 set check_as_cfi_result $success
1946 # Returns true if IFUNC works.
1948 proc check_ifunc_available { } {
1949 global ifunc_available_saved
1952 if {![info exists ifunc_available_saved]} {
1953 if { [which $CC] == 0 } {
1954 set ifunc_available_saved 0
1957 # Check if gcc supports -flto -fuse-linker-plugin
1959 if [board_info [target_info name] exists cflags] {
1960 append flags " [board_info [target_info name] cflags]"
1962 if [board_info [target_info name] exists ldflags] {
1963 append flags " [board_info [target_info name] ldflags]"
1966 set basename "tmpdir/ifunc[pid]"
1967 set src ${basename}.c
1968 set output ${basename}.out
1969 set f [open $src "w"]
1970 puts $f "extern int library_func2 (void);"
1971 puts $f "int main (void)"
1973 puts $f " if (library_func2 () != 2) __builtin_abort ();"
1974 puts $f " return 0; "
1976 puts $f "static int library_func1 (void) {return 2; }"
1977 puts $f "void *foo (void) __asm__ (\"library_func2\");"
1978 puts $f "void *foo (void) { return library_func1; }"
1979 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
1981 if [is_remote host] {
1982 set src [remote_download host $src]
1984 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
1985 if { $ifunc_available_saved == 1 } {
1986 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
1988 remote_file host delete $src
1989 remote_file host delete $output
1992 return $ifunc_available_saved
1995 # Returns true if ifunc attribute works.
1997 proc check_ifunc_attribute_available { } {
1998 global ifunc_attribute_available_saved
2001 if {![info exists ifunc_attribute_available_saved]} {
2002 if { [which $CC] == 0 } {
2003 set ifunc_attribute_available_saved 0
2006 # Check if gcc supports -flto -fuse-linker-plugin
2008 if [board_info [target_info name] exists cflags] {
2009 append flags " [board_info [target_info name] cflags]"
2011 if [board_info [target_info name] exists ldflags] {
2012 append flags " [board_info [target_info name] ldflags]"
2015 set basename "tmpdir/ifunc[pid]"
2016 set src ${basename}.c
2017 set output ${basename}.out
2018 set f [open $src "w"]
2019 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
2020 puts $f "int main (void)"
2022 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2023 puts $f " return 0; "
2025 puts $f "static int library_func1 (void) {return 2; }"
2026 puts $f "void *foo (void) { return library_func1; }"
2028 if [is_remote host] {
2029 set src [remote_download host $src]
2031 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2032 if { $ifunc_attribute_available_saved == 1 } {
2033 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
2035 remote_file host delete $src
2036 remote_file host delete $output
2039 return $ifunc_attribute_available_saved
2042 # Provide virtual target "cfi" for targets supporting CFI.
2044 rename "istarget" "istarget_ld"
2045 proc istarget { target } {
2046 if {$target == "cfi"} {
2047 return [check_as_cfi]
2049 return [istarget_ld $target]