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 as well as --decompress-debug-sections at the same time. + 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: test/tools/llvm-objcopy/decompress-debug-sections-zlib-gnu.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/decompress-debug-sections-zlib-gnu.test @@ -0,0 +1,52 @@ +# REQUIRES: zlib + +# 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 -s -r %t-compressed.o | FileCheck %s --check-prefix=CHECK-FLAGS +# RUN: llvm-objdump -s %t-decompressed.o -section=.debug_foo | FileCheck %s + + +# CHECK: .debug_foo: + +# CHECK-COMPRESSED: .zdebug_foo: +# CHECK-COMPRESSED: ZLIB +# CHECK-COMPRESSED: .notdebug_foo: + +# CHECK-FLAGS-NOT: Name: .debug_foo +# CHECK-FLAGS: Index: 1 +# CHECK-FLAGS-NEXT: Name: .zdebug_foo +# CHECK-FLAGS-NEXT: Type: SHT_PROGBITS +# CHECK-FLAGS-NEXT: Flags [ +# CHECK-FLAGS-NEXT: ] +# CHECK-FLAGS-NEXT: Address: +# CHECK-FLAGS-NEXT: Offset: +# CHECK-FLAGS-NEXT: Size: 23 + +# CHECK-FLAGS: Name: .notdebug_foo +# CHECK-FLAGS-NEXT: Type: SHT_PROGBITS +# CHECK-FLAGS-NEXT: Flags [ +# CHECK-FLAGS-NEXT: ] +# CHECK-FLAGS-NEXT: Address: +# CHECK-FLAGS-NEXT: Offset: +# CHECK-FLAGS-NEXT: Size: 8 + +# CHECK-FLAGS: Name: .rela.debug_foo +# CHECK-FLAGS-NEXT: Type: SHT_RELA +# CHECK-FLAGS-NEXT: Flags [ +# CHECK-FLAGS-NEXT: ] +# CHECK-FLAGS-NEXT: Address: +# CHECK-FLAGS-NEXT: Offset: +# CHECK-FLAGS-NEXT: Size: +# CHECK-FLAGS-NEXT: Link: +# CHECK-FLAGS-NEXT: Info: 1 + +# CHECK-FLAGS: Relocations [ +# CHECK-FLAGS-NEXT: .rela.debug_foo { +# CHECK-FLAGS-NEXT: 0x1 R_X86_64_32 - 0x0 +# CHECK-FLAGS-NEXT: } +# CHECK-FLAGS-NEXT: ] + Index: test/tools/llvm-objcopy/decompress-debug-sections-zlib.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/decompress-debug-sections-zlib.test @@ -0,0 +1,50 @@ +# REQUIRES: zlib + +# 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 -s -r %t-compressed.o | FileCheck %s --check-prefix=CHECK-FLAGS +# RUN: llvm-objdump -s %t-decompressed.o -section=.debug_foo | FileCheck %s + +# CHECK: .debug_foo: + +# CHECK-COMPRESSED: .notdebug_foo: +# CHECK-COMPRESSED: .debug_foo: + +# CHECK-FLAGS: Index: 1 +# CHECK-FLAGS-NEXT: Name: .debug_foo +# CHECK-FLAGS-NEXT: Type: SHT_PROGBITS +# CHECK-FLAGS-NEXT: Flags [ +# CHECK-FLAGS-NEXT: SHF_COMPRESSED +# CHECK-FLAGS-NEXT: ] +# CHECK-FLAGS-NEXT: Address: +# CHECK-FLAGS-NEXT: Offset: +# CHECK-FLAGS-NEXT: Size: 35 + +# CHECK-FLAGS: Name: .notdebug_foo +# CHECK-FLAGS-NEXT: Type: SHT_PROGBITS +# CHECK-FLAGS-NEXT: Flags [ +# CHECK-FLAGS-NEXT: ] +# CHECK-FLAGS-NEXT: Address: +# CHECK-FLAGS-NEXT: Offset: +# CHECK-FLAGS-NEXT: Size: 8 + +# CHECK-FLAGS: Name: .rela.debug_foo +# CHECK-FLAGS-NEXT: Type: SHT_RELA +# CHECK-FLAGS-NEXT: Flags [ +# CHECK-FLAGS-NEXT: ] +# CHECK-FLAGS-NEXT: Address: +# CHECK-FLAGS-NEXT: Offset: +# CHECK-FLAGS-NEXT: Size: +# CHECK-FLAGS-NEXT: Link: +# CHECK-FLAGS-NEXT: Info: 1 + +# CHECK-FLAGS: Relocations [ +# CHECK-FLAGS-NEXT: .rela.debug_foo { +# CHECK-FLAGS-NEXT: 0x1 R_X86_64_32 - 0x0 +# CHECK-FLAGS-NEXT: } +# CHECK-FLAGS-NEXT: ] + 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) {} }; @@ -359,6 +364,37 @@ void accept(SectionVisitor &Sec) const override; }; +class DecompressedSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + uint64_t DecompressedSize; + uint64_t DecompressedAlign; + bool DoDecompression = false; + +public: + DecompressedSection(ArrayRef CompressedData, + uint64_t DecompressedSize, uint64_t DecompressedAlign) + : DecompressedSize(DecompressedSize), + DecompressedAlign(DecompressedAlign) { + OriginalData = CompressedData; + } + + void enableDecompression() { + DoDecompression = true; + Size = DecompressedSize; + Flags = (Flags & ~ELF::SHF_COMPRESSED); + if (StringRef(Name).startswith(".zdebug")) + Name = "." + Name.substr(2); + } + + void accept(SectionVisitor &Visitor) const override; + + static bool classof(const SectionBase *S) { + return (S->Flags & ELF::SHF_COMPRESSED) || + (StringRef(S->Name).startswith(".zdebug")); + } +}; + class CompressedSection : public SectionBase { MAKE_SEC_WRITER_FRIEND Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -136,6 +136,47 @@ std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf); } +static bool isCompressedGnuContents(const SectionBase &Section) { + const char *Magic = "ZLIB"; + return Section.OriginalData.size() > strlen(Magic) && + !strncmp(reinterpret_cast(Section.OriginalData.data()), + Magic, strlen(Magic)); +} + +template +void ELFSectionWriter::visit(const DecompressedSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + + if (!Sec.DoDecompression) { + std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf); + return; + } + + const unsigned dataOffset = + isCompressedGnuContents(Sec) + ? (strlen("ZLIB") + 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, + (size_t)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); } @@ -971,6 +1012,28 @@ return Obj.addSection
(Data); default: Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + + const bool IsGnuDebug = + StringRef((const char *)Data.data(), Data.size()).startswith("ZLIB"); + if (IsGnuDebug || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) { + + const uint64_t DecompressedSize = + IsGnuDebug + ? support::endian::read64be(reinterpret_cast( + Data.data() + strlen("ZLIB"))) + : reinterpret_cast *>(Data.data()) + ->ch_size; + + const uint64_t DecompressedAlign = + IsGnuDebug + ? 1 + : reinterpret_cast *>(Data.data()) + ->ch_addralign; + + return Obj.addSection(Data, DecompressedSize, + DecompressedAlign); + } + return Obj.addSection
(Data); } } 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; }; @@ -671,8 +672,14 @@ }; } - if (Config.CompressionType != DebugCompressionType::None) + if (Config.CompressionType != DebugCompressionType::None) { compressSections(Config, Obj, RemovePred); + } else if (Config.DecompressDebugSections) { + for (auto &Sec : Obj.sections()) { + if (auto DDS = dyn_cast(&Sec)) + DDS->enableDecompression(); + } + } Obj.removeSections(RemovePred); @@ -978,6 +985,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 +1006,12 @@ DriverConfig DC; DC.CopyConfigs.push_back(std::move(Config)); + if (Config.DecompressDebugSections && + Config.CompressionType != DebugCompressionType::None) { + error("Cannot specify --compress-debug-sections as well as " + "--decompress-debug-sections at the same time"); + } + return DC; }