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

Message ID CY8PR12MB734051E88B8DF63612A2EA8F87692@CY8PR12MB7340.namprd12.prod.outlook.com
State New
Headers
Series bfd: Fix addr2line for skipped DW_FORM_ref_addr CUs and DWARF5 one-file line tables |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply

Commit Message

Kumar N, Bhuvanendra Nov. 3, 2026, 4:50 p.m. UTC
  AMD General

Hi,

I am requesting review of this BFD patch. It fixes binutils addr2line for clang LTO objects. The patch is inlined below and attached to this email.

For clang LTO objects, binutils 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 in find_abstract_instance and search only that small set in _bfd_dwarf2_find_nearest_line_with_alt after the sequential scan.

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.

NOTE: This reduced test fails with clang LTO and passes with GCC LTO.

Reduced test case sources:

a.c:
__attribute__((always_inline)) int inner(int x);

__attribute__((noinline)) int first(int x)
{
  return inner(x);
}

b.c:
__attribute__((always_inline)) int inner(int x)
{
  return x + 1;
}

__attribute__((noinline)) int outer(int x)
{
  return x + 5;
}

main.c:
int first(int);
int outer(int);
int main(void)
{
  return first(1) + outer(4);
}

Test scenario 1: clang LTO, DWARF-5 (clang's default for -g)

  clang -g -O2 -flto -c a.c -o a.o
  clang -g -O2 -flto -c b.c -o b.o
  clang -g -O2 -c main.c -o main.o
  clang -flto a.o b.o main.o -o repro
  addr2line -e repro -f <address of outer>

  Before fix:
addr2line: DWARF error: mangled line number section (bad file number)
addr2line: DWARF error: mangled line number section (bad file number)
outer
??:?

  After fix:
    outer
    b.c:8

Test scenario 2: same commands with -gdwarf-4 on every clang invocation

  Before fix:
    outer
    ??:?

  After fix:
    outer
    b.c:8

regards,
Bhuvan

Patch inlined:

From d2dc62e51066cdc4fade93446c813f19fa158b54 Mon Sep 17 00:00:00 2001
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.
---
bfd/dwarf2.c | 37 +++++++++++++++++++++++++++++++++++--
1 file changed, 35 insertions(+), 2 deletions(-)

   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,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)
     {
--
2.43.0
  

Comments

Jan Beulich Nov. 6, 2026, 8:53 a.m. UTC | #1
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
  

Patch

diff --git a/bfd/dwarf2.c b/bfd/dwarf2.c
index 61473a50..2ed9e2b6 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. */