Add tests for PR gas/9966 and PR gas/11356.
[binutils-gdb.git] / gas / testsuite / lib / gas-defs.exp
1 # Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 # 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
17 # MA 02110-1301, USA.
18
19 # Please email any bugs, comments, and/or additions to this file to:
20 # dejagnu@gnu.org
21
22 # This file was written by Ken Raeburn (raeburn@cygnus.com).
23
24 proc gas_version {} {
25 global AS
26 if [is_remote host] then {
27 remote_exec host "$AS -version < /dev/null" "" "" "gas.version"
28 remote_exec host "which $AS" "" "" "gas.which"
29
30 remote_upload host "gas.version"
31 remote_upload host "gas.which"
32
33 set which_as [file_contents "gas.which"]
34 set tmp [file_contents "gas.version"]
35
36 remote_file build delete "gas.version"
37 remote_file build delete "gas.which"
38 remote_file host delete "gas.version"
39 remote_file host delete "gas.which"
40 } else {
41 set which_as [which $AS]
42 catch "exec $AS -version < /dev/null" tmp
43 }
44
45 # Should find a way to discard constant parts, keep whatever's
46 # left, so the version string could be almost anything at all...
47 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
48 if ![info exists number] then {
49 return "$which_as (no version number)\n"
50 }
51 clone_output "$which_as $number\n"
52 unset version
53 }
54
55 proc gas_host_run { cmd redir } {
56 verbose "Executing $cmd $redir"
57 set return_contents_of ""
58 if [regexp ">& */dev/null" $redir] then {
59 set output_file ""
60 set command "$cmd $redir"
61 } elseif [regexp "> */dev/null" $redir] then {
62 set output_file ""
63 set command "$cmd 2>gas.stderr"
64 set return_contents_of "gas.stderr"
65 } elseif [regexp ">&.*" $redir] then {
66 # See PR 5322 for why the following line is used.
67 regsub ">&" $redir "" output_file
68 set command "$cmd 2>&1"
69 } elseif [regexp "2>.*" $redir] then {
70 set output_file "gas.out"
71 set command "$cmd $redir"
72 set return_contents_of "gas.out"
73 } elseif [regexp ">.*" $redir] then {
74 set output_file ""
75 set command "$cmd $redir 2>gas.stderr"
76 set return_contents_of "gas.stderr"
77 } elseif { "$redir" == "" } then {
78 set output_file "gas.out"
79 set command "$cmd 2>&1"
80 set return_contents_of "gas.out"
81 } else {
82 fail "gas_host_run: unknown form of redirection string"
83 }
84
85 set status [remote_exec host [concat sh -c [list $command]] "" "/dev/null" "$output_file"]
86 set to_return ""
87 if { "$return_contents_of" != "" } then {
88 remote_upload host "$return_contents_of"
89 set to_return [file_contents "$return_contents_of"]
90 regsub "\n$" $to_return "" to_return
91 }
92
93 if { [lindex $status 0] == 0 && "$output_file" != ""
94 && "$output_file" != "$return_contents_of" } then {
95 remote_upload host "$output_file"
96 }
97
98 return [list [lindex $status 0] "$to_return"]
99 }
100
101 proc gas_run { prog as_opts redir } {
102 global AS
103 global ASFLAGS
104 global comp_output
105 global srcdir
106 global subdir
107 global host_triplet
108
109 set status [gas_host_run "$AS $ASFLAGS $as_opts $srcdir/$subdir/$prog" "$redir"]
110 set comp_output [lindex $status 1]
111 if { [lindex $status 0] != 0 && [regexp "2>.*" $redir] } then {
112 append comp_output "child process exited abnormally"
113 }
114 set comp_output [prune_warnings $comp_output]
115 verbose "output was $comp_output"
116 return [list $comp_output ""]
117 }
118
119 proc gas_run_stdin { prog as_opts redir } {
120 global AS
121 global ASFLAGS
122 global comp_output
123 global srcdir
124 global subdir
125 global host_triplet
126
127 set status [gas_host_run "$AS $ASFLAGS $as_opts < $srcdir/$subdir/$prog" "$redir"]
128 set comp_output [lindex $status 1]
129 if { [lindex $status 0] != 0 && [regexp "2>.*" $redir] } then {
130 append comp_output "child process exited abnormally"
131 }
132 set comp_output [prune_warnings $comp_output]
133 verbose "output was $comp_output"
134 return [list $comp_output ""]
135 }
136
137 proc all_ones { args } {
138 foreach x $args { if [expr $x!=1] { return 0 } }
139 return 1
140 }
141
142 # ${tool}_finish (gas_finish) will be called by runtest.exp. But
143 # gas_finish should only be used with gas_start. We use gas_started
144 # to tell gas_finish if gas_start has been called so that runtest.exp
145 # can call gas_finish without closing the wrong fd.
146 set gas_started 0
147
148 proc gas_start { prog as_opts } {
149 global AS
150 global ASFLAGS
151 global srcdir
152 global subdir
153 global spawn_id
154 global gas_started
155
156 set gas_started 1
157
158 verbose -log "Starting $AS $ASFLAGS $as_opts $prog" 2
159 set status [gas_host_run "$AS $ASFLAGS $as_opts $srcdir/$subdir/$prog" ">&gas.out"]
160 spawn -noecho -nottycopy cat gas.out
161 }
162
163 proc gas_finish { } {
164 global spawn_id
165 global gas_started
166
167 if { $gas_started == 1 } {
168 catch "close"
169 catch "wait"
170 set gas_started 0
171 }
172 }
173
174 proc want_no_output { testname } {
175 global comp_output
176
177 if ![string match "" $comp_output] then {
178 send_log "$comp_output\n"
179 verbose "$comp_output" 3
180 }
181 if [string match "" $comp_output] then {
182 pass "$testname"
183 return 1
184 } else {
185 fail "$testname"
186 return 0
187 }
188 }
189
190 proc gas_test_old { file as_opts testname } {
191 gas_run $file $as_opts ""
192 return [want_no_output $testname]
193 }
194
195 proc gas_test { file as_opts var_opts testname } {
196 global comp_output
197
198 set i 0
199 foreach word $var_opts {
200 set ignore_stdout($i) [string match "*>" $word]
201 set opt($i) [string trim $word {>}]
202 incr i
203 }
204 set max [expr 1<<$i]
205 for {set i 0} {[expr $i<$max]} {incr i} {
206 set maybe_ignore_stdout ""
207 set extra_opts ""
208 for {set bit 0} {(1<<$bit)<$max} {incr bit} {
209 set num [expr 1<<$bit]
210 if [expr $i&$num] then {
211 set extra_opts "$extra_opts $opt($bit)"
212 if $ignore_stdout($bit) then {
213 set maybe_ignore_stdout ">/dev/null"
214 }
215 }
216 }
217 set extra_opts [string trim $extra_opts]
218 gas_run $file "$as_opts $extra_opts" $maybe_ignore_stdout
219
220 # Should I be able to use a conditional expression here?
221 if [string match "" $extra_opts] then {
222 want_no_output $testname
223 } else {
224 want_no_output "$testname ($extra_opts)"
225 }
226 }
227 if [info exists errorInfo] then {
228 unset errorInfo
229 }
230 }
231
232 proc gas_test_ignore_stdout { file as_opts testname } {
233 global comp_output
234
235 gas_run $file $as_opts ">/dev/null"
236 want_no_output $testname
237 }
238
239 proc gas_test_error { file as_opts testname } {
240 global comp_output
241
242 gas_run $file $as_opts ">/dev/null"
243 send_log "$comp_output\n"
244 verbose "$comp_output" 3
245 if { ![string match "" $comp_output]
246 && ![string match "*Assertion failure*" $comp_output]
247 && ![string match "*Internal error*" $comp_output] } then {
248 pass "$testname"
249 } else {
250 fail "$testname"
251 }
252 }
253
254 proc gas_exit {} {}
255
256 proc gas_init { args } {
257 global target_cpu
258 global target_cpu_family
259 global target_family
260 global target_vendor
261 global target_os
262 global stdoptlist
263
264 case "$target_cpu" in {
265 "m68???" { set target_cpu_family m68k }
266 "i[3-7]86" { set target_cpu_family i386 }
267 default { set target_cpu_family $target_cpu }
268 }
269
270 set target_family "$target_cpu_family-$target_vendor-$target_os"
271 set stdoptlist "-a>"
272
273 if ![istarget "*-*-*"] {
274 perror "Target name [istarget] is not a triple."
275 }
276 # Need to return an empty string.
277 return
278 }
279
280 #
281 # is_elf_format
282 # true if the object format is known to be ELF
283 #
284 proc is_elf_format {} {
285 if { ![istarget *-*-sysv4*] \
286 && ![istarget *-*-unixware*] \
287 && ![istarget *-*-elf*] \
288 && ![istarget *-*-eabi*] \
289 && ![istarget hppa*64*-*-hpux*] \
290 && ![istarget *-*-linux*] \
291 && ![istarget frv-*-uclinux*] \
292 && ![istarget *-*-irix5*] \
293 && ![istarget *-*-irix6*] \
294 && ![istarget *-*-netbsd*] \
295 && ![istarget *-*-openbsd*] \
296 && ![istarget *-*-solaris2*] } {
297 return 0
298 }
299
300 if { [istarget *-*-linux*aout*] \
301 || [istarget *-*-linux*oldld*] } {
302 return 0
303 }
304
305 if { ![istarget *-*-netbsdelf*] \
306 && ([istarget *-*-netbsd*aout*] \
307 || [istarget *-*-netbsdpe*] \
308 || [istarget arm*-*-netbsd*] \
309 || [istarget sparc-*-netbsd*] \
310 || [istarget i*86-*-netbsd*] \
311 || [istarget m68*-*-netbsd*] \
312 || [istarget vax-*-netbsd*] \
313 || [istarget ns32k-*-netbsd*]) } {
314 return 0
315 }
316
317 if { [istarget arm-*-openbsd*] \
318 || [istarget i386-*-openbsd\[0-2\].*] \
319 || [istarget i386-*-openbsd3.\[0-3\]] \
320 || [istarget m68*-*-openbsd*] \
321 || [istarget ns32k-*-openbsd*] \
322 || [istarget sparc-*-openbsd\[0-2\].*] \
323 || [istarget sparc-*-openbsd3.\[0-1\]] \
324 || [istarget vax-*-openbsd*] } {
325 return 0
326 }
327
328 return 1
329 }
330
331 # True if the object format is known to be PE COFF.
332 #
333 proc is_pecoff_format {} {
334 if { ![istarget *-*-mingw*] \
335 && ![istarget *-*-cygwin*] \
336 && ![istarget *-*-pe*] } {
337 return 0
338 }
339
340 return 1
341 }
342
343 # run_dump_tests TESTCASES EXTRA_OPTIONS
344 # Wrapper for run_dump_test, which is suitable for invoking as
345 # run_dump_tests [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
346 # EXTRA_OPTIONS are passed down to run_dump_test. Honors runtest_file_p.
347 # Body cribbed from dg-runtest.
348
349 proc run_dump_tests { testcases {extra_options {}} } {
350 global runtests
351
352 foreach testcase $testcases {
353 # If testing specific files and this isn't one of them, skip it.
354 if ![runtest_file_p $runtests $testcase] {
355 continue
356 }
357 run_dump_test [file rootname [file tail $testcase]] $extra_options
358 }
359 }
360
361
362 # run_dump_test FILE (optional:) EXTRA_OPTIONS
363 #
364 # Assemble a .s file, then run some utility on it and check the output.
365 #
366 # There should be an assembly language file named FILE.s in the test
367 # suite directory, and a pattern file called FILE.d. `run_dump_test'
368 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
369 # `nm' on the .o file to produce textual output, and then analyze that
370 # with regexps. The FILE.d file specifies what program to run, and
371 # what to expect in its output.
372 #
373 # The FILE.d file begins with zero or more option lines, which specify
374 # flags to pass to the assembler, the program to run to dump the
375 # assembler's output, and the options it wants. The option lines have
376 # the syntax:
377 #
378 # # OPTION: VALUE
379 #
380 # OPTION is the name of some option, like "name" or "objdump", and
381 # VALUE is OPTION's value. The valid options are described below.
382 # Whitespace is ignored everywhere, except within VALUE. The option
383 # list ends with the first line that doesn't match the above syntax.
384 # However, a line within the options that begins with a #, but doesn't
385 # have a recognizable option name followed by a colon, is considered a
386 # comment and entirely ignored.
387 #
388 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
389 # two-element lists. The first element of each is an option name, and
390 # the second additional arguments to be added on to the end of the
391 # option list as given in FILE.d. (If omitted, no additional options
392 # are added.)
393 #
394 # The interesting options are:
395 #
396 # name: TEST-NAME
397 # The name of this test, passed to DejaGNU's `pass' and `fail'
398 # commands. If omitted, this defaults to FILE, the root of the
399 # .s and .d files' names.
400 #
401 # as: FLAGS
402 # When assembling FILE.s, pass FLAGS to the assembler.
403 #
404 # nm: FLAGS
405 # objcopy: FLAGS
406 # objdump: FLAGS
407 # readelf: FLAGS
408 # Use the specified program to analyze the .o file, and pass it
409 # FLAGS, in addition to the .o file name. Note that they are run
410 # with LC_ALL=C in the environment to give consistent sorting
411 # of symbols. If no FLAGS are needed then use:
412 # PROG: [nm objcopy objdump readelf]
413 # instead.
414 #
415 # source: SOURCE
416 # Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
417 # This is useful if several .d files want to share a .s file.
418 #
419 # target: GLOBS...
420 # Run this test only on a specified list of targets. More precisely,
421 # each glob in the space-separated list is passed to "istarget"; if
422 # it evaluates true for any of them, the test will be run, otherwise
423 # it will be marked unsupported.
424 #
425 # not-target: GLOBS...
426 # Do not run this test on a specified list of targets. Again,
427 # the each glob in the space-separated list is passed to
428 # "istarget", and the test is run if it evaluates *false* for
429 # *all* of them. Otherwise it will be marked unsupported.
430 #
431 # skip: GLOBS...
432 # not-skip: GLOBS...
433 # These are exactly the same as "not-target" and "target",
434 # respectively, except that they do nothing at all if the check
435 # fails. They should only be used in groups, to construct a single
436 # test which is run on all targets but with variant options or
437 # expected output on some targets. (For example, see
438 # gas/arm/inst.d and gas/arm/wince_inst.d.)
439 #
440 # error: REGEX
441 # An error with message matching REGEX must be emitted for the test
442 # to pass. The PROG, objdump, nm and objcopy options have no
443 # meaning and need not supplied if this is present.
444 #
445 # warning: REGEX
446 # Expect a gas warning matching REGEX. It is an error to issue
447 # both "error" and "warning".
448 #
449 # stderr: FILE
450 # FILE contains regexp lines to be matched against the diagnostic
451 # output of the assembler. This does not preclude the use of
452 # PROG, nm, objdump, or objcopy.
453 #
454 # error-output: FILE
455 # Means the same as 'stderr', but also indicates that the assembler
456 # is expected to exit unsuccessfully (therefore PROG, objdump, nm,
457 # and objcopy have no meaning and should not be supplied).
458 #
459 # Each option may occur at most once.
460 #
461 # After the option lines come regexp lines. `run_dump_test' calls
462 # `regexp_diff' to compare the output of the dumping tool against the
463 # regexps in FILE.d. `regexp_diff' is defined later in this file; see
464 # further comments there.
465
466 proc run_dump_test { name {extra_options {}} } {
467 global subdir srcdir
468 global OBJDUMP NM AS OBJCOPY READELF
469 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS
470 global host_triplet
471 global env
472
473 if [string match "*/*" $name] {
474 set file $name
475 set name [file tail $name]
476 } else {
477 set file "$srcdir/$subdir/$name"
478 }
479 set opt_array [slurp_options "${file}.d"]
480 if { $opt_array == -1 } {
481 perror "error reading options from $file.d"
482 unresolved $subdir/$name
483 return
484 }
485 set opts(as) {}
486 set opts(objdump) {}
487 set opts(nm) {}
488 set opts(objcopy) {}
489 set opts(readelf) {}
490 set opts(name) {}
491 set opts(PROG) {}
492 set opts(source) {}
493 set opts(stderr) {}
494 set opts(error) {}
495 set opts(error-output) {}
496 set opts(warning) {}
497 set opts(target) {}
498 set opts(not-target) {}
499 set opts(skip) {}
500 set opts(not-skip) {}
501
502 foreach i $opt_array {
503 set opt_name [lindex $i 0]
504 set opt_val [lindex $i 1]
505 if ![info exists opts($opt_name)] {
506 perror "unknown option $opt_name in file $file.d"
507 unresolved $subdir/$name
508 return
509 }
510 if [string length $opts($opt_name)] {
511 perror "option $opt_name multiply set in $file.d"
512 unresolved $subdir/$name
513 return
514 }
515 if { $opt_name == "as" } {
516 set opt_val [subst $opt_val]
517 }
518 set opts($opt_name) $opt_val
519 }
520
521 foreach i $extra_options {
522 set opt_name [lindex $i 0]
523 set opt_val [lindex $i 1]
524 if ![info exists opts($opt_name)] {
525 perror "unknown option $opt_name given in extra_opts"
526 unresolved $subdir/$name
527 return
528 }
529 # add extra option to end of existing option, adding space
530 # if necessary.
531 if [string length $opts($opt_name)] {
532 append opts($opt_name) " "
533 }
534 append opts($opt_name) $opt_val
535 }
536
537 if { $opts(name) == "" } {
538 set testname "$subdir/$name"
539 } else {
540 set testname $opts(name)
541 }
542 verbose "Testing $testname"
543
544 if { (($opts(warning) != "") && ($opts(error) != "")) \
545 || (($opts(warning) != "") && ($opts(stderr) != "")) \
546 || (($opts(error-output) != "") && ($opts(stderr) != "")) \
547 || (($opts(error-output) != "") && ($opts(error) != "")) \
548 || (($opts(error-output) != "") && ($opts(warning) != "")) } {
549 perror "$testname: bad mix of stderr, error-output, error, and warning test-directives"
550 unresolved $testname
551 return
552 }
553 if { $opts(error-output) != "" } then {
554 set opts(stderr) $opts(error-output)
555 }
556
557 set program ""
558 # It's meaningless to require an output-testing method when we
559 # expect an error.
560 if { $opts(error) == "" && $opts(error-output) == "" } {
561 if {$opts(PROG) != ""} {
562 switch -- $opts(PROG) {
563 objdump { set program objdump }
564 nm { set program nm }
565 objcopy { set program objcopy }
566 readelf { set program readelf }
567 default {
568 perror "unrecognized program option $opts(PROG) in $file.d"
569 unresolved $testname
570 return }
571 }
572 } else {
573 # Guess which program to run, by seeing which option was specified.
574 foreach p {objdump objcopy nm readelf} {
575 if {$opts($p) != ""} {
576 if {$program != ""} {
577 perror "ambiguous dump program in $file.d"
578 unresolved $testname
579 return
580 } else {
581 set program $p
582 }
583 }
584 }
585 }
586 if { $program == "" && $opts(warning) == "" } {
587 perror "dump program unspecified in $file.d"
588 unresolved $testname
589 return
590 }
591 }
592
593 # Handle skipping the test on specified targets.
594 # You can have both skip/not-skip and target/not-target, but you can't
595 # have both skip and not-skip, or target and not-target, in the same file.
596 if { $opts(skip) != "" } then {
597 if { $opts(not-skip) != "" } then {
598 perror "$testname: mixing skip and not-skip directives is invalid"
599 unresolved $testname
600 return
601 }
602 foreach glob $opts(skip) {
603 if {[istarget $glob]} { return }
604 }
605 }
606 if { $opts(not-skip) != "" } then {
607 set skip 1
608 foreach glob $opts(not-skip) {
609 if {[istarget $glob]} {
610 set skip 0
611 break
612 }
613 }
614 if {$skip} { return }
615 }
616 if { $opts(target) != "" } then {
617 if { $opts(not-target) != "" } then {
618 perror "$testname: mixing target and not-target directives is invalid"
619 unresolved $testname
620 return
621 }
622 set skip 1
623 foreach glob $opts(target) {
624 if {[istarget $glob]} {
625 set skip 0
626 break
627 }
628 }
629 if {$skip} {
630 unsupported $testname
631 return
632 }
633 }
634 if { $opts(not-target) != "" } then {
635 foreach glob $opts(not-target) {
636 if {[istarget $glob]} {
637 unsupported $testname
638 return
639 }
640 }
641 }
642
643
644 if { $opts(source) == "" } {
645 set sourcefile ${file}.s
646 } else {
647 set sourcefile $srcdir/$subdir/$opts(source)
648 }
649
650 set cmd "$AS $ASFLAGS $opts(as) -o dump.o $sourcefile"
651 send_log "$cmd\n"
652 set status [gas_host_run $cmd ""]
653 set cmdret [lindex $status 0]
654 set comp_output [prune_warnings [lindex $status 1]]
655
656 set expmsg $opts(error)
657 if { $opts(warning) != "" } {
658 set expmsg $opts(warning)
659 }
660 if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
661 # If the executed program writes to stderr and stderr is not
662 # redirected, exec *always* returns failure, regardless of the
663 # program exit code. Thankfully, we can retrieve the true
664 # return status from a special variable. Redirection would
665 # cause a tcl-specific message to be appended, and we'd rather
666 # not deal with that if we can help it.
667 global errorCode
668 if { $cmdret != 0 && [lindex $errorCode 0] == "NONE" } {
669 set cmdret 0
670 }
671
672 set exitstat "succeeded"
673 if { $cmdret != 0 } { set exitstat "failed" }
674
675 send_log "$comp_output\n"
676 verbose "$comp_output" 3
677 if { $opts(stderr) == "" } then {
678 if { [regexp $expmsg $comp_output] \
679 && (($cmdret == 0) == ($opts(warning) != "")) } {
680 # We have the expected output from gas.
681 # Return if there's nothing more to do.
682 if { $opts(error) != "" || $program == "" } {
683 pass $testname
684 return
685 }
686 } else {
687 verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
688
689 fail $testname
690 return
691 }
692 } else {
693 catch {write_file dump.stderr "$comp_output"} write_output
694 if ![string match "" $write_output] then {
695 send_log "error writing dump.stderr: $write_output\n"
696 verbose "error writing dump.stderr: $write_output" 3
697 send_log "$comp_output\n"
698 verbose "$comp_output" 3
699 fail $testname
700 return
701 }
702 set stderrfile $srcdir/$subdir/$opts(stderr)
703 verbose "wrote pruned stderr to dump.stderr" 3
704 if { [regexp_diff "dump.stderr" "$stderrfile"] } then {
705 if { $opts(error) != "" } {
706 verbose -log "$exitstat with: <$comp_output>, expected: <$opts(error)>"
707 if [regexp $opts(error) $comp_output] {
708 pass $testname
709 return
710 }
711 }
712 fail $testname
713 verbose "pruned stderr is [file_contents "dump.stderr"]" 2
714 return
715 } elseif { $opts(error-output) != "" } then {
716 pass $testname
717 return
718 }
719 }
720 } else {
721 if { $opts(error) != "" || $opts(error-output) != "" } {
722 fail $testname
723 }
724 }
725
726 if { $program == "" } {
727 return
728 }
729 set progopts1 $opts($program)
730 eval set progopts \$[string toupper $program]FLAGS
731 eval set binary \$[string toupper $program]
732
733 if { ![is_remote host] && [which $binary] == 0 } {
734 untested $testname
735 return
736 }
737
738 if { $progopts1 == "" } { set $progopts1 "-r" }
739 verbose "running $binary $progopts $progopts1" 3
740
741 # Objcopy, unlike the other two, won't send its output to stdout,
742 # so we have to run it specially.
743 set cmd "$binary $progopts $progopts1 dump.o"
744 set redir ">dump.out"
745 if { $program == "objcopy" } {
746 set cmd "$binary $progopts $progopts1 dump.o dump.out"
747 set redir ""
748 }
749
750 # Ensure consistent sorting of symbols
751 if {[info exists env(LC_ALL)]} {
752 set old_lc_all $env(LC_ALL)
753 }
754 set env(LC_ALL) "C"
755 send_log "$cmd\n"
756 set status [gas_host_run "$cmd" "$redir"]
757 set comp_output [prune_warnings [lindex $status 1]]
758 if {[info exists old_lc_all]} {
759 set env(LC_ALL) $old_lc_all
760 } else {
761 unset env(LC_ALL)
762 }
763 set comp_output [prune_warnings $comp_output]
764 if ![string match "" $comp_output] then {
765 send_log "$comp_output\n"
766 fail $testname
767 return
768 }
769
770 verbose_eval {[file_contents "dump.out"]} 3
771 if { [regexp_diff "dump.out" "${file}.d"] } then {
772 fail $testname
773 verbose "output is [file_contents "dump.out"]" 2
774 return
775 }
776
777 pass $testname
778 }
779
780 proc slurp_options { file } {
781 if [catch { set f [open $file r] } x] {
782 #perror "couldn't open `$file': $x"
783 perror "$x"
784 return -1
785 }
786 set opt_array {}
787 # whitespace expression
788 set ws {[ ]*}
789 set nws {[^ ]*}
790 # whitespace is ignored anywhere except within the options list;
791 # option names are alphabetic plus dash
792 set pat "^#${ws}(\[a-zA-Z-\]*)$ws:${ws}(.*)$ws\$"
793 while { [gets $f line] != -1 } {
794 set line [string trim $line]
795 # Whitespace here is space-tab.
796 if [regexp $pat $line xxx opt_name opt_val] {
797 # match!
798 lappend opt_array [list $opt_name $opt_val]
799 } elseif {![regexp "^#" $line ]} {
800 break
801 }
802 }
803 close $f
804 return $opt_array
805 }
806
807 proc objdump { opts } {
808 global OBJDUMP
809 global comp_output
810 global host_triplet
811
812 set status [gas_host_run "$OBJDUMP $opts" ""]
813 set comp_output [prune_warnings [lindex $status 1]]
814 verbose "objdump output=$comp_output\n" 3
815 }
816
817 proc objdump_start_no_subdir { prog opts } {
818 global OBJDUMP
819 global srcdir
820 global spawn_id
821
822 verbose "Starting $OBJDUMP $opts $prog" 2
823 set status [gas_host_run "$OBJDUMP $opts $prog" ">&gas.out"]
824 spawn -noecho -nottycopy cat gas.out
825 }
826
827 proc objdump_finish { } {
828 global spawn_id
829
830 catch "close"
831 catch "wait"
832 }
833
834 # Default timeout is 10 seconds, loses on a slow machine. But some
835 # configurations of dejagnu may override it.
836 if {$timeout<120} then { set timeout 120 }
837
838 expect_after -i {
839 timeout { perror "timeout" }
840 "virtual memory exhausted" { perror "virtual memory exhausted" }
841 buffer_full { perror "buffer full" }
842 eof { perror "eof" }
843 }
844
845 # regexp_diff, based on simple_diff taken from ld test suite
846 # compares two files line-by-line
847 # file1 contains strings, file2 contains regexps and #-comments
848 # blank lines are ignored in either file
849 # returns non-zero if differences exist
850 #
851 proc regexp_diff { file_1 file_2 } {
852
853 set eof -1
854 set end_1 0
855 set end_2 0
856 set differences 0
857 set diff_pass 0
858
859 if [file exists $file_1] then {
860 set file_a [open $file_1 r]
861 } else {
862 perror "$file_1 doesn't exist"
863 return 1
864 }
865
866 if [file exists $file_2] then {
867 set file_b [open $file_2 r]
868 } else {
869 perror "$file_2 doesn't exist"
870 close $file_a
871 return 1
872 }
873
874 verbose " Regexp-diff'ing: $file_1 $file_2" 2
875
876 while { 1 } {
877 set line_a ""
878 set line_b ""
879 while { [string length $line_a] == 0 } {
880 if { [gets $file_a line_a] == $eof } {
881 set end_1 1
882 break
883 }
884 }
885 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
886 if [ string match "#pass" $line_b ] {
887 set end_2 1
888 set diff_pass 1
889 break
890 } elseif [ string match "#..." $line_b ] {
891 if { [gets $file_b line_b] == $eof } {
892 set end_2 1
893 set diff_pass 1
894 break
895 }
896 verbose "looking for \"^$line_b$\"" 3
897 while { ![regexp "^$line_b$" "$line_a"] } {
898 verbose "skipping \"$line_a\"" 3
899 if { [gets $file_a line_a] == $eof } {
900 set end_1 1
901 break
902 }
903 }
904 break
905 }
906 if { [gets $file_b line_b] == $eof } {
907 set end_2 1
908 break
909 }
910 }
911
912 if { $diff_pass } {
913 break
914 } elseif { $end_1 && $end_2 } {
915 break
916 } elseif { $end_1 } {
917 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
918 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
919 set differences 1
920 break
921 } elseif { $end_2 } {
922 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
923 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
924 set differences 1
925 break
926 } else {
927 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
928 if ![regexp "^$line_b$" "$line_a"] {
929 send_log "regexp_diff match failure\n"
930 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
931 verbose "regexp_diff match failure\n" 3
932 set differences 1
933 }
934 }
935 }
936
937 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
938 send_log "$file_1 and $file_2 are different lengths\n"
939 verbose "$file_1 and $file_2 are different lengths" 3
940 set differences 1
941 }
942
943 close $file_a
944 close $file_b
945
946 return $differences
947 }
948
949 proc file_contents { filename } {
950 set file [open $filename r]
951 set contents [read $file]
952 close $file
953 return $contents
954 }
955
956 proc write_file { filename contents } {
957 set file [open $filename w]
958 puts $file "$contents"
959 close $file
960 }
961
962 proc verbose_eval { expr { level 1 } } {
963 global verbose
964 if $verbose>$level then { eval verbose "$expr" $level }
965 }
966
967 # This definition is taken from an unreleased version of DejaGnu. Once
968 # that version gets released, and has been out in the world for a few
969 # months at least, it may be safe to delete this copy.
970 if ![string length [info proc prune_warnings]] {
971 #
972 # prune_warnings -- delete various system verbosities from TEXT.
973 #
974 # An example is:
975 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
976 #
977 # Sites with particular verbose os's may wish to override this in site.exp.
978 #
979 proc prune_warnings { text } {
980 # This is from sun4's. Do it for all machines for now.
981 # The "\\1" is to try to preserve a "\n" but only if necessary.
982 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
983
984 # It might be tempting to get carried away and delete blank lines, etc.
985 # Just delete *exactly* what we're ask to, and that's it.
986 return $text
987 }
988 }
989
990 # run_list_test NAME (optional): OPTS TESTNAME
991 #
992 # Assemble the file "NAME.s" with command line options OPTS and
993 # compare the assembler standard error output against the regular
994 # expressions given in the file "NAME.l". If TESTNAME is provided,
995 # it will be used as the name of the test.
996
997 proc run_list_test { name {opts {}} {testname {}} } {
998 global srcdir subdir
999 if { [string length $testname] == 0 } then {
1000 set testname "[file tail $subdir] $name"
1001 }
1002 set file $srcdir/$subdir/$name
1003 gas_run ${name}.s $opts ">&dump.out"
1004 if { [regexp_diff "dump.out" "${file}.l"] } then {
1005 fail $testname
1006 verbose "output is [file_contents "dump.out"]" 2
1007 return
1008 }
1009 pass $testname
1010 }
1011
1012 # run_list_test_stdin NAME (optional): OPTS TESTNAME
1013 #
1014 # Similar to run_list_test, but use stdin as input.
1015
1016 proc run_list_test_stdin { name {opts {}} {testname {}} } {
1017 global srcdir subdir
1018 if { [string length $testname] == 0 } then {
1019 set testname "[file tail $subdir] $name"
1020 }
1021 set file $srcdir/$subdir/$name
1022 gas_run_stdin ${name}.s $opts ">&dump.out"
1023 if { [regexp_diff "dump.out" "${file}.l"] } then {
1024 fail $testname
1025 verbose "output is [file_contents "dump.out"]" 2
1026 return
1027 }
1028 pass $testname
1029 }