Index: include/llvm/Object/Compressor.h =================================================================== --- /dev/null +++ include/llvm/Object/Compressor.h @@ -0,0 +1,101 @@ +//===-- Compressor.h --------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===-----------------------------------------------------------------------===/ + +#ifndef LLVM_OBJECT_COMPRESSOR_H +#define LLVM_OBJECT_COMPRESSOR_H + +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ELFTypes.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Compression.h" +#include "llvm/Support/EndianStream.h" + +namespace llvm { +namespace object { + +/// Compressor helps to handle compression of compressed sections. +class Compressor { +public: + /// Compress section data. + /// @param W Destination buffer stream for compressed data.. + Error writeCompressedSectionData(support::endian::Writer &W) { + SmallVector CompressedBuffer; + auto E = zlib::compress(SectionData, CompressedBuffer); + W.OS << StringRef(CompressedBuffer.data(), CompressedBuffer.size()).str(); + return E; + } + + explicit Compressor(StringRef Data) : SectionData(Data) {} + +private: + StringRef SectionData; +}; + +/// Return the gnu style compressed section name. +Expected getDebugSectionName(const StringRef Name, + bool IsGnuStyle); + +/// Returns if the section can be compressed based on its name (must have a +/// debug name, starts with .*debug. +bool isCompressable(StringRef Name); + +/// Returns if the section is already compressed based on the section contents +/// Name and Flags. +bool isCompressed(StringRef Name, uint64_t Flags); + +template void writeReserved(support::endian::Writer &W) { + if (T::Is64Bits) + W.write(static_cast(0)); // ch_reserved field. +} + +template +void produceZLibHeader(support::endian::Writer &W, uint64_t DecompressedSize, + unsigned Align, bool IsGnuStyle) { + if (IsGnuStyle) { + const StringRef Magic = "ZLIB"; + W.OS << Magic; + support::endian::write(W.OS, DecompressedSize, support::big); + return; + } + + using Chdr = Elf_Chdr_Impl; + W.write(static_cast(ELF::ELFCOMPRESS_ZLIB)); + writeReserved(W); + W.write(static_cast(DecompressedSize)); + W.write(static_cast(Align)); +} + +using CompressedResult = + std::tuple>>; + +// Returns compressed section content, including header data. Return value is +// a tuple that includes if the zlib library hit any errors, followed by a +// boolean denoting if the compressed content plus the header length is smaller +// than before, and lastly followed by the actual compressed section content. +template +CompressedResult compress(const StringRef Contents, uint64_t Align, + bool IsGnuStyle) { + auto CompressedContents = make_unique>(); + raw_svector_ostream OS(*CompressedContents.get()); + support::endian::Writer W(OS, T::TargetEndianness); + produceZLibHeader(W, Contents.size(), Align, IsGnuStyle); + auto C = Compressor(Contents); + auto Error = C.writeCompressedSectionData(W); + bool isSmaller = (CompressedContents->size() < Contents.size()); + return CompressedResult(std::move(Error), isSmaller, + std::move(CompressedContents)); +} + +} // end namespace object +} // end namespace llvm + +#endif // LLVM_OBJECT_COMPRESSOR_H Index: lib/Object/CMakeLists.txt =================================================================== --- lib/Object/CMakeLists.txt +++ lib/Object/CMakeLists.txt @@ -21,6 +21,7 @@ SymbolSize.cpp WasmObjectFile.cpp WindowsResource.cpp + Compressor.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/Object Index: lib/Object/Compressor.cpp =================================================================== --- /dev/null +++ lib/Object/Compressor.cpp @@ -0,0 +1,48 @@ +//===-- Compressor.cpp ----------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/Compressor.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Support/Compression.h" +#include "llvm/Support/DataExtractor.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/EndianStream.h" + +using namespace llvm; +using namespace llvm::support::endian; +using namespace object; + +namespace llvm { +namespace object { +Expected getDebugSectionName(const StringRef Name, + bool IsGnuStyle) { + if (!Name.startswith(".debug") && !Name.startswith(".zdebug")) + return make_error( + "Invalid Debug Section Name.", + std::make_error_code(std::errc::invalid_argument)); + std::string Prefix = IsGnuStyle ? ".z" : "."; + Expected NewName = + Prefix + Name.substr(Name.startswith(".debug") ? 1 : 2).str(); + return NewName; +} + +bool isCompressable(StringRef Name) { + Expected NewName = getDebugSectionName(Name, false); + if (errorToBool(NewName.takeError())) + return false; + return true; +} + +bool isCompressed(StringRef Name, uint64_t Flags) { + return Name.startswith(".zdebug") || (Flags & ELF::SHF_COMPRESSED); +} +} // end namespace object +} // end namespace llvm Index: test/tools/llvm-objcopy/zlib-dwarf.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/zlib-dwarf.test @@ -0,0 +1,220 @@ +# REQUIRES: shell +# REQUIRES: zlib + +# RUN: yaml2obj %s > %t-clean.o +# RUN: llvm-objdump -s %t-clean.o -section=.debug_str | grep -i clang + +# RUN: cp %t-clean.o %t-clean2.o +# RUN: cp %t-clean.o %t-zlib.o +# RUN: cp %t-clean.o %t-zlib-gnu.o + +# RUN: llvm-objcopy %t-clean2.o +# RUN: llvm-objcopy --compress-debug-sections=zlib %t-zlib.o +# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t-zlib-gnu.o + +# RUN: llvm-objdump -s %t-zlib.o -section=.debug_str | grep -vi clang +# RUN: llvm-objdump -s %t-zlib.o | grep -v ZLIB +# RUN: llvm-objdump -s %t-zlib.o | grep -v "\.zdebug" + +# RUN: llvm-objdump -s %t-zlib-gnu.o -section=.debug_str | grep -vi clang +# RUN: llvm-objdump -s %t-zlib-gnu.o | grep ZLIB +# RUN: llvm-objdump -s %t-zlib-gnu.o | grep "\.zdebug" + +# RUN: llvm-objcopy --decompress-debug-sections %t-zlib.o +# RUN: llvm-objcopy --decompress-debug-sections %t-zlib-gnu.o + +# RUN: llvm-objdump -s %t-clean2.o | grep " section \." | cut -f1 -d':' | grep " \.debug" | cut -f4 -d' ' | sort | xargs -I% llvm-objdump -s -section=% %t-clean2.o | grep -v "file format" > %t-clean2.o.txt +# RUN: llvm-objdump -s %t-zlib.o | grep " section \." | cut -f1 -d':' | grep " \.debug" | cut -f4 -d' ' | sort | xargs -I% llvm-objdump -s -section=% %t-zlib.o | grep -v "file format" > %t-zlib.o.txt +# RUN: llvm-objdump -s %t-zlib-gnu.o | grep " section \." | cut -f1 -d':' | grep " \.debug" | cut -f4 -d' ' | sort | xargs -I% llvm-objdump -s -section=% %t-zlib-gnu.o | grep -v "file format" > %t-zlib-gnu.o.txt + +# RUN: cmp %t-clean2.o.txt %t-zlib.o.txt +# RUN: cmp %t-clean2.o.txt %t-zlib-gnu.o.txt + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x0000000000000010 + Content: 554889E54883EC1048B80000000000000000897DFC8975F88B75FC8B55F84889C7B000E8000000008B55FC0355F88945F489D04883C4105DC3 + - Name: .rela.text + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + Info: .text + Relocations: + - Offset: 0x000000000000000A + Symbol: .rodata.str1.1 + Type: R_X86_64_64 + - Offset: 0x0000000000000024 + Symbol: printf + Type: R_X86_64_PC32 + Addend: -4 + - Name: .rodata.str1.1 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x0000000000000001 + Content: 2564202B2025640A00 + - Name: .debug_str + Type: SHT_PROGBITS + Flags: [ SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x0000000000000001 + Content: 46616365626F6F6B20636C616E672076657273696F6E20362E302E3020286C6C766D3A20643161616332376537326138353765393761316432373932383032623335363664373130383161362C206366653A20316431376134323466656137313738613363343434373732303766363033353764386339366335632C20636F6D70696C65722D72743A20663033656661636664666431666533363735383563653962663432313439376661356632653163632C206C6C643A206366396536643666653134313833343339346162306337396233666664643631623732323534613020316431376134323466656137313738613363343434373732303766363033353764386339366335632920287373683A2F2F6769742D726F2E7669702E66616365626F6F6B2E636F6D2F646174612F6769747265706F732F6F736D6574612F65787465726E616C2F6C6C766D20643161616332376537326138353765393761316432373932383032623335363664373130383161362920286261736564206F6E204C4C564D20362E302E302900666F6F2E63002F686F6D652F706C6F7466692F50726F6A656374732F6C6C766D2D7374616E64616C6F6E6500666F6F00696E740061006200 + - Name: .debug_abbrev + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: 011101250E1305030E10171B0EB44219110112060000022E01110112064018030E3A0B3B0B271949133F1900000305000218030E3A0B3B0B49130000042400030E3E0B0B0B000000 + - Name: .debug_info + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: 640000000400000000000801000000000C00000000000000000000000000000000000000000039000000020000000000000000390000000156000000000102600000000302917C000000000102600000000302917800000000010260000000000400000000050400 + - Name: .rela.debug_info + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + Info: .debug_info + Relocations: + - Offset: 0x0000000000000006 + Symbol: .debug_abbrev + Type: R_X86_64_32 + - Offset: 0x000000000000000C + Symbol: .debug_str + Type: R_X86_64_32 + - Offset: 0x0000000000000012 + Symbol: .debug_str + Type: R_X86_64_32 + Addend: 398 + - Offset: 0x0000000000000016 + Symbol: .debug_line + Type: R_X86_64_32 + - Offset: 0x000000000000001A + Symbol: .debug_str + Type: R_X86_64_32 + Addend: 404 + - Offset: 0x000000000000001E + Symbol: .text + Type: R_X86_64_64 + - Offset: 0x000000000000002B + Symbol: .text + Type: R_X86_64_64 + - Offset: 0x0000000000000039 + Symbol: .debug_str + Type: R_X86_64_32 + Addend: 442 + - Offset: 0x0000000000000047 + Symbol: .debug_str + Type: R_X86_64_32 + Addend: 450 + - Offset: 0x0000000000000055 + Symbol: .debug_str + Type: R_X86_64_32 + Addend: 452 + - Offset: 0x0000000000000061 + Symbol: .debug_str + Type: R_X86_64_32 + Addend: 446 + - Name: .debug_ranges + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: '' + - Name: .debug_macinfo + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: '00' + - Name: .debug_pubnames + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: 16000000020000000000680000002A000000666F6F0000000000 + - Name: .rela.debug_pubnames + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + Info: .debug_pubnames + Relocations: + - Offset: 0x0000000000000006 + Symbol: .debug_info + Type: R_X86_64_32 + - Name: .debug_pubtypes + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: '160000000200000000006800000060000000696E740000000000' + - Name: .rela.debug_pubtypes + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + Info: .debug_pubtypes + Relocations: + - Offset: 0x0000000000000006 + Symbol: .debug_info + Type: R_X86_64_32 + - Name: .comment + Type: SHT_PROGBITS + Flags: [ SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x0000000000000001 + Content: 0046616365626F6F6B20636C616E672076657273696F6E20362E302E3020286C6C766D3A20643161616332376537326138353765393761316432373932383032623335363664373130383161362C206366653A20316431376134323466656137313738613363343434373732303766363033353764386339366335632C20636F6D70696C65722D72743A20663033656661636664666431666533363735383563653962663432313439376661356632653163632C206C6C643A206366396536643666653134313833343339346162306337396233666664643631623732323534613020316431376134323466656137313738613363343434373732303766363033353764386339366335632920287373683A2F2F6769742D726F2E7669702E66616365626F6F6B2E636F6D2F646174612F6769747265706F732F6F736D6574612F65787465726E616C2F6C6C766D20643161616332376537326138353765393761316432373932383032623335363664373130383161362920286261736564206F6E204C4C564D20362E302E302900 + - Name: .note.GNU-stack + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: '' + - Name: .eh_frame + Type: SHT_X86_64_UNWIND + Flags: [ SHF_ALLOC ] + AddressAlign: 0x0000000000000008 + Content: 1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000003900000000410E108602430D0600000000000000 + - Name: .rela.eh_frame + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + Info: .eh_frame + Relocations: + - Offset: 0x0000000000000020 + Symbol: .text + Type: R_X86_64_PC32 + - Name: .debug_line + Type: SHT_PROGBITS + AddressAlign: 0x0000000000000001 + Content: 4B00000004001D000000010101FB0E0D00010101010000000100000100666F6F2E63000000000000090200000000000000001305170A0875051A063C05033C050A069F050C063C05033C020B000101 + - Name: .rela.debug_line + Type: SHT_RELA + Link: .symtab + AddressAlign: 0x0000000000000008 + Info: .debug_line + Relocations: + - Offset: 0x000000000000002A + Symbol: .text + Type: R_X86_64_64 +Symbols: + Local: + - Name: foo.c + Type: STT_FILE + - Name: .text + Type: STT_SECTION + Section: .text + - Name: .rodata.str1.1 + Type: STT_SECTION + Section: .rodata.str1.1 + - Name: .debug_str + Type: STT_SECTION + Section: .debug_str + - Name: .debug_abbrev + Type: STT_SECTION + Section: .debug_abbrev + - Name: .debug_info + Type: STT_SECTION + Section: .debug_info + - Name: .debug_line + Type: STT_SECTION + Section: .debug_line + Global: + - Name: foo + Type: STT_FUNC + Section: .text + Size: 0x0000000000000039 + - Name: printf +DynamicSymbols: +... Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -17,6 +17,11 @@ defm output_target : Eq<"output-target">, HelpText<"Format of the output file">, Values<"binary">; +defm compress_debug_sections : Eq<"compress-debug-sections">, + MetaVarName<"[ none | zlib | zlib-gnu ]">, + HelpText<"Enable zlib-gnu or zlib Compression of DWARF debug sections.">; +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 @@ -15,6 +15,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/MC/StringTableBuilder.h" +#include "llvm/Object/Compressor.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/JamCRC.h" @@ -39,6 +40,8 @@ class GnuDebugLinkSection; class GroupSection; class SectionIndexSection; +class CompressedSection; +class DecompressedSection; class Segment; class Object; struct Symbol; @@ -77,6 +80,8 @@ virtual void visit(const GnuDebugLinkSection &Sec) = 0; 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 { @@ -95,6 +100,8 @@ virtual void visit(const GnuDebugLinkSection &Sec) override = 0; 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) {} }; @@ -112,6 +119,8 @@ void visit(const GnuDebugLinkSection &Sec) override; 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) {} }; @@ -129,6 +138,8 @@ void visit(const GnuDebugLinkSection &Sec) override; 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) {} }; @@ -259,6 +270,10 @@ virtual void removeSymbols(function_ref ToRemove); virtual void accept(SectionVisitor &Visitor) const = 0; virtual void markSymbols(); + virtual StringRef getContents() const { + assert(false && "Section has no Contents."); + return ""; + } }; class Segment { @@ -317,6 +332,9 @@ void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; void finalize() override; + StringRef getContents() const override { + return StringRef((const char *)Contents.data(), Contents.size()); + } }; class OwnedDataSection : public SectionBase { @@ -334,6 +352,67 @@ } void accept(SectionVisitor &Sec) const override; + StringRef getContents() const override { + return StringRef((const char *)Data.data(), Data.size()); + } +}; + +class CompressedSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + std::string OwnedName; + std::unique_ptr> ModifiedData; + + void setName(std::string NewName) { + OwnedName = NewName; + Name = OwnedName; + } + +public: + CompressedSection(const SectionBase &Sec, + std::unique_ptr> CompressedData) + : ModifiedData(std::move(CompressedData)) { + Align = Sec.Align; + bool IsGnuStyle = StringRef(ModifiedData->data(), 4).startswith("ZLIB"); + Expected NewName = + object::getDebugSectionName(Sec.Name, IsGnuStyle); + assert(NewName && "Expected a valid debug section name."); + setName(IsGnuStyle ? *NewName : Sec.Name.str()); + Size = ModifiedData->size(); + + if (!IsGnuStyle) + Flags |= ELF::SHF_COMPRESSED; + } + + void finalize() override; + void accept(SectionVisitor &Visitor) const override; +}; + +class DecompressedSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + std::string OwnedName; + std::unique_ptr> ModifiedData; + + void setName(std::string NewName) { + OwnedName = NewName; + Name = OwnedName; + } + +public: + DecompressedSection(const SectionBase &Sec, + std::unique_ptr> DecompressedData) + : ModifiedData(std::move(DecompressedData)) { + Expected NewName = + object::getDebugSectionName(Sec.Name, false); + assert(NewName && "Expected a valid debug section name."); + setName(*NewName); + Align = Sec.Align; + Size = ModifiedData->size(); + } + + void finalize() override; + void accept(SectionVisitor &Visitor) const override; }; // There are two types of string tables that can exist, dynamic and not dynamic. Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -106,6 +106,14 @@ error("Cannot write symbol section index table '" + Sec.Name + "' "); } +void BinarySectionWriter::visit(const CompressedSection &Sec) { + error("Cannot write compressed section '" + Sec.Name + "' "); +} + +void BinarySectionWriter::visit(const DecompressedSection &Sec) { + error("Cannot write compressed section '" + Sec.Name + "' "); +} + void BinarySectionWriter::visit(const SymbolTableSection &Sec) { error("Cannot write symbol table '" + Sec.Name + "' out to binary"); } @@ -140,6 +148,30 @@ Visitor.visit(*this); } +template +void ELFSectionWriter::visit(const CompressedSection &Sec) { + uint8_t *Buf = Out.getBufferStart(); + Buf += Sec.Offset; + std::copy(Sec.ModifiedData->begin(), Sec.ModifiedData->end(), Buf); +} + +void CompressedSection::finalize() {} +void CompressedSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +template +void ELFSectionWriter::visit(const DecompressedSection &Sec) { + uint8_t *Buf = Out.getBufferStart(); + Buf += Sec.Offset; + std::copy(Sec.ModifiedData->begin(), Sec.ModifiedData->end(), Buf); +} + +void DecompressedSection::finalize() {} +void DecompressedSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); Size = StrTabBuilder.getSize(); Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -16,9 +16,12 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCTargetOptions.h" #include "llvm/Object/Archive.h" #include "llvm/Object/ArchiveWriter.h" #include "llvm/Object/Binary.h" +#include "llvm/Object/Compressor.h" +#include "llvm/Object/Decompressor.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ELFTypes.h" #include "llvm/Object/Error.h" @@ -157,6 +160,8 @@ bool DiscardAll = false; bool OnlyKeepDebug = false; bool KeepFileSymbols = false; + bool DecompressDebugSections = false; + DebugCompressionType CompressDebugSections = DebugCompressionType::None; }; using SectionPred = std::function; @@ -323,6 +328,111 @@ Writer->write(); } +Expected>> +decompress(StringRef Name, StringRef Contents, ElfType OutputElfType) { + + auto setElfType = [](ElfType OutputElfType) -> std::tuple { + switch (OutputElfType) { + case ELFT_ELF32LE: + return std::tuple(false, true); + case ELFT_ELF64LE: + return std::tuple(true, true); + case ELFT_ELF32BE: + return std::tuple(false, false); + case ELFT_ELF64BE: + return std::tuple(true, false); + } + llvm_unreachable("Invalid output format"); + }; + + bool Is64Bit, IsLittle; + std::tie(Is64Bit, IsLittle) = setElfType(OutputElfType); + + auto D = object::Decompressor::create(Name, Contents, IsLittle, Is64Bit); + if (!D) { + auto Err = D.takeError(); + return std::move(Err); + } + + auto DecompressedContents = make_unique>(); + if (auto Err = D->resizeAndDecompress(*DecompressedContents.get())) { + return std::move(Err); + } + + return std::move(DecompressedContents); +} + +object::CompressedResult compress(const StringRef Contents, uint64_t Align, + bool IsGnuStyle, ElfType OutputElfType) { + switch (OutputElfType) { + case ELFT_ELF32LE: + return object::compress(Contents, Align, IsGnuStyle); + case ELFT_ELF64LE: + return object::compress(Contents, Align, IsGnuStyle); + case ELFT_ELF32BE: + return object::compress(Contents, Align, IsGnuStyle); + case ELFT_ELF64BE: + return object::compress(Contents, Align, IsGnuStyle); + } + llvm_unreachable("Invalid output format"); +} + +static void tryCompressSections(const CopyConfig &Config, Object &Obj, + SectionPred &RemovePred, + ElfType OutputElfType) { + for (auto &Section : Obj.sections()) { + if (object::isCompressed(Section.Name, Section.Flags)) + continue; + if (!object::isCompressable(Section.Name)) + continue; + + auto Result = + compress(Section.getContents(), Section.Align, + (Config.CompressDebugSections == DebugCompressionType::GNU), + OutputElfType); + + bool IsSmaller = std::get<1>(Result); + bool DoBail = !IsSmaller || (nullptr == std::get<2>(Result)); + Error Err = std::move(std::get<0>(Result)); + if (Err) { + DoBail = true; + reportError(Config.InputFilename, std::move(Err)); + } else { + consumeError(std::move(Err)); + } + + if (DoBail) + continue; + + // Replace this Section with a compressed version. + RemovePred = [RemovePred, &Section](const SectionBase &Sec) { + return &Sec == &Section || RemovePred(Sec); + }; + Obj.addSection(Section, std::move(std::get<2>(Result))); + } +} + +static void tryDecompressSections(const CopyConfig &Config, Object &Obj, + SectionPred &RemovePred, + ElfType OutputElfType) { + for (auto &Section : Obj.sections()) { + if (!object::isCompressed(Section.Name, Section.Flags)) + continue; + auto DecompressedContents = + decompress(Section.Name, Section.getContents(), OutputElfType); + + if (!DecompressedContents) + reportError(Config.InputFilename, DecompressedContents.takeError()); + + // Replace this Section with a decompressed version. + RemovePred = [RemovePred, &Section](const SectionBase &Sec) { + return &Sec == &Section || RemovePred(Sec); + }; + Obj.addSection(Section, + std::move(*DecompressedContents)); + } +} + // This function handles the high level operations of GNU objcopy including // handling command line options. It's important to outline certain properties // we expect to hold of the command line operations. Any operation that "keeps" @@ -517,6 +627,12 @@ }; } + if (Config.CompressDebugSections != DebugCompressionType::None) { + tryCompressSections(Config, Obj, RemovePred, OutputElfType); + } else if (Config.DecompressDebugSections) { + tryDecompressSections(Config, Obj, RemovePred, OutputElfType); + } + Obj.removeSections(RemovePred); if (!Config.SectionsToRename.empty()) { @@ -686,6 +802,19 @@ Config.OutputFormat = InputArgs.getLastArgValue(OBJCOPY_output_target); Config.BinaryArch = InputArgs.getLastArgValue(OBJCOPY_binary_architecture); + Config.CompressDebugSections = + StringSwitch( + InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections)) + .Case("zlib-gnu", DebugCompressionType::GNU) + .Case("zlib", DebugCompressionType::Z) + .Default(DebugCompressionType::None); + + if (Config.CompressDebugSections == DebugCompressionType::None && + InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections) != "") { + error("Invaid or unsupported --compress-debug-sections format: " + + InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections)); + } + Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); @@ -724,6 +853,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_globalize_symbol)) @@ -735,6 +866,12 @@ for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) Config.SymbolsToKeep.push_back(Arg->getValue()); + if (Config.DecompressDebugSections && + Config.CompressDebugSections != DebugCompressionType::None) { + error("Cannot specify --compress-debug-sections as well as " + "--decompress-debug-sections at the same time."); + } + return Config; }