]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/testsuite/lib/binutils-common.exp
A few hppa testcase tidies
[thirdparty/binutils-gdb.git] / binutils / testsuite / lib / binutils-common.exp
1 # Copyright (C) 1993-2018 Free Software Foundation, Inc.
2 #
3 # This file is part of the GNU Binutils.
4 #
5 # This file is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19
20 # True if the object format is known to be ELF.
21 #
22 proc is_elf_format {} {
23 # config.sub for these targets curiously transforms a target doublet
24 # ending in -elf to -none. eg. m68hc12-elf to m68hc12-unknown-none
25 # They are always elf.
26 if { [istarget m68hc1*-*] || [istarget s12z*-*] || [istarget xgate-*] } {
27 return 1;
28 }
29 # vxworks (and windiss) excluded due to number of ELF tests that need
30 # modifying to pass on those targets.
31 # && ![istarget *-*-vxworks*]
32 # && ![istarget *-*-windiss*]
33
34 if { ![istarget *-*-chorus*]
35 && ![istarget *-*-cloudabi*]
36 && ![istarget *-*-eabi*]
37 && ![istarget *-*-*elf*]
38 && ![istarget *-*-*freebsd*]
39 && ![istarget *-*-fuchsia*]
40 && ![istarget *-*-gnu*]
41 && ![istarget *-*-irix5*]
42 && ![istarget *-*-irix6*]
43 && ![istarget *-*-kaos*]
44 && ![istarget *-*-*linux*]
45 && ![istarget *-*-lynxos*]
46 && ![istarget *-*-nacl*]
47 && ![istarget *-*-netbsd*]
48 && ![istarget *-*-nto*]
49 && ![istarget *-*-openbsd*]
50 && ![istarget *-*-rtems*]
51 && ![istarget *-*-solaris2*]
52 && ![istarget *-*-sysv4*]
53 && ![istarget *-*-unixware*]
54 && ![istarget *-*-wasm32*]
55 && ![istarget avr-*-*]
56 && ![istarget hppa*64*-*-hpux*]
57 && ![istarget ia64-*-hpux*] } {
58 return 0
59 }
60
61 if { [istarget *-*-linux*ecoff*]
62 || [istarget *-*-rtemscoff*] } {
63 return 0
64 }
65
66 if { ![istarget *-*-netbsdelf*]
67 && ( [istarget vax-*-netbsd*]
68 || [istarget ns32k-*-netbsd*]) } {
69 return 0
70 }
71
72 if { [istarget arm-*-openbsd*]
73 || [istarget ns32k-*-openbsd*]
74 || [istarget vax-*-openbsd*] } {
75 return 0
76 }
77
78 return 1
79 }
80
81 # True if the object format is known to be a.out.
82 #
83 proc is_aout_format {} {
84 if { [istarget *-*-*aout*]
85 || [istarget *-*-bsd*]
86 || [istarget *-*-msdos*]
87 || [istarget ns32k-*-*]
88 || [istarget pdp11-*-*]
89 || [istarget vax-*-netbsd] } {
90 return 1
91 }
92 return 0
93 }
94
95 # True if the object format is known to be PE COFF.
96 #
97 proc is_pecoff_format {} {
98 if { ![istarget *-*-mingw*]
99 && ![istarget *-*-cygwin*]
100 && ![istarget *-*-cegcc*]
101 && ![istarget *-*-pe*] } {
102 return 0
103 }
104
105 return 1
106 }
107
108 proc is_som_format {} {
109 if { ![istarget hppa*-*-*] || [istarget hppa*64*-*-*] } {
110 return 0;
111 }
112 if { [istarget *-*-osf*] \
113 || [istarget {*-*-h[ip]ux*}] \
114 || [istarget *-*-mpeix*] \
115 || [istarget *-*-bsd*] } {
116 return 1;
117 }
118 return 0;
119 }
120
121 # True if the object format is known to be 64-bit ELF.
122 #
123 proc is_elf64 { binary_file } {
124 global READELF
125 global READELFFLAGS
126
127 set tmpfile [file dirname $binary_file]/readelf.out
128 set readelf_size ""
129 catch "exec $READELF $READELFFLAGS -h $binary_file > $tmpfile" got
130
131 if ![string match "" $got] then {
132 return 0
133 }
134
135 if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
136 [file_contents $tmpfile] nil readelf_size] } {
137 return 0
138 }
139
140 if { $readelf_size == "64" } {
141 return 1
142 }
143
144 return 0
145 }
146
147 # True if the object format is known to use RELA relocations.
148 #
149 proc is_rela { binary_file } {
150 global READELF
151 global READELFFLAGS
152
153 set tmpfile [file dirname $binary_file]/readelf.out
154 catch "exec $READELF $READELFFLAGS -S $binary_file > $tmpfile" got
155
156 if ![string match "" $got] then {
157 return 0
158 }
159
160 if { ![regexp "RELA" [file_contents $tmpfile]] } {
161 return 0
162 }
163
164 return 1
165 }
166
167 # True if the target matches TARGET, specified as a TCL procedure if
168 # in square brackets or as machine triplet otherwise.
169 #
170 proc match_target { target } {
171 if [string match {\[*\]} $target] {
172 return $target
173 } else {
174 return [istarget $target]
175 }
176 }
177
178 # True if the ELF target supports STB_GNU_UNIQUE with the ELF header's
179 # OSABI field set to ELFOSABI_GNU.
180 #
181 # This generally depends on the target OS only, however there are a
182 # number of exceptions for bare metal targets as follows. The MSP430
183 # and Visium targets set OSABI to ELFOSABI_STANDALONE and cannot
184 # support STB_GNU_UNIQUE. Likewise non-EABI ARM targets set OSABI to
185 # ELFOSABI_ARM, and TI C6X targets to ELFOSABI_C6000_*. Finally
186 # rather than `bfd_elf_final_link' AM33/2.0, D30V, DLX, and
187 # picoJava targets use `_bfd_generic_final_link', which does not
188 # support STB_GNU_UNIQUE symbol binding causing assertion failures.
189 #
190 proc supports_gnu_unique {} {
191 if { [istarget *-*-gnu*]
192 || [istarget *-*-linux*]
193 || [istarget *-*-nacl*] } {
194 return 1
195 }
196 if { [istarget "arm*-*-*eabi*"] } {
197 return 1
198 }
199 if { [istarget "wasm32*-*-*"] } {
200 return 1
201 }
202 if { ![istarget "*-*-elf*"] } {
203 return 0
204 }
205 if { [istarget "arm*-*-*"]
206 || [istarget "msp430-*-*"]
207 || [istarget "tic6x-*-*"]
208 || [istarget "visium-*-*"] } {
209 return 0
210 }
211 if { [istarget "am33_2.0-*-*"]
212 || [istarget "d30v-*-*"]
213 || [istarget "dlx-*-*"]
214 || [istarget "pj*-*-*"]
215 || [istarget "xgate-*-*"] } {
216 return 0
217 }
218 return 1
219 }
220
221 # True for targets that do not sort .symtab as per the ELF standard.
222 # ie. any that have mips_elf32_be_vec, mips_elf32_le_vec,
223 # mips_elf32_n_be_vec or mips_elf32_n_le_vec as the primary bfd target
224 # vector in config.bfd. When syncing with config.bfd, don't forget that
225 # earlier case-matches trump later ones.
226 proc is_bad_symtab {} {
227 if { ![istarget "mips*-*-*"] } {
228 return 0;
229 }
230 if { [istarget "*-*-chorus*"]
231 || [istarget "*-*-irix5*"]
232 || [istarget "*-*-irix6*"]
233 || [istarget "*-*-none"]
234 || [istarget "*-*-rtems*"]
235 || [istarget "*-*-windiss"] } {
236 return 1;
237 }
238 if { [istarget "*-*-elf*"]
239 && ![istarget "*-sde-*"]
240 && ![istarget "*-mti-*"]
241 && ![istarget "*-img-*"] } {
242 return 1;
243 }
244 if { [istarget "*-*-openbsd*"]
245 && ![istarget "mips64*-*-*"] } {
246 return 1;
247 }
248 return 0;
249 }
250
251 # Compare two files line-by-line. FILE_1 is the actual output and FILE_2
252 # is the expected output. Ignore blank lines in either file.
253 #
254 # FILE_2 is a series of regexps, comments and # directives. The directives
255 # are:
256 #
257 # #pass
258 # Treat the test as a PASS if everything up till this point has
259 # matched. Ignore any remaining lines in either FILE_1 or FILE_2.
260 #
261 # #failif
262 # Reverse the sense of the test: expect differences to exist.
263 #
264 # #...
265 # REGEXP
266 # Skip all lines in FILE_1 until the first that matches REGEXP.
267 #
268 # Other # lines are comments. Regexp lines starting with the `!' character
269 # specify inverse matching (use `\!' for literal matching against a leading
270 # `!'). Skip empty lines in both files.
271 #
272 # The first optional argument is a list of regexp substitutions of the form:
273 #
274 # EXP1 SUBSPEC1 EXP2 SUBSPEC2 ...
275 #
276 # This tells the function to apply each regexp substitution EXPi->SUBSPECi
277 # in order to every line of FILE_2.
278 #
279 # Return nonzero if differences exist.
280 proc regexp_diff { file_1 file_2 args } {
281 set eof -1
282 set end_1 0
283 set end_2 0
284 set differences 0
285 set diff_pass 0
286 set fail_if_match 0
287 set ref_subst ""
288 if { [llength $args] > 0 } {
289 set ref_subst [lindex $args 0]
290 }
291 if { [llength $args] > 1 } {
292 perror "Too many arguments to regexp_diff"
293 return 1
294 }
295
296 if [file exists $file_1] then {
297 set file_a [open $file_1 r]
298 } else {
299 perror "$file_1 doesn't exist"
300 return 1
301 }
302
303 if [file exists $file_2] then {
304 set file_b [open $file_2 r]
305 } else {
306 perror "$file_2 doesn't exist"
307 close $file_a
308 return 1
309 }
310
311 verbose " Regexp-diff'ing: $file_1 $file_2" 2
312
313 while { 1 } {
314 set line_a ""
315 set line_b ""
316 while { [string length $line_a] == 0 } {
317 # Ignore blank line in FILE_1.
318 if { [gets $file_a line_a] == $eof } {
319 set end_1 1
320 break
321 }
322 }
323 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
324 if { [string match "#pass" $line_b] } {
325 set end_2 1
326 set diff_pass 1
327 break
328 } elseif { [string match "#failif" $line_b] } {
329 send_log "fail if no difference\n"
330 verbose "fail if no difference" 3
331 set fail_if_match 1
332 } elseif { [string match "#..." $line_b] } {
333 if { [gets $file_b line_b] == $eof } {
334 set end_2 1
335 set diff_pass 1
336 break
337 }
338 set negated [expr { [string index $line_b 0] == "!" }]
339 set line_bx [string range $line_b $negated end]
340 set n [expr { $negated ? "! " : "" }]
341 # Substitute on the reference.
342 foreach {name value} $ref_subst {
343 regsub -- $name $line_bx $value line_bx
344 }
345 verbose "looking for $n\"^$line_bx$\"" 3
346 while { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
347 verbose "skipping \"$line_a\"" 3
348 if { [gets $file_a line_a] == $eof } {
349 set end_1 1
350 break
351 }
352 }
353 break
354 }
355 if { [gets $file_b line_b] == $eof } {
356 set end_2 1
357 break
358 }
359 }
360
361 if { $diff_pass } {
362 break
363 } elseif { $end_1 && $end_2 } {
364 break
365 } elseif { $end_1 } {
366 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
367 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
368 set differences 1
369 break
370 } elseif { $end_2 } {
371 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
372 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
373 set differences 1
374 break
375 } else {
376 set negated [expr { [string index $line_b 0] == "!" }]
377 set line_bx [string range $line_b $negated end]
378 set n [expr { $negated ? "! " : "" }]
379 set s [expr { $negated ? " " : "" }]
380 # Substitute on the reference.
381 foreach {name value} $ref_subst {
382 regsub -- $name $line_bx $value line_bx
383 }
384 verbose "regexp $n\"^$line_bx$\"\nline \"$line_a\"" 3
385 if { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
386 send_log "regexp_diff match failure\n"
387 send_log "regexp $n\"^$line_bx$\"\nline $s\"$line_a\"\n"
388 verbose "regexp_diff match failure\n" 3
389 set differences 1
390 }
391 }
392 }
393
394 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
395 send_log "$file_1 and $file_2 are different lengths\n"
396 verbose "$file_1 and $file_2 are different lengths" 3
397 set differences 1
398 }
399
400 if { $fail_if_match } {
401 if { $differences == 0 } {
402 set differences 1
403 } else {
404 set differences 0
405 }
406 }
407
408 close $file_a
409 close $file_b
410
411 return $differences
412 }
413
414 # prune_warnings_extra -- delete extra warnings from TEXT.
415 #
416 # An example is:
417 # ld: warning: /lib64/ld-linux-x86-64.so.2: unsupported GNU_PROPERTY_TYPE (5) type : 0xc0010001
418 proc prune_warnings_extra { text } {
419 global experimental
420 # Warnings are only pruned from non-experimental code (ie code not
421 # on a release branch). For experimental code we want the warnings
422 # as they indicate that the sources need to be updated to recognise
423 # the new properties.
424 if { "$experimental" == "false" } {
425 # The "\\1" is to try to preserve a "\n" but only if necessary.
426 regsub -all "(^|\n)(\[^\n\]*: warning:\[^\n\]*unsupported GNU_PROPERTY_TYPE\[^\n\]*\n?)+" $text "\\1" text
427 }
428 return $text
429 }
430
431 # This definition is taken from an unreleased version of DejaGnu. Once
432 # that version gets released, and has been out in the world for a few
433 # months at least, it may be safe to delete this copy.
434 if ![string length [info proc prune_warnings]] {
435 #
436 # prune_warnings -- delete various system verbosities from TEXT
437 #
438 # An example is:
439 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
440 #
441 # Sites with particular verbose os's may wish to override this in site.exp.
442 #
443 proc prune_warnings { text } {
444 # This is from sun4's. Do it for all machines for now.
445 # The "\\1" is to try to preserve a "\n" but only if necessary.
446 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
447 # It might be tempting to get carried away and delete blank lines, etc.
448 # Just delete *exactly* what we're ask to, and that's it.
449 set text [prune_warnings_extra $text]
450 return $text
451 }
452 } elseif { [info procs saved-prune_warnings] == [list] } {
453 rename prune_warnings saved-prune_warnings
454 proc prune_warnings { text } {
455 set text [saved-prune_warnings $text]
456 set text [prune_warnings_extra $text]
457 return $text
458 }
459 }
460
461 # run_dump_test FILE (optional:) EXTRA_OPTIONS
462 #
463 # Assemble a .s file, then run some utility on it and check the output.
464 #
465 # There should be an assembly language file named FILE.s in the test
466 # suite directory, and a pattern file called FILE.d. run_dump_test
467 # will assemble FILE.s, optionally run objcopy on the object file,
468 # optionally run ld, optionally run another objcopy, optionally run
469 # another tool under test specified by PROG, then run a dump tool like
470 # addr2line, nm, objdump, readelf or size on the object file to produce
471 # textual output, and then analyze that with regexps.
472 # The FILE.d file specifies what program to run, and what to expect in
473 # its output.
474 #
475 # The FILE.d file begins with zero or more option lines, which specify
476 # flags to pass to the assembler, the program to run to dump the
477 # assembler's output, and the options it wants. The option lines have
478 # the syntax:
479 #
480 # # OPTION: VALUE
481 #
482 # OPTION is the name of some option, like "name" or "objdump", and
483 # VALUE is OPTION's value. The valid options are described below.
484 # Whitespace is ignored everywhere, except within VALUE. The option
485 # list ends with the first line that doesn't match the above syntax.
486 # However, a line within the options that begins with a #, but doesn't
487 # have a recognizable option name followed by a colon, is considered a
488 # comment and entirely ignored.
489 #
490 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
491 # two-element lists. The first element of each is an option name, and
492 # the second additional arguments to be added on to the end of the
493 # option list as given in FILE.d. (If omitted, no additional options
494 # are added.)
495 #
496 # The interesting options are:
497 #
498 # name: TEST-NAME
499 # The name of this test, passed to DejaGNU's `pass' and `fail'
500 # commands. If omitted, this defaults to FILE, the root of the
501 # .s and .d files' names.
502 #
503 # as: FLAGS
504 # When assembling, pass FLAGS to the assembler.
505 # If assembling several files, you can pass different assembler
506 # options in the "source" directives. See below.
507 #
508 # ld: FLAGS
509 # Link assembled files using FLAGS, in the order of the "source"
510 # directives, when using multiple files.
511 #
512 # ld_after_inputfiles: FLAGS
513 # Similar to "ld", but put FLAGS after all input files.
514 #
515 # objcopy_objects: FLAGS
516 # Run objcopy with the specified flags after assembling any source
517 # that has the special marker RUN_OBJCOPY in the source specific
518 # flags.
519 #
520 # objcopy_linked_file: FLAGS
521 # Run objcopy on the linked file with the specified flags.
522 # This lets you transform the linked file using objcopy, before the
523 # result is analyzed by an analyzer program specified below.
524 #
525 # PROG: PROGRAM-NAME
526 # The name of a program under test, to run to modify or analyze the
527 # .o file produced by the assembler. Recognised names are: ar,
528 # elfedit, nm, objcopy, ranlib, strings, and strip.
529 #
530 # DUMPPROG: PROGRAM-NAME
531 # The name of the program to run to analyze the file produced
532 # by the assembler or the linker. This can be omitted;
533 # run_dump_test will guess which program to run from which of
534 # the flags options below is present.
535 #
536 # addr2line: FLAGS
537 # nm: FLAGS
538 # objdump: FLAGS
539 # readelf: FLAGS
540 # size: FLAGS
541 # Use the specified program to analyze the output file, and pass it
542 # FLAGS, in addition to the output name. Note that they are run
543 # with LC_ALL=C in the environment to give consistent sorting of
544 # symbols. If no FLAGS are needed then you can use:
545 # DUMPPROG: [nm objdump readelf addr2line]
546 # instead, or just pass a flag that happens to be the default.
547 # If objdump is the dump tool and we're not dumping binary, nor
548 # have run ld, then the standard section names (.text, .data and
549 # .bss) are replaced by target ones if any (eg. rx-elf uses "P"
550 # instead of .text). The substition is done for both the
551 # objdump options (eg: "-j .text" is replaced by "-j P") and the
552 # reference file.
553 #
554 # source: SOURCE [FLAGS]
555 # Assemble the file SOURCE.s using the flags in the "as" directive
556 # and the (optional) FLAGS. If omitted, the source defaults to
557 # FILE.s.
558 # This is useful if several .d files want to share a .s file.
559 # More than one "source" directive can be given, which is useful
560 # when testing linking.
561 #
562 # dump: DUMP
563 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
564 # is useful if several .d files differ by options only. Options are
565 # always read from FILE.d.
566 #
567 # target: GLOB|PROC ...
568 # Run this test only on a specified list of targets. More precisely,
569 # in the space-separated list each glob is passed to "istarget" and
570 # each proc is called as a TCL procedure. List items are interpreted
571 # such that procs are denoted by surrounding square brackets, and any
572 # other items are consired globs. If the call evaluates true for any
573 # of them, the test will be run, otherwise it will be marked
574 # unsupported.
575 #
576 # notarget: GLOB|PROC ...
577 # Do not run this test on a specified list of targets. Again, each
578 # glob in the space-separated list is passed to "istarget" and each
579 # proc is called as a TCL procedure, and the test is run if it
580 # evaluates *false* for *all* of them. Otherwise it will be marked
581 # unsupported.
582 #
583 # alltargets: GLOB|PROC ...
584 # Run this test on a specified list of targets. Again, each
585 # glob in the space-separated list is passed to "istarget" and each
586 # proc is called as a TCL procedure, and the test is run if it
587 # evaluates *true* for *all* of them. Otherwise it will be marked
588 # unsupported.
589 #
590 # skip: GLOB|PROC ...
591 # anyskip: GLOB|PROC ...
592 # noskip: GLOB|PROC ...
593 # These are exactly the same as "notarget", "alltargets" and
594 # "target" respectively, except that they do nothing at all if the
595 # check fails. They should only be used in groups, to construct a
596 # single test which is run on all targets but with variant options
597 # or expected output on some targets. (For example, see
598 # gas/arm/inst.d and gas/arm/wince_inst.d.)
599 #
600 # xfail: GLOB|PROC ...
601 # Run this test and it is is expected to fail on a specified list
602 # of targets.
603 #
604 # error: REGEX
605 # An error with message matching REGEX must be emitted for the test
606 # to pass. The DUMPPROG, addr2line, nm, objdump, readelf and size
607 # options have no meaning and need not supplied if this is present.
608 # Multiple "error" directives append to the expected error message.
609 #
610 # error_output: FILE
611 # Means the same as 'error', except the regular expression lines
612 # are contains in FILE.
613 #
614 # warning: REGEX
615 # Expect a warning matching REGEX. It is an error to issue
616 # both "error" and "warning". Multiple "warning" directives
617 # append to the expected warning message.
618 #
619 # warning_output: FILE
620 # Means the same as 'warning', except the regular expression
621 # lines are contains in FILE.
622 #
623 # map: FILE
624 # Adding this option will cause the linker to generate a linker
625 # map file, using the -Map=MAPFILE command line option. If
626 # there is no -Map=MAPFILE in the 'ld: FLAGS' then one will be
627 # added to the linker command line. The contents of the
628 # generated MAPFILE are then compared against the regexp lines
629 # in FILE using `regexp_diff' (see below for details).
630 #
631 # section_subst: no
632 # Means that the section substitution for objdump is disabled.
633 #
634 # Each option may occur at most once unless otherwise mentioned.
635 #
636 # After the option lines come regexp lines. run_dump_test calls
637 # regexp_diff to compare the output of the dumping tool against the
638 # regexps in FILE.d.
639 #
640 proc run_dump_test { name {extra_options {}} } {
641 global ADDR2LINE ADDR2LINEFLAGS AS ASFLAGS ELFEDIT ELFEDITFLAGS LD LDFLAGS
642 global NM NMFLAGS OBJCOPY OBJCOPYFLAGS OBJDUMP OBJDUMPFLAGS
643 global READELF READELFFLAGS STRIP STRIPFLAGS
644 global copyfile env ld_elf_shared_opt runtests srcdir subdir verbose
645
646 if [string match "*/*" $name] {
647 set file $name
648 set name [file tail $name]
649 } else {
650 set file "$srcdir/$subdir/$name"
651 }
652
653 if ![runtest_file_p $runtests $name] then {
654 return
655 }
656
657 set opt_array [slurp_options "${file}.d"]
658 if { $opt_array == -1 } {
659 perror "error reading options from $file.d"
660 unresolved $subdir/$name
661 return
662 }
663 set dumpfile tmpdir/dump.out
664 set run_ld 0
665 set run_objcopy 0
666 set objfile_names {}
667 set opts(PROG) {}
668 set opts(DUMPPROG) {}
669 set opts(addr2line) {}
670 set opts(alltargets) {}
671 set opts(anyskip) {}
672 set opts(ar) {}
673 set opts(as) {}
674 set opts(dump) {}
675 set opts(elfedit) {}
676 set opts(error) {}
677 set opts(error_output) {}
678 set opts(ld) {}
679 set opts(ld_after_inputfiles) {}
680 set opts(map) {}
681 set opts(name) {}
682 set opts(nm) {}
683 set opts(noskip) {}
684 set opts(notarget) {}
685 set opts(objcopy) {}
686 set opts(objcopy_linked_file) {}
687 set opts(objcopy_objects) {}
688 set opts(objdump) {}
689 set opts(ranlib) {}
690 set opts(readelf) {}
691 set opts(section_subst) {}
692 set opts(size) {}
693 set opts(strings) {}
694 set opts(strip) {}
695 set opts(skip) {}
696 set opts(source) {}
697 set opts(strip) {}
698 set opts(target) {}
699 set opts(warning) {}
700 set opts(warning_output) {}
701 set opts(xfail) {}
702
703 set in_extra 0
704 foreach i [concat $opt_array {{} {}} $extra_options] {
705 set opt_name [lindex $i 0]
706 set opt_val [lindex $i 1]
707 if { $opt_name == "" } {
708 set in_extra 1
709 continue
710 }
711 if ![info exists opts($opt_name)] {
712 perror "unknown option $opt_name in file $file.d"
713 unresolved $subdir/$name
714 return
715 }
716
717 # Allow more substitutions, including tcl functions, for as and ld.
718 # Not done in general because extra quoting is needed for glob
719 # args used for example in binutils-all/remove-relocs-04.d.
720 if { $opt_name == "as" || $opt_name == "ld" } {
721 set opt_val [subst $opt_val]
722 } else {
723 # Just substitute $srcdir and $subdir
724 regsub -all {\$srcdir} "$opt_val" "$srcdir" opt_val
725 regsub -all {\$subdir} "$opt_val" "$subdir" opt_val
726 }
727
728 switch -- $opt_name {
729 xfail {}
730 target {}
731 alltargets {}
732 notarget {}
733 skip {}
734 anyskip {}
735 noskip {}
736 warning {}
737 error {}
738 source {
739 # Move any source-specific as-flags to a separate list to
740 # simplify processing.
741 if { [llength $opt_val] > 1 } {
742 lappend asflags [lrange $opt_val 1 end]
743 set opt_val [lindex $opt_val 0]
744 } else {
745 lappend asflags {}
746 }
747
748 # Create the object file name based on nothing but the source
749 # file name.
750 set new_objfile \
751 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]].o]
752 # But, sometimes, we have the exact same source filename in
753 # different directories (foo/src.s bar/src.s) which would lead
754 # us to try and create two src.o files. We detect this
755 # conflict here, and instead create src.o and src1.o.
756 set j 0
757 while { [lsearch $objfile_names $new_objfile] != -1 } {
758 incr j
759 set new_objfile \
760 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]]${j}.o]
761 }
762 lappend objfile_names $new_objfile
763 }
764 default {
765 if { !$in_extra && [string length $opts($opt_name)] } {
766 perror "option $opt_name multiply set in $file.d"
767 unresolved $subdir/$name
768 return
769 }
770
771 # A single "#ld:" with no options should do the right thing.
772 if { $opt_name == "ld" } {
773 set run_ld 1
774 }
775 # Likewise objcopy_linked_file.
776 if { $opt_name == "objcopy_linked_file" } {
777 set run_objcopy 1
778 }
779 }
780 }
781
782 # Append differently whether it's a message (without space) or
783 # an option or list (with space).
784 switch -- $opt_name {
785 warning -
786 error {
787 append opts($opt_name) $opt_val
788 }
789 default {
790 set opts($opt_name) [concat $opts($opt_name) $opt_val]
791 }
792 }
793 }
794
795 foreach opt { as ld } {
796 regsub {\[big_or_little_endian\]} $opts($opt) \
797 [big_or_little_endian] opts($opt)
798 }
799
800 if { $opts(name) == "" } {
801 set testname "$subdir/$name"
802 } else {
803 set testname $opts(name)
804 }
805
806 set err_warn 0
807 foreach opt { warning error warning_output error_output } {
808 if { $opts($opt) != "" } {
809 if { $err_warn } {
810 perror "$testname: bad mix of warning and error test directives"
811 unresolved $testname
812 return
813 }
814 set err_warn 1
815 }
816 }
817
818 # Decide early whether we should run the test for this target.
819 if { [llength $opts(noskip)] > 0 } {
820 set targmatch 0
821 foreach targ $opts(noskip) {
822 if [match_target $targ] {
823 set targmatch 1
824 break
825 }
826 }
827 if { $targmatch == 0 } {
828 return
829 }
830 }
831 foreach targ $opts(anyskip) {
832 if ![match_target $targ] {
833 return
834 }
835 }
836 foreach targ $opts(skip) {
837 if [match_target $targ] {
838 return
839 }
840 }
841 if { [llength $opts(target)] > 0 } {
842 set targmatch 0
843 foreach targ $opts(target) {
844 if [match_target $targ] {
845 set targmatch 1
846 break
847 }
848 }
849 if { $targmatch == 0 } {
850 unsupported $testname
851 return
852 }
853 }
854 foreach targ $opts(alltargets) {
855 if ![match_target $targ] {
856 unsupported $testname
857 return
858 }
859 }
860 foreach targ $opts(notarget) {
861 if [match_target $targ] {
862 unsupported $testname
863 return
864 }
865 }
866
867 set dumpprogram ""
868 # It's meaningless to require an output-testing method when we
869 # expect an error.
870 if { $opts(error) == "" && $opts(error_output) == "" } {
871 if { $opts(DUMPPROG) != "" } {
872 switch -- $opts(DUMPPROG) {
873 addr2line { set dumpprogram addr2line }
874 nm { set dumpprogram nm }
875 objdump { set dumpprogram objdump }
876 readelf { set dumpprogram readelf }
877 size { set dumpprogram size }
878 default {
879 perror "unrecognized DUMPPROG option $opts(DUMPPROG) in $file.d"
880 unresolved $testname
881 return
882 }
883 }
884 } else {
885 # Guess which program to run, by seeing which option was specified.
886 foreach p {addr2line nm objdump readelf size} {
887 if {$opts($p) != ""} {
888 if {$dumpprogram != ""} {
889 perror "ambiguous dump program in $file.d"
890 unresolved $testname
891 return
892 } else {
893 set dumpprogram $p
894 }
895 }
896 }
897 }
898 if { $dumpprogram == "" && $opts(map) == "" && !$err_warn } {
899 perror "dump program unspecified in $file.d"
900 unresolved $testname
901 return
902 }
903 }
904
905 if { $opts(source) == "" } {
906 set sourcefiles [list ${file}.s]
907 set asflags [list ""]
908 set objfile_names [list tmpdir/[file tail ${file}].o]
909 } else {
910 set sourcefiles {}
911 foreach sf $opts(source) {
912 if { [string match "/*" $sf] } {
913 lappend sourcefiles "$sf"
914 } else {
915 lappend sourcefiles "$srcdir/$subdir/$sf"
916 }
917 }
918 }
919
920 if { $opts(dump) == "" } {
921 set dfile ${file}.d
922 } else {
923 set dfile $srcdir/$subdir/$opts(dump)
924 }
925
926 # Time to setup xfailures.
927 foreach targ $opts(xfail) {
928 setup_xfail $targ
929 }
930
931 # Assemble each file.
932 set objfiles {}
933 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
934 set sourcefile [lindex $sourcefiles $i]
935 set sourceasflags [lindex $asflags $i]
936 set run_objcopy_objects 0
937
938 if { [string match "*RUN_OBJCOPY*" $sourceasflags] } {
939 set run_objcopy_objects 1
940 }
941 regsub "RUN_OBJCOPY" $sourceasflags "" sourceasflags
942
943 set objfile [lindex $objfile_names $i]
944 catch "exec rm -f $objfile" exec_output
945 lappend objfiles $objfile
946
947 if { $opts(as) == "binary" } {
948 while {[file type $sourcefile] eq "link"} {
949 set newfile [file readlink $sourcefile]
950 if {[string index $newfile 0] ne "/"} {
951 set newfile [file dirname $sourcefile]/$newfile
952 }
953 set sourcefile $newfile
954 }
955 set newfile [remote_download host $sourcefile $objfile]
956 set cmdret 0
957 if { $newfile == "" } {
958 set cmdret 1
959 }
960 } else {
961 if { [istarget "hppa*-*-*"] \
962 && ![istarget "*-*-linux*"] \
963 && ![istarget "*-*-netbsd*" ] } {
964 set cmd "sed -e 's/^\[ \]*\.comm \\(\[^,\]*\\),\\(.*\\)/\\1 .comm \\2/' < $sourcefile > tmpdir/asm.s"
965 send_log "$cmd\n"
966 set cmdret [remote_exec host [concat sh -c [list "$cmd"]]]
967 set cmdret [lindex $cmdret 0]
968 if { $cmdret != 0 } {
969 perror "sed failure"
970 unresolved $testname
971 return
972 }
973 set sourcefile tmpdir/asm.s
974 }
975 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
976
977 send_log "$cmd\n"
978 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "dump.tmp"]
979 remote_upload host "dump.tmp"
980 set comp_output [prune_warnings [file_contents "dump.tmp"]]
981 remote_file host delete "dump.tmp"
982 remote_file build delete "dump.tmp"
983 set cmdret [lindex $cmdret 0]
984 }
985 if { $cmdret == 0 && $run_objcopy_objects } {
986 set cmd "$OBJCOPY $opts(objcopy_objects) $objfile"
987
988 send_log "$cmd\n"
989 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] \
990 "" "/dev/null" "dump.tmp"]
991 remote_upload host "dump.tmp"
992 append comp_output [prune_warnings [file_contents "dump.tmp"]]
993 remote_file host delete "dump.tmp"
994 remote_file build delete "dump.tmp"
995 set cmdret [lindex $cmdret 0]
996 }
997 }
998
999 # Perhaps link the file(s).
1000 if { $cmdret == 0 && $run_ld } {
1001 set objfile "tmpdir/dump"
1002 catch "exec rm -f $objfile" exec_output
1003
1004 set ld_extra_opt ""
1005 if { [is_elf_format] && [check_shared_lib_support] } {
1006 set ld_extra_opt "$ld_elf_shared_opt"
1007 }
1008
1009 # Add -L$srcdir/$subdir so that the linker command can use
1010 # linker scripts in the source directory.
1011 set cmd "$LD $ld_extra_opt $LDFLAGS -L$srcdir/$subdir \
1012 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
1013
1014 # If needed then check for, or add a -Map option.
1015 set mapfile ""
1016 if { $opts(map) != "" } then {
1017 if { [regexp -- "-Map=(\[^ \]+)" $cmd all mapfile] } then {
1018 # Found existing mapfile option
1019 verbose -log "Existing mapfile '$mapfile' found"
1020 } else {
1021 # No mapfile option.
1022 set mapfile "tmpdir/dump.map"
1023 verbose -log "Adding mapfile '$mapfile'"
1024 set cmd "$cmd -Map=$mapfile"
1025 }
1026 }
1027
1028 send_log "$cmd\n"
1029 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "dump.tmp"]
1030 remote_upload host "dump.tmp"
1031 append comp_output [file_contents "dump.tmp"]
1032 remote_file host delete "dump.tmp"
1033 remote_file build delete "dump.tmp"
1034 set cmdret [lindex $cmdret 0]
1035
1036 if { $cmdret == 0 && $run_objcopy } {
1037 set infile $objfile
1038 set objfile "tmpdir/dump1"
1039 remote_file host delete $objfile
1040
1041 # Note that we don't use OBJCOPYFLAGS here; any flags must be
1042 # explicitly specified.
1043 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
1044
1045 send_log "$cmd\n"
1046 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "dump.tmp"]
1047 remote_upload host "dump.tmp"
1048 append comp_output [file_contents "dump.tmp"]
1049 remote_file host delete "dump.tmp"
1050 remote_file build delete "dump.tmp"
1051 set cmdret [lindex $cmdret 0]
1052 }
1053 } else {
1054 set objfile [lindex $objfiles 0]
1055 }
1056
1057 if { $cmdret == 0 && $opts(PROG) != "" } {
1058 set destopt ${copyfile}.o
1059 switch -- $opts(PROG) {
1060 ar { set program ar }
1061 elfedit {
1062 set program elfedit
1063 set destopt ""
1064 }
1065 nm { set program nm }
1066 objcopy { set program objcopy }
1067 ranlib { set program ranlib }
1068 strings { set program strings }
1069 strip {
1070 set program strip
1071 set destopt "-o $destopt"
1072 }
1073 default {
1074 perror "unrecognized PROG option $opts(PROG) in $file.d"
1075 unresolved $testname
1076 return
1077 }
1078 }
1079
1080 set progopts1 $opts($program)
1081 eval set progopts \$[string toupper $program]FLAGS
1082 eval set binary \$[string toupper $program]
1083
1084 if { ![is_remote host] && [which $binary] == 0 } {
1085 untested $testname
1086 return
1087 }
1088
1089 verbose "running $binary $progopts $progopts1" 3
1090 set cmd "$binary $progopts $progopts1 $objfile $destopt"
1091
1092 # Ensure consistent sorting of symbols
1093 if {[info exists env(LC_ALL)]} {
1094 set old_lc_all $env(LC_ALL)
1095 }
1096 set env(LC_ALL) "C"
1097 send_log "$cmd\n"
1098 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>dump.tmp"]] "" "/dev/null"]
1099 set cmdret [lindex $cmdret 0]
1100 remote_upload host "dump.tmp"
1101 append comp_output [prune_warnings [file_contents "dump.tmp"]]
1102 remote_file host delete "dump.tmp"
1103 remote_file build delete "dump.tmp"
1104 if {[info exists old_lc_all]} {
1105 set env(LC_ALL) $old_lc_all
1106 } else {
1107 unset env(LC_ALL)
1108 }
1109 if { $destopt != "" } {
1110 set objfile ${copyfile}.o
1111 }
1112 }
1113
1114 set want_out(source) ""
1115 set want_out(terminal) 0
1116 if { $err_warn } {
1117 if { $opts(error) != "" || $opts(error_output) != "" } {
1118 set want_out(terminal) 1
1119 }
1120
1121 if { $opts(error) != "" || $opts(warning) != "" } {
1122 set want_out(source) "regex"
1123 if { $opts(error) != "" } {
1124 set want_out(regex) $opts(error)
1125 } else {
1126 set want_out(regex) $opts(warning)
1127 }
1128 } else {
1129 set want_out(source) "file"
1130 if { $opts(error_output) != "" } {
1131 set want_out(file) $opts(error_output)
1132 } else {
1133 set want_out(file) $opts(warning_output)
1134 }
1135 }
1136 }
1137
1138 regsub "\n$" $comp_output "" comp_output
1139 if { $cmdret != 0 || $comp_output != "" || $want_out(source) != "" } {
1140 set exitstat "succeeded"
1141 if { $cmdret != 0 } { set exitstat "failed" }
1142
1143 if { $want_out(source) == "regex" } {
1144 verbose -log "$exitstat with: <$comp_output>, expected: <$want_out(regex)>"
1145 } elseif { $want_out(source) == "file" } {
1146 verbose -log "$exitstat with: <$comp_output>, expected in file $want_out(file)"
1147 set_file_contents "tmpdir/ld.messages" "$comp_output"
1148 } else {
1149 verbose -log "$exitstat with: <$comp_output>, no expected output"
1150 }
1151
1152 if { (($want_out(source) == "") == ($comp_output == "")) \
1153 && (($cmdret == 0) == ($want_out(terminal) == 0)) \
1154 && ((($want_out(source) == "regex") \
1155 && [regexp -- $want_out(regex) $comp_output]) \
1156 || (($want_out(source) == "file") \
1157 && (![regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$want_out(file)"]))) } {
1158 # We have the expected output.
1159 if { $want_out(terminal) || $dumpprogram == "" } {
1160 pass $testname
1161 return
1162 }
1163 } else {
1164 fail $testname
1165 return
1166 }
1167 }
1168
1169 # We must not have expected failure if we get here.
1170 if { $opts(error) != "" } {
1171 fail $testname
1172 return
1173 }
1174
1175 if { $opts(map) != "" } then {
1176 # Check the map file matches.
1177 set map_pattern_file $srcdir/$subdir/$opts(map)
1178 verbose -log "Compare '$mapfile' against '$map_pattern_file'"
1179 if { [regexp_diff $mapfile $map_pattern_file] } then {
1180 fail "$testname (map file check)"
1181 } else {
1182 pass "$testname (map file check)"
1183 }
1184
1185 if { $dumpprogram == "" } then {
1186 return
1187 }
1188 }
1189
1190 set progopts1 $opts($dumpprogram)
1191 eval set progopts \$[string toupper $dumpprogram]FLAGS
1192 eval set binary \$[string toupper $dumpprogram]
1193
1194 if { ![is_remote host] && [which $binary] == 0 } {
1195 untested $testname
1196 return
1197 }
1198
1199 # For objdump of gas output, automatically translate standard section names
1200 set sect_names ""
1201 if { !$run_ld && $dumpprogram == "objdump" \
1202 && $opts(section_subst) != "no" \
1203 && ![string match "*-b binary*" $progopts1] } {
1204 set sect_names [get_standard_section_names]
1205 if { $sect_names != ""} {
1206 regsub -- "\\.text" $progopts1 "[lindex $sect_names 0]" progopts1
1207 regsub -- "\\.data" $progopts1 "[lindex $sect_names 1]" progopts1
1208 regsub -- "\\.bss" $progopts1 "[lindex $sect_names 2]" progopts1
1209 }
1210 }
1211
1212 if { $progopts1 == "" } { set $progopts1 "-r" }
1213 verbose "running $binary $progopts $progopts1" 3
1214
1215 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
1216
1217 # Ensure consistent sorting of symbols
1218 if {[info exists env(LC_ALL)]} {
1219 set old_lc_all $env(LC_ALL)
1220 }
1221 set env(LC_ALL) "C"
1222 send_log "$cmd\n"
1223 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>dump.tmp"]] "" "/dev/null"]
1224 set cmdret [lindex $cmdret 0]
1225 remote_upload host "dump.tmp"
1226 set comp_output [prune_warnings [file_contents "dump.tmp"]]
1227 remote_file host delete "dump.tmp"
1228 remote_file build delete "dump.tmp"
1229 if {[info exists old_lc_all]} {
1230 set env(LC_ALL) $old_lc_all
1231 } else {
1232 unset env(LC_ALL)
1233 }
1234 if { $cmdret != 0 || $comp_output != "" } {
1235 send_log "exited abnormally with $cmdret, output:$comp_output\n"
1236 fail $testname
1237 return
1238 }
1239
1240 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
1241
1242 # Create the substition list for objdump output.
1243 set regexp_subst ""
1244 if { $sect_names != "" } {
1245 set regexp_subst [list "\\\\?\\.text" [lindex $sect_names 0] \
1246 "\\\\?\\.data" [lindex $sect_names 1] \
1247 "\\\\?\\.bss" [lindex $sect_names 2] ]
1248 }
1249
1250 if { [regexp_diff $dumpfile "${dfile}" $regexp_subst] } then {
1251 fail $testname
1252 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
1253 return
1254 }
1255
1256 pass $testname
1257 }
1258
1259 proc slurp_options { file } {
1260 # If options_regsub(foo) is set to {a b}, then the contents of a
1261 # "#foo:" line will have regsub -all applied to replace a with b.
1262 global options_regsub
1263
1264 if [catch { set f [open $file r] } x] {
1265 #perror "couldn't open `$file': $x"
1266 perror "$x"
1267 return -1
1268 }
1269 set opt_array {}
1270 # whitespace expression
1271 set ws {[ ]*}
1272 set nws {[^ ]*}
1273 # whitespace is ignored anywhere except within the options list;
1274 # option names are alphanumeric plus underscore.
1275 set pat "^#${ws}(\[a-zA-Z0-9_\]*)$ws:${ws}(.*)$ws\$"
1276 while { [gets $f line] != -1 } {
1277 set line [string trim $line]
1278 # Whitespace here is space-tab.
1279 if [regexp $pat $line xxx opt_name opt_val] {
1280 # match!
1281 if [info exists options_regsub($opt_name)] {
1282 set subst $options_regsub($opt_name)
1283 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
1284 opt_val
1285 }
1286 lappend opt_array [list $opt_name $opt_val]
1287 } elseif {![regexp "^#" $line ]} {
1288 break
1289 }
1290 }
1291 close $f
1292 return $opt_array
1293 }
1294
1295 proc file_contents { filename } {
1296 set file [open $filename r]
1297 set contents [read $file]
1298 close $file
1299 return $contents
1300 }
1301
1302 proc set_file_contents { filename contents } {
1303 set file [open $filename w]
1304 puts $file "$contents"
1305 close $file
1306 }
1307
1308 # Look for big-endian or little-endian switches in the multlib
1309 # options and translate these into a -EB or -EL switch. Note
1310 # we cannot rely upon proc process_multilib_options to do this
1311 # for us because for some targets the compiler does not support
1312 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
1313 # the site.exp file will include the switch "-mbig-endian"
1314 # (rather than "big-endian") which is not detected by proc
1315 # process_multilib_options.
1316 #
1317 proc big_or_little_endian {} {
1318
1319 if [board_info [target_info name] exists multilib_flags] {
1320 set tmp_flags " [board_info [target_info name] multilib_flags]"
1321
1322 foreach x $tmp_flags {
1323 case $x in {
1324 {*big*endian eb EB -eb -EB -mb -meb} {
1325 set flags " -EB"
1326 return $flags
1327 }
1328 {*little*endian el EL -el -EL -ml -mel} {
1329 set flags " -EL"
1330 return $flags
1331 }
1332 }
1333 }
1334 }
1335
1336 set flags ""
1337 return $flags
1338 }
1339
1340 # Internal procedure: return the names of the standard sections
1341 #
1342 proc get_standard_section_names {} {
1343 if [istarget "rx-*-*"] {
1344 return { "P" "D_1" "B_1" }
1345 }
1346 if { [istarget "alpha*-*-*vms*"] || [is_som_format] } {
1347 return { {\$CODE\$} {\$DATA\$} {\$BSS\$} }
1348 }
1349 return
1350 }