bfd/
[binutils-gdb.git] / ld / testsuite / lib / ld-lib.exp
index bd8d211a347dce9430ab7e3724ca868c2d995630..acfe0df838f091f18f6ab6dca29d884e992db885 100644 (file)
@@ -1,24 +1,22 @@
 # Support routines for LD testsuite.
-#   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
-#   Free Software Foundation, Inc.
+#   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
+#    2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 #
 # This file is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-#
-#
-# default_ld_version 
-#      extract and print the version number of ld
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+
+# Extract and print the version number of ld.
 #
 proc default_ld_version { ld } {
     global host_triplet
@@ -27,7 +25,7 @@ proc default_ld_version { ld } {
        perror "$ld does not exist"
        exit 1
     }
-    
+
     catch "exec $ld --version" tmp
     set tmp [prune_warnings $tmp]
     regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
@@ -36,21 +34,22 @@ proc default_ld_version { ld } {
     }
 }
 
-#
-# default_ld_relocate 
-#      link an object using relocation
+# Link an object using relocation.
 #
 proc default_ld_relocate { ld target objects } {
     global HOSTING_EMU
     global host_triplet
-    
+    global exec_output
+
     if { [which $ld] == 0 } then {
        perror "$ld does not exist"
        return 0
     }
-    
+
+    catch "exec rm -f $target" exec_output
+
     verbose -log "$ld $HOSTING_EMU -o $target -r $objects"
-    
+
     catch "exec $ld $HOSTING_EMU -o $target -r $objects" exec_output
     set exec_output [prune_warnings $exec_output]
     if [string match "" $exec_output] then {
@@ -62,7 +61,7 @@ proc default_ld_relocate { ld target objects } {
 }
 
 # Check to see if ld is being invoked with a non-endian output format
-
+#
 proc is_endian_output_format { object_flags } {
 
     if {[string match "*-oformat binary*" $object_flags] ||      \
@@ -86,19 +85,19 @@ proc is_endian_output_format { object_flags } {
 # the site.exp file will include the switch "-mbig-endian"
 # (rather than "big-endian") which is not detected by proc
 # process_multilib_options.
-
+#
 proc big_or_little_endian {} {
-    
+
     if [board_info [target_info name] exists multilib_flags] {
-       set tmp_flags " [board_info [target_info name] multilib_flags]";
+       set tmp_flags " [board_info [target_info name] multilib_flags]"
 
        foreach x $tmp_flags {
            case $x in {
-               {*big*endian eb EB -eb -EB} {
+               {*big*endian eb EB -eb -EB -mb -meb} {
                    set flags " -EB"
                    return $flags
                }
-               {*little*endian el EL -el -EL} {
+               {*little*endian el EL -el -EL -ml -mel} {
                    set flags " -EL"
                    return $flags
                }
@@ -110,9 +109,7 @@ proc big_or_little_endian {} {
     return $flags
 }
 
-#
-# default_ld_link 
-#      link a program using ld
+# Link a program using ld.
 #
 proc default_ld_link { ld target objects } {
     global HOSTING_EMU
@@ -121,10 +118,11 @@ proc default_ld_link { ld target objects } {
     global LIBS
     global host_triplet
     global link_output
-    
+    global exec_output
+
     set objs "$HOSTING_CRT0 $objects"
     set libs "$LIBS $HOSTING_LIBS"
-    
+
     if { [which $ld] == 0 } then {
        perror "$ld does not exist"
        return 0
@@ -135,39 +133,57 @@ proc default_ld_link { ld target objects } {
     } else {
        set flags ""
     }
+
+    catch "exec rm -f $target" exec_output
+
     verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs"
-    
+
     catch "exec $ld $HOSTING_EMU $flags -o $target $objs $libs" link_output
     set exec_output [prune_warnings $link_output]
-    if [string match "" $link_output] then {
+    if [string match "" $exec_output] then {
        return 1
     } else {
-       verbose -log "$link_output"
+       verbose -log "$exec_output"
        return 0
     }
 }
 
-#
-# default_ld_simple_link 
-#      link a program using ld, without including any libraries
+# Link a program using ld, without including any libraries.
 #
 proc default_ld_simple_link { ld target objects } {
     global host_triplet
     global link_output
+    global gcc_ld_flag
+    global exec_output
 
     if { [which $ld] == 0 } then {
        perror "$ld does not exist"
        return 0
     }
-    
+
     if [is_endian_output_format $objects] then {
        set flags [big_or_little_endian]
     } else {
        set flags ""
     }
-    
+
+    # If we are compiling with gcc, we want to add gcc_ld_flag to
+    # flags.  Rather than determine this in some complex way, we guess
+    # based on the name of the compiler.
+    set ldexe $ld
+    set ldparm [string first " " $ld]
+    if { $ldparm > 0 } then {
+       set ldexe [string range $ld 0 $ldparm]
+    }
+    set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
+    if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
+       set flags "$gcc_ld_flag $flags"
+    }
+
+    catch "exec rm -f $target" exec_output
+
     verbose -log "$ld $flags -o $target $objects"
-    
+
     catch "exec $ld $flags -o $target $objects" link_output
     set exec_output [prune_warnings $link_output]
 
@@ -183,9 +199,7 @@ proc default_ld_simple_link { ld target objects } {
     }
 }
 
-#
-# default_ld_compile 
-#      compile an object using cc
+# Compile an object using cc.
 #
 proc default_ld_compile { cc source object } {
     global CFLAGS
@@ -210,17 +224,26 @@ proc default_ld_compile { cc source object } {
     # If we are compiling with gcc, we want to add gcc_gas_flag to
     # flags.  Rather than determine this in some complex way, we guess
     # based on the name of the compiler.
-    if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then {
+    set ccexe $cc
+    set ccparm [string first " " $cc]
+    set ccflags ""
+    if { $ccparm > 0 } then {
+       set ccflags [string range $cc $ccparm end]
+       set ccexe [string range $cc 0 $ccparm]
+       set cc $ccexe
+    }
+    set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
+    if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
        set flags "$gcc_gas_flag $flags"
     }
 
     if [board_info [target_info name] exists multilib_flags] {
-       append flags " [board_info [target_info name] multilib_flags]";
+       append flags " [board_info [target_info name] multilib_flags]"
     }
 
-    verbose -log "$cc $flags -c $source -o $object"
+    verbose -log "$cc $flags $ccflags -c $source -o $object"
 
-    catch "exec $cc $flags -c $source -o $object" exec_output
+    catch "exec $cc $flags $ccflags -c $source -o $object" exec_output
     set exec_output [prune_warnings $exec_output]
     if [string match "" $exec_output] then {
        if {![file exists $object]} then {
@@ -249,14 +272,12 @@ proc default_ld_compile { cc source object } {
     }
 }
 
-#
-# default_ld_assemble
-#      assemble a file
+# Assemble a file.
 #
 proc default_ld_assemble { as source object } {
     global ASFLAGS
     global host_triplet
-    
+
     if {[which $as] == 0} then {
        perror "$as does not exist"
        return 0
@@ -279,11 +300,9 @@ proc default_ld_assemble { as source object } {
     }
 }
 
+# Run nm on a file, putting the result in the array nm_output.
 #
-# default_ld_nm
-#      run nm on a file, putting the result in the array nm_output
-#
-proc default_ld_nm { nm object } {
+proc default_ld_nm { nm nmflags object } {
     global NMFLAGS
     global nm_output
     global host_triplet
@@ -299,15 +318,25 @@ proc default_ld_nm { nm object } {
 
     if ![info exists NMFLAGS] { set NMFLAGS "" }
 
-    verbose -log "$nm $NMFLAGS $object >tmpdir/nm.out"
+    # Ensure consistent sorting of symbols
+    if {[info exists env(LC_ALL)]} {
+       set old_lc_all $env(LC_ALL)
+    }
+    set env(LC_ALL) "C"
+    verbose -log "$nm $NMFLAGS $nmflags $object >tmpdir/nm.out"
 
-    catch "exec $nm $NMFLAGS $object >tmpdir/nm.out" exec_output
+    catch "exec $nm $NMFLAGS $nmflags $object >tmpdir/nm.out" exec_output
+    if {[info exists old_lc_all]} {
+       set env(LC_ALL) $old_lc_all
+    } else {
+       unset env(LC_ALL)
+    }
     set exec_output [prune_warnings $exec_output]
     if [string match "" $exec_output] then {
        set file [open tmpdir/nm.out r]
        while { [gets $file line] != -1 } {
            verbose "$line" 2
-           if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] (.+)$" $line whole value name] {
+           if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] \\.*(.+)$" $line whole value name] {
                 set name [string trimleft $name "_"]
                verbose "Setting nm_output($name) to 0x$value" 2
                set nm_output($name) 0x$value
@@ -322,34 +351,134 @@ proc default_ld_nm { nm object } {
     }
 }
 
+# True if the object format is known to be ELF.
 #
-# simple_diff
-#      compares two files line-by-line
-#      returns differences if exist
-#      returns null if file(s) cannot be opened
+proc is_elf_format {} {
+    if { ![istarget *-*-sysv4*] \
+        && ![istarget *-*-unixware*] \
+        && ![istarget *-*-elf*] \
+        && ![istarget *-*-eabi*] \
+        && ![istarget hppa*64*-*-hpux*] \
+        && ![istarget *-*-linux*] \
+        && ![istarget frv-*-uclinux*] \
+        && ![istarget *-*-irix5*] \
+        && ![istarget *-*-irix6*] \
+        && ![istarget *-*-netbsd*] \
+        && ![istarget *-*-solaris2*] } {
+       return 0
+    }
+
+    if { [istarget *-*-linux*aout*] \
+        || [istarget *-*-linux*oldld*] } {
+       return 0
+    }
+
+    if { ![istarget *-*-netbsdelf*] \
+        && ([istarget *-*-netbsd*aout*] \
+            || [istarget *-*-netbsdpe*] \
+            || [istarget arm*-*-netbsd*] \
+            || [istarget sparc-*-netbsd*] \
+            || [istarget i*86-*-netbsd*] \
+            || [istarget m68*-*-netbsd*] \
+            || [istarget vax-*-netbsd*] \
+            || [istarget ns32k-*-netbsd*]) } {
+       return 0
+    }
+    return 1
+}
+
+# True if the object format is known to be 64-bit ELF.
+#
+proc is_elf64 { binary_file } {
+    global READELF
+    global READELFFLAGS
+
+    set readelf_size ""
+    catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
+
+    if ![string match "" $got] then {
+       return 0
+    }
+
+    if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
+          [file_contents readelf.out] nil readelf_size] } {
+       return 0
+    }
+
+    if { $readelf_size == "64" } {
+       return 1
+    }
+
+    return 0
+}
+
+# True if the object format is known to be a.out.
+#
+proc is_aout_format {} {
+    if { [istarget *-*-*\[ab\]out*] \
+            || [istarget *-*-linux*oldld*] \
+            || [istarget *-*-msdos*] \
+            || [istarget arm-*-netbsd] \
+            || [istarget i?86-*-netbsd] \
+            || [istarget i?86-*-mach*] \
+            || [istarget i?86-*-vsta] \
+            || [istarget pdp11-*-*] \
+            || [istarget m68*-ericsson-ose] \
+            || [istarget m68k-hp-bsd*] \
+            || [istarget m68*-*-hpux*] \
+            || [istarget m68*-*-netbsd] \
+            || [istarget m68*-*-netbsd*4k*] \
+            || [istarget m68k-sony-*] \
+            || [istarget m68*-sun-sunos\[34\]*] \
+            || [istarget m68*-wrs-vxworks*] \
+            || [istarget ns32k-*-*] \
+            || [istarget sparc*-*-netbsd] \
+            || [istarget sparc-sun-sunos4*] \
+            || [istarget vax-dec-ultrix*] \
+            || [istarget vax-*-netbsd] } {
+       return 1
+    }
+    return 0
+}
+
+# True if the object format is known to be PE COFF.
+#
+proc is_pecoff_format {} {
+    if { ![istarget *-*-mingw*] \
+        && ![istarget *-*-cygwin*] \
+        && ![istarget *-*-pe*] } {
+       return 0
+    }
+
+    return 1
+}
+
+# Compares two files line-by-line.
+#   Returns differences if exist.
+#   Returns null if file(s) cannot be opened.
 #
 proc simple_diff { file_1 file_2 } {
     global target
-       
+
     set eof -1
     set differences 0
-    
+
     if [file exists $file_1] then {
        set file_a [open $file_1 r]
     } else {
        warning "$file_1 doesn't exist"
        return
     }
-    
+
     if [file exists $file_2] then {
        set file_b [open $file_2 r]
     } else {
        fail "$file_2 doesn't exist"
        return
     }
-    
+
     verbose "# Diff'ing: $file_1 $file_2\n" 2
-    
+
     while { [gets $file_a line] != $eof } {
        if [regexp "^#.*$" $line] then {
            continue
@@ -358,7 +487,7 @@ proc simple_diff { file_1 file_2 } {
        }
     }
     close $file_a
-    
+
     while { [gets $file_b line] != $eof } {
        if [regexp "^#.*$" $line] then {
            continue
@@ -382,7 +511,7 @@ proc simple_diff { file_1 file_2 } {
            return
        }
     }
-    
+
     if { [llength $list_a] != [llength $list_b] } {
        fail "Test: $target"
        return
@@ -393,11 +522,11 @@ proc simple_diff { file_1 file_2 } {
     }
 }
 
-# run_dump_test FILE 
+# run_dump_test FILE
 # Copied from gas testsuite, tweaked and further extended.
 #
 # Assemble a .s file, then run some utility on it and check the output.
-# 
+#
 # There should be an assembly language file named FILE.s in the test
 # suite directory, and a pattern file called FILE.d.  `run_dump_test'
 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
@@ -409,9 +538,9 @@ proc simple_diff { file_1 file_2 } {
 # flags to pass to the assembler, the program to run to dump the
 # assembler's output, and the options it wants.  The option lines have
 # the syntax:
-# 
+#
 #         # OPTION: VALUE
-# 
+#
 # OPTION is the name of some option, like "name" or "objdump", and
 # VALUE is OPTION's value.  The valid options are described below.
 # Whitespace is ignored everywhere, except within VALUE.  The option
@@ -419,12 +548,12 @@ proc simple_diff { file_1 file_2 } {
 # (hmm, not great for error detection).
 #
 # The interesting options are:
-# 
+#
 #   name: TEST-NAME
 #      The name of this test, passed to DejaGNU's `pass' and `fail'
 #       commands.  If omitted, this defaults to FILE, the root of the
 #       .s and .d files' names.
-# 
+#
 #   as: FLAGS
 #      When assembling, pass FLAGS to the assembler.
 #       If assembling several files, you can pass different assembler
@@ -434,6 +563,12 @@ proc simple_diff { file_1 file_2 } {
 #       Link assembled files using FLAGS, in the order of the "source"
 #       directives, when using multiple files.
 #
+#   objcopy_linked_file: FLAGS
+#      Run objcopy on the linked file with the specified flags.
+#      This lets you transform the linked file using objcopy, before the
+#      result is analyzed by an analyzer program specified below (which
+#      may in turn *also* be objcopy).
+#
 #   PROG: PROGRAM-NAME
 #       The name of the program to run to analyze the .o file produced
 #       by the assembler or the linker output.  This can be omitted;
@@ -445,6 +580,8 @@ proc simple_diff { file_1 file_2 } {
 #   objcopy: FLAGS
 #      Use the specified program to analyze the assembler or linker
 #       output file, and pass it FLAGS, in addition to the output name.
+#      Note that they are run with LC_ALL=C in the environment to give
+#      consistent sorting of symbols.
 #
 #   source: SOURCE [FLAGS]
 #      Assemble the file SOURCE.s using the flags in the "as" directive
@@ -471,18 +608,23 @@ proc simple_diff { file_1 file_2 } {
 #      to pass.  The PROG, objdump, nm and objcopy options have no
 #      meaning and need not supplied if this is present.
 #
+#   warning: REGEX
+#      Expect a linker warning matching REGEX.  It is an error to issue
+#      both "error" and "warning".
+#
 # Each option may occur at most once unless otherwise mentioned.
 #
 # After the option lines come regexp lines.  `run_dump_test' calls
 # `regexp_diff' to compare the output of the dumping tool against the
 # regexps in FILE.d.  `regexp_diff' is defined later in this file; see
 # further comments there.
-
+#
 proc run_dump_test { name } {
     global subdir srcdir
     global OBJDUMP NM AS OBJCOPY READELF LD
     global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS LDFLAGS
     global host_triplet runtests
+    global env
 
     if [string match "*/*" $name] {
        set file $name
@@ -503,6 +645,7 @@ proc run_dump_test { name } {
     }
     set dumpfile tmpdir/dump.out
     set run_ld 0
+    set run_objcopy 0
     set opts(as) {}
     set opts(ld) {}
     set opts(xfail) {}
@@ -516,7 +659,9 @@ proc run_dump_test { name } {
     set opts(PROG) {}
     set opts(source) {}
     set opts(error) {}
-    set asflags{${file}.s} {}
+    set opts(warning) {}
+    set opts(objcopy_linked_file) {}
+    set asflags(${file}.s) {}
 
     foreach i $opt_array {
        set opt_name [lindex $i 0]
@@ -552,6 +697,10 @@ proc run_dump_test { name } {
                if { $opt_name == "ld" } {
                    set run_ld 1
                }
+               # Likewise objcopy_linked_file.
+               if { $opt_name == "objcopy_linked_file" } {
+                   set run_objcopy 1
+               }
            }
        }
        set opts($opt_name) [concat $opts($opt_name) $opt_val]
@@ -576,49 +725,42 @@ proc run_dump_test { name } {
        }
     }
 
-    if {$opts(PROG) != ""} {
-       switch -- $opts(PROG) {
-           objdump
-               { set program objdump }
-           nm
-               { set program nm }
-           objcopy
-               { set program objcopy }
-           readelf
-               { set program readelf }
-           default
+    set program ""
+    # It's meaningless to require an output-testing method when we
+    # expect an error.
+    if { $opts(error) == "" } {
+       if {$opts(PROG) != ""} {
+           switch -- $opts(PROG) {
+               objdump { set program objdump }
+               nm      { set program nm }
+               objcopy { set program objcopy }
+               readelf { set program readelf }
+               default
                { perror "unrecognized program option $opts(PROG) in $file.d"
                  unresolved $subdir/$name
                  return }
-       }
-    } elseif { $opts(error) != "" } {
-       # It's meaningless to require an output-testing method when we
-       # expect an error.  For simplicity, we fake an arbitrary method.
-       set program "nm"
-    } else {
+           }
+       } else {
        # Guess which program to run, by seeing which option was specified.
-       set program ""
-       foreach p {objdump objcopy nm readelf} {
-           if {$opts($p) != ""} {
-               if {$program != ""} {
-                   perror "ambiguous dump program in $file.d"
-                   unresolved $subdir/$name
-                   return
-               } else {
-                   set program $p
+           foreach p {objdump objcopy nm readelf} {
+               if {$opts($p) != ""} {
+                   if {$program != ""} {
+                       perror "ambiguous dump program in $file.d"
+                       unresolved $subdir/$name
+                       return
+                   } else {
+                       set program $p
+                   }
                }
            }
        }
-       if {$program == ""} {
+       if { $program == "" && $opts(warning) == "" } {
            perror "dump program unspecified in $file.d"
            unresolved $subdir/$name
            return
        }
     }
 
-    set progopts1 $opts($program)
-    eval set progopts \$[string toupper $program]FLAGS
-    eval set binary \$[string toupper $program]
     if { $opts(name) == "" } {
        set testname "$subdir/$name"
     } else {
@@ -630,7 +772,11 @@ proc run_dump_test { name } {
     } else {
        set sourcefiles {}
        foreach sf $opts(source) {
-           lappend sourcefiles "$srcdir/$subdir/$sf"
+           if { [string match "/*" $sf] } {
+               lappend sourcefiles "$sf"
+           } else {
+               lappend sourcefiles "$srcdir/$subdir/$sf"
+           }
            # Must have asflags indexed on source name.
            set asflags($srcdir/$subdir/$sf) $asflags($sf)
        }
@@ -647,6 +793,7 @@ proc run_dump_test { name } {
        set sourcefile [lindex $sourcefiles $i]
 
        set objfile "tmpdir/dump$i.o"
+       catch "exec rm -f $objfile" exec_output
        lappend objfiles $objfile
        set cmd "$AS $ASFLAGS $opts(as) $asflags($sourcefile) -o $objfile $sourcefile"
 
@@ -654,43 +801,94 @@ proc run_dump_test { name } {
        set cmdret [catch "exec $cmd" comp_output]
        set comp_output [prune_warnings $comp_output]
 
-       # We accept errors at assembly stage too, unless we're supposed to
-       # link something.
        if { $cmdret != 0 || ![string match "" $comp_output] } then {
            send_log "$comp_output\n"
            verbose "$comp_output" 3
-           if { $opts(error) != "" && $run_ld == 0 } {
-               if [regexp $opts(error) $comp_output] {
-                   pass $testname
-                   return
-               }
-           }
+
+           set exitstat "succeeded"
+           if { $cmdret != 0 } { set exitstat "failed" }
+           verbose -log "$exitstat with: <$comp_output>"
            fail $testname
            return
        }
     }
 
+    set expmsg $opts(error)
+    if { $opts(warning) != "" } {
+       if { $expmsg != "" } {
+           perror "$testname: mixing error and warning test-directives"
+           return
+       }
+       set expmsg $opts(warning)
+    }
+
     # Perhaps link the file(s).
     if { $run_ld } {
        set objfile "tmpdir/dump"
-       set cmd "$LD $LDFLAGS $opts(ld) -o $objfile $objfiles"
+       catch "exec rm -f $objfile" exec_output
+
+       # Add -L$srcdir/$subdir so that the linker command can use
+       # linker scripts in the source directory.
+       set cmd "$LD $LDFLAGS -L$srcdir/$subdir \
+                  $opts(ld) -o $objfile $objfiles"
 
        send_log "$cmd\n"
        set cmdret [catch "exec $cmd" comp_output]
        set comp_output [prune_warnings $comp_output]
 
-       if { $cmdret != 0 || ![string match "" $comp_output] } then {
-           verbose -log "failed with: <$comp_output>, expected: <$opts(error)>"
+       if { $cmdret != 0 } then {
+           # If the executed program writes to stderr and stderr is not
+           # redirected, exec *always* returns failure, regardless of the
+           # program exit code.  Thankfully, we can retrieve the true
+           # return status from a special variable.  Redirection would
+           # cause a Tcl-specific message to be appended, and we'd rather
+           # not deal with that if we can help it.
+           global errorCode
+           if { [lindex $errorCode 0] == "NONE" } {
+               set cmdret 0
+           }
+       }
+
+       if { $cmdret == 0 && $run_objcopy } {
+           set infile $objfile
+           set objfile "tmpdir/dump1"
+           catch "exec rm -f $objfile" exec_output
+
+           # Note that we don't use OBJCOPYFLAGS here; any flags must be
+           # explicitly specified.
+           set cmd "$OBJCOPY $opts(objcopy_linked_file) $infile $objfile"
+
+           send_log "$cmd\n"
+           set cmdret [catch "exec $cmd" comp_output]
+           append comp_output [prune_warnings $comp_output]
+
+           if { $cmdret != 0 } then {
+               global errorCode
+               if { [lindex $errorCode 0] == "NONE" } {
+                   set cmdret 0
+               }
+           }
+       }
+
+       if { $cmdret != 0 || $comp_output != "" || $expmsg != "" } then {
+           set exitstat "succeeded"
+           if { $cmdret != 0 } { set exitstat "failed" }
+           verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
            send_log "$comp_output\n"
            verbose "$comp_output" 3
-           if { $opts(error) != "" } {
-               if [regexp $opts(error) $comp_output] {
+
+           if { [regexp $expmsg $comp_output] \
+                   && (($cmdret == 0) == ($opts(warning) != "")) } {
+               # We have the expected output from ld.
+               if { $opts(error) != "" || $program == "" } {
                    pass $testname
                    return
                }
+           } else {
+               verbose -log "$exitstat with: <$comp_output>, expected: <$expmsg>"
+               fail $testname
+               return
            }
-           fail $testname
-           return
        }
     } else {
        set objfile "tmpdir/dump0.o"
@@ -699,8 +897,13 @@ proc run_dump_test { name } {
     # We must not have expected failure if we get here.
     if { $opts(error) != "" } {
        fail $testname
+       return
     }
 
+    set progopts1 $opts($program)
+    eval set progopts \$[string toupper $program]FLAGS
+    eval set binary \$[string toupper $program]
+
     if { [which $binary] == 0 } {
        untested $testname
        return
@@ -711,26 +914,28 @@ proc run_dump_test { name } {
 
     # Objcopy, unlike the other two, won't send its output to stdout,
     # so we have to run it specially.
+    set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
     if { $program == "objcopy" } {
        set cmd "$binary $progopts $progopts1 $objfile $dumpfile"
-       send_log "$cmd\n"
-       catch "exec $cmd" comp_output
-       set comp_output [prune_warnings $comp_output]
-       if ![string match "" $comp_output] then {
-           send_log "$comp_output\n"
-           fail $testname
-           return
-       }
+    }
+
+    # Ensure consistent sorting of symbols
+    if {[info exists env(LC_ALL)]} {
+       set old_lc_all $env(LC_ALL)
+    }
+    set env(LC_ALL) "C"
+    send_log "$cmd\n"
+    catch "exec $cmd" comp_output
+    if {[info exists old_lc_all]} {
+       set env(LC_ALL) $old_lc_all
     } else {
-       set cmd "$binary $progopts $progopts1 $objfile > $dumpfile"
-       send_log "$cmd\n"
-       catch "exec $cmd" comp_output
-       set comp_output [prune_warnings $comp_output]
-       if ![string match "" $comp_output] then {
-           send_log "$comp_output\n"
-           fail $testname
-           return
-       }
+       unset env(LC_ALL)
+    }
+    set comp_output [prune_warnings $comp_output]
+    if ![string match "" $comp_output] then {
+       send_log "$comp_output\n"
+       fail $testname
+       return
     }
 
     verbose_eval {[file_contents $dumpfile]} 3
@@ -754,8 +959,8 @@ proc slurp_options { file } {
     set ws  {[         ]*}
     set nws {[^        ]*}
     # whitespace is ignored anywhere except within the options list;
-    # option names are alphabetic only
-    set pat "^#${ws}(\[a-zA-Z\]*)$ws:${ws}(.*)$ws\$"
+    # option names are alphabetic plus underscore only.
+    set pat "^#${ws}(\[a-zA-Z_\]*)$ws:${ws}(.*)$ws\$"
     while { [gets $f line] != -1 } {
        set line [string trim $line]
        # Whitespace here is space-tab.
@@ -818,6 +1023,7 @@ proc regexp_diff { file_1 file_2 } {
            } elseif [ string match "#..." $line_b ] {
                if { [gets $file_b line_b] == $eof } {
                    set end_2 1
+                   set diff_pass 1
                    break
                }
                verbose "looking for \"^$line_b$\"" 3
@@ -836,9 +1042,9 @@ proc regexp_diff { file_1 file_2 } {
            }
        }
 
-        if { $diff_pass } { 
-            break 
-        } elseif { $end_1 && $end_2 } { 
+        if { $diff_pass } {
+            break
+        } elseif { $end_1 && $end_2 } {
             break
         } elseif { $end_1 } {
             send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
@@ -879,6 +1085,150 @@ proc file_contents { filename } {
     return $contents
 }
 
+# List contains test-items with 3 items followed by 2 lists, one item and
+# one optional item:
+# 0:name 1:ld options 2:assembler options
+# 3:filenames of assembler files 4: action and options. 5: name of output file
+# 6:compiler flags (optional)
+#
+# Actions:
+# objdump: Apply objdump options on result.  Compare with regex (last arg).
+# nm: Apply nm options on result.  Compare with regex (last arg).
+# readelf: Apply readelf options on result.  Compare with regex (last arg).
+#
+proc run_ld_link_tests { ldtests } {
+    global ld
+    global as
+    global nm
+    global objdump
+    global READELF
+    global srcdir
+    global subdir
+    global env
+    global CC
+    global CFLAGS
+
+    foreach testitem $ldtests {
+       set testname [lindex $testitem 0]
+       set ld_options [lindex $testitem 1]
+       set as_options [lindex $testitem 2]
+       set src_files  [lindex $testitem 3]
+       set actions [lindex $testitem 4]
+       set binfile tmpdir/[lindex $testitem 5]
+       set cflags [lindex $testitem 6]
+       set objfiles {}
+       set is_unresolved 0
+       set failed 0
+
+#      verbose -log "Testname is $testname"
+#      verbose -log "ld_options is $ld_options"
+#      verbose -log "as_options is $as_options"
+#      verbose -log "src_files is $src_files"
+#      verbose -log "actions is $actions"
+#      verbose -log "binfile is $binfile"
+
+       # Assemble each file in the test.
+       foreach src_file $src_files {
+           set objfile "tmpdir/[file rootname $src_file].o"
+           lappend objfiles $objfile
+
+           if { [file extension $src_file] == ".c" } {
+               set as_file "tmpdir/[file rootname $src_file].s"
+               if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
+                   set is_unresolved 1
+                   break
+               }
+           } else {
+               set as_file "$srcdir/$subdir/$src_file"
+           }
+           if ![ld_assemble $as "$as_options $as_file" $objfile] {
+               set is_unresolved 1
+               break
+           }
+       }
+
+       # Catch assembler errors.
+       if { $is_unresolved != 0 } {
+           unresolved $testname
+           continue
+       }
+
+       if ![ld_simple_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
+           fail $testname
+       } else {
+           set failed 0
+           foreach actionlist $actions {
+               set action [lindex $actionlist 0]
+               set progopts [lindex $actionlist 1]
+
+               # There are actions where we run regexp_diff on the
+               # output, and there are other actions (presumably).
+               # Handling of the former look the same.
+               set dump_prog ""
+               switch -- $action {
+                   objdump
+                       { set dump_prog $objdump }
+                   nm
+                       { set dump_prog $nm }
+                   readelf
+                       { set dump_prog $READELF }
+                   default
+                       {
+                           perror "Unrecognized action $action"
+                           set is_unresolved 1
+                           break
+                       }
+                   }
+
+               if { $dump_prog != "" } {
+                   set dumpfile [lindex $actionlist 2]
+                   set binary $dump_prog
+
+                   # Ensure consistent sorting of symbols
+                   if {[info exists env(LC_ALL)]} {
+                       set old_lc_all $env(LC_ALL)
+                   }
+                   set env(LC_ALL) "C"
+                   set cmd "$binary $progopts $binfile > dump.out"
+                   send_log "$cmd\n"
+                   catch "exec $cmd" comp_output
+                   if {[info exists old_lc_all]} {
+                       set env(LC_ALL) $old_lc_all
+                   } else {
+                       unset env(LC_ALL)
+                   }
+                   set comp_output [prune_warnings $comp_output]
+
+                   if ![string match "" $comp_output] then {
+                       send_log "$comp_output\n"
+                       set failed 1
+                       break
+                   }
+
+                   if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
+                       verbose "output is [file_contents "dump.out"]" 2
+                       set failed 1
+                       break
+                   }
+               }
+           }
+
+           if { $failed != 0 } {
+               fail $testname
+           } else { if { $is_unresolved == 0 } {
+               pass $testname
+           } }
+       }
+
+       # Catch action errors.
+       if { $is_unresolved != 0 } {
+           unresolved $testname
+           continue
+       }
+    }
+}
+
+
 proc verbose_eval { expr { level 1 } } {
     global verbose
     if $verbose>$level then { eval verbose "$expr" $level }
@@ -906,3 +1256,259 @@ if ![string length [info proc prune_warnings]] {
        return $text
     }
 }
+
+# targets_to_xfail is a list of target triplets to be xfailed.
+# ldtests contains test-items with 3 items followed by 1 lists, 2 items
+# and 3 optional items:
+#   0:name
+#   1:ld options
+#   2:assembler options
+#   3:filenames of source files
+#   4:name of output file
+#   5:expected output
+#   6:compiler flags (optional)
+#   7:language (optional)
+#   8:linker warning (optional)
+
+proc run_ld_link_exec_tests { targets_to_xfail ldtests } {
+    global ld
+    global as
+    global srcdir
+    global subdir
+    global env
+    global CC
+    global CXX
+    global CFLAGS
+    global errcnt
+    global exec_output
+
+    foreach testitem $ldtests {
+       foreach target $targets_to_xfail {
+           setup_xfail $target
+       }
+       set testname [lindex $testitem 0]
+       set ld_options [lindex $testitem 1]
+       set as_options [lindex $testitem 2]
+       set src_files  [lindex $testitem 3]
+       set binfile tmpdir/[lindex $testitem 4]
+       set expfile [lindex $testitem 5]
+       set cflags [lindex $testitem 6]
+       set lang [lindex $testitem 7]
+       set warning [lindex $testitem 8]
+       set objfiles {}
+       set failed 0
+
+#      verbose -log "Testname is $testname"
+#      verbose -log "ld_options is $ld_options"
+#      verbose -log "as_options is $as_options"
+#      verbose -log "src_files is $src_files"
+#      verbose -log "actions is $actions"
+#      verbose -log "binfile is $binfile"
+
+       # Assemble each file in the test.
+       foreach src_file $src_files {
+           set objfile "tmpdir/[file rootname $src_file].o"
+           lappend objfiles $objfile
+
+           # We ignore warnings since some compilers may generate
+           # incorrect section attributes and the assembler will warn
+           # them.
+           ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
+
+           # We have to use $CC to build PIE and shared library.
+           if { [ string match "c" $lang ] } {
+               set link_proc ld_simple_link
+               set link_cmd $CC
+           } elseif { [ string match "c++" $lang ] } {
+               set link_proc ld_simple_link
+               set link_cmd $CXX
+           } elseif { [ string match "-shared" $ld_options ] \
+                || [ string match "-pie" $ld_options ] } {
+               set link_proc ld_simple_link
+               set link_cmd $CC
+           } else {
+               set link_proc ld_link
+               set link_cmd $ld
+           }
+
+           if ![$link_proc $link_cmd $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
+               set failed 1
+           } else {
+               set failed 0
+           }
+
+           # Check if exec_output is expected.
+           if { $warning != "" } then {
+               verbose -log "returned with: <$exec_output>, expected: <$warning>"
+               if { [regexp $warning $exec_output] } then {
+                   set failed 0
+               } else {
+                   set failed 1
+               }
+           }
+
+           if { $failed == 0 } {
+               send_log "Running: $binfile > $binfile.out\n"
+               verbose "Running: $binfile > $binfile.out"
+               catch "exec $binfile > $binfile.out" exec_output
+           
+               if ![string match "" $exec_output] then {
+                   send_log "$exec_output\n"
+                   verbose "$exec_output" 1
+                   set failed 1
+               } else {
+                   send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
+                   verbose "diff $binfile.out $srcdir/$subdir/$expfile"
+                   catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
+                   set exec_output [prune_warnings $exec_output]
+
+                   if ![string match "" $exec_output] then {
+                       send_log "$exec_output\n"
+                       verbose "$exec_output" 1
+                       set failed 1
+                   }
+               }
+           }
+
+           if { $failed != 0 } {
+               fail $testname
+           } else {
+               set errcnt 0
+               pass $testname
+           }
+       }
+    }
+}
+
+# List contains test-items with 3 items followed by 2 lists, one item and
+# one optional item:
+#  0:name
+#  1:link options
+#  2:compile options
+#  3:filenames of source files
+#  4:action and options.
+#  5:name of output file
+#  6:language (optional)
+#
+# Actions:
+# objdump: Apply objdump options on result.  Compare with regex (last arg).
+# nm: Apply nm options on result.  Compare with regex (last arg).
+# readelf: Apply readelf options on result.  Compare with regex (last arg).
+#
+proc run_cc_link_tests { ldtests } {
+    global nm
+    global objdump
+    global READELF
+    global srcdir
+    global subdir
+    global env
+    global CC
+    global CXX
+    global CFLAGS
+
+    foreach testitem $ldtests {
+       set testname [lindex $testitem 0]
+       set ldflags [lindex $testitem 1]
+       set cflags [lindex $testitem 2]
+       set src_files  [lindex $testitem 3]
+       set actions [lindex $testitem 4]
+       set binfile tmpdir/[lindex $testitem 5]
+       set lang [lindex $testitem 6]
+       set objfiles {}
+       set is_unresolved 0
+       set failed 0
+
+       # Compile each file in the test.
+       foreach src_file $src_files {
+           set objfile "tmpdir/[file rootname $src_file].o"
+           lappend objfiles $objfile
+
+           # We ignore warnings since some compilers may generate
+           # incorrect section attributes and the assembler will warn
+           # them.
+           ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile
+       }
+
+       # Clear error and warning counts.
+       reset_vars
+
+       if { [ string match "c++" $lang ] } {
+           set cc_cmd $CXX
+       } else {
+           set cc_cmd $CC
+       }
+
+       if ![ld_simple_link $cc_cmd $binfile "-L$srcdir/$subdir $ldflags $objfiles"] {
+           fail $testname
+       } else {
+           set failed 0
+           foreach actionlist $actions {
+               set action [lindex $actionlist 0]
+               set progopts [lindex $actionlist 1]
+
+               # There are actions where we run regexp_diff on the
+               # output, and there are other actions (presumably).
+               # Handling of the former look the same.
+               set dump_prog ""
+               switch -- $action {
+                   objdump
+                       { set dump_prog $objdump }
+                   nm
+                       { set dump_prog $nm }
+                   readelf
+                       { set dump_prog $READELF }
+                   default
+                       {
+                           perror "Unrecognized action $action"
+                           set is_unresolved 1
+                           break
+                       }
+                   }
+
+               if { $dump_prog != "" } {
+                   set dumpfile [lindex $actionlist 2]
+                   set binary $dump_prog
+
+                   # Ensure consistent sorting of symbols
+                   if {[info exists env(LC_ALL)]} {
+                       set old_lc_all $env(LC_ALL)
+                   }
+                   set env(LC_ALL) "C"
+                   set cmd "$binary $progopts $binfile > dump.out"
+                   send_log "$cmd\n"
+                   catch "exec $cmd" comp_output
+                   if {[info exists old_lc_all]} {
+                       set env(LC_ALL) $old_lc_all
+                   } else {
+                       unset env(LC_ALL)
+                   }
+                   set comp_output [prune_warnings $comp_output]
+
+                   if ![string match "" $comp_output] then {
+                       send_log "$comp_output\n"
+                       set failed 1
+                       break
+                   }
+
+                   if { [regexp_diff "dump.out" "$srcdir/$subdir/$dumpfile"] } then {
+                       verbose "output is [file_contents "dump.out"]" 2
+                       set failed 1
+                       break
+                   }
+               }
+           }
+
+           if { $failed != 0 } {
+               fail $testname
+           } else { if { $is_unresolved == 0 } {
+               pass $testname
+           } }
+       }
+
+       # Catch action errors.
+       if { $is_unresolved != 0 } {
+           unresolved $testname
+           continue
+       }
+    }
+}