1 # Support routines for LD testsuite.
2 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 # 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 # This file is part of the GNU Binutils.
7 # This file is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 # Extract and print the version number of ld.
24 proc default_ld_version { ld } {
27 if { ![is_remote host] && [which $ld] == 0 } then {
28 perror "$ld does not exist"
32 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
33 remote_upload host "ld.version"
34 set tmp [prune_warnings [file_contents "ld.version"]]
35 remote_file build delete "ld.version"
36 remote_file host delete "ld.version"
38 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
39 if [info exists number] then {
40 clone_output "$ld $number\n"
44 proc run_host_cmd { prog command } {
47 if { ![is_remote host] && [which "$prog"] == 0 } then {
48 perror "$prog does not exist"
52 verbose -log "$prog $command"
53 set status [remote_exec host [concat sh -c [list "$prog $command 2>&1"]] "" "/dev/null" "ld.tmp"]
54 remote_upload host "ld.tmp"
55 set link_output [file_contents "ld.tmp"]
56 regsub "\n$" $link_output "" link_output
57 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
58 append link_output "child process exited abnormally"
60 remote_file build delete ld.tmp
61 remote_file host delete ld.tmp
63 if [string match "" $link_output] then {
67 verbose -log "$link_output"
71 proc run_host_cmd_yesno { prog command } {
74 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
75 if [string match "" $exec_output] then {
81 # Link an object using relocation.
83 proc default_ld_relocate { ld target objects } {
86 remote_file host delete $target
87 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
90 # Check to see if ld is being invoked with a non-endian output format
92 proc is_endian_output_format { object_flags } {
94 if {[string match "*-oformat binary*" $object_flags] || \
95 [string match "*-oformat ieee*" $object_flags] || \
96 [string match "*-oformat ihex*" $object_flags] || \
97 [string match "*-oformat netbsd-core*" $object_flags] || \
98 [string match "*-oformat srec*" $object_flags] || \
99 [string match "*-oformat tekhex*" $object_flags] || \
100 [string match "*-oformat trad-core*" $object_flags] } then {
107 # Look for big-endian or little-endian switches in the multlib
108 # options and translate these into a -EB or -EL switch. Note
109 # we cannot rely upon proc process_multilib_options to do this
110 # for us because for some targets the compiler does not support
111 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
112 # the site.exp file will include the switch "-mbig-endian"
113 # (rather than "big-endian") which is not detected by proc
114 # process_multilib_options.
116 proc big_or_little_endian {} {
118 if [board_info [target_info name] exists multilib_flags] {
119 set tmp_flags " [board_info [target_info name] multilib_flags]"
121 foreach x $tmp_flags {
123 {*big*endian eb EB -eb -EB -mb -meb} {
127 {*little*endian el EL -el -EL -ml -mel} {
139 # Link a program using ld.
141 proc default_ld_link { ld target objects } {
150 set objs "$HOSTING_CRT0 $objects"
151 set libs "$LIBS $HOSTING_LIBS"
153 if [is_endian_output_format $objects] then {
154 set flags [big_or_little_endian]
159 remote_file host delete $target
161 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU $flags -o $target $objs $libs"]
164 # Link a program using ld, without including any libraries.
166 proc default_ld_simple_link { ld target objects } {
171 if [is_endian_output_format $objects] then {
172 set flags [big_or_little_endian]
177 # If we are compiling with gcc, we want to add gcc_ld_flag to
178 # flags. Rather than determine this in some complex way, we guess
179 # based on the name of the compiler.
181 set ldparm [string first " " $ld]
182 if { $ldparm > 0 } then {
183 set ldexe [string range $ld 0 $ldparm]
185 set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
186 if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
187 set flags "$gcc_ld_flag $flags"
190 remote_file host delete $target
192 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
193 set exec_output [prune_warnings $exec_output]
195 # We don't care if we get a warning about a non-existent start
196 # symbol, since the default linker script might use ENTRY.
197 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
199 if [string match "" $exec_output] then {
206 # Compile an object using cc.
208 proc default_ld_compile { cc source object } {
216 if {[llength $cc_prog] > 1} then {
217 set cc_prog [lindex $cc_prog 0]
219 if {![is_remote host] && [which $cc_prog] == 0} then {
220 perror "$cc_prog does not exist"
224 remote_file build delete "$object"
225 remote_file host delete "$object"
227 set flags "-I$srcdir/$subdir $CFLAGS"
229 # If we are compiling with gcc, we want to add gcc_gas_flag to
230 # flags. Rather than determine this in some complex way, we guess
231 # based on the name of the compiler.
233 set ccparm [string first " " $cc]
235 if { $ccparm > 0 } then {
236 set ccflags [string range $cc $ccparm end]
237 set ccexe [string range $cc 0 $ccparm]
240 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
241 if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
242 set flags "$gcc_gas_flag $flags"
245 if [board_info [target_info name] exists multilib_flags] {
246 append flags " [board_info [target_info name] multilib_flags]"
249 verbose -log "$cc $flags $ccflags -c $source -o $object"
251 set status [remote_exec host [concat sh -c [list "$cc $flags $ccflags -c $source -o $object 2>&1"]] "" "/dev/null" "ld.tmp"]
252 remote_upload host "ld.tmp"
253 set exec_output [file_contents "ld.tmp"]
254 remote_file build delete "ld.tmp"
255 remote_file host delete "ld.tmp"
256 set exec_output [prune_warnings $exec_output]
257 if [string match "" $exec_output] then {
258 if {![file exists $object]} then {
259 regexp ".*/(\[^/\]*)$" $source all dobj
260 regsub "\\.c" $dobj ".o" realobj
261 verbose "looking for $realobj"
262 if {[remote_file host exists $realobj]} then {
263 verbose -log "mv $realobj $object"
264 remote_upload "$realobj" "$object"
266 perror "$object not found after compilation"
272 verbose -log "$exec_output"
273 perror "$source: compilation failed"
280 proc default_ld_assemble { as source object } {
284 if ![info exists ASFLAGS] { set ASFLAGS "" }
286 set flags [big_or_little_endian]
287 set exec_output [run_host_cmd "$as" "$flags $ASFLAGS -o $object $source"]
288 set exec_output [prune_warnings $exec_output]
289 if [string match "" $exec_output] then {
292 perror "$source: assembly failed"
297 # Run nm on a file, putting the result in the array nm_output.
299 proc default_ld_nm { nm nmflags object } {
304 if {[info exists nm_output]} {
308 if ![info exists NMFLAGS] { set NMFLAGS "" }
310 # Ensure consistent sorting of symbols
311 if {[info exists env(LC_ALL)]} {
312 set old_lc_all $env(LC_ALL)
316 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
318 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
319 if {[info exists old_lc_all]} {
320 set env(LC_ALL) $old_lc_all
324 remote_upload host "ld.stderr"
325 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
326 set exec_output [prune_warnings [file_contents "ld.stderr"]]
327 remote_file host delete "ld.stderr"
328 remote_file build delete "ld.stderr"
329 if [string match "" $exec_output] then {
330 set file [open tmpdir/nm.out r]
331 while { [gets $file line] != -1 } {
333 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
334 set name [string trimleft $name "_"]
335 verbose "Setting nm_output($name) to 0x$value" 2
336 set nm_output($name) 0x$value
342 verbose -log "$exec_output"
343 perror "$object: nm failed"
348 # True if the object format is known to be ELF.
350 proc is_elf_format {} {
351 if { ![istarget *-*-sysv4*] \
352 && ![istarget *-*-unixware*] \
353 && ![istarget *-*-elf*] \
354 && ![istarget *-*-eabi*] \
355 && ![istarget hppa*64*-*-hpux*] \
356 && ![istarget *-*-linux*] \
357 && ![istarget frv-*-uclinux*] \
358 && ![istarget *-*-irix5*] \
359 && ![istarget *-*-irix6*] \
360 && ![istarget *-*-netbsd*] \
361 && ![istarget *-*-solaris2*] } {
365 if { [istarget *-*-linux*aout*] \
366 || [istarget *-*-linux*oldld*] } {
370 if { ![istarget *-*-netbsdelf*] \
371 && ([istarget *-*-netbsd*aout*] \
372 || [istarget *-*-netbsdpe*] \
373 || [istarget arm*-*-netbsd*] \
374 || [istarget sparc-*-netbsd*] \
375 || [istarget i*86-*-netbsd*] \
376 || [istarget m68*-*-netbsd*] \
377 || [istarget vax-*-netbsd*] \
378 || [istarget ns32k-*-netbsd*]) } {
384 # True if the object format is known to be 64-bit ELF.
386 proc is_elf64 { binary_file } {
391 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
393 if ![string match "" $got] then {
397 if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
398 [file_contents readelf.out] nil readelf_size] } {
402 if { $readelf_size == "64" } {
409 # True if the object format is known to be a.out.
411 proc is_aout_format {} {
412 if { [istarget *-*-*\[ab\]out*] \
413 || [istarget *-*-linux*oldld*] \
414 || [istarget *-*-msdos*] \
415 || [istarget arm-*-netbsd] \
416 || [istarget i?86-*-netbsd] \
417 || [istarget i?86-*-mach*] \
418 || [istarget i?86-*-vsta] \
419 || [istarget pdp11-*-*] \
420 || [istarget m68*-ericsson-ose] \
421 || [istarget m68k-hp-bsd*] \
422 || [istarget m68*-*-hpux*] \
423 || [istarget m68*-*-netbsd] \
424 || [istarget m68*-*-netbsd*4k*] \
425 || [istarget m68k-sony-*] \
426 || [istarget m68*-sun-sunos\[34\]*] \
427 || [istarget m68*-wrs-vxworks*] \
428 || [istarget ns32k-*-*] \
429 || [istarget sparc*-*-netbsd] \
430 || [istarget sparc-sun-sunos4*] \
431 || [istarget vax-dec-ultrix*] \
432 || [istarget vax-*-netbsd] } {
438 # True if the object format is known to be PE COFF.
440 proc is_pecoff_format {} {
441 if { ![istarget *-*-mingw*] \
442 && ![istarget *-*-cygwin*] \
443 && ![istarget *-*-pe*] } {
450 # Compares two files line-by-line.
451 # Returns differences if exist.
452 # Returns null if file(s) cannot be opened.
454 proc simple_diff { file_1 file_2 } {
460 if [file exists $file_1] then {
461 set file_a [open $file_1 r]
463 warning "$file_1 doesn't exist"
467 if [file exists $file_2] then {
468 set file_b [open $file_2 r]
470 fail "$file_2 doesn't exist"
474 verbose "# Diff'ing: $file_1 $file_2\n" 2
476 while { [gets $file_a line] != $eof } {
477 if [regexp "^#.*$" $line] then {
485 while { [gets $file_b line] != $eof } {
486 if [regexp "^#.*$" $line] then {
494 for { set i 0 } { $i < [llength $list_a] } { incr i } {
495 set line_a [lindex $list_a $i]
496 set line_b [lindex $list_b $i]
498 verbose "\t$file_1: $i: $line_a\n" 3
499 verbose "\t$file_2: $i: $line_b\n" 3
500 if [string compare $line_a $line_b] then {
501 verbose -log "\t$file_1: $i: $line_a\n"
502 verbose -log "\t$file_2: $i: $line_b\n"
509 if { [llength $list_a] != [llength $list_b] } {
514 if $differences<1 then {
520 # Copied from gas testsuite, tweaked and further extended.
522 # Assemble a .s file, then run some utility on it and check the output.
524 # There should be an assembly language file named FILE.s in the test
525 # suite directory, and a pattern file called FILE.d. `run_dump_test'
526 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
527 # `nm' on the .o file to produce textual output, and then analyze that
528 # with regexps. The FILE.d file specifies what program to run, and
529 # what to expect in its output.
531 # The FILE.d file begins with zero or more option lines, which specify
532 # flags to pass to the assembler, the program to run to dump the
533 # assembler's output, and the options it wants. The option lines have
538 # OPTION is the name of some option, like "name" or "objdump", and
539 # VALUE is OPTION's value. The valid options are described below.
540 # Whitespace is ignored everywhere, except within VALUE. The option
541 # list ends with the first line that doesn't match the above syntax
542 # (hmm, not great for error detection).
544 # The interesting options are:
547 # The name of this test, passed to DejaGNU's `pass' and `fail'
548 # commands. If omitted, this defaults to FILE, the root of the
549 # .s and .d files' names.
552 # When assembling, pass FLAGS to the assembler.
553 # If assembling several files, you can pass different assembler
554 # options in the "source" directives. See below.
557 # Link assembled files using FLAGS, in the order of the "source"
558 # directives, when using multiple files.
560 # objcopy_linked_file: FLAGS
561 # Run objcopy on the linked file with the specified flags.
562 # This lets you transform the linked file using objcopy, before the
563 # result is analyzed by an analyzer program specified below (which
564 # may in turn *also* be objcopy).
567 # The name of the program to run to analyze the .o file produced
568 # by the assembler or the linker output. This can be omitted;
569 # run_dump_test will guess which program to run by seeing which of
570 # the flags options below is present.
575 # Use the specified program to analyze the assembler or linker
576 # output file, and pass it FLAGS, in addition to the output name.
577 # Note that they are run with LC_ALL=C in the environment to give
578 # consistent sorting of symbols.
580 # source: SOURCE [FLAGS]
581 # Assemble the file SOURCE.s using the flags in the "as" directive
582 # and the (optional) FLAGS. If omitted, the source defaults to
584 # This is useful if several .d files want to share a .s file.
585 # More than one "source" directive can be given, which is useful
586 # when testing linking.
589 # The test is expected to fail on TARGET. This may occur more than
593 # Only run the test for TARGET. This may occur more than once; the
594 # target being tested must match at least one.
597 # Do not run the test for TARGET. This may occur more than once;
598 # the target being tested must not match any of them.
601 # An error with message matching REGEX must be emitted for the test
602 # to pass. The PROG, objdump, nm and objcopy options have no
603 # meaning and need not supplied if this is present.
606 # Expect a linker warning matching REGEX. It is an error to issue
607 # both "error" and "warning".
609 # Each option may occur at most once unless otherwise mentioned.
611 # After the option lines come regexp lines. `run_dump_test' calls
612 # `regexp_diff' to compare the output of the dumping tool against the
613 # regexps in FILE.d. `regexp_diff' is defined later in this file; see
614 # further comments there.
616 proc run_dump_test { name } {
618 global OBJDUMP NM AS OBJCOPY READELF LD
619 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
620 global host_triplet runtests
623 if [string match "*/*" $name] {
625 set name [file tail $name]
627 set file "$srcdir/$subdir/$name"
630 if ![runtest_file_p $runtests $name] then {
634 set opt_array [slurp_options "${file}.d"]
635 if { $opt_array == -1 } {
636 perror "error reading options from $file.d"
637 unresolved $subdir/$name
640 set dumpfile tmpdir/dump.out
647 set opts(notarget) {}
657 set opts(objcopy_linked_file) {}
658 set asflags(${file}.s) {}
660 foreach i $opt_array {
661 set opt_name [lindex $i 0]
662 set opt_val [lindex $i 1]
663 if ![info exists opts($opt_name)] {
664 perror "unknown option $opt_name in file $file.d"
665 unresolved $subdir/$name
669 switch -- $opt_name {
674 # Move any source-specific as-flags to a separate array to
675 # simplify processing.
676 if { [llength $opt_val] > 1 } {
677 set asflags([lindex $opt_val 0]) [lrange $opt_val 1 end]
678 set opt_val [lindex $opt_val 0]
680 set asflags($opt_val) {}
684 if [string length $opts($opt_name)] {
685 perror "option $opt_name multiply set in $file.d"
686 unresolved $subdir/$name
690 # A single "# ld:" with no options should do the right thing.
691 if { $opt_name == "ld" } {
694 # Likewise objcopy_linked_file.
695 if { $opt_name == "objcopy_linked_file" } {
700 if { $opt_name == "as" || $opt_name == "ld" } {
701 set opt_val [subst $opt_val]
703 set opts($opt_name) [concat $opts($opt_name) $opt_val]
705 foreach opt { as ld } {
706 regsub {\[big_or_little_endian\]} $opts($opt) \
707 [big_or_little_endian] opts($opt)
710 # Decide early whether we should run the test for this target.
711 if { [llength $opts(target)] > 0 } {
713 foreach targ $opts(target) {
714 if [istarget $targ] {
719 if { $targmatch == 0 } {
723 foreach targ $opts(notarget) {
724 if [istarget $targ] {
730 # It's meaningless to require an output-testing method when we
732 if { $opts(error) == "" } {
733 if {$opts(PROG) != ""} {
734 switch -- $opts(PROG) {
735 objdump { set program objdump }
736 nm { set program nm }
737 objcopy { set program objcopy }
738 readelf { set program readelf }
740 { perror "unrecognized program option $opts(PROG) in $file.d"
741 unresolved $subdir/$name
745 # Guess which program to run, by seeing which option was specified.
746 foreach p {objdump objcopy nm readelf} {
747 if {$opts($p) != ""} {
748 if {$program != ""} {
749 perror "ambiguous dump program in $file.d"
750 unresolved $subdir/$name
758 if { $program == "" && $opts(warning) == "" } {
759 perror "dump program unspecified in $file.d"
760 unresolved $subdir/$name
765 if { $opts(name) == "" } {
766 set testname "$subdir/$name"
768 set testname $opts(name)
771 if { $opts(source) == "" } {
772 set sourcefiles [list ${file}.s]
775 foreach sf $opts(source) {
776 if { [string match "/*" $sf] } {
777 lappend sourcefiles "$sf"
779 lappend sourcefiles "$srcdir/$subdir/$sf"
781 # Must have asflags indexed on source name.
782 set asflags($srcdir/$subdir/$sf) $asflags($sf)
786 # Time to setup xfailures.
787 foreach targ $opts(xfail) {
791 # Assemble each file.
793 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
794 set sourcefile [lindex $sourcefiles $i]
796 set objfile "tmpdir/dump$i.o"
797 catch "exec rm -f $objfile" exec_output
798 lappend objfiles $objfile
799 set cmd "$AS $ASFLAGS $opts(as) $asflags($sourcefile) -o $objfile $sourcefile"
802 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
803 remote_upload host "ld.tmp"
804 set comp_output [prune_warnings [file_contents "ld.tmp"]]
805 remote_file host delete "ld.tmp"
806 remote_file build delete "ld.tmp"
808 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
809 send_log "$comp_output\n"
810 verbose "$comp_output" 3
812 set exitstat "succeeded"
813 if { $cmdret != 0 } { set exitstat "failed" }
814 verbose -log "$exitstat with: <$comp_output>"
820 set expmsg $opts(error)
821 if { $opts(warning) != "" } {
822 if { $expmsg != "" } {
823 perror "$testname: mixing error and warning test-directives"
826 set expmsg $opts(warning)
829 # Perhaps link the file(s).
831 set objfile "tmpdir/dump"
832 catch "exec rm -f $objfile" exec_output
834 # Add -L$srcdir/$subdir so that the linker command can use
835 # linker scripts in the source directory.
836 set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
837 $opts(ld) -o $objfile $objfiles"
840 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
841 remote_upload host "ld.tmp"
842 set comp_output [prune_warnings [file_contents "ld.tmp"]]
843 remote_file host delete "ld.tmp"
844 remote_file build delete "ld.tmp"
845 set cmdret [lindex $cmdret 0]
847 if { $cmdret == 0 && $run_objcopy } {
849 set objfile "tmpdir/dump1"
850 remote_file host delete $objfile
852 # Note that we don't use OBJCOPYFLAGS here; any flags must be
853 # explicitly specified.
854 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
857 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
858 remote_upload host "ld.tmp"
859 append comp_output [prune_warnings [file_contents "ld.tmp"]]
860 remote_file host delete "ld.tmp"
861 remote_file build delete "ld.tmp"
862 set cmdret [lindex $cmdret 0]
865 regsub "\n$" $comp_output "" comp_output
866 if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
867 set exitstat "succeeded"
868 if { $cmdret != 0 } { set exitstat "failed" }
869 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
870 send_log "$comp_output\n"
871 verbose "$comp_output" 3
873 if { [regexp $expmsg $comp_output] \
874 && (($cmdret == 0) == ($opts(warning) != "")) } {
875 # We have the expected output from ld.
876 if { $opts(error) != "" || $program == "" } {
881 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
887 set objfile "tmpdir/dump0.o"
890 # We must not have expected failure if we get here.
891 if { $opts(error) != "" } {
896 set progopts1 $opts($program)
897 eval set progopts \$[string toupper $program]FLAGS
898 eval set binary \$[string toupper $program]
900 if { ![is_remote host] && [which $binary] == 0 } {
905 if { $progopts1 == "" } { set $progopts1 "-r" }
906 verbose "running $binary $progopts $progopts1" 3
908 # Objcopy, unlike the other two, won't send its output to stdout,
909 # so we have to run it specially.
910 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
911 if { $program == "objcopy" } {
912 set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
915 # Ensure consistent sorting of symbols
916 if {[info exists env(LC_ALL)]} {
917 set old_lc_all $env(LC_ALL)
921 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
922 remote_upload host "ld.tmp"
923 set comp_output [prune_warnings [file_contents "ld.tmp"]]
924 remote_file host delete "ld.tmp"
925 remote_file build delete "ld.tmp"
926 if {[info exists old_lc_all]} {
927 set env(LC_ALL) $old_lc_all
931 if ![string match "" $comp_output] then {
932 send_log "$comp_output\n"
937 verbose_eval {[file_contents $dumpfile]} 3
938 if { [regexp_diff $dumpfile "${file}.d"] } then {
940 verbose "output is [file_contents $dumpfile]" 2
947 proc slurp_options { file } {
948 if [catch { set f [open $file r] } x] {
949 #perror "couldn't open `$file': $x"
954 # whitespace expression
957 # whitespace is ignored anywhere except within the options list;
958 # option names are alphabetic plus underscore only.
959 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
960 while { [gets $f line] != -1 } {
961 set line [string trim $line]
962 # Whitespace here is space-tab.
963 if [regexp $pat $line xxx opt_name opt_val] {
965 lappend opt_array [list $opt_name $opt_val]
974 # regexp_diff, copied from gas, based on simple_diff above.
975 # compares two files line-by-line
976 # file1 contains strings, file2 contains regexps and #-comments
977 # blank lines are ignored in either file
978 # returns non-zero if differences exist
980 proc regexp_diff { file_1 file_2 } {
988 if [file exists $file_1] then {
989 set file_a [open $file_1 r]
991 warning "$file_1 doesn't exist"
995 if [file exists $file_2] then {
996 set file_b [open $file_2 r]
998 fail "$file_2 doesn't exist"
1003 verbose " Regexp-diff'ing: $file_1 $file_2" 2
1008 while { [string length $line_a] == 0 } {
1009 if { [gets $file_a line_a] == $eof } {
1014 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
1015 if [ string match "#pass" $line_b ] {
1019 } elseif [ string match "#..." $line_b ] {
1020 if { [gets $file_b line_b] == $eof } {
1025 verbose "looking for \"^$line_b$\"" 3
1026 while { ![regexp "^$line_b$" "$line_a"] } {
1027 verbose "skipping \"$line_a\"" 3
1028 if { [gets $file_a line_a] == $eof } {
1035 if { [gets $file_b line_b] == $eof } {
1043 } elseif { $end_1 && $end_2 } {
1045 } elseif { $end_1 } {
1046 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
1047 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
1050 } elseif { $end_2 } {
1051 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
1052 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
1056 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
1057 if ![regexp "^$line_b$" "$line_a"] {
1058 send_log "regexp_diff match failure\n"
1059 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
1065 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
1066 send_log "$file_1 and $file_2 are different lengths\n"
1067 verbose "$file_1 and $file_2 are different lengths" 3
1077 proc file_contents { filename } {
1078 set file [open $filename r]
1079 set contents [read $file]
1084 # List contains test-items with 3 items followed by 2 lists, one item and
1085 # one optional item:
1086 # 0:name 1:ld options 2:assembler options
1087 # 3:filenames of assembler files 4: action and options. 5: name of output file
1088 # 6:compiler flags (optional)
1091 # objdump: Apply objdump options on result. Compare with regex (last arg).
1092 # nm: Apply nm options on result. Compare with regex (last arg).
1093 # readelf: Apply readelf options on result. Compare with regex (last arg).
1095 proc run_ld_link_tests { ldtests } {
1107 foreach testitem $ldtests {
1108 set testname [lindex $testitem 0]
1109 set ld_options [lindex $testitem 1]
1110 set as_options [lindex $testitem 2]
1111 set src_files [lindex $testitem 3]
1112 set actions [lindex $testitem 4]
1113 set binfile tmpdir/[lindex $testitem 5]
1114 set cflags [lindex $testitem 6]
1119 # verbose -log "Testname is $testname"
1120 # verbose -log "ld_options is $ld_options"
1121 # verbose -log "as_options is $as_options"
1122 # verbose -log "src_files is $src_files"
1123 # verbose -log "actions is $actions"
1124 # verbose -log "binfile is $binfile"
1126 # Assemble each file in the test.
1127 foreach src_file $src_files {
1128 set objfile "tmpdir/[file rootname $src_file].o"
1129 lappend objfiles $objfile
1131 if { [file extension $src_file] == ".c" } {
1132 set as_file "tmpdir/[file rootname $src_file].s"
1133 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1138 set as_file "$srcdir/$subdir/$src_file"
1140 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1146 # Catch assembler errors.
1147 if { $is_unresolved != 0 } {
1148 unresolved $testname
1152 if ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1156 foreach actionlist $actions {
1157 set action [lindex $actionlist 0]
1158 set progopts [lindex $actionlist 1]
1160 # There are actions where we run regexp_diff on the
1161 # output, and there are other actions (presumably).
1162 # Handling of the former look the same.
1166 { set dump_prog $objdump }
1168 { set dump_prog $nm }
1170 { set dump_prog $READELF }
1173 perror "Unrecognized action $action"
1179 if { $dump_prog != "" } {
1180 set dumpfile [lindex $actionlist 2]
1181 set binary $dump_prog
1183 # Ensure consistent sorting of symbols
1184 if {[info exists env(LC_ALL)]} {
1185 set old_lc_all $env(LC_ALL)
1188 set cmd "$binary $progopts $binfile"
1189 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1191 remote_upload host "ld.stderr"
1192 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1193 remote_file host delete "ld.stderr"
1194 remote_file build delete "ld.stderr"
1196 if {[info exists old_lc_all]} {
1197 set env(LC_ALL) $old_lc_all
1202 if ![string match "" $comp_output] then {
1203 send_log "$comp_output\n"
1208 remote_upload host "dump.out"
1210 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1211 verbose "output is [file_contents "dump.out"]" 2
1213 remote_file build delete "dump.out"
1214 remote_file host delete "dump.out"
1217 remote_file build delete "dump.out"
1218 remote_file host delete "dump.out"
1222 if { $failed != 0 } {
1224 } else { if { $is_unresolved == 0 } {
1229 # Catch action errors.
1230 if { $is_unresolved != 0 } {
1231 unresolved $testname
1238 proc verbose_eval { expr { level 1 } } {
1240 if $verbose>$level then { eval verbose "$expr" $level }
1243 # This definition is taken from an unreleased version of DejaGnu. Once
1244 # that version gets released, and has been out in the world for a few
1245 # months at least, it may be safe to delete this copy.
1246 if ![string length [info proc prune_warnings]] {
1248 # prune_warnings -- delete various system verbosities from TEXT
1251 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
1253 # Sites with particular verbose os's may wish to override this in site.exp.
1255 proc prune_warnings { text } {
1256 # This is from sun4's. Do it for all machines for now.
1257 # The "\\1" is to try to preserve a "\n" but only if necessary.
1258 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
1260 # It might be tempting to get carried away and delete blank lines, etc.
1261 # Just delete *exactly* what we're ask to, and that's it.
1266 # targets_to_xfail is a list of target triplets to be xfailed.
1267 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1268 # and 3 optional items:
1271 # 2:assembler options
1272 # 3:filenames of source files
1273 # 4:name of output file
1275 # 6:compiler flags (optional)
1276 # 7:language (optional)
1277 # 8:linker warning (optional)
1279 proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
1291 foreach testitem $ldtests {
1292 foreach target $targets_to_xfail {
1295 set testname [lindex $testitem 0]
1296 set ld_options [lindex $testitem 1]
1297 set as_options [lindex $testitem 2]
1298 set src_files [lindex $testitem 3]
1299 set binfile tmpdir/[lindex $testitem 4]
1300 set expfile [lindex $testitem 5]
1301 set cflags [lindex $testitem 6]
1302 set lang [lindex $testitem 7]
1303 set warning [lindex $testitem 8]
1307 # verbose -log "Testname is $testname"
1308 # verbose -log "ld_options is $ld_options"
1309 # verbose -log "as_options is $as_options"
1310 # verbose -log "src_files is $src_files"
1311 # verbose -log "actions is $actions"
1312 # verbose -log "binfile is $binfile"
1314 # Assemble each file in the test.
1315 foreach src_file $src_files {
1316 set objfile "tmpdir/[file rootname $src_file].o"
1317 lappend objfiles $objfile
1319 # We ignore warnings since some compilers may generate
1320 # incorrect section attributes and the assembler will warn
1322 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1324 # We have to use $CC to build PIE and shared library.
1325 if { [ string match "c" $lang ] } {
1326 set link_proc ld_simple_link
1328 } elseif { [ string match "c++" $lang ] } {
1329 set link_proc ld_simple_link
1331 } elseif { [ string match "-shared" $ld_options ] \
1332 || [ string match "-pie" $ld_options ] } {
1333 set link_proc ld_simple_link
1336 set link_proc ld_link
1340 if ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
1346 # Check if exec_output is expected.
1347 if { $warning != "" } then {
1348 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1349 if { [regexp $warning $exec_output] } then {
1356 if { $failed == 0 } {
1357 send_log "Running: $binfile > $binfile.out\n"
1358 verbose "Running: $binfile > $binfile.out"
1359 catch "exec $binfile > $binfile.out" exec_output
1361 if ![string match "" $exec_output] then {
1362 send_log "$exec_output\n"
1363 verbose "$exec_output" 1
1366 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1367 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1368 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1369 set exec_output [prune_warnings $exec_output]
1371 if ![string match "" $exec_output] then {
1372 send_log "$exec_output\n"
1373 verbose "$exec_output" 1
1379 if { $failed != 0 } {
1389 # List contains test-items with 3 items followed by 2 lists, one item and
1390 # one optional item:
1394 # 3:filenames of source files
1395 # 4:action and options.
1396 # 5:name of output file
1397 # 6:language (optional)
1400 # objdump: Apply objdump options on result. Compare with regex (last arg).
1401 # nm: Apply nm options on result. Compare with regex (last arg).
1402 # readelf: Apply readelf options on result. Compare with regex (last arg).
1404 proc run_cc_link_tests { ldtests } {
1415 foreach testitem $ldtests {
1416 set testname [lindex $testitem 0]
1417 set ldflags [lindex $testitem 1]
1418 set cflags [lindex $testitem 2]
1419 set src_files [lindex $testitem 3]
1420 set actions [lindex $testitem 4]
1421 set binfile tmpdir/[lindex $testitem 5]
1422 set lang [lindex $testitem 6]
1427 # Compile each file in the test.
1428 foreach src_file $src_files {
1429 set objfile "tmpdir/[file rootname $src_file].o"
1430 lappend objfiles $objfile
1432 # We ignore warnings since some compilers may generate
1433 # incorrect section attributes and the assembler will warn
1435 ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
1438 # Clear error and warning counts.
1441 if { [ string match "c++" $lang ] } {
1447 if ![ld_simple_link $cc_cmd $binfile "-L$srcdir/$subdir $ldflags $objfiles"] {
1451 foreach actionlist $actions {
1452 set action [lindex $actionlist 0]
1453 set progopts [lindex $actionlist 1]
1455 # There are actions where we run regexp_diff on the
1456 # output, and there are other actions (presumably).
1457 # Handling of the former look the same.
1461 { set dump_prog $objdump }
1463 { set dump_prog $nm }
1465 { set dump_prog $READELF }
1468 perror "Unrecognized action $action"
1474 if { $dump_prog != "" } {
1475 set dumpfile [lindex $actionlist 2]
1476 set binary $dump_prog
1478 # Ensure consistent sorting of symbols
1479 if {[info exists env(LC_ALL)]} {
1480 set old_lc_all $env(LC_ALL)
1483 set cmd "$binary $progopts $binfile > dump.out"
1485 catch "exec $cmd" comp_output
1486 if {[info exists old_lc_all]} {
1487 set env(LC_ALL) $old_lc_all
1491 set comp_output [prune_warnings $comp_output]
1493 if ![string match "" $comp_output] then {
1494 send_log "$comp_output\n"
1499 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1500 verbose "output is [file_contents "dump.out"]" 2
1507 if { $failed != 0 } {
1509 } else { if { $is_unresolved == 0 } {
1514 # Catch action errors.
1515 if { $is_unresolved != 0 } {
1516 unresolved $testname