From 08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Wed, 27 Jul 2022 21:50:45 +0200 Subject: Vendor import of llvm-project main llvmorg-15-init-17826-g1f8ae9d7e7e4, the last commit before the upstream release/16.x branch was created. --- llvm/lib/ObjCopy/ELF/ELFObject.cpp | 52 ++++++++------------------------------ 1 file changed, 10 insertions(+), 42 deletions(-) (limited to 'llvm/lib/ObjCopy/ELF/ELFObject.cpp') diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp index 8b44c09023f1..b127e1b43b8e 100644 --- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp +++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp @@ -434,41 +434,13 @@ Error SectionWriter::visit(const OwnedDataSection &Sec) { return Error::success(); } -static constexpr std::array 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(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 Error ELFSectionWriter::visit(const DecompressedSection &Sec) { - const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData) - ? (ZlibGnuMagic.size() + sizeof(Sec.Size)) - : sizeof(Elf_Chdr_Impl); - - ArrayRef CompressedContent(Sec.OriginalData.data() + DataOffset, - Sec.OriginalData.size() - DataOffset); + ArrayRef Compressed = + Sec.OriginalData.slice(sizeof(Elf_Chdr_Impl)); SmallVector DecompressedContent; - if (Error Err = - compression::zlib::uncompress(CompressedContent, DecompressedContent, - static_cast(Sec.Size))) + if (Error Err = compression::zlib::uncompress(Compressed, DecompressedContent, + static_cast(Sec.Size))) return createStringError(errc::invalid_argument, "'" + Sec.Name + "': " + toString(std::move(Err))); @@ -518,7 +490,7 @@ Error BinarySectionWriter::visit(const CompressedSection &Sec) { template Error ELFSectionWriter::visit(const CompressedSection &Sec) { uint8_t *Buf = reinterpret_cast(Out.getBufferStart()) + Sec.Offset; - Elf_Chdr_Impl Chdr; + Elf_Chdr_Impl Chdr = {}; switch (Sec.CompressionType) { case DebugCompressionType::None: std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf); @@ -1731,15 +1703,11 @@ Expected ELFBuilder::makeSection(const Elf_Shdr &Shdr) { if (!Name) return Name.takeError(); - if (Name->startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) { - uint64_t DecompressedSize, DecompressedAlign; - std::tie(DecompressedSize, DecompressedAlign) = - getDecompressedSizeAndAlignment(*Data); - return Obj.addSection( - CompressedSection(*Data, DecompressedSize, DecompressedAlign)); - } - - return Obj.addSection
(*Data); + if (!(Shdr.sh_flags & ELF::SHF_COMPRESSED)) + return Obj.addSection
(*Data); + auto *Chdr = reinterpret_cast *>(Data->data()); + return Obj.addSection( + CompressedSection(*Data, Chdr->ch_size, Chdr->ch_addralign)); } } } -- cgit v1.3