Skip to content

Conversation

@noxwell
Copy link
Contributor

@noxwell noxwell commented Dec 9, 2024

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.

Fixes: #119020

@github-actions
Copy link

github-actions bot commented Dec 9, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the llvm:mc Machine (object) code label Dec 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 9, 2024

@llvm/pr-subscribers-mc

Author: Aleksei Vetrov (noxwell)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/119229.diff

2 Files Affected:

  • (modified) llvm/lib/MC/MCParser/AsmParser.cpp (+15-10)
  • (added) llvm/test/MC/ELF/debug-hash-file-empty-dwarf.s (+26)
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 3ce45f7d5d67e1..00291147b68682 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -162,8 +162,8 @@ class AsmParser : public MCAsmParser {
   };
   CppHashInfoTy CppHashInfo;
 
-  /// The filename from the first cpp hash file line comment, if any.
-  StringRef FirstCppHashFilename;
+  /// Have we seen any file line comment.
+  bool HadCppHashFilename = false;
 
   /// List of forward directional labels for diagnosis at the end.
   SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
@@ -952,12 +952,6 @@ bool AsmParser::enabledGenDwarfForAssembly() {
   // the assembler source was produced with debug info already) then emit one
   // describing the assembler source file itself.
   if (getContext().getGenDwarfFileNumber() == 0) {
-    // Use the first #line directive for this, if any. It's preprocessed, so
-    // there is no checksum, and of course no source directive.
-    if (!FirstCppHashFilename.empty())
-      getContext().setMCLineTableRootFile(
-          /*CUID=*/0, getContext().getCompilationDir(), FirstCppHashFilename,
-          /*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
     const MCDwarfFile &RootFile =
         getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
     getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
@@ -2440,8 +2434,19 @@ bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
   CppHashInfo.Filename = Filename;
   CppHashInfo.LineNumber = LineNumber;
   CppHashInfo.Buf = CurBuffer;
-  if (FirstCppHashFilename.empty())
-    FirstCppHashFilename = Filename;
+  if (!HadCppHashFilename) {
+    HadCppHashFilename = true;
+    // If we haven't encountered any .file directives, then the first #line
+    // directive describes the "root" file and directory of the compilation
+    // unit.
+    if (getContext().getGenDwarfFileNumber() == 0) {
+      // It's preprocessed, so there is no checksum, and of course no source
+      // directive.
+      getContext().setMCLineTableRootFile(
+          /*CUID=*/0, getContext().getCompilationDir(), Filename,
+          /*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
+    }
+  }
   return false;
 }
 
diff --git a/llvm/test/MC/ELF/debug-hash-file-empty-dwarf.s b/llvm/test/MC/ELF/debug-hash-file-empty-dwarf.s
new file mode 100644
index 00000000000000..bf7f9cb53b5b0e
--- /dev/null
+++ b/llvm/test/MC/ELF/debug-hash-file-empty-dwarf.s
@@ -0,0 +1,26 @@
+// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -g -dwarf-version 5 -o %t %s
+// RUN: llvm-dwarfdump -debug-info -debug-line %t | FileCheck %s
+
+// CHECK-NOT: DW_TAG_
+
+// CHECK:      include_directories[ 0] =
+// CHECK-NOT:  include_directories[ 1] =
+// CHECK:      file_names[ 0]:
+// CHECK-NEXT:           name: "/MyTest/Inputs/other.S"
+// CHECK-NEXT:      dir_index: 0
+// CHECK-NOT:  file_names[ 1]:
+
+// 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-NOT: DW_TAG_
+
+// MAP:      include_directories[  0] = "{{.*}}"
+// MAP-NEXT: file_names[  0]:
+// MAP-NEXT:            name: "/src_root/Inputs/other.S"
+// MAP-NEXT:       dir_index: 0
+
+# 1 "/MyTest/Inputs/other.S"
+
+.section .data
+    .asciz "data"

@noxwell
Copy link
Contributor Author

noxwell commented Dec 9, 2024

I've noticed that MasmParser.cpp is a copypaste of AsmParser.cpp and it has the same bug. Is it worth to copy the fix there also? Where are the tests for it, is it llvm/test/tools/llvm-ml? And it looks like MasmParser.cpp doesn't have any test coverage related to DWARF information...

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.

Fixes: llvm#119020
@nickdesaulniers
Copy link
Member

And it looks like MasmParser.cpp doesn't have any test coverage related to DWARF information...

IIUC, masm is the "Microsoft Macro Assembler" (https://en.wikipedia.org/wiki/Microsoft_Macro_Assembler). Windows doesn't use ELF for its object file container, and I think DWARF is only supported from ELF and mach-o (darwin-derived platforms). but @rnk or @zmodem could clarify if I'm wrong.

Where are the tests for it, is it llvm/test/tools/llvm-ml

Looks like it.

Is it worth to copy the fix there also?

Maybe @rnk @zmodem @ericastor @nico could help answer that?

@ericastor
Copy link
Contributor

It doesn't seem like it would be bad to include this in MasmParser.cpp as well? It is true that it was forked from AsmParser.cpp... but also, I'm not sure if a test case is feasible, since (as mentioned) it's really designed for targeting Windows. I'll let @rnk weigh in on whether it'd be worthwhile.

Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this change as-is. The Masm discussion can continue/be addressed in a separate change if needed.

@zmodem
Copy link
Collaborator

zmodem commented Dec 10, 2024

I think DWARF is only supported from ELF and mach-o (darwin-derived platforms). but @rnk or @zmodem could clarify if I'm wrong.

DWARF is not normally used on Windows, but it can be used (Clang can emit it, and I think it's not uncommon in MinGW builds maybe), and I believe LLDB can consume it on Windows.

Using MASM to generate DWARF does seem like a fringe use case though. I don't even know if there are flags to actually do so.

The Masm discussion can continue/be addressed in a separate change if needed.

This sounds reasonable to me.

@nickdesaulniers nickdesaulniers merged commit 5041d06 into llvm:main Dec 10, 2024
7 checks passed
@github-actions
Copy link

@noxwell Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@chapuni
Copy link
Contributor

chapuni commented Dec 10, 2024

This causes emitting .debug_line w/o any dwarf options in clang %.S. Is it desired?

It makes object files contain the absolute path of build directory for building llvm/lib/Support/BLAKE3.

@nickdesaulniers
Copy link
Member

This causes emitting .debug_line w/o any dwarf options in clang %.S. Is it desired?

Probably not, but it seems like there were no tests in tree for that!

@noxwell
Copy link
Contributor Author

noxwell commented Dec 11, 2024

This causes emitting .debug_line w/o any dwarf options in clang %.S. Is it desired?

Yes, it needs a check if (getContext().getGenDwarfForAssembly()), which I forgot. I don't think I have rollback permissions, could anyone do it for me? Or I can bring fix forward tomorrow.

dwblaikie added a commit that referenced this pull request Dec 11, 2024
dwblaikie added a commit that referenced this pull request Dec 11, 2024
…" (#119486)

Reverts #119229

Causes debug info to be unconditionally emitted, regardless of whether
it's requested.
noxwell added a commit to noxwell/llvm-project that referenced this pull request Dec 11, 2024
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 llvm#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: llvm#119020
Fixes: llvm#119229
noxwell added a commit to noxwell/llvm-project that referenced this pull request Dec 12, 2024
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 llvm#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: llvm#119020
Fixes: llvm#119229
nickdesaulniers pushed a commit that referenced this pull request Dec 12, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MC] Compiler produces incorrect DWARF file/line table on some assembly files

7 participants