From 53b674ee159ae0dd9f5e7492e1c0af45d2305e73 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 20 Nov 2022 21:56:13 +0000 Subject: [PATCH] [ELF] InputSectionBase: add bool compressed to avoid overloading size with compressed semantics Rename uncompressedSize to size, to prepare for shrinking rawData+size from 3 words to 2 words. --- lld/ELF/InputSection.cpp | 14 +++++++------- lld/ELF/InputSection.h | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 53daefb7a9e5..5c483c4f67e2 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -103,8 +103,8 @@ InputSectionBase::InputSectionBase(ObjFile &file, size_t InputSectionBase::getSize() const { if (auto *s = dyn_cast(this)) return s->getSize(); - if (uncompressedSize >= 0) - return uncompressedSize; + if (compressed) + return size; return rawData.size() - bytesDropped; } @@ -121,7 +121,6 @@ static void decompressAux(const InputSectionBase &sec, uint8_t *out, } void InputSectionBase::decompress() const { - size_t size = uncompressedSize; uint8_t *uncompressedBuf; { static std::mutex mu; @@ -131,7 +130,7 @@ void InputSectionBase::decompress() const { invokeELFT(decompressAux, *this, uncompressedBuf, size); rawData = makeArrayRef(uncompressedBuf, size); - uncompressedSize = -1; + compressed = false; } template RelsOrRelas InputSectionBase::relsOrRelas() const { @@ -230,7 +229,8 @@ template void InputSectionBase::parseCompressedHeader() { return; } - uncompressedSize = hdr->ch_size; + compressed = true; + size = hdr->ch_size; alignment = std::max(hdr->ch_addralign, 1); } @@ -1102,10 +1102,10 @@ template void InputSection::writeTo(uint8_t *buf) { // If this is a compressed section, uncompress section contents directly // to the buffer. - if (uncompressedSize >= 0) { + if (compressed) { auto *hdr = reinterpret_cast(rawData.data()); auto compressed = rawData.slice(sizeof(typename ELFT::Chdr)); - size_t size = uncompressedSize; + size_t size = this->size; if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB ? compression::zlib::decompress(compressed, buf, size) : compression::zstd::decompress(compressed, buf, size)) diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index c4caa01265b5..adbdefbf5eb1 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -139,6 +139,8 @@ public: // be reset to zero after uses. uint16_t bytesDropped = 0; + mutable bool compressed = false; + // Whether the section needs to be padded with a NOP filler due to // deleteFallThruJmpInsn. bool nopFiller = false; @@ -163,7 +165,7 @@ public: } ArrayRef data() const { - if (uncompressedSize >= 0) + if (compressed) decompress(); return rawData; } @@ -240,7 +242,7 @@ protected: // or -1 if rawData is not compressed (either because the section wasn't // compressed in the first place, or because we ended up uncompressing it). // Since the feature is not used often, this is usually -1. - mutable int64_t uncompressedSize = -1; + mutable int64_t size = -1; }; // SectionPiece represents a piece of splittable section contents.