diff options
author | Ali Tamur <tamur@google.com> | 2019-04-19 02:26:56 +0000 |
---|---|---|
committer | Ali Tamur <tamur@google.com> | 2019-04-19 02:26:56 +0000 |
commit | 783d84bb395ce2549a14ff96db4df5ac3a513d97 (patch) | |
tree | aae3ddf94c3dddbb2f1f52d3ca948be0cd082cbe /llvm/lib | |
parent | b6a8a6caa8147ea592b7ac65d586b02f6980d5a3 (diff) | |
download | bcm5719-llvm-783d84bb395ce2549a14ff96db4df5ac3a513d97.tar.gz bcm5719-llvm-783d84bb395ce2549a14ff96db4df5ac3a513d97.zip |
[llvm] Prevent duplicate files in debug line header in dwarf 5: another attempt
Another attempt to land the changes in debug line header to prevent duplicate
files in Dwarf 5. I rolled back my previous commit because of a mistake in
generating the object file in a test. Meanwhile, I addressed some offline
comments and changed the implementation; the largest difference is that
MCDwarfLineTableHeader does not keep DwarfVersion but gets it as a parameter. I
also merged the patch to fix two lld tests that will strt to fail into this
patch.
Original Commit:
https://reviews.llvm.org/D59515
Original Message:
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.
llvm-svn: 358732
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp | 5 | ||||
-rw-r--r-- | llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp | 19 | ||||
-rw-r--r-- | llvm/lib/MC/MCAsmStreamer.cpp | 3 | ||||
-rw-r--r-- | llvm/lib/MC/MCContext.cpp | 5 | ||||
-rw-r--r-- | llvm/lib/MC/MCDwarf.cpp | 16 |
5 files changed, 39 insertions, 9 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp index eec10282f57..b14eadd2559 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -315,7 +315,9 @@ unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) { addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0); } return SplitLineTable->getFile(File->getDirectory(), File->getFilename(), - getMD5AsBytes(File), File->getSource()); + getMD5AsBytes(File), + Asm->OutContext.getDwarfVersion(), + File->getSource()); } void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) { @@ -397,7 +399,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); } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp index 47df83e6817..84e13adbef6 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp @@ -969,14 +969,29 @@ bool DWARFDebugLine::LineTable::lookupAddressRangeImpl( } bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const { + uint16_t DwarfVersion = Prologue.getVersion(); + assert(DwarfVersion != 0 && "LineTable has no dwarf version information"); + if (DwarfVersion >= 5) + return FileIndex < Prologue.FileNames.size(); 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"); + // Unlike previous versions, in Dwarf 5 the file names is 0-indexed. + 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; @@ -996,7 +1011,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)) { diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index ef5c4666d92..350502d633d 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -1190,7 +1190,8 @@ Expected<unsigned> MCAsmStreamer::tryEmitDwarfFileDirective( MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID); unsigned NumFiles = Table.getMCDwarfFiles().size(); Expected<unsigned> FileNoOrErr = - Table.tryGetFile(Directory, Filename, Checksum, Source, FileNo); + Table.tryGetFile(Directory, Filename, Checksum, Source, + getContext().getDwarfVersion(), FileNo); if (!FileNoOrErr) return FileNoOrErr.takeError(); FileNo = FileNoOrErr.get(); diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index 4d16715c0ae..6ca7d3df19d 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -605,7 +605,8 @@ Expected<unsigned> MCContext::getDwarfFile(StringRef Directory, Optional<StringRef> Source, unsigned CUID) { MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID]; - return Table.tryGetFile(Directory, FileName, Checksum, Source, FileNumber); + return Table.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion, + FileNumber); } /// isValidDwarfFileNumber - takes a dwarf file number and returns true if it @@ -613,7 +614,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; diff --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp index 262c520a06a..e8edf6ba1d4 100644 --- a/llvm/lib/MC/MCDwarf.cpp +++ b/llvm/lib/MC/MCDwarf.cpp @@ -259,7 +259,7 @@ void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS, void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params, MCSection *Section) const { - if (Header.MCDwarfFiles.empty()) + if (!HasSplitLineTable) return; Optional<MCDwarfLineStr> NoLineStr(None); MCOS.SwitchSection(Section); @@ -538,8 +538,17 @@ Expected<unsigned> MCDwarfLineTable::tryGetFile(StringRef &Directory, StringRef &FileName, Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source, + uint16_t DwarfVersion, unsigned FileNumber) { - return Header.tryGetFile(Directory, FileName, Checksum, Source, FileNumber); + return Header.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion, + FileNumber); +} + +bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory, + StringRef &FileName, Optional<MD5::MD5Result> Checksum) { + if (RootFile.Name.empty() || RootFile.Name != FileName.data()) + return false; + return RootFile.Checksum == Checksum; } Expected<unsigned> @@ -547,6 +556,7 @@ MCDwarfLineTableHeader::tryGetFile(StringRef &Directory, StringRef &FileName, Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source, + uint16_t DwarfVersion, unsigned FileNumber) { if (Directory == CompilationDir) Directory = ""; @@ -561,6 +571,8 @@ MCDwarfLineTableHeader::tryGetFile(StringRef &Directory, trackMD5Usage(Checksum.hasValue()); 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. |