1 # Support routines for LD testsuite.
2 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 # 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 # Free Software Foundation, Inc.
6 # This file is part of the GNU Binutils.
8 # This file is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 proc load_common_lib { name } {
25 load_file $srcdir/../../binutils/testsuite/lib/$name
28 load_common_lib binutils-common.exp
30 # Returns 1 if the gcc for the target is at least version MAJOR.MINOR
31 # Returns 0 otherwise.
33 proc at_least_gcc_version { major minor } {
35 if {![info exists CC]} {
41 set state [remote_exec host $CC --version]
42 set tmp "[lindex $state 1]\n"
43 # Look for (eg) 4.6.1 in the version output.
44 regexp " .* (\[1-9\])\\.(\[0-9\])\\.\[0-9\]* .*" "$tmp" fred maj min
45 verbose "gcc version: $tmp"
46 verbose "major gcc version is $maj, want at least $major"
47 if { $maj == $major } then {
48 verbose "minor gcc version is $min, want at least $minor"
49 return [expr $min >= $minor ]
51 return [expr $maj > $major ]
55 # Extract and print the version number of ld.
57 proc default_ld_version { ld } {
60 if { ![is_remote host] && [which $ld] == 0 } then {
61 perror "$ld does not exist"
65 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
66 remote_upload host "ld.version"
67 set tmp [prune_warnings [file_contents "ld.version"]]
68 remote_file build delete "ld.version"
69 remote_file host delete "ld.version"
71 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
72 if [info exists number] then {
73 clone_output "$ld $number\n"
77 proc run_host_cmd { prog command } {
80 if { ![is_remote host] && [which "$prog"] == 0 } then {
81 perror "$prog does not exist"
85 verbose -log "$prog $command"
86 set status [remote_exec host [concat sh -c [list "$prog $command 2>&1"]] "" "/dev/null" "ld.tmp"]
87 remote_upload host "ld.tmp"
88 set link_output [file_contents "ld.tmp"]
89 regsub "\n$" $link_output "" link_output
90 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
91 append link_output "child process exited abnormally"
93 remote_file build delete ld.tmp
94 remote_file host delete ld.tmp
96 if [string match "" $link_output] then {
100 verbose -log "$link_output"
101 return "$link_output"
104 proc run_host_cmd_yesno { prog command } {
107 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
108 if [string match "" $exec_output] then {
114 # Link an object using relocation.
116 proc default_ld_relocate { ld target objects } {
119 remote_file host delete $target
120 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
123 # Check to see if ld is being invoked with a non-endian output format
125 proc is_endian_output_format { object_flags } {
127 if {[string match "*-oformat binary*" $object_flags] || \
128 [string match "*-oformat ieee*" $object_flags] || \
129 [string match "*-oformat ihex*" $object_flags] || \
130 [string match "*-oformat netbsd-core*" $object_flags] || \
131 [string match "*-oformat srec*" $object_flags] || \
132 [string match "*-oformat tekhex*" $object_flags] || \
133 [string match "*-oformat trad-core*" $object_flags] } then {
140 # Look for big-endian or little-endian switches in the multlib
141 # options and translate these into a -EB or -EL switch. Note
142 # we cannot rely upon proc process_multilib_options to do this
143 # for us because for some targets the compiler does not support
144 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
145 # the site.exp file will include the switch "-mbig-endian"
146 # (rather than "big-endian") which is not detected by proc
147 # process_multilib_options.
149 proc big_or_little_endian {} {
151 if [board_info [target_info name] exists multilib_flags] {
152 set tmp_flags " [board_info [target_info name] multilib_flags]"
154 foreach x $tmp_flags {
156 {*big*endian eb EB -eb -EB -mb -meb} {
160 {*little*endian el EL -el -EL -ml -mel} {
172 # Link a program using ld.
174 proc default_ld_link { ld target objects } {
183 set objs "$HOSTING_CRT0 $objects"
184 set libs "$LIBS $HOSTING_LIBS"
186 if [is_endian_output_format $objects] then {
187 set flags [big_or_little_endian]
192 remote_file host delete $target
194 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU $flags -o $target $objs $libs"]
197 # Link a program using ld, without including any libraries.
199 proc default_ld_simple_link { ld target objects } {
204 if [is_endian_output_format $objects] then {
205 set flags [big_or_little_endian]
210 # If we are compiling with gcc, we want to add gcc_ld_flag to
211 # flags. Rather than determine this in some complex way, we guess
212 # based on the name of the compiler.
214 set ldparm [string first " " $ld]
216 if { $ldparm > 0 } then {
217 set ldflags [string range $ld $ldparm end]
218 set ldexe [string range $ld 0 $ldparm]
221 set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
222 if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
223 set ldflags "$gcc_ld_flag $ldflags"
226 remote_file host delete $target
228 set exec_output [run_host_cmd "$ld" "$ldflags $flags -o $target $objects"]
229 set exec_output [prune_warnings $exec_output]
231 # We don't care if we get a warning about a non-existent start
232 # symbol, since the default linker script might use ENTRY.
233 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
235 if [string match "" $exec_output] then {
242 # Compile an object using cc.
244 proc default_ld_compile { cc source object } {
253 if {[llength $cc_prog] > 1} then {
254 set cc_prog [lindex $cc_prog 0]
256 if {![is_remote host] && [which $cc_prog] == 0} then {
257 perror "$cc_prog does not exist"
261 remote_file build delete "$object"
262 remote_file host delete "$object"
264 set flags "-I$srcdir/$subdir"
266 # If we are compiling with gcc, we want to add gcc_gas_flag to
267 # flags. Rather than determine this in some complex way, we guess
268 # based on the name of the compiler.
270 set ccparm [string first " " $cc]
272 if { $ccparm > 0 } then {
273 set ccflags [string range $cc $ccparm end]
274 set ccexe [string range $cc 0 $ccparm]
277 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
278 if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
279 set flags "$gcc_gas_flag $flags"
282 if {[string match "*++*" $ccexe]} {
283 set flags "$flags $CXXFLAGS"
285 set flags "$flags $CFLAGS"
288 if [board_info [target_info name] exists multilib_flags] {
289 append flags " [board_info [target_info name] multilib_flags]"
292 verbose -log "$cc $flags $ccflags -c $source -o $object"
294 set status [remote_exec host [concat sh -c [list "$cc $flags $ccflags -c $source -o $object 2>&1"]] "" "/dev/null" "ld.tmp"]
295 remote_upload host "ld.tmp"
296 set exec_output [file_contents "ld.tmp"]
297 remote_file build delete "ld.tmp"
298 remote_file host delete "ld.tmp"
299 set exec_output [prune_warnings $exec_output]
300 if [string match "" $exec_output] then {
301 if {![file exists $object]} then {
302 regexp ".*/(\[^/\]*)$" $source all dobj
303 regsub "\\.c" $dobj ".o" realobj
304 verbose "looking for $realobj"
305 if {[remote_file host exists $realobj]} then {
306 verbose -log "mv $realobj $object"
307 remote_upload "$realobj" "$object"
309 perror "$object not found after compilation"
315 verbose -log "$exec_output"
316 perror "$source: compilation failed"
323 proc default_ld_assemble { as in_flags source object } {
327 if ![info exists ASFLAGS] { set ASFLAGS "" }
329 set flags [big_or_little_endian]
330 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
331 set exec_output [prune_warnings $exec_output]
332 if [string match "" $exec_output] then {
335 perror "$source: assembly failed"
340 # Run nm on a file, putting the result in the array nm_output.
342 proc default_ld_nm { nm nmflags object } {
347 if {[info exists nm_output]} {
351 if ![info exists NMFLAGS] { set NMFLAGS "" }
353 # Ensure consistent sorting of symbols
354 if {[info exists env(LC_ALL)]} {
355 set old_lc_all $env(LC_ALL)
359 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
361 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
362 if {[info exists old_lc_all]} {
363 set env(LC_ALL) $old_lc_all
367 remote_upload host "ld.stderr"
368 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
369 set exec_output [prune_warnings [file_contents "ld.stderr"]]
370 remote_file host delete "ld.stderr"
371 remote_file build delete "ld.stderr"
372 if [string match "" $exec_output] then {
373 set file [open tmpdir/nm.out r]
374 while { [gets $file line] != -1 } {
376 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
377 set name [string trimleft $name "_"]
378 verbose "Setting nm_output($name) to 0x$value" 2
379 set nm_output($name) 0x$value
385 verbose -log "$exec_output"
386 perror "$object: nm failed"
391 # Define various symbols needed when not linking against all
393 proc ld_simple_link_defsyms {} {
395 set flags "--defsym __stack_chk_fail=0"
397 # ARM targets call __gccmain
398 if {[istarget arm*-*-*]} {
399 append flags " --defsym __gccmain=0"
402 # Windows targets need __main, prefixed with underscore.
403 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
404 append flags " --defsym ___main=0"
407 # PowerPC EABI code calls __eabi.
408 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
409 append flags " --defsym __eabi=0"
412 # mn10200 code calls __truncsipsi2_d0_d2.
413 if {[istarget mn10200*-*-*]} then {
414 append flags " --defsym __truncsipsi2_d0_d2=0"
417 # m6811/m6812 code has references to soft registers.
418 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
419 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
420 append flags " --defsym _.d3=0 --defsym _.d4=0"
421 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
424 # Some OpenBSD targets have ProPolice and reference __guard and
425 # __stack_smash_handler.
426 if [istarget *-*-openbsd*] {
427 append flags " --defsym __guard=0"
428 append flags " --defsym __stack_smash_handler=0"
435 # Copied from gas testsuite, tweaked and further extended.
437 # Assemble a .s file, then run some utility on it and check the output.
439 # There should be an assembly language file named FILE.s in the test
440 # suite directory, and a pattern file called FILE.d. `run_dump_test'
441 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
442 # `nm' on the .o file to produce textual output, and then analyze that
443 # with regexps. The FILE.d file specifies what program to run, and
444 # what to expect in its output.
446 # The FILE.d file begins with zero or more option lines, which specify
447 # flags to pass to the assembler, the program to run to dump the
448 # assembler's output, and the options it wants. The option lines have
453 # OPTION is the name of some option, like "name" or "objdump", and
454 # VALUE is OPTION's value. The valid options are described below.
455 # Whitespace is ignored everywhere, except within VALUE. The option
456 # list ends with the first line that doesn't match the above syntax
457 # (hmm, not great for error detection).
459 # The interesting options are:
462 # The name of this test, passed to DejaGNU's `pass' and `fail'
463 # commands. If omitted, this defaults to FILE, the root of the
464 # .s and .d files' names.
467 # When assembling, pass FLAGS to the assembler.
468 # If assembling several files, you can pass different assembler
469 # options in the "source" directives. See below.
472 # Link assembled files using FLAGS, in the order of the "source"
473 # directives, when using multiple files.
475 # ld_after_inputfiles: FLAGS
476 # Similar to "ld", but put after all input files.
478 # objcopy_linked_file: FLAGS
479 # Run objcopy on the linked file with the specified flags.
480 # This lets you transform the linked file using objcopy, before the
481 # result is analyzed by an analyzer program specified below (which
482 # may in turn *also* be objcopy).
485 # The name of the program to run to analyze the .o file produced
486 # by the assembler or the linker output. This can be omitted;
487 # run_dump_test will guess which program to run by seeing which of
488 # the flags options below is present.
493 # Use the specified program to analyze the assembler or linker
494 # output file, and pass it FLAGS, in addition to the output name.
495 # Note that they are run with LC_ALL=C in the environment to give
496 # consistent sorting of symbols.
498 # source: SOURCE [FLAGS]
499 # Assemble the file SOURCE.s using the flags in the "as" directive
500 # and the (optional) FLAGS. If omitted, the source defaults to
502 # This is useful if several .d files want to share a .s file.
503 # More than one "source" directive can be given, which is useful
504 # when testing linking.
507 # The test is expected to fail on TARGET. This may occur more than
511 # Only run the test for TARGET. This may occur more than once; the
512 # target being tested must match at least one. You may provide target
513 # name "cfi" for any target supporting the CFI statements.
516 # Do not run the test for TARGET. This may occur more than once;
517 # the target being tested must not match any of them.
520 # An error with message matching REGEX must be emitted for the test
521 # to pass. The PROG, objdump, nm and objcopy options have no
522 # meaning and need not supplied if this is present. Multiple "error"
523 # directives append to the expected linker error message.
526 # Expect a linker warning matching REGEX. It is an error to issue
527 # both "error" and "warning". Multiple "warning" directives
528 # append to the expected linker warning message.
530 # Each option may occur at most once unless otherwise mentioned.
532 # After the option lines come regexp lines. `run_dump_test' calls
533 # `regexp_diff' to compare the output of the dumping tool against the
534 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
535 # see further comments there.
537 proc run_dump_test { name } {
539 global OBJDUMP NM AS OBJCOPY READELF LD
540 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
541 global host_triplet runtests
544 if [string match "*/*" $name] {
546 set name [file tail $name]
548 set file "$srcdir/$subdir/$name"
551 if ![runtest_file_p $runtests $name] then {
555 set opt_array [slurp_options "${file}.d"]
556 if { $opt_array == -1 } {
557 perror "error reading options from $file.d"
558 unresolved $subdir/$name
561 set dumpfile tmpdir/dump.out
566 set opts(ld_after_inputfiles) {}
569 set opts(notarget) {}
579 set opts(objcopy_linked_file) {}
581 foreach i $opt_array {
582 set opt_name [lindex $i 0]
583 set opt_val [lindex $i 1]
584 if ![info exists opts($opt_name)] {
585 perror "unknown option $opt_name in file $file.d"
586 unresolved $subdir/$name
590 switch -- $opt_name {
597 # Move any source-specific as-flags to a separate list to
598 # simplify processing.
599 if { [llength $opt_val] > 1 } {
600 lappend asflags [lrange $opt_val 1 end]
601 set opt_val [lindex $opt_val 0]
607 if [string length $opts($opt_name)] {
608 perror "option $opt_name multiply set in $file.d"
609 unresolved $subdir/$name
613 # A single "# ld:" with no options should do the right thing.
614 if { $opt_name == "ld" } {
617 # Likewise objcopy_linked_file.
618 if { $opt_name == "objcopy_linked_file" } {
623 if { $opt_name == "as" || $opt_name == "ld" } {
624 set opt_val [subst $opt_val]
626 set opts($opt_name) [concat $opts($opt_name) $opt_val]
628 foreach opt { as ld } {
629 regsub {\[big_or_little_endian\]} $opts($opt) \
630 [big_or_little_endian] opts($opt)
633 # Decide early whether we should run the test for this target.
634 if { [llength $opts(target)] > 0 } {
636 foreach targ $opts(target) {
637 if [istarget $targ] {
642 if { $targmatch == 0 } {
646 foreach targ $opts(notarget) {
647 if [istarget $targ] {
653 # It's meaningless to require an output-testing method when we
655 if { $opts(error) == "" } {
656 if {$opts(PROG) != ""} {
657 switch -- $opts(PROG) {
658 objdump { set program objdump }
659 nm { set program nm }
660 objcopy { set program objcopy }
661 readelf { set program readelf }
663 { perror "unrecognized program option $opts(PROG) in $file.d"
664 unresolved $subdir/$name
668 # Guess which program to run, by seeing which option was specified.
669 foreach p {objdump objcopy nm readelf} {
670 if {$opts($p) != ""} {
671 if {$program != ""} {
672 perror "ambiguous dump program in $file.d"
673 unresolved $subdir/$name
681 if { $program == "" && $opts(warning) == "" } {
682 perror "dump program unspecified in $file.d"
683 unresolved $subdir/$name
688 if { $opts(name) == "" } {
689 set testname "$subdir/$name"
691 set testname $opts(name)
694 if { $opts(source) == "" } {
695 set sourcefiles [list ${file}.s]
696 set asflags [list ""]
699 foreach sf $opts(source) {
700 if { [string match "/*" $sf] } {
701 lappend sourcefiles "$sf"
703 lappend sourcefiles "$srcdir/$subdir/$sf"
708 # Time to setup xfailures.
709 foreach targ $opts(xfail) {
713 # Assemble each file.
715 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
716 set sourcefile [lindex $sourcefiles $i]
717 set sourceasflags [lindex $asflags $i]
719 set objfile "tmpdir/dump$i.o"
720 catch "exec rm -f $objfile" exec_output
721 lappend objfiles $objfile
722 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
725 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
726 remote_upload host "ld.tmp"
727 set comp_output [prune_warnings [file_contents "ld.tmp"]]
728 remote_file host delete "ld.tmp"
729 remote_file build delete "ld.tmp"
731 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
732 send_log "$comp_output\n"
733 verbose "$comp_output" 3
735 set exitstat "succeeded"
736 if { $cmdret != 0 } { set exitstat "failed" }
737 verbose -log "$exitstat with: <$comp_output>"
743 set expmsg $opts(error)
744 if { $opts(warning) != "" } {
745 if { $expmsg != "" } {
746 perror "$testname: mixing error and warning test-directives"
749 set expmsg $opts(warning)
752 # Perhaps link the file(s).
754 set objfile "tmpdir/dump"
755 catch "exec rm -f $objfile" exec_output
757 # Add -L$srcdir/$subdir so that the linker command can use
758 # linker scripts in the source directory.
759 set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
760 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
763 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
764 remote_upload host "ld.tmp"
765 set comp_output [file_contents "ld.tmp"]
766 remote_file host delete "ld.tmp"
767 remote_file build delete "ld.tmp"
768 set cmdret [lindex $cmdret 0]
770 if { $cmdret == 0 && $run_objcopy } {
772 set objfile "tmpdir/dump1"
773 remote_file host delete $objfile
775 # Note that we don't use OBJCOPYFLAGS here; any flags must be
776 # explicitly specified.
777 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
780 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
781 remote_upload host "ld.tmp"
782 append comp_output [file_contents "ld.tmp"]
783 remote_file host delete "ld.tmp"
784 remote_file build delete "ld.tmp"
785 set cmdret [lindex $cmdret 0]
788 regsub "\n$" $comp_output "" comp_output
789 if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
790 set exitstat "succeeded"
791 if { $cmdret != 0 } { set exitstat "failed" }
792 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
793 send_log "$comp_output\n"
794 verbose "$comp_output" 3
796 if { ($expmsg == "") == ($comp_output == "") \
797 && [regexp $expmsg $comp_output] \
798 && (($cmdret == 0) == ($opts(error) == "")) } {
799 # We have the expected output from ld.
800 if { $opts(error) != "" || $program == "" } {
805 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
811 set objfile "tmpdir/dump0.o"
814 # We must not have expected failure if we get here.
815 if { $opts(error) != "" } {
820 set progopts1 $opts($program)
821 eval set progopts \$[string toupper $program]FLAGS
822 eval set binary \$[string toupper $program]
824 if { ![is_remote host] && [which $binary] == 0 } {
829 if { $progopts1 == "" } { set $progopts1 "-r" }
830 verbose "running $binary $progopts $progopts1" 3
832 # Objcopy, unlike the other two, won't send its output to stdout,
833 # so we have to run it specially.
834 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
835 if { $program == "objcopy" } {
836 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
839 # Ensure consistent sorting of symbols
840 if {[info exists env(LC_ALL)]} {
841 set old_lc_all $env(LC_ALL)
845 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
846 set cmdret [lindex $cmdret 0]
847 remote_upload host "ld.tmp"
848 set comp_output [prune_warnings [file_contents "ld.tmp"]]
849 remote_file host delete "ld.tmp"
850 remote_file build delete "ld.tmp"
851 if {[info exists old_lc_all]} {
852 set env(LC_ALL) $old_lc_all
856 if { $cmdret != 0 || $comp_output != "" } {
857 send_log "exited abnormally with $cmdret, output:$comp_output\n"
862 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
863 if { [regexp_diff $dumpfile "${file}.d"] } then {
865 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
872 proc slurp_options { file } {
873 # If options_regsub(foo) is set to {a b}, then the contents of a
874 # "#foo:" line will have regsub -all applied to replace a with b.
875 global options_regsub
877 if [catch { set f [open $file r] } x] {
878 #perror "couldn't open `$file': $x"
883 # whitespace expression
886 # whitespace is ignored anywhere except within the options list;
887 # option names are alphabetic plus underscore only.
888 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
889 while { [gets $f line] != -1 } {
890 set line [string trim $line]
891 # Whitespace here is space-tab.
892 if [regexp $pat $line xxx opt_name opt_val] {
894 if [info exists options_regsub($opt_name)] {
895 set subst $options_regsub($opt_name)
896 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
899 lappend opt_array [list $opt_name $opt_val]
908 proc file_contents { filename } {
909 set file [open $filename r]
910 set contents [read $file]
915 proc set_file_contents { filename contents } {
916 set file [open $filename w]
917 puts $file "$contents"
921 # Create an archive using ar
923 proc ar_simple_create { ar aropts target objects } {
924 remote_file host delete $target
926 set exec_output [run_host_cmd "$ar" "$aropts rc $target $objects"]
927 set exec_output [prune_warnings $exec_output]
929 if [string match "" $exec_output] then {
930 send_log "$exec_output\n"
937 # List contains test-items with 3 items followed by 2 lists, one item and
941 # 2:assembler options
942 # 3:filenames of assembler files
943 # 4:list of actions, options and expected outputs.
944 # 5:name of output file
945 # 6:compiler flags (optional)
947 # Actions: { command command-line-options file-containg-expected-output-regexps }
949 # objdump: Apply objdump options on result.
950 # nm: Apply nm options on result.
951 # readelf: Apply readelf options on result.
952 # ld: Don't apply anything on result. Compare output during linking with
953 # the file containing regexps (which is the second arg, not the third).
954 # Note that this *must* be the first action if it is to be used at all;
955 # in all other cases, any output from the linker during linking is
956 # treated as a sign of an error and FAILs the test.
958 proc run_ld_link_tests { ldtests } {
973 foreach testitem $ldtests {
974 set testname [lindex $testitem 0]
976 if ![runtest_file_p $runtests $testname] then {
980 set ld_options [lindex $testitem 1]
981 set as_options [lindex $testitem 2]
982 set src_files [lindex $testitem 3]
983 set actions [lindex $testitem 4]
984 set binfile tmpdir/[lindex $testitem 5]
985 set cflags [lindex $testitem 6]
992 # verbose -log "Testname is $testname"
993 # verbose -log "ld_options is $ld_options"
994 # verbose -log "as_options is $as_options"
995 # verbose -log "src_files is $src_files"
996 # verbose -log "actions is $actions"
997 # verbose -log "binfile is $binfile"
999 # Assemble each file in the test.
1000 foreach src_file $src_files {
1001 set fileroot "[file rootname [file tail $src_file]]"
1002 set objfile "tmpdir/$fileroot.o"
1003 lappend objfiles $objfile
1005 if { [file extension $src_file] == ".c" } {
1006 set as_file "tmpdir/$fileroot.s"
1007 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1012 set as_file "$srcdir/$subdir/$src_file"
1014 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1020 # Catch assembler errors.
1021 if { $is_unresolved } {
1022 unresolved $testname
1026 if { [regexp ".*\\.a$" $binfile] } {
1027 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles"] } {
1030 } elseif { ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles"] } {
1032 set ld_output "$exec_output"
1036 foreach actionlist $actions {
1037 set action [lindex $actionlist 0]
1038 set progopts [lindex $actionlist 1]
1040 # There are actions where we run regexp_diff on the
1041 # output, and there are other actions (presumably).
1042 # Handling of the former look the same.
1046 { set dump_prog $objdump }
1048 { set dump_prog $nm }
1050 { set dump_prog $READELF }
1052 { set dump_prog "ld" }
1055 perror "Unrecognized action $action"
1061 if { $action == "ld" } {
1062 set regexpfile $progopts
1063 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1064 set_file_contents "tmpdir/ld.messages" "$ld_output"
1065 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1066 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1067 verbose "output is $ld_output" 2
1072 } elseif { !$maybe_failed && $dump_prog != "" } {
1073 set dumpfile [lindex $actionlist 2]
1074 set binary $dump_prog
1076 # Ensure consistent sorting of symbols
1077 if {[info exists env(LC_ALL)]} {
1078 set old_lc_all $env(LC_ALL)
1081 set cmd "$binary $progopts $binfile"
1082 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1084 remote_upload host "ld.stderr"
1085 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1086 remote_file host delete "ld.stderr"
1087 remote_file build delete "ld.stderr"
1089 if {[info exists old_lc_all]} {
1090 set env(LC_ALL) $old_lc_all
1095 if ![string match "" $comp_output] then {
1096 send_log "$comp_output\n"
1101 remote_upload host "dump.out"
1103 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1104 verbose "output is [file_contents "dump.out"]" 2
1106 remote_file build delete "dump.out"
1107 remote_file host delete "dump.out"
1110 remote_file build delete "dump.out"
1111 remote_file host delete "dump.out"
1116 if { $is_unresolved } {
1117 unresolved $testname
1118 } elseif { $maybe_failed || $failed } {
1126 # This definition is taken from an unreleased version of DejaGnu. Once
1127 # that version gets released, and has been out in the world for a few
1128 # months at least, it may be safe to delete this copy.
1129 if ![string length [info proc prune_warnings]] {
1131 # prune_warnings -- delete various system verbosities from TEXT
1134 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1136 # Sites with particular verbose os's may wish to override this in site.exp.
1138 proc prune_warnings { text } {
1139 # This is from sun4's. Do it for all machines for now.
1140 # The "\\1" is to try to preserve a "\n" but only if necessary.
1141 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1143 # It might be tempting to get carried away and delete blank lines, etc.
1144 # Just delete *exactly* what we're ask to, and that's it.
1149 # targets_to_xfail is a list of target triplets to be xfailed.
1150 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1151 # and 3 optional items:
1154 # 2:assembler options
1155 # 3:filenames of source files
1156 # 4:name of output file
1158 # 6:compiler flags (optional)
1159 # 7:language (optional)
1160 # 8:linker warning (optional)
1162 proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
1175 foreach testitem $ldtests {
1176 foreach target $targets_to_xfail {
1179 set testname [lindex $testitem 0]
1180 set ld_options [lindex $testitem 1]
1181 set as_options [lindex $testitem 2]
1182 set src_files [lindex $testitem 3]
1183 set binfile tmpdir/[lindex $testitem 4]
1184 set expfile [lindex $testitem 5]
1185 set cflags [lindex $testitem 6]
1186 set lang [lindex $testitem 7]
1187 set warning [lindex $testitem 8]
1191 # verbose -log "Testname is $testname"
1192 # verbose -log "ld_options is $ld_options"
1193 # verbose -log "as_options is $as_options"
1194 # verbose -log "src_files is $src_files"
1195 # verbose -log "actions is $actions"
1196 # verbose -log "binfile is $binfile"
1198 # Assemble each file in the test.
1199 foreach src_file $src_files {
1200 set fileroot "[file rootname [file tail $src_file]]"
1201 set objfile "tmpdir/$fileroot.o"
1202 lappend objfiles $objfile
1204 # We ignore warnings since some compilers may generate
1205 # incorrect section attributes and the assembler will warn
1207 if { [ string match "c++" $lang ] } {
1208 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1210 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1213 # We have to use $CC to build PIE and shared library.
1214 if { [ string match "c" $lang ] } {
1215 set link_proc ld_simple_link
1217 } elseif { [ string match "c++" $lang ] } {
1218 set link_proc ld_simple_link
1220 } elseif { [ string match "-shared" $ld_options ] \
1221 || [ string match "-pie" $ld_options ] } {
1222 set link_proc ld_simple_link
1225 set link_proc ld_link
1229 if ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1235 # Check if exec_output is expected.
1236 if { $warning != "" } then {
1237 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1238 if { [regexp $warning $exec_output] } then {
1245 if { $failed == 0 } {
1246 send_log "Running: $binfile > $binfile.out\n"
1247 verbose "Running: $binfile > $binfile.out"
1248 catch "exec $binfile > $binfile.out" exec_output
1250 if ![string match "" $exec_output] then {
1251 send_log "$exec_output\n"
1252 verbose "$exec_output" 1
1255 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1256 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1257 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1258 set exec_output [prune_warnings $exec_output]
1260 if ![string match "" $exec_output] then {
1261 send_log "$exec_output\n"
1262 verbose "$exec_output" 1
1268 if { $failed != 0 } {
1278 # List contains test-items with 3 items followed by 2 lists, one item and
1279 # one optional item:
1281 # 1:ld or ar options
1283 # 3:filenames of source files
1284 # 4:action and options.
1285 # 5:name of output file
1286 # 6:language (optional)
1287 # 7:linker warnings (optional)
1290 # objdump: Apply objdump options on result. Compare with regex (last arg).
1291 # nm: Apply nm options on result. Compare with regex (last arg).
1292 # readelf: Apply readelf options on result. Compare with regex (last arg).
1294 proc run_cc_link_tests { ldtests } {
1308 foreach testitem $ldtests {
1309 set testname [lindex $testitem 0]
1310 set ldflags [lindex $testitem 1]
1311 set cflags [lindex $testitem 2]
1312 set src_files [lindex $testitem 3]
1313 set actions [lindex $testitem 4]
1314 set binfile tmpdir/[lindex $testitem 5]
1315 set lang [lindex $testitem 6]
1316 set warnings [lindex $testitem 7]
1321 # Compile each file in the test.
1322 foreach src_file $src_files {
1323 set fileroot "[file rootname [file tail $src_file]]"
1324 set objfile "tmpdir/$fileroot.o"
1325 lappend objfiles $objfile
1327 # We ignore warnings since some compilers may generate
1328 # incorrect section attributes and the assembler will warn
1330 if { [ string match "c++" $lang ] } {
1331 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1333 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1337 # Clear error and warning counts.
1340 if { [ string match "c++" $lang ] } {
1346 if { [regexp ".*\\.a$" $binfile] } {
1347 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1353 } elseif { ![ld_simple_link $cc_cmd $binfile "-L$srcdir/$subdir $ldflags $objfiles"] } {
1354 # Check if exec_output is expected.
1355 if { $warnings != "" } then {
1356 verbose -log "returned with: <$exec_output>, expected: <$warnings>"
1357 if { [regexp $warnings $exec_output] } then {
1370 if { $failed == 0 } {
1371 foreach actionlist $actions {
1372 set action [lindex $actionlist 0]
1373 set progopts [lindex $actionlist 1]
1375 # There are actions where we run regexp_diff on the
1376 # output, and there are other actions (presumably).
1377 # Handling of the former look the same.
1381 { set dump_prog $objdump }
1383 { set dump_prog $nm }
1385 { set dump_prog $READELF }
1388 perror "Unrecognized action $action"
1394 if { $dump_prog != "" } {
1395 set dumpfile [lindex $actionlist 2]
1396 set binary $dump_prog
1398 # Ensure consistent sorting of symbols
1399 if {[info exists env(LC_ALL)]} {
1400 set old_lc_all $env(LC_ALL)
1403 set cmd "$binary $progopts $binfile > dump.out"
1405 catch "exec $cmd" comp_output
1406 if {[info exists old_lc_all]} {
1407 set env(LC_ALL) $old_lc_all
1411 set comp_output [prune_warnings $comp_output]
1413 if ![string match "" $comp_output] then {
1414 send_log "$comp_output\n"
1419 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1420 verbose "output is [file_contents "dump.out"]" 2
1427 if { $failed != 0 } {
1429 } else { if { $is_unresolved == 0 } {
1434 # Catch action errors.
1435 if { $is_unresolved != 0 } {
1436 unresolved $testname
1442 # Returns true if --gc-sections is supported on the target.
1444 proc check_gc_sections_available { } {
1445 global gc_sections_available_saved
1448 if {![info exists gc_sections_available_saved]} {
1449 # Some targets don't support gc-sections despite whatever's
1450 # advertised by ld's options.
1451 if {[istarget arc-*-*]
1452 || [istarget d30v-*-*]
1453 || [istarget dlx-*-*]
1454 || [istarget i960-*-*]
1455 || [istarget or32-*-*]
1456 || [istarget pj*-*-*]
1457 || [istarget alpha-*-*]
1458 || [istarget hppa*64-*-*]
1459 || [istarget i370-*-*]
1460 || [istarget i860-*-*]
1461 || [istarget ia64-*-*]
1462 || [istarget mep-*-*]
1463 || [istarget mn10200-*-*]
1464 || [istarget *-*-cygwin]
1465 || [istarget *-*-mingw*] } {
1466 set gc_sections_available_saved 0
1470 # elf2flt uses -q (--emit-relocs), which is incompatible with
1472 if { [board_info target exists ldflags]
1473 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1474 set gc_sections_available_saved 0
1478 # Check if the ld used by gcc supports --gc-sections.
1479 # FIXME: this test is useless since ld --help always says
1480 # --gc-sections is available
1481 set ld_output [remote_exec host $ld "--help"]
1482 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1483 set gc_sections_available_saved 1
1485 set gc_sections_available_saved 0
1488 return $gc_sections_available_saved
1491 # Returns true if -shared is supported on the target
1492 # Only used and accurate for ELF targets at the moment
1494 proc check_shared_lib_support { } {
1495 if {![istarget arc-*-*]
1496 && ![istarget avr-*-*]
1497 && ![istarget cr16-*-*]
1498 && ![istarget cris*-*-*]
1499 && ![istarget crx-*-*]
1500 && ![istarget d10v-*-*]
1501 && ![istarget d30v-*-*]
1502 && ![istarget dlx-*-*]
1503 && ![istarget epiphany-*-*]
1504 && ![istarget fr30-*-*]
1505 && ![istarget frv-*-*]
1506 && ![istarget h8300-*-*]
1507 && ![istarget i860-*-*]
1508 && ![istarget i960-*-*]
1509 && ![istarget ip2k-*-*]
1510 && ![istarget iq2000-*-*]
1511 && ![istarget lm32-*-*]
1512 && ![istarget m32c-*-*]
1513 && ![istarget m32r-*-*]
1514 && ![istarget m6811-*-*]
1515 && ![istarget m6812-*-*]
1516 && ![istarget m68hc1*-*-*]
1517 && ![istarget mcore*-*-*]
1518 && ![istarget mep-*-*]
1519 && ![istarget microblaze-*-*]
1520 && ![istarget mn10200-*-*]
1521 && ![istarget moxie-*-*]
1522 && ![istarget msp430-*-*]
1523 && ![istarget mt-*-*]
1524 && ![istarget openrisc-*-*]
1525 && ![istarget or32-*-*]
1526 && ![istarget pj-*-*]
1527 && ![istarget rx-*-*]
1528 && ![istarget spu-*-*]
1529 && ![istarget v850*-*-*]
1530 && ![istarget xstormy16-*-*]
1531 && ![istarget *-*-irix*]
1532 && ![istarget *-*-rtems] } {
1538 # Returns true if the target ld supports the plugin API.
1539 proc check_plugin_api_available { } {
1540 global plugin_api_available_saved
1542 if {![info exists plugin_api_available_saved]} {
1543 # Check if the ld used by gcc supports --plugin.
1544 set ld_output [remote_exec host $ld "--help"]
1545 if { [ string first "-plugin" $ld_output ] >= 0 } {
1546 set plugin_api_available_saved 1
1548 set plugin_api_available_saved 0
1551 return $plugin_api_available_saved
1554 # Check if the assembler supports CFI statements.
1556 proc check_as_cfi { } {
1557 global check_as_cfi_result
1559 if [info exists check_as_cfi_result] {
1560 return $check_as_cfi_result
1562 set as_file "tmpdir/check_as_cfi.s"
1563 set as_fh [open $as_file w 0666]
1564 puts $as_fh "# Generated file. DO NOT EDIT"
1565 puts $as_fh "\t.cfi_startproc"
1566 puts $as_fh "\t.cfi_endproc"
1568 remote_download host $as_file
1569 verbose -log "Checking CFI support:"
1570 rename "perror" "check_as_cfi_perror"
1571 proc perror { args } { }
1572 set success [ld_assemble $as $as_file "/dev/null"]
1574 rename "check_as_cfi_perror" "perror"
1575 #remote_file host delete $as_file
1576 set check_as_cfi_result $success
1580 # Provide virtual target "cfi" for targets supporting CFI.
1582 rename "istarget" "istarget_ld"
1583 proc istarget { target } {
1584 if {$target == "cfi"} {
1585 return [check_as_cfi]
1587 return [istarget_ld $target]