[lld] 53b674e - [ELF] InputSectionBase: add bool compressed to avoid overloading size with compressed semantics

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 20 13:56:19 PST 2022


Author: Fangrui Song
Date: 2022-11-20T21:56:13Z
New Revision: 53b674ee159ae0dd9f5e7492e1c0af45d2305e73

URL: https://github.com/llvm/llvm-project/commit/53b674ee159ae0dd9f5e7492e1c0af45d2305e73
DIFF: https://github.com/llvm/llvm-project/commit/53b674ee159ae0dd9f5e7492e1c0af45d2305e73.diff

LOG: [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.

Added: 
    

Modified: 
    lld/ELF/InputSection.cpp
    lld/ELF/InputSection.h

Removed: 
    


################################################################################
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<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))

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 @@ class InputSectionBase : public SectionBase {
   // 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 @@ class InputSectionBase : public SectionBase {
   }
 
   ArrayRef<uint8_t> data() const {
-    if (uncompressedSize >= 0)
+    if (compressed)
       decompress();
     return rawData;
   }
@@ -240,7 +242,7 @@ class InputSectionBase : public SectionBase {
   // 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.


        


More information about the llvm-commits mailing list