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
This commit is contained in:
Rui Ueyama 2017-04-19 11:31:58 +00:00
parent 4627873807
commit 066c4ab9fe
1 changed files with 19 additions and 41 deletions

View File

@ -84,53 +84,31 @@ static bool compareByFilePosition(InputSection *A, InputSection *B) {
return LA->OutSecOff < LB->OutSecOff; return LA->OutSecOff < LB->OutSecOff;
} }
// Compressed sections has header which we create in this function. // Compress section contents if this section contains debug info.
// Format is explaned here:
// https://docs.oracle.com/cd/E53394_01/html/E54813/section_compression.html
template <class ELFT>
static std::vector<uint8_t> createHeader(size_t Size, uint32_t Alignment) {
const endianness E = ELFT::TargetEndianness;
std::vector<uint8_t> Ret(sizeof(typename ELFT::Chdr));
uint8_t *Buf = &Ret[0];
write32<E>(Buf, ELFCOMPRESS_ZLIB);
Buf += 4;
if (Config->Is64) {
Buf += sizeof(Elf64_Word); // Skip ch_reserved field.
write64<E>(Buf, Size);
Buf += sizeof(ELFT::Chdr::ch_size);
write64<E>(Buf, Alignment);
Buf += sizeof(ELFT::Chdr::ch_addralign);
} else {
write32<E>(Buf, Size);
Buf += sizeof(ELFT::Chdr::ch_size);
write32<E>(Buf, Alignment);
Buf += sizeof(ELFT::Chdr::ch_addralign);
}
return Ret;
}
template <class ELFT> void OutputSection::maybeCompress() { template <class ELFT> void OutputSection::maybeCompress() {
// If -compress-debug-sections is specified, we compress output debug typedef typename ELFT::Chdr Elf_Chdr;
// sections.
if (!Config->CompressDebugSections || !Name.startswith(".debug_") || // Compress only DWARF debug sections.
(Flags & SHF_ALLOC)) if (!Config->CompressDebugSections || !(Flags & SHF_ALLOC) ||
!Name.startswith(".debug_"))
return; return;
this->Flags |= SHF_COMPRESSED; // Create a section header.
CompressedHeader = createHeader<ELFT>(this->Size, this->Alignment); CompressedHeader.resize(sizeof(Elf_Chdr));
auto *Hdr = reinterpret_cast<Elf_Chdr *>(CompressedHeader.data());
Hdr->ch_type = ELFCOMPRESS_ZLIB;
Hdr->ch_size = Size;
Hdr->ch_addralign = Alignment;
// Here we write relocated content of sections and compress it. // Write section contents to a temporary buffer and compress it.
std::vector<uint8_t> Data(this->Size); std::vector<uint8_t> Buf(Size);
this->writeTo<ELFT>(&Data[0]); writeTo<ELFT>(Buf.data());
if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
if (Error E = zlib::compress(StringRef((char *)Data.data(), Data.size()),
CompressedData))
fatal("compress failed: " + llvm::toString(std::move(E))); 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 <class ELFT> void OutputSection::finalize() { template <class ELFT> void OutputSection::finalize() {