bfd/
[binutils-gdb.git] / ld / testsuite / lib / ld-lib.exp
index cc0741bb4149e1a7039d805a0071d711e7a533bf..acfe0df838f091f18f6ab6dca29d884e992db885 100644 (file)
@@ -1,6 +1,6 @@
 # Support routines for LD testsuite.
 #   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-#    2004, 2005 Free Software Foundation, Inc.
+#    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
 # 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
-#
-#
-# default_ld_version
-#      extract and print the version number of ld
+
+# Extract and print the version number of ld.
 #
 proc default_ld_version { ld } {
     global host_triplet
@@ -36,19 +34,20 @@ 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
@@ -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,7 +85,7 @@ 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] {
@@ -94,11 +93,11 @@ proc big_or_little_endian {} {
 
        foreach x $tmp_flags {
            case $x in {
-               {*big*endian eb EB -eb -EB -mb} {
+               {*big*endian eb EB -eb -EB -mb -meb} {
                    set flags " -EB"
                    return $flags
                }
-               {*little*endian el EL -el -EL -ml} {
+               {*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,6 +118,7 @@ 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"
@@ -135,26 +133,28 @@ 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"
@@ -180,6 +180,8 @@ proc default_ld_simple_link { ld target objects } {
        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
@@ -197,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
@@ -226,8 +226,11 @@ proc default_ld_compile { cc source object } {
     # based on the name of the compiler.
     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 {
@@ -238,9 +241,9 @@ proc default_ld_compile { cc source object } {
        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 {
@@ -269,9 +272,7 @@ 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
@@ -299,9 +300,7 @@ proc default_ld_assemble { as source object } {
     }
 }
 
-#
-# default_ld_nm
-#      run nm on a file, putting the result in the array nm_output
+# Run nm on a file, putting the result in the array nm_output.
 #
 proc default_ld_nm { nm nmflags object } {
     global NMFLAGS
@@ -352,9 +351,7 @@ proc default_ld_nm { nm nmflags object } {
     }
 }
 
-#
-# is_elf_format
-#      true if the object format is known to be ELF
+# True if the object format is known to be ELF.
 #
 proc is_elf_format {} {
     if { ![istarget *-*-sysv4*] \
@@ -390,9 +387,8 @@ proc is_elf_format {} {
     return 1
 }
 
+# True if the object format is known to be 64-bit ELF.
 #
-# is_elf64
-#      true if the object format is known to be 64bit ELF
 proc is_elf64 { binary_file } {
     global READELF
     global READELFFLAGS
@@ -416,9 +412,8 @@ proc is_elf64 { binary_file } {
     return 0
 }
 
+# True if the object format is known to be a.out.
 #
-# is_aout_format
-#      true if the object format is known to be aout
 proc is_aout_format {} {
     if { [istarget *-*-*\[ab\]out*] \
             || [istarget *-*-linux*oldld*] \
@@ -446,12 +441,10 @@ proc is_aout_format {} {
     return 0
 }
 
-#
-# is_pecoff_format
-#      true if the object format is known to be PECOFF
+# True if the object format is known to be PE COFF.
 #
 proc is_pecoff_format {} {
-    if { ![istarget *-*-mingw32*] \
+    if { ![istarget *-*-mingw*] \
         && ![istarget *-*-cygwin*] \
         && ![istarget *-*-pe*] } {
        return 0
@@ -460,11 +453,9 @@ proc is_pecoff_format {} {
     return 1
 }
 
-#
-# simple_diff
-#      compares two files line-by-line
-#      returns differences if exist
-#      returns null if file(s) cannot be opened
+# 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
@@ -627,7 +618,7 @@ proc simple_diff { file_1 file_2 } {
 # `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
@@ -802,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"
 
@@ -833,6 +825,7 @@ proc run_dump_test { name } {
     # Perhaps link the file(s).
     if { $run_ld } {
        set objfile "tmpdir/dump"
+       catch "exec rm -f $objfile" exec_output
 
        # Add -L$srcdir/$subdir so that the linker command can use
        # linker scripts in the source directory.
@@ -848,7 +841,7 @@ proc run_dump_test { name } {
            # 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
+           # 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" } {
@@ -859,6 +852,7 @@ proc run_dump_test { name } {
        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.
@@ -1096,12 +1090,12 @@ proc file_contents { filename } {
 # 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
@@ -1262,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
+       }
+    }
+}