[MC] Delete dead zlib-gnu code and simplify writeSectionData

This commit is contained in:
Fangrui Song 2022-07-24 01:17:34 -07:00
parent 2eba08fd9a
commit 6977ff4006
3 changed files with 16 additions and 27 deletions

View File

@ -27,7 +27,6 @@ enum class ExceptionHandling {
enum class DebugCompressionType {
None, ///< No compression
GNU, ///< zlib-gnu style compression
Z, ///< zlib style complession
};

View File

@ -144,7 +144,7 @@ struct ELFWriter {
uint64_t align(unsigned Alignment);
bool maybeWriteCompression(uint64_t Size,
bool maybeWriteCompression(uint32_t ChType, uint64_t Size,
SmallVectorImpl<uint8_t> &CompressedContents,
unsigned Alignment);
@ -819,8 +819,8 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
// Include the debug info compression header.
bool ELFWriter::maybeWriteCompression(
uint64_t Size, SmallVectorImpl<uint8_t> &CompressedContents,
unsigned Alignment) {
uint32_t ChType, uint64_t Size,
SmallVectorImpl<uint8_t> &CompressedContents, unsigned Alignment) {
uint64_t HdrSize =
is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
if (Size <= HdrSize + CompressedContents.size())
@ -828,13 +828,13 @@ bool ELFWriter::maybeWriteCompression(
// Platform specific header is followed by compressed data.
if (is64Bit()) {
// Write Elf64_Chdr header.
write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB));
write(static_cast<ELF::Elf64_Word>(ChType));
write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
write(static_cast<ELF::Elf64_Xword>(Size));
write(static_cast<ELF::Elf64_Xword>(Alignment));
} else {
// Write Elf32_Chdr header otherwise.
write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB));
write(static_cast<ELF::Elf32_Word>(ChType));
write(static_cast<ELF::Elf32_Word>(Size));
write(static_cast<ELF::Elf32_Word>(Alignment));
}
@ -856,38 +856,31 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
return;
}
assert((MAI->compressDebugSections() == DebugCompressionType::Z ||
MAI->compressDebugSections() == DebugCompressionType::GNU) &&
"expected zlib or zlib-gnu style compression");
assert(MAI->compressDebugSections() == DebugCompressionType::Z &&
"expected zlib style compression");
SmallVector<char, 128> UncompressedData;
raw_svector_ostream VecOS(UncompressedData);
Asm.writeSectionData(VecOS, &Section, Layout);
SmallVector<uint8_t, 128> CompressedContents;
SmallVector<uint8_t, 128> Compressed;
const uint32_t ChType = ELF::ELFCOMPRESS_ZLIB;
compression::zlib::compress(
makeArrayRef(reinterpret_cast<uint8_t *>(UncompressedData.data()),
UncompressedData.size()),
CompressedContents);
Compressed);
bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
if (!maybeWriteCompression(ChType, UncompressedData.size(), Compressed,
Sec.getAlignment())) {
W.OS << UncompressedData;
return;
}
if (ZlibStyle) {
// Set the compressed flag. That is zlib style.
Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
// Alignment field should reflect the requirements of
// the compressed section header.
Section.setAlignment(is64Bit() ? Align(8) : Align(4));
} else {
// Add "z" prefix to section name. This is zlib-gnu style.
MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
}
W.OS << toStringRef(CompressedContents);
Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
// Alignment field should reflect the requirements of
// the compressed section header.
Section.setAlignment(is64Bit() ? Align(8) : Align(4));
W.OS << toStringRef(Compressed);
}
void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,

View File

@ -523,9 +523,6 @@ Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
case DebugCompressionType::None:
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
return Error::success();
case DebugCompressionType::GNU:
llvm_unreachable("unexpected zlib-gnu");
break;
case DebugCompressionType::Z:
Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
break;