]> granicus.if.org Git - llvm/commitdiff
[llvm] Reapply "Prevent duplicate files in debug line header in dwarf 5."
authorAli Tamur <tamur@google.com>
Tue, 26 Mar 2019 18:53:23 +0000 (18:53 +0000)
committerAli Tamur <tamur@google.com>
Tue, 26 Mar 2019 18:53:23 +0000 (18:53 +0000)
Reapply rL356941 after regenerating the object file in the failing test
llvm/test/tools/llvm-objdump/embedded-source.test from source.

Original commit message:

[llvm] Prevent duplicate files in debug line header in dwarf 5.

Motivation: In previous dwarf versions, file name indexes started from 1, and
the primary source file was not explicit. Dwarf 5 standard (6.2.4) prescribes
the primary source file to be explicitly given an entry with an index number 0.

The current implementation honors the specification by just duplicating the
main source file, once with index number 0, and later maybe with another
index number. While this is compliant with the letter of the standard, the
duplication causes problems for consumers of this information such as lldb.
(Some files are duplicated, where only some of them have a line table although
all refer to the same file)

With this change, dwarf 5 debug line section files always start from 0, and
the zeroth entry is not duplicated whenever possible. This requires different
handling of dwarf 4 and dwarf 5 during generation (e.g. when a function returns
an index zero for a file name, it signals an error in dwarf 4, but not in dwarf 5)
However, I think the minor complication is worth it, because it enables all
consumers (lldb, gdb, dwarfdump, objdump, and so on) to treat all files in the
file name list homogenously.

Tags: #llvm, #debug-info

Differential Revision: https://reviews.llvm.org/D59515

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357018 91177308-0d34-0410-b5e6-96231b3b80d8

14 files changed:
include/llvm/DebugInfo/DWARF/DWARFDebugLine.h
include/llvm/MC/MCContext.h
include/llvm/MC/MCDwarf.h
lib/CodeGen/AsmPrinter/DwarfCompileUnit.h
lib/CodeGen/AsmPrinter/DwarfDebug.cpp
lib/CodeGen/AsmPrinter/DwarfUnit.cpp
lib/DebugInfo/DWARF/DWARFDebugLine.cpp
lib/MC/MCContext.cpp
lib/MC/MCDwarf.cpp
test/MC/ARM/dwarf-asm-multiple-sections.s
test/MC/ELF/debug-mixed-md5.ll
test/MC/ELF/dwarf-file0.s
test/tools/llvm-objdump/Inputs/embedded-source
test/tools/llvm-objdump/X86/function-sections-line-numbers.s

index 1f36e0daed0c553c59b09c77edaca0a51324ee0b..6247c31d7176c3337f268eafe162e19639a80734 100644 (file)
@@ -275,6 +275,8 @@ public:
     SequenceVector Sequences;
 
   private:
+    const llvm::DWARFDebugLine::FileNameEntry &
+    getFileNameEntry(uint64_t Index) const;
     uint32_t findRowInSeq(const DWARFDebugLine::Sequence &Seq,
                           object::SectionedAddress Address) const;
     Optional<StringRef>
index c87df5a82ce2170dc3554601dc0487cd34dec6f7..6beb646396453579c7a65f4e8dcde93579a6e592 100644 (file)
@@ -521,7 +521,7 @@ namespace llvm {
     }
 
     MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) {
-      return MCDwarfLineTablesCUMap[CUID];
+      return MCDwarfLineTablesCUMap.emplace(CUID, DwarfVersion).first->second;
     }
 
     const MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) const {
index 991ad525f89c81401d7453f97c7f7ee830df2d1a..70624d0a4df5c79ebaff4f685c038464d6153c20 100644 (file)
@@ -218,9 +218,11 @@ struct MCDwarfLineTableHeader {
 private:
   bool HasAllMD5 = true;
   bool HasAnyMD5 = false;
+  unsigned DwarfVersion;
 
 public:
-  MCDwarfLineTableHeader() = default;
+  explicit MCDwarfLineTableHeader(unsigned DwarfVersion) :
+    DwarfVersion(DwarfVersion) {}
 
   Expected<unsigned> tryGetFile(StringRef &Directory, StringRef &FileName,
                                 MD5::MD5Result *Checksum,
@@ -245,6 +247,17 @@ public:
     return MCDwarfFiles.empty() || (HasAllMD5 == HasAnyMD5);
   }
 
+  void setRootFile(StringRef Directory, StringRef FileName,
+                   MD5::MD5Result *Checksum, Optional<StringRef> Source) {
+    CompilationDir = Directory;
+    RootFile.Name = FileName;
+    RootFile.DirIndex = 0;
+    RootFile.Checksum = Checksum;
+    RootFile.Source = Source;
+    trackMD5Usage(Checksum);
+    HasSource = Source.hasValue();
+  }
+
 private:
   void emitV2FileDirTables(MCStreamer *MCOS) const;
   void emitV5FileDirTables(MCStreamer *MCOS, Optional<MCDwarfLineStr> &LineStr,
@@ -255,6 +268,8 @@ class MCDwarfDwoLineTable {
   MCDwarfLineTableHeader Header;
 
 public:
+  MCDwarfDwoLineTable(unsigned DwarfVersion) : Header(DwarfVersion) {}
+
   void maybeSetRootFile(StringRef Directory, StringRef FileName,
                         MD5::MD5Result *Checksum, Optional<StringRef> Source) {
     if (!Header.RootFile.Name.empty())
@@ -282,6 +297,7 @@ class MCDwarfLineTable {
   MCLineSection MCLineSections;
 
 public:
+  MCDwarfLineTable(unsigned DwarfVersion) : Header(DwarfVersion) {}
   // This emits the Dwarf file and the line tables for all Compile Units.
   static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params);
 
index d9addb52376a47325943f5e2b34bf16b5193cf97..683d3245e5605c4f338fe604c64402c8a98a01da 100644 (file)
@@ -101,6 +101,7 @@ class DwarfCompileUnit final : public DwarfUnit {
   }
 
 public:
+  unsigned getDwarfVersion() const { return DD->getDwarfVersion(); }
   DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
                    DwarfDebug *DW, DwarfFile *DWU);
 
index 0e0a5b24864763eec1d701ad1cac7d53f3ce7812..226014cf0bb4f8758f4c6af48d9072d4513a94cd 100644 (file)
@@ -319,6 +319,7 @@ DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
     : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
       InfoHolder(A, "info_string", DIEValueAllocator),
       SkeletonHolder(A, "skel_string", DIEValueAllocator),
+      SplitTypeUnitFileTable(A->getDwarfVersion()),
       IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
   const Triple &TT = Asm->TM.getTargetTriple();
 
index 6e547469c24c7b3243a8ac815f63edadcf36428b..5d8e236a6b3683c9b9b45a145ec351e2391b0bd4 100644 (file)
@@ -397,7 +397,6 @@ void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) {
     return;
 
   unsigned FileID = getOrCreateSourceID(File);
-  assert(FileID && "Invalid file id");
   addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
   addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
 }
index 534201877730c950d31c26efce99160b16444328..6ad06a8553ea2920b95f0c49317c174cea78400c 100644 (file)
@@ -1017,14 +1017,28 @@ bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
 }
 
 bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
-  return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
+  uint16_t DwarfVersion = Prologue.getVersion();
+  assert(DwarfVersion != 0 && "LineTable has no dwarf version information");
+  if (DwarfVersion >= 5)
+    return FileIndex < Prologue.FileNames.size();
+  else
+    return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
+}
+const llvm::DWARFDebugLine::FileNameEntry &
+DWARFDebugLine::LineTable::getFileNameEntry(uint64_t Index) const {
+  uint16_t DwarfVersion = Prologue.getVersion();
+  assert(DwarfVersion != 0 && "LineTable has no dwarf version information");
+  if (DwarfVersion >= 5)
+    return Prologue.FileNames[Index];
+  else
+    return Prologue.FileNames[Index - 1];
 }
 
 Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
                                                                 FileLineInfoKind Kind) const {
   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
     return None;
-  const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
+  const FileNameEntry &Entry = getFileNameEntry(FileIndex);
   if (Optional<const char *> source = Entry.Source.getAsCString())
     return StringRef(*source);
   return None;
@@ -1044,7 +1058,7 @@ bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
                                                    std::string &Result) const {
   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
     return false;
-  const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
+  const FileNameEntry &Entry = getFileNameEntry(FileIndex);
   StringRef FileName = Entry.Name.getAsCString().getValue();
   if (Kind != FileLineInfoKind::AbsoluteFilePath ||
       isPathAbsoluteOnWindowsOrPosix(FileName)) {
index 49fad131a2c7802d630d2b37142adec578697075..348436f4bd083d3ce002c0bcfdaff211b88a1aeb 100644 (file)
@@ -603,7 +603,8 @@ Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
                                            MD5::MD5Result *Checksum,
                                            Optional<StringRef> Source,
                                            unsigned CUID) {
-  MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
+  MCDwarfLineTable &Table =
+      MCDwarfLineTablesCUMap.emplace(CUID, DwarfVersion).first->second;
   return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
 }
 
@@ -612,7 +613,7 @@ Expected<unsigned> MCContext::getDwarfFile(StringRef Directory,
 bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
   const MCDwarfLineTable &LineTable = getMCDwarfLineTable(CUID);
   if (FileNumber == 0)
-    return getDwarfVersion() >= 5 && LineTable.hasRootFile();
+    return getDwarfVersion() >= 5;
   if (FileNumber >= LineTable.getMCDwarfFiles().size())
     return false;
 
index 83b6b4f1aa3ceaae48bd385ad96f095afd760211..73b4d4bcd1989f2d492ecdaea41515c4b24f167e 100644 (file)
@@ -542,6 +542,15 @@ Expected<unsigned> MCDwarfLineTable::tryGetFile(StringRef &Directory,
   return Header.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
 }
 
+bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory,
+                StringRef &FileName, MD5::MD5Result *Checksum) {
+  if (RootFile.Name.empty() || RootFile.Name != FileName.data())
+    return false;
+  if (!RootFile.Checksum)
+    return !Checksum;
+  return *RootFile.Checksum == *Checksum;
+}
+
 Expected<unsigned>
 MCDwarfLineTableHeader::tryGetFile(StringRef &Directory,
                                    StringRef &FileName,
@@ -561,6 +570,8 @@ MCDwarfLineTableHeader::tryGetFile(StringRef &Directory,
     trackMD5Usage(Checksum);
     HasSource = (Source != None);
   }
+  if (isRootFile(RootFile, Directory, FileName, Checksum) && DwarfVersion >= 5)
+    return 0;
   if (FileNumber == 0) {
     // File numbers start with 1 and/or after any file numbers
     // allocated by inline-assembler .file directives.
index cff8f0007311de8e253f7abffa5f8fc9dcc2f0b7..39c69c9f7d2331248786acf65f13f294d6220831 100644 (file)
@@ -2,9 +2,9 @@
 // RUN: llvm-dwarfdump -v %t | FileCheck -check-prefix DWARF -check-prefix DWARF45 %s
 // RUN: llvm-dwarfdump --debug-line %t | FileCheck -check-prefix DWARF-DL -check-prefix DWARF-DL-5 -DDWVER=5 -DDWFILE=0 %s
 // RUN: llvm-objdump -r %t | FileCheck -check-prefix RELOC -check-prefix RELOC5 %s
-// RUN: llvm-mc < %s -triple=armv7-linux-gnueabi -filetype=obj -o %t -g -fdebug-compilation-dir=/tmp
+// RUN: llvm-mc < %s -triple=armv7-linux-gnueabi -filetype=obj -o %t -g -dwarf-version 4 -fdebug-compilation-dir=/tmp
 // RUN: llvm-dwarfdump -v %t | FileCheck -check-prefix DWARF -check-prefix DWARF45 %s
-// RUN: llvm-dwarfdump --debug-line %t | FileCheck -check-prefix DWARF-DL -DDWVER=4 -DDWFILE=1 %s
+// RUN: llvm-dwarfdump --debug-line %t | FileCheck -check-prefix DWARF-DL -check-prefix DWARF-DL-4 -DDWVER=4 -DDWFILE=1 %s
 // RUN: llvm-objdump -r %t | FileCheck -check-prefix RELOC -check-prefix RELOC4 %s
 // RUN: llvm-mc < %s -triple=armv7-linux-gnueabi -filetype=obj -o %t -g -dwarf-version 3 -fdebug-compilation-dir=/tmp
 // RUN: llvm-dwarfdump -v %t | FileCheck -check-prefix DWARF -check-prefix DWARF3 %s
@@ -57,11 +57,14 @@ b:
 // DWARF-DL-5:    include_directories[  0] = "/tmp"
 // DWARF-DL:      file_names[  [[DWFILE]]]:
 // DWARF-DL:      name: "{{(<stdin>|-)}}"
-// DWARF-DL:      0x0000000000000000     17      0      1   0   0  is_stmt
-// DWARF-DL-NEXT: 0x0000000000000004     17      0      1   0   0  is_stmt end_sequence
-// DWARF-DL-NEXT: 0x0000000000000000     21      0      1   0   0  is_stmt
-// DWARF-DL-NEXT: 0x0000000000000004     21      0      1   0   0  is_stmt end_sequence
-
+// DWARF-DL-5:      0x0000000000000000     17      0      0   0   0  is_stmt
+// DWARF-DL-5-NEXT: 0x0000000000000004     17      0      0   0   0  is_stmt end_sequence
+// DWARF-DL-5-NEXT: 0x0000000000000000     21      0      0   0   0  is_stmt
+// DWARF-DL-5-NEXT: 0x0000000000000004     21      0      0   0   0  is_stmt end_sequence
+// DWARF-DL-4:      0x0000000000000000     17      0      1   0   0  is_stmt
+// DWARF-DL-4-NEXT: 0x0000000000000004     17      0      1   0   0  is_stmt end_sequence
+// DWARF-DL-4-NEXT: 0x0000000000000000     21      0      1   0   0  is_stmt
+// DWARF-DL-4-NEXT: 0x0000000000000004     21      0      1   0   0  is_stmt end_sequence
 
 // DWARF: .debug_ranges contents:
 // DWARF: 00000000 ffffffff 00000000
index 2ec8141325f8db87e464e989bdd28cbc1b488aa5..d48e42c8d1214c6c00a88eb51ec185fbac333dc1 100644 (file)
@@ -1,8 +1,7 @@
 ; RUN: %llc_dwarf -filetype=asm -dwarf-version=5 %s -o - | FileCheck %s -check-prefix=ASM
 ; RUN: %llc_dwarf -filetype=obj -dwarf-version=5 %s -o - | llvm-dwarfdump -debug-line - | FileCheck %s -check-prefix=OBJ
 ; ASM: .file 0 "{{.+}}" md5
-; ASM: .file 1 "{{.+}}" md5
-; ASM: .file 2 "t1.cpp"
+; ASM: .file 1 "t1.cpp"
 ; ASM-NOT: md5
 ; OBJ: file_names[ 0]:
 ; OBJ-NOT: md5
index 1a3afb6875fd598bb767f05cc0d243ab038a5f7b..f98fdcc2b40d9fd88c275102621e0c9d24009bab 100644 (file)
 # CHECK:       file_names[ 1]:
 # CHECK-NEXT:  name: "header.h"
 # CHECK-NEXT:  dir_index: 1
-# CHECK:       file_names[ 2]:
-# CHECK-NEXT:  name: "root.cpp"
+# CHECK-4:     file_names[ 2]:
+# CHECK-4-NEXT: name: "root.cpp"
 # CHECK-4-NEXT: dir_index: 2
-# CHECK-5-NEXT: dir_index: 0
 
 # ASM-NOT: .file
 # ASM-5:   .file 0 "/test" "root.cpp"
 # ASM:     .file 1 "/include" "header.h"
 # ASM-4:   .file 2 "/test" "root.cpp"
-# ASM-5:   .file 2 "root.cpp"
 # ASM-NOT: .file
 
 # WARN:      file 0 not supported prior to DWARF-5
index 072b5a90c5c83aadeb6396d0523f57962d6f586e..f59027e0859e4d62becf5338984eb2f72f1fb043 100644 (file)
Binary files a/test/tools/llvm-objdump/Inputs/embedded-source and b/test/tools/llvm-objdump/Inputs/embedded-source differ
index b932a5d3f32f0dca18827050d243ba798afc2087..46607440cb9bde9490ce69e47a4e2851ec768043 100644 (file)
@@ -30,8 +30,7 @@
 _Z2f1v:                                 # @_Z2f1v
 .Lfunc_begin0:
        .file   0 "/home/avl" "test.cpp" md5 0xefae234cc05b45384d782316d3a5d338
-       .file   1 "test.cpp" md5 0xefae234cc05b45384d782316d3a5d338
-       .loc    1 1 0                   # test.cpp:1:0
+       .loc    0 1 0                   # test.cpp:1:0
        .cfi_startproc
 # %bb.0:                                # %entry
        pushq   %rbp
@@ -40,7 +39,7 @@ _Z2f1v:                                 # @_Z2f1v
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
 .Ltmp0:
-       .loc    1 1 12 prologue_end     # test.cpp:1:12
+       .loc    0 1 12 prologue_end     # test.cpp:1:12
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq
@@ -55,7 +54,7 @@ _Z2f1v:                                 # @_Z2f1v
        .type   _Z2f2v,@function
 _Z2f2v:                                 # @_Z2f2v
 .Lfunc_begin1:
-       .loc    1 2 0                   # test.cpp:2:0
+       .loc    0 2 0                   # test.cpp:2:0
        .cfi_startproc
 # %bb.0:                                # %entry
        pushq   %rbp
@@ -64,7 +63,7 @@ _Z2f2v:                                 # @_Z2f2v
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
 .Ltmp2:
-       .loc    1 2 12 prologue_end     # test.cpp:2:12
+       .loc    0 2 12 prologue_end     # test.cpp:2:12
        popq    %rbp
        .cfi_def_cfa %rsp, 8
        retq