From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 6FB073856275; Thu, 26 May 2022 17:50:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FB073856275 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 in new DWARF indexer X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 20a26f4e018cd0cb40a3b81568be0d4f164eed0f X-Git-Newrev: 834eaf9201c18efc5b888f3cd116dd7856bec759 Message-Id: <20220526175058.6FB073856275@sourceware.org> Date: Thu, 26 May 2022 17:50:58 +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, 26 May 2022 17:50:58 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D834eaf9201c1= 8efc5b888f3cd116dd7856bec759 commit 834eaf9201c18efc5b888f3cd116dd7856bec759 Author: Tom Tromey Date: Thu May 12 16:37:52 2022 -0600 Fix crash in new DWARF indexer =20 PR gdb/29128 points out a crash in the new DWARF index code. This happens if the aranges for a CU claims a PC, but the symtab that is created during CU expansion does not actually contain the PC. This can only occur due to bad debuginfo, but at the same time, gdb should not crash. =20 This patch fixes the bug and further merges some code into dwarf2_base_index_functions. This merger helps prevent the same issue from arising from the other index implementations. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29128 Diff: --- gdb/dwarf2/read.c | 72 +++++++++++++++-------------= ---- gdb/testsuite/gdb.dwarf2/cu-no-addrs.exp | 62 +++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 39 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index b7ad75e3a29..c4578c687d2 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -1831,9 +1831,15 @@ struct dwarf2_base_index_functions : public quick_sy= mbol_functions =20 void expand_all_symtabs (struct objfile *objfile) override; =20 + /* A helper function that finds the per-cu object from an "adjusted" + PC -- a PC with the base text offset removed. */ + virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd, + CORE_ADDR adjusted_pc); + struct compunit_symtab *find_pc_sect_compunit_symtab (struct objfile *objfile, struct bound_minimal_symbol msymbol, - CORE_ADDR pc, struct obj_section *section, int warn_if_readin) overri= de; + CORE_ADDR pc, struct obj_section *section, int warn_if_readin) + override final; =20 struct compunit_symtab *find_compunit_symtab_by_address (struct objfile *objfile, CORE_ADDR address) override @@ -4236,6 +4242,16 @@ recursively_find_pc_sect_compunit_symtab (struct com= punit_symtab *cust, return NULL; } =20 +dwarf2_per_cu_data * +dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd, + CORE_ADDR adjusted_pc) +{ + if (per_bfd->index_addrmap =3D=3D nullptr) + return nullptr; + return (struct dwarf2_per_cu_data *) addrmap_find (per_bfd->index_addrma= p, + adjusted_pc); +} + struct compunit_symtab * dwarf2_base_index_functions::find_pc_sect_compunit_symtab (struct objfile *objfile, @@ -4244,19 +4260,15 @@ dwarf2_base_index_functions::find_pc_sect_compunit_= symtab struct obj_section *section, int warn_if_readin) { - struct dwarf2_per_cu_data *data; struct compunit_symtab *result; =20 dwarf2_per_objfile *per_objfile =3D get_dwarf2_per_objfile (objfile); - if (per_objfile->per_bfd->index_addrmap =3D=3D nullptr) - return NULL; =20 CORE_ADDR baseaddr =3D objfile->text_section_offset (); - data =3D ((struct dwarf2_per_cu_data *) - addrmap_find (per_objfile->per_bfd->index_addrmap, - pc - baseaddr)); - if (!data) - return NULL; + struct dwarf2_per_cu_data *data =3D find_per_cu (per_objfile->per_bfd, + pc - baseaddr); + if (data =3D=3D nullptr) + return nullptr; =20 if (warn_if_readin && per_objfile->symtab_set_p (data)) warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"= ), @@ -4265,7 +4277,10 @@ dwarf2_base_index_functions::find_pc_sect_compunit_s= ymtab result =3D recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data, per_objfile, false), pc); =20 - gdb_assert (result !=3D NULL); + if (warn_if_readin && result =3D=3D nullptr) + warning (_("(Error: pc %s in address map, but not in symtab.)"), + paddress (objfile->arch (), pc)); + return result; } =20 @@ -18427,9 +18442,8 @@ cooked_indexer::make_index (cutu_reader *reader) =20 struct cooked_index_functions : public dwarf2_base_index_functions { - struct compunit_symtab *find_pc_sect_compunit_symtab - (struct objfile *objfile, struct bound_minimal_symbol msymbol, - CORE_ADDR pc, struct obj_section *section, int warn_if_readin) overri= de; + dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd, + CORE_ADDR adjusted_pc) override; =20 struct compunit_symtab *find_compunit_symtab_by_address (struct objfile *objfile, CORE_ADDR address) override; @@ -18468,36 +18482,16 @@ struct cooked_index_functions : public dwarf2_bas= e_index_functions } }; =20 -struct compunit_symtab * -cooked_index_functions::find_pc_sect_compunit_symtab - (struct objfile *objfile, - struct bound_minimal_symbol msymbol, - CORE_ADDR pc, - struct obj_section *section, - int warn_if_readin) +dwarf2_per_cu_data * +cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd, + CORE_ADDR adjusted_pc) { - dwarf2_per_objfile *per_objfile =3D get_dwarf2_per_objfile (objfile); - if (per_objfile->per_bfd->index_table =3D=3D nullptr) - return nullptr; - - CORE_ADDR baseaddr =3D objfile->text_section_offset (); cooked_index_vector *table =3D (static_cast - (per_objfile->per_bfd->index_table.get ())); - dwarf2_per_cu_data *per_cu =3D table->lookup (pc - baseaddr); - if (per_cu =3D=3D nullptr) + (per_bfd->index_table.get ())); + if (table =3D=3D nullptr) return nullptr; - - if (warn_if_readin && per_objfile->symtab_set_p (per_cu)) - warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"= ), - paddress (objfile->arch (), pc)); - - compunit_symtab *result =3D (recursively_find_pc_sect_compunit_symtab - (dw2_instantiate_symtab (per_cu, per_objfile, - false), - pc)); - gdb_assert (result !=3D nullptr); - return result; + return table->lookup (adjusted_pc); } =20 struct compunit_symtab * diff --git a/gdb/testsuite/gdb.dwarf2/cu-no-addrs.exp b/gdb/testsuite/gdb.d= warf2/cu-no-addrs.exp new file mode 100644 index 00000000000..0c7b80d5f4f --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/cu-no-addrs.exp @@ -0,0 +1,62 @@ +# Copyright (C) 2022 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 . + +# Regression test for the situation where aranges covers an address +# but the CU does not. + +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 main.c cu-no-addrs.S + +lassign [function_range main ${srcdir}/${subdir}/${srcfile}] \ + main_start main_length + +# Make some DWARF for the test. +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + global main_start main_length + + cu {label cu_start} { + # The PC range here is intentionally empty -- this was the + # trigger for the bug. + compile_unit { + {language @DW_LANG_C} + {DW_AT_low_pc $main_start DW_FORM_addr} + {DW_AT_high_pc $main_start DW_FORM_addr} + } { + DW_TAG_subprogram { + {DW_AT_name "main"} + {DW_AT_low_pc $main_start DW_FORM_addr} + } + } + } + + aranges {} cu_start { + arange {} $main_start $main_length + } +} + +if {[prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}]} { + return -1 +} + +gdb_test "break *$main_start" ".*Breakpoint $decimal at $hex" \ + "set breakpoint at main address"