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