From: Lancelot SIX Date: Tue, 5 Nov 2024 11:07:00 +0000 (+0000) Subject: gdb/dwarf2/read.c: Handle empty CU name X-Git-Tag: gdb-16-branchpoint~482 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c0a07e7d48801c7cffaf825545fc05024a7c686e;p=thirdparty%2Fbinutils-gdb.git gdb/dwarf2/read.c: Handle empty CU name I recently came across a case where a compiler would emit a CU with an empty name. In such case, the attribute object constructed by GDB will return nullptr when as_string is called. One place is not checking for this possibility. As a result, loading such binary results in a GDB crash: $ gdb -q a.out Reading symbols from a.out... Fatal signal: Segmentation fault ----- Backtrace ----- [...] 0x742f4dd8afab __strcmp_avx2 ../sysdeps/x86_64/multiarch/strcmp-avx2.S:283 0x58593704a0bc prepare_one_comp_unit ../../gdb/dwarf2/read.c:21842 0x585937053fd9 process_psymtab_comp_unit ../../gdb/dwarf2/read.c:4633 0x585937053fd9 _ZN23cooked_index_debug_info11process_cusEmN9__gnu_cxx17__normal_iteratorIPSt10unique_ptrI18dwarf2_per_cu_data26dwarf2_per_cu_data_deleterESt6vectorIS5_SaIS5_EEEESA_ ../../gdb/dwarf2/read.c:4943 [...] --------------------- A fatal error internal to GDB has been detected, further debugging is not possible. GDB will now terminate. This is a bug, please report it. For instructions, see: . Segmentation fault (core dumped) This seems to be a regression introduced by the following commit: commit 00105aa1c4d9933fe3cfe9bc1be0daefe9f8ca36 Date: Tue Sep 24 10:24:22 2024 +0200 [gdb/symtab] Don't expand non-Ada CUs for info exceptions This patch fixes this issue by checking if attr->as_string returns nullptr. Change-Id: I78fe7a090f0bd1045b8cb2f8d088a8d6cf57fe1c Approved-By: Andrew Burgess Approved-By: Tom Tromey --- diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 30ef69aea30..665e00bb8e7 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -21837,6 +21837,7 @@ prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die, attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu); if (attr != nullptr && cu->producer != nullptr + && attr->as_string () != nullptr && strcmp (attr->as_string (), "") == 0 && producer_is_gcc (cu->producer, nullptr, nullptr)) cu->per_cu->lto_artificial = true; diff --git a/gdb/testsuite/gdb.dwarf2/cu-empty-name.c b/gdb/testsuite/gdb.dwarf2/cu-empty-name.c new file mode 100644 index 00000000000..2b72fe16c1e --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/cu-empty-name.c @@ -0,0 +1,22 @@ +/* Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . */ + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/cu-empty-name.exp b/gdb/testsuite/gdb.dwarf2/cu-empty-name.exp new file mode 100644 index 00000000000..a8909621183 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/cu-empty-name.exp @@ -0,0 +1,48 @@ +# Copyright 2024 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 . + +# Check that GDB can load a CU with an empty name properly. + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +require dwarf2_support + +standard_testfile .c .S + +# Make some DWARF for the test. +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + cu {} { + DW_TAG_compile_unit { + {DW_AT_producer "Tsetsuite"} + {DW_AT_language @DW_LANG_C} + {DW_AT_name ""} + {DW_AT_comp_dir /tmp} + } { + } + } +} + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}] } { + return -1 +} + +if { ![runto_main] } { + return -1 +} + +gdb_continue_to_end