forked from OSchip/llvm-project
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:
parent
4627873807
commit
066c4ab9fe
|
@ -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 <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;
|
||||
}
|
||||
|
||||
// Compress section contents if this section contains debug info.
|
||||
template <class ELFT> void OutputSection::maybeCompress() {
|
||||
// If -compress-debug-sections is specified, we compress output debug
|
||||
// sections.
|
||||
if (!Config->CompressDebugSections || !Name.startswith(".debug_") ||
|
||||
(Flags & SHF_ALLOC))
|
||||
typedef typename ELFT::Chdr Elf_Chdr;
|
||||
|
||||
// Compress only DWARF debug sections.
|
||||
if (!Config->CompressDebugSections || !(Flags & SHF_ALLOC) ||
|
||||
!Name.startswith(".debug_"))
|
||||
return;
|
||||
|
||||
this->Flags |= SHF_COMPRESSED;
|
||||
CompressedHeader = createHeader<ELFT>(this->Size, this->Alignment);
|
||||
// Create a section header.
|
||||
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.
|
||||
std::vector<uint8_t> Data(this->Size);
|
||||
this->writeTo<ELFT>(&Data[0]);
|
||||
|
||||
if (Error E = zlib::compress(StringRef((char *)Data.data(), Data.size()),
|
||||
CompressedData))
|
||||
// Write section contents to a temporary buffer and compress it.
|
||||
std::vector<uint8_t> Buf(Size);
|
||||
writeTo<ELFT>(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 <class ELFT> void OutputSection::finalize() {
|
||||
|
|
Loading…
Reference in New Issue