[llvm-dwp] - Reuse object::Decompressor class

One more place where Decompressor class can be reused.

Differential revision: https://reviews.llvm.org/D28679

llvm-svn: 291906
This commit is contained in:
George Rimar 2017-01-13 15:58:55 +00:00
parent 87a036259b
commit 8f5976ec00
3 changed files with 26 additions and 33 deletions

View File

@ -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'

View File

@ -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

View File

@ -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<SmallString<32>> &UncompressedSections, StringRef &Name,
StringRef &Contents) {
if (!Name.startswith("zdebug_"))
static Error createError(StringRef Name, Error E) {
return make_error<DWPError>(
("failure while decompressing compressed section: '" + Name + "', " +
llvm::toString(std::move(E)))
.str());
}
static Error
handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
StringRef &Name, StringRef &Contents) {
if (!Decompressor::isGnuStyle(Name))
return Error::success();
Expected<Decompressor> 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<DWPError>("zlib not available");
if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize) ||
zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
zlib::StatusOK)
return make_error<DWPError>(
("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();