From: George Rimar Date: Fri, 13 Jan 2017 15:58:55 +0000 (+0000) Subject: [llvm-dwp] - Reuse object::Decompressor class X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=06943952f3e8330968be6f4312cc95c33a404226;p=llvm [llvm-dwp] - Reuse object::Decompressor class One more place where Decompressor class can be reused. Differential revision: https://reviews.llvm.org/D28679 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291906 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/tools/llvm-dwp/X86/compressfail.test b/test/tools/llvm-dwp/X86/compressfail.test index 78b6255724f..b1961e82a76 100644 --- a/test/tools/llvm-dwp/X86/compressfail.test +++ b/test/tools/llvm-dwp/X86/compressfail.test @@ -4,4 +4,4 @@ RUN: not llvm-dwp %p/../Inputs/invalid_compressed.dwo -o %t 2>&1 | FileCheck %s REQUIRES: zlib -CHECK: error: failure while decompressing compressed section: 'zdebug_{{.*}}.dwo' +CHECK: error: failure while decompressing compressed section: '.zdebug_{{.*}}.dwo' diff --git a/test/tools/llvm-dwp/X86/nocompress.test b/test/tools/llvm-dwp/X86/nocompress.test index 1de9444dd3e..c3df6b3fcca 100644 --- a/test/tools/llvm-dwp/X86/nocompress.test +++ b/test/tools/llvm-dwp/X86/nocompress.test @@ -2,4 +2,4 @@ RUN: not llvm-dwp %p/../Inputs/compress/a.dwo -o %t 2>&1 | FileCheck %s REQUIRES: nozlib -CHECK: error: zlib not available +CHECK: error: failure while decompressing compressed section: '.zdebug_{{.*}}.dwo', zlib is not available diff --git a/tools/llvm-dwp/llvm-dwp.cpp b/tools/llvm-dwp/llvm-dwp.cpp index 7418c77bdec..0c4af757660 100644 --- a/tools/llvm-dwp/llvm-dwp.cpp +++ b/tools/llvm-dwp/llvm-dwp.cpp @@ -27,6 +27,7 @@ #include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCTargetOptionsCommandFlags.h" +#include "llvm/Object/Decompressor.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Compression.h" #include "llvm/Support/DataExtractor.h" @@ -334,21 +335,6 @@ writeIndex(MCStreamer &Out, MCSection *Section, writeIndexTable(Out, ContributionOffsets, IndexEntries, &DWARFUnitIndex::Entry::SectionContribution::Length); } -static bool consumeCompressedDebugSectionHeader(StringRef &data, - uint64_t &OriginalSize) { - // Consume "ZLIB" prefix. - if (!data.startswith("ZLIB")) - return false; - data = data.substr(4); - // Consume uncompressed section size (big-endian 8 bytes). - DataExtractor extractor(data, false, 8); - uint32_t Offset = 0; - OriginalSize = extractor.getU64(&Offset); - if (Offset == 0) - return false; - data = data.substr(Offset); - return true; -} std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) { std::string Text = "\'"; @@ -368,22 +354,29 @@ std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWO return Text; } -static Error handleCompressedSection( - std::deque> &UncompressedSections, StringRef &Name, - StringRef &Contents) { - if (!Name.startswith("zdebug_")) +static Error createError(StringRef Name, Error E) { + return make_error( + ("failure while decompressing compressed section: '" + Name + "', " + + llvm::toString(std::move(E))) + .str()); +} + +static Error +handleCompressedSection(std::deque> &UncompressedSections, + StringRef &Name, StringRef &Contents) { + if (!Decompressor::isGnuStyle(Name)) return Error::success(); + + Expected Dec = + Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/); + if (!Dec) + return createError(Name, Dec.takeError()); + UncompressedSections.emplace_back(); - uint64_t OriginalSize; - if (!zlib::isAvailable()) - return make_error("zlib not available"); - if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize) || - zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) != - zlib::StatusOK) - return make_error( - ("failure while decompressing compressed section: '" + Name + "\'") - .str()); - Name = Name.substr(1); + if (Error E = Dec->decompress(UncompressedSections.back())) + return createError(Name, std::move(E)); + + Name = Name.substr(2); // Drop ".z" Contents = UncompressedSections.back(); return Error::success(); } @@ -409,8 +402,6 @@ static Error handleSection( if (std::error_code Err = Section.getName(Name)) return errorCodeToError(Err); - Name = Name.substr(Name.find_first_not_of("._")); - StringRef Contents; if (auto Err = Section.getContents(Contents)) return errorCodeToError(Err); @@ -418,6 +409,8 @@ static Error handleSection( if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents)) return Err; + Name = Name.substr(Name.find_first_not_of("._")); + auto SectionPair = KnownSections.find(Name); if (SectionPair == KnownSections.end()) return Error::success();