From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 1FEC93857371; Wed, 4 May 2022 14:38:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1FEC93857371 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix crash when creating index from index X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: d8a735330883faa2670ccf70880f04b3c29683ed X-Git-Newrev: 758ffab46b5b6c4f0a145e8833f24ba72a4259e4 Message-Id: <20220504143854.1FEC93857371@sourceware.org> Date: Wed, 4 May 2022 14:38:54 +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: Wed, 04 May 2022 14:38:54 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D758ffab46b5b= 6c4f0a145e8833f24ba72a4259e4 commit 758ffab46b5b6c4f0a145e8833f24ba72a4259e4 Author: Tom Tromey Date: Thu Apr 21 11:20:22 2022 -0600 Fix crash when creating index from index =20 My patches yesterday to unify the DWARF index base classes had a bug -- namely, I did the wholesale dynamic_cast-to-static_cast too hastily and introduced a crash. This can be seen by trying to add an index to a file that has an index, or by running a test like gdb-index-cxx.exp using the cc-with-debug-names.exp target board. =20 This patch fixes the crash by introducing a new virtual method and removing some of the static casts. Diff: --- gdb/dwarf2/cooked-index.h | 5 +++++ gdb/dwarf2/index-write.c | 27 +++++++++------------------ gdb/dwarf2/mapped-index.h | 14 +++++++++++++- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h index 21bda8698c1..a460351612f 100644 --- a/gdb/dwarf2/cooked-index.h +++ b/gdb/dwarf2/cooked-index.h @@ -295,6 +295,11 @@ public: "main". This will return NULL if no such entry is available. */ const cooked_index_entry *get_main () const; =20 + cooked_index_vector *index_for_writing () override + { + return this; + } + quick_symbol_functions_up make_quick_functions () const override; =20 private: diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 62a7466bcfe..dfa74ddad8e 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -1163,8 +1163,9 @@ write_cooked_index (cooked_index_vector *table, associated dwz file, DWZ_OUT_FILE must be NULL. */ =20 static void -write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file, - FILE *dwz_out_file) +write_gdbindex (dwarf2_per_objfile *per_objfile, + cooked_index_vector *table, + FILE *out_file, FILE *dwz_out_file) { mapped_symtab symtab; data_buf objfile_cu_list; @@ -1218,9 +1219,6 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE= *out_file, ++this_counter; } =20 - cooked_index_vector *table - =3D (static_cast - (per_objfile->per_bfd->index_table.get ())); write_cooked_index (table, cu_index_htab, &symtab); =20 /* Dump the address map. */ @@ -1256,6 +1254,7 @@ static const gdb_byte dwarf5_gdb_augmentation[] =3D {= 'G', 'D', 'B', 0 }; =20 static void write_debug_names (dwarf2_per_objfile *per_objfile, + cooked_index_vector *table, FILE *out_file, FILE *out_file_str) { const bool dwarf5_is_dwarf64 =3D check_dwarf64_offsets (per_objfile); @@ -1291,9 +1290,6 @@ write_debug_names (dwarf2_per_objfile *per_objfile, - per_objfile->per_bfd->tu_stats.nr_tus)); gdb_assert (types_counter =3D=3D per_objfile->per_bfd->tu_stats.nr_tus); =20 - cooked_index_vector *table - =3D (static_cast - (per_objfile->per_bfd->index_table.get ())); for (const cooked_index_entry *entry : table->all_entries ()) nametable.insert (entry); =20 @@ -1431,15 +1427,10 @@ write_dwarf_index (dwarf2_per_objfile *per_objfile,= const char *dir, { struct objfile *objfile =3D per_objfile->objfile; =20 + if (per_objfile->per_bfd->index_table =3D=3D nullptr) + error (_("No debugging symbols")); cooked_index_vector *table - =3D (static_cast - (per_objfile->per_bfd->index_table.get ())); - if (table =3D=3D nullptr) - { - if (per_objfile->per_bfd->index_table !=3D nullptr) - error (_("Cannot use an index to create the index")); - error (_("No debugging symbols")); - } + =3D per_objfile->per_bfd->index_table->index_for_writing (); =20 if (per_objfile->per_bfd->types.size () > 1) error (_("Cannot make an index when the file has multiple .debug_types= sections")); @@ -1460,13 +1451,13 @@ write_dwarf_index (dwarf2_per_objfile *per_objfile,= const char *dir, { index_wip_file str_wip_file (dir, basename, DEBUG_STR_SUFFIX); =20 - write_debug_names (per_objfile, objfile_index_wip.out_file.get (), + write_debug_names (per_objfile, table, objfile_index_wip.out_file.ge= t (), str_wip_file.out_file.get ()); =20 str_wip_file.finalize (); } else - write_gdbindex (per_objfile, objfile_index_wip.out_file.get (), + write_gdbindex (per_objfile, table, objfile_index_wip.out_file.get (), (dwz_index_wip.has_value () ? dwz_index_wip->out_file.get () : NULL)); =20 diff --git a/gdb/dwarf2/mapped-index.h b/gdb/dwarf2/mapped-index.h index e5d77469925..7d71347f9f4 100644 --- a/gdb/dwarf2/mapped-index.h +++ b/gdb/dwarf2/mapped-index.h @@ -1,6 +1,6 @@ /* Base class for mapped indices =20 - Copyright (C) 2021 Free Software Foundation, Inc. + Copyright (C) 2021, 2022 Free Software Foundation, Inc. =20 This file is part of GDB. =20 @@ -47,6 +47,8 @@ struct name_component offset_type idx; }; =20 +class cooked_index_vector; + /* Base class of all DWARF scanner types. */ =20 struct dwarf_scanner_base @@ -66,6 +68,11 @@ struct dwarf_scanner_base { return true; } + + /* This is called when writing an index. For a cooked index, it + will return 'this' as a cooked index. For other forms, it will + throw an exception with an appropriate error message. */ + virtual cooked_index_vector *index_for_writing () =3D 0; }; =20 /* Base class containing bits shared by both .gdb_index and @@ -109,6 +116,11 @@ struct mapped_index_base : public dwarf_scanner_base find_name_components_bounds (const lookup_name_info &ln_no_params, enum language lang, dwarf2_per_objfile *per_objfile) const; + + cooked_index_vector *index_for_writing () override + { + error (_("Cannot use an index to create the index")); + } }; =20 #endif /* GDB_DWARF2_MAPPED_INDEX_H */