diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h --- 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 @@ 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 --- 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 @@ 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 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -457,7 +457,8 @@ 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 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp @@ -31,7 +31,8 @@ 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 @@ // 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 --- 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 @@ 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 --- a/llvm/tools/obj2yaml/dwarf2yaml.cpp +++ b/llvm/tools/obj2yaml/dwarf2yaml.cpp @@ -57,8 +57,8 @@ } 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 --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugArangeSetTest.cpp @@ -16,9 +16,9 @@ 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 @@ // 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) {