[3/5] Avoid crash on missing dwz file

Message ID 20210221031647.949270-4-tom@tromey.com
State New
Headers show
Series
  • Some minor dwz-handling improvements & cleanups
Related show

Commit Message

Tom Tromey Feb. 21, 2021, 3:16 a.m.
If DWARF contains a reference to a "dwz" file, but there is no
.gnu_debugaltlink section, then gdb will crash.  This happens because
dwarf2_get_dwz_file will return NULL, but some callers do not expect
this.

This patch changes dwarf2_get_dwz_file so that callers can require a
dwz file.  Then, it updates the callers that are attempting to process
references to the dwz file to require one.

gdb/ChangeLog
2021-02-20  Tom Tromey  <tom@tromey.com>

	* dwarf2/read.h (dwarf2_get_dwz_file): Add 'require' parameter.
	* dwarf2/read.c (dwarf2_get_dwz_file): Add 'require' parameter.
	(get_abbrev_section_for_cu, read_attribute_value)
	(get_debug_line_section): Update.
	* dwarf2/macro.c (dwarf_decode_macro_bytes): Update.
---
 gdb/ChangeLog      |  8 ++++++++
 gdb/dwarf2/macro.c |  6 ++++--
 gdb/dwarf2/read.c  | 14 +++++++++-----
 gdb/dwarf2/read.h  | 13 ++++++++-----
 4 files changed, 29 insertions(+), 12 deletions(-)

-- 
2.26.2

Comments

Tom Tromey Feb. 21, 2021, 4:23 p.m. | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:


Tom> If DWARF contains a reference to a "dwz" file, but there is no
Tom> .gnu_debugaltlink section, then gdb will crash.  This happens because
Tom> dwarf2_get_dwz_file will return NULL, but some callers do not expect
Tom> this.

I didn't write a test for this, but it should probably have one, so I
will.

Tom
Tom Tromey Feb. 21, 2021, 5:19 p.m. | #2
Tom> I didn't write a test for this, but it should probably have one, so I
Tom> will.

Here's the updated patch.

Tom

commit be6b917e5997b2c5ed0ced04f0363e02e7e430bd
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Feb 20 19:41:14 2021 -0700

    Avoid crash on missing dwz file
    
    If DWARF contains a reference to a "dwz" file, but there is no
    .gnu_debugaltlink section, then gdb will crash.  This happens because
    dwarf2_get_dwz_file will return NULL, but some callers do not expect
    this.
    
    This patch changes dwarf2_get_dwz_file so that callers can require a
    dwz file.  Then, it updates the callers that are attempting to process
    references to the dwz file to require one.
    
    This includes a new testcase.  The dwarf.exp changes don't handle the
    new forms exactly correctly -- they are only handled well enough to
    let this test case complete.
    
    gdb/ChangeLog
    2021-02-20  Tom Tromey  <tom@tromey.com>
    
            * dwarf2/read.h (dwarf2_get_dwz_file): Add 'require' parameter.
            * dwarf2/read.c (dwarf2_get_dwz_file): Add 'require' parameter.
            (get_abbrev_section_for_cu, read_attribute_value)
            (get_debug_line_section): Update.
            * dwarf2/macro.c (dwarf_decode_macro_bytes): Update.
    
    gdb/testsuite/ChangeLog
    2021-02-21  Tom Tromey  <tom@tromey.com>
    
            * lib/dwarf.exp (_handle_DW_FORM): Treat DW_FORM_GNU_ref_alt and
            DW_FORM_GNU_strp_alt like DW_FORM_sec_offset.
            * gdb.dwarf2/dwznolink.exp: New file.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index c9e1d0f4b02..89ca0daf76e 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,11 @@
+2021-02-20  Tom Tromey  <tom@tromey.com>
+
+	* dwarf2/read.h (dwarf2_get_dwz_file): Add 'require' parameter.
+	* dwarf2/read.c (dwarf2_get_dwz_file): Add 'require' parameter.
+	(get_abbrev_section_for_cu, read_attribute_value)
+	(get_debug_line_section): Update.
+	* dwarf2/macro.c (dwarf_decode_macro_bytes): Update.
+
 2021-02-20  Tom Tromey  <tom@tromey.com>
 
 	* dwarf2/sect-names.h (struct dwarf2_section_names) <matches>: New
diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index afe2f91168b..2ecebe6173c 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -509,7 +509,8 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 		    || macinfo_type == DW_MACRO_undef_sup
 		    || section_is_dwz)
 		  {
-		    dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+		    dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
+							 true);
 
 		    body = dwz->read_string (objfile, str_offset);
 		  }
@@ -693,7 +694,8 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 
 	    if (macinfo_type == DW_MACRO_import_sup)
 	      {
-		dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+		dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
+						     true);
 
 		dwz->macro.read (objfile);
 
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 0347f91947e..6a755f716d1 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -2279,7 +2279,7 @@ dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
 /* See dwarf2read.h.  */
 
 struct dwz_file *
-dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
+dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
 {
   bfd_size_type buildid_len_arg;
   size_t buildid_len;
@@ -2295,7 +2295,11 @@ dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
   if (data == NULL)
     {
       if (bfd_get_error () == bfd_error_no_error)
-	return NULL;
+	{
+	  if (!require)
+	    return nullptr;
+	  error (_("could not read '.gnu_debugaltlink' section"));
+	}
       error (_("could not read '.gnu_debugaltlink' section: %s"),
 	     bfd_errmsg (bfd_get_error ()));
     }
@@ -6300,7 +6304,7 @@ get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
   dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
 
   if (this_cu->is_dwz)
-    abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
+    abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
   else
     abbrev = &per_bfd->abbrev;
 
@@ -20515,7 +20519,7 @@ read_attribute_value (const struct die_reader_specs *reader,
       /* FALLTHROUGH */
     case DW_FORM_GNU_strp_alt:
       {
-	dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+	dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
 	LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
 						     &bytes_read);
 
@@ -21113,7 +21117,7 @@ get_debug_line_section (struct dwarf2_cu *cu)
     section = &cu->dwo_unit->dwo_file->sections.line;
   else if (cu->per_cu->is_dwz)
     {
-      dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+      dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
 
       section = &dwz->line;
     }
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index d2bae5a7ee8..86ac6b50cc0 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -633,11 +633,14 @@ struct signatured_type
   struct dwo_unit *dwo_unit;
 };
 
-/* Open the separate '.dwz' debug file, if needed.  Return NULL if
-   there is no .gnu_debugaltlink section in the file.  Error if there
-   is such a section but the file cannot be found.  */
-
-extern dwz_file *dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd);
+/* Open the separate '.dwz' debug file, if needed.  If there is no
+   .gnu_debugaltlink section in the file, then the result depends on
+   REQUIRE: if REQUIRE is true, then error; if REQUIRE is false,
+   return NULL.  Always error if there is such a section but the file
+   cannot be found.  */
+
+extern dwz_file *dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd,
+				      bool require = false);
 
 /* Return the type of the DIE at DIE_OFFSET in the CU named by
    PER_CU.  */
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 70b836b8c2f..27f475525ac 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2021-02-21  Tom Tromey  <tom@tromey.com>
+
+	* lib/dwarf.exp (_handle_DW_FORM): Treat DW_FORM_GNU_ref_alt and
+	DW_FORM_GNU_strp_alt like DW_FORM_sec_offset.
+	* gdb.dwarf2/dwznolink.exp: New file.
+
 2021-02-18  Andrew Burgess  <andrew.burgess@embecosm.com>
 
 	* gdb.arch/i386-biarch-core.exp: Add target check.
diff --git a/gdb/testsuite/gdb.dwarf2/dwznolink.exp b/gdb/testsuite/gdb.dwarf2/dwznolink.exp
new file mode 100644
index 00000000000..98976d7e59c
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dwznolink.exp
@@ -0,0 +1,60 @@
+# Copyright 2021 Free Software Foundation, Inc.
+
+# This program 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 3 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, see <http://www.gnu.org/licenses/>.
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if {![dwarf2_support]} {
+    return 0
+}
+
+# No remote host testing either.
+if {[is_remote host]} {
+    return 0
+}
+
+standard_testfile main.c dwznolink.S
+
+set asm_file [standard_output_file $srcfile2]
+
+# The DWARF should contain a reference to a supplementary ("dwz")
+# file, but the section that links to the file should be missing.  At
+# one point, this caused gdb crashes.
+Dwarf::assemble $asm_file {
+    cu {} {
+	compile_unit {{language @DW_LANG_C}} {
+	    constant {
+		{name 0 DW_FORM_GNU_strp_alt}
+		{type 97 DW_FORM_GNU_ref_alt}
+		{const_value 99 data1}
+	    }
+	}
+    }
+}
+
+# We can't use prepare_for_testing here because we need to check the
+# 'file' command's output.
+if {[build_executable $testfile.exp $testfile \
+	 [list $srcfile $asm_file] {nodebug quiet}]} {
+    return -1
+}
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+
+gdb_test "file -readnow $binfile" \
+    "could not read '.gnu_debugaltlink' section" \
+    "file $testfile"
diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp
index c1c07be0b98..f8fbd381810 100644
--- a/gdb/testsuite/lib/dwarf.exp
+++ b/gdb/testsuite/lib/dwarf.exp
@@ -456,6 +456,8 @@ namespace eval Dwarf {
 		_op .${size}byte $value
 	    }
 
+	    DW_FORM_GNU_ref_alt -
+	    DW_FORM_GNU_strp_alt -
 	    DW_FORM_sec_offset {
 		variable _cu_offset_size
 		_op .${_cu_offset_size}byte $value
@@ -553,8 +555,6 @@ namespace eval Dwarf {
 
 	    DW_FORM_GNU_addr_index -
 	    DW_FORM_GNU_str_index -
-	    DW_FORM_GNU_ref_alt -
-	    DW_FORM_GNU_strp_alt -
 
 	    default {
 		error "unhandled form $form"

Patch

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index afe2f91168b..2ecebe6173c 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -509,7 +509,8 @@  dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 		    || macinfo_type == DW_MACRO_undef_sup
 		    || section_is_dwz)
 		  {
-		    dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+		    dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
+							 true);
 
 		    body = dwz->read_string (objfile, str_offset);
 		  }
@@ -693,7 +694,8 @@  dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 
 	    if (macinfo_type == DW_MACRO_import_sup)
 	      {
-		dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+		dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
+						     true);
 
 		dwz->macro.read (objfile);
 
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 0347f91947e..6a755f716d1 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -2279,7 +2279,7 @@  dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
 /* See dwarf2read.h.  */
 
 struct dwz_file *
-dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
+dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
 {
   bfd_size_type buildid_len_arg;
   size_t buildid_len;
@@ -2295,7 +2295,11 @@  dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
   if (data == NULL)
     {
       if (bfd_get_error () == bfd_error_no_error)
-	return NULL;
+	{
+	  if (!require)
+	    return nullptr;
+	  error (_("could not read '.gnu_debugaltlink' section"));
+	}
       error (_("could not read '.gnu_debugaltlink' section: %s"),
 	     bfd_errmsg (bfd_get_error ()));
     }
@@ -6300,7 +6304,7 @@  get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
   dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
 
   if (this_cu->is_dwz)
-    abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
+    abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
   else
     abbrev = &per_bfd->abbrev;
 
@@ -20515,7 +20519,7 @@  read_attribute_value (const struct die_reader_specs *reader,
       /* FALLTHROUGH */
     case DW_FORM_GNU_strp_alt:
       {
-	dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+	dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
 	LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
 						     &bytes_read);
 
@@ -21113,7 +21117,7 @@  get_debug_line_section (struct dwarf2_cu *cu)
     section = &cu->dwo_unit->dwo_file->sections.line;
   else if (cu->per_cu->is_dwz)
     {
-      dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
+      dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
 
       section = &dwz->line;
     }
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index d2bae5a7ee8..86ac6b50cc0 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -633,11 +633,14 @@  struct signatured_type
   struct dwo_unit *dwo_unit;
 };
 
-/* Open the separate '.dwz' debug file, if needed.  Return NULL if
-   there is no .gnu_debugaltlink section in the file.  Error if there
-   is such a section but the file cannot be found.  */
-
-extern dwz_file *dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd);
+/* Open the separate '.dwz' debug file, if needed.  If there is no
+   .gnu_debugaltlink section in the file, then the result depends on
+   REQUIRE: if REQUIRE is true, then error; if REQUIRE is false,
+   return NULL.  Always error if there is such a section but the file
+   cannot be found.  */
+
+extern dwz_file *dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd,
+				      bool require = false);
 
 /* Return the type of the DIE at DIE_OFFSET in the CU named by
    PER_CU.  */