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