Bug 29367 - [gdb, debug-types, gdb-index] Bad CU index complaint not triggered
Summary: [gdb, debug-types, gdb-index] Bad CU index complaint not triggered
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-14 13:35 UTC by Tom de Vries
Modified: 2022-07-21 09:00 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2022-07-14 13:35:02 UTC
[ .gdb_index variant of PR29336. ]

Do this change in the test-suite:
...
diff --git a/gdb/testsuite/gdb.ada/access_tagged_param.exp b/gdb/testsuite/gdb.ada/access_t
agged_param.exp
index 2b8e8ef172f..9c2b1871819 100644
--- a/gdb/testsuite/gdb.ada/access_tagged_param.exp
+++ b/gdb/testsuite/gdb.ada/access_tagged_param.exp
@@ -22,7 +22,7 @@ if { [skip_ada_tests] } { return -1 }
 
 standard_ada_testfile foo
 
-if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug]] != "" } {
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug additional_flags=-fdebug-types-section]] != "" } {
   return -1
 }
 
...
and run the test-case with target board cc-with-gdb-index.

Verify that there are no complaints:
...
$ gdb -q -batch -iex "set complaints 100" ./outputs/gdb.ada/access_tagged_param/foo -ex "b foo"
Breakpoint 1 at 0x4023f4: file /home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/access_tagged_param/foo.adb, line 17.
...

Observe using readelf -w that nr_cus == 56 and nr_tus == 10.

Now hack gdb:
...
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index e554bc4f642..b3f0f5506c8 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -2996,6 +2996,7 @@ dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
       int attrs_valid = (index.version >= 7
                         && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
 
+      cu_index = 56;
       /* Don't crash on bad data.  */
       if (cu_index >= per_objfile->per_bfd->all_comp_units.size (CUTU))
        {
...

Try again to see any complaints. Still none.

Now do:
...
       /* Don't crash on bad data.  */
-      if (cu_index >= per_objfile->per_bfd->all_comp_units.size (CUTU))
+      if (cu_index >= per_objfile->per_bfd->all_comp_units.size (CU))
...
[ assuming tentative patch for PR29336. ]

Try again:
...
$ gdb -q -batch -iex "set complaints 100" ./outputs/gdb.ada/access_tagged_param/foo -ex "b foo"
During symbol reading: .gdb_index entry has bad CU index [in module /home/vries/gdb_versions/devel/build/gdb/testsuite/outputs/gdb.ada/access_tagged_param/foo]
During symbol reading: .gdb_index entry has bad CU index [in module /home/vries/gdb_versions/devel/build/gdb/testsuite/outputs/gdb.ada/access_tagged_param/foo]
Breakpoint 1 at 0x4023f4: file /home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/access_tagged_param/foo.adb, line 17.
...
Bingo.

Now the question: is this change correct?

I can't figure out how a type in a .debug_types section would be addressed from the index.

Maybe 56 is the correct way to address TU 0 ?

I can't tell from the readelf output. Looking at the first type in .debug_types,  ada_main__local_interrupt_states___PAD, we find it back in the index here:
...
[2574] ada_main__local_interrupt_states___PAD: 0 [static, type]
...
It's says zero here, but how is CU id 0 distinguished from TU id 0?
Comment 1 Tom de Vries 2022-07-21 09:00:10 UTC
(In reply to Tom de Vries from comment #0)
> Now the question: is this change correct?
> 
> I can't figure out how a type in a .debug_types section would be addressed
> from the index.
> 
> Maybe 56 is the correct way to address TU 0 ?

At https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html I find:
...
Note that if there are type CUs, then conceptually CUs and type CUs form a single list for the purposes of CU indices.
...

I guess that answers my question.

So this:
...
       /* Don't crash on bad data.  */
-      if (cu_index >= per_objfile->per_bfd->all_comp_units.size (CUTU))
+      if (cu_index >= per_objfile->per_bfd->all_comp_units.size (CU))
...
is incorrect, the current code is in fact correct.

But we might be able to be more strict though:
...
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index bcd01107377..8d216318c13 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -2975,7 +2975,13 @@ dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
                         && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
 
       /* Don't crash on bad data.  */
-      if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
+      size_t nr_tus = per_objfile->per_bfd->tu_stats.nr_tus;
+      size_t nr_cus = (per_objfile->per_bfd->all_comp_units.size () - nr_tus);
+      /* Only allow type symbols in type units.  */
+      size_t size_for_kind = (symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE
+                             ? nr_cus + nr_tus
+                             : nr_cus);
+      if (cu_index >= size_for_kind)
        {
          complaint (_(".gdb_index entry has bad CU index"
                       " [in module %s]"), objfile_name (per_objfile->objfile));
...
by assuming there are only type symbols in the type units, which means for other symbols we can be more strict about the allowed indices.