1 # Copyright (C) 1993-2018 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
20 # This file was written by Rob Savoye <rob@cygnus.com>
21 # and extended by Ian Lance Taylor <ian@cygnus.com>
23 proc load_common_lib { name } {
27 load_common_lib binutils-common.exp
29 proc binutil_version { prog } {
30 if ![is_remote host] {
31 set path [which $prog]
32 if {$path == 0} then {
33 perror "$prog can't be run, file not found."
39 set state [remote_exec host $prog --version]
40 set tmp "[lindex $state 1]\n"
41 # Should find a way to discard constant parts, keep whatever's
42 # left, so the version string could be almost anything at all...
43 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" "$tmp" version cyg number
44 if ![info exists number] then {
45 return "$path (no version number)\n"
47 return "$path $number\n"
51 # default_binutils_run
52 # run a program, returning the output
53 # sets binutils_run_failed if the program does not exist
54 # sets binutils_run_status to the exit status of the program
56 proc default_binutils_run { prog progargs } {
57 global binutils_run_failed
58 global binutils_run_status
61 set binutils_run_failed 0
62 if [info exists binutils_run_status] {
63 unset binutils_run_status
66 if ![is_remote host] {
67 if {[which $prog] == 0} then {
68 perror "$prog does not exist"
69 set binutils_run_failed 1
74 # For objdump, automatically translate standard section
75 # names to the targets one, if they are different.
76 set sect_names [get_standard_section_names]
77 if { $sect_names != "" && [string match "*objdump" $prog] } {
78 regsub -- "-j \\.text" $progargs "-j [lindex $sect_names 0]" progargs
79 regsub -- "-j \\.data" $progargs "-j [lindex $sect_names 1]" progargs
80 regsub -- "-j \\.bss" $progargs "-j [lindex $sect_names 2]" progargs
83 send_log "$prog $progargs\n"
84 verbose "$prog $progargs"
86 # Gotta quote dollar-signs because they get mangled by the
88 regsub -all "\\$" "$progargs" "\\$" progargs
90 set state [remote_exec host $prog $progargs]
91 set binutils_run_status [lindex $state 0]
92 set exec_output [prune_warnings [lindex $state 1]]
93 if {![string match "" $exec_output]} then {
94 send_log "$exec_output\n"
95 verbose "$exec_output"
97 if { [lindex $state 0] != 0 } {
98 set exec_output "$prog exited with status [lindex $state 0]"
99 send_log "$exec_output\n"
100 verbose "$exec_output"
107 # default_binutils_assemble_flags
110 proc default_binutils_assemble_flags { source object asflags } {
114 # The HPPA assembler syntax is a little different than most, to make
115 # the test source file assemble we need to run it through sed.
117 # This is a hack in that it won't scale well if other targets need
118 # similar transformations to assemble. We'll generalize the hack
119 # if/when other targets need similar handling.
120 if { [istarget "hppa*-*-*"] && ![istarget "*-*-linux*" ] } then {
121 set sed_file $srcdir/config/hppa.sed
122 send_log "sed -f $sed_file < $source > asm.s\n"
123 verbose "sed -f $sed_file < $source > asm.s"
124 catch "exec sed -f $sed_file < $source > asm.s"
128 set exec_output [target_assemble $source $object $asflags]
129 set exec_output [prune_warnings $exec_output]
131 if [string match "" $exec_output] {
134 send_log "$exec_output\n"
135 verbose "$exec_output"
142 # Returns target executable extension, if any.
145 if { [istarget *-*-mingw*] || [istarget *-*-cygwin*] } {
152 # Copied and modified from gas.
154 # run_dump_test FILE (optional:) EXTRA_OPTIONS
156 # Assemble a .s file, then run some utility on it and check the output.
158 # There should be an assembly language file named FILE.s in the test
159 # suite directory, and a pattern file called FILE.d. `run_dump_test'
160 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
161 # `nm' on the .o file to produce textual output, and then analyze that
162 # with regexps. The FILE.d file specifies what program to run, and
163 # what to expect in its output.
165 # The FILE.d file begins with zero or more option lines, which specify
166 # flags to pass to the assembler, the program to run to dump the
167 # assembler's output, and the options it wants. The option lines have
172 # OPTION is the name of some option, like "name" or "objdump", and
173 # VALUE is OPTION's value. The valid options are described below.
174 # Whitespace is ignored everywhere, except within VALUE. The option
175 # list ends with the first line that doesn't match the above syntax.
176 # However, a line within the options that begins with a #, but doesn't
177 # have a recognizable option name followed by a colon, is considered a
178 # comment and entirely ignored.
180 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
181 # two-element lists. The first element of each is an option name, and
182 # the second additional arguments to be added on to the end of the
183 # option list as given in FILE.d. (If omitted, no additional options
186 # The interesting options are:
189 # The name of this test, passed to DejaGNU's `pass' and `fail'
190 # commands. If omitted, this defaults to FILE, the root of the
191 # .s and .d files' names.
194 # When assembling FILE.s, pass FLAGS to the assembler.
197 # The name of the program to run to modify or analyze the .o file
198 # produced by the assembler. This option is required. Recognised
199 # names are: ar, elfedit, nm, objcopy, ranlib, strings, and strip.
201 # DUMPPROG: PROGRAM-NAME
202 # The name of the program to run to analyze the .o file after it has
203 # has been modified by PROG. This can be omitted; run_dump_test will
204 # guess which program to run by seeing if any of the flags options
205 # for the recognised dump programs are set. Recognised names are:
206 # addr2line, nm, objdump, readelf and size.
213 # Use the specified program to analyze the .o file, and pass it
214 # FLAGS, in addition to the .o file name. Note that they are run
215 # with LC_ALL=C in the environment to give consistent sorting
219 # Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
220 # This is useful if several .d files want to share a .s file.
223 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
224 # is useful if several .d files differ by options only. Options are
225 # always read from FILE.d.
228 # Run this test only on a specified list of targets. More precisely,
229 # each glob in the space-separated list is passed to "istarget"; if
230 # it evaluates true for any of them, the test will be run, otherwise
231 # it will be marked unsupported.
233 # not-target: GLOBS...
234 # Do not run this test on a specified list of targets. Again,
235 # the each glob in the space-separated list is passed to
236 # "istarget", and the test is run if it evaluates *false* for
237 # *all* of them. Otherwise it will be marked unsupported.
241 # These are exactly the same as "not-target" and "target",
242 # respectively, except that they do nothing at all if the check
243 # fails. They should only be used in groups, to construct a single
244 # test which is run on all targets but with variant options or
245 # expected output on some targets. (For example, see
246 # gas/arm/inst.d and gas/arm/wince_inst.d.)
249 # An error with message matching REGEX must be emitted for the test
250 # to pass. The DUMPPROG, addr2line, nm, objdump, readelf and size
251 # options have no meaning and need not supplied if this is present.
252 # Multiple "error" directives append to the expected error message.
255 # Means the same as 'error', except the regular expression lines
256 # are contains in FILE.
259 # Expect a warning matching REGEX. It is an error to issue both
260 # "error" and "warning". Multiple "warning" directives append to
261 # the expected linker warning message.
263 # warning_output: FILE
264 # Means the same as 'warning', except the regular expression
265 # lines are contains in FILE.
267 # Each option may occur at most once.
269 # After the option lines come regexp lines. `run_dump_test' calls
270 # `regexp_diff' to compare the output of the dumping tool against the
271 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
272 # see further comments there.
274 proc run_dump_test { name {extra_options {}} } {
276 global OBJDUMP NM OBJCOPY READELF STRIP
277 global OBJDUMPFLAGS NMFLAGS OBJCOPYFLAGS READELFFLAGS STRIPFLAGS
278 global ELFEDIT ELFEDITFLAGS
279 global binutils_run_status
285 if [string match "*/*" $name] {
287 set name [file tail $name]
289 set file "$srcdir/$subdir/$name"
291 set opt_array [slurp_options "${file}.d"]
292 if { $opt_array == -1 } {
293 perror "error reading options from $file.d"
294 unresolved $subdir/$name
297 set opts(addr2line) {}
311 set opts(DUMPPROG) {}
316 set opts(error_output) {}
317 set opts(warning_output) {}
319 set opts(not-target) {}
321 set opts(not-skip) {}
323 foreach i $opt_array {
324 set opt_name [lindex $i 0]
325 set opt_val [lindex $i 1]
326 if ![info exists opts($opt_name)] {
327 perror "unknown option $opt_name in file $file.d"
328 unresolved $subdir/$name
332 # Permit the option to use $srcdir to refer to the source
334 regsub -all "\\\$srcdir" "$opt_val" "$srcdir/$subdir" opt_val
336 switch -- $opt_name {
340 if [string length $opts($opt_name)] {
341 perror "option $opt_name multiply set in $file.d"
342 unresolved $subdir/$name
347 append opts($opt_name) $opt_val
350 foreach i $extra_options {
351 set opt_name [lindex $i 0]
352 set opt_val [lindex $i 1]
353 if ![info exists opts($opt_name)] {
354 perror "unknown option $opt_name given in extra_opts"
355 unresolved $subdir/$name
359 # Permit the option to use $srcdir to refer to the source
361 regsub -all "\\\$srcdir" "$opt_val" "$srcdir/$subdir" opt_val
363 # add extra option to end of existing option, adding space
365 if { ![regexp "warning|error" $opt_name]
366 && [string length $opts($opt_name)] } {
367 append opts($opt_name) " "
369 append opts($opt_name) $opt_val
372 if { $opts(name) == "" } {
373 set testname "$subdir/$name"
375 set testname $opts(name)
377 verbose "Testing $testname"
379 if {$opts(PROG) == ""} {
380 perror "PROG isn't set in $file.d"
386 switch -- $opts(PROG) {
387 ar { set program ar }
388 elfedit { set program elfedit }
389 nm { set program nm }
390 objcopy { set program objcopy }
391 ranlib { set program ranlib }
392 strings { set program strings }
398 perror "unrecognized program option $opts(PROG) in $file.d"
404 # It's meaningless to require an output-testing method when we
406 if { $opts(error) == "" && $opts(error_output) == "" } {
407 if { $opts(DUMPPROG) != "" } {
408 switch -- $opts(DUMPPROG) {
409 addr2line { set dumpprogram addr2line }
410 nm { set dumpprogram nm }
411 objdump { set dumpprogram objdump }
412 readelf { set dumpprogram readelf }
413 size { set dumpprogram size }
415 perror "unrecognized dump program option $opts(DUMPPROG)\
422 # Guess which program to run, by seeing which option was specified.
423 foreach p {addr2line nm objdump readelf size} {
424 if {$opts($p) != ""} {
425 if {$dumpprogram != ""} {
426 perror "more than one possible dump program specified\
438 # Handle skipping the test on specified targets.
439 # You can have both skip/not-skip and target/not-target, but you can't
440 # have both skip and not-skip, or target and not-target, in the same file.
441 if { $opts(skip) != "" } then {
442 if { $opts(not-skip) != "" } then {
443 perror "$testname: mixing skip and not-skip directives is invalid"
447 foreach glob $opts(skip) {
448 if {[istarget $glob]} { return }
451 if { $opts(not-skip) != "" } then {
453 foreach glob $opts(not-skip) {
454 if {[istarget $glob]} {
459 if {$skip} { return }
461 if { $opts(target) != "" } then {
463 foreach glob $opts(target) {
464 if {[istarget $glob]} {
470 unsupported $testname
474 if { $opts(not-target) != "" } then {
475 foreach glob $opts(not-target) {
476 if {[istarget $glob]} {
477 unsupported $testname
483 if { $opts(source) == "" } {
484 set srcfile ${file}.s
486 set srcfile $srcdir/$subdir/$opts(source)
489 if { $opts(dump) == "" } {
490 set dumpfile ${file}.d
492 set dumpfile $srcdir/$subdir/$opts(dump)
495 if { $opts(as) == "binary" } {
496 while {[file type $srcfile] eq "link"} {
497 set newfile [file readlink $srcfile]
498 if {[string index $newfile 0] ne "/"} {
499 set newfile [file dirname $srcfile]/$newfile
503 # Make sure we copy the file if we are doing remote host testing.
504 remote_download host ${srcfile} $tempfile
506 set exec_output [binutils_assemble_flags ${srcfile} $tempfile $opts(as)]
507 if [string match "" $exec_output] then {
508 send_log "$exec_output\n"
509 verbose "$exec_output"
515 if { (($opts(warning) != "") && ($opts(error) != "")) \
516 || (($opts(warning) != "") && ($opts(error_output) != "")) \
517 || (($opts(warning) != "") && ($opts(warning_output) != "")) \
518 || (($opts(error) != "") && ($opts(warning_output) != "")) \
519 || (($opts(error) != "") && ($opts(error_output) != "")) \
520 || (($opts(warning_output) != "") && ($opts(error_output) != "")) } {
521 perror "bad mix of warning, error, warning_output, and error_output\
527 set check_prog(source) ""
528 set check_prog(terminal) 0
529 if { $opts(error) != "" \
530 || $opts(warning) != "" \
531 || $opts(error_output) != "" \
532 || $opts(warning_output) != "" } {
534 if { $opts(error) != "" || $opts(error_output) != "" } {
535 set check_prog(terminal) 1
537 set check_prog(terminal) 0
540 if { $opts(error) != "" || $opts(warning) != "" } {
541 set check_prog(source) "regex"
542 if { $opts(error) != "" } {
543 set check_prog(regex) $opts(error)
545 set check_prog(regex) $opts(warning)
548 set check_prog(source) "file"
549 if { $opts(error_output) != "" } {
550 set check_prog(file) $opts(error_output)
552 set check_prog(file) $opts(warning_output)
557 set progopts1 $opts($program)
558 eval set progopts \$[string toupper $program]FLAGS
559 eval set binary \$[string toupper $program]
561 set exec_output [binutils_run $binary "$progopts $progopts1 $tempfile $destopt ${copyfile}.o"]
563 if [info exists binutils_run_status] {
564 set cmdret $binutils_run_status
567 regsub "\n$" $exec_output "" exec_output
568 if { $cmdret != 0 || $exec_output != "" || $check_prog(source) != "" } {
569 set exitstat "succeeded"
570 if { $cmdret != 0 } {
571 set exitstat "failed"
574 if { $check_prog(source) == "regex" } {
575 verbose -log "$exitstat with: <$exec_output>,\
576 expected: <$check_prog(regex)>"
577 } elseif { $check_prog(source) == "file" } {
578 verbose -log "$exitstat with: <$exec_output>,\
579 expected in file $check_prog(file)"
580 set_file_contents "tmpdir/prog.messages" "$exec_output"
582 verbose -log "$exitstat with: <$exec_output>, no expected output"
584 send_log -- "$exec_output\n"
585 verbose "$exec_output"
587 if { (($check_prog(source) == "") == ($exec_output == "")) \
588 && (($cmdret == 0) == ($check_prog(terminal) == 0)) \
589 && ((($check_prog(source) == "regex") \
590 && ($check_prog(regex) == "") == ($exec_output == "") \
591 && [regexp -- $check_prog(regex) $exec_output]) \
592 || (($check_prog(source) == "file") \
593 && (![regexp_diff "tmpdir/prog.messages" \
594 "$srcdir/$subdir/$check_prog(file)"]))) } {
595 # We have the expected output from prog.
596 if { $check_prog(terminal) || $program == "" } {
606 set progopts1 $opts($dumpprogram)
607 eval set progopts \$[string toupper $dumpprogram]FLAGS
608 eval set binary \$[string toupper $dumpprogram]
610 if { ![is_remote host] && [which $binary] == 0 } {
615 # For objdump, automatically translate standard section names to the targets one,
616 # if they are different.
617 set sect_names [get_standard_section_names]
618 if { $sect_names != "" && $dumpprogram == "objdump"} {
619 regsub -- "-j \\.text" $progopts1 "-j [lindex $sect_names 0]" progopts1
620 regsub -- "-j \\.data" $progopts1 "-j [lindex $sect_names 1]" progopts1
621 regsub -- "-j \\.bss" $progopts1 "-j [lindex $sect_names 2]" progopts1
624 verbose "running $binary $progopts $progopts1" 3
626 set cmd "$binary $progopts $progopts1 ${copyfile}.o"
628 # Ensure consistent sorting of symbols
629 if {[info exists env(LC_ALL)]} {
630 set old_lc_all $env(LC_ALL)
634 set comp_output [remote_exec host $cmd "" "/dev/null" "tmpdir/dump.out"]
635 if {[info exists old_lc_all]} {
636 set env(LC_ALL) $old_lc_all
640 if { [lindex $comp_output 0] != 0 } then {
641 send_log "$comp_output\n"
645 set comp_output [prune_warnings [lindex $comp_output 1]]
646 if ![string match "" $comp_output] then {
647 send_log "$comp_output\n"
652 verbose_eval {[file_contents "tmpdir/dump.out"]} 3
653 if { [regexp_diff "tmpdir/dump.out" "${dumpfile}"] } then {
655 verbose "output is [file_contents "tmpdir/dump.out"]" 2
662 proc slurp_options { file } {
663 if [catch { set f [open $file r] } x] {
664 #perror "couldn't open `$file': $x"
669 # whitespace expression
672 # whitespace is ignored anywhere except within the options list;
673 # option names are alphabetic plus dash
674 set pat "^#${ws}(\[a-zA-Z-\]*)$ws:${ws}(.*)$ws\$"
675 while { [gets $f line] != -1 } {
676 set line [string trim $line]
677 # Whitespace here is space-tab.
678 if [regexp $pat $line xxx opt_name opt_val] {
680 lappend opt_array [list $opt_name $opt_val]
681 } elseif {![regexp "^#" $line ]} {
689 proc file_contents { filename } {
690 set file [open $filename r]
691 set contents [read $file]
696 proc verbose_eval { expr { level 1 } } {
698 if $verbose>$level then { eval verbose "$expr" $level }
701 # Internal procedure: return the names of the standard sections
703 proc get_standard_section_names {} {
704 if [istarget "rx-*-*"] {
705 return { "P" "D_1" "B_1" }
707 if [istarget "alpha*-*-*vms*"] {
708 # Double quote: for TCL and for sh.
709 return { "\\\$CODE\\\$" "\\\$DATA\\\$" "\\\$BSS\\\$" }