Index: test/tools/llvm-objcopy/compress-and-decompress-debug-sections-error.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/compress-and-decompress-debug-sections-error.test @@ -0,0 +1,5 @@ +# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o +# RUN: not llvm-objcopy --compress-debug-sections=zlib --decompress-debug-sections %t.o 2>&1 | FileCheck %s + +# CHECK: Cannot specify --compress-debug-sections at the same time as --decompress-debug-sections at the same time. + Index: test/tools/llvm-objcopy/compress-debug-sections-zlib-gnu.test =================================================================== --- test/tools/llvm-objcopy/compress-debug-sections-zlib-gnu.test +++ test/tools/llvm-objcopy/compress-debug-sections-zlib-gnu.test @@ -2,10 +2,12 @@ # RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o # RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t.o %t-compressed.o +# RUN: llvm-objcopy --decompress-debug-sections %t-compressed.o %t-decompressed.o # RUN: llvm-objdump -s %t.o -section=.debug_foo | FileCheck %s # RUN: llvm-objdump -s %t-compressed.o | FileCheck %s --check-prefix=CHECK-COMPRESSED # RUN: llvm-readobj -relocations -s %t-compressed.o | FileCheck %s --check-prefix=CHECK-FLAGS +# RUN: llvm-objdump -s %t-decompressed.o -section=.debug_foo | FileCheck %s # CHECK: .debug_foo: Index: test/tools/llvm-objcopy/compress-debug-sections-zlib.test =================================================================== --- test/tools/llvm-objcopy/compress-debug-sections-zlib.test +++ test/tools/llvm-objcopy/compress-debug-sections-zlib.test @@ -2,10 +2,12 @@ # RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o # RUN: llvm-objcopy --compress-debug-sections=zlib %t.o %t-compressed.o +# RUN: llvm-objcopy --decompress-debug-sections %t-compressed.o %t-decompressed.o # RUN: llvm-objdump -s %t.o -section=.debug_foo | FileCheck %s # RUN: llvm-objdump -s %t-compressed.o | FileCheck %s --check-prefix=CHECK-COMPRESSED # RUN: llvm-readobj -relocations -s %t-compressed.o | FileCheck %s --check-prefix=CHECK-FLAGS +# RUN: llvm-objdump -s %t-decompressed.o -section=.debug_foo | FileCheck %s # CHECK: .debug_foo: Index: test/tools/llvm-objcopy/compress-debug-sections.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/compress-debug-sections.test @@ -0,0 +1,18 @@ +# REQUIRES: zlib + +# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o + +# RUN: llvm-objcopy --compress-debug-sections=zlib %t.o %tz.o +# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t.o %tzg.o + +# RUN: llvm-objcopy --decompress-debug-sections %tz.o %t2.o +# RUN: llvm-objcopy --decompress-debug-sections %tzg.o %t3.o + +# Using redirects to avoid llvm-objdump from printing the filename. +# RUN: llvm-objdump -s -section=.debug_str - < %t.o > %t.txt +# RUN: llvm-objdump -s -section=.debug_str - < %t2.o > %t2.txt +# RUN: llvm-objdump -s -section=.debug_str - < %t3.o > %t3.txt + +# RUN: diff %t.txt %t2.txt +# RUN: diff %t.txt %t3.txt + Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -23,6 +23,8 @@ HelpText<"Compress DWARF debug sections using " "specified style. Supported styles: " "'zlib-gnu' and 'zlib'">; +def decompress_debug_sections : Flag<["-", "--"], "decompress-debug-sections">, + HelpText<"Decompress DWARF debug sections.">; def O : JoinedOrSeparate<["-"], "O">, Alias; defm split_dwo : Eq<"split-dwo">, Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -41,6 +41,7 @@ class GroupSection; class SectionIndexSection; class CompressedSection; +class DecompressedSection; class Segment; class Object; struct Symbol; @@ -89,6 +90,7 @@ virtual void visit(const GroupSection &Sec) = 0; virtual void visit(const SectionIndexSection &Sec) = 0; virtual void visit(const CompressedSection &Sec) = 0; + virtual void visit(const DecompressedSection &Sec) = 0; }; class SectionWriter : public SectionVisitor { @@ -108,6 +110,7 @@ virtual void visit(const GroupSection &Sec) override = 0; virtual void visit(const SectionIndexSection &Sec) override = 0; virtual void visit(const CompressedSection &Sec) override = 0; + virtual void visit(const DecompressedSection &Sec) override = 0; explicit SectionWriter(Buffer &Buf) : Out(Buf) {} }; @@ -127,6 +130,7 @@ void visit(const GroupSection &Sec) override; void visit(const SectionIndexSection &Sec) override; void visit(const CompressedSection &Sec) override; + void visit(const DecompressedSection &Sec) override; explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {} }; @@ -145,6 +149,7 @@ void visit(const GroupSection &Sec) override; void visit(const SectionIndexSection &Sec) override; void visit(const CompressedSection &Sec) override; + void visit(const DecompressedSection &Sec) override; explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {} }; @@ -361,6 +366,7 @@ class CompressedSection : public SectionBase { MAKE_SEC_WRITER_FRIEND + friend class DecompressedSection; DebugCompressionType CompressionType; uint64_t DecompressedSize; @@ -370,6 +376,34 @@ public: CompressedSection(const SectionBase &Sec, DebugCompressionType CompressionType); + CompressedSection(ArrayRef CompressedData, uint64_t DecompressedSize, + uint64_t DecompressedAlign); + + void accept(SectionVisitor &Visitor) const override; + + static bool classof(const SectionBase *S) { + return (S->Flags & ELF::SHF_COMPRESSED) || + (StringRef(S->Name).startswith(".zdebug")); + } +}; + +class DecompressedSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + uint64_t DecompressedSize; + uint64_t DecompressedAlign; + +public: + explicit DecompressedSection(const CompressedSection &Sec) + : SectionBase(Sec), DecompressedSize(Sec.DecompressedSize), + DecompressedAlign(Sec.DecompressedAlign) { + Size = DecompressedSize; + Align = DecompressedAlign; + Flags = (Flags & ~ELF::SHF_COMPRESSED); + if (StringRef(Name).startswith(".zdebug")) + Name = "." + Name.substr(2); + } + void accept(SectionVisitor &Visitor) const override; }; Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -136,6 +136,64 @@ std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf); } +static const std::vector ZlibGnuMagic = {'Z', 'L', 'I', 'B'}; + +static bool isDataGnuCompressed(ArrayRef Data) { + return Data.size() > ZlibGnuMagic.size() && + std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data()); +} + +template +static std::tuple +getDecompressedSizeAndAlignment(ArrayRef Data) { + const bool IsGnuDebug = isDataGnuCompressed(Data); + const uint64_t DecompressedSize = + IsGnuDebug + ? support::endian::read64be(reinterpret_cast( + Data.data() + ZlibGnuMagic.size())) + : reinterpret_cast *>(Data.data())->ch_size; + const uint64_t DecompressedAlign = + IsGnuDebug ? 1 + : reinterpret_cast *>(Data.data()) + ->ch_addralign; + + return std::make_tuple(DecompressedSize, DecompressedAlign); +} + +template +void ELFSectionWriter::visit(const DecompressedSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + + if (!zlib::isAvailable()) { + std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf); + return; + } + + const unsigned DataOffset = + isDataGnuCompressed(Sec.OriginalData) + ? (ZlibGnuMagic.size() + sizeof(Sec.DecompressedSize)) + : sizeof(Elf_Chdr_Impl); + + StringRef CompressedContent( + reinterpret_cast(Sec.OriginalData.data()) + DataOffset, + Sec.OriginalData.size() - DataOffset); + + SmallVector DecompressedContent; + if (Error E = zlib::uncompress(CompressedContent, DecompressedContent, + static_cast(Sec.DecompressedSize))) + reportError(Sec.Name, std::move(E)); + + std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf); +} + +void BinarySectionWriter::visit(const DecompressedSection &Sec) { + error("Cannot write compressed section '" + Sec.Name + "' "); +} + +void DecompressedSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + void OwnedDataSection::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } @@ -206,6 +264,14 @@ Align = 8; } +CompressedSection::CompressedSection(ArrayRef CompressedData, + uint64_t DecompressedSize, + uint64_t DecompressedAlign) + : CompressionType(DebugCompressionType::None), + DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) { + OriginalData = CompressedData; +} + void CompressedSection::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } @@ -969,11 +1035,21 @@ } case SHT_NOBITS: return Obj.addSection
(Data); - default: + default: { Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + + if (isDataGnuCompressed(Data) || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) { + uint64_t DecompressedSize, DecompressedAlign; + std::tie(DecompressedSize, DecompressedAlign) = + getDecompressedSizeAndAlignment(Data); + return Obj.addSection(Data, DecompressedSize, + DecompressedAlign); + } + return Obj.addSection
(Data); } } +} template void ELFBuilder::readSectionHeaders() { uint32_t Index = 0; Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -178,6 +178,7 @@ bool StripSections = false; bool StripUnneeded = false; bool Weaken = false; + bool DecompressDebugSections = false; DebugCompressionType CompressionType = DebugCompressionType::None; }; @@ -430,33 +431,34 @@ Section.Name != ".gdb_index"; } -static void compressSections(const CopyConfig &Config, Object &Obj, - SectionPred &RemovePred) { - SmallVector ToCompress; +static void replaceDebugSections( + const CopyConfig &Config, Object &Obj, SectionPred &RemovePred, + std::function shouldReplace, + std::function addSection) { + SmallVector ToReplace; SmallVector RelocationSections; for (auto &Sec : Obj.sections()) { if (RelocationSection *R = dyn_cast(&Sec)) { - if (isCompressable(*R->getSection())) + if (shouldReplace(*R->getSection())) RelocationSections.push_back(R); continue; } - if (isCompressable(Sec)) - ToCompress.push_back(&Sec); + if (shouldReplace(Sec)) + ToReplace.push_back(&Sec); } - for (SectionBase *S : ToCompress) { - CompressedSection &CS = - Obj.addSection(*S, Config.CompressionType); + for (SectionBase *S : ToReplace) { + SectionBase *NewSection = addSection(S); for (RelocationSection *RS : RelocationSections) { if (RS->getSection() == S) - RS->setSection(&CS); + RS->setSection(NewSection); } } - RemovePred = [RemovePred](const SectionBase &Sec) { - return isCompressable(Sec) || RemovePred(Sec); + RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) { + return shouldReplace(Sec) || RemovePred(Sec); }; } @@ -672,7 +674,19 @@ } if (Config.CompressionType != DebugCompressionType::None) - compressSections(Config, Obj, RemovePred); + replaceDebugSections(Config, Obj, RemovePred, isCompressable, + [&Config, &Obj](const SectionBase *S) { + return &Obj.addSection( + *S, Config.CompressionType); + }); + else if (Config.DecompressDebugSections) + replaceDebugSections( + Config, Obj, RemovePred, + [](const SectionBase &S) { return isa(&S); }, + [&Obj](const SectionBase *S) { + auto CS = cast(S); + return &Obj.addSection(*CS); + }); Obj.removeSections(RemovePred); @@ -978,6 +992,8 @@ Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all); Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); + Config.DecompressDebugSections = + InputArgs.hasArg(OBJCOPY_decompress_debug_sections); for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) Config.SymbolsToLocalize.push_back(Arg->getValue()); for (auto Arg : InputArgs.filtered(OBJCOPY_keep_global_symbol)) @@ -997,6 +1013,15 @@ DriverConfig DC; DC.CopyConfigs.push_back(std::move(Config)); + if (Config.DecompressDebugSections && + Config.CompressionType != DebugCompressionType::None) { + error("Cannot specify --compress-debug-sections at the same time as " + "--decompress-debug-sections at the same time"); + } + + if (Config.DecompressDebugSections && !zlib::isAvailable()) + error("LLVM was not compiled with LLVM_ENABLE_ZLIB: can not decompress."); + return DC; }