Improve binutils testsuite coverage for GNU/Hurd.
[deliverable/binutils-gdb.git] / binutils / testsuite / lib / utils-lib.exp
CommitLineData
aa820537 1# Copyright 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2003, 2004, 2006, 2007,
5940a93c 2# 2009, 2010 Free Software Foundation, Inc.
252b5132
RH
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
32866df7 6# the Free Software Foundation; either version 3 of the License, or
252b5132
RH
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
b43b5d5f 16# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
252b5132
RH
17
18# Please email any bugs, comments, and/or additions to this file to:
19# bug-dejagnu@prep.ai.mit.edu
20
21# This file was written by Rob Savoye <rob@cygnus.com>
22# and extended by Ian Lance Taylor <ian@cygnus.com>
23
24proc binutil_version { prog } {
25 if ![is_remote host] {
8d263650 26 set path [which $prog]
252b5132
RH
27 if {$path == 0} then {
28 perror "$prog can't be run, file not found."
29 return ""
30 }
31 } else {
32 set path $prog
33 }
8d263650
BE
34 set state [remote_exec host $prog --version]
35 set tmp "[lindex $state 1]\n"
252b5132
RH
36 # Should find a way to discard constant parts, keep whatever's
37 # left, so the version string could be almost anything at all...
38 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" "$tmp" version cyg number
39 if ![info exists number] then {
40 return "$path (no version number)\n"
41 }
42 return "$path $number\n"
43}
44
45#
46# default_binutils_run
47# run a program, returning the output
48# sets binutils_run_failed if the program does not exist
49#
50proc default_binutils_run { prog progargs } {
51 global binutils_run_failed
52 global host_triplet
53
54 set binutils_run_failed 0
55
56 if ![is_remote host] {
57 if {[which $prog] == 0} then {
58 perror "$prog does not exist"
59 set binutils_run_failed 1
60 return ""
61 }
62 }
63
64 send_log "$prog $progargs\n"
65 verbose "$prog $progargs"
66
67 # Gotta quote dollar-signs because they get mangled by the
68 # shell otherwise.
69 regsub -all "\\$" "$progargs" "\\$" progargs
70
71 set state [remote_exec host $prog $progargs]
8d263650 72 set exec_output [prune_warnings [lindex $state 1]]
252b5132
RH
73 if {![string match "" $exec_output]} then {
74 send_log "$exec_output\n"
75 verbose "$exec_output"
da28e1e1
L
76 } else {
77 if { [lindex $state 0] != 0 } {
78 set exec_output "$prog exited with status [lindex $state 0]"
79 send_log "$exec_output\n"
80 verbose "$exec_output"
81 }
252b5132
RH
82 }
83 return $exec_output
84}
85
86#
368886ac 87# default_binutils_assemble_flags
252b5132
RH
88# assemble a file
89#
368886ac 90proc default_binutils_assemble_flags { source object asflags } {
252b5132
RH
91 global srcdir
92 global host_triplet
93
94 # The HPPA assembler syntax is a little different than most, to make
95 # the test source file assemble we need to run it through sed.
96 #
97 # This is a hack in that it won't scale well if other targets need
98 # similar transformations to assemble. We'll generalize the hack
99 # if/when other targets need similar handling.
12c616f1
AM
100 if { [istarget "hppa*-*-*"] && ![istarget "*-*-linux*" ] } then {
101 set sed_file $srcdir/config/hppa.sed
102 send_log "sed -f $sed_file < $source > asm.s\n"
103 verbose "sed -f $sed_file < $source > asm.s"
8d263650 104 catch "exec sed -f $sed_file < $source > asm.s"
252b5132
RH
105 set source asm.s
106 }
107
368886ac 108 set exec_output [target_assemble $source $object $asflags]
252b5132
RH
109 set exec_output [prune_warnings $exec_output]
110
111 if [string match "" $exec_output] {
112 return 1
113 } else {
114 send_log "$exec_output\n"
115 verbose "$exec_output"
116 perror "$source: assembly failed"
117 return 0
118 }
119}
9ce701e2 120
f3f391b8 121# True if the object format is known to be ELF.
9ce701e2
L
122#
123proc is_elf_format {} {
f3f391b8
AM
124 if { ![istarget *-*-sysv4*]
125 && ![istarget *-*-unixware*]
126 && ![istarget *-*-elf*]
127 && ![istarget *-*-eabi*]
128 && ![istarget *-*-rtems*]
129 && ![istarget hppa*64*-*-hpux*]
130 && ![istarget ia64-*-hpux*]
131 && ![istarget *-*-linux*]
5940a93c 132 && ![istarget *-*-gnu*]
f3f391b8
AM
133 && ![istarget frv-*-uclinux*]
134 && ![istarget bfin-*-uclinux]
135 && ![istarget sh*-*-uclinux*]
136 && ![istarget *-*-irix5*]
137 && ![istarget *-*-irix6*]
138 && ![istarget *-*-netbsd*]
139 && ![istarget *-*-openbsd*]
9ce701e2
L
140 && ![istarget *-*-solaris2*] } {
141 return 0
142 }
143
f3f391b8
AM
144 if { [istarget *-*-linux*aout*]
145 || [istarget *-*-linux*oldld*]
146 || [istarget h8500-*-rtems*]
147 || [istarget i960-*-rtems*]
148 || [istarget *-*-rtemscoff*] } {
9ce701e2
L
149 return 0
150 }
151
f3f391b8
AM
152 if { ![istarget *-*-netbsdelf*]
153 && ([istarget *-*-netbsd*aout*]
154 || [istarget *-*-netbsdpe*]
155 || [istarget arm*-*-netbsd*]
156 || [istarget sparc-*-netbsd*]
157 || [istarget i*86-*-netbsd*]
158 || [istarget m68*-*-netbsd*]
159 || [istarget vax-*-netbsd*]
9ce701e2
L
160 || [istarget ns32k-*-netbsd*]) } {
161 return 0
162 }
f3f391b8
AM
163
164 if { [istarget arm-*-openbsd*]
165 || [istarget i386-*-openbsd\[0-2\].*]
166 || [istarget i386-*-openbsd3.\[0-2\]]
167 || [istarget m68*-*-openbsd*]
168 || [istarget ns32k-*-openbsd*]
169 || [istarget sparc-*-openbsd\[0-2\].*]
170 || [istarget sparc-*-openbsd3.\[0-1\]]
171 || [istarget vax-*-openbsd*] } {
172 return 0
173 }
174
9ce701e2
L
175 return 1
176}
0fd555c4
NC
177
178#
179# exe_ext
180# Returns target executable extension, if any.
181#
182proc exe_ext {} {
99ad8390 183 if { [istarget *-*-mingw*] || [istarget *-*-cygwin*] } {
0fd555c4
NC
184 return ".exe"
185 } else {
186 return ""
187 }
188}
af3c5dea
L
189
190# Copied and modified from gas.
191
192# run_dump_test FILE (optional:) EXTRA_OPTIONS
193#
194# Assemble a .s file, then run some utility on it and check the output.
195#
196# There should be an assembly language file named FILE.s in the test
197# suite directory, and a pattern file called FILE.d. `run_dump_test'
198# will assemble FILE.s, run some tool like `objdump', `objcopy', or
199# `nm' on the .o file to produce textual output, and then analyze that
200# with regexps. The FILE.d file specifies what program to run, and
201# what to expect in its output.
202#
203# The FILE.d file begins with zero or more option lines, which specify
204# flags to pass to the assembler, the program to run to dump the
205# assembler's output, and the options it wants. The option lines have
206# the syntax:
207#
208# # OPTION: VALUE
209#
210# OPTION is the name of some option, like "name" or "objdump", and
211# VALUE is OPTION's value. The valid options are described below.
212# Whitespace is ignored everywhere, except within VALUE. The option
213# list ends with the first line that doesn't match the above syntax.
214# However, a line within the options that begins with a #, but doesn't
215# have a recognizable option name followed by a colon, is considered a
216# comment and entirely ignored.
217#
218# The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
219# two-element lists. The first element of each is an option name, and
220# the second additional arguments to be added on to the end of the
221# option list as given in FILE.d. (If omitted, no additional options
222# are added.)
223#
224# The interesting options are:
225#
226# name: TEST-NAME
227# The name of this test, passed to DejaGNU's `pass' and `fail'
228# commands. If omitted, this defaults to FILE, the root of the
229# .s and .d files' names.
230#
231# as: FLAGS
232# When assembling FILE.s, pass FLAGS to the assembler.
233#
234# PROG: PROGRAM-NAME
235# The name of the program to run to analyze the .o file produced
236# by the assembler. This can be omitted; run_dump_test will guess
237# which program to run by seeing which of the flags options below
238# is present.
239#
240# objdump: FLAGS
241# nm: FLAGS
242# objcopy: FLAGS
243# Use the specified program to analyze the .o file, and pass it
244# FLAGS, in addition to the .o file name. Note that they are run
245# with LC_ALL=C in the environment to give consistent sorting
246# of symbols.
247#
248# source: SOURCE
249# Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
250# This is useful if several .d files want to share a .s file.
251#
252# target: GLOBS...
253# Run this test only on a specified list of targets. More precisely,
254# each glob in the space-separated list is passed to "istarget"; if
255# it evaluates true for any of them, the test will be run, otherwise
256# it will be marked unsupported.
257#
258# not-target: GLOBS...
259# Do not run this test on a specified list of targets. Again,
260# the each glob in the space-separated list is passed to
261# "istarget", and the test is run if it evaluates *false* for
262# *all* of them. Otherwise it will be marked unsupported.
263#
264# skip: GLOBS...
265# not-skip: GLOBS...
266# These are exactly the same as "not-target" and "target",
267# respectively, except that they do nothing at all if the check
268# fails. They should only be used in groups, to construct a single
269# test which is run on all targets but with variant options or
270# expected output on some targets. (For example, see
271# gas/arm/inst.d and gas/arm/wince_inst.d.)
272#
273# error: REGEX
274# An error with message matching REGEX must be emitted for the test
275# to pass. The PROG, objdump, nm and objcopy options have no
276# meaning and need not supplied if this is present.
277#
278# warning: REGEX
279# Expect a gas warning matching REGEX. It is an error to issue
280# both "error" and "warning".
281#
282# stderr: FILE
283# FILE contains regexp lines to be matched against the diagnostic
284# output of the assembler. This does not preclude the use of
285# PROG, nm, objdump, or objcopy.
286#
287# error-output: FILE
288# Means the same as 'stderr', but also indicates that the assembler
289# is expected to exit unsuccessfully (therefore PROG, objdump, nm,
290# and objcopy have no meaning and should not be supplied).
291#
292# Each option may occur at most once.
293#
294# After the option lines come regexp lines. `run_dump_test' calls
295# `regexp_diff' to compare the output of the dumping tool against the
296# regexps in FILE.d. `regexp_diff' is defined later in this file; see
297# further comments there.
298
299proc run_dump_test { name {extra_options {}} } {
300 global subdir srcdir
748fc5e9
L
301 global OBJDUMP NM OBJCOPY READELF STRIP
302 global OBJDUMPFLAGS NMFLAGS OBJCOPYFLAGS READELFFLAGS STRIPFLAGS
30fd33bb 303 global ELFEDIT ELFEDITFLAGS
af3c5dea
L
304 global host_triplet
305 global env
306 global copyfile
307 global tempfile
308
309 if [string match "*/*" $name] {
310 set file $name
311 set name [file tail $name]
312 } else {
313 set file "$srcdir/$subdir/$name"
314 }
315 set opt_array [slurp_options "${file}.d"]
316 if { $opt_array == -1 } {
317 perror "error reading options from $file.d"
318 unresolved $subdir/$name
319 return
320 }
321 set opts(addr2line) {}
322 set opts(ar) {}
368886ac 323 set opts(as) {}
af3c5dea
L
324 set opts(nm) {}
325 set opts(objcopy) {}
326 set opts(objdump) {}
327 set opts(strip) {}
328 set opts(ranlib) {}
329 set opts(readelf) {}
330 set opts(size) {}
331 set opts(strings) {}
332 set opts(name) {}
30fd33bb 333 set opts(elfedit) {}
af3c5dea
L
334 set opts(PROG) {}
335 set opts(DUMPPROG) {}
336 set opts(source) {}
337 set opts(target) {}
338 set opts(not-target) {}
339 set opts(skip) {}
340 set opts(not-skip) {}
341
342 foreach i $opt_array {
343 set opt_name [lindex $i 0]
344 set opt_val [lindex $i 1]
345 if ![info exists opts($opt_name)] {
346 perror "unknown option $opt_name in file $file.d"
347 unresolved $subdir/$name
348 return
349 }
500ee42e
ILT
350
351 # Permit the option to use $srcdir to refer to the source
352 # directory.
353 regsub -all "\\\$srcdir" "$opt_val" "$srcdir/$subdir" opt_val
354
af3c5dea
L
355 if [string length $opts($opt_name)] {
356 perror "option $opt_name multiply set in $file.d"
357 unresolved $subdir/$name
358 return
359 }
360 set opts($opt_name) $opt_val
361 }
362
363 foreach i $extra_options {
364 set opt_name [lindex $i 0]
365 set opt_val [lindex $i 1]
366 if ![info exists opts($opt_name)] {
367 perror "unknown option $opt_name given in extra_opts"
368 unresolved $subdir/$name
369 return
370 }
500ee42e
ILT
371
372 # Permit the option to use $srcdir to refer to the source
373 # directory.
374 regsub -all "\\\$srcdir" "$opt_val" "$srcdir/$subdir" opt_val
375
af3c5dea
L
376 # add extra option to end of existing option, adding space
377 # if necessary.
378 if [string length $opts($opt_name)] {
379 append opts($opt_name) " "
380 }
381 append opts($opt_name) $opt_val
382 }
383
384 if { $opts(name) == "" } {
385 set testname "$subdir/$name"
386 } else {
387 set testname $opts(name)
388 }
389 verbose "Testing $testname"
390
391 if {$opts(PROG) == ""} {
392 perror "program isn't set in $file.d"
393 unresolved $testname
394 return
395 }
396
748fc5e9 397 set destopt ""
af3c5dea
L
398 switch -- $opts(PROG) {
399 ar { set program ar }
400 objcopy { set program objcopy }
401 ranlib { set program ranlib }
748fc5e9
L
402 strip {
403 set program strip
404 set destopt "-o"
405 }
af3c5dea 406 strings { set program strings }
30fd33bb 407 elfedit { set program elfedit }
af3c5dea
L
408 default {
409 perror "unrecognized program option $opts(PROG) in $file.d"
410 unresolved $testname
411 return }
412 }
413
414 set dumpprogram ""
415 if { $opts(DUMPPROG) != "" } {
416 switch -- $opts(DUMPPROG) {
417 addr2line { set dumpprogram addr2line }
418 nm { set dumpprogram nm }
419 objdump { set dumpprogram objdump }
420 readelf { set dumpprogram readelf }
421 size { set dumpprogram size }
422 default {
423 perror "unrecognized dump program option $opts(DUMPPROG) in $file.d"
424 unresolved $testname
425 return }
426 }
427 } else {
428 # Guess which program to run, by seeing which option was specified.
429 foreach p {objdump nm readelf} {
430 if {$opts($p) != ""} {
431 if {$dumpprogram != ""} {
432 perror "ambiguous dump program in $file.d"
433 unresolved $testname
434 return
435 } else {
436 set dumpprogram $p
437 }
438 }
439 }
440 }
441
442 # Handle skipping the test on specified targets.
443 # You can have both skip/not-skip and target/not-target, but you can't
444 # have both skip and not-skip, or target and not-target, in the same file.
445 if { $opts(skip) != "" } then {
446 if { $opts(not-skip) != "" } then {
447 perror "$testname: mixing skip and not-skip directives is invalid"
448 unresolved $testname
449 return
450 }
451 foreach glob $opts(skip) {
452 if {[istarget $glob]} { return }
453 }
454 }
455 if { $opts(not-skip) != "" } then {
456 set skip 1
457 foreach glob $opts(not-skip) {
458 if {[istarget $glob]} {
459 set skip 0
460 break
461 }
462 }
463 if {$skip} { return }
464 }
465 if { $opts(target) != "" } then {
466 if { $opts(not-target) != "" } then {
467 perror "$testname: mixing target and not-target directives is invalid"
468 unresolved $testname
469 return
470 }
471 set skip 1
472 foreach glob $opts(target) {
473 if {[istarget $glob]} {
474 set skip 0
475 break
476 }
477 }
478 if {$skip} {
479 unsupported $testname
480 return
481 }
482 }
483 if { $opts(not-target) != "" } then {
484 foreach glob $opts(not-target) {
485 if {[istarget $glob]} {
486 unsupported $testname
487 return
488 }
489 }
490 }
491
492 if { $opts(source) == "" } {
493 set srcfile ${file}.s
494 } else {
495 set srcfile $srcdir/$subdir/$opts(source)
496 }
497
368886ac 498 set exec_output [binutils_assemble_flags ${srcfile} $tempfile $opts(as)]
af3c5dea
L
499 if [string match "" $exec_output] then {
500 send_log "$exec_output\n"
501 verbose "$exec_output"
502 fail $testname
503 return
504 }
505
506 set progopts1 $opts($program)
507 eval set progopts \$[string toupper $program]FLAGS
508 eval set binary \$[string toupper $program]
509
748fc5e9 510 set exec_output [binutils_run $binary "$progopts $progopts1 $tempfile $destopt ${copyfile}.o"]
af3c5dea
L
511 if ![string match "" $exec_output] {
512 send_log "$exec_output\n"
513 verbose "$exec_output"
514 fail $testname
515 return
516 }
517
518 set progopts1 $opts($dumpprogram)
519 eval set progopts \$[string toupper $dumpprogram]FLAGS
520 eval set binary \$[string toupper $dumpprogram]
521
7f6a71ff 522 if { ![is_remote host] && [which $binary] == 0 } {
af3c5dea
L
523 untested $testname
524 return
525 }
526
527 verbose "running $binary $progopts $progopts1" 3
528
7f6a71ff 529 set cmd "$binary $progopts $progopts1 ${copyfile}.o"
af3c5dea
L
530
531 # Ensure consistent sorting of symbols
532 if {[info exists env(LC_ALL)]} {
533 set old_lc_all $env(LC_ALL)
534 }
535 set env(LC_ALL) "C"
536 send_log "$cmd\n"
7f6a71ff 537 set comp_output [remote_exec host $cmd "" "/dev/null" "tmpdir/dump.out"]
af3c5dea
L
538 if {[info exists old_lc_all]} {
539 set env(LC_ALL) $old_lc_all
540 } else {
541 unset env(LC_ALL)
542 }
7f6a71ff
JM
543 if { [lindex $comp_output 0] != 0 } then {
544 send_log "$comp_output\n"
545 fail $testname
546 return
547 }
548 set comp_output [prune_warnings [lindex $comp_output 1]]
af3c5dea
L
549 if ![string match "" $comp_output] then {
550 send_log "$comp_output\n"
551 fail $testname
552 return
553 }
554
555 verbose_eval {[file_contents "tmpdir/dump.out"]} 3
556 if { [regexp_diff "tmpdir/dump.out" "${file}.d"] } then {
557 fail $testname
558 verbose "output is [file_contents "tmpdir/dump.out"]" 2
559 return
560 }
561
562 pass $testname
563}
564
565proc slurp_options { file } {
566 if [catch { set f [open $file r] } x] {
567 #perror "couldn't open `$file': $x"
568 perror "$x"
569 return -1
570 }
571 set opt_array {}
572 # whitespace expression
573 set ws {[ ]*}
574 set nws {[^ ]*}
575 # whitespace is ignored anywhere except within the options list;
576 # option names are alphabetic plus dash
577 set pat "^#${ws}(\[a-zA-Z-\]*)$ws:${ws}(.*)$ws\$"
578 while { [gets $f line] != -1 } {
579 set line [string trim $line]
580 # Whitespace here is space-tab.
581 if [regexp $pat $line xxx opt_name opt_val] {
582 # match!
583 lappend opt_array [list $opt_name $opt_val]
584 } elseif {![regexp "^#" $line ]} {
585 break
586 }
587 }
588 close $f
589 return $opt_array
590}
591
592# regexp_diff, based on simple_diff taken from ld test suite
593# compares two files line-by-line
594# file1 contains strings, file2 contains regexps and #-comments
595# blank lines are ignored in either file
596# returns non-zero if differences exist
597#
598proc regexp_diff { file_1 file_2 } {
599
600 set eof -1
601 set end_1 0
602 set end_2 0
603 set differences 0
604 set diff_pass 0
605
606 if [file exists $file_1] then {
607 set file_a [open $file_1 r]
608 } else {
609 perror "$file_1 doesn't exist"
610 return 1
611 }
612
613 if [file exists $file_2] then {
614 set file_b [open $file_2 r]
615 } else {
616 perror "$file_2 doesn't exist"
617 close $file_a
618 return 1
619 }
620
621 verbose " Regexp-diff'ing: $file_1 $file_2" 2
622
623 while { 1 } {
624 set line_a ""
625 set line_b ""
626 while { [string length $line_a] == 0 } {
627 if { [gets $file_a line_a] == $eof } {
628 set end_1 1
629 break
630 }
631 }
632 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
633 if [ string match "#pass" $line_b ] {
634 set end_2 1
635 set diff_pass 1
636 break
637 } elseif [ string match "#..." $line_b ] {
638 if { [gets $file_b line_b] == $eof } {
639 set end_2 1
640 set diff_pass 1
641 break
642 }
643 verbose "looking for \"^$line_b$\"" 3
644 while { ![regexp "^$line_b$" "$line_a"] } {
645 verbose "skipping \"$line_a\"" 3
646 if { [gets $file_a line_a] == $eof } {
647 set end_1 1
648 break
649 }
650 }
651 break
652 }
653 if { [gets $file_b line_b] == $eof } {
654 set end_2 1
655 break
656 }
657 }
658
659 if { $diff_pass } {
660 break
661 } elseif { $end_1 && $end_2 } {
662 break
663 } elseif { $end_1 } {
664 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
665 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
666 set differences 1
667 break
668 } elseif { $end_2 } {
669 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
670 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
671 set differences 1
672 break
673 } else {
674 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
675 if ![regexp "^$line_b$" "$line_a"] {
676 send_log "regexp_diff match failure\n"
677 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
678 verbose "regexp_diff match failure\n" 3
679 set differences 1
680 }
681 }
682 }
683
684 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
685 send_log "$file_1 and $file_2 are different lengths\n"
686 verbose "$file_1 and $file_2 are different lengths" 3
687 set differences 1
688 }
689
690 close $file_a
691 close $file_b
692
693 return $differences
694}
695
696proc file_contents { filename } {
697 set file [open $filename r]
698 set contents [read $file]
699 close $file
700 return $contents
701}
702
703proc verbose_eval { expr { level 1 } } {
704 global verbose
705 if $verbose>$level then { eval verbose "$expr" $level }
706}
This page took 0.441698 seconds and 4 git commands to generate.