diff options
Diffstat (limited to 'llvm/tools')
-rw-r--r-- | llvm/tools/llvm-objcopy/ObjcopyOpts.td | 6 | ||||
-rw-r--r-- | llvm/tools/llvm-objcopy/Object.cpp | 61 | ||||
-rw-r--r-- | llvm/tools/llvm-objcopy/Object.h | 27 | ||||
-rw-r--r-- | llvm/tools/llvm-objcopy/llvm-objcopy.cpp | 73 |
4 files changed, 6 insertions, 161 deletions
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td index e56dc52758c..c34494736c3 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td +++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td @@ -17,12 +17,6 @@ def I : JoinedOrSeparate<[ "-" ], "I">, Alias<input_target>; defm output_target : Eq<"output-target">, HelpText<"Format of the output file">, Values<"binary">; -def compress_debug_sections : Flag<["--", "-"], "compress-debug-sections">; -def compress_debug_sections_eq : Joined<["--", "-"], "compress-debug-sections=">, - MetaVarName<"[ zlib | zlib-gnu ]">, - HelpText<"Compress DWARF debug sections using " - "specified style. Supported styles: " - "'zlib-gnu' and 'zlib'">; def O : JoinedOrSeparate<["-"], "O">, Alias<output_target>; defm split_dwo : Eq<"split-dwo">, diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp index 3893c0e2c53..12fd80228bf 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -15,9 +15,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/ADT/iterator_range.h" #include "llvm/BinaryFormat/ELF.h" -#include "llvm/MC/MCTargetOptions.h" #include "llvm/Object/ELFObjectFile.h" -#include "llvm/Support/Compression.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/Path.h" @@ -140,65 +138,6 @@ void OwnedDataSection::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } -void BinarySectionWriter::visit(const CompressedSection &Sec) { - error("Cannot write compressed section '" + Sec.Name + "' "); -} - -template <class ELFT> -void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) { - uint8_t *Buf = Out.getBufferStart(); - Buf += Sec.Offset; - - if (Sec.CompressionType == DebugCompressionType::GNU) { - ArrayRef<uint8_t> Magic = {'Z', 'L', 'I', 'B'}; - std::copy(Magic.begin(), Magic.end(), Buf); - Buf += Magic.size(); - const uint64_t DecompressedSize = - support::endian::read64be(&Sec.DecompressedSize); - memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize)); - Buf += sizeof(DecompressedSize); - } else { - auto Chdr = reinterpret_cast<Elf_Chdr_Impl<ELFT> *>(Buf); - Chdr->ch_type = ELF::ELFCOMPRESS_ZLIB; - Chdr->ch_size = Sec.DecompressedSize; - Chdr->ch_addralign = Sec.DecompressedAlign; - Buf += sizeof(*Chdr); - } - - std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf); -} - -CompressedSection::CompressedSection(const SectionBase &Sec, - DebugCompressionType CompressionType) - : SectionBase(Sec), CompressionType(CompressionType), - DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) { - - if (Error E = zlib::compress( - StringRef(reinterpret_cast<const char *>(OriginalData.data()), - OriginalData.size()), - CompressedData)) - reportError(Name, std::move(E)); - - size_t ChdrSize; - if (CompressionType == DebugCompressionType::GNU) { - Name = ".z" + Sec.Name.substr(1); - ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t); - } else { - Flags |= ELF::SHF_COMPRESSED; - ChdrSize = - std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>), - sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)), - std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>), - sizeof(object::Elf_Chdr_Impl<object::ELF32BE>))); - } - Size = ChdrSize + CompressedData.size(); - Align = 8; -} - -void CompressedSection::accept(SectionVisitor &Visitor) const { - Visitor.visit(*this); -} - void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); Size = StrTabBuilder.getSize(); diff --git a/llvm/tools/llvm-objcopy/Object.h b/llvm/tools/llvm-objcopy/Object.h index 42b2733dc1a..e9a4c35d398 100644 --- a/llvm/tools/llvm-objcopy/Object.h +++ b/llvm/tools/llvm-objcopy/Object.h @@ -26,7 +26,6 @@ #include <vector> namespace llvm { -enum class DebugCompressionType; namespace objcopy { class Buffer; @@ -40,7 +39,6 @@ class DynamicRelocationSection; class GnuDebugLinkSection; class GroupSection; class SectionIndexSection; -class CompressedSection; class Segment; class Object; struct Symbol; @@ -88,7 +86,6 @@ public: 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 { @@ -107,7 +104,6 @@ public: 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) {} }; @@ -126,7 +122,6 @@ public: 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) {} }; @@ -144,7 +139,6 @@ public: 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) {} }; @@ -252,7 +246,7 @@ public: class SectionBase { public: - std::string Name; + StringRef Name; Segment *ParentSegment = nullptr; uint64_t HeaderOffset; uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max(); @@ -271,9 +265,6 @@ public: uint64_t Type = ELF::SHT_NULL; ArrayRef<uint8_t> OriginalData; - SectionBase() = default; - SectionBase(const SectionBase &) = default; - virtual ~SectionBase() = default; virtual void initialize(SectionTableRef SecTable); @@ -350,7 +341,7 @@ class OwnedDataSection : public SectionBase { public: OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data) : Data(std::begin(Data), std::end(Data)) { - Name = SecName.str(); + Name = SecName; Type = ELF::SHT_PROGBITS; Size = Data.size(); OriginalOffset = std::numeric_limits<uint64_t>::max(); @@ -359,20 +350,6 @@ public: void accept(SectionVisitor &Sec) const override; }; -class CompressedSection : public SectionBase { - MAKE_SEC_WRITER_FRIEND - - DebugCompressionType CompressionType; - uint64_t DecompressedSize; - uint64_t DecompressedAlign; - SmallVector<char, 128> CompressedData; - -public: - CompressedSection(const SectionBase &Sec, - DebugCompressionType CompressionType); - void accept(SectionVisitor &Visitor) const override; -}; - // There are two types of string tables that can exist, dynamic and not dynamic. // In the dynamic case the string table is allocated. Changing a dynamic string // table would mean altering virtual addresses and thus the memory image. So diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index f0d6b4416cf..72501f3eb98 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -17,7 +17,6 @@ #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" @@ -176,7 +175,6 @@ struct CopyConfig { bool StripSections = false; bool StripUnneeded = false; bool Weaken = false; - DebugCompressionType CompressionType = DebugCompressionType::None; }; using SectionPred = std::function<bool(const SectionBase &Sec)>; @@ -292,12 +290,12 @@ static SectionRename parseRenameSectionValue(StringRef FlagValue) { } static bool isDebugSection(const SectionBase &Sec) { - return StringRef(Sec.Name).startswith(".debug") || - StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index"; + return Sec.Name.startswith(".debug") || Sec.Name.startswith(".zdebug") || + Sec.Name == ".gdb_index"; } static bool isDWOSection(const SectionBase &Sec) { - return StringRef(Sec.Name).endswith(".dwo"); + return Sec.Name.endswith(".dwo"); } static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { @@ -407,49 +405,6 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename, object_error::parse_failed); } -static bool isCompressed(const SectionBase &Section) { - ArrayRef<uint8_t> GnuPrefix = {'Z', 'L', 'I', 'B'}; - return StringRef(Section.Name).startswith(".zdebug") || - (Section.OriginalData.size() > strlen("ZLIB") && - std::equal(GnuPrefix.begin(), GnuPrefix.end(), - Section.OriginalData.data())) || - (Section.Flags & ELF::SHF_COMPRESSED); -} - -static bool isCompressable(const SectionBase &Section) { - return !isCompressed(Section) && isDebugSection(Section) && - Section.Name != ".gdb_index"; -} - -static void compressSections(const CopyConfig &Config, Object &Obj, - SectionPred &RemovePred) { - SmallVector<SectionBase *, 13> ToCompress; - SmallVector<RelocationSection *, 13> RelocationSections; - for (auto &Sec : Obj.sections()) { - if (RelocationSection *R = dyn_cast<RelocationSection>(&Sec)) { - if (isCompressable(*R->getSection())) - RelocationSections.push_back(R); - continue; - } - - if (isCompressable(Sec)) - ToCompress.push_back(&Sec); - } - - for (SectionBase *S : ToCompress) { - CompressedSection &CS = - Obj.addSection<CompressedSection>(*S, Config.CompressionType); - - for (RelocationSection *RS : RelocationSections) { - if (RS->getSection() == S) - RS->setSection(&CS); - } - } - - RemovePred = [RemovePred](const SectionBase &Sec) { - return isCompressable(Sec) || 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" @@ -609,7 +564,7 @@ static void handleArgs(const CopyConfig &Config, Object &Obj, return true; if (&Sec == Obj.SectionNames) return false; - if (StringRef(Sec.Name).startswith(".gnu.warning")) + if (Sec.Name.startswith(".gnu.warning")) return false; return (Sec.Flags & SHF_ALLOC) == 0; }; @@ -661,9 +616,6 @@ static void handleArgs(const CopyConfig &Config, Object &Obj, }; } - if (Config.CompressionType != DebugCompressionType::None) - compressSections(Config, Obj, RemovePred); - Obj.removeSections(RemovePred); if (!Config.SectionsToRename.empty()) { @@ -908,23 +860,6 @@ static CopyConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) { Config.BinaryArch = getMachineInfo(BinaryArch); } - if (auto Arg = InputArgs.getLastArg(OBJCOPY_compress_debug_sections, - OBJCOPY_compress_debug_sections_eq)) { - Config.CompressionType = DebugCompressionType::Z; - - if (Arg->getOption().getID() == OBJCOPY_compress_debug_sections_eq) { - Config.CompressionType = - StringSwitch<DebugCompressionType>( - InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)) - .Case("zlib-gnu", DebugCompressionType::GNU) - .Case("zlib", DebugCompressionType::Z) - .Default(DebugCompressionType::None); - if (Config.CompressionType == DebugCompressionType::None) - error("Invalid or unsupported --compress-debug-sections format: " + - InputArgs.getLastArgValue(OBJCOPY_compress_debug_sections_eq)); - } - } - Config.SplitDWO = InputArgs.getLastArgValue(OBJCOPY_split_dwo); Config.AddGnuDebugLink = InputArgs.getLastArgValue(OBJCOPY_add_gnu_debuglink); Config.SymbolsPrefix = InputArgs.getLastArgValue(OBJCOPY_prefix_symbols); |