From eb2b17eea7785bb790dab11cff1e3365cd3218cc Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 2 Mar 2020 12:48:59 +0100 Subject: [PATCH] Use DWARFDataExtractor::getInitialLength in debug_aranges Summary: getInitialLength is a *DWARF*DataExtractor method so I had to "upgrade" some DataExtractors to be able to make use of it. Reviewers: ikudrin, jhenderson, probinson Subscribers: aprantl, hiraditya, llvm-commits, dblaikie Tags: #llvm Differential Revision: https://reviews.llvm.org/D75535 --- .../llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h | 4 +- .../llvm/DebugInfo/DWARF/DWARFDebugAranges.h | 4 +- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 3 +- llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp | 52 ++++++---------------- llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp | 6 +-- llvm/tools/obj2yaml/dwarf2yaml.cpp | 4 +- .../DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp | 27 +++++------ 7 files changed, 37 insertions(+), 63 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h index 806ad05..92c4605 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h @@ -10,7 +10,7 @@ #define LLVM_DEBUGINFO_DWARFDEBUGARANGESET_H #include "llvm/ADT/iterator_range.h" -#include "llvm/Support/DataExtractor.h" +#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" #include "llvm/Support/Error.h" #include #include @@ -58,7 +58,7 @@ public: DWARFDebugArangeSet() { clear(); } void clear(); - Error extract(DataExtractor data, uint64_t *offset_ptr); + Error extract(DWARFDataExtractor data, uint64_t *offset_ptr); void dump(raw_ostream &OS) const; uint64_t getCompileUnitDIEOffset() const { return HeaderData.CuOffset; } diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h index cd04bf1..31a8b46 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAranges.h @@ -10,7 +10,7 @@ #define LLVM_DEBUGINFO_DWARFDEBUGARANGES_H #include "llvm/ADT/DenseSet.h" -#include "llvm/Support/DataExtractor.h" +#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" #include #include @@ -25,7 +25,7 @@ public: private: void clear(); - void extract(DataExtractor DebugArangesData, + void extract(DWARFDataExtractor DebugArangesData, function_ref RecoverableErrorHandler); /// Call appendRange multiple times and then call construct. diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index d08f1e0..7541bb7 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -457,7 +457,8 @@ void DWARFContext::dump( if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges, DObj->getArangesSection())) { uint64_t offset = 0; - DataExtractor arangesData(DObj->getArangesSection(), isLittleEndian(), 0); + DWARFDataExtractor arangesData(DObj->getArangesSection(), isLittleEndian(), + 0); DWARFDebugArangeSet set; while (arangesData.isValidOffset(offset)) { if (Error E = set.extract(arangesData, &offset)) { diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp index c4af6cf4..9d640b8 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp @@ -31,7 +31,8 @@ void DWARFDebugArangeSet::clear() { ArangeDescriptors.clear(); } -Error DWARFDebugArangeSet::extract(DataExtractor data, uint64_t *offset_ptr) { +Error DWARFDebugArangeSet::extract(DWARFDataExtractor data, + uint64_t *offset_ptr) { assert(data.isValidOffset(*offset_ptr)); ArangeDescriptors.clear(); Offset = *offset_ptr; @@ -59,45 +60,20 @@ Error DWARFDebugArangeSet::extract(DataExtractor data, uint64_t *offset_ptr) { // the segment selectors are omitted from all tuples, including // the terminating tuple. - constexpr unsigned CommonFieldsLength = 2 + // Version - 1 + // Address Size - 1; // Segment Selector Size - static const unsigned DWARF32HeaderLength = - dwarf::getUnitLengthFieldByteSize(dwarf::DWARF32) + CommonFieldsLength + - dwarf::getDwarfOffsetByteSize(dwarf::DWARF32); // Debug Info Offset - static const unsigned DWARF64HeaderLength = - dwarf::getUnitLengthFieldByteSize(dwarf::DWARF64) + CommonFieldsLength + - dwarf::getDwarfOffsetByteSize(dwarf::DWARF64); // Debug Info Offset - - if (!data.isValidOffsetForDataOfSize(Offset, DWARF32HeaderLength)) + dwarf::DwarfFormat format; + Error Err = Error::success(); + std::tie(HeaderData.Length, format) = data.getInitialLength(offset_ptr, &Err); + HeaderData.Version = data.getU16(offset_ptr, &Err); + HeaderData.CuOffset = + data.getUnsigned(offset_ptr, dwarf::getDwarfOffsetByteSize(format), &Err); + HeaderData.AddrSize = data.getU8(offset_ptr, &Err); + HeaderData.SegSize = data.getU8(offset_ptr, &Err); + if (Err) { return createStringError(errc::invalid_argument, - "section is not large enough to contain " - "an address range table at offset 0x%" PRIx64, - Offset); - - dwarf::DwarfFormat format = dwarf::DWARF32; - HeaderData.Length = data.getU32(offset_ptr); - if (HeaderData.Length == dwarf::DW_LENGTH_DWARF64) { - if (!data.isValidOffsetForDataOfSize(Offset, DWARF64HeaderLength)) - return createStringError( - errc::invalid_argument, - "section is not large enough to contain a DWARF64 " - "address range table at offset 0x%" PRIx64, - Offset); - HeaderData.Length = data.getU64(offset_ptr); - format = dwarf::DWARF64; - } else if (HeaderData.Length >= dwarf::DW_LENGTH_lo_reserved) { - return createStringError( - errc::invalid_argument, - "address range table at offset 0x%" PRIx64 - " has unsupported reserved unit length of value 0x%8.8" PRIx64, - Offset, HeaderData.Length); + "parsing address ranges table at offset 0x%" PRIx64 + ": %s", + Offset, toString(std::move(Err)).c_str()); } - HeaderData.Version = data.getU16(offset_ptr); - HeaderData.CuOffset = - data.getUnsigned(offset_ptr, dwarf::getDwarfOffsetByteSize(format)); - HeaderData.AddrSize = data.getU8(offset_ptr); - HeaderData.SegSize = data.getU8(offset_ptr); // Perform basic validation of the header fields. uint64_t full_length = diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp index 0344844..e8ed630 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp @@ -20,7 +20,7 @@ using namespace llvm; void DWARFDebugAranges::extract( - DataExtractor DebugArangesData, + DWARFDataExtractor DebugArangesData, function_ref RecoverableErrorHandler) { if (!DebugArangesData.isValidOffset(0)) return; @@ -48,8 +48,8 @@ void DWARFDebugAranges::generate(DWARFContext *CTX) { return; // Extract aranges from .debug_aranges section. - DataExtractor ArangesData(CTX->getDWARFObj().getArangesSection(), - CTX->isLittleEndian(), 0); + DWARFDataExtractor ArangesData(CTX->getDWARFObj().getArangesSection(), + CTX->isLittleEndian(), 0); extract(ArangesData, CTX->getRecoverableErrorHandler()); // Generate aranges from DIEs: even if .debug_aranges section is present, diff --git a/llvm/tools/obj2yaml/dwarf2yaml.cpp b/llvm/tools/obj2yaml/dwarf2yaml.cpp index 524f43e..edb1b54 100644 --- a/llvm/tools/obj2yaml/dwarf2yaml.cpp +++ b/llvm/tools/obj2yaml/dwarf2yaml.cpp @@ -57,8 +57,8 @@ void dumpDebugStrings(DWARFContext &DCtx, DWARFYAML::Data &Y) { } Error dumpDebugARanges(DWARFContext &DCtx, DWARFYAML::Data &Y) { - DataExtractor ArangesData(DCtx.getDWARFObj().getArangesSection(), - DCtx.isLittleEndian(), 0); + DWARFDataExtractor ArangesData(DCtx.getDWARFObj().getArangesSection(), + DCtx.isLittleEndian(), 0); uint64_t Offset = 0; DWARFDebugArangeSet Set; diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp index fb5405b..5629a0d 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp @@ -16,9 +16,9 @@ namespace { template void ExpectExtractError(const char (&SecDataRaw)[SecSize], const char *ErrorMessage) { - DataExtractor Extractor(StringRef(SecDataRaw, SecSize - 1), - /* IsLittleEndian = */ true, - /* AddressSize = */ 4); + DWARFDataExtractor Extractor(StringRef(SecDataRaw, SecSize - 1), + /* IsLittleEndian = */ true, + /* AddressSize = */ 4); DWARFDebugArangeSet Set; uint64_t Offset = 0; Error E = Set.extract(Extractor, &Offset); @@ -111,29 +111,26 @@ TEST(DWARFDebugArangeSet, ReservedUnitLength) { // the section. 1 will be automatically subtracted in ExpectExtractError(). static const char DebugArangesSecRaw[12 + 1] = "\xf0\xff\xff\xff"; // Reserved unit length value - ExpectExtractError( - DebugArangesSecRaw, - "address range table at offset 0x0 has unsupported reserved unit length " - "of value 0xfffffff0"); + ExpectExtractError(DebugArangesSecRaw, + "parsing address ranges table at offset 0x0: unsupported " + "reserved unit length of value 0xfffffff0"); } TEST(DWARFDebugArangeSet, SectionTooShort) { // Note: 1 will be automatically subtracted in ExpectExtractError(). static const char DebugArangesSecRaw[11 + 1] = {0}; - ExpectExtractError( - DebugArangesSecRaw, - "section is not large enough to contain an address range table " - "at offset 0x0"); + ExpectExtractError(DebugArangesSecRaw, + "parsing address ranges table at offset 0x0: unexpected " + "end of data at offset 0xb"); } TEST(DWARFDebugArangeSet, SectionTooShortDWARF64) { // Note: 1 will be automatically subtracted in ExpectExtractError(). static const char DebugArangesSecRaw[23 + 1] = "\xff\xff\xff\xff"; // DWARF64 mark - ExpectExtractError( - DebugArangesSecRaw, - "section is not large enough to contain a DWARF64 address range table " - "at offset 0x0"); + ExpectExtractError(DebugArangesSecRaw, + "parsing address ranges table at offset 0x0: unexpected " + "end of data at offset 0x17"); } TEST(DWARFDebugArangeSet, NoSpaceForEntries) { -- 2.7.4