1 # Support routines for LD testsuite.
2 # Copyright (C) 1994-2018 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 proc load_common_lib { name } {
23 load_file $srcdir/../../binutils/testsuite/lib/$name
26 load_common_lib binutils-common.exp
28 # Returns 1 if the gcc for the target is at least version MAJOR.MINOR
29 # Returns 0 otherwise.
31 proc at_least_gcc_version { major minor } {
34 if {![info exists CC]} {
40 set state [remote_exec host $CC --version]
41 if { [lindex $state 0] != 0 } {
44 set tmp "[lindex $state 1]\n"
45 # Look for (eg) 4.6.1 in the version output.
46 set ver_re "\[^\\.0-9\]+(\[1-9\]\[0-9\]*)\\.(\[0-9\]+)(?:\\.\[0-9\]+)?"
47 regexp $ver_re $tmp fred maj min
48 verbose "gcc version: $tmp"
49 if { ![info exists maj] || ![info exists min] } then {
50 perror "can't decipher gcc version number, fix the framework!"
53 verbose "major gcc version is $maj, want at least $major"
54 if { $maj == $major } then {
55 verbose "minor gcc version is $min, want at least $minor"
56 return [expr $min >= $minor]
58 return [expr $maj > $major]
62 # Extract and print the version number of ld.
64 proc default_ld_version { ld } {
67 if { ![is_remote host] && [which $ld] == 0 } then {
68 perror "$ld does not exist"
72 remote_exec host "$ld --version" "" "/dev/null" "ld.version"
73 remote_upload host "ld.version"
74 set tmp [prune_warnings [file_contents "ld.version"]]
75 remote_file build delete "ld.version"
76 remote_file host delete "ld.version"
78 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
79 if [info exists number] then {
80 clone_output "$ld $number\n"
84 proc run_host_cmd { prog command } {
88 global gcc_ld_B_opt_tested
91 if { ![is_remote host] && [which "$prog"] == 0 } then {
92 perror "$prog does not exist"
96 # If we are compiling with gcc, we want to add gcc_B_opt and
97 # ld_L_opt to flags. However, if $prog already has -B options,
98 # which might be the case when running gcc out of a build
99 # directory, we want our -B options to come first.
101 set gccparm [string first " " $gccexe]
103 if { $gccparm > 0 } then {
104 set gccflags [string range $gccexe $gccparm end]
105 set gccexe [string range $gccexe 0 $gccparm]
108 set gccexe [string replace $gccexe 0 [string last "/" $gccexe] ""]
109 if {[string match "*cc*" $gccexe] || [string match "*++*" $gccexe]} then {
110 set gccflags "$gcc_B_opt $gccflags $ld_L_opt"
111 if {![info exists gcc_ld_B_opt_tested]} {
112 set gcc_ld_B_opt_tested 1
113 set ld_version_message [run_host_cmd "$ld" "--version"]
114 set gcc_ld_version_message [run_host_cmd "$prog" "$gccflags -Wl,--version"]
115 if {[string first $ld_version_message $gcc_ld_version_message] < 0} {
116 perror "************************************************************************"
117 perror "Your compiler driver ignores -B when choosing ld."
118 perror "You will not be testing the new ld in many of the following tests."
119 set gcc_ld_version [run_host_cmd "$prog" "$gccflags --print-prog-name=ld"]
120 if {![string match "" $gcc_ld_version] && ![string match "ld" $gcc_ld_version]} {
122 perror "It seems you will be testing $gcc_ld_version instead."
124 perror "************************************************************************"
129 verbose -log "$prog $gccflags $command"
130 set status [remote_exec host [concat sh -c [list "$prog $gccflags $command 2>&1"]] "" "/dev/null" "ld.tmp"]
131 remote_upload host "ld.tmp"
132 set link_output [file_contents "ld.tmp"]
133 regsub "\n$" $link_output "" link_output
134 if { [lindex $status 0] != 0 && [string match "" $link_output] } then {
135 append link_output "child process exited abnormally"
137 remote_file build delete ld.tmp
138 remote_file host delete ld.tmp
140 if [string match "" $link_output] then {
144 verbose -log "$link_output"
145 return "$link_output"
148 proc run_host_cmd_yesno { prog command } {
150 global errcnt warncnt
152 set exec_output [prune_warnings [run_host_cmd "$prog" "$command"]]
153 # Ignore error and warning.
156 if [string match "" $exec_output] then {
162 # Link an object using relocation.
164 proc default_ld_relocate { ld target objects } {
167 remote_file host delete $target
168 return [run_host_cmd_yesno "$ld" "$HOSTING_EMU -o $target -r $objects"]
171 # Check to see if ld is being invoked with a non-endian output format
173 proc is_endian_output_format { object_flags } {
175 if {[string match "*-oformat binary*" $object_flags] || \
176 [string match "*-oformat ieee*" $object_flags] || \
177 [string match "*-oformat ihex*" $object_flags] || \
178 [string match "*-oformat netbsd-core*" $object_flags] || \
179 [string match "*-oformat srec*" $object_flags] || \
180 [string match "*-oformat tekhex*" $object_flags] || \
181 [string match "*-oformat trad-core*" $object_flags] } then {
188 # Look for big-endian or little-endian switches in the multlib
189 # options and translate these into a -EB or -EL switch. Note
190 # we cannot rely upon proc process_multilib_options to do this
191 # for us because for some targets the compiler does not support
192 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
193 # the site.exp file will include the switch "-mbig-endian"
194 # (rather than "big-endian") which is not detected by proc
195 # process_multilib_options.
197 proc big_or_little_endian {} {
199 if [board_info [target_info name] exists multilib_flags] {
200 set tmp_flags " [board_info [target_info name] multilib_flags]"
202 foreach x $tmp_flags {
204 {*big*endian eb EB -eb -EB -mb -meb} {
208 {*little*endian el EL -el -EL -ml -mel} {
220 # Link a program using ld
222 proc default_ld_link { ld target objects } {
227 if [is_endian_output_format $objects] then {
228 set flags [big_or_little_endian]
231 remote_file host delete $target
232 set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
233 set exec_output [prune_warnings $exec_output]
235 # We don't care if we get a warning about a non-existent start
236 # symbol, since the default linker script might use ENTRY.
237 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
239 return [string match "" $exec_output]
242 # Compile an object using cc.
244 proc default_ld_compile { cc source object } {
253 if {[llength $cc_prog] > 1} then {
254 set cc_prog [lindex $cc_prog 0]
256 if {![is_remote host] && [which $cc_prog] == 0} then {
257 perror "$cc_prog does not exist"
261 remote_file build delete "$object"
262 remote_file host delete "$object"
264 set flags "$gcc_B_opt -I$srcdir/$subdir"
266 # If we are compiling with gcc, we want to add gcc_B_opt to flags.
267 # However, if $prog already has -B options, which might be the
268 # case when running gcc out of a build directory, we want our -B
269 # options to come first.
271 set ccparm [string first " " $cc]
273 if { $ccparm > 0 } then {
274 set ccflags [string range $cc $ccparm end]
275 set ccexe [string range $cc 0 $ccparm]
279 set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
280 if {[string match "*++*" $ccexe]} {
281 append flags " $CXXFLAGS"
283 append flags " $CFLAGS"
286 if [board_info [target_info name] exists cflags] {
287 append flags " [board_info [target_info name] cflags]"
290 if [board_info [target_info name] exists multilib_flags] {
291 append flags " [board_info [target_info name] multilib_flags]"
294 set cmd "$cc $flags $ccflags -c $source -o $object"
297 set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
298 remote_upload host "ld.tmp"
299 set exec_output [file_contents "ld.tmp"]
300 remote_file build delete "ld.tmp"
301 remote_file host delete "ld.tmp"
302 set exec_output [prune_warnings $exec_output]
303 # Versions of gcc up to and including pre-release gcc-7, at least on
304 # some targets, generate .section directives with incorrect type.
305 # Ignore warnings from the assembler about this.
306 regsub -all "(^|\n)\[^\n\]*: ignoring incorrect section type \[^\n\]*" $exec_output "" exec_output
307 regsub -all "^\[^\n\]*: Assembler messages:\n" $exec_output "" exec_output
308 if [string match "" $exec_output] then {
309 if {![file exists $object]} then {
310 regexp ".*/(\[^/\]*)$" $source all dobj
311 regsub "\\.c" $dobj ".o" realobj
312 verbose "looking for $realobj"
313 if {[remote_file host exists $realobj]} then {
314 verbose -log "mv $realobj $object"
315 remote_upload "$realobj" "$object"
317 perror "$object not found after compilation"
323 verbose -log "$exec_output"
324 perror "$source: compilation failed"
331 proc default_ld_assemble { as in_flags source object } {
337 if ![info exists ASFLAGS] { set ASFLAGS "" }
339 set flags "[big_or_little_endian] -I$srcdir/$subdir"
340 set exec_output [run_host_cmd "$as" "$flags $in_flags $ASFLAGS -o $object $source"]
341 set exec_output [prune_warnings $exec_output]
342 if [string match "" $exec_output] then {
345 perror "$source: assembly failed"
350 # Run nm on a file, putting the result in the array nm_output.
352 proc default_ld_nm { nm nmflags object } {
357 if {[info exists nm_output]} {
361 if ![info exists NMFLAGS] { set NMFLAGS "" }
363 # Ensure consistent sorting of symbols
364 if {[info exists env(LC_ALL)]} {
365 set old_lc_all $env(LC_ALL)
369 verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
371 set status [remote_exec host [concat sh -c [list "$nm $NMFLAGS $nmflags $object 2>ld.stderr"]] "" "/dev/null" "tmpdir/nm.out"]
372 if {[info exists old_lc_all]} {
373 set env(LC_ALL) $old_lc_all
377 remote_upload host "ld.stderr"
378 remote_upload host "tmpdir/nm.out" "tmpdir/nm.out"
379 set exec_output [prune_warnings [file_contents "ld.stderr"]]
380 remote_file host delete "ld.stderr"
381 remote_file build delete "ld.stderr"
382 if [string match "" $exec_output] then {
383 set file [open tmpdir/nm.out r]
384 while { [gets $file line] != -1 } {
386 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
387 set name [string trimleft $name "_"]
388 verbose "Setting nm_output($name) to 0x$value" 2
389 set nm_output($name) 0x$value
395 verbose -log "$exec_output"
396 perror "$object: nm failed"
401 # Define various symbols needed when not linking against all
403 proc ld_link_defsyms {} {
405 set flags "--defsym __stack_chk_fail=0"
407 # ARM targets call __gccmain
408 if {[istarget arm*-*-*]} {
409 append flags " --defsym __gccmain=0"
412 # Windows targets need __main, some prefixed with underscore.
413 if {[istarget *-*-cygwin* ] || [istarget *-*-mingw*]} {
414 append flags " --defsym __main=0 --defsym ___main=0"
417 # PowerPC EABI code calls __eabi.
418 if {[istarget powerpc*-*-eabi*] || [istarget powerpc*-*-rtems*]} {
419 append flags " --defsym __eabi=0"
422 # mn10200 code calls __truncsipsi2_d0_d2.
423 if {[istarget mn10200*-*-*]} then {
424 append flags " --defsym __truncsipsi2_d0_d2=0"
427 # m6811/m6812 code has references to soft registers.
428 if {[istarget m6811-*-*] || [istarget m6812-*-*] || [istarget m68hc1*-*-*]} {
429 append flags " --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0"
430 append flags " --defsym _.d3=0 --defsym _.d4=0"
431 append flags " --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0"
434 # Some OpenBSD targets have ProPolice and reference __guard and
435 # __stack_smash_handler.
436 if [istarget *-*-openbsd*] {
437 append flags " --defsym __guard=0"
438 append flags " --defsym __stack_smash_handler=0"
444 # run_dump_test FILE (optional:) EXTRA_OPTIONS
445 # Copied from gas testsuite, tweaked and further extended.
447 # Assemble a .s file, then run some utility on it and check the output.
449 # There should be an assembly language file named FILE.s in the test
450 # suite directory, and a pattern file called FILE.d. `run_dump_test'
451 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
452 # `nm' on the .o file to produce textual output, and then analyze that
453 # with regexps. The FILE.d file specifies what program to run, and
454 # what to expect in its output.
456 # The FILE.d file begins with zero or more option lines, which specify
457 # flags to pass to the assembler, the program to run to dump the
458 # assembler's output, and the options it wants. The option lines have
463 # OPTION is the name of some option, like "name" or "objdump", and
464 # VALUE is OPTION's value. The valid options are described below.
465 # Whitespace is ignored everywhere, except within VALUE. The option
466 # list ends with the first line that doesn't match the above syntax
467 # (hmm, not great for error detection).
469 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
470 # two-element lists. The first element of each is an option name, and
471 # the second additional arguments to be added on to the end of the
472 # option list as given in FILE.d. (If omitted, no additional options
475 # The interesting options are:
478 # The name of this test, passed to DejaGNU's `pass' and `fail'
479 # commands. If omitted, this defaults to FILE, the root of the
480 # .s and .d files' names.
483 # When assembling, pass FLAGS to the assembler.
484 # If assembling several files, you can pass different assembler
485 # options in the "source" directives. See below.
488 # Link assembled files using FLAGS, in the order of the "source"
489 # directives, when using multiple files.
491 # ld_after_inputfiles: FLAGS
492 # Similar to "ld", but put after all input files.
494 # objcopy_objects: FLAGS
495 # Run objcopy with the specified flags after assembling any source
496 # that has the special marker RUN_OBJCOPY in the source specific
499 # objcopy_linked_file: FLAGS
500 # Run objcopy on the linked file with the specified flags.
501 # This lets you transform the linked file using objcopy, before the
502 # result is analyzed by an analyzer program specified below (which
503 # may in turn *also* be objcopy).
505 # DUMPPROG: PROGRAM-NAME
506 # The name of the program to run to analyze the .o file produced
507 # by the assembler or the linker output. This can be omitted;
508 # run_dump_test will guess which program to run by seeing which of
509 # the flags options below is present.
514 # Use the specified program to analyze the assembler or linker
515 # output file, and pass it FLAGS, in addition to the output name.
516 # Note that they are run with LC_ALL=C in the environment to give
517 # consistent sorting of symbols.
519 # source: SOURCE [FLAGS]
520 # Assemble the file SOURCE.s using the flags in the "as" directive
521 # and the (optional) FLAGS. If omitted, the source defaults to
523 # This is useful if several .d files want to share a .s file.
524 # More than one "source" directive can be given, which is useful
525 # when testing linking.
528 # Match against DUMP.d. If omitted, this defaults to FILE.d. This
529 # is useful if several .d files differ by options only. Options are
530 # always read from FILE.d.
533 # The test is expected to fail on TARGET. This may occur more than
537 # Only run the test for TARGET.
538 # You may provide target name "cfi" for any target supporting the
539 # CFI statements. You may provide target name "shared" for any
540 # target supporting shared libraries. Otherwise TARGET is called
541 # as a TCL procedure if surrounded by square brackets, or passed
542 # to "istarget" if not.
543 # This may occur more than once; the target being tested must match
544 # at least one. Otherwise the test will be marked unsupported.
547 # Only run the test for TARGET.
548 # The syntax for TARGET is as with 'target'.
549 # This may occur more than once; the target being tested must match
550 # all of them. Otherwise the test will be marked unsupported.
553 # Do not run the test for TARGET.
554 # The syntax for TARGET is as with 'target'.
555 # This may occur more than once; the target being tested must not
556 # match any of them. Otherwise the test will be marked unsupported.
561 # These are exactly the same as "notarget", "alltargets" and
562 # "target" respectively, except that they do nothing at all if the
563 # check fails. They should only be used in groups, to construct a
564 # single test which is run on all targets but with variant options
565 # or expected output on some targets. (For example, see
566 # gas/arm/inst.d and gas/arm/wince_inst.d.)
569 # An error with message matching REGEX must be emitted for the test
570 # to pass. The DUMPPROG, readelf, objdump, and nm options have
571 # no meaning and need not be supplied if this is present. Multiple
572 # "error" directives append to the expected linker error message.
575 # Means the same as 'error', except the regular expression lines
576 # are contains in FILE.
579 # Expect a linker warning matching REGEX. It is an error to issue
580 # both "error" and "warning". Multiple "warning" directives
581 # append to the expected linker warning message.
583 # warning_output: FILE
584 # Means the same as 'warning', except the regular expression
585 # lines are contains in FILE.
588 # Adding this option will cause the linker to generate a linker
589 # map file, using the -Map=MAPFILE command line option. If
590 # there is no -Map=MAPFILE in the 'ld: FLAGS' then one will be
591 # added to the linker command line. The contents of the
592 # generated MAPFILE are then compared against the regexp lines
593 # in FILE using `regexp_diff' (see below for details).
595 # Each option may occur at most once unless otherwise mentioned.
597 # After the option lines come regexp lines. `run_dump_test' calls
598 # `regexp_diff' to compare the output of the dumping tool against the
599 # regexps in FILE.d. `regexp_diff' is defined in binutils-common.exp;
600 # see further comments there.
602 proc run_dump_test { name {extra_options {}} } {
604 global OBJDUMP NM AS OBJCOPY READELF LD
605 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
606 global host_triplet runtests
608 global ld_elf_shared_opt
610 if { [is_elf_format] && [check_shared_lib_support] } {
611 set ld_extra_opt "$ld_elf_shared_opt"
616 if [string match "*/*" $name] {
618 set name [file tail $name]
620 set file "$srcdir/$subdir/$name"
623 if ![runtest_file_p $runtests $name] then {
627 set opt_array [slurp_options "${file}.d"]
628 if { $opt_array == -1 } {
629 perror "error reading options from $file.d"
630 unresolved $subdir/$name
633 set dumpfile tmpdir/dump.out
639 set opts(ld_after_inputfiles) {}
642 set opts(alltargets) {}
643 set opts(notarget) {}
651 set opts(DUMPPROG) {}
656 set opts(error_output) {}
657 set opts(warning_output) {}
658 set opts(objcopy_linked_file) {}
659 set opts(objcopy_objects) {}
662 foreach i $opt_array {
663 set opt_name [lindex $i 0]
664 set opt_val [lindex $i 1]
665 if ![info exists opts($opt_name)] {
666 perror "unknown option $opt_name in file $file.d"
667 unresolved $subdir/$name
671 switch -- $opt_name {
682 # Move any source-specific as-flags to a separate list to
683 # simplify processing.
684 if { [llength $opt_val] > 1 } {
685 lappend asflags [lrange $opt_val 1 end]
686 set opt_val [lindex $opt_val 0]
691 # Create the object file name based on nothing but the source
694 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]].o]
695 # But, sometimes, we have the exact same source filename in
696 # different directories (foo/src.s bar/src.s) which would lead
697 # us to try and create two src.o files. We detect this
698 # conflict here, and instead create src.o and src1.o.
700 while { [lsearch $objfile_names $new_objfile] != -1 } {
703 [concat tmpdir/[file rootname [file tail [lindex $opt_val 0]]]${j}.o]
705 lappend objfile_names $new_objfile
708 if [string length $opts($opt_name)] {
709 perror "option $opt_name multiply set in $file.d"
710 unresolved $subdir/$name
714 # A single "# ld:" with no options should do the right thing.
715 if { $opt_name == "ld" } {
718 # Likewise objcopy_linked_file.
719 if { $opt_name == "objcopy_linked_file" } {
724 if { $opt_name == "as" || $opt_name == "ld" } {
725 set opt_val [subst $opt_val]
728 # Append differently whether it's a message (without space) or
729 # an option or list (with space).
730 switch -- $opt_name {
733 append opts($opt_name) $opt_val
736 set opts($opt_name) [concat $opts($opt_name) $opt_val]
741 foreach i $extra_options {
742 set opt_name [lindex $i 0]
743 set opt_val [lindex $i 1]
744 if ![info exists opts($opt_name)] {
745 perror "unknown option $opt_name given in extra_opts"
746 unresolved $subdir/$name
749 # Add extra option to end of existing option, adding space
751 if { ![regexp "warning|error" $opt_name]
752 && [string length $opts($opt_name)] } {
753 append opts($opt_name) " "
755 append opts($opt_name) $opt_val
758 foreach opt { as ld } {
759 regsub {\[big_or_little_endian\]} $opts($opt) \
760 [big_or_little_endian] opts($opt)
763 if { $opts(name) == "" } {
764 set testname "$subdir/$name"
766 set testname $opts(name)
769 # Decide early whether we should run the test for this target.
770 if { [llength $opts(noskip)] > 0 } {
772 foreach targ $opts(noskip) {
773 if [match_target $targ] {
778 if { $targmatch == 0 } {
782 foreach targ $opts(anyskip) {
783 if ![match_target $targ] {
787 foreach targ $opts(skip) {
788 if [match_target $targ] {
792 if { [llength $opts(target)] > 0 } {
794 foreach targ $opts(target) {
795 if [match_target $targ] {
800 if { $targmatch == 0 } {
801 unsupported $testname
805 foreach targ $opts(alltargets) {
806 if ![match_target $targ] {
807 unsupported $testname
811 foreach targ $opts(notarget) {
812 if [match_target $targ] {
813 unsupported $testname
819 # It's meaningless to require an output-testing method when we
821 if { $opts(error) == "" && $opts(error_output) == "" } {
822 if { $opts(DUMPPROG) != "" } {
823 switch -- $opts(DUMPPROG) {
824 objdump { set dumpprogram objdump }
825 nm { set dumpprogram nm }
826 readelf { set dumpprogram readelf }
828 perror "unrecognized DUMPPROG option $opts(DUMPPROG) in $file.d"
834 # Guess which program to run, by seeing which option was specified.
835 foreach p {objdump nm readelf} {
836 if {$opts($p) != ""} {
837 if {$dumpprogram != ""} {
838 perror "ambiguous dump program in $file.d"
847 if { $dumpprogram == "" \
848 && $opts(map) == "" \
849 && $opts(warning) == "" \
850 && $opts(warning_output) == "" \
851 && $opts(error) == "" \
852 && $opts(error_output) == "" } {
853 perror "dump program unspecified in $file.d"
859 if { $opts(source) == "" } {
860 set sourcefiles [list ${file}.s]
861 set asflags [list ""]
862 set objfile_names [list tmpdir/[file tail ${file}].o]
865 foreach sf $opts(source) {
866 if { [string match "/*" $sf] } {
867 lappend sourcefiles "$sf"
869 lappend sourcefiles "$srcdir/$subdir/$sf"
874 if { $opts(dump) == "" } {
877 set dfile $srcdir/$subdir/$opts(dump)
880 # Time to setup xfailures.
881 foreach targ $opts(xfail) {
885 # Assemble each file.
887 for { set i 0 } { $i < [llength $sourcefiles] } { incr i } {
888 set sourcefile [lindex $sourcefiles $i]
889 set sourceasflags [lindex $asflags $i]
890 set run_objcopy_objects 0
892 if { [string match "*RUN_OBJCOPY*" $sourceasflags] } {
893 set run_objcopy_objects 1
895 regsub "RUN_OBJCOPY" $sourceasflags "" sourceasflags
897 set objfile [lindex $objfile_names $i]
898 catch "exec rm -f $objfile" exec_output
899 lappend objfiles $objfile
900 set cmd "$AS $ASFLAGS $opts(as) $sourceasflags -o $objfile $sourcefile"
903 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
904 remote_upload host "ld.tmp"
905 set comp_output [prune_warnings [file_contents "ld.tmp"]]
906 remote_file host delete "ld.tmp"
907 remote_file build delete "ld.tmp"
909 if { [lindex $cmdret 0] != 0 || ![string match "" $comp_output] } then {
910 send_log -- "$comp_output\n"
911 verbose "$comp_output" 3
913 set exitstat "succeeded"
914 if { $cmdret != 0 } { set exitstat "failed" }
915 verbose -log "$exitstat with: <$comp_output>"
920 if { $run_objcopy_objects } {
921 set cmd "$OBJCOPY $opts(objcopy_objects) $objfile"
924 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] \
925 "" "/dev/null" "objcopy.tmp"]
926 remote_upload host "objcopy.tmp"
927 set comp_output [prune_warnings [file_contents "objcopy.tmp"]]
928 remote_file host delete "objcopy.tmp"
929 remote_file build delete "objcopy.tmp"
931 if { [lindex $cmdret 0] != 0 \
932 || ![string match "" $comp_output] } {
933 send_log -- "$comp_output\n"
934 verbose "$comp_output" 3
936 set exitstat "succeeded"
937 if { $cmdret != 0 } { set exitstat "failed" }
938 verbose -log "$exitstat with: <$comp_output>"
945 if { (($opts(warning) != "") && ($opts(error) != "")) \
946 || (($opts(warning) != "") && ($opts(error_output) != "")) \
947 || (($opts(warning) != "") && ($opts(warning_output) != "")) \
948 || (($opts(error) != "") && ($opts(warning_output) != "")) \
949 || (($opts(error) != "") && ($opts(error_output) != "")) \
950 || (($opts(warning_output) != "") && ($opts(error_output) != "")) } {
951 perror "$testname: bad mix of warning, error, warning_output, and error_output test-directives"
956 set check_ld(source) ""
957 set check_ld(terminal) 0
958 if { $opts(error) != "" \
959 || $opts(warning) != "" \
960 || $opts(error_output) != "" \
961 || $opts(warning_output) != "" } {
963 if { $opts(error) != "" || $opts(error_output) != "" } {
964 set check_ld(terminal) 1
966 set check_ld(terminal) 0
969 if { $opts(error) != "" || $opts(warning) != "" } {
970 set check_ld(source) "regex"
971 if { $opts(error) != "" } {
972 set check_ld(regex) $opts(error)
974 set check_ld(regex) $opts(warning)
977 set check_ld(source) "file"
978 if { $opts(error_output) != "" } {
979 set check_ld(file) $opts(error_output)
981 set check_ld(file) $opts(warning_output)
986 # Perhaps link the file(s).
988 set objfile "tmpdir/dump"
989 catch "exec rm -f $objfile" exec_output
991 # Add -L$srcdir/$subdir so that the linker command can use
992 # linker scripts in the source directory.
993 set cmd "$LD $ld_extra_opt $LDFLAGS -L$srcdir/$subdir \
994 $opts(ld) -o $objfile $objfiles $opts(ld_after_inputfiles)"
996 # If needed then check for, or add a -Map option.
998 if { $opts(map) != "" } then {
999 if { [regexp -- "-Map=(\[^ \]+)" $cmd all mapfile] } then {
1000 # Found existing mapfile option
1001 verbose -log "Existing mapfile '$mapfile' found"
1003 # No mapfile option.
1004 set mapfile "tmpdir/dump.map"
1005 verbose -log "Adding mapfile '$mapfile'"
1006 set cmd "$cmd -Map=$mapfile"
1011 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
1012 remote_upload host "ld.tmp"
1013 set comp_output [file_contents "ld.tmp"]
1014 remote_file host delete "ld.tmp"
1015 remote_file build delete "ld.tmp"
1016 set cmdret [lindex $cmdret 0]
1018 if { $cmdret == 0 && $run_objcopy } {
1020 set objfile "tmpdir/dump1"
1021 remote_file host delete $objfile
1023 # Note that we don't use OBJCOPYFLAGS here; any flags must be
1024 # explicitly specified.
1025 set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
1028 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
1029 remote_upload host "ld.tmp"
1030 append comp_output [file_contents "ld.tmp"]
1031 remote_file host delete "ld.tmp"
1032 remote_file build delete "ld.tmp"
1033 set cmdret [lindex $cmdret 0]
1036 regsub "\n$" $comp_output "" comp_output
1037 if { $cmdret != 0 || $comp_output != "" || $check_ld(source) != "" } then {
1038 set exitstat "succeeded"
1039 if { $cmdret != 0 } { set exitstat "failed" }
1041 if { $check_ld(source) == "regex" } {
1042 verbose -log "$exitstat with: <$comp_output>, expected: <$check_ld(regex)>"
1043 } elseif { $check_ld(source) == "file" } {
1044 verbose -log "$exitstat with: <$comp_output>, expected in file $check_ld(file)"
1045 set_file_contents "tmpdir/ld.messages" "$comp_output"
1047 verbose -log "$exitstat with: <$comp_output>, no expected output"
1049 send_log -- "$comp_output\n"
1050 verbose "$comp_output" 3
1052 if { (($check_ld(source) == "") == ($comp_output == "")) \
1053 && (($cmdret == 0) == ($check_ld(terminal) == 0)) \
1054 && ((($check_ld(source) == "regex") \
1055 && ($check_ld(regex) == "") == ($comp_output == "") \
1056 && [regexp -- $check_ld(regex) $comp_output]) \
1057 || (($check_ld(source) == "file") \
1058 && (![regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"]))) } {
1059 # We have the expected output from ld.
1060 if { $check_ld(terminal) || $dumpprogram == "" } {
1070 if { $opts(map) != "" } then {
1071 # Check the map file matches.
1072 set map_pattern_file $srcdir/$subdir/$opts(map)
1073 verbose -log "Compare '$mapfile' against '$map_pattern_file'"
1074 if { [regexp_diff $mapfile $map_pattern_file] } then {
1075 fail "$testname (map file check)"
1077 pass "$testname (map file check)"
1080 if { $dumpprogram == "" } then {
1085 set objfile [lindex $objfiles 0]
1088 # We must not have expected failure if we get here.
1089 if { $opts(error) != "" } {
1094 set progopts1 $opts($dumpprogram)
1095 eval set progopts \$[string toupper $dumpprogram]FLAGS
1096 eval set binary \$[string toupper $dumpprogram]
1098 if { ![is_remote host] && [which $binary] == 0 } {
1103 if { $progopts1 == "" } { set $progopts1 "-r" }
1104 verbose "running $binary $progopts $progopts1" 3
1105 set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
1107 # Ensure consistent sorting of symbols
1108 if {[info exists env(LC_ALL)]} {
1109 set old_lc_all $env(LC_ALL)
1113 set cmdret [remote_exec host [concat sh -c [list "$cmd 2>ld.tmp"]] "" "/dev/null"]
1114 set cmdret [lindex $cmdret 0]
1115 remote_upload host "ld.tmp"
1116 set comp_output [prune_warnings [file_contents "ld.tmp"]]
1117 remote_file host delete "ld.tmp"
1118 remote_file build delete "ld.tmp"
1119 if {[info exists old_lc_all]} {
1120 set env(LC_ALL) $old_lc_all
1124 if { $cmdret != 0 || $comp_output != "" } {
1125 send_log "exited abnormally with $cmdret, output:$comp_output\n"
1130 if { $verbose > 2 } then { verbose "output is [file_contents $dumpfile]" 3 }
1131 if { [regexp_diff $dumpfile "${dfile}"] } then {
1133 if { $verbose == 2 } then { verbose "output is [file_contents $dumpfile]" 2 }
1140 proc slurp_options { file } {
1141 # If options_regsub(foo) is set to {a b}, then the contents of a
1142 # "#foo:" line will have regsub -all applied to replace a with b.
1143 global options_regsub
1145 if [catch { set f [open $file r] } x] {
1146 #perror "couldn't open `$file': $x"
1151 # whitespace expression
1154 # whitespace is ignored anywhere except within the options list;
1155 # option names are alphabetic plus underscore only.
1156 set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
1157 while { [gets $f line] != -1 } {
1158 set line [string trim $line]
1159 # Whitespace here is space-tab.
1160 if [regexp $pat $line xxx opt_name opt_val] {
1162 if [info exists options_regsub($opt_name)] {
1163 set subst $options_regsub($opt_name)
1164 regsub -all -- [lindex $subst 0] $opt_val [lindex $subst 1] \
1167 lappend opt_array [list $opt_name $opt_val]
1176 proc file_contents { filename } {
1177 set file [open $filename r]
1178 set contents [read $file]
1183 proc set_file_contents { filename contents } {
1184 set file [open $filename w]
1185 puts $file "$contents"
1189 # Create an archive using ar
1191 proc ar_simple_create { ar aropts target objects } {
1192 remote_file host delete $target
1194 set exec_output [run_host_cmd "$ar" "-rc $aropts $target $objects"]
1195 set exec_output [prune_warnings $exec_output]
1197 if [string match "" $exec_output] then {
1198 send_log "$exec_output\n"
1205 # List contains test-items with 3 items followed by 2 lists, one item and
1206 # one optional item:
1208 # 1:ld/ar leading options, placed before object files
1209 # 2:ld/ar trailing options, placed after object files
1210 # 3:assembler options
1211 # 4:filenames of assembler files
1212 # 5:list of actions, options and expected outputs.
1213 # 6:name of output file
1214 # 7:compiler flags (optional)
1216 # Actions: { command command-line-options file-containg-expected-output-regexps }
1218 # objdump: Apply objdump options on result.
1219 # nm: Apply nm options on result.
1220 # readelf: Apply readelf options on result.
1221 # ld: Don't apply anything on result. Compare output during linking with
1222 # the file containing regexps (which is the second arg, not the third).
1223 # Note that this *must* be the first action if it is to be used at all;
1224 # in all other cases, any output from the linker during linking is
1225 # treated as a sign of an error and FAILs the test.
1227 # args is an optional list of target triplets to be xfailed.
1229 proc run_ld_link_tests { ldtests args } {
1243 global ld_elf_shared_opt
1245 if { [is_elf_format] && [check_shared_lib_support] } {
1246 set ld_extra_opt "$ld_elf_shared_opt"
1251 foreach testitem $ldtests {
1252 set testname [lindex $testitem 0]
1254 if ![runtest_file_p $runtests $testname] then {
1258 foreach target $args {
1262 set ld_options [lindex $testitem 1]
1263 set ld_after [lindex $testitem 2]
1264 set as_options [lindex $testitem 3]
1265 set src_files [lindex $testitem 4]
1266 set actions [lindex $testitem 5]
1267 set binfile tmpdir/[lindex $testitem 6]
1268 set cflags [lindex $testitem 7]
1275 # verbose -log "Testname is $testname"
1276 # verbose -log "ld_options is $ld_options"
1277 # verbose -log "ld_after is $ld_after"
1278 # verbose -log "as_options is $as_options"
1279 # verbose -log "src_files is $src_files"
1280 # verbose -log "actions is $actions"
1281 # verbose -log "binfile is $binfile"
1283 # Assemble each file in the test.
1284 foreach src_file $src_files {
1285 set fileroot "[file rootname [file tail $src_file]]"
1286 set objfile "tmpdir/$fileroot.o"
1287 lappend objfiles $objfile
1289 if { [file extension $src_file] == ".c" } {
1290 set as_file "tmpdir/$fileroot.s"
1291 if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
1296 set as_file "$srcdir/$subdir/$src_file"
1298 if ![ld_assemble $as "$as_options $as_file" $objfile] {
1304 # Catch assembler errors.
1305 if { $is_unresolved } {
1306 unresolved $testname
1310 if { $binfile eq "tmpdir/" } {
1312 } elseif { [regexp ".*\\.a$" $binfile] } {
1313 if { ![ar_simple_create $ar $ld_options $binfile "$objfiles $ld_after"] } {
1316 } elseif { ![ld_link $ld $binfile "$ld_extra_opt -L$srcdir/$subdir $ld_options $objfiles $ld_after"] } {
1318 set ld_output "$exec_output"
1322 foreach actionlist $actions {
1323 set action [lindex $actionlist 0]
1324 set progopts [lindex $actionlist 1]
1326 # There are actions where we run regexp_diff on the
1327 # output, and there are other actions (presumably).
1328 # Handling of the former look the same.
1332 { set dump_prog $objdump }
1334 { set dump_prog $nm }
1336 { set dump_prog $READELF }
1338 { set dump_prog "ld" }
1341 perror "Unrecognized action $action"
1347 if { $action == "ld" } {
1348 set regexpfile $progopts
1349 verbose "regexpfile is $srcdir/$subdir/$regexpfile"
1350 set_file_contents "tmpdir/ld.messages" "$ld_output"
1351 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1352 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$regexpfile"] } then {
1353 verbose "output is $ld_output" 2
1358 } elseif { !$maybe_failed && $dump_prog != "" } {
1359 set dumpfile [lindex $actionlist 2]
1360 set binary $dump_prog
1362 # Ensure consistent sorting of symbols
1363 if {[info exists env(LC_ALL)]} {
1364 set old_lc_all $env(LC_ALL)
1367 set cmd "$binary $progopts $binfile"
1368 set status [remote_exec host [concat sh -c [list "$cmd >dump.out 2>ld.stderr"]] "" "/dev/null"]
1370 remote_upload host "ld.stderr"
1371 set comp_output [prune_warnings [file_contents "ld.stderr"]]
1372 remote_file host delete "ld.stderr"
1373 remote_file build delete "ld.stderr"
1375 if {[info exists old_lc_all]} {
1376 set env(LC_ALL) $old_lc_all
1381 if ![string match "" $comp_output] then {
1382 send_log "$comp_output\n"
1387 remote_upload host "dump.out"
1389 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1390 verbose "output is [file_contents "dump.out"]" 2
1392 remote_file build delete "dump.out"
1393 remote_file host delete "dump.out"
1396 remote_file build delete "dump.out"
1397 remote_file host delete "dump.out"
1402 if { $is_unresolved } {
1403 unresolved $testname
1404 } elseif { $maybe_failed || $failed } {
1412 # ldtests contains test-items with 3 items followed by 1 lists, 2 items
1413 # and 3 optional items:
1415 # 1:ld leading options, placed before object files
1416 # 2:assembler options
1417 # 3:filenames of source files
1418 # 4:name of output file
1420 # 6:compiler flags (optional)
1421 # 7:language (optional)
1422 # 8:linker warning (optional)
1423 # 9:ld trailing options, placed after object files (optional)
1424 # args is an optional list of target triplets to be xfailed.
1426 proc run_ld_link_exec_tests { ldtests args } {
1439 global STATIC_LDFLAGS
1441 # When using GCC as the linker driver, we need to specify board cflags when
1442 # linking because cflags may contain linker options. For example when
1443 # linker options are included in GCC spec files then we need the -specs
1445 if [board_info [target_info name] exists cflags] {
1446 set board_cflags " [board_info [target_info name] cflags]"
1451 foreach testitem $ldtests {
1452 foreach target $args {
1455 set testname [lindex $testitem 0]
1456 set ld_options [lindex $testitem 1]
1457 set as_options [lindex $testitem 2]
1458 set src_files [lindex $testitem 3]
1459 set binfile tmpdir/[lindex $testitem 4]
1460 set expfile [lindex $testitem 5]
1461 set cflags [lindex $testitem 6]
1462 set lang [lindex $testitem 7]
1463 set warning [lindex $testitem 8]
1464 set ld_after [lindex $testitem 9]
1468 # verbose -log "Testname is $testname"
1469 # verbose -log "ld_options is $ld_options"
1470 # verbose -log "as_options is $as_options"
1471 # verbose -log "src_files is $src_files"
1472 # verbose -log "binfile is $binfile"
1474 # Assemble each file in the test.
1475 foreach src_file $src_files {
1476 set fileroot "[file rootname [file tail $src_file]]"
1477 set objfile "tmpdir/$fileroot.o"
1478 lappend objfiles $objfile
1480 if { [ string match "c++" $lang ] } {
1481 set cmd "$CXX -c $CXXFLAGS $cflags"
1483 set cmd "$CC -c $CFLAGS $cflags"
1485 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
1490 if { $failed != 0 } {
1491 unresolved $testname
1495 if { [ string match "asm" $lang ] } {
1496 set link_proc ld_link
1498 } elseif { [ string match "c++" $lang ] } {
1499 set link_proc ld_link
1502 set link_proc ld_link
1506 if { $binfile eq "tmpdir/" } {
1511 if { [string match "" $STATIC_LDFLAGS] \
1512 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ld_options $objfiles $ld_after "] } {
1516 if ![$link_proc $link_cmd $binfile "$board_cflags -L$srcdir/$subdir $ld_options $objfiles $ld_after"] {
1521 # Check if exec_output is expected.
1522 if { $warning != "" } then {
1523 verbose -log "returned with: <$exec_output>, expected: <$warning>"
1524 if { [regexp $warning $exec_output] } then {
1531 if { $failed == 0 && [isnative] } {
1532 send_log "Running: $binfile > $binfile.out\n"
1533 verbose "Running: $binfile > $binfile.out"
1534 catch "exec $binfile > $binfile.out" exec_output
1536 if ![string match "" $exec_output] then {
1537 send_log "$exec_output\n"
1538 verbose "$exec_output" 1
1541 send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
1542 verbose "diff $binfile.out $srcdir/$subdir/$expfile"
1543 catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
1544 set exec_output [prune_warnings $exec_output]
1546 if ![string match "" $exec_output] then {
1547 send_log "$exec_output\n"
1548 verbose "$exec_output" 1
1554 if { $failed != 0 } {
1556 } elseif ![isnative] {
1557 unsupported $testname
1565 # List contains test-items with 3 items followed by 2 lists, one item and
1566 # one optional item:
1568 # 1:ld or ar options
1570 # 3:filenames of source files
1571 # 4:action and options.
1572 # 5:name of output file
1573 # 6:language (optional)
1576 # objdump: Apply objdump options on result. Compare with regex (last arg).
1577 # nm: Apply nm options on result. Compare with regex (last arg).
1578 # readelf: Apply readelf options on result. Compare with regex (last arg).
1579 # warning: Check linker output against regex (last arg).
1580 # error: Like 'warning' but checking output in error case.
1581 # warning_output: Check linker output against regex in a file (last arg).
1582 # error_output: Like 'warning_output' but checking output in error case.
1584 proc run_cc_link_tests { ldtests } {
1598 global STATIC_LDFLAGS
1600 if [board_info [target_info name] exists cflags] {
1601 set board_cflags " [board_info [target_info name] cflags]"
1606 foreach testitem $ldtests {
1607 set testname [lindex $testitem 0]
1608 set ldflags [lindex $testitem 1]
1609 set cflags [lindex $testitem 2]
1610 set src_files [lindex $testitem 3]
1611 set actions [lindex $testitem 4]
1612 set binfile tmpdir/[lindex $testitem 5]
1613 set lang [lindex $testitem 6]
1617 set check_ld(terminal) 0
1618 set check_ld(source) ""
1620 #verbose -log "testname is $testname"
1621 #verbose -log "ldflags is $ldflags"
1622 #verbose -log "cflags is $cflags"
1623 #verbose -log "src_files is $src_files"
1624 #verbose -log "actions is $actions"
1625 #verbose -log "binfile is $binfile"
1626 #verbose -log "lang is $lang"
1628 foreach actionlist $actions {
1629 set action [lindex $actionlist 0]
1630 set progopts [lindex $actionlist 1]
1632 # Find actions related to error/warning processing.
1636 set check_ld(source) "regexp"
1637 set check_ld(regexp) $progopts
1638 set check_ld(terminal) 1
1642 set check_ld(source) "regexp"
1643 set check_ld(regexp) $progopts
1647 set check_ld(source) "file"
1648 set check_ld(file) $progopts
1649 set check_ld(terminal) 1
1653 set check_ld(source) "file"
1654 set check_ld(file) $progopts
1659 # Compile each file in the test.
1660 foreach src_file $src_files {
1661 set fileroot "[file rootname [file tail $src_file]]"
1662 set objfile "tmpdir/$fileroot.o"
1663 lappend objfiles $objfile
1665 if { [ string match "c++" $lang ] } {
1666 set cmd "$CXX -c $CXXFLAGS $cflags"
1668 set cmd "$CC -c $CFLAGS $cflags"
1670 if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
1675 if { $failed != 0 } {
1676 unresolved $testname
1680 # Clear error and warning counts.
1683 if { [ string match "c++" $lang ] } {
1689 if { $binfile eq "tmpdir/" } {
1691 } elseif { [regexp ".*\\.a$" $binfile] } {
1692 if { ![ar_simple_create $ar $ldflags $binfile "$objfiles"] } {
1696 if { [string match "" $STATIC_LDFLAGS] \
1697 && [regexp -- ".* \[-\]+static .*" " $board_cflags $ldflags $objfiles "] } {
1701 ld_link $cc_cmd $binfile "$board_cflags -L$srcdir/$subdir $ldflags $objfiles"
1702 set ld_output "$exec_output"
1704 if { $check_ld(source) == "regexp" } then {
1705 # Match output against regexp argument.
1706 verbose -log "returned with: <$ld_output>, expected: <$check_ld(regexp)>"
1707 if { ![regexp $check_ld(regexp) $ld_output] } then {
1710 } elseif { $check_ld(source) == "file" } then {
1711 # Match output against patterns in a file.
1712 set_file_contents "tmpdir/ld.messages" "$ld_output"
1713 verbose "ld.messages has '[file_contents tmpdir/ld.messages]'"
1714 if { [regexp_diff "tmpdir/ld.messages" "$srcdir/$subdir/$check_ld(file)"] } then {
1715 verbose "output is $ld_output" 2
1720 if { $check_ld(source) != "" } then {
1721 if { $ld_output == "" } then {
1722 verbose -log "Linker was expected to give error or warning"
1726 if { $ld_output != "" } then {
1727 verbose -log "Unexpected linker warning or error"
1733 if { $failed == 0 } {
1734 foreach actionlist $actions {
1735 set action [lindex $actionlist 0]
1736 set progopts [lindex $actionlist 1]
1738 # There are actions where we run regexp_diff on the
1739 # output, and there are other actions (presumably).
1740 # Handling of the former look the same.
1744 { set dump_prog $objdump }
1746 { set dump_prog $nm }
1748 { set dump_prog $READELF }
1755 perror "Unrecognized action $action"
1761 if { $dump_prog != "" } {
1762 set dumpfile [lindex $actionlist 2]
1763 set binary $dump_prog
1765 # Ensure consistent sorting of symbols
1766 if {[info exists env(LC_ALL)]} {
1767 set old_lc_all $env(LC_ALL)
1770 set cmd "$binary $progopts $binfile > dump.out"
1772 catch "exec $cmd" comp_output
1773 if {[info exists old_lc_all]} {
1774 set env(LC_ALL) $old_lc_all
1778 set comp_output [prune_warnings $comp_output]
1780 if ![string match "" $comp_output] then {
1781 send_log "$comp_output\n"
1786 if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
1787 verbose "output is [file_contents "dump.out"]" 2
1797 } elseif { $is_unresolved } {
1798 unresolved $testname
1805 # Returns true if --gc-sections is supported on the target.
1807 proc check_gc_sections_available { } {
1808 global gc_sections_available_saved
1811 if {![info exists gc_sections_available_saved]} {
1812 # Some targets don't support gc-sections despite whatever's
1813 # advertised by ld's options.
1814 if { [istarget alpha-*-*]
1815 || [istarget d30v-*-*]
1816 || [istarget dlx-*-*]
1817 || [istarget hppa*64-*-*]
1818 || [istarget ia64-*-*]
1819 || [istarget mep-*-*]
1820 || [istarget mn10200-*-*]
1821 || [istarget pj*-*-*]
1822 || [istarget pru*-*-*]
1823 || [istarget xgate-*-*] } {
1824 set gc_sections_available_saved 0
1828 # elf2flt uses -q (--emit-relocs), which is incompatible with
1830 if { [board_info target exists ldflags]
1831 && [regexp " -elf2flt\[ =\]" " [board_info target ldflags] "] } {
1832 set gc_sections_available_saved 0
1836 # Check if the ld used by gcc supports --gc-sections.
1837 # FIXME: this test is useless since ld --help always says
1838 # --gc-sections is available
1839 set ld_output [remote_exec host $ld "--help"]
1840 if { [ string first "--gc-sections" $ld_output ] >= 0 } {
1841 set gc_sections_available_saved 1
1843 set gc_sections_available_saved 0
1846 return $gc_sections_available_saved
1849 # Returns true if -shared is supported on the target
1851 proc check_shared_lib_support { } {
1852 global shared_available_saved
1855 if {![info exists shared_available_saved]} {
1856 set ld_output [remote_exec host $ld "-shared"]
1857 if { [ string first "not supported" $ld_output ] >= 0 } {
1858 set shared_available_saved 0
1860 set shared_available_saved 1
1863 return $shared_available_saved
1866 # Return true if target uses genelf.em (assuming it is ELF).
1867 proc is_generic_elf { } {
1868 if { [istarget "d30v-*-*"]
1869 || [istarget "dlx-*-*"]
1870 || [istarget "fr30-*-*"]
1871 || ([istarget "frv-*-*"] && ![istarget "frv-*-linux*"])
1872 || [istarget "ft32-*-*"]
1873 || [istarget "iq2000-*-*"]
1874 || [istarget "mn10200-*-*"]
1875 || [istarget "moxie-*-*"]
1876 || [istarget "msp430-*-*"]
1877 || [istarget "mt-*-*"]
1878 || [istarget "pj*-*-*"]
1879 || [istarget "xgate-*-*"] } {
1885 proc is_underscore_target { } {
1886 global is_underscore_target_saved
1887 global target_triplet
1890 if { ![info exists is_underscore_target_saved] } {
1891 set cmd "targ=$target_triplet . $srcdir/../../bfd/config.bfd &&"
1892 append cmd { echo "$targ_underscore"}
1894 set status [catch {exec sh -c $cmd} result]
1895 if { $status == 0 && [string match "yes" $result] } {
1896 set is_underscore_target_saved 1
1898 set is_underscore_target_saved 0
1901 return $is_underscore_target_saved
1904 # Returns true if the target ld supports the plugin API.
1905 proc check_plugin_api_available { } {
1906 global plugin_api_available_saved
1908 if {![info exists plugin_api_available_saved]} {
1909 # Check if the ld used by gcc supports --plugin.
1910 set ld_output [remote_exec host $ld "--help"]
1911 if { [ string first "-plugin PLUGIN" $ld_output ] >= 0 } {
1912 set plugin_api_available_saved 1
1914 set plugin_api_available_saved 0
1917 return $plugin_api_available_saved
1920 # Sets ld_sysroot to the current sysroot (empty if not supported) and
1921 # returns true if the target ld supports sysroot.
1922 proc check_sysroot_available { } {
1923 global ld_sysroot_available_saved ld ld_sysroot
1924 if {![info exists ld_sysroot_available_saved]} {
1925 # Check if ld supports --sysroot *other* than empty.
1926 set ld_sysroot [string trimright [lindex [remote_exec host $ld "--print-sysroot"] 1]]
1927 if { $ld_sysroot == "" } {
1928 set ld_sysroot_available_saved 0
1930 set ld_sysroot_available_saved 1
1933 return $ld_sysroot_available_saved
1936 # Returns 1 if plugin is enabled in gcc. Returns 0 otherwise.
1937 proc check_gcc_plugin_enabled { } {
1940 if {![info exists CC]} {
1946 set state [remote_exec host $CC -v]
1947 if { [lindex $state 0] != 0 } {
1950 for { set i 1 } { $i < [llength $state] } { incr i } {
1951 set v [lindex $state $i]
1952 if { [ string match "*--disable-plugin*" $v ] } {
1953 verbose "plugin is disabled by $v"
1961 # Returns true if the target compiler supports LTO
1962 proc check_lto_available { } {
1963 global lto_available_saved
1966 if {![info exists lto_available_saved]} {
1967 if { ![check_gcc_plugin_enabled] } {
1968 set lto_available_saved 0
1971 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
1972 # -ffat-lto-objects, we always run LTO tests on Linux with
1974 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
1975 set lto_available_saved 1
1978 # Check if gcc supports -flto -fuse-linker-plugin
1980 if [board_info [target_info name] exists cflags] {
1981 append flags " [board_info [target_info name] cflags]"
1983 if [board_info [target_info name] exists ldflags] {
1984 append flags " [board_info [target_info name] ldflags]"
1987 set basename "tmpdir/lto[pid]"
1988 set src ${basename}.c
1989 set output ${basename}.out
1990 set f [open $src "w"]
1991 puts $f "int main() { return 0; }"
1993 if [is_remote host] {
1994 set src [remote_download host $src]
1996 set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
1997 remote_file host delete $src
1998 remote_file host delete $output
2001 return $lto_available_saved
2004 # Returns true if the target compiler supports LTO -ffat-lto-objects
2005 proc check_lto_fat_available { } {
2006 global lto_fat_available_saved
2009 if {![info exists lto_fat_available_saved]} {
2010 if { ![check_gcc_plugin_enabled] } {
2011 set lto_fat_available_saved 0
2014 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
2015 # -ffat-lto-objects, we always run LTO tests on Linux with
2017 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
2018 set lto_fat_available_saved 1
2021 # Check if gcc supports -flto -fuse-linker-plugin
2023 if [board_info [target_info name] exists cflags] {
2024 append flags " [board_info [target_info name] cflags]"
2026 if [board_info [target_info name] exists ldflags] {
2027 append flags " [board_info [target_info name] ldflags]"
2030 set basename "tmpdir/lto[pid]"
2031 set src ${basename}.c
2032 set output ${basename}.out
2033 set f [open $src "w"]
2034 puts $f "int main() { return 0; }"
2036 if [is_remote host] {
2037 set src [remote_download host $src]
2039 set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
2040 remote_file host delete $src
2041 remote_file host delete $output
2044 return $lto_fat_available_saved
2047 # Returns true if the target compiler supports LTO and -shared
2048 proc check_lto_shared_available { } {
2049 global lto_shared_available_saved
2052 if {![info exists lto_shared_available_saved]} {
2053 if { ![check_gcc_plugin_enabled] } {
2054 set lto_shared_available_saved 0
2057 # This test will hide LTO bugs in ld. Since GCC 4.9 adds
2058 # -ffat-lto-objects, we always run LTO tests on Linux with
2060 if { [istarget "*-*-linux*"] && [at_least_gcc_version 4 9] } {
2061 set lto_shared_available_saved 1
2064 # Check if gcc supports -flto -fuse-linker-plugin -shared
2066 if [board_info [target_info name] exists cflags] {
2067 append flags " [board_info [target_info name] cflags]"
2069 if [board_info [target_info name] exists ldflags] {
2070 append flags " [board_info [target_info name] ldflags]"
2073 set basename "tmpdir/lto_shared[pid]"
2074 set src ${basename}.c
2075 set output ${basename}.so
2076 set f [open $src "w"]
2079 if [is_remote host] {
2080 set src [remote_download host $src]
2082 set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
2083 remote_file host delete $src
2084 remote_file host delete $output
2087 return $lto_shared_available_saved
2090 # Check if the assembler supports CFI statements.
2092 proc check_as_cfi { } {
2093 global check_as_cfi_result
2095 if [info exists check_as_cfi_result] {
2096 return $check_as_cfi_result
2098 set as_file "tmpdir/check_as_cfi.s"
2099 set as_fh [open $as_file w 0666]
2100 puts $as_fh "# Generated file. DO NOT EDIT"
2101 puts $as_fh "\t.cfi_startproc"
2102 puts $as_fh "\t.cfi_endproc"
2104 remote_download host $as_file
2105 verbose -log "Checking CFI support:"
2106 rename "perror" "check_as_cfi_perror"
2107 proc perror { args } { }
2108 set success [ld_assemble $as $as_file "/dev/null"]
2110 rename "check_as_cfi_perror" "perror"
2111 #remote_file host delete $as_file
2112 set check_as_cfi_result $success
2116 # Returns true if IFUNC works.
2118 proc check_ifunc_available { } {
2119 global ifunc_available_saved
2122 if {![info exists ifunc_available_saved]} {
2123 if { [which $CC] == 0 } {
2124 set ifunc_available_saved 0
2127 # Check if gcc supports -flto -fuse-linker-plugin
2129 if [board_info [target_info name] exists cflags] {
2130 append flags " [board_info [target_info name] cflags]"
2132 if [board_info [target_info name] exists ldflags] {
2133 append flags " [board_info [target_info name] ldflags]"
2136 set basename "tmpdir/ifunc[pid]"
2137 set src ${basename}.c
2138 set output ${basename}.out
2139 set f [open $src "w"]
2140 puts $f "extern int library_func2 (void);"
2141 puts $f "int main (void)"
2143 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2144 puts $f " return 0; "
2146 puts $f "static int library_func1 (void) {return 2; }"
2147 puts $f "void *foo (void) __asm__ (\"library_func2\");"
2148 puts $f "void *foo (void) { return library_func1; }"
2149 puts $f "__asm__(\".type library_func2, %gnu_indirect_function\");"
2151 if [is_remote host] {
2152 set src [remote_download host $src]
2154 set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2155 if { [isnative] && $ifunc_available_saved == 1 } {
2156 set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
2158 remote_file host delete $src
2159 remote_file host delete $output
2162 return $ifunc_available_saved
2165 # Returns true if ifunc attribute works.
2167 proc check_ifunc_attribute_available { } {
2168 global ifunc_attribute_available_saved
2171 if {![info exists ifunc_attribute_available_saved]} {
2172 if { [which $CC] == 0 } {
2173 set ifunc_attribute_available_saved 0
2176 # Check if gcc supports -flto -fuse-linker-plugin
2178 if [board_info [target_info name] exists cflags] {
2179 append flags " [board_info [target_info name] cflags]"
2181 if [board_info [target_info name] exists ldflags] {
2182 append flags " [board_info [target_info name] ldflags]"
2185 set basename "tmpdir/ifunc[pid]"
2186 set src ${basename}.c
2187 set output ${basename}.out
2188 set f [open $src "w"]
2189 puts $f "extern int library_func2 (void) __attribute__ ((ifunc (\"foo\")));"
2190 puts $f "int main (void)"
2192 puts $f " if (library_func2 () != 2) __builtin_abort ();"
2193 puts $f " return 0; "
2195 puts $f "static int library_func1 (void) {return 2; }"
2196 puts $f "void *foo (void) { return library_func1; }"
2198 if [is_remote host] {
2199 set src [remote_download host $src]
2201 set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
2202 if { [isnative] && $ifunc_attribute_available_saved == 1 } {
2203 set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
2205 remote_file host delete $src
2206 remote_file host delete $output
2209 return $ifunc_attribute_available_saved
2212 # Provide virtual target "cfi" for targets supporting CFI.
2214 rename "istarget" "istarget_ld"
2215 proc istarget { target } {
2216 if {$target == "cfi"} {
2217 return [check_as_cfi]
2219 if {$target == "shared"} {
2220 return [check_shared_lib_support]
2222 return [istarget_ld $target]
2225 # Return true if libdl is supported.
2227 proc check_libdl_available { } {
2228 global libdl_available_saved
2231 if {![info exists libdl_available_saved]} {
2232 if { [which $CC] == 0 } {
2233 set libdl_available_saved 0
2237 set basename "tmpdir/dl_avail_test[pid]"
2238 set src ${basename}.c
2239 set output ${basename}.out
2240 set f [open $src "w"]
2242 puts $f "#include <dlfcn.h>"
2243 puts $f "int main (void)"
2245 puts $f " dlopen (\"dummy.so\", RTLD_NOW);"
2246 puts $f " return 0; "
2249 if [is_remote host] {
2250 set src [remote_download host $src]
2252 set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
2253 remote_file host delete $src
2254 remote_file host delete $output
2257 return $libdl_available_saved
2260 # Returns true if GNU2 TLS works.
2262 proc check_gnu2_tls_available { } {
2263 global gnu2_tls_available_saved
2267 if {![info exists gnu2_tls_available_saved]} {
2268 if { [which $CC] == 0 || "$GNU2_CFLAGS" == "" } {
2269 set gnu2_tls_available_saved 0
2272 # Check if GNU2 TLS works.
2273 set flags "$GNU2_CFLAGS"
2274 if [board_info [target_info name] exists cflags] {
2275 append flags " [board_info [target_info name] cflags]"
2277 if [board_info [target_info name] exists ldflags] {
2278 append flags " [board_info [target_info name] ldflags]"
2281 set basename "tmpdir/gnu2_tls[pid]"
2282 set src1 ${basename}1.c
2283 set output1 ${basename}.so
2284 set f [open $src1 "w"]
2285 puts $f "extern __thread int zzz;"
2286 puts $f "int foo (void)"
2288 puts $f " return zzz;"
2291 if [is_remote host] {
2292 set src1 [remote_download host $src1]
2294 set src2 ${basename}2.c
2295 set output2 ${basename}.exe
2296 set f [open $src2 "w"]
2297 puts $f "__thread int zzz = 20;"
2298 puts $f "extern int foo (void);"
2299 puts $f "int main (void)"
2301 puts $f " if (foo () != 20) __builtin_abort ();"
2302 puts $f " return 0; "
2305 if [is_remote host] {
2306 set src2 [remote_download host $src2]
2308 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "-fPIC -shared $flags $src1 -o $output1"]
2309 if { $gnu2_tls_available_saved == 1 } {
2310 set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "$flags $src2 $output1 -o $output2"]
2311 if { $gnu2_tls_available_saved == 1 } {
2312 set gnu2_tls_available_saved [run_host_cmd_yesno "$output2" ""]
2315 remote_file host delete $src1
2316 remote_file host delete $output1
2317 remote_file host delete $src2
2318 remote_file host delete $output2
2319 file delete $src1 $src2
2321 return $gnu2_tls_available_saved