[PATCH] D105662: Fix incorrect DWARF 5 file name 0 when using -no-integrated-as

Omar Sandoval via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 8 14:38:16 PDT 2021


osandov created this revision.
osandov added a reviewer: hubert.reinterpretcast.
Herald added a subscriber: hiraditya.
osandov requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

DWARF 5 specifies that in a line number program header in .debug_line,
directory entry 0 must be the compilation directory and file name entry
0 must be the compilation file name. Consider the following correct
output:

  $ pwd
  /home/osandov/test
  $ cat foo/bar/baz.c
  int my_var = 1;
  $ clang -g -gdwarf-5 -c foo/bar/baz.c
  $ readelf --debug-dump=line baz.o
  ...
   The Directory Table (offset 0x22, lines 1, columns 1):
    Entry Name
    0     (indirect line string, offset: 0x0): /home/osandov/test
  
   The File Name Table (offset 0x2e, lines 1, columns 3):
    Entry Dir     MD5                             Name
    0     0 0x436e7c8f87b60ba4ecab92f1eddfcbb4    (indirect line string, offset: 0x13): foo/bar/baz.c

However, when using `-no-integrated-as`, the compilation directory is
incorrectly given as the parent directory of the source file, and the
compilation file name is given relative to that:

  $ clang -g -gdwarf-5 -no-integrated-as -c foo/bar/baz.c
  $ readelf --debug-dump=line baz.o
  ...
   The Directory Table (offset 0x22, lines 1, columns 1):
    Entry Name
    0     (indirect line string, offset: 0x0): /home/osandov/test/foo/bar
  
   The File Name Table (offset 0x2e, lines 1, columns 3):
    Entry Dir     MD5                             Name
    0     0 0xb4cbdfedf192abeca40bb6878f7c6e43    (indirect line string, offset: 0x1b): baz.c

This is because the generated assembly `.file` directive does not split
the directory from the file name if `-no-integrated-as` is used:

  $ clang -g -gdwarf-5 -S foo/bar/baz.c -o - | grep '\.file\s\+0'
          .file   0 "/home/osandov/test" "foo/bar/baz.c" md5 0xb4cbdfedf192abeca40bb6878f7c6e43
  $ clang -g -gdwarf-5 -no-integrated-as -S foo/bar/baz.c -o - | grep '\.file\s\+0'
          .file   0 "/home/osandov/test/foo/bar/baz.c" md5 0xb4cbdfedf192abeca40bb6878f7c6e43

This is ostensibly for compatibility, since the [extended `.file`
syntax](https://sourceware.org/binutils/docs/as/File.html) that allows
specifying the directory separately was only recently added to the GNU
Assembler in binutils
2.35 <https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=5496f3c635dce3e12348d6e81c3f74815fdfe7b5>.

But, the `md5` argument also relies on the extended syntax, so this
isn't actually improving compatibility when using DWARF 5. Fix this bug
by always using the directory argument for DWARF 5.

I noticed this bug when compiling the Linux kernel with Clang and DWARF
5, as the Linux kernel is compiled with `-no-integrated-as`.

Test Plan:
Ran the reproducer above and saw that it matched the `-integrated-as`
version.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105662

Files:
  llvm/lib/MC/MCAsmStreamer.cpp


Index: llvm/lib/MC/MCAsmStreamer.cpp
===================================================================
--- llvm/lib/MC/MCAsmStreamer.cpp
+++ llvm/lib/MC/MCAsmStreamer.cpp
@@ -65,7 +65,6 @@
                                StringRef Filename,
                                Optional<MD5::MD5Result> Checksum,
                                Optional<StringRef> Source,
-                               bool UseDwarfDirectory,
                                raw_svector_ostream &OS) const;
   void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
   void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
@@ -1450,13 +1449,16 @@
   EmitEOL();
 }
 
-void MCAsmStreamer::printDwarfFileDirective(
-    unsigned FileNo, StringRef Directory, StringRef Filename,
-    Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source,
-    bool UseDwarfDirectory, raw_svector_ostream &OS) const {
+void MCAsmStreamer::printDwarfFileDirective(unsigned FileNo,
+                                            StringRef Directory,
+                                            StringRef Filename,
+                                            Optional<MD5::MD5Result> Checksum,
+                                            Optional<StringRef> Source,
+                                            raw_svector_ostream &OS) const {
   SmallString<128> FullPathName;
 
-  if (!UseDwarfDirectory && !Directory.empty()) {
+  if (getContext().getDwarfVersion() < 5 && !UseDwarfDirectory &&
+      !Directory.empty()) {
     if (sys::path::is_absolute(Filename))
       Directory = "";
     else {
@@ -1503,8 +1505,7 @@
 
   SmallString<128> Str;
   raw_svector_ostream OS1(Str);
-  printDwarfFileDirective(FileNo, Directory, Filename, Checksum, Source,
-                          UseDwarfDirectory, OS1);
+  printDwarfFileDirective(FileNo, Directory, Filename, Checksum, Source, OS1);
 
   if (MCTargetStreamer *TS = getTargetStreamer())
     TS->emitDwarfFileDirective(OS1.str());
@@ -1533,8 +1534,7 @@
 
   SmallString<128> Str;
   raw_svector_ostream OS1(Str);
-  printDwarfFileDirective(0, Directory, Filename, Checksum, Source,
-                          UseDwarfDirectory, OS1);
+  printDwarfFileDirective(0, Directory, Filename, Checksum, Source, OS1);
 
   if (MCTargetStreamer *TS = getTargetStreamer())
     TS->emitDwarfFileDirective(OS1.str());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105662.357365.patch
Type: text/x-patch
Size: 2343 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210708/fdfa1adc/attachment.bin>


More information about the llvm-commits mailing list