Skip to content

Commit e8fafaf

Browse files
committed
[ELF] Replace rawData+size with content_+size+compressedSize
compressedSize resides in an existing union. sizeof(InputSection) decreases from 160 to 152 on ELF64 systems.
1 parent 2bf5d86 commit e8fafaf

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

lld/ELF/Arch/RISCV.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,8 @@ void elf::riscvFinalizeRelax(int passes) {
735735
uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize);
736736
uint64_t offset = 0;
737737
int64_t delta = 0;
738-
sec->rawData = makeArrayRef(p, newSize);
738+
sec->content_ = p;
739+
sec->size = newSize;
739740
sec->bytesDropped = 0;
740741

741742
// Update section content: remove NOPs for R_RISCV_ALIGN and rewrite

lld/ELF/InputSection.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
5555
StringRef name, Kind sectionKind)
5656
: SectionBase(sectionKind, name, flags, entsize, alignment, type, info,
5757
link),
58-
file(file), rawData(data) {
58+
file(file), content_(data.data()), size(data.size()) {
5959
// In order to reduce memory allocation, we assume that mergeable
6060
// sections are smaller than 4 GiB, which is not an unreasonable
6161
// assumption as of 2017.
@@ -111,9 +111,9 @@ size_t InputSectionBase::getSize() const {
111111
template <class ELFT>
112112
static void decompressAux(const InputSectionBase &sec, uint8_t *out,
113113
size_t size) {
114-
auto *hdr =
115-
reinterpret_cast<const typename ELFT::Chdr *>(sec.content().data());
116-
auto compressed = sec.content().slice(sizeof(typename ELFT::Chdr));
114+
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.content_);
115+
auto compressed = ArrayRef<uint8_t>(sec.content_, sec.compressedSize)
116+
.slice(sizeof(typename ELFT::Chdr));
117117
if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
118118
? compression::zlib::decompress(compressed, out, size)
119119
: compression::zstd::decompress(compressed, out, size))
@@ -130,7 +130,7 @@ void InputSectionBase::decompress() const {
130130
}
131131

132132
invokeELFT(decompressAux, *this, uncompressedBuf, size);
133-
rawData = makeArrayRef(uncompressedBuf, size);
133+
content_ = uncompressedBuf;
134134
compressed = false;
135135
}
136136

@@ -231,6 +231,7 @@ template <typename ELFT> void InputSectionBase::parseCompressedHeader() {
231231
}
232232

233233
compressed = true;
234+
compressedSize = size;
234235
size = hdr->ch_size;
235236
alignment = std::max<uint32_t>(hdr->ch_addralign, 1);
236237
}
@@ -1104,8 +1105,9 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
11041105
// If this is a compressed section, uncompress section contents directly
11051106
// to the buffer.
11061107
if (compressed) {
1107-
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data());
1108-
auto compressed = content().slice(sizeof(typename ELFT::Chdr));
1108+
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content_);
1109+
auto compressed = ArrayRef<uint8_t>(content_, compressedSize)
1110+
.slice(sizeof(typename ELFT::Chdr));
11091111
size_t size = this->size;
11101112
if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
11111113
? compression::zlib::decompress(compressed, buf, size)

lld/ELF/InputSection.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,23 @@ class InputSectionBase : public SectionBase {
155155
bytesDropped -= num;
156156
}
157157

158-
mutable ArrayRef<uint8_t> rawData;
158+
mutable const uint8_t *content_;
159+
uint64_t size;
159160

160161
void trim() {
161162
if (bytesDropped) {
162-
rawData = rawData.drop_back(bytesDropped);
163+
size -= bytesDropped;
163164
bytesDropped = 0;
164165
}
165166
}
166167

167-
ArrayRef<uint8_t> content() const { return rawData; }
168+
ArrayRef<uint8_t> content() const {
169+
return ArrayRef<uint8_t>(content_, size);
170+
}
168171
ArrayRef<uint8_t> contentMaybeDecompress() const {
169172
if (compressed)
170173
decompress();
171-
return rawData;
174+
return content();
172175
}
173176

174177
// The next member in the section group if this section is in a group. This is
@@ -217,6 +220,9 @@ class InputSectionBase : public SectionBase {
217220
// Auxiliary information for RISC-V linker relaxation. RISC-V does not use
218221
// jumpInstrMod.
219222
RISCVRelaxAux *relaxAux;
223+
224+
// The compressed content size when `compressed` is true.
225+
size_t compressedSize;
220226
};
221227

222228
// A function compiled with -fsplit-stack calling a function
@@ -238,12 +244,6 @@ class InputSectionBase : public SectionBase {
238244
template <typename ELFT>
239245
void parseCompressedHeader();
240246
void decompress() const;
241-
242-
// This field stores the uncompressed size of the compressed data in rawData,
243-
// or -1 if rawData is not compressed (either because the section wasn't
244-
// compressed in the first place, or because we ended up uncompressing it).
245-
// Since the feature is not used often, this is usually -1.
246-
mutable int64_t size = -1;
247247
};
248248

249249
// SectionPiece represents a piece of splittable section contents.
@@ -397,7 +397,7 @@ class InputSection : public InputSectionBase {
397397
template <class ELFT> void copyShtGroup(uint8_t *buf);
398398
};
399399

400-
static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
400+
static_assert(sizeof(InputSection) <= 152, "InputSection is too big");
401401

402402
class SyntheticSection : public InputSection {
403403
public:

0 commit comments

Comments
 (0)