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
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 # Extract and print the version number of ld.
32 proc default_ld_version { ld } {
35 if { ![is_remote host] && [which $ld] == 0 } then {
36 perror "$ld does not exist"
40 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
41 remote_upload host "ld.version"
42 set tmp [prune_warnings [file_contents "ld.version"]]
43 remote_file build delete "ld.version"
44 remote_file host delete "ld.version"
46 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
47 if [info exists number] then {
48 clone_output "$ld $number\n"
52 proc run_host_cmd { prog command } {
55 if { ![is_remote host] && [which "$prog"] == 0 } then {
56 perror "$prog does not exist"
60 verbose -log "$prog $command"
61 set status [remote_exec host [concat sh -c [list "$prog $command 2>&1"]] "" "/dev/null" "ld.tmp"]
62 remote_upload host "ld.tmp"
63 set link_output [file_contents "ld.tmp"]
64 regsub "\n$" $link_output "" link_output
65 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
66 append link_output "child process exited abnormally"
68 remote_file build delete ld.tmp
69 remote_file host delete ld.tmp
71 if [string match "" $link_output] then {
75 verbose -log "$link_output"
79 proc run_host_cmd_yesno { prog command } {
82 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
83 if [string match "" $exec_output] then {
89 # Link an object using relocation.
91 proc default_ld_relocate { ld target objects } {
94 remote_file host delete $target
95 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
98 # Check to see if ld is being invoked with a non-endian output format
100 proc is_endian_output_format { object_flags } {
102 if {[string match "*-oformat binary*" $object_flags] || \
103 [string match "*-oformat ieee*" $object_flags] || \
104 [string match "*-oformat ihex*" $object_flags] || \
105 [string match "*-oformat netbsd-core*" $object_flags] || \
106 [string match "*-oformat srec*" $object_flags] || \
107 [string match "*-oformat tekhex*" $object_flags] || \
108 [string match "*-oformat trad-core*" $object_flags] } then {
115 # Look for big-endian or little-endian switches in the multlib
116 # options and translate these into a -EB or -EL switch. Note
117 # we cannot rely upon proc process_multilib_options to do this
118 # for us because for some targets the compiler does not support
119 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
120 # the site.exp file will include the switch "-mbig-endian"
121 # (rather than "big-endian") which is not detected by proc
122 # process_multilib_options.
124 proc big_or_little_endian {} {
126 if [board_info [target_info name] exists multilib_flags] {
127 set tmp_flags " [board_info [target_info name] multilib_flags]"
129 foreach x $tmp_flags {
131 {*big*endian eb EB -eb -EB -mb -meb} {
135 {*little*endian el EL -el -EL -ml -mel} {
147 # Link a program using ld.
149 proc default_ld_link { ld target objects } {
158 set objs "$HOSTING_CRT0 $objects"
159 set libs "$LIBS $HOSTING_LIBS"
161 if [is_endian_output_format $objects] then {
162 set flags [big_or_little_endian]
167 remote_file host delete $target
169 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU $flags -o $target $objs $libs"]
172 # Link a program using ld, without including any libraries.
174 proc default_ld_simple_link { ld target objects } {
179 if [is_endian_output_format $objects] then {
180 set flags [big_or_little_endian]
185 # If we are compiling with gcc, we want to add gcc_ld_flag to
186 # flags. Rather than determine this in some complex way, we guess
187 # based on the name of the compiler.
189 set ldparm [string first " " $ld]
191 if { $ldparm > 0 } then {
192 set ldflags [string range $ld $ldparm end]
193 set ldexe [string range $ld 0 $ldparm]
196 set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
197 if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
198 set ldflags "$gcc_ld_flag $ldflags"
201 remote_file host delete $target
203 set exec_output [run_host_cmd "$ld" "$ldflags $flags -o $target $objects"]
204 set exec_output [prune_warnings $exec_output]
206 # We don't care if we get a warning about a non-existent start
207 # symbol, since the default linker script might use ENTRY.
208 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
210 if [string match "" $exec_output] then {
217 # Compile an object using cc.
219 proc default_ld_compile { cc source object } {
228 if {[llength $cc_prog] > 1} then {
229 set cc_prog [lindex $cc_prog 0]
231 if {![is_remote host] && [which $cc_prog] == 0} then {
232 perror "$cc_prog does not exist"
236 remote_file build delete "$object"
237 remote_file host delete "$object"
239 set flags "-I$srcdir/$subdir"
241 # If we are compiling with gcc, we want to add gcc_gas_flag to
242 # flags. Rather than determine this in some complex way, we guess
243 # based on the name of the compiler.
245 set ccparm [string first " " $cc]
247 if { $ccparm > 0 } then {
248 set ccflags [string range $cc $ccparm end]
249 set ccexe [string range $cc 0 $ccparm]
252 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
253 if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
254 set flags "$gcc_gas_flag $flags"
257 if {[string match "*++*" $ccexe]} {
258 set flags "$flags $CXXFLAGS"
260 set flags "$flags $CFLAGS"
263 if [board_info [target_info name] exists multilib_flags] {
264 append flags " [board_info [target_info name] multilib_flags]"
267 verbose -log "$cc $flags $ccflags -c $source -o $object"
269 set status [remote_exec host [concat sh -c [list "$cc $flags $ccflags -c $source -o $object 2>&1"]] "" "/dev/null" "ld.tmp"]
270 remote_upload host "ld.tmp"
271 set exec_output [file_contents "ld.tmp"]
272 remote_file build delete "ld.tmp"
273 remote_file host delete "ld.tmp"
274 set exec_output [prune_warnings $exec_output]
275 if [string match "" $exec_output] then {
276 if {![file exists $object]} then {
277 regexp ".*/(\[^/\]*)$" $source all dobj
278 regsub "\\.c" $dobj ".o" realobj
279 verbose "looking for $realobj"
280 if {[remote_file host exists $realobj]} then {
281 verbose -log "mv $realobj $object"
282 remote_upload "$realobj" "$object"
284 perror "$object not found after compilation"
290 verbose -log "$exec_output"
291 perror "$source: compilation failed"
298 proc default_ld_assemble { as in_flags source object } {
302 if ![info exists ASFLAGS] { set ASFLAGS "" }
304 set flags [big_or_little_endian]
305 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
306 set exec_output [prune_warnings $exec_output]
307 if [string match "" $exec_output] then {
310 perror "$source: assembly failed"
315 # Run nm on a file, putting the result in the array nm_output.
317 proc default_ld_nm { nm nmflags object } {
322 if {[info exists nm_output]} {
326 if ![info exists NMFLAGS] { set NMFLAGS "" }
328 # Ensure consistent sorting of symbols
329 if {[info exists env(LC_ALL)]} {
330 set old_lc_all $env(LC_ALL)
334 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
336 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
337 if {[info exists old_lc_all]} {
338 set env(LC_ALL) $old_lc_all
342 remote_upload host "ld.stderr"
343 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
344 set exec_output [prune_warnings [file_contents "ld.stderr"]]
345 remote_file host delete "ld.stderr"
346 remote_file build delete "ld.stderr"
347 if [string match "" $exec_output] then {
348 set file [open tmpdir/nm.out r]
349 while { [gets $file line] != -1 } {
351 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
352 set name [string trimleft $name "_"]
353 verbose "Setting nm_output($name) to 0x$value" 2
354 set nm_output($name) 0x$value
360 verbose -log "$exec_output"
361 perror "$object: nm failed"
366 # Define various symbols needed when not linking against all
368 proc ld_simple_link_defsyms {} {
370 set flags "--defsym __stack_chk_fail=0"
372 # ARM targets call __gccmain
373 if {[istarget arm*-*-*]} {
374 append flags " --defsym __gccmain=0"
377 # Windows targets need __main, prefixed with underscore.
378 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
379 append flags " --defsym ___main=0"
382 # PowerPC EABI code calls __eabi.
383 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
384 append flags " --defsym __eabi=0"
387 # mn10200 code calls __truncsipsi2_d0_d2.
388 if {[istarget mn10200*-*-*]} then {
389 append flags " --defsym __truncsipsi2_d0_d2=0"
392 # m6811/m6812 code has references to soft registers.
393 if {[istarget m6811-*-*] || [istarget m6812-*-*]} {
394 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
395 append flags " --defsym _.d3=0 --defsym _.d4=0"
396 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
399 # Some OpenBSD targets have ProPolice and reference __guard and
400 # __stack_smash_handler.
401 if [istarget *-*-openbsd*] {
402 append flags " --defsym __guard=0"
403 append flags " --defsym __stack_smash_handler=0"
410 # Copied from gas testsuite, tweaked and further extended.
412 # Assemble a .s file, then run some utility on it and check the output.
414 # There should be an assembly language file named FILE.s in the test
415 # suite directory, and a pattern file called FILE.d. `run_dump_test'
416 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
417 # `nm' on the .o file to produce textual output, and then analyze that
418 # with regexps. The FILE.d file specifies what program to run, and
419 # what to expect in its output.
421 # The FILE.d file begins with zero or more option lines, which specify
422 # flags to pass to the assembler, the program to run to dump the
423 # assembler's output, and the options it wants. The option lines have
428 # OPTION is the name of some option, like "name" or "objdump", and
429 # VALUE is OPTION's value. The valid options are described below.
430 # Whitespace is ignored everywhere, except within VALUE. The option
431 # list ends with the first line that doesn't match the above syntax
432 # (hmm, not great for error detection).
434 # The interesting options are:
437 # The name of this test, passed to DejaGNU's `pass' and `fail'
438 # commands. If omitted, this defaults to FILE, the root of the
439 # .s and .d files' names.
442 # When assembling, pass FLAGS to the assembler.
443 # If assembling several files, you can pass different assembler
444 # options in the "source" directives. See below.
447 # Link assembled files using FLAGS, in the order of the "source"
448 # directives, when using multiple files.
450 # ld_after_inputfiles: FLAGS
451 # Similar to "ld", but put after all input files.
453 # objcopy_linked_file: FLAGS
454 # Run objcopy on the linked file with the specified flags.
455 # This lets you transform the linked file using objcopy, before the
456 # result is analyzed by an analyzer program specified below (which
457 # may in turn *also* be objcopy).
460 # The name of the program to run to analyze the .o file produced
461 # by the assembler or the linker output. This can be omitted;
462 # run_dump_test will guess which program to run by seeing which of
463 # the flags options below is present.
468 # Use the specified program to analyze the assembler or linker
469 # output file, and pass it FLAGS, in addition to the output name.
470 # Note that they are run with LC_ALL=C in the environment to give
471 # consistent sorting of symbols.
473 # source: SOURCE [FLAGS]
474 # Assemble the file SOURCE.s using the flags in the "as" directive
475 # and the (optional) FLAGS. If omitted, the source defaults to
477 # This is useful if several .d files want to share a .s file.
478 # More than one "source" directive can be given, which is useful
479 # when testing linking.
482 # The test is expected to fail on TARGET. This may occur more than
486 # Only run the test for TARGET. This may occur more than once; the
487 # target being tested must match at least one. You may provide target
488 # name "cfi" for any target supporting the CFI statements.
491 # Do not run the test for TARGET. This may occur more than once;
492 # the target being tested must not match any of them.
495 # An error with message matching REGEX must be emitted for the test
496 # to pass. The PROG, objdump, nm and objcopy options have no
497 # meaning and need not supplied if this is present. Multiple "error"
498 # directives append to the expected linker error message.
501 # Expect a linker warning matching REGEX. It is an error to issue
502 # both "error" and "warning". Multiple "warning" directives
503 # append to the expected linker warning message.
505 # Each option may occur at most once unless otherwise mentioned.
507 # After the option lines come regexp lines. `run_dump_test' calls
508 # `regexp_diff' to compare the output of the dumping tool against the
509 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
510 # see further comments there.
512 proc run_dump_test { name } {
514 global OBJDUMP NM AS OBJCOPY READELF LD
515 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
516 global host_triplet runtests
519 if [string match "*/*" $name] {
521 set name [file tail $name]
523 set file "$srcdir/$subdir/$name"
526 if ![runtest_file_p $runtests $name] then {
530 set opt_array [slurp_options "${file}.d"]
531 if { $opt_array == -1 } {
532 perror "error reading options from $file.d"
533 unresolved $subdir/$name
536 set dumpfile tmpdir/dump.out
541 set opts(ld_after_inputfiles) {}
544 set opts(notarget) {}
554 set opts(objcopy_linked_file) {}
556 foreach i $opt_array {
557 set opt_name [lindex $i 0]
558 set opt_val [lindex $i 1]
559 if ![info exists opts($opt_name)] {
560 perror "unknown option $opt_name in file $file.d"
561 unresolved $subdir/$name
565 switch -- $opt_name {
572 # Move any source-specific as-flags to a separate list to
573 # simplify processing.
574 if { [llength $opt_val] > 1 } {
575 lappend asflags [lrange $opt_val 1 end]
576 set opt_val [lindex $opt_val 0]
582 if [string length $opts($opt_name)] {
583 perror "option $opt_name multiply set in $file.d"
584 unresolved $subdir/$name
588 # A single "# ld:" with no options should do the right thing.
589 if { $opt_name == "ld" } {
592 # Likewise objcopy_linked_file.
593 if { $opt_name == "objcopy_linked_file" } {
598 if { $opt_name == "as" || $opt_name == "ld" } {
599 set opt_val [subst $opt_val]
601 set opts($opt_name) [concat $opts($opt_name) $opt_val]
603 foreach opt { as ld } {
604 regsub {\[big_or_little_endian\]} $opts($opt) \
605 [big_or_little_endian] opts($opt)
608 # Decide early whether we should run the test for this target.
609 if { [llength $opts(target)] > 0 } {
611 foreach targ $opts(target) {
612 if [istarget $targ] {
617 if { $targmatch == 0 } {
621 foreach targ $opts(notarget) {
622 if [istarget $targ] {
628 # It's meaningless to require an output-testing method when we
630 if { $opts(error) == "" } {
631 if {$opts(PROG) != ""} {
632 switch -- $opts(PROG) {
633 objdump { set program objdump }
634 nm { set program nm }
635 objcopy { set program objcopy }
636 readelf { set program readelf }
638 { perror "unrecognized program option $opts(PROG) in $file.d"
639 unresolved $subdir/$name
643 # Guess which program to run, by seeing which option was specified.
644 foreach p {objdump objcopy nm readelf} {
645 if {$opts($p) != ""} {
646 if {$program != ""} {
647 perror "ambiguous dump program in $file.d"
648 unresolved $subdir/$name
656 if { $program == "" && $opts(warning) == "" } {
657 perror "dump program unspecified in $file.d"
658 unresolved $subdir/$name
663 if { $opts(name) == "" } {
664 set testname "$subdir/$name"
666 set testname $opts(name)
669 if { $opts(source) == "" } {
670 set sourcefiles [list ${file}.s]
671 set asflags [list ""]
674 foreach sf $opts(source) {
675 if { [string match "/*" $sf] } {
676 lappend sourcefiles "$sf"
678 lappend sourcefiles "$srcdir/$subdir/$sf"
683 # Time to setup xfailures.
684 foreach targ $opts(xfail) {
688 # Assemble each file.
690 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
691 set sourcefile [lindex $sourcefiles $i]
692 set sourceasflags [lindex $asflags $i]
694 set objfile "tmpdir/dump$i.o"
695 catch "exec rm -f $objfile" exec_output
696 lappend objfiles $objfile
697 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
700 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
701 remote_upload host "ld.tmp"
702 set comp_output [prune_warnings [file_contents "ld.tmp"]]
703 remote_file host delete "ld.tmp"
704 remote_file build delete "ld.tmp"
706 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
707 send_log "$comp_output\n"
708 verbose "$comp_output" 3
710 set exitstat "succeeded"
711 if { $cmdret != 0 } { set exitstat "failed" }
712 verbose -log "$exitstat with: <$comp_output>"
718 set expmsg $opts(error)
719 if { $opts(warning) != "" } {
720 if { $expmsg != "" } {
721 perror "$testname: mixing error and warning test-directives"
724 set expmsg $opts(warning)
727 # Perhaps link the file(s).
729 set objfile "tmpdir/dump"
730 catch "exec rm -f $objfile" exec_output
732 # Add -L$srcdir/$subdir so that the linker command can use
733 # linker scripts in the source directory.
734 set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
735 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
738 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
739 remote_upload host "ld.tmp"
740 set comp_output [file_contents "ld.tmp"]
741 remote_file host delete "ld.tmp"
742 remote_file build delete "ld.tmp"
743 set cmdret [lindex $cmdret 0]
745 if { $cmdret == 0 && $run_objcopy } {
747 set objfile "tmpdir/dump1"
748 remote_file host delete $objfile
750 # Note that we don't use OBJCOPYFLAGS here; any flags must be
751 # explicitly specified.
752 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
755 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
756 remote_upload host "ld.tmp"
757 append comp_output [file_contents "ld.tmp"]
758 remote_file host delete "ld.tmp"
759 remote_file build delete "ld.tmp"
760 set cmdret [lindex $cmdret 0]
763 regsub "\n$" $comp_output "" comp_output
764 if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
765 set exitstat "succeeded"
766 if { $cmdret != 0 } { set exitstat "failed" }
767 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
768 send_log "$comp_output\n"
769 verbose "$comp_output" 3
771 if { ($expmsg == "") == ($comp_output == "") \
772 && [regexp $expmsg $comp_output] \
773 && (($cmdret == 0) == ($opts(error) == "")) } {
774 # We have the expected output from ld.
775 if { $opts(error) != "" || $program == "" } {
780 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
786 set objfile "tmpdir/dump0.o"
789 # We must not have expected failure if we get here.
790 if { $opts(error) != "" } {
795 set progopts1 $opts($program)
796 eval set progopts \$[string toupper $program]FLAGS
797 eval set binary \$[string toupper $program]
799 if { ![is_remote host] && [which $binary] == 0 } {
804 if { $progopts1 == "" } { set $progopts1 "-r" }
805 verbose "running $binary $progopts $progopts1" 3
807 # Objcopy, unlike the other two, won't send its output to stdout,
808 # so we have to run it specially.
809 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
810 if { $program == "objcopy" } {
811 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
814 # Ensure consistent sorting of symbols
815 if {[info exists env(LC_ALL)]} {
816 set old_lc_all $env(LC_ALL)
820 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
821 set cmdret [lindex $cmdret 0]
822 remote_upload host "ld.tmp"
823 set comp_output [prune_warnings [file_contents "ld.tmp"]]
824 remote_file host delete "ld.tmp"
825 remote_file build delete "ld.tmp"
826 if {[info exists old_lc_all]} {
827 set env(LC_ALL) $old_lc_all
831 if { $cmdret != 0 || $comp_output != "" } {
832 send_log "exited abnormally with $cmdret, output:$comp_output\n"
837 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
838 if { [regexp_diff $dumpfile "${file}.d"] } then {
840 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
847 proc slurp_options { file } {
848 if [catch { set f [open $file r] } x] {
849 #perror "couldn't open `$file': $x"
854 # whitespace expression
857 # whitespace is ignored anywhere except within the options list;
858 # option names are alphabetic plus underscore only.
859 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
860 while { [gets $f line] != -1 } {
861 set line [string trim $line]
862 # Whitespace here is space-tab.
863 if [regexp $pat $line xxx opt_name opt_val] {
865 lappend opt_array [list $opt_name $opt_val]
874 proc file_contents { filename } {
875 set file [open $filename r]
876 set contents [read $file]
881 proc set_file_contents { filename contents } {
882 set file [open $filename w]
883 puts $file "$contents"
887 # Create an archive using ar
889 proc ar_simple_create { ar aropts target objects } {
890 remote_file host delete $target
892 set exec_output [run_host_cmd "$ar" "$aropts rc $target $objects"]
893 set exec_output [prune_warnings $exec_output]
895 if [string match "" $exec_output] then {
896 send_log "$exec_output\n"
903 # List contains test-items with 3 items followed by 2 lists, one item and
907 # 2:assembler options
908 # 3:filenames of assembler files
909 # 4:list of actions, options and expected outputs.
910 # 5:name of output file
911 # 6:compiler flags (optional)
913 # Actions: { command command-line-options file-containg-expected-output-regexps }
915 # objdump: Apply objdump options on result.
916 # nm: Apply nm options on result.
917 # readelf: Apply readelf options on result.
918 # ld: Don't apply anything on result. Compare output during linking with
919 # the file containing regexps (which is the second arg, not the third).
920 # Note that this *must* be the first action if it is to be used at all;
921 # in all other cases, any output from the linker during linking is
922 # treated as a sign of an error and FAILs the test.
924 proc run_ld_link_tests { ldtests } {
939 foreach testitem $ldtests {
940 set testname [lindex $testitem 0]
942 if ![runtest_file_p $runtests $testname] then {
946 set ld_options [lindex $testitem 1]
947 set as_options [lindex $testitem 2]
948 set src_files [lindex $testitem 3]
949 set actions [lindex $testitem 4]
950 set binfile tmpdir/[lindex $testitem 5]
951 set cflags [lindex $testitem 6]
958 # verbose -log "Testname is $testname"
959 # verbose -log "ld_options is $ld_options"
960 # verbose -log "as_options is $as_options"
961 # verbose -log "src_files is $src_files"
962 # verbose -log "actions is $actions"
963 # verbose -log "binfile is $binfile"
965 # Assemble each file in the test.
966 foreach src_file $src_files {
967 set fileroot "[file rootname [file tail $src_file]]"
968 set objfile "tmpdir/$fileroot.o"
969 lappend objfiles $objfile
971 if { [file extension $src_file] == ".c" } {
972 set as_file "tmpdir/$fileroot.s"
973 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
978 set as_file "$srcdir/$subdir/$src_file"
980 if ![ld_assemble $as "$as_options $as_file" $objfile] {
986 # Catch assembler errors.
987 if { $is_unresolved } {
992 if { [regexp ".*\\.a$" $binfile] } {
993 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles"] } {
996 } elseif { ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles"] } {
998 set ld_output "$exec_output"
1002 foreach actionlist $actions {
1003 set action [lindex $actionlist 0]
1004 set progopts [lindex $actionlist 1]
1006 # There are actions where we run regexp_diff on the
1007 # output, and there are other actions (presumably).
1008 # Handling of the former look the same.
1012 { set dump_prog $objdump }
1014 { set dump_prog $nm }
1016 { set dump_prog $READELF }
1018 { set dump_prog "ld" }
1021 perror "Unrecognized action $action"
1027 if { $action == "ld" } {
1028 set regexpfile $progopts
1029 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1030 set_file_contents "tmpdir/ld.messages" "$ld_output"
1031 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1032 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1033 verbose "output is $ld_output" 2
1038 } elseif { !$maybe_failed && $dump_prog != "" } {
1039 set dumpfile [lindex $actionlist 2]
1040 set binary $dump_prog
1042 # Ensure consistent sorting of symbols
1043 if {[info exists env(LC_ALL)]} {
1044 set old_lc_all $env(LC_ALL)
1047 set cmd "$binary $progopts $binfile"
1048 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1050 remote_upload host "ld.stderr"
1051 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1052 remote_file host delete "ld.stderr"
1053 remote_file build delete "ld.stderr"
1055 if {[info exists old_lc_all]} {
1056 set env(LC_ALL) $old_lc_all
1061 if ![string match "" $comp_output] then {
1062 send_log "$comp_output\n"
1067 remote_upload host "dump.out"
1069 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1070 verbose "output is [file_contents "dump.out"]" 2
1072 remote_file build delete "dump.out"
1073 remote_file host delete "dump.out"
1076 remote_file build delete "dump.out"
1077 remote_file host delete "dump.out"
1082 if { $is_unresolved } {
1083 unresolved $testname
1084 } elseif { $maybe_failed || $failed } {
1092 # This definition is taken from an unreleased version of DejaGnu. Once
1093 # that version gets released, and has been out in the world for a few
1094 # months at least, it may be safe to delete this copy.
1095 if ![string length [info proc prune_warnings]] {
1097 # prune_warnings -- delete various system verbosities from TEXT
1100 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1102 # Sites with particular verbose os's may wish to override this in site.exp.
1104 proc prune_warnings { text } {
1105 # This is from sun4's. Do it for all machines for now.
1106 # The "\\1" is to try to preserve a "\n" but only if necessary.
1107 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1109 # It might be tempting to get carried away and delete blank lines, etc.
1110 # Just delete *exactly* what we're ask to, and that's it.
1115 # targets_to_xfail is a list of target triplets to be xfailed.
1116 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1117 # and 3 optional items:
1120 # 2:assembler options
1121 # 3:filenames of source files
1122 # 4:name of output file
1124 # 6:compiler flags (optional)
1125 # 7:language (optional)
1126 # 8:linker warning (optional)
1128 proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
1141 foreach testitem $ldtests {
1142 foreach target $targets_to_xfail {
1145 set testname [lindex $testitem 0]
1146 set ld_options [lindex $testitem 1]
1147 set as_options [lindex $testitem 2]
1148 set src_files [lindex $testitem 3]
1149 set binfile tmpdir/[lindex $testitem 4]
1150 set expfile [lindex $testitem 5]
1151 set cflags [lindex $testitem 6]
1152 set lang [lindex $testitem 7]
1153 set warning [lindex $testitem 8]
1157 # verbose -log "Testname is $testname"
1158 # verbose -log "ld_options is $ld_options"
1159 # verbose -log "as_options is $as_options"
1160 # verbose -log "src_files is $src_files"
1161 # verbose -log "actions is $actions"
1162 # verbose -log "binfile is $binfile"
1164 # Assemble each file in the test.
1165 foreach src_file $src_files {
1166 set fileroot "[file rootname [file tail $src_file]]"
1167 set objfile "tmpdir/$fileroot.o"
1168 lappend objfiles $objfile
1170 # We ignore warnings since some compilers may generate
1171 # incorrect section attributes and the assembler will warn
1173 if { [ string match "c++" $lang ] } {
1174 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1176 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1179 # We have to use $CC to build PIE and shared library.
1180 if { [ string match "c" $lang ] } {
1181 set link_proc ld_simple_link
1183 } elseif { [ string match "c++" $lang ] } {
1184 set link_proc ld_simple_link
1186 } elseif { [ string match "-shared" $ld_options ] \
1187 || [ string match "-pie" $ld_options ] } {
1188 set link_proc ld_simple_link
1191 set link_proc ld_link
1195 if ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1201 # Check if exec_output is expected.
1202 if { $warning != "" } then {
1203 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1204 if { [regexp $warning $exec_output] } then {
1211 if { $failed == 0 } {
1212 send_log "Running: $binfile > $binfile.out\n"
1213 verbose "Running: $binfile > $binfile.out"
1214 catch "exec $binfile > $binfile.out" exec_output
1216 if ![string match "" $exec_output] then {
1217 send_log "$exec_output\n"
1218 verbose "$exec_output" 1
1221 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1222 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1223 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1224 set exec_output [prune_warnings $exec_output]
1226 if ![string match "" $exec_output] then {
1227 send_log "$exec_output\n"
1228 verbose "$exec_output" 1
1234 if { $failed != 0 } {
1244 # List contains test-items with 3 items followed by 2 lists, one item and
1245 # one optional item:
1247 # 1:ld or ar options
1249 # 3:filenames of source files
1250 # 4:action and options.
1251 # 5:name of output file
1252 # 6:language (optional)
1253 # 7:linker warnings (optional)
1256 # objdump: Apply objdump options on result. Compare with regex (last arg).
1257 # nm: Apply nm options on result. Compare with regex (last arg).
1258 # readelf: Apply readelf options on result. Compare with regex (last arg).
1260 proc run_cc_link_tests { ldtests } {
1274 foreach testitem $ldtests {
1275 set testname [lindex $testitem 0]
1276 set ldflags [lindex $testitem 1]
1277 set cflags [lindex $testitem 2]
1278 set src_files [lindex $testitem 3]
1279 set actions [lindex $testitem 4]
1280 set binfile tmpdir/[lindex $testitem 5]
1281 set lang [lindex $testitem 6]
1282 set warnings [lindex $testitem 7]
1287 # Compile each file in the test.
1288 foreach src_file $src_files {
1289 set fileroot "[file rootname [file tail $src_file]]"
1290 set objfile "tmpdir/$fileroot.o"
1291 lappend objfiles $objfile
1293 # We ignore warnings since some compilers may generate
1294 # incorrect section attributes and the assembler will warn
1296 if { [ string match "c++" $lang ] } {
1297 ld_compile "$CXX -c $CXXFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1299 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1303 # Clear error and warning counts.
1306 if { [ string match "c++" $lang ] } {
1312 if { [regexp ".*\\.a$" $binfile] } {
1313 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1319 } elseif { ![ld_simple_link $cc_cmd $binfile "-L$srcdir/$subdir $ldflags $objfiles"] } {
1320 # Check if exec_output is expected.
1321 if { $warnings != "" } then {
1322 verbose -log "returned with: <$exec_output>, expected: <$warnings>"
1323 if { [regexp $warnings $exec_output] } then {
1336 if { $failed == 0 } {
1337 foreach actionlist $actions {
1338 set action [lindex $actionlist 0]
1339 set progopts [lindex $actionlist 1]
1341 # There are actions where we run regexp_diff on the
1342 # output, and there are other actions (presumably).
1343 # Handling of the former look the same.
1347 { set dump_prog $objdump }
1349 { set dump_prog $nm }
1351 { set dump_prog $READELF }
1354 perror "Unrecognized action $action"
1360 if { $dump_prog != "" } {
1361 set dumpfile [lindex $actionlist 2]
1362 set binary $dump_prog
1364 # Ensure consistent sorting of symbols
1365 if {[info exists env(LC_ALL)]} {
1366 set old_lc_all $env(LC_ALL)
1369 set cmd "$binary $progopts $binfile > dump.out"
1371 catch "exec $cmd" comp_output
1372 if {[info exists old_lc_all]} {
1373 set env(LC_ALL) $old_lc_all
1377 set comp_output [prune_warnings $comp_output]
1379 if ![string match "" $comp_output] then {
1380 send_log "$comp_output\n"
1385 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1386 verbose "output is [file_contents "dump.out"]" 2
1393 if { $failed != 0 } {
1395 } else { if { $is_unresolved == 0 } {
1400 # Catch action errors.
1401 if { $is_unresolved != 0 } {
1402 unresolved $testname
1408 # Returns true if --gc-sections is supported on the target.
1410 proc check_gc_sections_available { } {
1411 global gc_sections_available_saved
1414 if {![info exists gc_sections_available_saved]} {
1415 # Some targets don't support gc-sections despite whatever's
1416 # advertised by ld's options.
1417 if {[istarget arc-*-*]
1418 || [istarget d30v-*-*]
1419 || [istarget dlx-*-*]
1420 || [istarget i960-*-*]
1421 || [istarget or32-*-*]
1422 || [istarget pj*-*-*]
1423 || [istarget alpha-*-*]
1424 || [istarget hppa*64-*-*]
1425 || [istarget i370-*-*]
1426 || [istarget i860-*-*]
1427 || [istarget ia64-*-*]
1428 || [istarget mep-*-*]
1429 || [istarget mn10200-*-*]
1430 || [istarget *-*-cygwin]
1431 || [istarget *-*-mingw*] } {
1432 set gc_sections_available_saved 0
1436 # elf2flt uses -q (--emit-relocs), which is incompatible with
1438 if { [board_info target exists ldflags]
1439 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1440 set gc_sections_available_saved 0
1444 # Check if the ld used by gcc supports --gc-sections.
1445 # FIXME: this test is useless since ld --help always says
1446 # --gc-sections is available
1447 set ld_output [remote_exec host $ld "--help"]
1448 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1449 set gc_sections_available_saved 1
1451 set gc_sections_available_saved 0
1454 return $gc_sections_available_saved
1457 # Returns true if -shared is supported on the target
1458 # Only used and accurate for ELF targets at the moment
1460 proc check_shared_lib_support { } {
1461 if {![istarget arc-*-*]
1462 && ![istarget avr-*-*]
1463 && ![istarget cr16-*-*]
1464 && ![istarget cris*-*-*]
1465 && ![istarget crx-*-*]
1466 && ![istarget d10v-*-*]
1467 && ![istarget d30v-*-*]
1468 && ![istarget dlx-*-*]
1469 && ![istarget epiphany-*-*]
1470 && ![istarget fr30-*-*]
1471 && ![istarget frv-*-*]
1472 && ![istarget h8300-*-*]
1473 && ![istarget i860-*-*]
1474 && ![istarget i960-*-*]
1475 && ![istarget ip2k-*-*]
1476 && ![istarget iq2000-*-*]
1477 && ![istarget lm32-*-*]
1478 && ![istarget m32c-*-*]
1479 && ![istarget m32r-*-*]
1480 && ![istarget m68hc1*-*-*]
1481 && ![istarget mcore*-*-*]
1482 && ![istarget mep-*-*]
1483 && ![istarget microblaze-*-*]
1484 && ![istarget mn10200-*-*]
1485 && ![istarget moxie-*-*]
1486 && ![istarget msp430-*-*]
1487 && ![istarget mt-*-*]
1488 && ![istarget openrisc-*-*]
1489 && ![istarget or32-*-*]
1490 && ![istarget pj-*-*]
1491 && ![istarget rx-*-*]
1492 && ![istarget spu-*-*]
1493 && ![istarget v850*-*-*]
1494 && ![istarget xstormy16-*-*]
1495 && ![istarget *-*-irix*]
1496 && ![istarget *-*-rtems] } {
1502 # Returns true if the target ld supports the plugin API.
1503 proc check_plugin_api_available { } {
1504 global plugin_api_available_saved
1506 if {![info exists plugin_api_available_saved]} {
1507 # Check if the ld used by gcc supports --plugin.
1508 set ld_output [remote_exec host $ld "--help"]
1509 if { [ string first "-plugin" $ld_output ] >= 0 } {
1510 set plugin_api_available_saved 1
1512 set plugin_api_available_saved 0
1515 return $plugin_api_available_saved
1518 # Check if the assembler supports CFI statements.
1520 proc check_as_cfi { } {
1521 global check_as_cfi_result
1523 if [info exists check_as_cfi_result] {
1524 return $check_as_cfi_result
1526 set as_file "tmpdir/check_as_cfi.s"
1527 set as_fh [open $as_file w 0666]
1528 puts $as_fh "# Generated file. DO NOT EDIT"
1529 puts $as_fh "\t.cfi_startproc"
1530 puts $as_fh "\t.cfi_endproc"
1532 remote_download host $as_file
1533 verbose -log "Checking CFI support:"
1534 rename "perror" "check_as_cfi_perror"
1535 proc perror { args } { }
1536 set success [ld_assemble $as $as_file "/dev/null"]
1538 rename "check_as_cfi_perror" "perror"
1539 #remote_file host delete $as_file
1540 set check_as_cfi_result $success
1544 # Provide virtual target "cfi" for targets supporting CFI.
1546 rename "istarget" "istarget_ld"
1547 proc istarget { target } {
1548 if {$target == "cfi"} {
1549 return [check_as_cfi]
1551 return [istarget_ld $target]