Index: include/llvm/Object/Compressor.h =================================================================== --- /dev/null +++ include/llvm/Object/Compressor.h @@ -0,0 +1,116 @@ +//===-- 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/MC/MCTargetOptions.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" +#include "llvm/Support/Errc.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; +}; + +/// Returns a new debug section name based on Name and CompressionType. This +/// function assumes that Name is one of the standard debug section names that +/// begins with either .debug or .zdebug: any other prefix will result in an +/// Error. +/// +/// If CompressionType is GNU, then the name prefix is set to ".zdebug", +/// otherwise the prefix will be set to ".debug". This function can be used to +/// fixup GNU ".zdebug" names to the corresponding ".debug" name during +/// decompression. +Expected +getNewDebugSectionName(const StringRef Name, + DebugCompressionType CompressionType); + +/// Returns if the section can be compressed based on its name (must have a +/// debug name, starts with .*debug. +bool isCompressableSectionName(StringRef Name); + +template void writeReserved(support::endian::Writer &W) { + if (T::Is64Bits) + W.write(static_cast(0)); // ch_reserved field. +} + +/// Writes the proper compression header info to W depending on CompressionType. +template +Error produceZLibHeader(support::endian::Writer &W, uint64_t DecompressedSize, + unsigned Align, DebugCompressionType CompressionType) { + if (CompressionType != DebugCompressionType::GNU && + CompressionType != DebugCompressionType::Z) { + return createStringError( + llvm::errc::invalid_argument, + "Invalid DebugCompressionType, only GNU and ZLIB are supported."); + } + + if (CompressionType == DebugCompressionType::GNU) { + const StringRef Magic = "ZLIB"; + W.OS << Magic; + support::endian::write(W.OS, DecompressedSize, support::big); + return Error::success(); + } + + using Chdr = Elf_Chdr_Impl; + W.write(static_cast(ELF::ELFCOMPRESS_ZLIB)); + writeReserved(W); + W.write(static_cast(DecompressedSize)); + W.write(static_cast(Align)); + return Error::success(); +} + +// 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 +Expected>> +compress(const StringRef Contents, uint64_t Align, + DebugCompressionType CompressionType) { + auto CompressedContents = make_unique>(); + raw_svector_ostream OS(*CompressedContents.get()); + support::endian::Writer W(OS, T::TargetEndianness); + if (Error E = + produceZLibHeader(W, Contents.size(), Align, CompressionType)) + return std::move(E); + Compressor C(Contents); + if (Error E = C.writeCompressedSectionData(W)) + return std::move(E); + return 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,47 @@ +//===-- 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" +#include "llvm/Support/Errc.h" + +using namespace llvm; +using namespace llvm::support::endian; +using namespace object; + +namespace llvm { +namespace object { + +bool isCompressableSectionName(StringRef Name) { + return (Name.startswith(".debug") || Name.startswith(".zdebug")); +} + +Expected +getNewDebugSectionName(const StringRef Name, + DebugCompressionType CompressionType) { + if (!isCompressableSectionName(Name)) { + return createStringError(llvm::errc::invalid_argument, + "Invalid Debug Section Name: %s.", + Name.str().c_str()); + } + std::string Prefix = + (CompressionType == DebugCompressionType::GNU) ? ".z" : "."; + Expected NewName = + Prefix + Name.substr(Name.startswith(".debug") ? 1 : 2).str(); + return NewName; +} + +} // end namespace object +} // end namespace llvm Index: test/tools/llvm-objcopy/compress-debug-sections-zlib-gnu.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/compress-debug-sections-zlib-gnu.test @@ -0,0 +1,26 @@ +# REQUIRES: zlib + +# Reuse compress-debug-sections.test. +# Here we are testing that zlib-gnu properly sets the right magic header and +# also roundtrips properly. + +# RUN: yaml2obj %S/compress-debug-sections.test > %t-clean.o +# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t-clean.o %t-zlib-gnu.o +# RUN: llvm-objcopy --decompress-debug-sections %t-zlib-gnu.o %t-decompressed.o + +# RUN: llvm-objdump -s %t-clean.o -section=.debug_str | FileCheck %s +# RUN: llvm-objdump -s %t-zlib-gnu.o | FileCheck %s --check-prefix=CHECK-ZLIB-GNU +# RUN: llvm-objdump -s %t-decompressed.o -section=.debug_str | FileCheck %s + +# CHECK: .debug_str +# CHECK: clang + +# CHECK-ZLIB-GNU: .zdebug_str +# CHECK-ZLIB-GNU: ZLIB + +# CHECK-ZLIB-GNU: .zdebug_info +# CHECK-ZLIB-GNU: ZLIB + +# CHECK-ZLIB-GNU: .zdebug_line +# CHECK-ZLIB-GNU: ZLIB + Index: test/tools/llvm-objcopy/compress-debug-sections.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/compress-debug-sections.test @@ -0,0 +1,225 @@ +# REQUIRES: shell +# REQUIRES: zlib + +# Produce the obj file from yaml. Make sure the original has some recognizable +# .debug_str contents, ie "clang" +# RUN: yaml2obj %s > %t-clean.o + +# This produces a small shell script that takes an object file, reads out and +# sorts its debug section names, and then in sorted order it runs llvm-objdump +# (via xargs) on each debug section while redirecting that output to a text +# file. We compare the txt files of the original vs zlib vs zlib-gnu +# round-tripping. +# RUN: echo "llvm-objdump -s \$1 | grep \" section \.\" | cut -f1 -d':' | \ +# RUN: grep \" \.debug\" | cut -f4 -d' ' | sort | \ +# RUN: xargs -I% llvm-objdump -s -section=% \$1 | \ +# RUN: grep -v \"file format\" > \$1.txt" > generateObjTxtOutput.sh + +# RUN: cp %t-clean.o %t-clean2.o +# RUN: llvm-objcopy %t-clean2.o +# RUN: sh generateObjTxtOutput.sh %t-clean2.o + +# RUN: cp %t-clean.o %t-zlib.o +# RUN: llvm-objcopy --compress-debug-sections=zlib %t-zlib.o +# RUN: llvm-objcopy --decompress-debug-sections %t-zlib.o +# RUN: sh generateObjTxtOutput.sh %t-zlib.o +# RUN: diff %t-clean2.o.txt %t-zlib.o.txt +# RUN: rm -f %t-zlib.o.txt %t-zlib.o + +# RUN: cp %t-clean.o %t-zlib-gnu.o +# RUN: llvm-objcopy --compress-debug-sections=zlib-gnu %t-zlib-gnu.o +# RUN: llvm-objcopy --decompress-debug-sections %t-zlib-gnu.o +# RUN: sh generateObjTxtOutput.sh %t-zlib-gnu.o +# RUN: diff %t-clean2.o.txt %t-zlib-gnu.o.txt +# RUN: rm -f %t-zlib-gnu.o.txt %t-zlib-gnu.o + +# RUN: rm -f %t-clean2.o.txt %t-clean2.o %t-clean.o generateObjTxtOutput.sh + +--- !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,7 @@ class GnuDebugLinkSection; class GroupSection; class SectionIndexSection; +class CompressedSection; class Segment; class Object; struct Symbol; @@ -77,6 +79,7 @@ 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; }; class SectionWriter : public SectionVisitor { @@ -95,6 +98,7 @@ 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; explicit SectionWriter(Buffer &Buf) : Out(Buf) {} }; @@ -112,6 +116,7 @@ void visit(const GnuDebugLinkSection &Sec) override; void visit(const GroupSection &Sec) override; void visit(const SectionIndexSection &Sec) override; + void visit(const CompressedSection &Sec) override; explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {} }; @@ -129,6 +134,7 @@ void visit(const GnuDebugLinkSection &Sec) override; void visit(const GroupSection &Sec) override; void visit(const SectionIndexSection &Sec) override; + void visit(const CompressedSection &Sec) override; explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {} }; @@ -259,6 +265,8 @@ virtual void removeSymbols(function_ref ToRemove); virtual void accept(SectionVisitor &Visitor) const = 0; virtual void markSymbols(); + virtual Expected> getOriginalContents() const; + bool isCompressed() const; }; class Segment { @@ -317,23 +325,53 @@ void removeSectionReferences(const SectionBase *Sec) override; void initialize(SectionTableRef SecTable) override; void finalize() override; + Expected> getOriginalContents() const override; }; class OwnedDataSection : public SectionBase { MAKE_SEC_WRITER_FRIEND +protected: + std::string OwnedName; std::vector Data; + void setName(std::string NewName) { + OwnedName = NewName; + Name = OwnedName; + } + public: OwnedDataSection(StringRef SecName, ArrayRef Data) : Data(std::begin(Data), std::end(Data)) { - Name = SecName; + setName(SecName.str()); Type = ELF::SHT_PROGBITS; Size = Data.size(); OriginalOffset = std::numeric_limits::max(); } void accept(SectionVisitor &Sec) const override; + Expected> getOriginalContents() const override; +}; + +class CompressedSection : public OwnedDataSection { + MAKE_SEC_WRITER_FRIEND + + bool isGnuStyle() const { + ArrayRef GnuPrefix = {'Z', 'L', 'I', 'B'}; + return std::equal(GnuPrefix.begin(), GnuPrefix.end(), Data.data()); + } + +public: + CompressedSection(StringRef NewName, ArrayRef Data, + const SectionBase &Sec) + : OwnedDataSection(NewName, Data) { + Align = Sec.Align; + if (!isGnuStyle()) + Flags |= ELF::SHF_COMPRESSED; + } + + 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 @@ -83,6 +83,14 @@ void SectionBase::initialize(SectionTableRef SecTable) {} void SectionBase::finalize() {} void SectionBase::markSymbols() {} +Expected> SectionBase::getOriginalContents() const { + return make_error( + "No Section Contents.", + std::make_error_code(std::errc::operation_not_permitted)); +} +bool SectionBase::isCompressed() const { + return Name.startswith(".zdebug") || (Flags & ELF::SHF_COMPRESSED); +} template void ELFWriter::writeShdr(const SectionBase &Sec) { uint8_t *B = Buf.getBufferStart(); @@ -106,6 +114,10 @@ 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 SymbolTableSection &Sec) { error("Cannot write symbol table '" + Sec.Name + "' out to binary"); } @@ -139,6 +151,21 @@ void OwnedDataSection::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } +Expected> OwnedDataSection::getOriginalContents() const { + return ArrayRef(Data.data(), Data.size()); +} + +template +void ELFSectionWriter::visit(const CompressedSection &Sec) { + uint8_t *Buf = Out.getBufferStart(); + Buf += Sec.Offset; + std::copy(Sec.Data.begin(), Sec.Data.end(), Buf); +} + +void CompressedSection::finalize() {} +void CompressedSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); @@ -490,6 +517,9 @@ } void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; } +Expected> Section::getOriginalContents() const { + return Contents; +} void GnuDebugLinkSection::init(StringRef File, StringRef Data) { FileName = sys::path::filename(File); 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,137 @@ Writer->write(); } +Expected>> +decompress(StringRef Name, StringRef Contents, ElfType OutputElfType) { + bool Is64Bit, IsLittle; + std::tie(Is64Bit, + IsLittle) = [](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"); + }(OutputElfType); + + Expected D = + object::Decompressor::create(Name, Contents, IsLittle, Is64Bit); + if (!D) + return D.takeError(); + + std::unique_ptr> DecompressedContents = + make_unique>(); + if (Error E = D->resizeAndDecompress(*DecompressedContents.get())) { + return std::move(E); + } + + return std::move(DecompressedContents); +} + +Expected>> +compress(StringRef Contents, uint64_t Align, + DebugCompressionType CompressionType, ElfType OutputElfType) { + switch (OutputElfType) { + case ELFT_ELF32LE: + return object::compress(Contents, Align, CompressionType); + case ELFT_ELF64LE: + return object::compress(Contents, Align, CompressionType); + case ELFT_ELF32BE: + return object::compress(Contents, Align, CompressionType); + case ELFT_ELF64BE: + return object::compress(Contents, Align, CompressionType); + } + llvm_unreachable("Invalid output format"); +} + +static void compressSections(const CopyConfig &Config, Object &Obj, + SectionPred &RemovePred, ElfType OutputElfType) { + for (auto &Section : Obj.sections()) { + if (Section.isCompressed() || + !object::isCompressableSectionName(Section.Name)) + continue; + + Expected> ContentsOrError = Section.getOriginalContents(); + if (!ContentsOrError) + reportError(Config.InputFilename, ContentsOrError.takeError()); + StringRef Contents(reinterpret_cast(ContentsOrError->data()), + ContentsOrError->size()); + + Expected>> CompressedContentOrError = + compress(Contents, Section.Align, Config.CompressDebugSections, + OutputElfType); + + if (!CompressedContentOrError) + reportError(Config.InputFilename, CompressedContentOrError.takeError()); + + std::unique_ptr> CompressedContents = + std::move(*CompressedContentOrError); + bool IsSmaller = (CompressedContents->size() < Contents.size()); + if (!IsSmaller) + continue; + + Expected NewNameOrError = object::getNewDebugSectionName( + Section.Name, Config.CompressDebugSections); + if (!NewNameOrError) + reportError(Config.InputFilename, NewNameOrError.takeError()); + + auto BufPtr = reinterpret_cast(CompressedContents->data()); + auto BufSize = CompressedContents->size(); + StringRef NewName(*NewNameOrError); + Obj.addSection( + NewName, ArrayRef(BufPtr, BufSize), Section); + + // Replace this Section with a compressed version. + RemovePred = [RemovePred, &Section](const SectionBase &Sec) { + return &Sec == &Section || RemovePred(Sec); + }; + } +} + +static void decompressSections(const CopyConfig &Config, Object &Obj, + SectionPred &RemovePred, ElfType OutputElfType) { + for (auto &Section : Obj.sections()) { + if (!Section.isCompressed()) + continue; + + Expected> ContentsOrError = Section.getOriginalContents(); + if (!ContentsOrError) + reportError(Config.InputFilename, ContentsOrError.takeError()); + StringRef Contents(reinterpret_cast(ContentsOrError->data()), + ContentsOrError->size()); + + Expected>> + DecompressedContentsOrError = + decompress(Section.Name, Contents, OutputElfType); + + if (!DecompressedContentsOrError) + reportError(Config.InputFilename, + DecompressedContentsOrError.takeError()); + + Expected NewNameOrError = object::getNewDebugSectionName( + Section.Name, DebugCompressionType::None); + if (!NewNameOrError) + reportError(Config.InputFilename, NewNameOrError.takeError()); + StringRef NewName(*NewNameOrError); + + auto BufPtr = reinterpret_cast( + (*DecompressedContentsOrError)->data()); + auto BufSize = (*DecompressedContentsOrError)->size(); + Obj.addSection(NewName, + ArrayRef(BufPtr, BufSize)); + + // Replace this Section with a decompressed version. + RemovePred = [RemovePred, &Section](const SectionBase &Sec) { + return &Sec == &Section || RemovePred(Sec); + }; + } +} + // 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 +653,12 @@ }; } + if (Config.CompressDebugSections != DebugCompressionType::None) { + compressSections(Config, Obj, RemovePred, OutputElfType); + } else if (Config.DecompressDebugSections) { + decompressSections(Config, Obj, RemovePred, OutputElfType); + } + Obj.removeSections(RemovePred); if (!Config.SectionsToRename.empty()) { @@ -686,6 +828,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("Invalid 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 +879,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 +892,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; }