From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id BE8B33858D28; Thu, 21 Jul 2022 11:05:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE8B33858D28 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/symtab] Fix bad compile unit index complaint X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: ea09fe92596fbb2a5da4595459bfe8b23789ec8d X-Git-Newrev: 2fe9a3c41faa71b1f2bde40e818187491a5a9925 Message-Id: <20220721110542.BE8B33858D28@sourceware.org> Date: Thu, 21 Jul 2022 11:05:42 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jul 2022 11:05:42 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D2fe9a3c41faa= 71b1f2bde40e818187491a5a9925 commit 2fe9a3c41faa71b1f2bde40e818187491a5a9925 Author: Tom de Vries Date: Thu Jul 21 13:05:39 2022 +0200 [gdb/symtab] Fix bad compile unit index complaint =20 I noticed this code in dw2_debug_names_iterator::next: ... case DW_IDX_compile_unit: /* Don't crash on bad data. */ if (ull >=3D per_bfd->all_comp_units.size ()) { complaint (_(".debug_names entry has bad CU index %s" " [in module %s]"), pulongest (ull), objfile_name (objfile)); continue; } per_cu =3D per_bfd->get_cu (ull); break; ... =20 This code used to DTRT, before we started keeping both CUs and TUs in all_comp_units. =20 Fix by using "per_bfd->all_comp_units.size () - per_bfd->tu_stats.nr_tu= s" instead. =20 It's hard to produce a test-case for this, but let's try at least to tr= igger the complaint somehow. We start out by creating an exec with .debug_ty= pes and .debug_names: ... $ gcc -g ~/hello.c -fdebug-types-section $ gdb-add-index -dwarf-5 a.out ... and verify that we don't see any complaints: ... $ gdb -q -batch -iex "set complaints 100" ./a.out ... =20 We look at the CU and TU table using readelf -w and conclude that we ha= ve nr_cus =3D=3D 6 and nr_tus =3D=3D 1. =20 Now override ull in dw2_debug_names_iterator::next for the DW_IDX_compi= le_unit case to 6, and we have: ... $ gdb -q -batch -iex "set complaints 100" ./a.out During symbol reading: .debug_names entry has bad CU index 6 [in module= a.out] ... =20 After this, it still crashes because this code in dw2_debug_names_iterator::next: ... /* Skip if already read in. */ if (m_per_objfile->symtab_set_p (per_cu)) goto again; ... is called with per_cu =3D=3D nullptr. =20 Fix this by skipping the entry if per_cu =3D=3D nullptr. =20 Now revert the fix and observe that the complaint disappears, so we've confirmed that the fix is required. =20 A somewhat similar issue for .gdb_index in dw2_symtab_iter_next has bee= n filed as PR29367. =20 Tested on x86_64-linux, with native and target board cc-with-debug-name= s. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29336 Diff: --- gdb/dwarf2/read.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index bcd01107377..42230607fe0 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -4970,15 +4970,19 @@ dw2_debug_names_iterator::next () switch (attr.dw_idx) { case DW_IDX_compile_unit: - /* Don't crash on bad data. */ - if (ull >=3D per_bfd->all_comp_units.size ()) - { - complaint (_(".debug_names entry has bad CU index %s" - " [in module %s]"), - pulongest (ull), - objfile_name (objfile)); - continue; - } + { + /* Don't crash on bad data. */ + int nr_cus =3D (per_bfd->all_comp_units.size () + - per_bfd->tu_stats.nr_tus); + if (ull >=3D nr_cus) + { + complaint (_(".debug_names entry has bad CU index %s" + " [in module %s]"), + pulongest (ull), + objfile_name (objfile)); + continue; + } + } per_cu =3D per_bfd->get_cu (ull); break; case DW_IDX_type_unit: @@ -5016,6 +5020,10 @@ dw2_debug_names_iterator::next () } } =20 + /* Skip if we couldn't find a valid CU/TU index. */ + if (per_cu =3D=3D nullptr) + goto again; + /* Skip if already read in. */ if (m_per_objfile->symtab_set_p (per_cu)) goto again;