1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2018 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 # Versions of gcc up to and including pre-release gcc-7, at least on
286 # some targets, generate .section directives with incorrect type.
287 # Ignore warnings from the assembler about this.
288 regsub -all "(^|\n)\[^\n\]*: ignoring incorrect section type \[^\n\]*" $exec_output "" exec_output
289 regsub -all "^\[^\n\]*: Assembler messages:\n" $exec_output "" exec_output
290 if [string match "" $exec_output] then {
291 if {![file exists $object]} then {
292 regexp ".*/(\[^/\]*)$" $source all dobj
293 regsub "\\.c" $dobj ".o" realobj
294 verbose "looking for $realobj"
295 if {[remote_file host exists $realobj]} then {
296 verbose -log "mv $realobj $object"
297 remote_upload "$realobj" "$object"
299 perror "$object not found after compilation"
305 verbose -log "$exec_output"
306 perror "$source: compilation failed"
313 proc default_ld_assemble { as in_flags source object } {
319 if ![info exists ASFLAGS] { set ASFLAGS "" }
321 set flags "[big_or_little_endian] -I$srcdir/$subdir"
322 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
323 set exec_output [prune_warnings $exec_output]
324 if [string match "" $exec_output] then {
327 perror "$source: assembly failed"
332 # Run nm on a file, putting the result in the array nm_output.
334 proc default_ld_nm { nm nmflags object } {
339 if {[info exists nm_output]} {
343 if ![info exists NMFLAGS] { set NMFLAGS "" }
345 # Ensure consistent sorting of symbols
346 if {[info exists env(LC_ALL)]} {
347 set old_lc_all $env(LC_ALL)
351 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
353 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
354 if {[info exists old_lc_all]} {
355 set env(LC_ALL) $old_lc_all
359 remote_upload host "ld.stderr"
360 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
361 set exec_output [prune_warnings [file_contents "ld.stderr"]]
362 remote_file host delete "ld.stderr"
363 remote_file build delete "ld.stderr"
364 if [string match "" $exec_output] then {
365 set file [open tmpdir/nm.out r]
366 while { [gets $file line] != -1 } {
368 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
369 set name [string trimleft $name "_"]
370 verbose "Setting nm_output($name) to 0x$value" 2
371 set nm_output($name) 0x$value
377 verbose -log "$exec_output"
378 perror "$object: nm failed"
383 # Define various symbols needed when not linking against all
385 proc ld_link_defsyms {} {
387 set flags "--defsym __stack_chk_fail=0"
389 # ARM targets call __gccmain
390 if {[istarget arm*-*-*]} {
391 append flags " --defsym __gccmain=0"
394 # Windows targets need __main, some prefixed with underscore.
395 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
396 append flags " --defsym __main=0 --defsym ___main=0"
399 # PowerPC EABI code calls __eabi.
400 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
401 append flags " --defsym __eabi=0"
404 # mn10200 code calls __truncsipsi2_d0_d2.
405 if {[istarget mn10200*-*-*]} then {
406 append flags " --defsym __truncsipsi2_d0_d2=0"
409 # m6811/m6812 code has references to soft registers.
410 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
411 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
412 append flags " --defsym _.d3=0 --defsym _.d4=0"
413 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
416 # Some OpenBSD targets have ProPolice and reference __guard and
417 # __stack_smash_handler.
418 if [istarget *-*-openbsd*] {
419 append flags " --defsym __guard=0"
420 append flags " --defsym __stack_smash_handler=0"
426 # run_dump_test FILE (optional:) EXTRA_OPTIONS
427 # Copied from gas testsuite, tweaked and further extended.
429 # Assemble a .s file, then run some utility on it and check the output.
431 # There should be an assembly language file named FILE.s in the test
432 # suite directory, and a pattern file called FILE.d. `run_dump_test'
433 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
434 # `nm' on the .o file to produce textual output, and then analyze that
435 # with regexps. The FILE.d file specifies what program to run, and
436 # what to expect in its output.
438 # The FILE.d file begins with zero or more option lines, which specify
439 # flags to pass to the assembler, the program to run to dump the
440 # assembler's output, and the options it wants. The option lines have
445 # OPTION is the name of some option, like "name" or "objdump", and
446 # VALUE is OPTION's value. The valid options are described below.
447 # Whitespace is ignored everywhere, except within VALUE. The option
448 # list ends with the first line that doesn't match the above syntax
449 # (hmm, not great for error detection).
451 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
452 # two-element lists. The first element of each is an option name, and
453 # the second additional arguments to be added on to the end of the
454 # option list as given in FILE.d. (If omitted, no additional options
457 # The interesting options are:
460 # The name of this test, passed to DejaGNU's `pass' and `fail'
461 # commands. If omitted, this defaults to FILE, the root of the
462 # .s and .d files' names.
465 # When assembling, pass FLAGS to the assembler.
466 # If assembling several files, you can pass different assembler
467 # options in the "source" directives. See below.
470 # Link assembled files using FLAGS, in the order of the "source"
471 # directives, when using multiple files.
473 # ld_after_inputfiles: FLAGS
474 # Similar to "ld", but put after all input files.
476 # objcopy_objects: FLAGS
477 # Run objcopy with the specified flags after assembling any source
478 # that has the special marker RUN_OBJCOPY in the source specific
481 # objcopy_linked_file: FLAGS
482 # Run objcopy on the linked file with the specified flags.
483 # This lets you transform the linked file using objcopy, before the
484 # result is analyzed by an analyzer program specified below (which
485 # may in turn *also* be objcopy).
488 # The name of the program to run to analyze the .o file produced
489 # by the assembler or the linker output. This can be omitted;
490 # run_dump_test will guess which program to run by seeing which of
491 # the flags options below is present.
497 # Use the specified program to analyze the assembler or linker
498 # output file, and pass it FLAGS, in addition to the output name.
499 # Note that they are run with LC_ALL=C in the environment to give
500 # consistent sorting of symbols.
502 # source: SOURCE [FLAGS]
503 # Assemble the file SOURCE.s using the flags in the "as" directive
504 # and the (optional) FLAGS. If omitted, the source defaults to
506 # This is useful if several .d files want to share a .s file.
507 # More than one "source" directive can be given, which is useful
508 # when testing linking.
511 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
512 # is useful if several .d files differ by options only. Options are
513 # always read from FILE.d.
516 # The test is expected to fail on TARGET. This may occur more than
520 # Only run the test for TARGET. This may occur more than once; the
521 # target being tested must match at least one. You may provide target
522 # name "cfi" for any target supporting the CFI statements. You may
523 # provide target name "shared" for any target supporting shared
527 # Only run the test for TARGET. This may occur more than once; the
528 # target being tested must match all of them.
531 # Do not run the test for TARGET. This may occur more than once;
532 # the target being tested must not match any of them.
535 # An error with message matching REGEX must be emitted for the test
536 # to pass. The PROG, readelf, objdump, nm and objcopy options have
537 # no meaning and need not be supplied if this is present. Multiple
538 # "error" directives append to the expected linker error message.
541 # Means the same as 'error', except the regular expression lines
542 # are contains in FILE.
545 # Expect a linker warning matching REGEX. It is an error to issue
546 # both "error" and "warning". Multiple "warning" directives
547 # append to the expected linker warning message.
549 # warning_output: FILE
550 # Means the same as 'warning', except the regular expression
551 # lines are contains in FILE.
554 # Adding this option will cause the linker to generate a linker
555 # map file, using the -Map=MAPFILE command line option. If
556 # there is no -Map=MAPFILE in the 'ld: FLAGS' then one will be
557 # added to the linker command line. The contents of the
558 # generated MAPFILE are then compared against the regexp lines
559 # in FILE using `regexp_diff' (see below for details).
561 # Each option may occur at most once unless otherwise mentioned.
563 # After the option lines come regexp lines. `run_dump_test' calls
564 # `regexp_diff' to compare the output of the dumping tool against the
565 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
566 # see further comments there.
568 proc run_dump_test { name {extra_options {}} } {
570 global OBJDUMP NM AS OBJCOPY READELF LD
571 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
572 global host_triplet runtests
574 global ld_elf_shared_opt
576 if { [is_elf_format] && [check_shared_lib_support] } {
577 set ld_extra_opt "$ld_elf_shared_opt"
582 if [string match "*/*" $name] {
584 set name [file tail $name]
586 set file "$srcdir/$subdir/$name"
589 if ![runtest_file_p $runtests $name] then {
593 set opt_array [slurp_options "${file}.d"]
594 if { $opt_array == -1 } {
595 perror "error reading options from $file.d"
596 unresolved $subdir/$name
599 set dumpfile tmpdir/dump.out
605 set opts(ld_after_inputfiles) {}
608 set opts(alltargets) {}
609 set opts(notarget) {}
620 set opts(error_output) {}
621 set opts(warning_output) {}
622 set opts(objcopy_linked_file) {}
623 set opts(objcopy_objects) {}
626 foreach i $opt_array {
627 set opt_name [lindex $i 0]
628 set opt_val [lindex $i 1]
629 if ![info exists opts($opt_name)] {
630 perror "unknown option $opt_name in file $file.d"
631 unresolved $subdir/$name
635 switch -- $opt_name {
643 # Move any source-specific as-flags to a separate list to
644 # simplify processing.
645 if { [llength $opt_val] > 1 } {
646 lappend asflags [lrange $opt_val 1 end]
647 set opt_val [lindex $opt_val 0]
652 # Create the object file name based on nothing but the source
655 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]].o]
656 # But, sometimes, we have the exact same source filename in
657 # different directories (foo/src.s bar/src.s) which would lead
658 # us to try and create two src.o files. We detect this
659 # conflict here, and instead create src.o and src1.o.
661 while { [lsearch $objfile_names $new_objfile] != -1 } {
664 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]]${j}.o]
666 lappend objfile_names $new_objfile
669 if [string length $opts($opt_name)] {
670 perror "option $opt_name multiply set in $file.d"
671 unresolved $subdir/$name
675 # A single "# ld:" with no options should do the right thing.
676 if { $opt_name == "ld" } {
679 # Likewise objcopy_linked_file.
680 if { $opt_name == "objcopy_linked_file" } {
685 if { $opt_name == "as" || $opt_name == "ld" } {
686 set opt_val [subst $opt_val]
689 # Append differently whether it's a message (without space) or
690 # an option or list (with space).
691 switch -- $opt_name {
694 append opts($opt_name) $opt_val
697 set opts($opt_name) [concat $opts($opt_name) $opt_val]
702 foreach i $extra_options {
703 set opt_name [lindex $i 0]
704 set opt_val [lindex $i 1]
705 if ![info exists opts($opt_name)] {
706 perror "unknown option $opt_name given in extra_opts"
707 unresolved $subdir/$name
710 # Add extra option to end of existing option, adding space
712 if { ![regexp "warning|error" $opt_name]
713 && [string length $opts($opt_name)] } {
714 append opts($opt_name) " "
716 append opts($opt_name) $opt_val
719 foreach opt { as ld } {
720 regsub {\[big_or_little_endian\]} $opts($opt) \
721 [big_or_little_endian] opts($opt)
724 # Decide early whether we should run the test for this target.
725 if { [llength $opts(target)] > 0 } {
727 foreach targ $opts(target) {
728 if [istarget $targ] {
733 if { $targmatch == 0 } {
737 foreach targ $opts(alltargets) {
738 if ![istarget $targ] {
742 foreach targ $opts(notarget) {
743 if [istarget $targ] {
749 # It's meaningless to require an output-testing method when we
751 if { $opts(error) == "" && $opts(error_output) == "" } {
752 if {$opts(PROG) != ""} {
753 switch -- $opts(PROG) {
754 objdump { set program objdump }
755 nm { set program nm }
756 objcopy { set program objcopy }
757 readelf { set program readelf }
759 { perror "unrecognized program option $opts(PROG) in $file.d"
760 unresolved $subdir/$name
764 # Guess which program to run, by seeing which option was specified.
765 foreach p {objdump objcopy nm readelf} {
766 if {$opts($p) != ""} {
767 if {$program != ""} {
768 perror "ambiguous dump program in $file.d"
769 unresolved $subdir/$name
777 if { $program == "" \
778 && $opts(map) == "" \
779 && $opts(warning) == "" \
780 && $opts(warning_output) == "" \
781 && $opts(error) == "" \
782 && $opts(error_output) == "" } {
783 perror "dump program unspecified in $file.d"
784 unresolved $subdir/$name
789 if { $opts(name) == "" } {
790 set testname "$subdir/$name"
792 set testname $opts(name)
795 if { $opts(source) == "" } {
796 set sourcefiles [list ${file}.s]
797 set asflags [list ""]
798 set objfile_names [list tmpdir/[file tail ${file}].o]
801 foreach sf $opts(source) {
802 if { [string match "/*" $sf] } {
803 lappend sourcefiles "$sf"
805 lappend sourcefiles "$srcdir/$subdir/$sf"
810 if { $opts(dump) == "" } {
813 set dfile $srcdir/$subdir/$opts(dump)
816 # Time to setup xfailures.
817 foreach targ $opts(xfail) {
821 # Assemble each file.
823 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
824 set sourcefile [lindex $sourcefiles $i]
825 set sourceasflags [lindex $asflags $i]
826 set run_objcopy_objects 0
828 if { [string match "*RUN_OBJCOPY*" $sourceasflags] } {
829 set run_objcopy_objects 1
831 regsub "RUN_OBJCOPY" $sourceasflags "" sourceasflags
833 set objfile [lindex $objfile_names $i]
834 catch "exec rm -f $objfile" exec_output
835 lappend objfiles $objfile
836 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
839 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
840 remote_upload host "ld.tmp"
841 set comp_output [prune_warnings [file_contents "ld.tmp"]]
842 remote_file host delete "ld.tmp"
843 remote_file build delete "ld.tmp"
845 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
846 send_log -- "$comp_output\n"
847 verbose "$comp_output" 3
849 set exitstat "succeeded"
850 if { $cmdret != 0 } { set exitstat "failed" }
851 verbose -log "$exitstat with: <$comp_output>"
856 if { $run_objcopy_objects } {
857 set cmd "$OBJCOPY $opts(objcopy_objects) $objfile"
860 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] \
861 "" "/dev/null" "objcopy.tmp"]
862 remote_upload host "objcopy.tmp"
863 set comp_output [prune_warnings [file_contents "objcopy.tmp"]]
864 remote_file host delete "objcopy.tmp"
865 remote_file build delete "objcopy.tmp"
867 if { [lindex $cmdret 0] != 0 \
868 || ![string match "" $comp_output] } {
869 send_log -- "$comp_output\n"
870 verbose "$comp_output" 3
872 set exitstat "succeeded"
873 if { $cmdret != 0 } { set exitstat "failed" }
874 verbose -log "$exitstat with: <$comp_output>"
881 if { (($opts(warning) != "") && ($opts(error) != "")) \
882 || (($opts(warning) != "") && ($opts(error_output) != "")) \
883 || (($opts(warning) != "") && ($opts(warning_output) != "")) \
884 || (($opts(error) != "") && ($opts(warning_output) != "")) \
885 || (($opts(error) != "") && ($opts(error_output) != "")) \
886 || (($opts(warning_output) != "") && ($opts(error_output) != "")) } {
887 perror "$testname: bad mix of warning, error, warning_output, and error_output test-directives"
892 set check_ld(source) ""
893 set check_ld(terminal) 0
894 if { $opts(error) != "" \
895 || $opts(warning) != "" \
896 || $opts(error_output) != "" \
897 || $opts(warning_output) != "" } {
899 if { $opts(error) != "" || $opts(error_output) != "" } {
900 set check_ld(terminal) 1
902 set check_ld(terminal) 0
905 if { $opts(error) != "" || $opts(warning) != "" } {
906 set check_ld(source) "regex"
907 if { $opts(error) != "" } {
908 set check_ld(regex) $opts(error)
910 set check_ld(regex) $opts(warning)
913 set check_ld(source) "file"
914 if { $opts(error_output) != "" } {
915 set check_ld(file) $opts(error_output)
917 set check_ld(file) $opts(warning_output)
922 # Perhaps link the file(s).
924 set objfile "tmpdir/dump"
925 catch "exec rm -f $objfile" exec_output
927 # Add -L$srcdir/$subdir so that the linker command can use
928 # linker scripts in the source directory.
929 set cmd "$LD $ld_extra_opt $LDFLAGS -L$srcdir/$subdir \
930 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
932 # If needed then check for, or add a -Map option.
934 if { $opts(map) != "" } then {
935 if { [regexp -- "-Map=(\[^ \]+)" $cmd all mapfile] } then {
936 # Found existing mapfile option
937 verbose -log "Existing mapfile '$mapfile' found"
940 set mapfile "tmpdir/dump.map"
941 verbose -log "Adding mapfile '$mapfile'"
942 set cmd "$cmd -Map=$mapfile"
947 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
948 remote_upload host "ld.tmp"
949 set comp_output [file_contents "ld.tmp"]
950 remote_file host delete "ld.tmp"
951 remote_file build delete "ld.tmp"
952 set cmdret [lindex $cmdret 0]
954 if { $cmdret == 0 && $run_objcopy } {
956 set objfile "tmpdir/dump1"
957 remote_file host delete $objfile
959 # Note that we don't use OBJCOPYFLAGS here; any flags must be
960 # explicitly specified.
961 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
964 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
965 remote_upload host "ld.tmp"
966 append comp_output [file_contents "ld.tmp"]
967 remote_file host delete "ld.tmp"
968 remote_file build delete "ld.tmp"
969 set cmdret [lindex $cmdret 0]
972 regsub "\n$" $comp_output "" comp_output
973 if { $cmdret != 0 || $comp_output != "" || $check_ld(source) != "" } then {
974 set exitstat "succeeded"
975 if { $cmdret != 0 } { set exitstat "failed" }
977 if { $check_ld(source) == "regex" } {
978 verbose -log "$exitstat with: <$comp_output>, expected: <$check_ld(regex)>"
979 } elseif { $check_ld(source) == "file" } {
980 verbose -log "$exitstat with: <$comp_output>, expected in file $check_ld(file)"
981 set_file_contents "tmpdir/ld.messages" "$comp_output"
983 verbose -log "$exitstat with: <$comp_output>, no expected output"
985 send_log -- "$comp_output\n"
986 verbose "$comp_output" 3
988 if { (($check_ld(source) == "") == ($comp_output == "")) \
989 && (($cmdret == 0) == ($check_ld(terminal) == 0)) \
990 && ((($check_ld(source) == "regex") \
991 && ($check_ld(regex) == "") == ($comp_output == "") \
992 && [regexp -- $check_ld(regex) $comp_output]) \
993 || (($check_ld(source) == "file") \
994 && (![regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"]))) } {
995 # We have the expected output from ld.
996 if { $check_ld(terminal) || $program == "" } {
1006 if { $opts(map) != "" } then {
1007 # Check the map file matches.
1008 set map_pattern_file $srcdir/$subdir/$opts(map)
1009 verbose -log "Compare '$mapfile' against '$map_pattern_file'"
1010 if { [regexp_diff $mapfile $map_pattern_file] } then {
1011 fail "$testname (map file check)"
1013 pass "$testname (map file check)"
1016 if { $program == "" } then {
1021 set objfile [lindex $objfiles 0]
1024 # We must not have expected failure if we get here.
1025 if { $opts(error) != "" } {
1030 set progopts1 $opts($program)
1031 eval set progopts \$[string toupper $program]FLAGS
1032 eval set binary \$[string toupper $program]
1034 if { ![is_remote host] && [which $binary] == 0 } {
1039 if { $progopts1 == "" } { set $progopts1 "-r" }
1040 verbose "running $binary $progopts $progopts1" 3
1042 # Objcopy, unlike the other two, won't send its output to stdout,
1043 # so we have to run it specially.
1044 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
1045 if { $program == "objcopy" } {
1046 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
1049 # Ensure consistent sorting of symbols
1050 if {[info exists env(LC_ALL)]} {
1051 set old_lc_all $env(LC_ALL)
1055 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
1056 set cmdret [lindex $cmdret 0]
1057 remote_upload host "ld.tmp"
1058 set comp_output [prune_warnings [file_contents "ld.tmp"]]
1059 remote_file host delete "ld.tmp"
1060 remote_file build delete "ld.tmp"
1061 if {[info exists old_lc_all]} {
1062 set env(LC_ALL) $old_lc_all
1066 if { $cmdret != 0 || $comp_output != "" } {
1067 send_log "exited abnormally with $cmdret, output:$comp_output\n"
1072 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
1073 if { [regexp_diff $dumpfile "${dfile}"] } then {
1075 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
1082 proc slurp_options { file } {
1083 # If options_regsub(foo) is set to {a b}, then the contents of a
1084 # "#foo:" line will have regsub -all applied to replace a with b.
1085 global options_regsub
1087 if [catch { set f [open $file r] } x] {
1088 #perror "couldn't open `$file': $x"
1093 # whitespace expression
1096 # whitespace is ignored anywhere except within the options list;
1097 # option names are alphabetic plus underscore only.
1098 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
1099 while { [gets $f line] != -1 } {
1100 set line [string trim $line]
1101 # Whitespace here is space-tab.
1102 if [regexp $pat $line xxx opt_name opt_val] {
1104 if [info exists options_regsub($opt_name)] {
1105 set subst $options_regsub($opt_name)
1106 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
1109 lappend opt_array [list $opt_name $opt_val]
1118 proc file_contents { filename } {
1119 set file [open $filename r]
1120 set contents [read $file]
1125 proc set_file_contents { filename contents } {
1126 set file [open $filename w]
1127 puts $file "$contents"
1131 # Create an archive using ar
1133 proc ar_simple_create { ar aropts target objects } {
1134 remote_file host delete $target
1136 set exec_output [run_host_cmd "$ar" "-rc $aropts $target $objects"]
1137 set exec_output [prune_warnings $exec_output]
1139 if [string match "" $exec_output] then {
1140 send_log "$exec_output\n"
1147 # List contains test-items with 3 items followed by 2 lists, one item and
1148 # one optional item:
1150 # 1:ld/ar leading options, placed before object files
1151 # 2:ld/ar trailing options, placed after object files
1152 # 3:assembler options
1153 # 4:filenames of assembler files
1154 # 5:list of actions, options and expected outputs.
1155 # 6:name of output file
1156 # 7:compiler flags (optional)
1158 # Actions: { command command-line-options file-containg-expected-output-regexps }
1160 # objdump: Apply objdump options on result.
1161 # nm: Apply nm options on result.
1162 # readelf: Apply readelf options on result.
1163 # ld: Don't apply anything on result. Compare output during linking with
1164 # the file containing regexps (which is the second arg, not the third).
1165 # Note that this *must* be the first action if it is to be used at all;
1166 # in all other cases, any output from the linker during linking is
1167 # treated as a sign of an error and FAILs the test.
1169 # args is an optional list of target triplets to be xfailed.
1171 proc run_ld_link_tests { ldtests args } {
1185 global ld_elf_shared_opt
1187 if { [is_elf_format] && [check_shared_lib_support] } {
1188 set ld_extra_opt "$ld_elf_shared_opt"
1193 foreach testitem $ldtests {
1194 set testname [lindex $testitem 0]
1196 if ![runtest_file_p $runtests $testname] then {
1200 foreach target $args {
1204 set ld_options [lindex $testitem 1]
1205 set ld_after [lindex $testitem 2]
1206 set as_options [lindex $testitem 3]
1207 set src_files [lindex $testitem 4]
1208 set actions [lindex $testitem 5]
1209 set binfile tmpdir/[lindex $testitem 6]
1210 set cflags [lindex $testitem 7]
1217 # verbose -log "Testname is $testname"
1218 # verbose -log "ld_options is $ld_options"
1219 # verbose -log "ld_after is $ld_after"
1220 # verbose -log "as_options is $as_options"
1221 # verbose -log "src_files is $src_files"
1222 # verbose -log "actions is $actions"
1223 # verbose -log "binfile is $binfile"
1225 # Assemble each file in the test.
1226 foreach src_file $src_files {
1227 set fileroot "[file rootname [file tail $src_file]]"
1228 set objfile "tmpdir/$fileroot.o"
1229 lappend objfiles $objfile
1231 if { [file extension $src_file] == ".c" } {
1232 set as_file "tmpdir/$fileroot.s"
1233 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1238 set as_file "$srcdir/$subdir/$src_file"
1240 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1246 # Catch assembler errors.
1247 if { $is_unresolved } {
1248 unresolved $testname
1252 if { $binfile eq "tmpdir/" } {
1254 } elseif { [regexp ".*\\.a$" $binfile] } {
1255 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
1258 } elseif { ![ld_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
1260 set ld_output "$exec_output"
1264 foreach actionlist $actions {
1265 set action [lindex $actionlist 0]
1266 set progopts [lindex $actionlist 1]
1268 # There are actions where we run regexp_diff on the
1269 # output, and there are other actions (presumably).
1270 # Handling of the former look the same.
1274 { set dump_prog $objdump }
1276 { set dump_prog $nm }
1278 { set dump_prog $READELF }
1280 { set dump_prog "ld" }
1283 perror "Unrecognized action $action"
1289 if { $action == "ld" } {
1290 set regexpfile $progopts
1291 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1292 set_file_contents "tmpdir/ld.messages" "$ld_output"
1293 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1294 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1295 verbose "output is $ld_output" 2
1300 } elseif { !$maybe_failed && $dump_prog != "" } {
1301 set dumpfile [lindex $actionlist 2]
1302 set binary $dump_prog
1304 # Ensure consistent sorting of symbols
1305 if {[info exists env(LC_ALL)]} {
1306 set old_lc_all $env(LC_ALL)
1309 set cmd "$binary $progopts $binfile"
1310 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1312 remote_upload host "ld.stderr"
1313 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1314 remote_file host delete "ld.stderr"
1315 remote_file build delete "ld.stderr"
1317 if {[info exists old_lc_all]} {
1318 set env(LC_ALL) $old_lc_all
1323 if ![string match "" $comp_output] then {
1324 send_log "$comp_output\n"
1329 remote_upload host "dump.out"
1331 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1332 verbose "output is [file_contents "dump.out"]" 2
1334 remote_file build delete "dump.out"
1335 remote_file host delete "dump.out"
1338 remote_file build delete "dump.out"
1339 remote_file host delete "dump.out"
1344 if { $is_unresolved } {
1345 unresolved $testname
1346 } elseif { $maybe_failed || $failed } {
1354 # This definition is taken from an unreleased version of DejaGnu. Once
1355 # that version gets released, and has been out in the world for a few
1356 # months at least, it may be safe to delete this copy.
1357 if ![string length [info proc prune_warnings]] {
1359 # prune_warnings -- delete various system verbosities from TEXT
1362 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1364 # Sites with particular verbose os's may wish to override this in site.exp.
1366 proc prune_warnings { text } {
1367 # This is from sun4's. Do it for all machines for now.
1368 # The "\\1" is to try to preserve a "\n" but only if necessary.
1369 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1371 # It might be tempting to get carried away and delete blank lines, etc.
1372 # Just delete *exactly* what we're ask to, and that's it.
1377 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1378 # and 3 optional items:
1380 # 1:ld leading options, placed before object files
1381 # 2:assembler options
1382 # 3:filenames of source files
1383 # 4:name of output file
1385 # 6:compiler flags (optional)
1386 # 7:language (optional)
1387 # 8:linker warning (optional)
1388 # 9:ld trailing options, placed after object files (optional)
1389 # args is an optional list of target triplets to be xfailed.
1391 proc run_ld_link_exec_tests { ldtests args } {
1404 global STATIC_LDFLAGS
1406 # When using GCC as the linker driver, we need to specify board cflags when
1407 # linking because cflags may contain linker options. For example when
1408 # linker options are included in GCC spec files then we need the -specs
1410 if [board_info [target_info name] exists cflags] {
1411 set board_cflags " [board_info [target_info name] cflags]"
1416 foreach testitem $ldtests {
1417 foreach target $args {
1420 set testname [lindex $testitem 0]
1421 set ld_options [lindex $testitem 1]
1422 set as_options [lindex $testitem 2]
1423 set src_files [lindex $testitem 3]
1424 set binfile tmpdir/[lindex $testitem 4]
1425 set expfile [lindex $testitem 5]
1426 set cflags [lindex $testitem 6]
1427 set lang [lindex $testitem 7]
1428 set warning [lindex $testitem 8]
1429 set ld_after [lindex $testitem 9]
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 if { [ string match "c++" $lang ] } {
1446 set cmd "$CXX -c $CXXFLAGS $cflags"
1448 set cmd "$CC -c $CFLAGS $cflags"
1450 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
1455 if { $failed != 0 } {
1456 unresolved $testname
1460 if { [ string match "c++" $lang ] } {
1461 set link_proc ld_link
1464 set link_proc ld_link
1468 if { $binfile eq "tmpdir/" } {
1473 if { [string match "" $STATIC_LDFLAGS] \
1474 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ld_options $objfiles $ld_after "] } {
1478 if ![$link_proc $link_cmd $binfile "$board_cflags -L$srcdir/$subdir $ld_options $objfiles $ld_after"] {
1483 # Check if exec_output is expected.
1484 if { $warning != "" } then {
1485 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1486 if { [regexp $warning $exec_output] } then {
1493 if { $failed == 0 && [isnative] } {
1494 send_log "Running: $binfile > $binfile.out\n"
1495 verbose "Running: $binfile > $binfile.out"
1496 catch "exec $binfile > $binfile.out" exec_output
1498 if ![string match "" $exec_output] then {
1499 send_log "$exec_output\n"
1500 verbose "$exec_output" 1
1503 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1504 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1505 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1506 set exec_output [prune_warnings $exec_output]
1508 if ![string match "" $exec_output] then {
1509 send_log "$exec_output\n"
1510 verbose "$exec_output" 1
1516 if { $failed != 0 } {
1518 } elseif ![isnative] {
1519 unsupported $testname
1527 # List contains test-items with 3 items followed by 2 lists, one item and
1528 # one optional item:
1530 # 1:ld or ar options
1532 # 3:filenames of source files
1533 # 4:action and options.
1534 # 5:name of output file
1535 # 6:language (optional)
1538 # objdump: Apply objdump options on result. Compare with regex (last arg).
1539 # nm: Apply nm options on result. Compare with regex (last arg).
1540 # readelf: Apply readelf options on result. Compare with regex (last arg).
1541 # warning: Check linker output against regex (last arg).
1542 # error: Like 'warning' but checking output in error case.
1543 # warning_output: Check linker output against regex in a file (last arg).
1544 # error_output: Like 'warning_output' but checking output in error case.
1546 proc run_cc_link_tests { ldtests } {
1560 global STATIC_LDFLAGS
1562 if [board_info [target_info name] exists cflags] {
1563 set board_cflags " [board_info [target_info name] cflags]"
1568 foreach testitem $ldtests {
1569 set testname [lindex $testitem 0]
1570 set ldflags [lindex $testitem 1]
1571 set cflags [lindex $testitem 2]
1572 set src_files [lindex $testitem 3]
1573 set actions [lindex $testitem 4]
1574 set binfile tmpdir/[lindex $testitem 5]
1575 set lang [lindex $testitem 6]
1579 set check_ld(terminal) 0
1580 set check_ld(source) ""
1582 #verbose -log "testname is $testname"
1583 #verbose -log "ldflags is $ldflags"
1584 #verbose -log "cflags is $cflags"
1585 #verbose -log "src_files is $src_files"
1586 #verbose -log "actions is $actions"
1587 #verbose -log "binfile is $binfile"
1588 #verbose -log "lang is $lang"
1590 foreach actionlist $actions {
1591 set action [lindex $actionlist 0]
1592 set progopts [lindex $actionlist 1]
1594 # Find actions related to error/warning processing.
1598 set check_ld(source) "regexp"
1599 set check_ld(regexp) $progopts
1600 set check_ld(terminal) 1
1604 set check_ld(source) "regexp"
1605 set check_ld(regexp) $progopts
1609 set check_ld(source) "file"
1610 set check_ld(file) $progopts
1611 set check_ld(terminal) 1
1615 set check_ld(source) "file"
1616 set check_ld(file) $progopts
1621 # Compile each file in the test.
1622 foreach src_file $src_files {
1623 set fileroot "[file rootname [file tail $src_file]]"
1624 set objfile "tmpdir/$fileroot.o"
1625 lappend objfiles $objfile
1627 if { [ string match "c++" $lang ] } {
1628 set cmd "$CXX -c $CXXFLAGS $cflags"
1630 set cmd "$CC -c $CFLAGS $cflags"
1632 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
1637 if { $failed != 0 } {
1638 unresolved $testname
1642 # Clear error and warning counts.
1645 if { [ string match "c++" $lang ] } {
1651 if { $binfile eq "tmpdir/" } {
1653 } elseif { [regexp ".*\\.a$" $binfile] } {
1654 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1658 if { [string match "" $STATIC_LDFLAGS] \
1659 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ldflags $objfiles "] } {
1663 ld_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"
1664 set ld_output "$exec_output"
1666 if { $check_ld(source) == "regexp" } then {
1667 # Match output against regexp argument.
1668 verbose -log "returned with: <$ld_output>, expected: <$check_ld(regexp)>"
1669 if { ![regexp $check_ld(regexp) $ld_output] } then {
1672 } elseif { $check_ld(source) == "file" } then {
1673 # Match output against patterns in a file.
1674 set_file_contents "tmpdir/ld.messages" "$ld_output"
1675 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1676 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"] } then {
1677 verbose "output is $ld_output" 2
1682 if { $check_ld(source) != "" } then {
1683 if { $ld_output == "" } then {
1684 verbose -log "Linker was expected to give error or warning"
1688 if { $ld_output != "" } then {
1689 verbose -log "Unexpected linker warning or error"
1695 if { $failed == 0 } {
1696 foreach actionlist $actions {
1697 set action [lindex $actionlist 0]
1698 set progopts [lindex $actionlist 1]
1700 # There are actions where we run regexp_diff on the
1701 # output, and there are other actions (presumably).
1702 # Handling of the former look the same.
1706 { set dump_prog $objdump }
1708 { set dump_prog $nm }
1710 { set dump_prog $READELF }
1717 perror "Unrecognized action $action"
1723 if { $dump_prog != "" } {
1724 set dumpfile [lindex $actionlist 2]
1725 set binary $dump_prog
1727 # Ensure consistent sorting of symbols
1728 if {[info exists env(LC_ALL)]} {
1729 set old_lc_all $env(LC_ALL)
1732 set cmd "$binary $progopts $binfile > dump.out"
1734 catch "exec $cmd" comp_output
1735 if {[info exists old_lc_all]} {
1736 set env(LC_ALL) $old_lc_all
1740 set comp_output [prune_warnings $comp_output]
1742 if ![string match "" $comp_output] then {
1743 send_log "$comp_output\n"
1748 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1749 verbose "output is [file_contents "dump.out"]" 2
1759 } elseif { $is_unresolved } {
1760 unresolved $testname
1767 # Returns true if --gc-sections is supported on the target.
1769 proc check_gc_sections_available { } {
1770 global gc_sections_available_saved
1773 if {![info exists gc_sections_available_saved]} {
1774 # Some targets don't support gc-sections despite whatever's
1775 # advertised by ld's options.
1776 if { [istarget d30v-*-*]
1777 || [istarget dlx-*-*]
1778 || [istarget pj*-*-*]
1779 || [istarget pru*-*-*]
1780 || [istarget alpha-*-*]
1781 || [istarget hppa*64-*-*]
1782 || [istarget i370-*-*]
1783 || [istarget ia64-*-*]
1784 || [istarget mep-*-*]
1785 || [istarget mn10200-*-*] } {
1786 set gc_sections_available_saved 0
1790 # elf2flt uses -q (--emit-relocs), which is incompatible with
1792 if { [board_info target exists ldflags]
1793 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1794 set gc_sections_available_saved 0
1798 # Check if the ld used by gcc supports --gc-sections.
1799 # FIXME: this test is useless since ld --help always says
1800 # --gc-sections is available
1801 set ld_output [remote_exec host $ld "--help"]
1802 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1803 set gc_sections_available_saved 1
1805 set gc_sections_available_saved 0
1808 return $gc_sections_available_saved
1811 # Returns true if -shared is supported on the target
1813 proc check_shared_lib_support { } {
1814 global shared_available_saved
1817 if {![info exists shared_available_saved]} {
1818 set ld_output [remote_exec host $ld "-shared"]
1819 if { [ string first "not supported" $ld_output ] >= 0 } {
1820 set shared_available_saved 0
1822 set shared_available_saved 1
1825 return $shared_available_saved
1828 # Return true if target uses genelf.em (assuming it is ELF).
1829 proc is_generic_elf { } {
1830 if { [istarget "d30v-*-*"]
1831 || [istarget "dlx-*-*"]
1832 || [istarget "fr30-*-*"]
1833 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1834 || [istarget "ft32-*-*"]
1835 || [istarget "iq2000-*-*"]
1836 || [istarget "mn10200-*-*"]
1837 || [istarget "moxie-*-*"]
1838 || [istarget "msp430-*-*"]
1839 || [istarget "mt-*-*"]
1840 || [istarget "pj*-*-*"] } {
1846 proc is_underscore_target { } {
1847 global is_underscore_target_saved
1848 global target_triplet
1851 if { ![info exists is_underscore_target_saved] } {
1852 set cmd "targ=$target_triplet . $srcdir/../../bfd/config.bfd &&"
1853 append cmd { echo "$targ_underscore"}
1855 set status [catch {exec sh -c $cmd} result]
1856 if { $status == 0 && [string match "yes" $result] } {
1857 set is_underscore_target_saved 1
1859 set is_underscore_target_saved 0
1862 return $is_underscore_target_saved
1865 # Returns true if the target ld supports the plugin API.
1866 proc check_plugin_api_available { } {
1867 global plugin_api_available_saved
1869 if {![info exists plugin_api_available_saved]} {
1870 # Check if the ld used by gcc supports --plugin.
1871 set ld_output [remote_exec host $ld "--help"]
1872 if { [ string first "-plugin PLUGIN" $ld_output ] >= 0 } {
1873 set plugin_api_available_saved 1
1875 set plugin_api_available_saved 0
1878 return $plugin_api_available_saved
1881 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1882 # returns true if the target ld supports sysroot.
1883 proc check_sysroot_available { } {
1884 global ld_sysroot_available_saved ld ld_sysroot
1885 if {![info exists ld_sysroot_available_saved]} {
1886 # Check if ld supports --sysroot *other* than empty.
1887 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1888 if { $ld_sysroot == "" } {
1889 set ld_sysroot_available_saved 0
1891 set ld_sysroot_available_saved 1
1894 return $ld_sysroot_available_saved
1897 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1898 proc check_gcc_plugin_enabled { } {
1901 if {![info exists CC]} {
1907 set state [remote_exec host $CC -v]
1908 if { [lindex $state 0] != 0 } {
1911 for { set i 1 } { $i < [llength $state] } { incr i } {
1912 set v [lindex $state $i]
1913 if { [ string match "*--disable-plugin*" $v ] } {
1914 verbose "plugin is disabled by $v"
1922 # Returns true if the target compiler supports LTO
1923 proc check_lto_available { } {
1924 global lto_available_saved
1927 if {![info exists lto_available_saved]} {
1928 if { ![check_gcc_plugin_enabled] } {
1929 set lto_available_saved 0
1932 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1933 # -ffat-lto-objects, we always run LTO tests on Linux with
1935 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1936 set lto_available_saved 1
1939 # Check if gcc supports -flto -fuse-linker-plugin
1941 if [board_info [target_info name] exists cflags] {
1942 append flags " [board_info [target_info name] cflags]"
1944 if [board_info [target_info name] exists ldflags] {
1945 append flags " [board_info [target_info name] ldflags]"
1948 set basename "tmpdir/lto[pid]"
1949 set src ${basename}.c
1950 set output ${basename}.out
1951 set f [open $src "w"]
1952 puts $f "int main() { return 0; }"
1954 if [is_remote host] {
1955 set src [remote_download host $src]
1957 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1958 remote_file host delete $src
1959 remote_file host delete $output
1962 return $lto_available_saved
1965 # Returns true if the target compiler supports LTO -ffat-lto-objects
1966 proc check_lto_fat_available { } {
1967 global lto_fat_available_saved
1970 if {![info exists lto_fat_available_saved]} {
1971 if { ![check_gcc_plugin_enabled] } {
1972 set lto_fat_available_saved 0
1975 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1976 # -ffat-lto-objects, we always run LTO tests on Linux with
1978 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1979 set lto_fat_available_saved 1
1982 # Check if gcc supports -flto -fuse-linker-plugin
1984 if [board_info [target_info name] exists cflags] {
1985 append flags " [board_info [target_info name] cflags]"
1987 if [board_info [target_info name] exists ldflags] {
1988 append flags " [board_info [target_info name] ldflags]"
1991 set basename "tmpdir/lto[pid]"
1992 set src ${basename}.c
1993 set output ${basename}.out
1994 set f [open $src "w"]
1995 puts $f "int main() { return 0; }"
1997 if [is_remote host] {
1998 set src [remote_download host $src]
2000 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
2001 remote_file host delete $src
2002 remote_file host delete $output
2005 return $lto_fat_available_saved
2008 # Returns true if the target compiler supports LTO and -shared
2009 proc check_lto_shared_available { } {
2010 global lto_shared_available_saved
2013 if {![info exists lto_shared_available_saved]} {
2014 if { ![check_gcc_plugin_enabled] } {
2015 set lto_shared_available_saved 0
2018 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
2019 # -ffat-lto-objects, we always run LTO tests on Linux with
2021 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
2022 set lto_shared_available_saved 1
2025 # Check if gcc supports -flto -fuse-linker-plugin -shared
2027 if [board_info [target_info name] exists cflags] {
2028 append flags " [board_info [target_info name] cflags]"
2030 if [board_info [target_info name] exists ldflags] {
2031 append flags " [board_info [target_info name] ldflags]"
2034 set basename "tmpdir/lto_shared[pid]"
2035 set src ${basename}.c
2036 set output ${basename}.so
2037 set f [open $src "w"]
2040 if [is_remote host] {
2041 set src [remote_download host $src]
2043 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
2044 remote_file host delete $src
2045 remote_file host delete $output
2048 return $lto_shared_available_saved
2051 # Check if the assembler supports CFI statements.
2053 proc check_as_cfi { } {
2054 global check_as_cfi_result
2056 if [info exists check_as_cfi_result] {
2057 return $check_as_cfi_result
2059 set as_file "tmpdir/check_as_cfi.s"
2060 set as_fh [open $as_file w 0666]
2061 puts $as_fh "# Generated file. DO NOT EDIT"
2062 puts $as_fh "\t.cfi_startproc"
2063 puts $as_fh "\t.cfi_endproc"
2065 remote_download host $as_file
2066 verbose -log "Checking CFI support:"
2067 rename "perror" "check_as_cfi_perror"
2068 proc perror { args } { }
2069 set success [ld_assemble $as $as_file "/dev/null"]
2071 rename "check_as_cfi_perror" "perror"
2072 #remote_file host delete $as_file
2073 set check_as_cfi_result $success
2077 # Returns true if IFUNC works.
2079 proc check_ifunc_available { } {
2080 global ifunc_available_saved
2083 if {![info exists ifunc_available_saved]} {
2084 if { [which $CC] == 0 } {
2085 set ifunc_available_saved 0
2088 # Check if gcc supports -flto -fuse-linker-plugin
2090 if [board_info [target_info name] exists cflags] {
2091 append flags " [board_info [target_info name] cflags]"
2093 if [board_info [target_info name] exists ldflags] {
2094 append flags " [board_info [target_info name] ldflags]"
2097 set basename "tmpdir/ifunc[pid]"
2098 set src ${basename}.c
2099 set output ${basename}.out
2100 set f [open $src "w"]
2101 puts $f "extern int library_func2 (void);"
2102 puts $f "int main (void)"
2104 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2105 puts $f " return 0; "
2107 puts $f "static int library_func1 (void) {return 2; }"
2108 puts $f "void *foo (void) __asm__ (\"library_func2\");"
2109 puts $f "void *foo (void) { return library_func1; }"
2110 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
2112 if [is_remote host] {
2113 set src [remote_download host $src]
2115 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2116 if { $ifunc_available_saved == 1 } {
2117 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
2119 remote_file host delete $src
2120 remote_file host delete $output
2123 return $ifunc_available_saved
2126 # Returns true if ifunc attribute works.
2128 proc check_ifunc_attribute_available { } {
2129 global ifunc_attribute_available_saved
2132 if {![info exists ifunc_attribute_available_saved]} {
2133 if { [which $CC] == 0 } {
2134 set ifunc_attribute_available_saved 0
2137 # Check if gcc supports -flto -fuse-linker-plugin
2139 if [board_info [target_info name] exists cflags] {
2140 append flags " [board_info [target_info name] cflags]"
2142 if [board_info [target_info name] exists ldflags] {
2143 append flags " [board_info [target_info name] ldflags]"
2146 set basename "tmpdir/ifunc[pid]"
2147 set src ${basename}.c
2148 set output ${basename}.out
2149 set f [open $src "w"]
2150 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
2151 puts $f "int main (void)"
2153 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2154 puts $f " return 0; "
2156 puts $f "static int library_func1 (void) {return 2; }"
2157 puts $f "void *foo (void) { return library_func1; }"
2159 if [is_remote host] {
2160 set src [remote_download host $src]
2162 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2163 if { $ifunc_attribute_available_saved == 1 } {
2164 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
2166 remote_file host delete $src
2167 remote_file host delete $output
2170 return $ifunc_attribute_available_saved
2173 # Provide virtual target "cfi" for targets supporting CFI.
2175 rename "istarget" "istarget_ld"
2176 proc istarget { target } {
2177 if {$target == "cfi"} {
2178 return [check_as_cfi]
2180 if {$target == "shared"} {
2181 return [check_shared_lib_support]
2183 return [istarget_ld $target]
2186 # Return true if libdl is supported.
2188 proc check_libdl_available { } {
2189 global libdl_available_saved
2192 if {![info exists libdl_available_saved]} {
2193 if { [which $CC] == 0 } {
2194 set libdl_available_saved 0
2198 set basename "tmpdir/dl_avail_test[pid]"
2199 set src ${basename}.c
2200 set output ${basename}.out
2201 set f [open $src "w"]
2203 puts $f "#include <dlfcn.h>"
2204 puts $f "int main (void)"
2206 puts $f " dlopen (\"dummy.so\", RTLD_NOW);"
2207 puts $f " return 0; "
2210 if [is_remote host] {
2211 set src [remote_download host $src]
2213 set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
2214 remote_file host delete $src
2215 remote_file host delete $output
2218 return $libdl_available_saved
2221 # Returns true if GNU2 TLS works.
2223 proc check_gnu2_tls_available { } {
2224 global gnu2_tls_available_saved
2228 if {![info exists gnu2_tls_available_saved]} {
2229 if { [which $CC] == 0 || "$GNU2_CFLAGS" == "" } {
2230 set gnu2_tls_available_saved 0
2233 # Check if GNU2 TLS works.
2234 set flags "$GNU2_CFLAGS"
2235 if [board_info [target_info name] exists cflags] {
2236 append flags " [board_info [target_info name] cflags]"
2238 if [board_info [target_info name] exists ldflags] {
2239 append flags " [board_info [target_info name] ldflags]"
2242 set basename "tmpdir/gnu2_tls[pid]"
2243 set src1 ${basename}1.c
2244 set output1 ${basename}.so
2245 set f [open $src1 "w"]
2246 puts $f "extern __thread int zzz;"
2247 puts $f "int foo (void)"
2249 puts $f " return zzz;"
2252 if [is_remote host] {
2253 set src1 [remote_download host $src1]
2255 set src2 ${basename}2.c
2256 set output2 ${basename}.exe
2257 set f [open $src2 "w"]
2258 puts $f "__thread int zzz = 20;"
2259 puts $f "extern int foo (void);"
2260 puts $f "int main (void)"
2262 puts $f " if (foo () != 20) __builtin_abort ();"
2263 puts $f " return 0; "
2266 if [is_remote host] {
2267 set src2 [remote_download host $src2]
2269 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "-fPIC -shared $flags $src1 -o $output1"]
2270 if { $gnu2_tls_available_saved == 1 } {
2271 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "$flags $src2 $output1 -o $output2"]
2272 if { $gnu2_tls_available_saved == 1 } {
2273 set gnu2_tls_available_saved [run_host_cmd_yesno "$output2" ""]
2276 remote_file host delete $src1
2277 remote_file host delete $output1
2278 remote_file host delete $src2
2279 remote_file host delete $output2
2280 file delete $src1 $src2
2282 return $gnu2_tls_available_saved