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 } {
209 if [is_endian_output_format $objects] then {
210 set flags [big_or_little_endian]
213 remote_file host delete $target
214 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
215 set exec_output [prune_warnings $exec_output]
217 # We don't care if we get a warning about a non-existent start
218 # symbol, since the default linker script might use ENTRY.
219 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
221 return [string match "" $exec_output]
224 # Compile an object using cc.
226 proc default_ld_compile { cc source object } {
235 if {[llength $cc_prog] > 1} then {
236 set cc_prog [lindex $cc_prog 0]
238 if {![is_remote host] && [which $cc_prog] == 0} then {
239 perror "$cc_prog does not exist"
243 remote_file build delete "$object"
244 remote_file host delete "$object"
246 set flags "$gcc_B_opt -I$srcdir/$subdir"
248 # If we are compiling with gcc, we want to add gcc_B_opt to flags.
249 # However, if $prog already has -B options, which might be the
250 # case when running gcc out of a build directory, we want our -B
251 # options to come first.
253 set ccparm [string first " " $cc]
255 if { $ccparm > 0 } then {
256 set ccflags [string range $cc $ccparm end]
257 set ccexe [string range $cc 0 $ccparm]
261 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
262 if {[string match "*++*" $ccexe]} {
263 append flags " $CXXFLAGS"
265 append flags " $CFLAGS"
268 if [board_info [target_info name] exists cflags] {
269 append flags " [board_info [target_info name] cflags]"
272 if [board_info [target_info name] exists multilib_flags] {
273 append flags " [board_info [target_info name] multilib_flags]"
276 set cmd "$cc $flags $ccflags -c $source -o $object"
279 set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
280 remote_upload host "ld.tmp"
281 set exec_output [file_contents "ld.tmp"]
282 remote_file build delete "ld.tmp"
283 remote_file host delete "ld.tmp"
284 set exec_output [prune_warnings $exec_output]
285 if [string match "" $exec_output] then {
286 if {![file exists $object]} then {
287 regexp ".*/(\[^/\]*)$" $source all dobj
288 regsub "\\.c" $dobj ".o" realobj
289 verbose "looking for $realobj"
290 if {[remote_file host exists $realobj]} then {
291 verbose -log "mv $realobj $object"
292 remote_upload "$realobj" "$object"
294 perror "$object not found after compilation"
300 verbose -log "$exec_output"
301 perror "$source: compilation failed"
308 proc default_ld_assemble { as in_flags source object } {
314 if ![info exists ASFLAGS] { set ASFLAGS "" }
316 set flags "[big_or_little_endian] -I$srcdir/$subdir"
317 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
318 set exec_output [prune_warnings $exec_output]
319 if [string match "" $exec_output] then {
322 perror "$source: assembly failed"
327 # Run nm on a file, putting the result in the array nm_output.
329 proc default_ld_nm { nm nmflags object } {
334 if {[info exists nm_output]} {
338 if ![info exists NMFLAGS] { set NMFLAGS "" }
340 # Ensure consistent sorting of symbols
341 if {[info exists env(LC_ALL)]} {
342 set old_lc_all $env(LC_ALL)
346 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
348 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
349 if {[info exists old_lc_all]} {
350 set env(LC_ALL) $old_lc_all
354 remote_upload host "ld.stderr"
355 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
356 set exec_output [prune_warnings [file_contents "ld.stderr"]]
357 remote_file host delete "ld.stderr"
358 remote_file build delete "ld.stderr"
359 if [string match "" $exec_output] then {
360 set file [open tmpdir/nm.out r]
361 while { [gets $file line] != -1 } {
363 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
364 set name [string trimleft $name "_"]
365 verbose "Setting nm_output($name) to 0x$value" 2
366 set nm_output($name) 0x$value
372 verbose -log "$exec_output"
373 perror "$object: nm failed"
378 # Define various symbols needed when not linking against all
380 proc ld_link_defsyms {} {
382 set flags "--defsym __stack_chk_fail=0"
384 # ARM targets call __gccmain
385 if {[istarget arm*-*-*]} {
386 append flags " --defsym __gccmain=0"
389 # Windows targets need __main, some prefixed with underscore.
390 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
391 append flags " --defsym __main=0 --defsym ___main=0"
394 # PowerPC EABI code calls __eabi.
395 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
396 append flags " --defsym __eabi=0"
399 # mn10200 code calls __truncsipsi2_d0_d2.
400 if {[istarget mn10200*-*-*]} then {
401 append flags " --defsym __truncsipsi2_d0_d2=0"
404 # m6811/m6812 code has references to soft registers.
405 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
406 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
407 append flags " --defsym _.d3=0 --defsym _.d4=0"
408 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
411 # Some OpenBSD targets have ProPolice and reference __guard and
412 # __stack_smash_handler.
413 if [istarget *-*-openbsd*] {
414 append flags " --defsym __guard=0"
415 append flags " --defsym __stack_smash_handler=0"
421 # run_dump_test FILE (optional:) EXTRA_OPTIONS
422 # Copied from gas testsuite, tweaked and further extended.
424 # Assemble a .s file, then run some utility on it and check the output.
426 # There should be an assembly language file named FILE.s in the test
427 # suite directory, and a pattern file called FILE.d. `run_dump_test'
428 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
429 # `nm' on the .o file to produce textual output, and then analyze that
430 # with regexps. The FILE.d file specifies what program to run, and
431 # what to expect in its output.
433 # The FILE.d file begins with zero or more option lines, which specify
434 # flags to pass to the assembler, the program to run to dump the
435 # assembler's output, and the options it wants. The option lines have
440 # OPTION is the name of some option, like "name" or "objdump", and
441 # VALUE is OPTION's value. The valid options are described below.
442 # Whitespace is ignored everywhere, except within VALUE. The option
443 # list ends with the first line that doesn't match the above syntax
444 # (hmm, not great for error detection).
446 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
447 # two-element lists. The first element of each is an option name, and
448 # the second additional arguments to be added on to the end of the
449 # option list as given in FILE.d. (If omitted, no additional options
452 # The interesting options are:
455 # The name of this test, passed to DejaGNU's `pass' and `fail'
456 # commands. If omitted, this defaults to FILE, the root of the
457 # .s and .d files' names.
460 # When assembling, pass FLAGS to the assembler.
461 # If assembling several files, you can pass different assembler
462 # options in the "source" directives. See below.
465 # Link assembled files using FLAGS, in the order of the "source"
466 # directives, when using multiple files.
468 # ld_after_inputfiles: FLAGS
469 # Similar to "ld", but put after all input files.
471 # objcopy_objects: FLAGS
472 # Run objcopy with the specified flags after assembling any source
473 # that has the special marker RUN_OBJCOPY in the source specific
476 # objcopy_linked_file: FLAGS
477 # Run objcopy on the linked file with the specified flags.
478 # This lets you transform the linked file using objcopy, before the
479 # result is analyzed by an analyzer program specified below (which
480 # may in turn *also* be objcopy).
483 # The name of the program to run to analyze the .o file produced
484 # by the assembler or the linker output. This can be omitted;
485 # run_dump_test will guess which program to run by seeing which of
486 # the flags options below is present.
492 # Use the specified program to analyze the assembler or linker
493 # output file, and pass it FLAGS, in addition to the output name.
494 # Note that they are run with LC_ALL=C in the environment to give
495 # consistent sorting of symbols.
497 # source: SOURCE [FLAGS]
498 # Assemble the file SOURCE.s using the flags in the "as" directive
499 # and the (optional) FLAGS. If omitted, the source defaults to
501 # This is useful if several .d files want to share a .s file.
502 # More than one "source" directive can be given, which is useful
503 # when testing linking.
506 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
507 # is useful if several .d files differ by options only. Options are
508 # always read from FILE.d.
511 # The test is expected to fail on TARGET. This may occur more than
515 # Only run the test for TARGET. This may occur more than once; the
516 # target being tested must match at least one. You may provide target
517 # name "cfi" for any target supporting the CFI statements.
520 # Do not run the test for TARGET. This may occur more than once;
521 # the target being tested must not match any of them.
524 # An error with message matching REGEX must be emitted for the test
525 # to pass. The PROG, readelf, objdump, nm and objcopy options have
526 # no meaning and need not be supplied if this is present. Multiple
527 # "error" directives append to the expected linker error message.
530 # Means the same as 'error', except the regular expression lines
531 # are contains in FILE.
534 # Expect a linker warning matching REGEX. It is an error to issue
535 # both "error" and "warning". Multiple "warning" directives
536 # append to the expected linker warning message.
538 # warning_output: FILE
539 # Means the same as 'warning', except the regular expression
540 # lines are contains in FILE.
543 # Adding this option will cause the linker to generate a linker
544 # map file, using the -Map=MAPFILE command line option. If
545 # there is no -Map=MAPFILE in the 'ld: FLAGS' then one will be
546 # added to the linker command line. The contents of the
547 # generated MAPFILE are then compared against the regexp lines
548 # in FILE using `regexp_diff' (see below for details).
550 # Each option may occur at most once unless otherwise mentioned.
552 # After the option lines come regexp lines. `run_dump_test' calls
553 # `regexp_diff' to compare the output of the dumping tool against the
554 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
555 # see further comments there.
557 proc run_dump_test { name {extra_options {}} } {
559 global OBJDUMP NM AS OBJCOPY READELF LD
560 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
561 global host_triplet runtests
563 global ld_elf_shared_opt
565 if { [is_elf_format] && [check_shared_lib_support] } {
566 set ld_extra_opt "$ld_elf_shared_opt"
571 if [string match "*/*" $name] {
573 set name [file tail $name]
575 set file "$srcdir/$subdir/$name"
578 if ![runtest_file_p $runtests $name] then {
582 set opt_array [slurp_options "${file}.d"]
583 if { $opt_array == -1 } {
584 perror "error reading options from $file.d"
585 unresolved $subdir/$name
588 set dumpfile tmpdir/dump.out
594 set opts(ld_after_inputfiles) {}
597 set opts(notarget) {}
608 set opts(error_output) {}
609 set opts(warning_output) {}
610 set opts(objcopy_linked_file) {}
611 set opts(objcopy_objects) {}
614 foreach i $opt_array {
615 set opt_name [lindex $i 0]
616 set opt_val [lindex $i 1]
617 if ![info exists opts($opt_name)] {
618 perror "unknown option $opt_name in file $file.d"
619 unresolved $subdir/$name
623 switch -- $opt_name {
630 # Move any source-specific as-flags to a separate list to
631 # simplify processing.
632 if { [llength $opt_val] > 1 } {
633 lappend asflags [lrange $opt_val 1 end]
634 set opt_val [lindex $opt_val 0]
639 # Create the object file name based on nothing but the source
642 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]].o]
643 # But, sometimes, we have the exact same source filename in
644 # different directories (foo/src.s bar/src.s) which would lead
645 # us to try and create two src.o files. We detect this
646 # conflict here, and instead create src.o and src1.o.
648 while { [lsearch $objfile_names $new_objfile] != -1 } {
651 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]]${j}.o]
653 lappend objfile_names $new_objfile
656 if [string length $opts($opt_name)] {
657 perror "option $opt_name multiply set in $file.d"
658 unresolved $subdir/$name
662 # A single "# ld:" with no options should do the right thing.
663 if { $opt_name == "ld" } {
666 # Likewise objcopy_linked_file.
667 if { $opt_name == "objcopy_linked_file" } {
672 if { $opt_name == "as" || $opt_name == "ld" } {
673 set opt_val [subst $opt_val]
676 # Append differently whether it's a message (without space) or
677 # an option or list (with space).
678 switch -- $opt_name {
681 append opts($opt_name) $opt_val
684 set opts($opt_name) [concat $opts($opt_name) $opt_val]
689 foreach i $extra_options {
690 set opt_name [lindex $i 0]
691 set opt_val [lindex $i 1]
692 if ![info exists opts($opt_name)] {
693 perror "unknown option $opt_name given in extra_opts"
694 unresolved $subdir/$name
697 # Add extra option to end of existing option, adding space
699 if { ![regexp "warning|error" $opt_name]
700 && [string length $opts($opt_name)] } {
701 append opts($opt_name) " "
703 append opts($opt_name) $opt_val
706 foreach opt { as ld } {
707 regsub {\[big_or_little_endian\]} $opts($opt) \
708 [big_or_little_endian] opts($opt)
711 # Decide early whether we should run the test for this target.
712 if { [llength $opts(target)] > 0 } {
714 foreach targ $opts(target) {
715 if [istarget $targ] {
720 if { $targmatch == 0 } {
724 foreach targ $opts(notarget) {
725 if [istarget $targ] {
731 # It's meaningless to require an output-testing method when we
733 if { $opts(error) == "" && $opts(error_output) == "" } {
734 if {$opts(PROG) != ""} {
735 switch -- $opts(PROG) {
736 objdump { set program objdump }
737 nm { set program nm }
738 objcopy { set program objcopy }
739 readelf { set program readelf }
741 { perror "unrecognized program option $opts(PROG) in $file.d"
742 unresolved $subdir/$name
746 # Guess which program to run, by seeing which option was specified.
747 foreach p {objdump objcopy nm readelf} {
748 if {$opts($p) != ""} {
749 if {$program != ""} {
750 perror "ambiguous dump program in $file.d"
751 unresolved $subdir/$name
759 if { $program == "" \
760 && $opts(map) == "" \
761 && $opts(warning) == "" \
762 && $opts(warning_output) == "" \
763 && $opts(error) == "" \
764 && $opts(error_output) == "" } {
765 perror "dump program unspecified in $file.d"
766 unresolved $subdir/$name
771 if { $opts(name) == "" } {
772 set testname "$subdir/$name"
774 set testname $opts(name)
777 if { $opts(source) == "" } {
778 set sourcefiles [list ${file}.s]
779 set asflags [list ""]
780 set objfile_names [list tmpdir/[file tail ${file}].o]
783 foreach sf $opts(source) {
784 if { [string match "/*" $sf] } {
785 lappend sourcefiles "$sf"
787 lappend sourcefiles "$srcdir/$subdir/$sf"
792 if { $opts(dump) == "" } {
795 set dfile $srcdir/$subdir/$opts(dump)
798 # Time to setup xfailures.
799 foreach targ $opts(xfail) {
803 # Assemble each file.
805 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
806 set sourcefile [lindex $sourcefiles $i]
807 set sourceasflags [lindex $asflags $i]
808 set run_objcopy_objects 0
810 if { [string match "*RUN_OBJCOPY*" $sourceasflags] } {
811 set run_objcopy_objects 1
813 regsub "RUN_OBJCOPY" $sourceasflags "" sourceasflags
815 set objfile [lindex $objfile_names $i]
816 catch "exec rm -f $objfile" exec_output
817 lappend objfiles $objfile
818 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
821 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
822 remote_upload host "ld.tmp"
823 set comp_output [prune_warnings [file_contents "ld.tmp"]]
824 remote_file host delete "ld.tmp"
825 remote_file build delete "ld.tmp"
827 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
828 send_log -- "$comp_output\n"
829 verbose "$comp_output" 3
831 set exitstat "succeeded"
832 if { $cmdret != 0 } { set exitstat "failed" }
833 verbose -log "$exitstat with: <$comp_output>"
838 if { $run_objcopy_objects } {
839 set cmd "$OBJCOPY $opts(objcopy_objects) $objfile"
842 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] \
843 "" "/dev/null" "objcopy.tmp"]
844 remote_upload host "objcopy.tmp"
845 set comp_output [prune_warnings [file_contents "objcopy.tmp"]]
846 remote_file host delete "objcopy.tmp"
847 remote_file build delete "objcopy.tmp"
849 if { [lindex $cmdret 0] != 0 \
850 || ![string match "" $comp_output] } {
851 send_log -- "$comp_output\n"
852 verbose "$comp_output" 3
854 set exitstat "succeeded"
855 if { $cmdret != 0 } { set exitstat "failed" }
856 verbose -log "$exitstat with: <$comp_output>"
863 if { (($opts(warning) != "") && ($opts(error) != "")) \
864 || (($opts(warning) != "") && ($opts(error_output) != "")) \
865 || (($opts(warning) != "") && ($opts(warning_output) != "")) \
866 || (($opts(error) != "") && ($opts(warning_output) != "")) \
867 || (($opts(error) != "") && ($opts(error_output) != "")) \
868 || (($opts(warning_output) != "") && ($opts(error_output) != "")) } {
869 perror "$testname: bad mix of warning, error, warning_output, and error_output test-directives"
874 set check_ld(source) ""
875 set check_ld(terminal) 0
876 if { $opts(error) != "" \
877 || $opts(warning) != "" \
878 || $opts(error_output) != "" \
879 || $opts(warning_output) != "" } {
881 if { $opts(error) != "" || $opts(error_output) != "" } {
882 set check_ld(terminal) 1
884 set check_ld(terminal) 0
887 if { $opts(error) != "" || $opts(warning) != "" } {
888 set check_ld(source) "regex"
889 if { $opts(error) != "" } {
890 set check_ld(regex) $opts(error)
892 set check_ld(regex) $opts(warning)
895 set check_ld(source) "file"
896 if { $opts(error_output) != "" } {
897 set check_ld(file) $opts(error_output)
899 set check_ld(file) $opts(warning_output)
904 # Perhaps link the file(s).
906 set objfile "tmpdir/dump"
907 catch "exec rm -f $objfile" exec_output
909 # Add -L$srcdir/$subdir so that the linker command can use
910 # linker scripts in the source directory.
911 set cmd "$LD $ld_extra_opt $LDFLAGS -L$srcdir/$subdir \
912 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
914 # If needed then check for, or add a -Map option.
916 if { $opts(map) != "" } then {
917 if { [regexp -- "-Map=(\[^ \]+)" $cmd all mapfile] } then {
918 # Found existing mapfile option
919 verbose -log "Existing mapfile '$mapfile' found"
922 set mapfile "tmpdir/dump.map"
923 verbose -log "Adding mapfile '$mapfile'"
924 set cmd "$cmd -Map=$mapfile"
929 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
930 remote_upload host "ld.tmp"
931 set comp_output [file_contents "ld.tmp"]
932 remote_file host delete "ld.tmp"
933 remote_file build delete "ld.tmp"
934 set cmdret [lindex $cmdret 0]
936 if { $cmdret == 0 && $run_objcopy } {
938 set objfile "tmpdir/dump1"
939 remote_file host delete $objfile
941 # Note that we don't use OBJCOPYFLAGS here; any flags must be
942 # explicitly specified.
943 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
946 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
947 remote_upload host "ld.tmp"
948 append comp_output [file_contents "ld.tmp"]
949 remote_file host delete "ld.tmp"
950 remote_file build delete "ld.tmp"
951 set cmdret [lindex $cmdret 0]
954 regsub "\n$" $comp_output "" comp_output
955 if { $cmdret != 0 || $comp_output != "" || $check_ld(source) != "" } then {
956 set exitstat "succeeded"
957 if { $cmdret != 0 } { set exitstat "failed" }
959 if { $check_ld(source) == "regex" } {
960 verbose -log "$exitstat with: <$comp_output>, expected: <$check_ld(regex)>"
961 } elseif { $check_ld(source) == "file" } {
962 verbose -log "$exitstat with: <$comp_output>, expected in file $check_ld(file)"
963 set_file_contents "tmpdir/ld.messages" "$comp_output"
965 verbose -log "$exitstat with: <$comp_output>, no expected output"
967 send_log -- "$comp_output\n"
968 verbose "$comp_output" 3
970 if { (($check_ld(source) == "") == ($comp_output == "")) \
971 && (($cmdret == 0) == ($check_ld(terminal) == 0)) \
972 && ((($check_ld(source) == "regex") \
973 && ($check_ld(regex) == "") == ($comp_output == "") \
974 && [regexp $check_ld(regex) $comp_output]) \
975 || (($check_ld(source) == "file") \
976 && (![regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"]))) } {
977 # We have the expected output from ld.
978 if { $check_ld(terminal) || $program == "" } {
988 if { $opts(map) != "" } then {
989 # Check the map file matches.
990 set map_pattern_file $srcdir/$subdir/$opts(map)
991 verbose -log "Compare '$mapfile' against '$map_pattern_file'"
992 if { [regexp_diff $mapfile $map_pattern_file] } then {
993 fail "$testname (map file check)"
995 pass "$testname (map file check)"
998 if { $program == "" } then {
1003 set objfile [lindex $objfiles 0]
1006 # We must not have expected failure if we get here.
1007 if { $opts(error) != "" } {
1012 set progopts1 $opts($program)
1013 eval set progopts \$[string toupper $program]FLAGS
1014 eval set binary \$[string toupper $program]
1016 if { ![is_remote host] && [which $binary] == 0 } {
1021 if { $progopts1 == "" } { set $progopts1 "-r" }
1022 verbose "running $binary $progopts $progopts1" 3
1024 # Objcopy, unlike the other two, won't send its output to stdout,
1025 # so we have to run it specially.
1026 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
1027 if { $program == "objcopy" } {
1028 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
1031 # Ensure consistent sorting of symbols
1032 if {[info exists env(LC_ALL)]} {
1033 set old_lc_all $env(LC_ALL)
1037 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
1038 set cmdret [lindex $cmdret 0]
1039 remote_upload host "ld.tmp"
1040 set comp_output [prune_warnings [file_contents "ld.tmp"]]
1041 remote_file host delete "ld.tmp"
1042 remote_file build delete "ld.tmp"
1043 if {[info exists old_lc_all]} {
1044 set env(LC_ALL) $old_lc_all
1048 if { $cmdret != 0 || $comp_output != "" } {
1049 send_log "exited abnormally with $cmdret, output:$comp_output\n"
1054 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
1055 if { [regexp_diff $dumpfile "${dfile}"] } then {
1057 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
1064 proc slurp_options { file } {
1065 # If options_regsub(foo) is set to {a b}, then the contents of a
1066 # "#foo:" line will have regsub -all applied to replace a with b.
1067 global options_regsub
1069 if [catch { set f [open $file r] } x] {
1070 #perror "couldn't open `$file': $x"
1075 # whitespace expression
1078 # whitespace is ignored anywhere except within the options list;
1079 # option names are alphabetic plus underscore only.
1080 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
1081 while { [gets $f line] != -1 } {
1082 set line [string trim $line]
1083 # Whitespace here is space-tab.
1084 if [regexp $pat $line xxx opt_name opt_val] {
1086 if [info exists options_regsub($opt_name)] {
1087 set subst $options_regsub($opt_name)
1088 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
1091 lappend opt_array [list $opt_name $opt_val]
1100 proc file_contents { filename } {
1101 set file [open $filename r]
1102 set contents [read $file]
1107 proc set_file_contents { filename contents } {
1108 set file [open $filename w]
1109 puts $file "$contents"
1113 # Create an archive using ar
1115 proc ar_simple_create { ar aropts target objects } {
1116 remote_file host delete $target
1118 set exec_output [run_host_cmd "$ar" "-rc $aropts $target $objects"]
1119 set exec_output [prune_warnings $exec_output]
1121 if [string match "" $exec_output] then {
1122 send_log "$exec_output\n"
1129 # List contains test-items with 3 items followed by 2 lists, one item and
1130 # one optional item:
1132 # 1:ld/ar leading options, placed before object files
1133 # 2:ld/ar trailing options, placed after object files
1134 # 3:assembler options
1135 # 4:filenames of assembler files
1136 # 5:list of actions, options and expected outputs.
1137 # 6:name of output file
1138 # 7:compiler flags (optional)
1140 # Actions: { command command-line-options file-containg-expected-output-regexps }
1142 # objdump: Apply objdump options on result.
1143 # nm: Apply nm options on result.
1144 # readelf: Apply readelf options on result.
1145 # ld: Don't apply anything on result. Compare output during linking with
1146 # the file containing regexps (which is the second arg, not the third).
1147 # Note that this *must* be the first action if it is to be used at all;
1148 # in all other cases, any output from the linker during linking is
1149 # treated as a sign of an error and FAILs the test.
1151 # args is an optional list of target triplets to be xfailed.
1153 proc run_ld_link_tests { ldtests args } {
1167 global ld_elf_shared_opt
1169 if { [is_elf_format] && [check_shared_lib_support] } {
1170 set ld_extra_opt "$ld_elf_shared_opt"
1175 foreach testitem $ldtests {
1176 set testname [lindex $testitem 0]
1178 if ![runtest_file_p $runtests $testname] then {
1182 foreach target $args {
1186 set ld_options [lindex $testitem 1]
1187 set ld_after [lindex $testitem 2]
1188 set as_options [lindex $testitem 3]
1189 set src_files [lindex $testitem 4]
1190 set actions [lindex $testitem 5]
1191 set binfile tmpdir/[lindex $testitem 6]
1192 set cflags [lindex $testitem 7]
1199 # verbose -log "Testname is $testname"
1200 # verbose -log "ld_options is $ld_options"
1201 # verbose -log "ld_after is $ld_after"
1202 # verbose -log "as_options is $as_options"
1203 # verbose -log "src_files is $src_files"
1204 # verbose -log "actions is $actions"
1205 # verbose -log "binfile is $binfile"
1207 # Assemble each file in the test.
1208 foreach src_file $src_files {
1209 set fileroot "[file rootname [file tail $src_file]]"
1210 set objfile "tmpdir/$fileroot.o"
1211 lappend objfiles $objfile
1213 if { [file extension $src_file] == ".c" } {
1214 set as_file "tmpdir/$fileroot.s"
1215 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1220 set as_file "$srcdir/$subdir/$src_file"
1222 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1228 # Catch assembler errors.
1229 if { $is_unresolved } {
1230 unresolved $testname
1234 if { $binfile eq "tmpdir/" } {
1236 } elseif { [regexp ".*\\.a$" $binfile] } {
1237 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
1240 } elseif { ![ld_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
1242 set ld_output "$exec_output"
1246 foreach actionlist $actions {
1247 set action [lindex $actionlist 0]
1248 set progopts [lindex $actionlist 1]
1250 # There are actions where we run regexp_diff on the
1251 # output, and there are other actions (presumably).
1252 # Handling of the former look the same.
1256 { set dump_prog $objdump }
1258 { set dump_prog $nm }
1260 { set dump_prog $READELF }
1262 { set dump_prog "ld" }
1265 perror "Unrecognized action $action"
1271 if { $action == "ld" } {
1272 set regexpfile $progopts
1273 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1274 set_file_contents "tmpdir/ld.messages" "$ld_output"
1275 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1276 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1277 verbose "output is $ld_output" 2
1282 } elseif { !$maybe_failed && $dump_prog != "" } {
1283 set dumpfile [lindex $actionlist 2]
1284 set binary $dump_prog
1286 # Ensure consistent sorting of symbols
1287 if {[info exists env(LC_ALL)]} {
1288 set old_lc_all $env(LC_ALL)
1291 set cmd "$binary $progopts $binfile"
1292 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1294 remote_upload host "ld.stderr"
1295 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1296 remote_file host delete "ld.stderr"
1297 remote_file build delete "ld.stderr"
1299 if {[info exists old_lc_all]} {
1300 set env(LC_ALL) $old_lc_all
1305 if ![string match "" $comp_output] then {
1306 send_log "$comp_output\n"
1311 remote_upload host "dump.out"
1313 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1314 verbose "output is [file_contents "dump.out"]" 2
1316 remote_file build delete "dump.out"
1317 remote_file host delete "dump.out"
1320 remote_file build delete "dump.out"
1321 remote_file host delete "dump.out"
1326 if { $is_unresolved } {
1327 unresolved $testname
1328 } elseif { $maybe_failed || $failed } {
1336 # This definition is taken from an unreleased version of DejaGnu. Once
1337 # that version gets released, and has been out in the world for a few
1338 # months at least, it may be safe to delete this copy.
1339 if ![string length [info proc prune_warnings]] {
1341 # prune_warnings -- delete various system verbosities from TEXT
1344 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1346 # Sites with particular verbose os's may wish to override this in site.exp.
1348 proc prune_warnings { text } {
1349 # This is from sun4's. Do it for all machines for now.
1350 # The "\\1" is to try to preserve a "\n" but only if necessary.
1351 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1353 # It might be tempting to get carried away and delete blank lines, etc.
1354 # Just delete *exactly* what we're ask to, and that's it.
1359 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1360 # and 3 optional items:
1363 # 2:assembler options
1364 # 3:filenames of source files
1365 # 4:name of output file
1367 # 6:compiler flags (optional)
1368 # 7:language (optional)
1369 # 8:linker warning (optional)
1370 # args is an optional list of target triplets to be xfailed.
1372 proc run_ld_link_exec_tests { ldtests args } {
1386 # When using GCC as the linker driver, we need to specify board cflags when
1387 # linking because cflags may contain linker options. For example when
1388 # linker options are included in GCC spec files then we need the -specs
1390 if [board_info [target_info name] exists cflags] {
1391 set board_cflags " [board_info [target_info name] cflags]"
1396 foreach testitem $ldtests {
1397 foreach target $args {
1400 set testname [lindex $testitem 0]
1401 set ld_options [lindex $testitem 1]
1402 set as_options [lindex $testitem 2]
1403 set src_files [lindex $testitem 3]
1404 set binfile tmpdir/[lindex $testitem 4]
1405 set expfile [lindex $testitem 5]
1406 set cflags [lindex $testitem 6]
1407 set lang [lindex $testitem 7]
1408 set warning [lindex $testitem 8]
1412 # verbose -log "Testname is $testname"
1413 # verbose -log "ld_options is $ld_options"
1414 # verbose -log "as_options is $as_options"
1415 # verbose -log "src_files is $src_files"
1416 # verbose -log "binfile is $binfile"
1418 # Assemble each file in the test.
1419 foreach src_file $src_files {
1420 set fileroot "[file rootname [file tail $src_file]]"
1421 set objfile "tmpdir/$fileroot.o"
1422 lappend objfiles $objfile
1424 # We ignore warnings since some compilers may generate
1425 # incorrect section attributes and the assembler will warn
1427 if { [ string match "c++" $lang ] } {
1428 set cmd "$CXX -c $CXXFLAGS $cflags"
1430 set cmd "$CC -c $CFLAGS $cflags"
1432 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
1437 if { $failed != 0 } {
1438 unresolved $testname
1442 if { [ string match "c++" $lang ] } {
1443 set link_proc ld_link
1446 set link_proc ld_link
1450 if { $binfile eq "tmpdir/" } {
1454 } elseif ![$link_proc $link_cmd $binfile "$board_cflags -L$srcdir/$subdir $ld_options $objfiles"] {
1458 # Check if exec_output is expected.
1459 if { $warning != "" } then {
1460 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1461 if { [regexp $warning $exec_output] } then {
1468 if { $failed == 0 && [isnative] } {
1469 send_log "Running: $binfile > $binfile.out\n"
1470 verbose "Running: $binfile > $binfile.out"
1471 catch "exec $binfile > $binfile.out" exec_output
1473 if ![string match "" $exec_output] then {
1474 send_log "$exec_output\n"
1475 verbose "$exec_output" 1
1478 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1479 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1480 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1481 set exec_output [prune_warnings $exec_output]
1483 if ![string match "" $exec_output] then {
1484 send_log "$exec_output\n"
1485 verbose "$exec_output" 1
1491 if { $failed != 0 } {
1493 } elseif ![isnative] {
1494 unsupported $testname
1502 # List contains test-items with 3 items followed by 2 lists, one item and
1503 # one optional item:
1505 # 1:ld or ar options
1507 # 3:filenames of source files
1508 # 4:action and options.
1509 # 5:name of output file
1510 # 6:language (optional)
1511 # 7:linker warnings (optional)
1514 # objdump: Apply objdump options on result. Compare with regex (last arg).
1515 # nm: Apply nm options on result. Compare with regex (last arg).
1516 # readelf: Apply readelf options on result. Compare with regex (last arg).
1518 proc run_cc_link_tests { ldtests } {
1533 if [board_info [target_info name] exists cflags] {
1534 set board_cflags " [board_info [target_info name] cflags]"
1539 foreach testitem $ldtests {
1540 set testname [lindex $testitem 0]
1541 set ldflags [lindex $testitem 1]
1542 set cflags [lindex $testitem 2]
1543 set src_files [lindex $testitem 3]
1544 set actions [lindex $testitem 4]
1545 set binfile tmpdir/[lindex $testitem 5]
1546 set lang [lindex $testitem 6]
1547 set warnings [lindex $testitem 7]
1552 #verbose -log "testname is $testname"
1553 #verbose -log "ldflags is $ldflags"
1554 #verbose -log "cflags is $cflags"
1555 #verbose -log "src_files is $src_files"
1556 #verbose -log "actions is $actions"
1557 #verbose -log "binfile is $binfile"
1558 #verbose -log "lang is $lang"
1559 #verbose -log "warnings is $warnings"
1561 # Compile each file in the test.
1562 foreach src_file $src_files {
1563 set fileroot "[file rootname [file tail $src_file]]"
1564 set objfile "tmpdir/$fileroot.o"
1565 lappend objfiles $objfile
1567 # We ignore warnings since some compilers may generate
1568 # incorrect section attributes and the assembler will warn
1570 if { [ string match "c++" $lang ] } {
1571 set cmd "$CXX -c $CXXFLAGS $cflags"
1573 set cmd "$CC -c $CFLAGS $cflags"
1575 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
1580 if { $failed != 0 } {
1581 unresolved $testname
1585 # Clear error and warning counts.
1588 if { [ string match "c++" $lang ] } {
1594 if { $binfile eq "tmpdir/" } {
1596 } elseif { [regexp ".*\\.a$" $binfile] } {
1597 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1601 if { ![ld_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"] } {
1605 # Check if exec_output is expected.
1606 if { $warnings != "" } then {
1607 verbose -log "returned with: <$exec_output>, expected: <$warnings>"
1608 if { [regexp $warnings $exec_output] } then {
1616 if { $failed == 0 } {
1617 foreach actionlist $actions {
1618 set action [lindex $actionlist 0]
1619 set progopts [lindex $actionlist 1]
1621 # There are actions where we run regexp_diff on the
1622 # output, and there are other actions (presumably).
1623 # Handling of the former look the same.
1627 { set dump_prog $objdump }
1629 { set dump_prog $nm }
1631 { set dump_prog $READELF }
1634 perror "Unrecognized action $action"
1640 if { $dump_prog != "" } {
1641 set dumpfile [lindex $actionlist 2]
1642 set binary $dump_prog
1644 # Ensure consistent sorting of symbols
1645 if {[info exists env(LC_ALL)]} {
1646 set old_lc_all $env(LC_ALL)
1649 set cmd "$binary $progopts $binfile > dump.out"
1651 catch "exec $cmd" comp_output
1652 if {[info exists old_lc_all]} {
1653 set env(LC_ALL) $old_lc_all
1657 set comp_output [prune_warnings $comp_output]
1659 if ![string match "" $comp_output] then {
1660 send_log "$comp_output\n"
1665 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1666 verbose "output is [file_contents "dump.out"]" 2
1676 } elseif { $is_unresolved } {
1677 unresolved $testname
1684 # Returns true if --gc-sections is supported on the target.
1686 proc check_gc_sections_available { } {
1687 global gc_sections_available_saved
1690 if {![info exists gc_sections_available_saved]} {
1691 # Some targets don't support gc-sections despite whatever's
1692 # advertised by ld's options.
1693 if { [istarget d30v-*-*]
1694 || [istarget dlx-*-*]
1695 || [istarget i960-*-*]
1696 || [istarget pj*-*-*]
1697 || [istarget pru*-*-*]
1698 || [istarget alpha-*-*]
1699 || [istarget hppa*64-*-*]
1700 || [istarget i370-*-*]
1701 || [istarget i860-*-*]
1702 || [istarget ia64-*-*]
1703 || [istarget mep-*-*]
1704 || [istarget mn10200-*-*] } {
1705 set gc_sections_available_saved 0
1709 # elf2flt uses -q (--emit-relocs), which is incompatible with
1711 if { [board_info target exists ldflags]
1712 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1713 set gc_sections_available_saved 0
1717 # Check if the ld used by gcc supports --gc-sections.
1718 # FIXME: this test is useless since ld --help always says
1719 # --gc-sections is available
1720 set ld_output [remote_exec host $ld "--help"]
1721 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1722 set gc_sections_available_saved 1
1724 set gc_sections_available_saved 0
1727 return $gc_sections_available_saved
1730 # Returns true if -shared is supported on the target
1731 # Only used and accurate for ELF targets at the moment
1733 proc check_shared_lib_support { } {
1734 if {![istarget aarch64*-*-elf]
1735 && ![istarget arc*-*-elf*]
1736 && ![istarget arm*-*-elf]
1737 && ![istarget avr-*-*]
1738 && ![istarget cr16-*-*]
1739 && ![istarget cris*-*-elf]
1740 && ![istarget crx-*-*]
1741 && ![istarget d10v-*-*]
1742 && ![istarget d30v-*-*]
1743 && ![istarget dlx-*-*]
1744 && ![istarget epiphany-*-*]
1745 && ![istarget fr30-*-*]
1746 && ![istarget frv-*-*]
1747 && ![istarget ft32-*-*]
1748 && ![istarget h8300-*-*]
1749 && ![istarget i860-*-*]
1750 && ![istarget i960-*-*]
1751 && ![istarget ip2k-*-*]
1752 && ![istarget iq2000-*-*]
1753 && ![istarget lm32-*-*]
1754 && ![istarget m32c-*-*]
1755 && ![istarget m32r-*-*]
1756 && ![istarget m6811-*-*]
1757 && ![istarget m6812-*-*]
1758 && ![istarget m68hc1*-*-*]
1759 && ![istarget mcore*-*-*]
1760 && ![istarget mep-*-*]
1761 && ![istarget microblaze-*-*]
1762 && ![istarget mips*-*-elf]
1763 && ![istarget mn10200-*-*]
1764 && ![istarget moxie-*-*]
1765 && ![istarget msp430-*-*]
1766 && ![istarget mt-*-*]
1767 && ![istarget nds32*-*-*]
1768 && ![istarget or1k*-*-*]
1769 && ![istarget pj-*-*]
1770 && ![istarget pru-*-*]
1771 && ![istarget rl78-*-*]
1772 && ![istarget rx-*-*]
1773 && ![istarget spu-*-*]
1774 && ![istarget v850*-*-*]
1775 && ![istarget visium-*-*]
1776 && ![istarget xc16x-*-elf]
1777 && ![istarget xgate-*-*]
1778 && ![istarget xstormy16-*-*]
1779 && ![istarget *-*-irix*]
1780 && ![istarget *-*-rtems] } {
1786 # Return true if target uses genelf.em (assuming it is ELF).
1787 proc is_generic_elf { } {
1788 if { [istarget "d30v-*-*"]
1789 || [istarget "dlx-*-*"]
1790 || [istarget "fr30-*-*"]
1791 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1792 || [istarget "ft32-*-*"]
1793 || [istarget "i860-*-*"]
1794 || [istarget "i960-*-*"]
1795 || [istarget "iq2000-*-*"]
1796 || [istarget "mn10200-*-*"]
1797 || [istarget "moxie-*-*"]
1798 || [istarget "msp430-*-*"]
1799 || [istarget "mt-*-*"]
1800 || [istarget "pj*-*-*"] } {
1806 # Returns true if the target ld supports the plugin API.
1807 proc check_plugin_api_available { } {
1808 global plugin_api_available_saved
1810 if {![info exists plugin_api_available_saved]} {
1811 # Check if the ld used by gcc supports --plugin.
1812 set ld_output [remote_exec host $ld "--help"]
1813 if { [ string first "-plugin PLUGIN" $ld_output ] >= 0 } {
1814 set plugin_api_available_saved 1
1816 set plugin_api_available_saved 0
1819 return $plugin_api_available_saved
1822 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1823 # returns true if the target ld supports sysroot.
1824 proc check_sysroot_available { } {
1825 global ld_sysroot_available_saved ld ld_sysroot
1826 if {![info exists ld_sysroot_available_saved]} {
1827 # Check if ld supports --sysroot *other* than empty.
1828 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1829 if { $ld_sysroot == "" } {
1830 set ld_sysroot_available_saved 0
1832 set ld_sysroot_available_saved 1
1835 return $ld_sysroot_available_saved
1838 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1839 proc check_gcc_plugin_enabled { } {
1842 if {![info exists CC]} {
1848 set state [remote_exec host $CC -v]
1849 if { [lindex $state 0] != 0 } {
1852 for { set i 1 } { $i < [llength $state] } { incr i } {
1853 set v [lindex $state $i]
1854 if { [ string match "*--disable-plugin*" $v ] } {
1855 verbose "plugin is disabled by $v"
1863 # Returns true if the target compiler supports LTO
1864 proc check_lto_available { } {
1865 global lto_available_saved
1868 if {![info exists lto_available_saved]} {
1869 if { ![check_gcc_plugin_enabled] } {
1870 set lto_available_saved 0
1873 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1874 # -ffat-lto-objects, we always run LTO tests on Linux with
1876 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1877 set lto_available_saved 1
1880 # Check if gcc supports -flto -fuse-linker-plugin
1882 if [board_info [target_info name] exists cflags] {
1883 append flags " [board_info [target_info name] cflags]"
1885 if [board_info [target_info name] exists ldflags] {
1886 append flags " [board_info [target_info name] ldflags]"
1889 set basename "tmpdir/lto[pid]"
1890 set src ${basename}.c
1891 set output ${basename}.out
1892 set f [open $src "w"]
1893 puts $f "int main() { return 0; }"
1895 if [is_remote host] {
1896 set src [remote_download host $src]
1898 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1899 remote_file host delete $src
1900 remote_file host delete $output
1903 return $lto_available_saved
1906 # Returns true if the target compiler supports LTO -ffat-lto-objects
1907 proc check_lto_fat_available { } {
1908 global lto_fat_available_saved
1911 if {![info exists lto_fat_available_saved]} {
1912 if { ![check_gcc_plugin_enabled] } {
1913 set lto_fat_available_saved 0
1916 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1917 # -ffat-lto-objects, we always run LTO tests on Linux with
1919 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1920 set lto_fat_available_saved 1
1923 # Check if gcc supports -flto -fuse-linker-plugin
1925 if [board_info [target_info name] exists cflags] {
1926 append flags " [board_info [target_info name] cflags]"
1928 if [board_info [target_info name] exists ldflags] {
1929 append flags " [board_info [target_info name] ldflags]"
1932 set basename "tmpdir/lto[pid]"
1933 set src ${basename}.c
1934 set output ${basename}.out
1935 set f [open $src "w"]
1936 puts $f "int main() { return 0; }"
1938 if [is_remote host] {
1939 set src [remote_download host $src]
1941 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
1942 remote_file host delete $src
1943 remote_file host delete $output
1946 return $lto_fat_available_saved
1949 # Returns true if the target compiler supports LTO and -shared
1950 proc check_lto_shared_available { } {
1951 global lto_shared_available_saved
1954 if {![info exists lto_shared_available_saved]} {
1955 if { ![check_gcc_plugin_enabled] } {
1956 set lto_shared_available_saved 0
1959 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1960 # -ffat-lto-objects, we always run LTO tests on Linux with
1962 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1963 set lto_shared_available_saved 1
1966 # Check if gcc supports -flto -fuse-linker-plugin -shared
1968 if [board_info [target_info name] exists cflags] {
1969 append flags " [board_info [target_info name] cflags]"
1971 if [board_info [target_info name] exists ldflags] {
1972 append flags " [board_info [target_info name] ldflags]"
1975 set basename "tmpdir/lto_shared[pid]"
1976 set src ${basename}.c
1977 set output ${basename}.so
1978 set f [open $src "w"]
1981 if [is_remote host] {
1982 set src [remote_download host $src]
1984 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
1985 remote_file host delete $src
1986 remote_file host delete $output
1989 return $lto_shared_available_saved
1992 # Check if the assembler supports CFI statements.
1994 proc check_as_cfi { } {
1995 global check_as_cfi_result
1997 if [info exists check_as_cfi_result] {
1998 return $check_as_cfi_result
2000 set as_file "tmpdir/check_as_cfi.s"
2001 set as_fh [open $as_file w 0666]
2002 puts $as_fh "# Generated file. DO NOT EDIT"
2003 puts $as_fh "\t.cfi_startproc"
2004 puts $as_fh "\t.cfi_endproc"
2006 remote_download host $as_file
2007 verbose -log "Checking CFI support:"
2008 rename "perror" "check_as_cfi_perror"
2009 proc perror { args } { }
2010 set success [ld_assemble $as $as_file "/dev/null"]
2012 rename "check_as_cfi_perror" "perror"
2013 #remote_file host delete $as_file
2014 set check_as_cfi_result $success
2018 # Returns true if IFUNC works.
2020 proc check_ifunc_available { } {
2021 global ifunc_available_saved
2024 if {![info exists ifunc_available_saved]} {
2025 if { [which $CC] == 0 } {
2026 set ifunc_available_saved 0
2029 # Check if gcc supports -flto -fuse-linker-plugin
2031 if [board_info [target_info name] exists cflags] {
2032 append flags " [board_info [target_info name] cflags]"
2034 if [board_info [target_info name] exists ldflags] {
2035 append flags " [board_info [target_info name] ldflags]"
2038 set basename "tmpdir/ifunc[pid]"
2039 set src ${basename}.c
2040 set output ${basename}.out
2041 set f [open $src "w"]
2042 puts $f "extern int library_func2 (void);"
2043 puts $f "int main (void)"
2045 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2046 puts $f " return 0; "
2048 puts $f "static int library_func1 (void) {return 2; }"
2049 puts $f "void *foo (void) __asm__ (\"library_func2\");"
2050 puts $f "void *foo (void) { return library_func1; }"
2051 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
2053 if [is_remote host] {
2054 set src [remote_download host $src]
2056 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2057 if { $ifunc_available_saved == 1 } {
2058 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
2060 remote_file host delete $src
2061 remote_file host delete $output
2064 return $ifunc_available_saved
2067 # Returns true if ifunc attribute works.
2069 proc check_ifunc_attribute_available { } {
2070 global ifunc_attribute_available_saved
2073 if {![info exists ifunc_attribute_available_saved]} {
2074 if { [which $CC] == 0 } {
2075 set ifunc_attribute_available_saved 0
2078 # Check if gcc supports -flto -fuse-linker-plugin
2080 if [board_info [target_info name] exists cflags] {
2081 append flags " [board_info [target_info name] cflags]"
2083 if [board_info [target_info name] exists ldflags] {
2084 append flags " [board_info [target_info name] ldflags]"
2087 set basename "tmpdir/ifunc[pid]"
2088 set src ${basename}.c
2089 set output ${basename}.out
2090 set f [open $src "w"]
2091 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
2092 puts $f "int main (void)"
2094 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2095 puts $f " return 0; "
2097 puts $f "static int library_func1 (void) {return 2; }"
2098 puts $f "void *foo (void) { return library_func1; }"
2100 if [is_remote host] {
2101 set src [remote_download host $src]
2103 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2104 if { $ifunc_attribute_available_saved == 1 } {
2105 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
2107 remote_file host delete $src
2108 remote_file host delete $output
2111 return $ifunc_attribute_available_saved
2114 # Provide virtual target "cfi" for targets supporting CFI.
2116 rename "istarget" "istarget_ld"
2117 proc istarget { target } {
2118 if {$target == "cfi"} {
2119 return [check_as_cfi]
2121 return [istarget_ld $target]
2124 # Return true if libdl is supported.
2126 proc check_libdl_available { } {
2127 global libdl_available_saved
2130 if {![info exists libdl_available_saved]} {
2131 if { [which $CC] == 0 } {
2132 set libdl_available_saved 0
2136 set basename "tmpdir/dl_avail_test[pid]"
2137 set src ${basename}.c
2138 set output ${basename}.out
2139 set f [open $src "w"]
2141 puts $f "#include <dlfcn.h>"
2142 puts $f "int main (void)"
2144 puts $f " dlopen (\"dummy.so\", RTLD_NOW);"
2145 puts $f " return 0; "
2148 if [is_remote host] {
2149 set src [remote_download host $src]
2151 set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
2152 remote_file host delete $src
2153 remote_file host delete $output
2156 return $libdl_available_saved