diff mbox

Don't crash on dummy CUs

Message ID 001a11c301ba256356051b42e72e@google.com
State New
Headers show

Commit Message

Doug Evans July 19, 2015, 11:37 p.m. UTC
Hi.

This patch fixes a crash when loading dummy CUs.
Dummy CUs are used to add filler for the incremental linker,
and consist of a CU header but no dies.

2015-07-19  Doug Evans  <dje@google.com>

	* dwarf2read.c (dwarf2_per_cu_data): Add comment.
	(load_cu): Handle dummy CUs.
	(dw2_do_instantiate_symtab, process_queuef): Ditto.
	(dwarf2_fetch_die_loc_sect_off, dwarf2_fetch_constant_bytes): Ditto.

	testsuite/
	* gdb.dwarf2/dw2-dummy-cu.S: New file.
	* gdb.dwarf2/dw2-dummy-cu.exp: New file.

+
+set GDBFLAGS $saved_gdbflags

Comments

Yao Qi July 20, 2015, 1:06 p.m. UTC | #1
Doug Evans <dje@google.com> writes:

> +	.text
> +main:
> +	.4byte	0
> +main_end:
> +
> +	.section	.debug_info
> +	.4byte	.Ldebug_info_end - 1f	/* Length of Compilation Unit Info */
> +1:
> +	.2byte	0x2	/* DWARF version number */
> +	.4byte	.Ldebug_abbrev0	/* Offset Into Abbrev. Section */
> +	.byte	0x4	/* Pointer Size (in bytes) */
> +
> +	/* Nothing else, this is a dummy die.  */
> +.Ldebug_info_end:
> +
> +	.section	.debug_abbrev
> +.Ldebug_abbrev0:
> +	.byte	0x0

Hi Doug,
Can we generate this file by dwarf assembler?  I don't know much about
dwarf2read.c, so can't give any comments useful to changes there.
Doug Evans July 21, 2015, 2:43 p.m. UTC | #2
On Mon, Jul 20, 2015 at 6:06 AM, Yao Qi <qiyaoltc@gmail.com> wrote:
>
> Doug Evans <dje@google.com> writes:
>
> > +     .text
> > +main:
> > +     .4byte  0
> > +main_end:
> > +
> > +     .section        .debug_info
> > +     .4byte  .Ldebug_info_end - 1f   /* Length of Compilation Unit Info */
> > +1:
> > +     .2byte  0x2     /* DWARF version number */
> > +     .4byte  .Ldebug_abbrev0 /* Offset Into Abbrev. Section */
> > +     .byte   0x4     /* Pointer Size (in bytes) */
> > +
> > +     /* Nothing else, this is a dummy die.  */
> > +.Ldebug_info_end:
> > +
> > +     .section        .debug_abbrev
> > +.Ldebug_abbrev0:
> > +     .byte   0x0
>
> Hi Doug,
> Can we generate this file by dwarf assembler?  I don't know much about
> dwarf2read.c, so can't give any comments useful to changes there.
>

There's no real value to using the dwarf assembler here, it's an
intentionally empty CU.
Doug Evans July 23, 2015, 4:28 p.m. UTC | #3
On Tue, Jul 21, 2015 at 7:43 AM, Doug Evans <dje@google.com> wrote:
> On Mon, Jul 20, 2015 at 6:06 AM, Yao Qi <qiyaoltc@gmail.com> wrote:
>>
>> Doug Evans <dje@google.com> writes:
>>
>> > +     .text
>> > +main:
>> > +     .4byte  0
>> > +main_end:
>> > +
>> > +     .section        .debug_info
>> > +     .4byte  .Ldebug_info_end - 1f   /* Length of Compilation Unit Info */
>> > +1:
>> > +     .2byte  0x2     /* DWARF version number */
>> > +     .4byte  .Ldebug_abbrev0 /* Offset Into Abbrev. Section */
>> > +     .byte   0x4     /* Pointer Size (in bytes) */
>> > +
>> > +     /* Nothing else, this is a dummy die.  */
>> > +.Ldebug_info_end:
>> > +
>> > +     .section        .debug_abbrev
>> > +.Ldebug_abbrev0:
>> > +     .byte   0x0
>>
>> Hi Doug,
>> Can we generate this file by dwarf assembler?  I don't know much about
>> dwarf2read.c, so can't give any comments useful to changes there.
>>
>
> There's no real value to using the dwarf assembler here, it's an
> intentionally empty CU.

[fyi]
Committed.
diff mbox

Patch

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index f440956..24a4022 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -610,7 +610,8 @@  struct dwarf2_per_cu_data
    struct dwarf2_section_info *section;

    /* Set to non-NULL iff this CU is currently loaded.  When it gets freed  
out
-     of the CU cache it gets reset to NULL again.  */
+     of the CU cache it gets reset to NULL again.  This is left as NULL for
+     dummy CUs (a CU header, but nothing else).  */
    struct dwarf2_cu *cu;

    /* The corresponding objfile.
@@ -2655,7 +2656,8 @@  load_cu (struct dwarf2_per_cu_data *per_cu)
    else
      load_full_comp_unit (per_cu, language_minimal);

-  gdb_assert (per_cu->cu != NULL);
+  if (per_cu->cu == NULL)
+    return;  /* Dummy CU.  */

    dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
  }
@@ -2685,6 +2687,7 @@  dw2_do_instantiate_symtab (struct dwarf2_per_cu_data  
*per_cu)
  	 that may badly handle TUs, load all the TUs in that DWO as well.
  	 http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */
        if (!per_cu->is_debug_types
+	  && per_cu->cu != NULL
  	  && per_cu->cu->dwo_unit != NULL
  	  && dwarf2_per_objfile->index_table != NULL
  	  && dwarf2_per_objfile->index_table->version <= 7
@@ -7544,9 +7547,11 @@  process_queue (void)
       may load a new CU, adding it to the end of the queue.  */
    for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
      {
-      if (dwarf2_per_objfile->using_index
-	  ? !item->per_cu->v.quick->compunit_symtab
-	  : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
+      if ((dwarf2_per_objfile->using_index
+	   ? !item->per_cu->v.quick->compunit_symtab
+	   : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
+	  /* Skip dummy CUs.  */
+	  && item->per_cu->cu != NULL)
  	{
  	  struct dwarf2_per_cu_data *per_cu = item->per_cu;
  	  unsigned int debug_print_threshold;
@@ -20017,6 +20022,13 @@  dwarf2_fetch_die_loc_sect_off (sect_offset offset,
    if (per_cu->cu == NULL)
      load_cu (per_cu);
    cu = per_cu->cu;
+  if (cu == NULL)
+    {
+      /* We shouldn't get here for a dummy CU, but don't crash on the user.
+	 Instead just throw an error, not much else we can do.  */
+      error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
+	     offset.sect_off, objfile_name (per_cu->objfile));
+    }

    die = follow_die_offset (offset, per_cu->is_dwz, &cu);
    if (!die)
@@ -20118,6 +20130,13 @@  dwarf2_fetch_constant_bytes (sect_offset offset,
    if (per_cu->cu == NULL)
      load_cu (per_cu);
    cu = per_cu->cu;
+  if (cu == NULL)
+    {
+      /* We shouldn't get here for a dummy CU, but don't crash on the user.
+	 Instead just throw an error, not much else we can do.  */
+      error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
+	     offset.sect_off, objfile_name (per_cu->objfile));
+    }

    die = follow_die_offset (offset, per_cu->is_dwz, &cu);
    if (!die)
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-dummy-cu.S  
b/gdb/testsuite/gdb.dwarf2/dw2-dummy-cu.S
new file mode 100644
index 0000000..fa31561
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-dummy-cu.S
@@ -0,0 +1,33 @@ 
+/* Copyright 2015 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/>.   
*/
+
+	.text
+main:
+	.4byte	0
+main_end:
+
+	.section	.debug_info
+	.4byte	.Ldebug_info_end - 1f	/* Length of Compilation Unit Info */
+1:
+	.2byte	0x2	/* DWARF version number */
+	.4byte	.Ldebug_abbrev0	/* Offset Into Abbrev. Section */
+	.byte	0x4	/* Pointer Size (in bytes) */
+
+	/* Nothing else, this is a dummy die.  */
+.Ldebug_info_end:
+
+	.section	.debug_abbrev
+.Ldebug_abbrev0:
+	.byte	0x0
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-dummy-cu.exp  
b/gdb/testsuite/gdb.dwarf2/dw2-dummy-cu.exp
new file mode 100644
index 0000000..98120e4
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-dummy-cu.exp
@@ -0,0 +1,39 @@ 
+# Copyright 2015 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
+}
+
+standard_testfile .S
+set executable ${testfile}
+
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object  
{}] != "" } {
+    return -1
+}
+
+set saved_gdbflags $GDBFLAGS
+set GDBFLAGS "$GDBFLAGS -readnow"
+
+clean_restart $executable
+
+# Something simple to verify gdb didn't crash, and has read in whatever  
symbol
+# info is there.
+gdb_test "info fun main" "main_end"