[committed 4/4] gprofng: Handle DW_AT_decl_file 0 in DWARF 5

Claudiu Zissulescu claziss@gmail.com
Fri Nov 27 09:42:28 GMT 2026


In DWARF 5 the file table starts at index 0, and entry 0 is the
primary source file.  Clang uses DW_AT_decl_file 0 for everything
declared in the primary file.  DwrCU::get_source rejected index 0, as
it does for DWARF 4 and older where 0 means "no file", so a function
declared in the primary file got no default source file.  Function
::setSource then fell back to the last include file that had line
information, so "gprofng display src -source" and the line view
showed a header file instead of the file that defines the function.
GCC's DWARF 5 output starts at index 1 and was not affected.

Accept index 0 when the line table is DWARF 5 or newer.  A DIE without
DW_AT_decl_file still gives no source file: add DwrCU::get_decl_source
for that, so that the callers that look at a declaration for the file
can tell "no attribute" from "file 0".

	* src/DwarfLib.h (DwrLineRegs::get_version): New.
	(DwrCU::get_decl_source): Declare.
	* src/DwarfLib.cc (DwrCU::get_source): Accept file 0 for DWARF 5.
	(DwrCU::get_decl_source): New.
	(DwrCU::set_source, DwrCU::inherit_prop, DwrCU::find_declaration):
	Use it.
	* testsuite/lib/dwarf5decl.h: New file.
	* testsuite/lib/dwarf5test.c (hot_function): Inline a function from
	dwarf5decl.h.
	* testsuite/gprofng.display/dwarf5.exp: Copy dwarf5decl.h.
---
 gprofng/src/DwarfLib.cc                      | 26 +++++++++++++++-----
 gprofng/src/DwarfLib.h                       |  2 ++
 gprofng/testsuite/gprofng.display/dwarf5.exp |  3 +++
 gprofng/testsuite/lib/dwarf5decl.h           | 12 +++++++++
 gprofng/testsuite/lib/dwarf5test.c           |  8 +++---
 5 files changed, 42 insertions(+), 9 deletions(-)
 create mode 100644 gprofng/testsuite/lib/dwarf5decl.h

diff --git a/gprofng/src/DwarfLib.cc b/gprofng/src/DwarfLib.cc
index a767b34230b..e15bbca5836 100644
--- a/gprofng/src/DwarfLib.cc
+++ b/gprofng/src/DwarfLib.cc
@@ -2200,25 +2200,39 @@ DwrCU::set_source (Function *func)
   int lineno = (int) Dwarf_data (DW_AT_decl_line);
   func->setLineFirst (lineno);
 
-  int fileno = (int) Dwarf_data (DW_AT_decl_file);
-  func->setDefSrc (get_source (fileno));
+  func->setDefSrc (get_decl_source ());
 }
 
 SourceFile *
 DwrCU::get_source (int fileno)
 {
-  if (fileno > 0 && fileno < VecSize (srcFiles))
+  // File numbers start at 1 in the line number programs before DWARF 5, and
+  // 0 means "no file".  In DWARF 5 they start at 0 and entry 0 is the
+  // primary source file; Clang uses DW_AT_decl_file 0 for entities declared
+  // in it.
+  int first = (dwrLineReg && dwrLineReg->get_version () >= 5) ? 0 : 1;
+  if (fileno >= first && fileno < VecSize (srcFiles))
     return srcFiles->get (fileno);
   return NULL;
 }
 
+// The source file named by DW_AT_decl_file of the current DIE, or NULL if
+// the DIE has none (so that callers can look at the declaration instead).
+SourceFile *
+DwrCU::get_decl_source ()
+{
+  if (dwrTag.get_attr (DW_AT_decl_file) == NULL)
+    return NULL;
+  return get_source ((int) Dwarf_data (DW_AT_decl_file));
+}
+
 void
 DwrCU::inherit_prop (int64_t offset, source_t *src)
 {
   if (src->lineno == 0)
     src->lineno = (int) Dwarf_data (DW_AT_decl_line);
   if (src->sf == NULL)
-    src->sf = get_source ((int) Dwarf_data (DW_AT_decl_file));
+    src->sf = get_decl_source ();
 
   int64_t old_offset = dwrTag.offset;
   if (set_die (offset) == DW_DLV_OK)
@@ -2226,7 +2240,7 @@ DwrCU::inherit_prop (int64_t offset, source_t *src)
       if (src->lineno == 0)
 	src->lineno = (int) Dwarf_data (DW_AT_decl_line);
       if (src->sf == NULL)
-	src->sf = get_source ((int) Dwarf_data (DW_AT_decl_file));
+	src->sf = get_decl_source ();
 
       Dwr_Attr *dwrAttr = dwrTag.get_attr (DW_AT_specification);
       if (dwrAttr)
@@ -2283,7 +2297,7 @@ DwrCU::find_declaration (int64_t offset, source_t *src)
       if (src->lineno == 0)
 	src->lineno = (int) Dwarf_data (DW_AT_decl_line);
       if (src->sf == NULL)
-	src->sf = get_source ((int) Dwarf_data (DW_AT_decl_file));
+	src->sf = get_decl_source ();
 
       sym = Symbol::get_symbol (symbols_sorted_by_name, get_linkage_name ());
       if (sym && sym->func == NULL)
diff --git a/gprofng/src/DwarfLib.h b/gprofng/src/DwarfLib.h
index def82bd4c32..b91750944be 100644
--- a/gprofng/src/DwarfLib.h
+++ b/gprofng/src/DwarfLib.h
@@ -135,6 +135,7 @@ public:
   char *getPath (int fn);
   Vector<DwrLine *> *get_lines ();
   void dump ();
+  Dwarf_Half get_version () { return version; }
 
   Vector<DwrFileName *> *file_names;
 
@@ -344,6 +345,7 @@ private:
   void update_source (Symbol *sym, source_t *prop);
   Symbol *find_declaration (int64_t offset, source_t *prop);
   SourceFile *get_source (int fileno);
+  SourceFile *get_decl_source ();
   Vector<Symbol *> *get_symbols (Vector<Symbol *> *syms = NULL);
   Vector <Range *> *get_ranges();
   void set_source (Function *func);
diff --git a/gprofng/testsuite/gprofng.display/dwarf5.exp b/gprofng/testsuite/gprofng.display/dwarf5.exp
index e98c99433c5..e67c8b80b05 100644
--- a/gprofng/testsuite/gprofng.display/dwarf5.exp
+++ b/gprofng/testsuite/gprofng.display/dwarf5.exp
@@ -29,9 +29,12 @@ set tdir "tmpdir/dwarf5"
 
 run_native_host_cmd "mkdir -p $tdir"
 run_native_host_cmd "cp $srcdir/lib/dwarf5test.c $tdir/t.c"
+run_native_host_cmd "cp $srcdir/lib/dwarf5decl.h $tdir/dwarf5decl.h"
 
 # Build t.c with COMPILER and -gdwarf-5, profile it, and check that the
 # hot function has a line number and that its source file can be found.
+# hot_function inlines a function from dwarf5decl.h; Clang describes
+# hot_function itself with DW_AT_decl_file 0 (the primary file in DWARF 5).
 proc check_dwarf5 { name compiler {flags ""} } {
   global gprofng tdir
 
diff --git a/gprofng/testsuite/lib/dwarf5decl.h b/gprofng/testsuite/lib/dwarf5decl.h
new file mode 100644
index 00000000000..0c6147e388b
--- /dev/null
+++ b/gprofng/testsuite/lib/dwarf5decl.h
@@ -0,0 +1,12 @@
+/* Header for dwarf5.exp: a function that is inlined into hot_function,
+   so that the line table of the test program refers to a second file.  */
+#ifndef DWARF5DECL_H
+#define DWARF5DECL_H
+
+static inline long
+step (long v)
+{
+  return v * 3 + 1;
+}
+
+#endif
diff --git a/gprofng/testsuite/lib/dwarf5test.c b/gprofng/testsuite/lib/dwarf5test.c
index a18776e91a1..c4307d292dd 100644
--- a/gprofng/testsuite/lib/dwarf5test.c
+++ b/gprofng/testsuite/lib/dwarf5test.c
@@ -1,7 +1,9 @@
-/* Test program for dwarf5.exp: one hot function that runs for about a second,
-   so that the profile has samples attributed to a known source line.  */
+/* Test program for dwarf5.exp: the hot function is declared in this file and
+   inlines code from dwarf5decl.h.  Clang describes such a function with
+   DW_AT_decl_file 0, the primary source file of the DWARF 5 file table.  */
 #include <stdio.h>
 #include <time.h>
+#include "dwarf5decl.h"
 
 typedef long long hrtime_t;
 
@@ -27,7 +29,7 @@ __attribute__ ((noinline)) void
 hot_function (void)
 {
   for (int j = 0; j < 1000000; j++)
-    x = x + 1;
+    x = step (x);
 }
 
 int
-- 
2.56.0



More information about the Binutils mailing list