[PATCH] bfd: Fix addr2line for skipped DW_FORM_ref_addr CUs and DWARF5 one-file line tables

Kumar N, Bhuvanendra Bhuvanendra.KumarN@amd.com
Wed Nov 11 11:35:23 GMT 2026


AMD General

Thanks for the review comments. I've revised the patch and reduced the size of this hunk as suggested. Is it okay to proceed with committing this patch?

Please find the patch attached with this email and inlined below:

>From 4750f6a03e87affcaae74c5c4770a1db1f8ff9cc Mon Sep 17 00:00:00 2001
From: bhkumarn <Bhuvanendra.KumarN@amd.com>
Date: Wed, 11 Nov 2026 11:29:38 +0000
Subject: [PATCH] bfd: Fix addr2line for skipped DW_FORM_ref_addr CUs and
 DWARF5 one-file line tables

For clang compiled objects, addr2line can miss a later compilation unit
when an earlier CU is scanned and DW_FORM_ref_addr causes stash_comp_unit
to parse ahead.  Those units never go through the sequential reader.
Queue them on unsearched_units and search only that small set.

Separately, PR 30783 starts the DWARF5 line state machine at file
index 1.  A table that contains only file 0 then reports a bad file
number.  Keep the initial index at 1 when that slot exists; fall back
to file 0 only for a one-entry DWARF5 table.

bfd/
        * dwarf2.c (struct dwarf2_debug_file): Add unsearched_units.
        (struct comp_unit): Add next_unsearched.
        (decode_line_info): Fall back to file 0 when a DWARF5 line
        table has no file 1.
        (find_abstract_instance): Queue CUs parsed via DW_FORM_ref_addr
        or DW_FORM_GNU_ref_alt.
        (_bfd_dwarf2_find_nearest_line_with_alt): Drain unsearched_units
        as a work queue after the sequential scan.
---
 bfd/dwarf2.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/bfd/dwarf2.c b/bfd/dwarf2.c
index 61473a50..a95c1248 100644
--- a/bfd/dwarf2.c
+++ b/bfd/dwarf2.c
@@ -271,6 +271,9 @@ struct dwarf2_debug_file
   /* Last comp unit in list above.  */
   struct comp_unit *last_comp_unit;

+  /* Unsearched CUs parsed via DW_FORM_ref_addr.  */
+  struct comp_unit *unsearched_units;
+
   /* Line table at line_offset zero.  */
   struct line_info_table *line_table;

@@ -357,6 +360,9 @@ struct comp_unit
      Unused if arange.high != 0. */
   struct comp_unit *next_unit_without_ranges;

+  /* Chain of CUs parsed via DW_FORM_ref_addr.  */
+  struct comp_unit *next_unsearched;
+
   /* Likewise, chain the compilation unit read after this one.
      The comp units are stored in reversed reading order.  */
   struct comp_unit *prev_unit;
@@ -2924,8 +2930,12 @@ decode_line_info (struct comp_unit *unit)
       if (table->num_files)
        {
          /* PR 30783: Always start with a file index of 1, even
-            for DWARF-5.  */
-         filename = concat_filename (table, 1);
+            for DWARF-5.  Use file 0 if file 1 is absent.  */
+         unsigned int file_index = 1;
+
+         if (table->use_dir_and_file_0 && file_index >= table->num_files)
+           file_index = 0;
+         filename = concat_filename (table, file_index);
        }

       /* Decode the table.  */
@@ -3562,6 +3572,8 @@ find_abstract_instance (struct comp_unit *unit,
                u = stash_comp_unit (unit->stash, &unit->stash->f);
                if (u == NULL)
                  break;
+               u->next_unsearched = unit->file->unsearched_units;
+               unit->file->unsearched_units = u;
                if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
                  break;
                u = NULL;
@@ -3573,6 +3585,8 @@ find_abstract_instance (struct comp_unit *unit,
                u = stash_comp_unit (unit->stash, &unit->stash->alt);
                if (u == NULL)
                  break;
+               u->next_unsearched = unit->stash->alt.unsearched_units;
+               unit->stash->alt.unsearched_units = u;
                if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
                  break;
                u = NULL;
@@ -6016,6 +6030,20 @@ _bfd_dwarf2_find_nearest_line_with_alt
        break;
     }

+  /* Search CUs parsed via DW_FORM_ref_addr.  */
+  while (!found && !do_line
+        && (each = stash->f.unsearched_units) != NULL)
+    {
+      stash->f.unsearched_units = each->next_unsearched;
+      each->next_unsearched = NULL;
+      found = (comp_unit_may_contain_address (each, addr)
+              && comp_unit_find_nearest_line (each, addr,
+                                              filename_ptr,
+                                              &function,
+                                              linenumber_ptr,
+                                              discriminator_ptr));
+    }
+
  done:
   if (functionname_ptr && function && function->is_linkage)
     {
--
2.43.0

-----Original Message-----
From: Jan Beulich <jbeulich@suse.com>
Sent: Friday, November 6, 2026 2:23 PM
To: Kumar N, Bhuvanendra <Bhuvanendra.KumarN@amd.com>
Cc: George, Jini Susan <JiniSusan.George@amd.com>; binutils@sourceware.org
Subject: Re: [PATCH] bfd: Fix addr2line for skipped DW_FORM_ref_addr CUs and DWARF5 one-file line tables

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.


On 03.11.2026 17:50, Kumar N, Bhuvanendra wrote:
> From: bhkumarn
> Bhuvanendra.KumarN@amd.com<mailto:Bhuvanendra.KumarN@amd.com>
> Date: Tue, 3 Nov 2026 16:00:28 +0000
> Subject: [PATCH] bfd: Fix addr2line for skipped DW_FORM_ref_addr CUs
> and
> DWARF5 one-file line tables
>
> For clang compiled objects, addr2line can miss a later compilation
> unit when an earlier CU is scanned and DW_FORM_ref_addr causes
> stash_comp_unit to parse ahead.  Those units never go through the sequential reader.
> Queue them on unsearched_units and search only that small set.
>
> Separately, PR 30783 starts the DWARF5 line state machine at file
> index 1.  A table that contains only file 0 then reports a bad file
> number.  Keep the initial index at 1 when that slot exists; fall back
> to file 0 only for a one-entry DWARF5 table.
>
> bfd/
>             * dwarf2.c (struct dwarf2_debug_file): Add unsearched_units.
>             (struct comp_unit): Add next_unsearched.
>             (decode_line_info): Fall back to file 0 when a DWARF5 line
>             table has no file 1.
>             (find_abstract_instance): Queue CUs parsed via DW_FORM_ref_addr
>             or DW_FORM_GNU_ref_alt.
>             (_bfd_dwarf2_find_nearest_line_with_alt): Drain unsearched_units
>             as a work queue after the sequential scan.

Okay, albeit I think I'll ...

> @@ -6016,6 +6030,25 @@ _bfd_dwarf2_find_nearest_line_with_alt
>            break;
>      }
> +  /* Search CUs parsed via DW_FORM_ref_addr.  */  if (!found &&
> + !do_line)
> +    {
> +      while (stash->f.unsearched_units != NULL)
> +          {
> +            each = stash->f.unsearched_units;
> +            stash->f.unsearched_units = each->next_unsearched;
> +            each->next_unsearched = NULL;
> +            found = (comp_unit_may_contain_address (each, addr)
> +                         && comp_unit_find_nearest_line (each, addr,
> +                                                                         filename_ptr,
> +                                                                         &function,
> +                                                                         linenumber_ptr,
> +                                                                         discriminator_ptr));
> +            if (found)
> +              break;
> +          }
> +    }
> +
>   done:
>    if (functionname_ptr && function && function->is_linkage)
>      {

... shrink this hunk some before committing.

Jan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-bfd-Fix-addr2line-for-skipped-DW_FORM_ref_addr-CUs_revised.patch
Type: application/octet-stream
Size: 3879 bytes
Desc: 0001-bfd-Fix-addr2line-for-skipped-DW_FORM_ref_addr-CUs_revised.patch
URL: <https://sourceware.org/pipermail/binutils/attachments/20261111/30ef0be2/attachment-0001.obj>


More information about the Binutils mailing list