diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp @@ -340,18 +340,15 @@ const uint64_t PrologueOffset = *OffsetPtr; clear(); - TotalLength = DebugLineData.getRelocatedValue(4, OffsetPtr); - if (TotalLength == dwarf::DW_LENGTH_DWARF64) { - FormParams.Format = dwarf::DWARF64; - TotalLength = DebugLineData.getU64(OffsetPtr); - } else if (TotalLength >= dwarf::DW_LENGTH_lo_reserved) { - // Treat this error as unrecoverable - we have no way of knowing where the - // table ends. - return createStringError(errc::invalid_argument, - "parsing line table prologue at offset 0x%8.8" PRIx64 - " unsupported reserved unit length found of value 0x%8.8" PRIx64, - PrologueOffset, TotalLength); - } + Error Err = Error::success(); + std::tie(TotalLength, FormParams.Format) = + DebugLineData.getInitialLength(OffsetPtr, &Err); + if (Err) + return createStringError( + errc::invalid_argument, + "parsing line table prologue at offset 0x%8.8" PRIx64 ": %s", + PrologueOffset, toString(std::move(Err)).c_str()); + FormParams.Version = DebugLineData.getU16(OffsetPtr); if (!versionIsSupported(getVersion())) // Treat this error as unrecoverable - we cannot be sure what any of @@ -360,7 +357,7 @@ return createStringError( errc::not_supported, "parsing line table prologue at offset 0x%8.8" PRIx64 - " found unsupported version %" PRIu16, + ": unsupported version %" PRIu16, PrologueOffset, getVersion()); if (getVersion() >= 5) { @@ -1218,8 +1215,7 @@ } bool DWARFDebugLine::Prologue::totalLengthIsValid() const { - return TotalLength == dwarf::DW_LENGTH_DWARF64 || - TotalLength < dwarf::DW_LENGTH_lo_reserved; + return TotalLength != 0u; } DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext( diff --git a/llvm/test/tools/llvm-dwarfdump/X86/debug_line_invalid.test b/llvm/test/tools/llvm-dwarfdump/X86/debug_line_invalid.test --- a/llvm/test/tools/llvm-dwarfdump/X86/debug_line_invalid.test +++ b/llvm/test/tools/llvm-dwarfdump/X86/debug_line_invalid.test @@ -42,7 +42,7 @@ ## For fatal issues, the following table(s) should not be dumped: # FATAL: debug_line[0x00000048] # RESERVED-NOT: prologue -# RESERVED: warning: parsing line table prologue at offset 0x00000048 unsupported reserved unit length found of value 0xfffffffe +# RESERVED: warning: parsing line table prologue at offset 0x00000048: unsupported reserved unit length of value 0xfffffffe # RESERVED-NOT: prologue # FATAL-NOT: debug_line @@ -50,7 +50,7 @@ ## Version 0 table. # NONFATAL: debug_line[0x00000048] -# ALL: warning: parsing line table prologue at offset 0x00000048 found unsupported version 0 +# ALL: warning: parsing line table prologue at offset 0x00000048: unsupported version 0 # NONFATAL-NEXT: Line table prologue # NONFATAL-NEXT: total_length: 0x00000002 # NONFATAL-NEXT: version: 0 @@ -58,7 +58,7 @@ ## Version 1 table. # NONFATAL: debug_line[0x0000004e] -# ALL-NEXT: warning: parsing line table prologue at offset 0x0000004e found unsupported version 1 +# ALL-NEXT: warning: parsing line table prologue at offset 0x0000004e: unsupported version 1 # NONFATAL-NEXT: Line table prologue # NONFATAL-NEXT: total_length: 0x00000002 # NONFATAL-NEXT: version: 1 diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp @@ -316,8 +316,8 @@ EXPECT_THAT_EXPECTED( getOrParseLineTableFatalErrors(), FailedWithMessage( - "parsing line table prologue at offset 0x00000000 unsupported " - "reserved unit length found of value 0xfffffff0")); + "parsing line table prologue at offset 0x00000000: unsupported " + "reserved unit length of value 0xfffffff0")); } struct DebugLineUnsupportedVersionFixture : public TestWithParam, @@ -339,8 +339,8 @@ EXPECT_THAT_EXPECTED( getOrParseLineTableFatalErrors(), - FailedWithMessage("parsing line table prologue at offset 0x00000000 " - "found unsupported version " + + FailedWithMessage("parsing line table prologue at offset 0x00000000: " + "unsupported version " + std::to_string(Version))); } @@ -819,15 +819,15 @@ DWARFDebugLine::SectionParser Parser(LineData, *Context, CUs, TUs); Parser.parseNext(RecordRecoverable, RecordUnrecoverable); - EXPECT_EQ(Parser.getOffset(), 4u); + EXPECT_EQ(Parser.getOffset(), 0u); EXPECT_TRUE(Parser.done()); EXPECT_FALSE(Recoverable); EXPECT_THAT_ERROR( std::move(Unrecoverable), FailedWithMessage( - "parsing line table prologue at offset 0x00000000 unsupported " - "reserved unit length found of value 0xfffffff0")); + "parsing line table prologue at offset 0x00000000: unsupported " + "reserved unit length of value 0xfffffff0")); } TEST_F(DebugLineBasicFixture, ParserMovesToEndForBadLengthWhenSkipping) { @@ -842,15 +842,15 @@ DWARFDebugLine::SectionParser Parser(LineData, *Context, CUs, TUs); Parser.skip(RecordRecoverable, RecordUnrecoverable); - EXPECT_EQ(Parser.getOffset(), 4u); + EXPECT_EQ(Parser.getOffset(), 0u); EXPECT_TRUE(Parser.done()); EXPECT_FALSE(Recoverable); EXPECT_THAT_ERROR( std::move(Unrecoverable), FailedWithMessage( - "parsing line table prologue at offset 0x00000000 unsupported " - "reserved unit length found of value 0xfffffff0")); + "parsing line table prologue at offset 0x00000000: unsupported " + "reserved unit length of value 0xfffffff0")); } TEST_F(DebugLineBasicFixture, ParserReportsFirstErrorInEachTableWhenParsing) { @@ -873,10 +873,10 @@ EXPECT_THAT_ERROR( std::move(Unrecoverable), - FailedWithMessage("parsing line table prologue at offset 0x00000000 " - "found unsupported version 0", - "parsing line table prologue at offset 0x00000006 " - "found unsupported version 1")); + FailedWithMessage("parsing line table prologue at offset 0x00000000: " + "unsupported version 0", + "parsing line table prologue at offset 0x00000006: " + "unsupported version 1")); } TEST_F(DebugLineBasicFixture, ParserReportsNonPrologueProblemsWhenParsing) { @@ -932,10 +932,10 @@ EXPECT_THAT_ERROR( std::move(Unrecoverable), - FailedWithMessage("parsing line table prologue at offset 0x00000000 " - "found unsupported version 0", - "parsing line table prologue at offset 0x00000006 " - "found unsupported version 1")); + FailedWithMessage("parsing line table prologue at offset 0x00000000: " + "unsupported version 0", + "parsing line table prologue at offset 0x00000006: " + "unsupported version 1")); } TEST_F(DebugLineBasicFixture, ParserIgnoresNonPrologueErrorsWhenSkipping) {