[05/11,gdb/macros] Work around a gcc PR fixed in gcc 9

Message ID 20240521154415.9543-5-tdevries@suse.de
State New
Headers
Series [01/11,gdb/testsuite] Add gdb.base/fission-macro.exp |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom de Vries May 21, 2024, 3:44 p.m. UTC
  When running test-case gdb.base/fission-macro-i.exp with gcc 8, we run into:
...
(gdb) set complaints 0^M
Offset from DW_FORM_GNU_str_index or DW_FORM_strx pointing outside of \
  .debug_str.dwo section in CU at offset 0x0 \
  [in module fission-macro-i-v5-b32-s0]^M
(gdb) FAIL: gdb.base/fission-macro-i.exp: dwarf_version=5: dwarf_bits=32: \
  strict_dwarf=0: set complaints 0
...

We're using dwarf5, so the .debug_str_offsets section is supposed to have a
header, but there's none:
...
        .section        .debug_str_offsets.dwo,"e",@progbits
        .long   0       # indexed string 0x0: GNU C17 8.2.1 20180831 ...
        .long   0x87    # indexed string 0x1: FIRST 1
        .long   0x8f    # indexed string 0x2: /home/vries/gdb
        .long   0x9f    # indexed string 0x3: main
        .long   0xa4    # indexed string 0x4: fission-macro-i.i
        .long   0xe1    # indexed string 0x5: SECOND 2
...

In other words, we have a v4-style .debug_str_offsets section.

Workaround this by detecting that it's not a valid v5 header by comparing the
initial length to the the section size, and handling it as a v4 section.

IWBN to enable this workaround only for gcc version < 9, but unfortunately the
workaround is required to correctly read the producer string, so that's not
possible.

Tested on x86_64-linux.

Tested test-case (and gdb.base/fission-macro.exp) using a current gcc trunk
build and gcc 7-14.
---
 gdb/dwarf2/read.c                          | 77 +++++++++++++++-------
 gdb/testsuite/gdb.base/fission-macro-i.exp |  2 -
 2 files changed, 53 insertions(+), 26 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 3c2698d6832..abe128f35df 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -17514,32 +17514,47 @@  read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
       const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
 
       /* Header: Initial length.  */
-      read_initial_length (abfd, p + bytes_read, &bytes_read);
+      LONGEST initial_length
+	= read_initial_length (abfd, p + bytes_read, &bytes_read);
 
-      /* Determine offset_size based on the .debug_str_offsets header.  */
-      const bool dwarf5_is_dwarf64 = bytes_read != 4;
-      offset_size = dwarf5_is_dwarf64 ? 8 : 4;
+      if (initial_length + bytes_read
+	  == reader->dwo_file->sections.str_offsets.size)
+	{
+	  /* Determine offset_size based on the .debug_str_offsets header.  */
+	  const bool dwarf5_is_dwarf64 = bytes_read != 4;
+	  offset_size = dwarf5_is_dwarf64 ? 8 : 4;
 
-      /* Header: Version.  */
-      unsigned version = read_2_bytes (abfd, p + bytes_read);
-      bytes_read += 2;
+	  /* Header: Version.  */
+	  unsigned version = read_2_bytes (abfd, p + bytes_read);
+	  bytes_read += 2;
 
-      if (version <= 4)
-	{
-	  /* We'd like one warning here about ignoring the section, but
-	     because we parse the header more than once (see FIXME above)
-	     we'd have many warnings, so use a complaint instead, which at
-	     least has a limit. */
-	  complaint (_("Section .debug_str_offsets in %s has unsupported"
-		       " version %d, use empty string."),
-		     reader->dwo_file->dwo_name.c_str (), version);
-	  return "";
-	}
+	  if (version <= 4)
+	    {
+	      /* We'd like one warning here about ignoring the section, but
+		 because we parse the header more than once (see FIXME above)
+		 we'd have many warnings, so use a complaint instead, which at
+		 least has a limit. */
+	      complaint (_("Section .debug_str_offsets in %s has unsupported"
+			   " version %d, use empty string."),
+			 reader->dwo_file->dwo_name.c_str (), version);
+	      return "";
+	    }
 
-      /* Header: Padding.  */
-      bytes_read += 2;
+	  /* Header: Padding.  */
+	  bytes_read += 2;
 
-      str_offsets_base = bytes_read;
+	  str_offsets_base = bytes_read;
+	}
+      else
+	{
+	  /* GCC 8 and earlier has a bug that for dwarf5 it produces a
+	     pre-dwarf5 .debug_str_offsets section.  We'd wish we could make
+	     this work-around more precise by checking that producer is gcc and
+	     gcc version <= 8, but that doesn't work if we need this workaround
+	     to read the producer string.  */
+	  offset_size = 4;
+	  str_offsets_base = 0;
+	}
     }
   else
     {
@@ -21249,9 +21264,23 @@  dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
 	{
 	  bfd *abfd = str_offsets_section->get_bfd_owner ();
 	  unsigned int bytes_read = 0;
-	  read_initial_length (abfd, str_offsets_section->buffer, &bytes_read, false);
-	  const bool is_dwarf64 = bytes_read != 4;
-	  str_offsets_base = is_dwarf64 ? 16 : 8;
+	  LONGEST initial_length
+	    = read_initial_length (abfd, str_offsets_section->buffer,
+				   &bytes_read, false);
+	  if ((bytes_read == 4 || bytes_read == 12)
+	      && initial_length + bytes_read == str_offsets_section->size)
+	    {
+	      const bool is_dwarf64 = bytes_read != 4;
+	      str_offsets_base = is_dwarf64 ? 16 : 8;
+	    }
+	  else
+	    {
+	      /* GCC 8 and earlier has a bug that for dwarf5 it produces a
+		 pre-dwarf5 .debug_str_offsets section.  We use the same test
+		 as in read_dwo_str_index.  */
+	      str_offsets_base = 0;
+	      offset_size = 4;
+	    }
 	}
     }
   else
diff --git a/gdb/testsuite/gdb.base/fission-macro-i.exp b/gdb/testsuite/gdb.base/fission-macro-i.exp
index 3121e1d289b..ae211d0e09b 100644
--- a/gdb/testsuite/gdb.base/fission-macro-i.exp
+++ b/gdb/testsuite/gdb.base/fission-macro-i.exp
@@ -13,8 +13,6 @@ 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-require {expr [gcc_major_version] >= 9}
-
 # Use a .i file instead of a .c file, to make sure that we only generate one
 # .debug_macros section, working around gcc PR debug/111409.
 standard_testfile .i -2.i