]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Don't use .debug_names if .debug_aranges is missing
authorTom Tromey <tom@tromey.com>
Tue, 27 Jan 2026 03:49:26 +0000 (20:49 -0700)
committerTom Tromey <tom@tromey.com>
Sat, 14 Feb 2026 18:24:35 +0000 (11:24 -0700)
gdb's .debug_names reader depends on .debug_aranges as well, because
if the ranges are missing, it won't be able to create a map from PC to
CU.

However, this requirement isn't enforced by gdb.  I no longer recall
know why, but I can see in commit b371f07c ("Rewrite .debug_names
reader") that I made this choice on purpose:

    In v1 of this patch, I made the new reader more strict about requiring
    .debug_aranges.  In v2, I've backed this out and kept the previous
    logic.  This solved a few test failures, though it's arguably not the
    right approach.

However, the current setup now seems clearly incorrect to me.  And, it
was causing problems for another patch I'm working on.

Fixing this required patching a few tests to add a .debug_aranges
section.  I've also updated the intro comment to
read_addrmap_from_aranges to reflect how it ought to be used.

Regression tested on x86-64 Fedora 41 with both the default and the
cc-with-debug-names target boards.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33847

gdb/dwarf2/aranges.c
gdb/dwarf2/aranges.h
gdb/dwarf2/read-debug-names.c
gdb/testsuite/gdb.dwarf2/debug-names-bad-cu-index.exp
gdb/testsuite/gdb.dwarf2/debug-names-tu.exp.tcl
gdb/testsuite/gdb.dwarf2/debug-names.exp

index 4f25763ebc835d4f7e978e9b2741f8e01cd290ba..9b7603263d43fc449beda874818723c0f14fac2a 100644 (file)
@@ -20,6 +20,7 @@
 #include "dwarf2/aranges.h"
 #include "dwarf2/read.h"
 #include "extract-store-integer.h"
+#include "cli/cli-style.h"
 
 /* See aranges.h.  */
 
@@ -29,12 +30,18 @@ read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
                           addrmap_mutable *mutable_map,
                           deferred_warnings *warn)
 {
+  struct objfile *objfile = per_objfile->objfile;
+
   /* Caller must ensure that the section has already been read.  */
   gdb_assert (section->read_in);
   if (section->empty ())
-    return false;
+    {
+      warn->warn (_(".debug_aranges section is empty or missing from %ps"),
+                 styled_string (file_name_style.style (),
+                                objfile_name (objfile)));
+      return false;
+    }
 
-  struct objfile *objfile = per_objfile->objfile;
   bfd *abfd = objfile->obfd.get ();
   struct gdbarch *gdbarch = objfile->arch ();
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
index a8e534580a0bd5d69588dcfd53c5b041f208f4f7..26709062d177e884098a61d9517df9d5a9f8be04 100644 (file)
@@ -26,7 +26,12 @@ class addrmap_mutable;
 
 /* Read the address map data from DWARF-5 .debug_aranges, and use it
    to populate given addrmap.  Returns true on success, false on
-   failure.  */
+   failure.  On failure, it is guaranteed that a warning will be
+   emitted to WARN.
+
+   Note that this will emit a warning and fail if the section is
+   empty.  If a caller can tolerate a missing .debug_aranges section,
+   it should check this explicitly before the call.  */
 
 extern bool read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
                                       dwarf2_section_info *section,
index 6258fcb86a71500164f25ff390ed5366efea5d73..a5669bde71e4d6786571bdbd7068a6c720a76553 100644 (file)
@@ -21,6 +21,7 @@
 #include "dwarf2/aranges.h"
 #include "dwarf2/cooked-index.h"
 
+#include "cli/cli-style.h"
 #include "complaints.h"
 #include "cp-support.h"
 #include "dwz.h"
@@ -833,9 +834,18 @@ dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
      into place later.  */
   cooked_index_worker_result first;
   deferred_warnings warnings;
-  read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
-                            first.get_addrmap (), &warnings);
+  bool ok = read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
+                                      first.get_addrmap (), &warnings);
   warnings.emit ();
+  if (!ok)
+    {
+      /* read_addrmap_from_aranges must have emitted a warning in this
+        case.  */
+      warning (_("... not using '.debug_names' for %ps"),
+              styled_string (file_name_style.style (),
+                             objfile_name (objfile)));
+      return false;
+    }
 
   const auto n_workers
     = std::max<std::size_t> (gdb::thread_pool::g_thread_pool->thread_count (),
index b88430eaa5926c62bf3a3e400914442570d40147..e91ff225e9430f6daa2ef66b35bd44f739b21261 100644 (file)
@@ -73,6 +73,10 @@ Dwarf::assemble {
        name _start subprogram CU-1 0xEDDB6232
        name struct_with_int_member structure_type tu_label 0x53A2AE86
     }
+
+    aranges {} cu_label {
+       arange {} $_start_start $_start_len
+    }
 }
 
 if {[build_executable ${testfile}.exp $testfile "${asm_file} ${srcfile}" \
index be1a9f490f0d1620c6c218be8281c0101506420a..f5803a32b3c024944fda46f10166d72468dd9167 100644 (file)
@@ -81,6 +81,10 @@ Dwarf::assemble {
        name struct_with_int_member structure_type tu_label 0x53A2AE86 \
            {DW_LANG_C}
     }
+
+    aranges {} cu_label {
+       arange {} $_start_start $_start_len
+    }
 }
 
 if {[prepare_for_testing "failed to prepare" $testfile "${asm_file} ${srcfile}" \
index cb55c447dc4f148a31627a1a70ff60b60e1586a9..66458df39745ed0936e6c42d62ca6bccd3b5cfee 100644 (file)
@@ -56,6 +56,10 @@ Dwarf::assemble {
        name _start subprogram cu_label 0xEDDB6232
        name int base_type cu_label 0xB888030
     }
+
+    aranges {} cu_label {
+       arange {} $_start_end $_start_len
+    }
 }
 
 if {[prepare_for_testing "failed to prepare" $testfile "${asm_file} ${srcfile}" \