[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.
This commit is contained in:
Fangrui Song 2022-11-20 21:56:13 +00:00
parent 5fef791cb9
commit 53b674ee15
2 changed files with 11 additions and 9 deletions

View File

@ -103,8 +103,8 @@ InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
size_t InputSectionBase::getSize() const {
if (auto *s = dyn_cast<SyntheticSection>(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 <class ELFT> RelsOrRelas<ELFT> InputSectionBase::relsOrRelas() const {
@ -230,7 +229,8 @@ template <typename ELFT> void InputSectionBase::parseCompressedHeader() {
return;
}
uncompressedSize = hdr->ch_size;
compressed = true;
size = hdr->ch_size;
alignment = std::max<uint32_t>(hdr->ch_addralign, 1);
}
@ -1102,10 +1102,10 @@ template <class ELFT> 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<const typename ELFT::Chdr *>(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))

View File

@ -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<uint8_t> 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.