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