From 066c4ab9fee4b987f01c4131d8a7502455626e8d Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 19 Apr 2017 11:31:58 +0000 Subject: [PATCH] Simplify createHeader and inline it. createHeader didn't use data members of Elf_Chdr type and write directly to a given buffer. That is not a good practice because the function had a knowledge of the struct layout. llvm-svn: 300674 --- lld/ELF/OutputSections.cpp | 62 +++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 9fc6969..d7a1ef5 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -84,53 +84,31 @@ static bool compareByFilePosition(InputSection *A, InputSection *B) { return LA->OutSecOff < LB->OutSecOff; } -// Compressed sections has header which we create in this function. -// Format is explaned here: -// https://docs.oracle.com/cd/E53394_01/html/E54813/section_compression.html -template -static std::vector createHeader(size_t Size, uint32_t Alignment) { - const endianness E = ELFT::TargetEndianness; - - std::vector Ret(sizeof(typename ELFT::Chdr)); - uint8_t *Buf = &Ret[0]; - write32(Buf, ELFCOMPRESS_ZLIB); - Buf += 4; - - if (Config->Is64) { - Buf += sizeof(Elf64_Word); // Skip ch_reserved field. - write64(Buf, Size); - Buf += sizeof(ELFT::Chdr::ch_size); - write64(Buf, Alignment); - Buf += sizeof(ELFT::Chdr::ch_addralign); - } else { - write32(Buf, Size); - Buf += sizeof(ELFT::Chdr::ch_size); - write32(Buf, Alignment); - Buf += sizeof(ELFT::Chdr::ch_addralign); - } - - return Ret; -} - +// Compress section contents if this section contains debug info. template void OutputSection::maybeCompress() { - // If -compress-debug-sections is specified, we compress output debug - // sections. - if (!Config->CompressDebugSections || !Name.startswith(".debug_") || - (Flags & SHF_ALLOC)) - return; + typedef typename ELFT::Chdr Elf_Chdr; - this->Flags |= SHF_COMPRESSED; - CompressedHeader = createHeader(this->Size, this->Alignment); - - // Here we write relocated content of sections and compress it. - std::vector Data(this->Size); - this->writeTo(&Data[0]); + // Compress only DWARF debug sections. + if (!Config->CompressDebugSections || !(Flags & SHF_ALLOC) || + !Name.startswith(".debug_")) + return; - if (Error E = zlib::compress(StringRef((char *)Data.data(), Data.size()), - CompressedData)) + // Create a section header. + CompressedHeader.resize(sizeof(Elf_Chdr)); + auto *Hdr = reinterpret_cast(CompressedHeader.data()); + Hdr->ch_type = ELFCOMPRESS_ZLIB; + Hdr->ch_size = Size; + Hdr->ch_addralign = Alignment; + + // Write section contents to a temporary buffer and compress it. + std::vector Buf(Size); + writeTo(Buf.data()); + if (Error E = zlib::compress(toStringRef(Buf), CompressedData)) fatal("compress failed: " + llvm::toString(std::move(E))); - this->Size = this->CompressedHeader.size() + this->CompressedData.size(); + // Update section headers. + Size = sizeof(Elf_Chdr) + CompressedData.size(); + Flags |= SHF_COMPRESSED; } template void OutputSection::finalize() { -- 2.7.4