From d797c2ffdb591d10de2964907962aaacb2e360ca Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 14 Aug 2022 20:58:23 -0700 Subject: [PATCH] [DebugInfo] -fdebug-prefix-map: handle '#line "file"' for asm source `getContext().setMCLineTableRootFile` (from D62074) sets `RootFile.Name` to `FirstCppHashFilename`. `RootFile.Name` is not processed by -fdebug-prefix-map and will go to DW_TAG_compile_unit's DT_AT_name and DW_TAG_label's DW_AT_decl_file. Remap `RootFile.Name`. Fix another issue reported by https://github.com/llvm/llvm-project/issues/56609 Reviewed By: #debug-info, dblaikie, raj.khem Differential Revision: https://reviews.llvm.org/D131848 --- llvm/include/llvm/MC/MCDwarf.h | 1 + llvm/lib/MC/MCContext.cpp | 8 +++++++- llvm/test/MC/ELF/debug-hash-file.s | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/MC/MCDwarf.h b/llvm/include/llvm/MC/MCDwarf.h index 8f648900fa53..557c713575e4 100644 --- a/llvm/include/llvm/MC/MCDwarf.h +++ b/llvm/include/llvm/MC/MCDwarf.h @@ -387,6 +387,7 @@ public: bool hasRootFile() const { return !Header.RootFile.Name.empty(); } + MCDwarfFile &getRootFile() { return Header.RootFile; } const MCDwarfFile &getRootFile() const { return Header.RootFile; } // Report whether MD5 usage has been consistent (all-or-none). diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index c6fe3fc56655..29597088a212 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -869,7 +869,7 @@ void MCContext::RemapDebugPaths() { // Remap compilation directory. remapDebugPath(CompilationDir); - // Remap MCDwarfDirs in all compilation units. + // Remap MCDwarfDirs and RootFile.Name in all compilation units. SmallString<256> P; for (auto &CUIDTablePair : MCDwarfLineTablesCUMap) { for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs()) { @@ -877,6 +877,12 @@ void MCContext::RemapDebugPaths() { remapDebugPath(P); Dir = std::string(P); } + + // Used by DW_TAG_compile_unit's DT_AT_name and DW_TAG_label's + // DW_AT_decl_file for DWARF v5 generated for assembly source. + P = CUIDTablePair.second.getRootFile().Name; + remapDebugPath(P); + CUIDTablePair.second.getRootFile().Name = std::string(P); } } diff --git a/llvm/test/MC/ELF/debug-hash-file.s b/llvm/test/MC/ELF/debug-hash-file.s index 9d4cf6ae68bd..99e3d6dca9bb 100644 --- a/llvm/test/MC/ELF/debug-hash-file.s +++ b/llvm/test/MC/ELF/debug-hash-file.s @@ -23,6 +23,26 @@ // DWARF5-NEXT: dir_index: 0 // DWARF5-NOT: file_names[ 1]: +// RUN: llvm-mc -triple=x86_64 -filetype=obj -g -dwarf-version=4 -fdebug-prefix-map=/MyTest=/src_root %s -o %t.4.o +// RUN: llvm-dwarfdump -debug-info -debug-line %t.4.o | FileCheck %s --check-prefixes=MAP,MAP_V4 +// RUN: llvm-mc -triple=x86_64 -filetype=obj -g -dwarf-version=5 -fdebug-prefix-map=/MyTest=/src_root %s -o %t.5.o +// RUN: llvm-dwarfdump -debug-info -debug-line %t.5.o | FileCheck %s --check-prefixes=MAP,MAP_V5 + +// MAP-LABEL: DW_TAG_compile_unit +// MAP: DW_AT_name ("/src_root/Inputs{{(/|\\)+}}other.S") +// MAP-LABEL: DW_TAG_label +// MAP: DW_AT_decl_file ("/src_root/Inputs{{(/|\\)+}}other.S") + +// MAP_V4: include_directories[ 1] = "/src_root/Inputs" +// MAP_V4-NEXT: file_names[ 1]: +// MAP_V4-NEXT: name: "other.S" +// MAP_V4-NEXT: dir_index: 1 + +// MAP_V5: include_directories[ 0] = "{{.*}}" +// MAP_V5-NEXT: file_names[ 0]: +// MAP_V5-NEXT: name: "/src_root/Inputs/other.S" +// MAP_V5-NEXT: dir_index: 0 + # 1 "/MyTest/Inputs/other.S" foo: