Skip to content

Commit f62e60f

Browse files
committed
[MCDwarf] Respect -fdebug-prefix-map= for generated assembly debug info (DWARF v5)
For generated assembly debug info, MCDwarfLineTableHeader::CompilationDir is an unmapped path set in MCContext::setGenDwarfRootFile. Remap it. A relative destination path of -fdebug-prefix-map= exposes a llvm-dwarfdump bug which joins relative DW_AT_comp_dir and directories[0]. Fix llvm#56609 Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D131749
1 parent 5311351 commit f62e60f

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,9 @@ class MCContext {
698698
/// Add an entry to the debug prefix map.
699699
void addDebugPrefixMapEntry(const std::string &From, const std::string &To);
700700

701+
/// Remap one path in-place as per the debug prefix map.
702+
void remapDebugPath(SmallVectorImpl<char> &Path);
703+
701704
// Remaps all debug directory paths in-place as per the debug prefix map.
702705
void RemapDebugPaths();
703706

llvm/include/llvm/MC/MCDwarf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/MC/StringTableBuilder.h"
2323
#include "llvm/Support/Error.h"
2424
#include "llvm/Support/MD5.h"
25+
#include "llvm/Support/StringSaver.h"
2526
#include <cassert>
2627
#include <cstdint>
2728
#include <string>
@@ -48,6 +49,8 @@ MCSymbol *emitListsTableHeaderStart(MCStreamer &S);
4849

4950
/// Manage the .debug_line_str section contents, if we use it.
5051
class MCDwarfLineStr {
52+
BumpPtrAllocator Alloc;
53+
StringSaver Saver{Alloc};
5154
MCSymbol *LineStrLabel = nullptr;
5255
StringTableBuilder LineStrings{StringTableBuilder::DWARF};
5356
bool UseRelocs = false;
@@ -57,6 +60,8 @@ class MCDwarfLineStr {
5760
/// v5 line table).
5861
explicit MCDwarfLineStr(MCContext &Ctx);
5962

63+
StringSaver &getSaver() { return Saver; }
64+
6065
/// Emit a reference to the string.
6166
void emitRef(MCStreamer *MCOS, StringRef Path);
6267

llvm/lib/MC/MCContext.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -855,30 +855,29 @@ void MCContext::addDebugPrefixMapEntry(const std::string &From,
855855
DebugPrefixMap.insert(std::make_pair(From, To));
856856
}
857857

858+
void MCContext::remapDebugPath(SmallVectorImpl<char> &Path) {
859+
for (const auto &[From, To] : DebugPrefixMap)
860+
if (llvm::sys::path::replace_path_prefix(Path, From, To))
861+
break;
862+
}
863+
858864
void MCContext::RemapDebugPaths() {
859865
const auto &DebugPrefixMap = this->DebugPrefixMap;
860866
if (DebugPrefixMap.empty())
861867
return;
862868

863-
const auto RemapDebugPath = [&DebugPrefixMap](std::string &Path) {
864-
SmallString<256> P(Path);
865-
for (const auto &Entry : DebugPrefixMap) {
866-
if (llvm::sys::path::replace_path_prefix(P, Entry.first, Entry.second)) {
867-
Path = P.str().str();
868-
break;
869-
}
870-
}
871-
};
872-
873869
// Remap compilation directory.
874-
std::string CompDir = std::string(CompilationDir.str());
875-
RemapDebugPath(CompDir);
876-
CompilationDir = CompDir;
870+
remapDebugPath(CompilationDir);
877871

878872
// Remap MCDwarfDirs in all compilation units.
879-
for (auto &CUIDTablePair : MCDwarfLineTablesCUMap)
880-
for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs())
881-
RemapDebugPath(Dir);
873+
SmallString<256> P;
874+
for (auto &CUIDTablePair : MCDwarfLineTablesCUMap) {
875+
for (auto &Dir : CUIDTablePair.second.getMCDwarfDirs()) {
876+
P = Dir;
877+
remapDebugPath(P);
878+
Dir = std::string(P);
879+
}
880+
}
882881
}
883882

884883
//===----------------------------------------------------------------------===//

llvm/lib/MC/MCDwarf.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,15 @@ void MCDwarfLineTableHeader::emitV5FileDirTables(
416416
: dwarf::DW_FORM_string);
417417
MCOS->emitULEB128IntValue(MCDwarfDirs.size() + 1);
418418
// Try not to emit an empty compilation directory.
419-
const StringRef CompDir = CompilationDir.empty()
420-
? MCOS->getContext().getCompilationDir()
421-
: StringRef(CompilationDir);
419+
SmallString<256> Dir;
420+
StringRef CompDir = MCOS->getContext().getCompilationDir();
421+
if (!CompilationDir.empty()) {
422+
Dir = CompilationDir;
423+
MCOS->getContext().remapDebugPath(Dir);
424+
CompDir = Dir.str();
425+
if (LineStr)
426+
CompDir = LineStr->getSaver().save(CompDir);
427+
}
422428
if (LineStr) {
423429
// Record path strings, emit references here.
424430
LineStr->emitRef(MCOS, CompDir);

llvm/test/MC/ELF/debug-prefix-map.s

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ f:
3131
# MAP_V4: DW_AT_decl_file [DW_FORM_data4] ("src_root{{(/|\\)+}}src.s")
3232
# MAP_V4-NOT: .foo
3333

34-
## FIXME DW_AT_decl_file and include_directories are not mapped
3534
# MAP_V5: DW_AT_name [DW_FORM_string] ("src.s")
3635
# MAP_V5: DW_AT_comp_dir [DW_FORM_string] ("src_root")
37-
# MAP_V5: DW_AT_decl_file [DW_FORM_data4] ("{{.*}}.foo{{(/|\\)+}}src.s")
38-
# MAP_V5: include_directories[ 0] = .debug_line_str[0x00000000] = "{{.*}}.foo"
36+
## FIXME llvm-dwarfdump incorrectly joins include_directories[0] to DW_AT_comp_dir,
37+
## so there are two src_root path components.
38+
# MAP_V5: DW_AT_decl_file [DW_FORM_data4] ("src_root{{(/|\\)+}}src_root{{(/|\\)+}}src.s")
39+
# MAP_V5: include_directories[ 0] = .debug_line_str[0x00000000] = "src_root"
3940

4041
# MAPABS_V4: DW_AT_name [DW_FORM_string] ("src.s")
4142
# MAPABS_V4: DW_AT_comp_dir [DW_FORM_string] ("{{(/|\\)+}}src_root")
4243
# MAPABS_V4: DW_AT_decl_file [DW_FORM_data4] ("{{(/|\\)+}}src_root{{(/|\\)+}}src.s")
4344
# MAPABS_V4-NOT: .foo
4445

45-
## FIXME DW_AT_decl_file and include_directories are not mapped
4646
# MAPABS_V5: DW_AT_name [DW_FORM_string] ("src.s")
4747
# MAPABS_V5: DW_AT_comp_dir [DW_FORM_string] ("{{(/|\\)+}}src_root")
48-
# MAPABS_V5: DW_AT_decl_file [DW_FORM_data4] ("{{.*}}.foo{{(/|\\)+}}src.s")
49-
# MAPABS_V5: include_directories[ 0] = .debug_line_str[0x00000000] = "{{.*}}.foo"
48+
# MAPABS_V5: DW_AT_decl_file [DW_FORM_data4] ("/src_root{{(/|\\)+}}src.s")
49+
# MAPABS_V5: include_directories[ 0] = .debug_line_str[0x00000000] = "/src_root"

0 commit comments

Comments
 (0)