]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/testsuite/lib/utils-lib.exp
binutils/testsuite: Permit the reuse of dump patterns
[thirdparty/binutils-gdb.git] / binutils / testsuite / lib / utils-lib.exp
1 # Copyright (C) 1993-2017 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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.
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
19
20 # This file was written by Rob Savoye <rob@cygnus.com>
21 # and extended by Ian Lance Taylor <ian@cygnus.com>
22
23 proc load_common_lib { name } {
24 load_lib $name
25 }
26
27 load_common_lib binutils-common.exp
28
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."
34 return ""
35 }
36 } else {
37 set path $prog
38 }
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"
46 }
47 return "$path $number\n"
48 }
49
50 #
51 # default_binutils_run
52 # run a program, returning the output
53 # sets binutils_run_failed if the program does not exist
54 #
55 proc default_binutils_run { prog progargs } {
56 global binutils_run_failed
57 global host_triplet
58
59 set binutils_run_failed 0
60
61 if ![is_remote host] {
62 if {[which $prog] == 0} then {
63 perror "$prog does not exist"
64 set binutils_run_failed 1
65 return ""
66 }
67 }
68
69 # For objdump, automatically translate standard section
70 # names to the targets one, if they are different.
71 set sect_names [get_standard_section_names]
72 if { $sect_names != "" && [string match "*objdump" $prog] } {
73 regsub -- "-j \\.text" $progargs "-j [lindex $sect_names 0]" progargs
74 regsub -- "-j \\.data" $progargs "-j [lindex $sect_names 1]" progargs
75 regsub -- "-j \\.bss" $progargs "-j [lindex $sect_names 2]" progargs
76 }
77
78 send_log "$prog $progargs\n"
79 verbose "$prog $progargs"
80
81 # Gotta quote dollar-signs because they get mangled by the
82 # shell otherwise.
83 regsub -all "\\$" "$progargs" "\\$" progargs
84
85 set state [remote_exec host $prog $progargs]
86 set exec_output [prune_warnings [lindex $state 1]]
87 if {![string match "" $exec_output]} then {
88 send_log "$exec_output\n"
89 verbose "$exec_output"
90 } else {
91 if { [lindex $state 0] != 0 } {
92 set exec_output "$prog exited with status [lindex $state 0]"
93 send_log "$exec_output\n"
94 verbose "$exec_output"
95 }
96 }
97 return $exec_output
98 }
99
100 #
101 # default_binutils_assemble_flags
102 # assemble a file
103 #
104 proc default_binutils_assemble_flags { source object asflags } {
105 global srcdir
106 global host_triplet
107
108 # The HPPA assembler syntax is a little different than most, to make
109 # the test source file assemble we need to run it through sed.
110 #
111 # This is a hack in that it won't scale well if other targets need
112 # similar transformations to assemble. We'll generalize the hack
113 # if/when other targets need similar handling.
114 if { [istarget "hppa*-*-*"] && ![istarget "*-*-linux*" ] } then {
115 set sed_file $srcdir/config/hppa.sed
116 send_log "sed -f $sed_file < $source > asm.s\n"
117 verbose "sed -f $sed_file < $source > asm.s"
118 catch "exec sed -f $sed_file < $source > asm.s"
119 set source asm.s
120 }
121
122 set exec_output [target_assemble $source $object $asflags]
123 set exec_output [prune_warnings $exec_output]
124
125 if [string match "" $exec_output] {
126 return 1
127 } else {
128 send_log "$exec_output\n"
129 verbose "$exec_output"
130 perror "$source: assembly failed"
131 return 0
132 }
133 }
134
135 #
136 # exe_ext
137 # Returns target executable extension, if any.
138 #
139 proc exe_ext {} {
140 if { [istarget *-*-mingw*] || [istarget *-*-cygwin*] } {
141 return ".exe"
142 } else {
143 return ""
144 }
145 }
146
147 # Copied and modified from gas.
148
149 # run_dump_test FILE (optional:) EXTRA_OPTIONS
150 #
151 # Assemble a .s file, then run some utility on it and check the output.
152 #
153 # There should be an assembly language file named FILE.s in the test
154 # suite directory, and a pattern file called FILE.d. `run_dump_test'
155 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
156 # `nm' on the .o file to produce textual output, and then analyze that
157 # with regexps. The FILE.d file specifies what program to run, and
158 # what to expect in its output.
159 #
160 # The FILE.d file begins with zero or more option lines, which specify
161 # flags to pass to the assembler, the program to run to dump the
162 # assembler's output, and the options it wants. The option lines have
163 # the syntax:
164 #
165 # # OPTION: VALUE
166 #
167 # OPTION is the name of some option, like "name" or "objdump", and
168 # VALUE is OPTION's value. The valid options are described below.
169 # Whitespace is ignored everywhere, except within VALUE. The option
170 # list ends with the first line that doesn't match the above syntax.
171 # However, a line within the options that begins with a #, but doesn't
172 # have a recognizable option name followed by a colon, is considered a
173 # comment and entirely ignored.
174 #
175 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
176 # two-element lists. The first element of each is an option name, and
177 # the second additional arguments to be added on to the end of the
178 # option list as given in FILE.d. (If omitted, no additional options
179 # are added.)
180 #
181 # The interesting options are:
182 #
183 # name: TEST-NAME
184 # The name of this test, passed to DejaGNU's `pass' and `fail'
185 # commands. If omitted, this defaults to FILE, the root of the
186 # .s and .d files' names.
187 #
188 # as: FLAGS
189 # When assembling FILE.s, pass FLAGS to the assembler.
190 #
191 # PROG: PROGRAM-NAME
192 # The name of the program to run to modify or analyze the .o file
193 # produced by the assembler. This option is required. Recognised
194 # names are: ar, elfedit, nm, objcopy, ranlib, strings, and strip.
195 #
196 # DUMPPROG: PROGRAM-NAME
197 # The name of the program to run to analyze the .o file after it has
198 # has been modified by PROG. This can be omitted; run_dump_test will
199 # guess which program to run by seeing if any of the flags options
200 # for the recognised dump programs are set. Recognised names are:
201 # addr2line, nm, objdump, readelf and size.
202 #
203 # nm: FLAGS
204 # objcopy: FLAGS
205 # objdump: FLAGS
206 # readelf: FLAGS
207 # size: FLAGS
208 # Use the specified program to analyze the .o file, and pass it
209 # FLAGS, in addition to the .o file name. Note that they are run
210 # with LC_ALL=C in the environment to give consistent sorting
211 # of symbols.
212 #
213 # source: SOURCE
214 # Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
215 # This is useful if several .d files want to share a .s file.
216 #
217 # dump: DUMP
218 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
219 # is useful if several .d files differ by options only. Options are
220 # always read from FILE.d.
221 #
222 # target: GLOBS...
223 # Run this test only on a specified list of targets. More precisely,
224 # each glob in the space-separated list is passed to "istarget"; if
225 # it evaluates true for any of them, the test will be run, otherwise
226 # it will be marked unsupported.
227 #
228 # not-target: GLOBS...
229 # Do not run this test on a specified list of targets. Again,
230 # the each glob in the space-separated list is passed to
231 # "istarget", and the test is run if it evaluates *false* for
232 # *all* of them. Otherwise it will be marked unsupported.
233 #
234 # skip: GLOBS...
235 # not-skip: GLOBS...
236 # These are exactly the same as "not-target" and "target",
237 # respectively, except that they do nothing at all if the check
238 # fails. They should only be used in groups, to construct a single
239 # test which is run on all targets but with variant options or
240 # expected output on some targets. (For example, see
241 # gas/arm/inst.d and gas/arm/wince_inst.d.)
242 #
243 # error: REGEX
244 # An error with message matching REGEX must be emitted for the test
245 # to pass. The PROG, objdump, nm and objcopy options have no
246 # meaning and need not supplied if this is present.
247 #
248 # warning: REGEX
249 # Expect a gas warning matching REGEX. It is an error to issue
250 # both "error" and "warning".
251 #
252 # stderr: FILE
253 # FILE contains regexp lines to be matched against the diagnostic
254 # output of the assembler. This does not preclude the use of
255 # PROG, nm, objdump, or objcopy.
256 #
257 # error-output: FILE
258 # Means the same as 'stderr', but also indicates that the assembler
259 # is expected to exit unsuccessfully (therefore PROG, objdump, nm,
260 # and objcopy have no meaning and should not be supplied).
261 #
262 # Each option may occur at most once.
263 #
264 # After the option lines come regexp lines. `run_dump_test' calls
265 # `regexp_diff' to compare the output of the dumping tool against the
266 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
267 # see further comments there.
268
269 proc run_dump_test { name {extra_options {}} } {
270 global subdir srcdir
271 global OBJDUMP NM OBJCOPY READELF STRIP
272 global OBJDUMPFLAGS NMFLAGS OBJCOPYFLAGS READELFFLAGS STRIPFLAGS
273 global ELFEDIT ELFEDITFLAGS
274 global host_triplet
275 global env
276 global copyfile
277 global tempfile
278
279 if [string match "*/*" $name] {
280 set file $name
281 set name [file tail $name]
282 } else {
283 set file "$srcdir/$subdir/$name"
284 }
285 set opt_array [slurp_options "${file}.d"]
286 if { $opt_array == -1 } {
287 perror "error reading options from $file.d"
288 unresolved $subdir/$name
289 return
290 }
291 set opts(addr2line) {}
292 set opts(ar) {}
293 set opts(as) {}
294 set opts(elfedit) {}
295 set opts(name) {}
296 set opts(nm) {}
297 set opts(objcopy) {}
298 set opts(objdump) {}
299 set opts(ranlib) {}
300 set opts(readelf) {}
301 set opts(size) {}
302 set opts(strings) {}
303 set opts(strip) {}
304 set opts(PROG) {}
305 set opts(DUMPPROG) {}
306 set opts(source) {}
307 set opts(dump) {}
308 set opts(target) {}
309 set opts(not-target) {}
310 set opts(skip) {}
311 set opts(not-skip) {}
312
313 foreach i $opt_array {
314 set opt_name [lindex $i 0]
315 set opt_val [lindex $i 1]
316 if ![info exists opts($opt_name)] {
317 perror "unknown option $opt_name in file $file.d"
318 unresolved $subdir/$name
319 return
320 }
321
322 # Permit the option to use $srcdir to refer to the source
323 # directory.
324 regsub -all "\\\$srcdir" "$opt_val" "$srcdir/$subdir" opt_val
325
326 if [string length $opts($opt_name)] {
327 perror "option $opt_name multiply set in $file.d"
328 unresolved $subdir/$name
329 return
330 }
331 set opts($opt_name) $opt_val
332 }
333
334 foreach i $extra_options {
335 set opt_name [lindex $i 0]
336 set opt_val [lindex $i 1]
337 if ![info exists opts($opt_name)] {
338 perror "unknown option $opt_name given in extra_opts"
339 unresolved $subdir/$name
340 return
341 }
342
343 # Permit the option to use $srcdir to refer to the source
344 # directory.
345 regsub -all "\\\$srcdir" "$opt_val" "$srcdir/$subdir" opt_val
346
347 # add extra option to end of existing option, adding space
348 # if necessary.
349 if [string length $opts($opt_name)] {
350 append opts($opt_name) " "
351 }
352 append opts($opt_name) $opt_val
353 }
354
355 if { $opts(name) == "" } {
356 set testname "$subdir/$name"
357 } else {
358 set testname $opts(name)
359 }
360 verbose "Testing $testname"
361
362 if {$opts(PROG) == ""} {
363 perror "PROG isn't set in $file.d"
364 unresolved $testname
365 return
366 }
367
368 set destopt ""
369 switch -- $opts(PROG) {
370 ar { set program ar }
371 elfedit { set program elfedit }
372 nm { set program nm }
373 objcopy { set program objcopy }
374 ranlib { set program ranlib }
375 strings { set program strings }
376 strip {
377 set program strip
378 set destopt "-o"
379 }
380 default {
381 perror "unrecognized program option $opts(PROG) in $file.d"
382 unresolved $testname
383 return }
384 }
385
386 set dumpprogram ""
387 if { $opts(DUMPPROG) != "" } {
388 switch -- $opts(DUMPPROG) {
389 addr2line { set dumpprogram addr2line }
390 nm { set dumpprogram nm }
391 objdump { set dumpprogram objdump }
392 readelf { set dumpprogram readelf }
393 size { set dumpprogram size }
394 default {
395 perror "unrecognized dump program option $opts(DUMPPROG) in $file.d"
396 unresolved $testname
397 return }
398 }
399 } else {
400 # Guess which program to run, by seeing which option was specified.
401 foreach p {addr2line nm objdump readelf size} {
402 if {$opts($p) != ""} {
403 if {$dumpprogram != ""} {
404 perror "more than one possible dump program specified in $file.d"
405 unresolved $testname
406 return
407 } else {
408 set dumpprogram $p
409 }
410 }
411 }
412 }
413
414 # Handle skipping the test on specified targets.
415 # You can have both skip/not-skip and target/not-target, but you can't
416 # have both skip and not-skip, or target and not-target, in the same file.
417 if { $opts(skip) != "" } then {
418 if { $opts(not-skip) != "" } then {
419 perror "$testname: mixing skip and not-skip directives is invalid"
420 unresolved $testname
421 return
422 }
423 foreach glob $opts(skip) {
424 if {[istarget $glob]} { return }
425 }
426 }
427 if { $opts(not-skip) != "" } then {
428 set skip 1
429 foreach glob $opts(not-skip) {
430 if {[istarget $glob]} {
431 set skip 0
432 break
433 }
434 }
435 if {$skip} { return }
436 }
437 if { $opts(target) != "" } then {
438 set skip 1
439 foreach glob $opts(target) {
440 if {[istarget $glob]} {
441 set skip 0
442 break
443 }
444 }
445 if {$skip} {
446 unsupported $testname
447 return
448 }
449 }
450 if { $opts(not-target) != "" } then {
451 foreach glob $opts(not-target) {
452 if {[istarget $glob]} {
453 unsupported $testname
454 return
455 }
456 }
457 }
458
459 if { $opts(source) == "" } {
460 set srcfile ${file}.s
461 } else {
462 set srcfile $srcdir/$subdir/$opts(source)
463 }
464
465 if { $opts(dump) == "" } {
466 set dumpfile ${file}.d
467 } else {
468 set dumpfile $srcdir/$subdir/$opts(dump)
469 }
470
471 if { $opts(as) == "binary" } {
472 while {[file type $srcfile] eq "link"} {
473 set newfile [file readlink $srcfile]
474 if {[string index $newfile 0] ne "/"} {
475 set newfile [file dirname $srcfile]/$newfile
476 }
477 set srcfile $newfile
478 }
479 # Make sure we copy the file if we are doing remote host testing.
480 remote_download host ${srcfile} $tempfile
481 } else {
482 set exec_output [binutils_assemble_flags ${srcfile} $tempfile $opts(as)]
483 if [string match "" $exec_output] then {
484 send_log "$exec_output\n"
485 verbose "$exec_output"
486 fail $testname
487 return
488 }
489 }
490
491 set progopts1 $opts($program)
492 eval set progopts \$[string toupper $program]FLAGS
493 eval set binary \$[string toupper $program]
494
495 set exec_output [binutils_run $binary "$progopts $progopts1 $tempfile $destopt ${copyfile}.o"]
496 if ![string match "" $exec_output] {
497 send_log "$exec_output\n"
498 verbose "$exec_output"
499 fail $testname
500 return
501 }
502
503 set progopts1 $opts($dumpprogram)
504 eval set progopts \$[string toupper $dumpprogram]FLAGS
505 eval set binary \$[string toupper $dumpprogram]
506
507 if { ![is_remote host] && [which $binary] == 0 } {
508 untested $testname
509 return
510 }
511
512 # For objdump, automatically translate standard section names to the targets one,
513 # if they are different.
514 set sect_names [get_standard_section_names]
515 if { $sect_names != "" && $dumpprogram == "objdump"} {
516 regsub -- "-j \\.text" $progopts1 "-j [lindex $sect_names 0]" progopts1
517 regsub -- "-j \\.data" $progopts1 "-j [lindex $sect_names 1]" progopts1
518 regsub -- "-j \\.bss" $progopts1 "-j [lindex $sect_names 2]" progopts1
519 }
520
521 verbose "running $binary $progopts $progopts1" 3
522
523 set cmd "$binary $progopts $progopts1 ${copyfile}.o"
524
525 # Ensure consistent sorting of symbols
526 if {[info exists env(LC_ALL)]} {
527 set old_lc_all $env(LC_ALL)
528 }
529 set env(LC_ALL) "C"
530 send_log "$cmd\n"
531 set comp_output [remote_exec host $cmd "" "/dev/null" "tmpdir/dump.out"]
532 if {[info exists old_lc_all]} {
533 set env(LC_ALL) $old_lc_all
534 } else {
535 unset env(LC_ALL)
536 }
537 if { [lindex $comp_output 0] != 0 } then {
538 send_log "$comp_output\n"
539 fail $testname
540 return
541 }
542 set comp_output [prune_warnings [lindex $comp_output 1]]
543 if ![string match "" $comp_output] then {
544 send_log "$comp_output\n"
545 fail $testname
546 return
547 }
548
549 verbose_eval {[file_contents "tmpdir/dump.out"]} 3
550 if { [regexp_diff "tmpdir/dump.out" "${dumpfile}"] } then {
551 fail $testname
552 verbose "output is [file_contents "tmpdir/dump.out"]" 2
553 return
554 }
555
556 pass $testname
557 }
558
559 proc slurp_options { file } {
560 if [catch { set f [open $file r] } x] {
561 #perror "couldn't open `$file': $x"
562 perror "$x"
563 return -1
564 }
565 set opt_array {}
566 # whitespace expression
567 set ws {[ ]*}
568 set nws {[^ ]*}
569 # whitespace is ignored anywhere except within the options list;
570 # option names are alphabetic plus dash
571 set pat "^#${ws}(\[a-zA-Z-\]*)$ws:${ws}(.*)$ws\$"
572 while { [gets $f line] != -1 } {
573 set line [string trim $line]
574 # Whitespace here is space-tab.
575 if [regexp $pat $line xxx opt_name opt_val] {
576 # match!
577 lappend opt_array [list $opt_name $opt_val]
578 } elseif {![regexp "^#" $line ]} {
579 break
580 }
581 }
582 close $f
583 return $opt_array
584 }
585
586 proc file_contents { filename } {
587 set file [open $filename r]
588 set contents [read $file]
589 close $file
590 return $contents
591 }
592
593 proc verbose_eval { expr { level 1 } } {
594 global verbose
595 if $verbose>$level then { eval verbose "$expr" $level }
596 }
597
598 # Internal procedure: return the names of the standard sections
599 #
600 proc get_standard_section_names {} {
601 if [istarget "rx-*-*"] {
602 return { "P" "D_1" "B_1" }
603 }
604 if [istarget "alpha*-*-*vms*"] {
605 # Double quote: for TCL and for sh.
606 return { "\\\$CODE\\\$" "\\\$DATA\\\$" "\\\$BSS\\\$" }
607 }
608 return
609 }