[gdb/symtab] Fix incomplete CU list assert in .debug_names
authorTom de Vries <tdevries@suse.de>
Mon, 11 May 2020 13:03:54 +0000 (15:03 +0200)
committerTom de Vries <tdevries@suse.de>
Mon, 11 May 2020 13:03:54 +0000 (15:03 +0200)
Consider the following two-file test-case:
...
$ cat main.c
extern int foo (void);

int
main (void)
{
  int sum, a, b;
  sum = a + b + foo ();
  return sum;
}
$ cat foo.c
int
foo (void)
{
  return 3;
}
...

Compiled like this:
...
$ clang-10 -gdwarf-5 -gpubnames -c main.c
$ clang-10 -gdwarf-5 -c foo.c
$ clang-10 -gdwarf-5 -gpubnames main.o foo.o
...

When loading this exec into gdb, we run into this assert:
...
$ gdb a.out
Reading symbols from a.out...

warning: Section .debug_aranges in a.out entry at offset 0 \
  debug_info_offset 0 does not exists, ignoring .debug_aranges.
src/gdb/dwarf2/read.c:6949: \
  internal-error: cutu_reader::cutu_reader(dwarf2_per_cu_data*, \
                              abbrev_table*, int, bool): \
  Assertion `this_cu->length == cu->header.get_length ()' failed.
...

The problem is that the determined length of the CU:
...
(gdb) p /x this_cu->length
$4 = 0x26a
...
does not match the actual length:
...
(gdb) p /x cu->header.get_length ()
$5 = 0x59
...

The length of the CU is determined in create_cus_from_debug_names_list, and
set based on this list in the .debug_names section:
...
  Compilation Unit offsets [
    CU[0]: 0x000000c7
  ]
...
and it is assumed that this is a complete list, so the size of the CU is
calculated using the end of the .debug_section at 0x331, making it 0x331 -
0xc7 == 0x26a.

However, the CU list is not complete:
...
$ llvm-dwarfdump -debug-info a.out \
  | grep "Compile Unit" \
  | sed 's/Compile Unit.*//'
0x00000000:
0x0000002e:
0x000000a5:
0x000000c7:
0x00000120:
0x00000157:
0x0000030f:
...
In particular, because the CU for foo.c is there at 0x120 (the rest of the CUs
is due to openSUSE having debug info for various linked in objects).

Fix the assert by not assuming to know the length of CUs in
create_cus_from_debug_names_list (if the .debug_names is not produced by GDB),
and setting it to 0, and setting it later to the actual length.

Note that this does not fix the .debug_aranges warning, that's PR25969.

Build and tested on x86_64-linux, with native and debug-names.

gdb/ChangeLog:

2020-05-11  Tom de Vries  <tdevries@suse.de>

PR symtab/25941
* dwarf2/read.c (create_cus_from_debug_names_list): Initialize CUs
with length 0, if not gdb-produced.
(cutu_reader::cutu_reader): Set CU length to actual length if 0.

gdb/testsuite/ChangeLog:

2020-05-11  Tom de Vries  <tdevries@suse.de>

PR symtab/25941
* gdb.dwarf2/clang-debug-names.exp.in: New include exp file, factored
out of ...
* gdb.dwarf2/clang-debug-names.exp: ... here.
* gdb.dwarf2/clang-debug-names-2.exp: New file.  Include
clang-debug-names.exp.in.
* gdb.dwarf2/clang-debug-names-2-foo.c: New test.
* gdb.dwarf2/clang-debug-names-2.c: New test.

gdb/ChangeLog
gdb/dwarf2/read.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.dwarf2/clang-debug-names-2-foo.c [new file with mode: 0644]
gdb/testsuite/gdb.dwarf2/clang-debug-names-2.c [new file with mode: 0644]
gdb/testsuite/gdb.dwarf2/clang-debug-names-2.exp [new file with mode: 0644]
gdb/testsuite/gdb.dwarf2/clang-debug-names.exp
gdb/testsuite/gdb.dwarf2/clang-debug-names.exp.in [new file with mode: 0644]

index a132f20dde5944447f4ec41349223a704f506100..3b04b6c8d9158dc3ef2385543227d3d9a35d31b0 100644 (file)
@@ -1,3 +1,10 @@
+2020-05-11  Tom de Vries  <tdevries@suse.de>
+
+       PR symtab/25941
+       * dwarf2/read.c (create_cus_from_debug_names_list): Initialize CUs
+       with length 0, if not gdb-produced.
+       (cutu_reader::cutu_reader): Set CU length to actual length if 0.
+
 2020-05-09  Tom de Vries  <tdevries@suse.de>
 
        PR gdb/25955
index 4c8a0717c785b63126713703d944c4f5261af449..27bf40a898dcad845edf22382d96ac6b34bb9f36 100644 (file)
@@ -5022,6 +5022,26 @@ create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
                                  dwarf2_section_info &section,
                                  bool is_dwz)
 {
+  if (!map.augmentation_is_gdb)
+    {
+    for (uint32_t i = 0; i < map.cu_count; ++i)
+      {
+       sect_offset sect_off
+         = (sect_offset) (extract_unsigned_integer
+                          (map.cu_table_reordered + i * map.offset_size,
+                           map.offset_size,
+                           map.dwarf5_byte_order));
+       /* We don't know the length of the CU, because the CU list in a
+          .debug_names index can be incomplete, so we can't use the start of
+          the next CU as end of this CU.  We create the CUs here with length 0,
+          and in cutu_reader::cutu_reader we'll fill in the actual length.  */
+       dwarf2_per_cu_data *per_cu
+         = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
+                                      sect_off, 0);
+       dwarf2_per_objfile->all_comp_units.push_back (per_cu);
+      }
+    }
+
   sect_offset sect_off_prev;
   for (uint32_t i = 0; i <= map.cu_count; ++i)
     {
@@ -6946,7 +6966,10 @@ cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
                                                    rcuh_kind::COMPILE);
 
          gdb_assert (this_cu->sect_off == cu->header.sect_off);
-         gdb_assert (this_cu->length == cu->header.get_length ());
+         if (this_cu->length == 0)
+           this_cu->length = cu->header.get_length ();
+         else
+           gdb_assert (this_cu->length == cu->header.get_length ());
          this_cu->dwarf_version = cu->header.version;
        }
     }
index c13b651e90cd034e471a4044f929f3547f9c23d7..319d3eb976c94796d1ec415a32f078e28014bb15 100644 (file)
@@ -1,3 +1,14 @@
+2020-05-11  Tom de Vries  <tdevries@suse.de>
+
+       PR symtab/25941
+       * gdb.dwarf2/clang-debug-names.exp.in: New include exp file, factored
+       out of ...
+       * gdb.dwarf2/clang-debug-names.exp: ... here.
+       * gdb.dwarf2/clang-debug-names-2.exp: New file.  Include
+       clang-debug-names.exp.in.
+       * gdb.dwarf2/clang-debug-names-2-foo.c: New test.
+       * gdb.dwarf2/clang-debug-names-2.c: New test.
+
 2020-05-10  Alok Kumar Sharma  <alokkumar.sharma@amd.com>
 
        * lib/fortran.exp (fortran_main): New Proc, handle flang MAIN_,
diff --git a/gdb/testsuite/gdb.dwarf2/clang-debug-names-2-foo.c b/gdb/testsuite/gdb.dwarf2/clang-debug-names-2-foo.c
new file mode 100644 (file)
index 0000000..cabdc7a
--- /dev/null
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 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/>.  */
+
+int
+foo (void)
+{
+  return 3;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/clang-debug-names-2.c b/gdb/testsuite/gdb.dwarf2/clang-debug-names-2.c
new file mode 100644 (file)
index 0000000..e9c52e4
--- /dev/null
@@ -0,0 +1,27 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 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/>.  */
+
+extern int foo (void);
+
+int
+main (void)
+{
+  asm ("main_label: .globl main_label");
+  int sum, a, b;
+  sum = a + b + foo ();
+  return sum;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/clang-debug-names-2.exp b/gdb/testsuite/gdb.dwarf2/clang-debug-names-2.exp
new file mode 100644 (file)
index 0000000..185dddf
--- /dev/null
@@ -0,0 +1,45 @@
+# Copyright 2020 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 clang-debug-names-2.c clang-debug-names-debug-2.S \
+    clang-debug-names-2-foo.c
+
+lassign \
+    [function_range main \
+        "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile3}"] \
+    main_start main_length
+
+set asm_file [standard_output_file $srcfile2]
+source $srcdir/$subdir/clang-debug-names.exp.in
+
+if { [build_executable_from_specs "failed to prepare" ${testfile} "" \
+         $srcfile "nodebug" $asm_file "nodebug" $srcfile3 "debug"] } {
+    return -1
+}
+clean_restart $binfile
+
+set cmd "ptype main"
+set pass_re \
+    [multi_line \
+        $cmd \
+        "type = int \\(\\)"]
+gdb_test $cmd $pass_re
index a6e33c1d8995090fb41511838d097ed73d88089f..b5af898838fba6659ecaa6db8a6dea0b3f10cc13 100644 (file)
@@ -22,115 +22,11 @@ if {![dwarf2_support]} {
 
 standard_testfile clang-debug-names.c clang-debug-names-debug.S
 
-# Set up the DWARF for the test.
-
-set main_str_label [Dwarf::_compute_label info_string3]
-set int_str_label [Dwarf::_compute_label info_string4]
-set main_die_label [Dwarf::_compute_label main_die_label]
-set int_die_label [Dwarf::_compute_label int_die_label]
-
-set debug_str \
-    [list \
-         "$main_str_label:" \
-         "  .asciz \"main\"" \
-         "$int_str_label:" \
-         "  .asciz \"int\""]
-
-set debug_names \
-    [list \
-         "  .4byte  .Ldebug_names_end - .Ldebug_names_start" \
-         ".Ldebug_names_start:" \
-         "  .short 5                      # Header: version" \
-         "  .short 0                      # Header: padding" \
-         "  .long 1                       # Header: compilation unit count" \
-         "  .long 0                       # Header: local type unit count" \
-         "  .long 0                       # Header: foreign type unit count" \
-         "  .long 2                       # Header: bucket count" \
-         "  .long 2                       # Header: name count" \
-         "  .long .Lnames_abbrev_end0-.Lnames_abbrev_start0 " \
-         "                                # Header: abbreviation table size" \
-         "  .long 8                       # Header: augmentation string size" \
-         "  .ascii \"LLVM0700\"    # Header: augmentation string" \
-         "  .long .Lcu1_begin             # Compilation unit 0" \
-         "  .long 1                       # Bucket 0" \
-         "  .long 0                       # Bucket 1" \
-         "  .long 193495088               # Hash in Bucket 0" \
-         "  .long 2090499946              # Hash in Bucket 0" \
-         "  .long $int_str_label          # String in Bucket 0: int" \
-         "  .long $main_str_label         # String in Bucket 0: main" \
-         "  .long .Lnames1-.Lnames_entries0 # Offset in Bucket 0" \
-         "  .long .Lnames0-.Lnames_entries0 # Offset in Bucket 0" \
-         ".Lnames_abbrev_start0:" \
-         "  .byte 46                      # Abbrev code" \
-         "  .byte 46                      # DW_TAG_subprogram" \
-         "  .byte 3                       # DW_IDX_die_offset" \
-         "  .byte 19                      # DW_FORM_ref4" \
-         "  .byte 0                       # End of abbrev" \
-         "  .byte 0                       # End of abbrev" \
-         "  .byte 36                      # Abbrev code" \
-         "  .byte 36                      # DW_TAG_base_type" \
-         "  .byte 3                       # DW_IDX_die_offset" \
-         "  .byte 19                      # DW_FORM_ref4" \
-         "  .byte 0                       # End of abbrev" \
-         "  .byte 0                       # End of abbrev" \
-         "  .byte 0                       # End of abbrev list" \
-         ".Lnames_abbrev_end0:" \
-         ".Lnames_entries0:" \
-         ".Lnames1:" \
-         "  .byte 36                      # Abbreviation code" \
-         "  .long $int_die_label - .Lcu1_begin # DW_IDX_die_offset" \
-         "  .long 0                       # End of list: int" \
-         ".Lnames0:" \
-         "  .byte 46                      # Abbreviation code" \
-         "  .long $main_die_label - .Lcu1_begin # DW_IDX_die_offset" \
-         "  .long 0                       # End of list: main" \
-         "  .p2align 2" \
-         ".Ldebug_names_end:"]
+lassign [function_range main ${srcdir}/${subdir}/${srcfile}] \
+    main_start main_length
 
 set asm_file [standard_output_file $srcfile2]
-Dwarf::assemble $asm_file {
-    global srcdir subdir srcfile
-
-    lassign [function_range main ${srcdir}/${subdir}/${srcfile}] \
-       main_start main_length
-
-    cu {} {
-       DW_TAG_compile_unit {
-                {DW_AT_language @DW_LANG_C}
-                {DW_AT_name     clang-debug-names.c}
-                {DW_AT_comp_dir /tmp}
-
-        } {
-           global int_die_label
-           global main_die_label
-
-           define_label $int_die_label
-           base_type {
-               {name "int"}
-               {encoding @DW_ATE_signed}
-               {byte_size 4 DW_FORM_sdata}
-           }
-
-           define_label $main_die_label
-           subprogram {
-               {name main}
-               {type :$int_die_label}
-               {low_pc $main_start addr}
-               {high_pc "$main_start + $main_length" addr}
-           }
-       }
-    }
-
-    _defer_output .debug_str {
-       global debug_str
-       _emit [join $debug_str "\n"]
-    }
-
-    _defer_output .debug_names {
-       global debug_names
-       _emit [join $debug_names "\n"]
-    }
-}
+source $srcdir/$subdir/clang-debug-names.exp.in
 
 if { [prepare_for_testing "failed to prepare" ${testfile} \
          [list $srcfile $asm_file] {nodebug}] } {
diff --git a/gdb/testsuite/gdb.dwarf2/clang-debug-names.exp.in b/gdb/testsuite/gdb.dwarf2/clang-debug-names.exp.in
new file mode 100644 (file)
index 0000000..39e0e1e
--- /dev/null
@@ -0,0 +1,121 @@
+# Copyright 2020 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/>.
+
+# Set up the DWARF for the test.
+
+set main_str_label [Dwarf::_compute_label info_string3]
+set int_str_label [Dwarf::_compute_label info_string4]
+set main_die_label [Dwarf::_compute_label main_die_label]
+set int_die_label [Dwarf::_compute_label int_die_label]
+
+set debug_str \
+    [list \
+         "$main_str_label:" \
+         "  .asciz \"main\"" \
+         "$int_str_label:" \
+         "  .asciz \"int\""]
+
+set debug_names \
+    [list \
+         "  .4byte  .Ldebug_names_end - .Ldebug_names_start" \
+         ".Ldebug_names_start:" \
+         "  .short 5                      # Header: version" \
+         "  .short 0                      # Header: padding" \
+         "  .long 1                       # Header: compilation unit count" \
+         "  .long 0                       # Header: local type unit count" \
+         "  .long 0                       # Header: foreign type unit count" \
+         "  .long 2                       # Header: bucket count" \
+         "  .long 2                       # Header: name count" \
+         "  .long .Lnames_abbrev_end0-.Lnames_abbrev_start0 " \
+         "                                # Header: abbreviation table size" \
+         "  .long 8                       # Header: augmentation string size" \
+         "  .ascii \"LLVM0700\"    # Header: augmentation string" \
+         "  .long .Lcu1_begin             # Compilation unit 0" \
+         "  .long 1                       # Bucket 0" \
+         "  .long 0                       # Bucket 1" \
+         "  .long 193495088               # Hash in Bucket 0" \
+         "  .long 2090499946              # Hash in Bucket 0" \
+         "  .long $int_str_label          # String in Bucket 0: int" \
+         "  .long $main_str_label         # String in Bucket 0: main" \
+         "  .long .Lnames1-.Lnames_entries0 # Offset in Bucket 0" \
+         "  .long .Lnames0-.Lnames_entries0 # Offset in Bucket 0" \
+         ".Lnames_abbrev_start0:" \
+         "  .byte 46                      # Abbrev code" \
+         "  .byte 46                      # DW_TAG_subprogram" \
+         "  .byte 3                       # DW_IDX_die_offset" \
+         "  .byte 19                      # DW_FORM_ref4" \
+         "  .byte 0                       # End of abbrev" \
+         "  .byte 0                       # End of abbrev" \
+         "  .byte 36                      # Abbrev code" \
+         "  .byte 36                      # DW_TAG_base_type" \
+         "  .byte 3                       # DW_IDX_die_offset" \
+         "  .byte 19                      # DW_FORM_ref4" \
+         "  .byte 0                       # End of abbrev" \
+         "  .byte 0                       # End of abbrev" \
+         "  .byte 0                       # End of abbrev list" \
+         ".Lnames_abbrev_end0:" \
+         ".Lnames_entries0:" \
+         ".Lnames1:" \
+         "  .byte 36                      # Abbreviation code" \
+         "  .long $int_die_label - .Lcu1_begin # DW_IDX_die_offset" \
+         "  .long 0                       # End of list: int" \
+         ".Lnames0:" \
+         "  .byte 46                      # Abbreviation code" \
+         "  .long $main_die_label - .Lcu1_begin # DW_IDX_die_offset" \
+         "  .long 0                       # End of list: main" \
+         "  .p2align 2" \
+         ".Ldebug_names_end:"]
+
+Dwarf::assemble $asm_file {
+    global srcdir subdir srcfile
+    global main_start main_length
+
+    cu {} {
+       DW_TAG_compile_unit {
+                {DW_AT_language @DW_LANG_C}
+                {DW_AT_name     clang-debug-names.c}
+                {DW_AT_comp_dir /tmp}
+
+        } {
+           global int_die_label
+           global main_die_label
+
+           define_label $int_die_label
+           base_type {
+               {name "int"}
+               {encoding @DW_ATE_signed}
+               {byte_size 4 DW_FORM_sdata}
+           }
+
+           define_label $main_die_label
+           subprogram {
+               {name main}
+               {type :$int_die_label}
+               {low_pc $main_start addr}
+               {high_pc "$main_start + $main_length" addr}
+           }
+       }
+    }
+
+    _defer_output .debug_str {
+       global debug_str
+       _emit [join $debug_str "\n"]
+    }
+
+    _defer_output .debug_names {
+       global debug_names
+       _emit [join $debug_names "\n"]
+    }
+}