]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
Handle DW_AT_decl_file 0
authorAaron Merey <amerey@redhat.com>
Sat, 10 Feb 2024 02:10:19 +0000 (21:10 -0500)
committerAaron Merey <amerey@redhat.com>
Tue, 13 Feb 2024 01:36:55 +0000 (20:36 -0500)
Modify dwarf_decl_file to support DW_AT_decl_file with value 0.

Because of inconsistencies in the DWARF 5 spec, it is ambiguous whether
DW_AT_decl_file value 0 is a valid .debug_line file table index for the
main source file or if it means that there is no source file specified.

dwarf_decl_file interprets DW_AT_decl_file 0 as meaning no source file
is specified.  This works with DWARF 5 produced by gcc, which duplicates
the main source file name at index 0 and 1 of the file table and avoids
using DW_AT_decl_file 0.

However clang uses DW_AT_decl_file 0 for the main source index with no
duplication at another index.  In this case dwarf_decl_file will be
unable to find the file name of the main file.

This patch changes dwarf_decl_file to treat DW_AT_decl_file 0 as a normal
index into the file table, allowing it to work with DWARF 5 debuginfo
produced by clang.

As for earlier DWARF versions which exclusively use DW_AT_decl_file 0
to indicate that no source file is specified, dwarf_decl_file will now
return the name "???" if called on a DIE with DW_AT_decl_file 0.

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

Signed-off-by: Aaron Merey <amerey@redhat.com>
libdw/dwarf_decl_file.c
tests/Makefile.am
tests/run-allfcts.sh
tests/testfile-dwarf5-line-clang.bz2 [new file with mode: 0755]

index 75662a334843f91002a3649fdd53f948518e5834..7dde4af07233a712a28a568f86ae174d4966b464 100644 (file)
@@ -48,13 +48,6 @@ dwarf_decl_file (Dwarf_Die *die)
                               &idx) != 0)
     return NULL;
 
-  /* Zero means no source file information available.  */
-  if (idx == 0)
-    {
-      __libdw_seterrno (DWARF_E_NO_ENTRY);
-      return NULL;
-    }
-
   /* Get the array of source files for the CU.  */
   struct Dwarf_CU *cu = attr_mem.cu;
   if (cu->lines == NULL)
@@ -70,8 +63,8 @@ dwarf_decl_file (Dwarf_Die *die)
 
   if (cu->lines == (void *) -1l)
     {
-      /* If the file index is not zero, there must be file information
-        available.  */
+      /* If DW_AT_decl_file was present, there should be file information
+       available.  */
       __libdw_seterrno (DWARF_E_INVALID_DWARF);
       return NULL;
     }
index 13bd9d56863559d6ac9e822e5b458378bcb91e77..b075e3c364a5496c47fa04270e77b1985f4c498e 100644 (file)
@@ -634,7 +634,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
             testfile-largealign.o.bz2 run-strip-largealign.sh \
             run-funcretval++11.sh \
             test-ar-duplicates.a.bz2 \
-            run-dwfl-core-noncontig.sh testcore-noncontig.bz2
+            run-dwfl-core-noncontig.sh testcore-noncontig.bz2 \
+            testfile-dwarf5-line-clang.bz2
 
 
 if USE_VALGRIND
index 9c0a55d86a1fd5d4dc6043d9b07131f2cbba3437..4dd0284a8812c9e66552c992cb4aa7e94c6b89ce 100755 (executable)
@@ -170,4 +170,21 @@ testrun_compare ${abs_builddir}/allfcts testfile-lto-gcc9 <<\EOF
 /home/mark/src/tests/testfile-lto-main.c:6:main
 EOF
 
+# = dwarf5-line.c =
+# int
+# main (int argc, char **argv)
+# {
+#   return 0;
+# }
+
+# Using clang version 17.0.4 (Fedora 17.0.4-1.fc39)
+# clang -gdwarf-5 -o testfile-dwarf5-line-clang dwarf5-line.c
+
+testfiles testfile-dwarf5-line-clang
+
+# Check that dwarf_decl_file can handle .debug_line file table index 0
+testrun_compare ${abs_builddir}/allfcts testfile-dwarf5-line-clang <<\EOF
+/home/amerey/test/dwarf5-line.c:2:main
+EOF
+
 exit 0
diff --git a/tests/testfile-dwarf5-line-clang.bz2 b/tests/testfile-dwarf5-line-clang.bz2
new file mode 100755 (executable)
index 0000000..bdd66e9
Binary files /dev/null and b/tests/testfile-dwarf5-line-clang.bz2 differ