Skip to content

Commit 4a5f82b

Browse files
authored
[MC] Fix DWARF file table for files with empty DWARF (#119572)
Update root file in DWARF file/line table as soon as we see the first "#line" directive. This was moved from "enabledGenDwarfForAssembly", which is called right before we emit DWARF information. But if the file is empty or contains expressions that doesn't need DWARF, it is never called, leaving an original root file and not the file in the "#line" directive. Add a test checking for this case. This is reapply of #119229 with the following fix: "MCContext::setMCLineTableRootFile" has the effect of adding ".debug_line" section to the output, even if DWARF generation is disabled. Add a check and a test for this case. Fixes: #119020 Fixes: #119229
1 parent 8eec301 commit 4a5f82b

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ class AsmParser : public MCAsmParser {
162162
};
163163
CppHashInfoTy CppHashInfo;
164164

165-
/// The filename from the first cpp hash file line comment, if any.
166-
StringRef FirstCppHashFilename;
165+
/// Have we seen any file line comment.
166+
bool HadCppHashFilename = false;
167167

168168
/// List of forward directional labels for diagnosis at the end.
169169
SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
@@ -952,12 +952,6 @@ bool AsmParser::enabledGenDwarfForAssembly() {
952952
// the assembler source was produced with debug info already) then emit one
953953
// describing the assembler source file itself.
954954
if (getContext().getGenDwarfFileNumber() == 0) {
955-
// Use the first #line directive for this, if any. It's preprocessed, so
956-
// there is no checksum, and of course no source directive.
957-
if (!FirstCppHashFilename.empty())
958-
getContext().setMCLineTableRootFile(
959-
/*CUID=*/0, getContext().getCompilationDir(), FirstCppHashFilename,
960-
/*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
961955
const MCDwarfFile &RootFile =
962956
getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
963957
getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
@@ -2440,8 +2434,20 @@ bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
24402434
CppHashInfo.Filename = Filename;
24412435
CppHashInfo.LineNumber = LineNumber;
24422436
CppHashInfo.Buf = CurBuffer;
2443-
if (FirstCppHashFilename.empty())
2444-
FirstCppHashFilename = Filename;
2437+
if (!HadCppHashFilename) {
2438+
HadCppHashFilename = true;
2439+
// If we haven't encountered any .file directives, then the first #line
2440+
// directive describes the "root" file and directory of the compilation
2441+
// unit.
2442+
if (getContext().getGenDwarfForAssembly() &&
2443+
getContext().getGenDwarfFileNumber() == 0) {
2444+
// It's preprocessed, so there is no checksum, and of course no source
2445+
// directive.
2446+
getContext().setMCLineTableRootFile(
2447+
/*CUID=*/0, getContext().getCompilationDir(), Filename,
2448+
/*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
2449+
}
2450+
}
24452451
return false;
24462452
}
24472453

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -g -dwarf-version 5 -o %t %s
2+
// RUN: llvm-dwarfdump -debug-info -debug-line %t | FileCheck %s
3+
4+
// CHECK-NOT: DW_TAG_
5+
6+
// CHECK: include_directories[ 0] =
7+
// CHECK-NOT: include_directories[ 1] =
8+
// CHECK: file_names[ 0]:
9+
// CHECK-NEXT: name: "/MyTest/Inputs/other.S"
10+
// CHECK-NEXT: dir_index: 0
11+
// CHECK-NOT: file_names[ 1]:
12+
13+
// RUN: llvm-mc -triple=x86_64 -filetype=obj -g -dwarf-version=5 -fdebug-prefix-map=/MyTest=/src_root %s -o %t.5.o
14+
// RUN: llvm-dwarfdump -debug-info -debug-line %t.5.o | FileCheck %s --check-prefixes=MAP
15+
16+
// MAP-NOT: DW_TAG_
17+
18+
// MAP: include_directories[ 0] = "{{.*}}"
19+
// MAP-NEXT: file_names[ 0]:
20+
// MAP-NEXT: name: "/src_root/Inputs/other.S"
21+
// MAP-NEXT: dir_index: 0
22+
23+
# 1 "/MyTest/Inputs/other.S"
24+
25+
.section .data
26+
.asciz "data"

llvm/test/MC/ELF/debug-hash-file.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
// MAP_V5-NEXT: name: "/src_root/Inputs/other.S"
4444
// MAP_V5-NEXT: dir_index: 0
4545

46+
// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -o %t %s
47+
// RUN: llvm-readelf --sections %t | FileCheck %s --check-prefix=CHECK-NO-DEBUG
48+
49+
// CHECK-NO-DEBUG: Section Headers:
50+
// CHECK-NO-DEBUG-NOT: .debug_
51+
4652
# 1 "/MyTest/Inputs/other.S"
4753

4854
foo:

0 commit comments

Comments
 (0)