From fddf396f1a20e350ab233625f4866abb6bec70b7 Mon Sep 17 00:00:00 2001
From: Caroline Tice <cmtice@google.com>
Date: Thu, 16 Jul 2020 09:26:04 -0700
Subject: [PATCH v1] Update GDB testsuite to allow tests to use pre-built
 object files.

This change is motivated by the fact that clang and GCC sometimes generate
different output, which affects whether or not a test works properly.  The
particular motivating example is the gdb.dwarf2/dw5-rnglist-test.exp test,
which tests whether or not GDB is properly reading/handling DWARF v5
DW_AT_ranges of the form DW_FORM_rnglistx in lexical block dies.  GCC does
not generate that code, so compiling the test with GCC does not successfully
test anything. Clang does generate that code, so the only way to properly
test this is to compile the test case with clang.  A further twist is that
the GNU assembler currently chokes on DWARF v5 .debug_line sections, because
they start their file index at 0 instead of 1, and the GNU assembler has not
been updated to handle this.  This is not a problem for
assembly files generated by GCC, because GCC generates DWARF v3
.debug_line sections, even when passed -gdwarf-5.  Clang on the other hand,
when passed -gdwarf-5, generates DWARF v5 .debug_line tables. So using a
clang-generated .s file for the test is not an option, because the GNU
assembler chokes on it.

This patch updates lib/gdb.exp to allow it to properly handle test
cases where the source file is in fact an object file, and it updates
the dw5-rnglist-test to use an object file rather than the .cc file.  This
in turn makes the test itself compiler-agnostic.
---
 gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.exp |  15 ++++++++++++--
 gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.o   | Bin 0 -> 93144 bytes
 gdb/testsuite/lib/gdb.exp                     |  19 ++++++++++++++----
 3 files changed, 28 insertions(+), 6 deletions(-)
 create mode 100644 gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.o

diff --git a/gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.exp b/gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.exp
index af6c34b5dd..53f7c16795 100644
--- a/gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.exp
@@ -17,12 +17,23 @@
 # DW_FORM_rnglistx DW_AT_ranges field.  This test is intended for DWARF-5,
 # compiled with clang++.
 
-standard_testfile .cc
+# In order to allow this test to work with GCC as well as clang, we
+# compiled the source to an object file, using the command below, and then
+# run the test on the .o file.  This ensures that it will work for
+# either compiler, and that it will continue to test what it was
+# intened to test, even if subsequent compilers change their code
+# generation.
+#
+# Compilation command:
+# $ clang++ -gdwarf-5 -O0 -fPIE -c dw5-rnglist-test.cc
+
+standard_testfile .o
 
 # This test is intended for targets which support DWARF-5.
 # Since we pass an explicit -gdwarf-5 to the compiler,
 # we let that be the test of whether the target supports it.
 
+
 if { [prepare_for_testing "failed to prepare" "${testfile}" \
           $srcfile {debug c++ additional_flags=-gdwarf-5 \
                         additional_flags=-O0}] } {
@@ -33,7 +44,7 @@ if ![runto_main] {
     return -1
 }
 
-gdb_breakpoint [gdb_get_line_number "break-here"]
+gdb_breakpoint dw5-rnglist-test.cc:70
 gdb_continue_to_breakpoint "break-here" ".* break-here .*"
 
 gdb_test "print curr" "\\\(node \\\*\\\) $hex <node_array>"
diff --git a/gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.o b/gdb/testsuite/gdb.dwarf2/dw5-rnglist-test.o
new file mode 100644
index 0000000000000000000000000000000000000000..a45678039dde17c8bfaf89eb568a44525ca459d7
GIT binary patch
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 78dae17b0c..f5e1f0e876 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6227,6 +6227,7 @@ proc build_executable_from_specs {testname executable options args} {
     global srcdir
 
     set binfile [standard_output_file $executable]
+    set output_dir [standard_output_file "."]
 
     set info_options ""
     if { [lsearch -exact $options "c++"] >= 0 } {
@@ -6272,11 +6273,21 @@ proc build_executable_from_specs {testname executable options args} {
 	    if { ! [regexp "^/" "$s"] } then {
 		set s "$srcdir/$subdir/$s"
 	    }
-	    if  { [$func "${s}" "${binfile}${i}.o" object $local_options] != "" } {
-		untested $testname
-		return -1
+	    # Only compile sourcfile into .o file if it is not already a
+	    # .o file.
+	    if { ! [string match "*.o" "$s"] } then {
+		if  { [$func "${s}" "${binfile}${i}.o" object $local_options] != "" } {
+		    untested $testname
+		    return -1
+		}
+		lappend objects "${binfile}${i}.o"
+	    } else {
+		# Source file IS an object file, so copy it to the
+		# standard_output_file directory and append it to the objects
+		# list.
+		eval exec cp "${s}" "${output_dir}"
+		lappend objects "${binfile}.o"
 	    }
-	    lappend objects "${binfile}${i}.o"
 	    incr i
 	}
 	set ret [$func $objects "${binfile}" executable $options]
-- 
2.27.0.389.gc38d7665816-goog

