1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2017 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 } {
89 if { ![is_remote host] && [which "$prog"] == 0 } then {
90 perror "$prog does not exist"
94 # If we are compiling with gcc, we want to add gcc_B_opt and
95 # ld_L_opt to flags. However, if $prog already has -B options,
96 # which might be the case when running gcc out of a build
97 # directory, we want our -B options to come first.
99 set gccparm [string first " " $gccexe]
101 if { $gccparm > 0 } then {
102 set gccflags [string range $gccexe $gccparm end]
103 set gccexe [string range $gccexe 0 $gccparm]
106 set gccexe [string replace $gccexe 0 [string last "/" $gccexe] ""]
107 if {[string match "*cc*" $gccexe] || [string match "*++*" $gccexe]} then {
108 set gccflags "$gcc_B_opt $gccflags $ld_L_opt"
111 verbose -log "$prog $gccflags $command"
112 set status [remote_exec host [concat sh -c [list "$prog $gccflags $command 2>&1"]] "" "/dev/null" "ld.tmp"]
113 remote_upload host "ld.tmp"
114 set link_output [file_contents "ld.tmp"]
115 regsub "\n$" $link_output "" link_output
116 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
117 append link_output "child process exited abnormally"
119 remote_file build delete ld.tmp
120 remote_file host delete ld.tmp
122 if [string match "" $link_output] then {
126 verbose -log "$link_output"
127 return "$link_output"
130 proc run_host_cmd_yesno { prog command } {
132 global errcnt warncnt
134 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
135 # Ignore error and warning.
138 if [string match "" $exec_output] then {
144 # Link an object using relocation.
146 proc default_ld_relocate { ld target objects } {
149 remote_file host delete $target
150 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
153 # Check to see if ld is being invoked with a non-endian output format
155 proc is_endian_output_format { object_flags } {
157 if {[string match "*-oformat binary*" $object_flags] || \
158 [string match "*-oformat ieee*" $object_flags] || \
159 [string match "*-oformat ihex*" $object_flags] || \
160 [string match "*-oformat netbsd-core*" $object_flags] || \
161 [string match "*-oformat srec*" $object_flags] || \
162 [string match "*-oformat tekhex*" $object_flags] || \
163 [string match "*-oformat trad-core*" $object_flags] } then {
170 # Look for big-endian or little-endian switches in the multlib
171 # options and translate these into a -EB or -EL switch. Note
172 # we cannot rely upon proc process_multilib_options to do this
173 # for us because for some targets the compiler does not support
174 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
175 # the site.exp file will include the switch "-mbig-endian"
176 # (rather than "big-endian") which is not detected by proc
177 # process_multilib_options.
179 proc big_or_little_endian {} {
181 if [board_info [target_info name] exists multilib_flags] {
182 set tmp_flags " [board_info [target_info name] multilib_flags]"
184 foreach x $tmp_flags {
186 {*big*endian eb EB -eb -EB -mb -meb} {
190 {*little*endian el EL -el -EL -ml -mel} {
202 # Link a program using ld.
204 proc default_ld_link { ld target objects } {
215 if { [ string match "* -pie *" $objects ] } {
216 set objs "$HOSTING_SCRT0 $objects"
217 set libs "$LIBS $HOSTING_SLIBS"
219 set objs "$HOSTING_CRT0 $objects"
220 set libs "$LIBS $HOSTING_LIBS"
223 if [is_endian_output_format $objects] then {
224 set flags [big_or_little_endian]
229 remote_file host delete $target
231 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU $flags -o $target $objs $libs"]
234 # Link a program using ld, without including any libraries.
236 proc default_ld_simple_link { ld target objects } {
241 if [is_endian_output_format $objects] then {
242 set flags [big_or_little_endian]
245 remote_file host delete $target
246 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
247 set exec_output [prune_warnings $exec_output]
249 # We don't care if we get a warning about a non-existent start
250 # symbol, since the default linker script might use ENTRY.
251 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
253 return [string match "" $exec_output]
256 # Compile an object using cc.
258 proc default_ld_compile { cc source object } {
267 if {[llength $cc_prog] > 1} then {
268 set cc_prog [lindex $cc_prog 0]
270 if {![is_remote host] && [which $cc_prog] == 0} then {
271 perror "$cc_prog does not exist"
275 remote_file build delete "$object"
276 remote_file host delete "$object"
278 set flags "$gcc_B_opt -I$srcdir/$subdir"
280 # If we are compiling with gcc, we want to add gcc_B_opt to flags.
281 # However, if $prog already has -B options, which might be the
282 # case when running gcc out of a build directory, we want our -B
283 # options to come first.
285 set ccparm [string first " " $cc]
287 if { $ccparm > 0 } then {
288 set ccflags [string range $cc $ccparm end]
289 set ccexe [string range $cc 0 $ccparm]
293 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
294 if {[string match "*++*" $ccexe]} {
295 append flags " $CXXFLAGS"
297 append flags " $CFLAGS"
300 if [board_info [target_info name] exists cflags] {
301 append flags " [board_info [target_info name] cflags]"
304 if [board_info [target_info name] exists multilib_flags] {
305 append flags " [board_info [target_info name] multilib_flags]"
308 set cmd "$cc $flags $ccflags -c $source -o $object"
311 set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
312 remote_upload host "ld.tmp"
313 set exec_output [file_contents "ld.tmp"]
314 remote_file build delete "ld.tmp"
315 remote_file host delete "ld.tmp"
316 set exec_output [prune_warnings $exec_output]
317 if [string match "" $exec_output] then {
318 if {![file exists $object]} then {
319 regexp ".*/(\[^/\]*)$" $source all dobj
320 regsub "\\.c" $dobj ".o" realobj
321 verbose "looking for $realobj"
322 if {[remote_file host exists $realobj]} then {
323 verbose -log "mv $realobj $object"
324 remote_upload "$realobj" "$object"
326 perror "$object not found after compilation"
332 verbose -log "$exec_output"
333 perror "$source: compilation failed"
340 proc default_ld_assemble { as in_flags source object } {
346 if ![info exists ASFLAGS] { set ASFLAGS "" }
348 set flags "[big_or_little_endian] -I$srcdir/$subdir"
349 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
350 set exec_output [prune_warnings $exec_output]
351 if [string match "" $exec_output] then {
354 perror "$source: assembly failed"
359 # Run nm on a file, putting the result in the array nm_output.
361 proc default_ld_nm { nm nmflags object } {
366 if {[info exists nm_output]} {
370 if ![info exists NMFLAGS] { set NMFLAGS "" }
372 # Ensure consistent sorting of symbols
373 if {[info exists env(LC_ALL)]} {
374 set old_lc_all $env(LC_ALL)
378 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
380 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
381 if {[info exists old_lc_all]} {
382 set env(LC_ALL) $old_lc_all
386 remote_upload host "ld.stderr"
387 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
388 set exec_output [prune_warnings [file_contents "ld.stderr"]]
389 remote_file host delete "ld.stderr"
390 remote_file build delete "ld.stderr"
391 if [string match "" $exec_output] then {
392 set file [open tmpdir/nm.out r]
393 while { [gets $file line] != -1 } {
395 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
396 set name [string trimleft $name "_"]
397 verbose "Setting nm_output($name) to 0x$value" 2
398 set nm_output($name) 0x$value
404 verbose -log "$exec_output"
405 perror "$object: nm failed"
410 # Define various symbols needed when not linking against all
412 proc ld_simple_link_defsyms {} {
414 set flags "--defsym __stack_chk_fail=0"
416 # ARM targets call __gccmain
417 if {[istarget arm*-*-*]} {
418 append flags " --defsym __gccmain=0"
421 # Windows targets need __main, some prefixed with underscore.
422 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
423 append flags " --defsym __main=0 --defsym ___main=0"
426 # PowerPC EABI code calls __eabi.
427 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
428 append flags " --defsym __eabi=0"
431 # mn10200 code calls __truncsipsi2_d0_d2.
432 if {[istarget mn10200*-*-*]} then {
433 append flags " --defsym __truncsipsi2_d0_d2=0"
436 # m6811/m6812 code has references to soft registers.
437 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
438 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
439 append flags " --defsym _.d3=0 --defsym _.d4=0"
440 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
443 # Some OpenBSD targets have ProPolice and reference __guard and
444 # __stack_smash_handler.
445 if [istarget *-*-openbsd*] {
446 append flags " --defsym __guard=0"
447 append flags " --defsym __stack_smash_handler=0"
453 # run_dump_test FILE (optional:) EXTRA_OPTIONS
454 # Copied from gas testsuite, tweaked and further extended.
456 # Assemble a .s file, then run some utility on it and check the output.
458 # There should be an assembly language file named FILE.s in the test
459 # suite directory, and a pattern file called FILE.d. `run_dump_test'
460 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
461 # `nm' on the .o file to produce textual output, and then analyze that
462 # with regexps. The FILE.d file specifies what program to run, and
463 # what to expect in its output.
465 # The FILE.d file begins with zero or more option lines, which specify
466 # flags to pass to the assembler, the program to run to dump the
467 # assembler's output, and the options it wants. The option lines have
472 # OPTION is the name of some option, like "name" or "objdump", and
473 # VALUE is OPTION's value. The valid options are described below.
474 # Whitespace is ignored everywhere, except within VALUE. The option
475 # list ends with the first line that doesn't match the above syntax
476 # (hmm, not great for error detection).
478 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
479 # two-element lists. The first element of each is an option name, and
480 # the second additional arguments to be added on to the end of the
481 # option list as given in FILE.d. (If omitted, no additional options
484 # The interesting options are:
487 # The name of this test, passed to DejaGNU's `pass' and `fail'
488 # commands. If omitted, this defaults to FILE, the root of the
489 # .s and .d files' names.
492 # When assembling, pass FLAGS to the assembler.
493 # If assembling several files, you can pass different assembler
494 # options in the "source" directives. See below.
497 # Link assembled files using FLAGS, in the order of the "source"
498 # directives, when using multiple files.
500 # ld_after_inputfiles: FLAGS
501 # Similar to "ld", but put after all input files.
503 # objcopy_objects: FLAGS
504 # Run objcopy with the specified flags after assembling any source
505 # that has the special marker RUN_OBJCOPY in the source specific
508 # objcopy_linked_file: FLAGS
509 # Run objcopy on the linked file with the specified flags.
510 # This lets you transform the linked file using objcopy, before the
511 # result is analyzed by an analyzer program specified below (which
512 # may in turn *also* be objcopy).
515 # The name of the program to run to analyze the .o file produced
516 # by the assembler or the linker output. This can be omitted;
517 # run_dump_test will guess which program to run by seeing which of
518 # the flags options below is present.
524 # Use the specified program to analyze the assembler or linker
525 # output file, and pass it FLAGS, in addition to the output name.
526 # Note that they are run with LC_ALL=C in the environment to give
527 # consistent sorting of symbols.
529 # source: SOURCE [FLAGS]
530 # Assemble the file SOURCE.s using the flags in the "as" directive
531 # and the (optional) FLAGS. If omitted, the source defaults to
533 # This is useful if several .d files want to share a .s file.
534 # More than one "source" directive can be given, which is useful
535 # when testing linking.
538 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
539 # is useful if several .d files differ by options only. Options are
540 # always read from FILE.d.
543 # The test is expected to fail on TARGET. This may occur more than
547 # Only run the test for TARGET. This may occur more than once; the
548 # target being tested must match at least one. You may provide target
549 # name "cfi" for any target supporting the CFI statements.
552 # Do not run the test for TARGET. This may occur more than once;
553 # the target being tested must not match any of them.
556 # An error with message matching REGEX must be emitted for the test
557 # to pass. The PROG, readelf, objdump, nm and objcopy options have
558 # no meaning and need not be supplied if this is present. Multiple
559 # "error" directives append to the expected linker error message.
562 # Means the same as 'error', except the regular expression lines
563 # are contains in FILE.
566 # Expect a linker warning matching REGEX. It is an error to issue
567 # both "error" and "warning". Multiple "warning" directives
568 # append to the expected linker warning message.
570 # warning_output: FILE
571 # Means the same as 'warning', except the regular expression
572 # lines are contains in FILE.
575 # Adding this option will cause the linker to generate a linker
576 # map file, using the -Map=MAPFILE command line option. If
577 # there is no -Map=MAPFILE in the 'ld: FLAGS' then one will be
578 # added to the linker command line. The contents of the
579 # generated MAPFILE are then compared against the regexp lines
580 # in FILE using `regexp_diff' (see below for details).
582 # Each option may occur at most once unless otherwise mentioned.
584 # After the option lines come regexp lines. `run_dump_test' calls
585 # `regexp_diff' to compare the output of the dumping tool against the
586 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
587 # see further comments there.
589 proc run_dump_test { name {extra_options {}} } {
591 global OBJDUMP NM AS OBJCOPY READELF LD
592 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
593 global host_triplet runtests
595 global ld_elf_shared_opt
597 if { [is_elf_format] && [check_shared_lib_support] } {
598 set ld_extra_opt "$ld_elf_shared_opt"
603 if [string match "*/*" $name] {
605 set name [file tail $name]
607 set file "$srcdir/$subdir/$name"
610 if ![runtest_file_p $runtests $name] then {
614 set opt_array [slurp_options "${file}.d"]
615 if { $opt_array == -1 } {
616 perror "error reading options from $file.d"
617 unresolved $subdir/$name
620 set dumpfile tmpdir/dump.out
626 set opts(ld_after_inputfiles) {}
629 set opts(notarget) {}
640 set opts(error_output) {}
641 set opts(warning_output) {}
642 set opts(objcopy_linked_file) {}
643 set opts(objcopy_objects) {}
646 foreach i $opt_array {
647 set opt_name [lindex $i 0]
648 set opt_val [lindex $i 1]
649 if ![info exists opts($opt_name)] {
650 perror "unknown option $opt_name in file $file.d"
651 unresolved $subdir/$name
655 switch -- $opt_name {
662 # Move any source-specific as-flags to a separate list to
663 # simplify processing.
664 if { [llength $opt_val] > 1 } {
665 lappend asflags [lrange $opt_val 1 end]
666 set opt_val [lindex $opt_val 0]
671 # Create the object file name based on nothing but the source
674 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]].o]
675 # But, sometimes, we have the exact same source filename in
676 # different directories (foo/src.s bar/src.s) which would lead
677 # us to try and create two src.o files. We detect this
678 # conflict here, and instead create src.o and src1.o.
680 while { [lsearch $objfile_names $new_objfile] != -1 } {
683 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]]${j}.o]
685 lappend objfile_names $new_objfile
688 if [string length $opts($opt_name)] {
689 perror "option $opt_name multiply set in $file.d"
690 unresolved $subdir/$name
694 # A single "# ld:" with no options should do the right thing.
695 if { $opt_name == "ld" } {
698 # Likewise objcopy_linked_file.
699 if { $opt_name == "objcopy_linked_file" } {
704 if { $opt_name == "as" || $opt_name == "ld" } {
705 set opt_val [subst $opt_val]
708 # Append differently whether it's a message (without space) or
709 # an option or list (with space).
710 switch -- $opt_name {
713 append opts($opt_name) $opt_val
716 set opts($opt_name) [concat $opts($opt_name) $opt_val]
721 foreach i $extra_options {
722 set opt_name [lindex $i 0]
723 set opt_val [lindex $i 1]
724 if ![info exists opts($opt_name)] {
725 perror "unknown option $opt_name given in extra_opts"
726 unresolved $subdir/$name
729 # Add extra option to end of existing option, adding space
731 if { ![regexp "warning|error" $opt_name]
732 && [string length $opts($opt_name)] } {
733 append opts($opt_name) " "
735 append opts($opt_name) $opt_val
738 foreach opt { as ld } {
739 regsub {\[big_or_little_endian\]} $opts($opt) \
740 [big_or_little_endian] opts($opt)
743 # Decide early whether we should run the test for this target.
744 if { [llength $opts(target)] > 0 } {
746 foreach targ $opts(target) {
747 if [istarget $targ] {
752 if { $targmatch == 0 } {
756 foreach targ $opts(notarget) {
757 if [istarget $targ] {
763 # It's meaningless to require an output-testing method when we
765 if { $opts(error) == "" && $opts(error_output) == "" } {
766 if {$opts(PROG) != ""} {
767 switch -- $opts(PROG) {
768 objdump { set program objdump }
769 nm { set program nm }
770 objcopy { set program objcopy }
771 readelf { set program readelf }
773 { perror "unrecognized program option $opts(PROG) in $file.d"
774 unresolved $subdir/$name
778 # Guess which program to run, by seeing which option was specified.
779 foreach p {objdump objcopy nm readelf} {
780 if {$opts($p) != ""} {
781 if {$program != ""} {
782 perror "ambiguous dump program in $file.d"
783 unresolved $subdir/$name
791 if { $program == "" \
792 && $opts(map) == "" \
793 && $opts(warning) == "" \
794 && $opts(warning_output) == "" \
795 && $opts(error) == "" \
796 && $opts(error_output) == "" } {
797 perror "dump program unspecified in $file.d"
798 unresolved $subdir/$name
803 if { $opts(name) == "" } {
804 set testname "$subdir/$name"
806 set testname $opts(name)
809 if { $opts(source) == "" } {
810 set sourcefiles [list ${file}.s]
811 set asflags [list ""]
812 set objfile_names [list tmpdir/[file tail ${file}].o]
815 foreach sf $opts(source) {
816 if { [string match "/*" $sf] } {
817 lappend sourcefiles "$sf"
819 lappend sourcefiles "$srcdir/$subdir/$sf"
824 if { $opts(dump) == "" } {
827 set dfile $srcdir/$subdir/$opts(dump)
830 # Time to setup xfailures.
831 foreach targ $opts(xfail) {
835 # Assemble each file.
837 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
838 set sourcefile [lindex $sourcefiles $i]
839 set sourceasflags [lindex $asflags $i]
840 set run_objcopy_objects 0
842 if { [string match "*RUN_OBJCOPY*" $sourceasflags] } {
843 set run_objcopy_objects 1
845 regsub "RUN_OBJCOPY" $sourceasflags "" sourceasflags
847 set objfile [lindex $objfile_names $i]
848 catch "exec rm -f $objfile" exec_output
849 lappend objfiles $objfile
850 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
853 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
854 remote_upload host "ld.tmp"
855 set comp_output [prune_warnings [file_contents "ld.tmp"]]
856 remote_file host delete "ld.tmp"
857 remote_file build delete "ld.tmp"
859 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
860 send_log -- "$comp_output\n"
861 verbose "$comp_output" 3
863 set exitstat "succeeded"
864 if { $cmdret != 0 } { set exitstat "failed" }
865 verbose -log "$exitstat with: <$comp_output>"
870 if { $run_objcopy_objects } {
871 set cmd "$OBJCOPY $opts(objcopy_objects) $objfile"
874 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] \
875 "" "/dev/null" "objcopy.tmp"]
876 remote_upload host "objcopy.tmp"
877 set comp_output [prune_warnings [file_contents "objcopy.tmp"]]
878 remote_file host delete "objcopy.tmp"
879 remote_file build delete "objcopy.tmp"
881 if { [lindex $cmdret 0] != 0 \
882 || ![string match "" $comp_output] } {
883 send_log -- "$comp_output\n"
884 verbose "$comp_output" 3
886 set exitstat "succeeded"
887 if { $cmdret != 0 } { set exitstat "failed" }
888 verbose -log "$exitstat with: <$comp_output>"
895 if { (($opts(warning) != "") && ($opts(error) != "")) \
896 || (($opts(warning) != "") && ($opts(error_output) != "")) \
897 || (($opts(warning) != "") && ($opts(warning_output) != "")) \
898 || (($opts(error) != "") && ($opts(warning_output) != "")) \
899 || (($opts(error) != "") && ($opts(error_output) != "")) \
900 || (($opts(warning_output) != "") && ($opts(error_output) != "")) } {
901 perror "$testname: bad mix of warning, error, warning_output, and error_output test-directives"
906 set check_ld(source) ""
907 set check_ld(terminal) 0
908 if { $opts(error) != "" \
909 || $opts(warning) != "" \
910 || $opts(error_output) != "" \
911 || $opts(warning_output) != "" } {
913 if { $opts(error) != "" || $opts(error_output) != "" } {
914 set check_ld(terminal) 1
916 set check_ld(terminal) 0
919 if { $opts(error) != "" || $opts(warning) != "" } {
920 set check_ld(source) "regex"
921 if { $opts(error) != "" } {
922 set check_ld(regex) $opts(error)
924 set check_ld(regex) $opts(warning)
927 set check_ld(source) "file"
928 if { $opts(error_output) != "" } {
929 set check_ld(file) $opts(error_output)
931 set check_ld(file) $opts(warning_output)
936 # Perhaps link the file(s).
938 set objfile "tmpdir/dump"
939 catch "exec rm -f $objfile" exec_output
941 # Add -L$srcdir/$subdir so that the linker command can use
942 # linker scripts in the source directory.
943 set cmd "$LD $ld_extra_opt $LDFLAGS -L$srcdir/$subdir \
944 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
946 # If needed then check for, or add a -Map option.
948 if { $opts(map) != "" } then {
949 if { [regexp -- "-Map=(\[^ \]+)" $cmd all mapfile] } then {
950 # Found existing mapfile option
951 verbose -log "Existing mapfile '$mapfile' found"
954 set mapfile "tmpdir/dump.map"
955 verbose -log "Adding mapfile '$mapfile'"
956 set cmd "$cmd -Map=$mapfile"
961 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
962 remote_upload host "ld.tmp"
963 set comp_output [file_contents "ld.tmp"]
964 remote_file host delete "ld.tmp"
965 remote_file build delete "ld.tmp"
966 set cmdret [lindex $cmdret 0]
968 if { $cmdret == 0 && $run_objcopy } {
970 set objfile "tmpdir/dump1"
971 remote_file host delete $objfile
973 # Note that we don't use OBJCOPYFLAGS here; any flags must be
974 # explicitly specified.
975 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
978 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
979 remote_upload host "ld.tmp"
980 append comp_output [file_contents "ld.tmp"]
981 remote_file host delete "ld.tmp"
982 remote_file build delete "ld.tmp"
983 set cmdret [lindex $cmdret 0]
986 regsub "\n$" $comp_output "" comp_output
987 if { $cmdret != 0 || $comp_output != "" || $check_ld(source) != "" } then {
988 set exitstat "succeeded"
989 if { $cmdret != 0 } { set exitstat "failed" }
991 if { $check_ld(source) == "regexp" } {
992 verbose -log "$exitstat with: <$comp_output>, expected: <$check_ld(regex)>"
993 } elseif { $check_ld(source) == "file" } {
994 verbose -log "$exitstat with: <$comp_output>, expected in file $check_ld(file)"
995 set_file_contents "tmpdir/ld.messages" "$comp_output"
997 verbose -log "$exitstat with: <$comp_output>, no expected output"
999 send_log -- "$comp_output\n"
1000 verbose "$comp_output" 3
1002 if { (($check_ld(source) == "") == ($comp_output == "")) \
1003 && (($cmdret == 0) == ($check_ld(terminal) == 0)) \
1004 && ((($check_ld(source) == "regex") \
1005 && ($check_ld(regex) == "") == ($comp_output == "") \
1006 && [regexp $check_ld(regex) $comp_output]) \
1007 || (($check_ld(source) == "file") \
1008 && (![regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"]))) } {
1009 # We have the expected output from ld.
1010 if { $check_ld(terminal) || $program == "" } {
1020 if { $opts(map) != "" } then {
1021 # Check the map file matches.
1022 set map_pattern_file $srcdir/$subdir/$opts(map)
1023 verbose -log "Compare '$mapfile' against '$map_pattern_file'"
1024 if { [regexp_diff $mapfile $map_pattern_file] } then {
1025 fail "$testname (map file check)"
1027 pass "$testname (map file check)"
1030 if { $program == "" } then {
1035 set objfile [lindex $objfiles 0]
1038 # We must not have expected failure if we get here.
1039 if { $opts(error) != "" } {
1044 set progopts1 $opts($program)
1045 eval set progopts \$[string toupper $program]FLAGS
1046 eval set binary \$[string toupper $program]
1048 if { ![is_remote host] && [which $binary] == 0 } {
1053 if { $progopts1 == "" } { set $progopts1 "-r" }
1054 verbose "running $binary $progopts $progopts1" 3
1056 # Objcopy, unlike the other two, won't send its output to stdout,
1057 # so we have to run it specially.
1058 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
1059 if { $program == "objcopy" } {
1060 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
1063 # Ensure consistent sorting of symbols
1064 if {[info exists env(LC_ALL)]} {
1065 set old_lc_all $env(LC_ALL)
1069 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
1070 set cmdret [lindex $cmdret 0]
1071 remote_upload host "ld.tmp"
1072 set comp_output [prune_warnings [file_contents "ld.tmp"]]
1073 remote_file host delete "ld.tmp"
1074 remote_file build delete "ld.tmp"
1075 if {[info exists old_lc_all]} {
1076 set env(LC_ALL) $old_lc_all
1080 if { $cmdret != 0 || $comp_output != "" } {
1081 send_log "exited abnormally with $cmdret, output:$comp_output\n"
1086 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
1087 if { [regexp_diff $dumpfile "${dfile}"] } then {
1089 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
1096 proc slurp_options { file } {
1097 # If options_regsub(foo) is set to {a b}, then the contents of a
1098 # "#foo:" line will have regsub -all applied to replace a with b.
1099 global options_regsub
1101 if [catch { set f [open $file r] } x] {
1102 #perror "couldn't open `$file': $x"
1107 # whitespace expression
1110 # whitespace is ignored anywhere except within the options list;
1111 # option names are alphabetic plus underscore only.
1112 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
1113 while { [gets $f line] != -1 } {
1114 set line [string trim $line]
1115 # Whitespace here is space-tab.
1116 if [regexp $pat $line xxx opt_name opt_val] {
1118 if [info exists options_regsub($opt_name)] {
1119 set subst $options_regsub($opt_name)
1120 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
1123 lappend opt_array [list $opt_name $opt_val]
1132 proc file_contents { filename } {
1133 set file [open $filename r]
1134 set contents [read $file]
1139 proc set_file_contents { filename contents } {
1140 set file [open $filename w]
1141 puts $file "$contents"
1145 # Create an archive using ar
1147 proc ar_simple_create { ar aropts target objects } {
1148 remote_file host delete $target
1150 set exec_output [run_host_cmd "$ar" "-rc $aropts $target $objects"]
1151 set exec_output [prune_warnings $exec_output]
1153 if [string match "" $exec_output] then {
1154 send_log "$exec_output\n"
1161 # List contains test-items with 3 items followed by 2 lists, one item and
1162 # one optional item:
1164 # 1:ld/ar leading options, placed before object files
1165 # 2:ld/ar trailing options, placed after object files
1166 # 3:assembler options
1167 # 4:filenames of assembler files
1168 # 5:list of actions, options and expected outputs.
1169 # 6:name of output file
1170 # 7:compiler flags (optional)
1172 # Actions: { command command-line-options file-containg-expected-output-regexps }
1174 # objdump: Apply objdump options on result.
1175 # nm: Apply nm options on result.
1176 # readelf: Apply readelf options on result.
1177 # ld: Don't apply anything on result. Compare output during linking with
1178 # the file containing regexps (which is the second arg, not the third).
1179 # Note that this *must* be the first action if it is to be used at all;
1180 # in all other cases, any output from the linker during linking is
1181 # treated as a sign of an error and FAILs the test.
1183 # args is an optional list of target triplets to be xfailed.
1185 proc run_ld_link_tests { ldtests args } {
1199 global ld_elf_shared_opt
1201 if { [is_elf_format] && [check_shared_lib_support] } {
1202 set ld_extra_opt "$ld_elf_shared_opt"
1207 foreach testitem $ldtests {
1208 set testname [lindex $testitem 0]
1210 if ![runtest_file_p $runtests $testname] then {
1214 foreach target $args {
1218 set ld_options [lindex $testitem 1]
1219 set ld_after [lindex $testitem 2]
1220 set as_options [lindex $testitem 3]
1221 set src_files [lindex $testitem 4]
1222 set actions [lindex $testitem 5]
1223 set binfile tmpdir/[lindex $testitem 6]
1224 set cflags [lindex $testitem 7]
1231 # verbose -log "Testname is $testname"
1232 # verbose -log "ld_options is $ld_options"
1233 # verbose -log "ld_after is $ld_after"
1234 # verbose -log "as_options is $as_options"
1235 # verbose -log "src_files is $src_files"
1236 # verbose -log "actions is $actions"
1237 # verbose -log "binfile is $binfile"
1239 # Assemble each file in the test.
1240 foreach src_file $src_files {
1241 set fileroot "[file rootname [file tail $src_file]]"
1242 set objfile "tmpdir/$fileroot.o"
1243 lappend objfiles $objfile
1245 if { [file extension $src_file] == ".c" } {
1246 set as_file "tmpdir/$fileroot.s"
1247 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1252 set as_file "$srcdir/$subdir/$src_file"
1254 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1260 # Catch assembler errors.
1261 if { $is_unresolved } {
1262 unresolved $testname
1266 if { $binfile eq "tmpdir/" } {
1268 } elseif { [regexp ".*\\.a$" $binfile] } {
1269 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
1272 } elseif { ![ld_simple_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
1274 set ld_output "$exec_output"
1278 foreach actionlist $actions {
1279 set action [lindex $actionlist 0]
1280 set progopts [lindex $actionlist 1]
1282 # There are actions where we run regexp_diff on the
1283 # output, and there are other actions (presumably).
1284 # Handling of the former look the same.
1288 { set dump_prog $objdump }
1290 { set dump_prog $nm }
1292 { set dump_prog $READELF }
1294 { set dump_prog "ld" }
1297 perror "Unrecognized action $action"
1303 if { $action == "ld" } {
1304 set regexpfile $progopts
1305 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1306 set_file_contents "tmpdir/ld.messages" "$ld_output"
1307 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1308 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1309 verbose "output is $ld_output" 2
1314 } elseif { !$maybe_failed && $dump_prog != "" } {
1315 set dumpfile [lindex $actionlist 2]
1316 set binary $dump_prog
1318 # Ensure consistent sorting of symbols
1319 if {[info exists env(LC_ALL)]} {
1320 set old_lc_all $env(LC_ALL)
1323 set cmd "$binary $progopts $binfile"
1324 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1326 remote_upload host "ld.stderr"
1327 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1328 remote_file host delete "ld.stderr"
1329 remote_file build delete "ld.stderr"
1331 if {[info exists old_lc_all]} {
1332 set env(LC_ALL) $old_lc_all
1337 if ![string match "" $comp_output] then {
1338 send_log "$comp_output\n"
1343 remote_upload host "dump.out"
1345 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1346 verbose "output is [file_contents "dump.out"]" 2
1348 remote_file build delete "dump.out"
1349 remote_file host delete "dump.out"
1352 remote_file build delete "dump.out"
1353 remote_file host delete "dump.out"
1358 if { $is_unresolved } {
1359 unresolved $testname
1360 } elseif { $maybe_failed || $failed } {
1368 # This definition is taken from an unreleased version of DejaGnu. Once
1369 # that version gets released, and has been out in the world for a few
1370 # months at least, it may be safe to delete this copy.
1371 if ![string length [info proc prune_warnings]] {
1373 # prune_warnings -- delete various system verbosities from TEXT
1376 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1378 # Sites with particular verbose os's may wish to override this in site.exp.
1380 proc prune_warnings { text } {
1381 # This is from sun4's. Do it for all machines for now.
1382 # The "\\1" is to try to preserve a "\n" but only if necessary.
1383 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1385 # It might be tempting to get carried away and delete blank lines, etc.
1386 # Just delete *exactly* what we're ask to, and that's it.
1391 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1392 # and 3 optional items:
1395 # 2:assembler options
1396 # 3:filenames of source files
1397 # 4:name of output file
1399 # 6:compiler flags (optional)
1400 # 7:language (optional)
1401 # 8:linker warning (optional)
1402 # args is an optional list of target triplets to be xfailed.
1404 proc run_ld_link_exec_tests { ldtests args } {
1417 foreach testitem $ldtests {
1418 foreach target $args {
1421 set testname [lindex $testitem 0]
1422 set ld_options [lindex $testitem 1]
1423 set as_options [lindex $testitem 2]
1424 set src_files [lindex $testitem 3]
1425 set binfile tmpdir/[lindex $testitem 4]
1426 set expfile [lindex $testitem 5]
1427 set cflags [lindex $testitem 6]
1428 set lang [lindex $testitem 7]
1429 set warning [lindex $testitem 8]
1433 # verbose -log "Testname is $testname"
1434 # verbose -log "ld_options is $ld_options"
1435 # verbose -log "as_options is $as_options"
1436 # verbose -log "src_files is $src_files"
1437 # verbose -log "binfile is $binfile"
1439 # Assemble each file in the test.
1440 foreach src_file $src_files {
1441 set fileroot "[file rootname [file tail $src_file]]"
1442 set objfile "tmpdir/$fileroot.o"
1443 lappend objfiles $objfile
1445 # We ignore warnings since some compilers may generate
1446 # incorrect section attributes and the assembler will warn
1448 if { [ string match "c++" $lang ] } {
1449 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1451 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1455 # We have to use $CC to build PIE and shared library.
1456 if { [ string match "c" $lang ] } {
1457 set link_proc ld_simple_link
1459 } elseif { [ string match "c++" $lang ] } {
1460 set link_proc ld_simple_link
1462 } elseif { [ string match "-shared" $ld_options ] \
1463 || [ string match "-pie" $ld_options ] } {
1464 set link_proc ld_simple_link
1467 set link_proc ld_link
1471 if { $binfile eq "tmpdir/" } {
1475 } elseif ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1479 # Check if exec_output is expected.
1480 if { $warning != "" } then {
1481 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1482 if { [regexp $warning $exec_output] } then {
1489 if { $failed == 0 } {
1490 send_log "Running: $binfile > $binfile.out\n"
1491 verbose "Running: $binfile > $binfile.out"
1492 catch "exec $binfile > $binfile.out" exec_output
1494 if ![string match "" $exec_output] then {
1495 send_log "$exec_output\n"
1496 verbose "$exec_output" 1
1499 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1500 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1501 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1502 set exec_output [prune_warnings $exec_output]
1504 if ![string match "" $exec_output] then {
1505 send_log "$exec_output\n"
1506 verbose "$exec_output" 1
1512 if { $failed != 0 } {
1521 # List contains test-items with 3 items followed by 2 lists, one item and
1522 # one optional item:
1524 # 1:ld or ar options
1526 # 3:filenames of source files
1527 # 4:action and options.
1528 # 5:name of output file
1529 # 6:language (optional)
1530 # 7:linker warnings (optional)
1533 # objdump: Apply objdump options on result. Compare with regex (last arg).
1534 # nm: Apply nm options on result. Compare with regex (last arg).
1535 # readelf: Apply readelf options on result. Compare with regex (last arg).
1537 proc run_cc_link_tests { ldtests } {
1552 if [board_info [target_info name] exists cflags] {
1553 set board_cflags " [board_info [target_info name] cflags]"
1558 foreach testitem $ldtests {
1559 set testname [lindex $testitem 0]
1560 set ldflags [lindex $testitem 1]
1561 set cflags [lindex $testitem 2]
1562 set src_files [lindex $testitem 3]
1563 set actions [lindex $testitem 4]
1564 set binfile tmpdir/[lindex $testitem 5]
1565 set lang [lindex $testitem 6]
1566 set warnings [lindex $testitem 7]
1571 #verbose -log "testname is $testname"
1572 #verbose -log "ldflags is $ldflags"
1573 #verbose -log "cflags is $cflags"
1574 #verbose -log "src_files is $src_files"
1575 #verbose -log "actions is $actions"
1576 #verbose -log "binfile is $binfile"
1577 #verbose -log "lang is $lang"
1578 #verbose -log "warnings is $warnings"
1580 # Compile each file in the test.
1581 foreach src_file $src_files {
1582 set fileroot "[file rootname [file tail $src_file]]"
1583 set objfile "tmpdir/$fileroot.o"
1584 lappend objfiles $objfile
1586 # We ignore warnings since some compilers may generate
1587 # incorrect section attributes and the assembler will warn
1589 if { [ string match "c++" $lang ] } {
1590 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1592 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1596 # Clear error and warning counts.
1599 if { [ string match "c++" $lang ] } {
1605 if { $binfile eq "tmpdir/" } {
1607 } elseif { [regexp ".*\\.a$" $binfile] } {
1608 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1612 if { ![ld_simple_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"] } {
1616 # Check if exec_output is expected.
1617 if { $warnings != "" } then {
1618 verbose -log "returned with: <$exec_output>, expected: <$warnings>"
1619 if { [regexp $warnings $exec_output] } then {
1627 if { $failed == 0 } {
1628 foreach actionlist $actions {
1629 set action [lindex $actionlist 0]
1630 set progopts [lindex $actionlist 1]
1632 # There are actions where we run regexp_diff on the
1633 # output, and there are other actions (presumably).
1634 # Handling of the former look the same.
1638 { set dump_prog $objdump }
1640 { set dump_prog $nm }
1642 { set dump_prog $READELF }
1645 perror "Unrecognized action $action"
1651 if { $dump_prog != "" } {
1652 set dumpfile [lindex $actionlist 2]
1653 set binary $dump_prog
1655 # Ensure consistent sorting of symbols
1656 if {[info exists env(LC_ALL)]} {
1657 set old_lc_all $env(LC_ALL)
1660 set cmd "$binary $progopts $binfile > dump.out"
1662 catch "exec $cmd" comp_output
1663 if {[info exists old_lc_all]} {
1664 set env(LC_ALL) $old_lc_all
1668 set comp_output [prune_warnings $comp_output]
1670 if ![string match "" $comp_output] then {
1671 send_log "$comp_output\n"
1676 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1677 verbose "output is [file_contents "dump.out"]" 2
1687 } elseif { $is_unresolved } {
1688 unresolved $testname
1695 # Returns true if --gc-sections is supported on the target.
1697 proc check_gc_sections_available { } {
1698 global gc_sections_available_saved
1701 if {![info exists gc_sections_available_saved]} {
1702 # Some targets don't support gc-sections despite whatever's
1703 # advertised by ld's options.
1704 if { [istarget d30v-*-*]
1705 || [istarget dlx-*-*]
1706 || [istarget i960-*-*]
1707 || [istarget pj*-*-*]
1708 || [istarget pru*-*-*]
1709 || [istarget alpha-*-*]
1710 || [istarget hppa*64-*-*]
1711 || [istarget i370-*-*]
1712 || [istarget i860-*-*]
1713 || [istarget ia64-*-*]
1714 || [istarget mep-*-*]
1715 || [istarget mn10200-*-*] } {
1716 set gc_sections_available_saved 0
1720 # elf2flt uses -q (--emit-relocs), which is incompatible with
1722 if { [board_info target exists ldflags]
1723 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1724 set gc_sections_available_saved 0
1728 # Check if the ld used by gcc supports --gc-sections.
1729 # FIXME: this test is useless since ld --help always says
1730 # --gc-sections is available
1731 set ld_output [remote_exec host $ld "--help"]
1732 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1733 set gc_sections_available_saved 1
1735 set gc_sections_available_saved 0
1738 return $gc_sections_available_saved
1741 # Returns true if -shared is supported on the target
1742 # Only used and accurate for ELF targets at the moment
1744 proc check_shared_lib_support { } {
1745 if {![istarget aarch64*-*-elf]
1746 && ![istarget arc*-*-elf*]
1747 && ![istarget arm*-*-elf]
1748 && ![istarget avr-*-*]
1749 && ![istarget cr16-*-*]
1750 && ![istarget cris*-*-elf]
1751 && ![istarget crx-*-*]
1752 && ![istarget d10v-*-*]
1753 && ![istarget d30v-*-*]
1754 && ![istarget dlx-*-*]
1755 && ![istarget epiphany-*-*]
1756 && ![istarget fr30-*-*]
1757 && ![istarget frv-*-*]
1758 && ![istarget ft32-*-*]
1759 && ![istarget h8300-*-*]
1760 && ![istarget i860-*-*]
1761 && ![istarget i960-*-*]
1762 && ![istarget ip2k-*-*]
1763 && ![istarget iq2000-*-*]
1764 && ![istarget lm32-*-*]
1765 && ![istarget m32c-*-*]
1766 && ![istarget m32r-*-*]
1767 && ![istarget m6811-*-*]
1768 && ![istarget m6812-*-*]
1769 && ![istarget m68hc1*-*-*]
1770 && ![istarget mcore*-*-*]
1771 && ![istarget mep-*-*]
1772 && ![istarget microblaze-*-*]
1773 && ![istarget mips*-*-elf]
1774 && ![istarget mn10200-*-*]
1775 && ![istarget moxie-*-*]
1776 && ![istarget msp430-*-*]
1777 && ![istarget mt-*-*]
1778 && ![istarget nds32*-*-*]
1779 && ![istarget or1k*-*-*]
1780 && ![istarget pj-*-*]
1781 && ![istarget pru-*-*]
1782 && ![istarget rl78-*-*]
1783 && ![istarget rx-*-*]
1784 && ![istarget spu-*-*]
1785 && ![istarget v850*-*-*]
1786 && ![istarget visium-*-*]
1787 && ![istarget xc16x-*-elf]
1788 && ![istarget xgate-*-*]
1789 && ![istarget xstormy16-*-*]
1790 && ![istarget *-*-irix*]
1791 && ![istarget *-*-rtems] } {
1797 # Return true if target uses genelf.em (assuming it is ELF).
1798 proc is_generic_elf { } {
1799 if { [istarget "d30v-*-*"]
1800 || [istarget "dlx-*-*"]
1801 || [istarget "fr30-*-*"]
1802 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1803 || [istarget "ft32-*-*"]
1804 || [istarget "i860-*-*"]
1805 || [istarget "i960-*-*"]
1806 || [istarget "iq2000-*-*"]
1807 || [istarget "mn10200-*-*"]
1808 || [istarget "moxie-*-*"]
1809 || [istarget "msp430-*-*"]
1810 || [istarget "mt-*-*"]
1811 || [istarget "pj*-*-*"] } {
1817 # Returns true if the target ld supports the plugin API.
1818 proc check_plugin_api_available { } {
1819 global plugin_api_available_saved
1821 if {![info exists plugin_api_available_saved]} {
1822 # Check if the ld used by gcc supports --plugin.
1823 set ld_output [remote_exec host $ld "--help"]
1824 if { [ string first "-plugin PLUGIN" $ld_output ] >= 0 } {
1825 set plugin_api_available_saved 1
1827 set plugin_api_available_saved 0
1830 return $plugin_api_available_saved
1833 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1834 # returns true if the target ld supports sysroot.
1835 proc check_sysroot_available { } {
1836 global ld_sysroot_available_saved ld ld_sysroot
1837 if {![info exists ld_sysroot_available_saved]} {
1838 # Check if ld supports --sysroot *other* than empty.
1839 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1840 if { $ld_sysroot == "" } {
1841 set ld_sysroot_available_saved 0
1843 set ld_sysroot_available_saved 1
1846 return $ld_sysroot_available_saved
1849 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1850 proc check_gcc_plugin_enabled { } {
1853 if {![info exists CC]} {
1859 set state [remote_exec host $CC -v]
1860 if { [lindex $state 0] != 0 } {
1863 for { set i 1 } { $i < [llength $state] } { incr i } {
1864 set v [lindex $state $i]
1865 if { [ string match "*--disable-plugin*" $v ] } {
1866 verbose "plugin is disabled by $v"
1874 # Returns true if the target compiler supports LTO
1875 proc check_lto_available { } {
1876 global lto_available_saved
1879 if {![info exists lto_available_saved]} {
1880 if { ![check_gcc_plugin_enabled] } {
1881 set lto_available_saved 0
1884 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1885 # -ffat-lto-objects, we always run LTO tests on Linux with
1887 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1888 set lto_available_saved 1
1891 # Check if gcc supports -flto -fuse-linker-plugin
1893 if [board_info [target_info name] exists cflags] {
1894 append flags " [board_info [target_info name] cflags]"
1896 if [board_info [target_info name] exists ldflags] {
1897 append flags " [board_info [target_info name] ldflags]"
1900 set basename "tmpdir/lto[pid]"
1901 set src ${basename}.c
1902 set output ${basename}.out
1903 set f [open $src "w"]
1904 puts $f "int main() { return 0; }"
1906 if [is_remote host] {
1907 set src [remote_download host $src]
1909 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1910 remote_file host delete $src
1911 remote_file host delete $output
1914 return $lto_available_saved
1917 # Returns true if the target compiler supports LTO -ffat-lto-objects
1918 proc check_lto_fat_available { } {
1919 global lto_fat_available_saved
1922 if {![info exists lto_fat_available_saved]} {
1923 if { ![check_gcc_plugin_enabled] } {
1924 set lto_fat_available_saved 0
1927 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1928 # -ffat-lto-objects, we always run LTO tests on Linux with
1930 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1931 set lto_fat_available_saved 1
1934 # Check if gcc supports -flto -fuse-linker-plugin
1936 if [board_info [target_info name] exists cflags] {
1937 append flags " [board_info [target_info name] cflags]"
1939 if [board_info [target_info name] exists ldflags] {
1940 append flags " [board_info [target_info name] ldflags]"
1943 set basename "tmpdir/lto[pid]"
1944 set src ${basename}.c
1945 set output ${basename}.out
1946 set f [open $src "w"]
1947 puts $f "int main() { return 0; }"
1949 if [is_remote host] {
1950 set src [remote_download host $src]
1952 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
1953 remote_file host delete $src
1954 remote_file host delete $output
1957 return $lto_fat_available_saved
1960 # Returns true if the target compiler supports LTO and -shared
1961 proc check_lto_shared_available { } {
1962 global lto_shared_available_saved
1965 if {![info exists lto_shared_available_saved]} {
1966 if { ![check_gcc_plugin_enabled] } {
1967 set lto_shared_available_saved 0
1970 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1971 # -ffat-lto-objects, we always run LTO tests on Linux with
1973 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1974 set lto_shared_available_saved 1
1977 # Check if gcc supports -flto -fuse-linker-plugin -shared
1979 if [board_info [target_info name] exists cflags] {
1980 append flags " [board_info [target_info name] cflags]"
1982 if [board_info [target_info name] exists ldflags] {
1983 append flags " [board_info [target_info name] ldflags]"
1986 set basename "tmpdir/lto_shared[pid]"
1987 set src ${basename}.c
1988 set output ${basename}.so
1989 set f [open $src "w"]
1992 if [is_remote host] {
1993 set src [remote_download host $src]
1995 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
1996 remote_file host delete $src
1997 remote_file host delete $output
2000 return $lto_shared_available_saved
2003 # Check if the assembler supports CFI statements.
2005 proc check_as_cfi { } {
2006 global check_as_cfi_result
2008 if [info exists check_as_cfi_result] {
2009 return $check_as_cfi_result
2011 set as_file "tmpdir/check_as_cfi.s"
2012 set as_fh [open $as_file w 0666]
2013 puts $as_fh "# Generated file. DO NOT EDIT"
2014 puts $as_fh "\t.cfi_startproc"
2015 puts $as_fh "\t.cfi_endproc"
2017 remote_download host $as_file
2018 verbose -log "Checking CFI support:"
2019 rename "perror" "check_as_cfi_perror"
2020 proc perror { args } { }
2021 set success [ld_assemble $as $as_file "/dev/null"]
2023 rename "check_as_cfi_perror" "perror"
2024 #remote_file host delete $as_file
2025 set check_as_cfi_result $success
2029 # Returns true if IFUNC works.
2031 proc check_ifunc_available { } {
2032 global ifunc_available_saved
2035 if {![info exists ifunc_available_saved]} {
2036 if { [which $CC] == 0 } {
2037 set ifunc_available_saved 0
2040 # Check if gcc supports -flto -fuse-linker-plugin
2042 if [board_info [target_info name] exists cflags] {
2043 append flags " [board_info [target_info name] cflags]"
2045 if [board_info [target_info name] exists ldflags] {
2046 append flags " [board_info [target_info name] ldflags]"
2049 set basename "tmpdir/ifunc[pid]"
2050 set src ${basename}.c
2051 set output ${basename}.out
2052 set f [open $src "w"]
2053 puts $f "extern int library_func2 (void);"
2054 puts $f "int main (void)"
2056 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2057 puts $f " return 0; "
2059 puts $f "static int library_func1 (void) {return 2; }"
2060 puts $f "void *foo (void) __asm__ (\"library_func2\");"
2061 puts $f "void *foo (void) { return library_func1; }"
2062 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
2064 if [is_remote host] {
2065 set src [remote_download host $src]
2067 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2068 if { $ifunc_available_saved == 1 } {
2069 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
2071 remote_file host delete $src
2072 remote_file host delete $output
2075 return $ifunc_available_saved
2078 # Returns true if ifunc attribute works.
2080 proc check_ifunc_attribute_available { } {
2081 global ifunc_attribute_available_saved
2084 if {![info exists ifunc_attribute_available_saved]} {
2085 if { [which $CC] == 0 } {
2086 set ifunc_attribute_available_saved 0
2089 # Check if gcc supports -flto -fuse-linker-plugin
2091 if [board_info [target_info name] exists cflags] {
2092 append flags " [board_info [target_info name] cflags]"
2094 if [board_info [target_info name] exists ldflags] {
2095 append flags " [board_info [target_info name] ldflags]"
2098 set basename "tmpdir/ifunc[pid]"
2099 set src ${basename}.c
2100 set output ${basename}.out
2101 set f [open $src "w"]
2102 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
2103 puts $f "int main (void)"
2105 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2106 puts $f " return 0; "
2108 puts $f "static int library_func1 (void) {return 2; }"
2109 puts $f "void *foo (void) { return library_func1; }"
2111 if [is_remote host] {
2112 set src [remote_download host $src]
2114 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2115 if { $ifunc_attribute_available_saved == 1 } {
2116 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
2118 remote_file host delete $src
2119 remote_file host delete $output
2122 return $ifunc_attribute_available_saved
2125 # Provide virtual target "cfi" for targets supporting CFI.
2127 rename "istarget" "istarget_ld"
2128 proc istarget { target } {
2129 if {$target == "cfi"} {
2130 return [check_as_cfi]
2132 return [istarget_ld $target]