[DebugInfo] Don't join DW_AT_comp_dir and directories[0] for DWARF v5 line tables
authorFangrui Song <i@maskray.me>
Fri, 12 Aug 2022 21:01:52 +0000 (14:01 -0700)
committerFangrui Song <i@maskray.me>
Fri, 12 Aug 2022 21:01:52 +0000 (14:01 -0700)
DWARF v5 6.2.4 The Line Number Program Header says:

> The first entry is the current directory of the compilation. Each additional
> path entry is either a full path name or is relative to the current directory of
> the compilation.

When forming a path, relative DW_AT_comp_dir and directories[0] are not supposed
to be joined together. Fix getFileNameByIndex to special case DWARF v5 DirIdx == 0.

Reviewed By: #debug-info, dblaikie

Differential Revision: https://reviews.llvm.org/D131804

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
llvm/test/DebugInfo/X86/symbolize_function_start_v5.s
llvm/test/MC/ELF/debug-prefix-map.s

index d2ed4fe..5ea4c4c 100644 (file)
@@ -1382,10 +1382,12 @@ bool DWARFDebugLine::Prologue::getFileNameByIndex(
       IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);
   }
 
-  // For absolute paths only, include the compilation directory of compile unit.
-  // We know that FileName is not absolute, the only way to have an absolute
-  // path at this point would be if IncludeDir is absolute.
-  if (Kind == FileLineInfoKind::AbsoluteFilePath && !CompDir.empty() &&
+  // For absolute paths only, include the compilation directory of compile unit,
+  // unless v5 DirIdx == 0 (IncludeDir indicates the compilation directory). We
+  // know that FileName is not absolute, the only way to have an absolute path
+  // at this point would be if IncludeDir is absolute.
+  if (Kind == FileLineInfoKind::AbsoluteFilePath &&
+      (getVersion() < 5 || Entry.DirIdx != 0) && !CompDir.empty() &&
       !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
     sys::path::append(FilePath, Style, CompDir);
 
index f7c6a6c..c04e973 100644 (file)
@@ -5,8 +5,8 @@
 # RUN: llvm-symbolizer --verbose 0x0 --obj=test.o | FileCheck --check-prefix=SYM %s
 # RUN: llvm-dwarfdump -lookup=0x1 test.o | FileCheck --check-prefix=LOOKUP %s
 
-# SYM: Filename: .{{[/\\]}}.{{[/\\]}}test.c
-# SYM: Function start filename: .{{[/\\]}}.{{[/\\]}}test.c
+# SYM: Filename: .{{[/\\]}}test.c
+# SYM: Function start filename: .{{[/\\]}}test.c
 
 # LOOKUP: Line info: line 0, column 0, start file 'test.c', start line 1
 
index e7b1721..4e4041d 100644 (file)
@@ -33,14 +33,12 @@ f:
 
 # MAP_V5:      DW_AT_name [DW_FORM_string] ("src.s")
 # MAP_V5:      DW_AT_comp_dir [DW_FORM_string] ("src_root")
-## FIXME llvm-dwarfdump incorrectly joins include_directories[0] to DW_AT_comp_dir,
-## so there are two src_root path components.
-# MAP_V5:      DW_AT_decl_file [DW_FORM_data4] ("src_root{{(/|\\)+}}src_root{{(/|\\)+}}src.s")
+# MAP_V5:      DW_AT_decl_file [DW_FORM_data4] ("src_root{{(/|\\)+}}src.s")
 # MAP_V5:      include_directories[ 0] = .debug_line_str[0x00000000] = "src_root"
 
 # MAPABS_V4:      DW_AT_name [DW_FORM_string] ("src.s")
 # MAPABS_V4:      DW_AT_comp_dir [DW_FORM_string] ("{{(/|\\)+}}src_root")
-# MAPABS_V4:      DW_AT_decl_file [DW_FORM_data4] ("{{(/|\\)+}}src_root{{(/|\\)+}}src.s")
+# MAPABS_V4:      DW_AT_decl_file [DW_FORM_data4] ("/src_root{{(/|\\)+}}src.s")
 # MAPABS_V4-NOT:  .foo
 
 # MAPABS_V5:      DW_AT_name [DW_FORM_string] ("src.s")