diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 39a2d32718..b378ccf2e1 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2021-04-07 Andrew Burgess + + * dwarf2/section.c (dwarf2_section_info::get_bfd_owner): Add an + assert. + (dwarf2_section_info::get_file_name): Add an assert. + (dwarf2_section_info::read_string): Display a minimal, sane error + when the dwarf2_section_info is not associated with a bfd section. + 2021-04-07 Andrew Burgess * top.c (staged_gdb_datadir): Update comment. diff --git a/gdb/dwarf2/section.c b/gdb/dwarf2/section.c index a839c47ecc..8e1c0197c8 100644 --- a/gdb/dwarf2/section.c +++ b/gdb/dwarf2/section.c @@ -54,6 +54,7 @@ dwarf2_section_info::get_bfd_owner () const section = get_containing_section (); gdb_assert (!section->is_virtual); } + gdb_assert (section->s.section != nullptr); return section->s.section->owner; } @@ -83,6 +84,7 @@ dwarf2_section_info::get_file_name () const { bfd *abfd = get_bfd_owner (); + gdb_assert (abfd != nullptr); return bfd_get_filename (abfd); } @@ -194,8 +196,14 @@ dwarf2_section_info::read_string (struct objfile *objfile, LONGEST str_offset, { read (objfile); if (buffer == NULL) - error (_("%s used without %s section [in module %s]"), - form_name, get_name (), get_file_name ()); + { + if (get_bfd_section () == nullptr) + error (_("Dwarf Error: %s used without required section"), + form_name); + else + error (_("Dwarf Error: %s used without %s section [in module %s]"), + form_name, get_name (), get_file_name ()); + } if (str_offset >= size) error (_("%s pointing outside of %s section [in module %s]"), form_name, get_name (), get_file_name ()); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 04ed4ad96e..690c8b3498 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2021-04-07 Andrew Burgess + + * gdb.dwarf2/dw2-using-debug-str.exp: Add an additional test. + 2021-04-07 Andrew Burgess * gdb.python/py-parameter.exp: Add test for reading data-directory diff --git a/gdb/testsuite/gdb.dwarf2/dw2-using-debug-str.exp b/gdb/testsuite/gdb.dwarf2/dw2-using-debug-str.exp index 3184f9078c..62febdb0a9 100644 --- a/gdb/testsuite/gdb.dwarf2/dw2-using-debug-str.exp +++ b/gdb/testsuite/gdb.dwarf2/dw2-using-debug-str.exp @@ -99,3 +99,44 @@ if ![runto_main] { # fictional, it only exists in the DWARF, but it contains lots of nice # field names, all of which are stored in the .debug_str section. gdb_test "p global_var" " = \\{aa = 0, bb = 0, cc = 0\\}" + +# Now copy the executable, and remove the .debug_str section. This +# creates an executable with an invalid DWARF configuration. GDB +# should give an error when trying to read the debug information from +# this executable. +set binfile_no_debug_str "${binfile}-no-debug-str" +set args "--remove-section .debug_str $binfile ${binfile_no_debug_str}" +if {[run_on_host "objcopy" [gdb_find_objcopy] "$args"]} { + perror "failed to run objcopy" + return -1 +} + +# Restart GDB, but don't load an executable. When we do load the +# executable we're going to get an error, which we check for below. +clean_restart + +# This pattern is hit when GDB does not use -readnow (i.e. the default +# behaviour). +set pattern1 \ + [multi_line \ + "Reading symbols from \[^\r\n\]+" \ + "Dwarf Error: DW_FORM_strp used without required section" \ + "\\(No debugging symbols \[^\r\n\]+\\)"] + +# This pattern is hit when GDB does use -readnow (e.g. running with +# --target_board=readnow). +set pattern2 \ + [multi_line \ + "Reading symbols from \[^\r\n\]+" \ + "Expanding full symbols from \[^\r\n\]+" \ + "Dwarf Error: DW_FORM_strp used without required section"] + +# Load the executable, we expect an error from the DWARF parser. +gdb_test_multiple "file $binfile_no_debug_str" "file $testfile" { + -wrap -re $pattern1 { + pass $gdb_test_name + } + -re -wrap "$pattern2" { + pass $gdb_test_name + } +}