Skip to content

Commit 2bf5d86

Browse files
committed
[ELF] Change rawData to content() and data() to contentMaybeDecompress()
Clarify data() which may trigger decompression and make it feasible to refactor the member variable rawData.
1 parent 30f9eb1 commit 2bf5d86

17 files changed

+70
-66
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,
350350
}
351351

352352
uint64_t patchOff = 0;
353-
const uint8_t *buf = isec->rawData.begin();
353+
const uint8_t *buf = isec->content().begin();
354354
const ulittle32_t *instBuf = reinterpret_cast<const ulittle32_t *>(buf + off);
355355
uint32_t instr1 = *instBuf++;
356356
uint32_t instr2 = *instBuf++;
@@ -409,7 +409,7 @@ uint64_t Patch843419Section::getLDSTAddr() const {
409409
void Patch843419Section::writeTo(uint8_t *buf) {
410410
// Copy the instruction that we will be replacing with a branch in the
411411
// patchee Section.
412-
write32le(buf, read32le(patchee->rawData.begin() + patcheeOffset));
412+
write32le(buf, read32le(patchee->content().begin() + patcheeOffset));
413413

414414
// Apply any relocation transferred from the original patchee section.
415415
target->relocateAlloc(*this, buf);
@@ -591,8 +591,8 @@ AArch64Err843419Patcher::patchInputSectionDescription(
591591
while (codeSym != mapSyms.end()) {
592592
auto dataSym = std::next(codeSym);
593593
uint64_t off = (*codeSym)->value;
594-
uint64_t limit =
595-
(dataSym == mapSyms.end()) ? isec->rawData.size() : (*dataSym)->value;
594+
uint64_t limit = (dataSym == mapSyms.end()) ? isec->content().size()
595+
: (*dataSym)->value;
596596

597597
while (off < limit) {
598598
uint64_t startAddr = isec->getVA(off);

lld/ELF/ARMErrataFix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off,
266266
}
267267

268268
ScanResult scanRes = {0, 0, nullptr};
269-
const uint8_t *buf = isec->rawData.begin();
269+
const uint8_t *buf = isec->content().begin();
270270
// ARMv7-A Thumb 32-bit instructions are encoded 2 consecutive
271271
// little-endian halfwords.
272272
const ulittle16_t *instBuf = reinterpret_cast<const ulittle16_t *>(buf + off);
@@ -498,8 +498,8 @@ ARMErr657417Patcher::patchInputSectionDescription(
498498
while (thumbSym != mapSyms.end()) {
499499
auto nonThumbSym = std::next(thumbSym);
500500
uint64_t off = (*thumbSym)->value;
501-
uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->rawData.size()
502-
: (*nonThumbSym)->value;
501+
uint64_t limit = nonThumbSym == mapSyms.end() ? isec->content().size()
502+
: (*nonThumbSym)->value;
503503

504504
while (off < limit) {
505505
ScanResult sr = scanCortexA8Errata657417(isec, off, limit);

lld/ELF/Arch/PPC64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ void PPC64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
15611561
// recursive calls even if the function is preemptible. This is not
15621562
// wrong in the common case where the function is not preempted at
15631563
// runtime. Just ignore.
1564-
if ((rel.offset + 8 > sec.rawData.size() ||
1564+
if ((rel.offset + 8 > sec.content().size() ||
15651565
read32(loc + 4) != 0x60000000) &&
15661566
rel.sym->file != sec.file) {
15671567
// Use substr(6) to remove the "__plt_" prefix.

lld/ELF/Arch/RISCV.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ static void relaxCall(const InputSection &sec, size_t i, uint64_t loc,
556556
Relocation &r, uint32_t &remove) {
557557
const bool rvc = config->eflags & EF_RISCV_RVC;
558558
const Symbol &sym = *r.sym;
559-
const uint64_t insnPair = read64le(sec.rawData.data() + r.offset);
559+
const uint64_t insnPair = read64le(sec.content().data() + r.offset);
560560
const uint32_t rd = extractBits(insnPair, 32 + 11, 32 + 7);
561561
const uint64_t dest =
562562
(r.expr == R_PLT_PC ? sym.getPltVA() : sym.getVA()) + r.addend;
@@ -584,7 +584,7 @@ static void relaxTlsLe(const InputSection &sec, size_t i, uint64_t loc,
584584
uint64_t val = r.sym->getVA(r.addend);
585585
if (hi20(val) != 0)
586586
return;
587-
uint32_t insn = read32le(sec.rawData.data() + r.offset);
587+
uint32_t insn = read32le(sec.content().data() + r.offset);
588588
switch (r.type) {
589589
case R_RISCV_TPREL_HI20:
590590
case R_RISCV_TPREL_ADD:
@@ -728,7 +728,7 @@ void elf::riscvFinalizeRelax(int passes) {
728728
continue;
729729

730730
auto &rels = sec->relocations;
731-
ArrayRef<uint8_t> old = sec->rawData;
731+
ArrayRef<uint8_t> old = sec->content();
732732
size_t newSize =
733733
old.size() - aux.relocDeltas[sec->relocations.size() - 1];
734734
size_t writesIdx = 0;

lld/ELF/Arch/X86_64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
253253
Relocation &r = is.relocations[rIndex];
254254

255255
// Check if the relocation corresponds to a direct jmp.
256-
const uint8_t *secContents = is.rawData.data();
256+
const uint8_t *secContents = is.content().data();
257257
// If it is not a direct jmp instruction, there is nothing to do here.
258258
if (*(secContents + r.offset - 1) != 0xe9)
259259
return false;

lld/ELF/DWARF.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
4444
.Case(".debug_str_offsets", &strOffsetsSection)
4545
.Case(".debug_line", &lineSection)
4646
.Default(nullptr)) {
47-
m->Data = toStringRef(sec->data());
47+
m->Data = toStringRef(sec->contentMaybeDecompress());
4848
m->sec = sec;
4949
continue;
5050
}
5151

5252
if (sec->name == ".debug_abbrev")
53-
abbrevSection = toStringRef(sec->data());
53+
abbrevSection = toStringRef(sec->contentMaybeDecompress());
5454
else if (sec->name == ".debug_str")
55-
strSection = toStringRef(sec->data());
55+
strSection = toStringRef(sec->contentMaybeDecompress());
5656
else if (sec->name == ".debug_line_str")
57-
lineStrSection = toStringRef(sec->data());
57+
lineStrSection = toStringRef(sec->contentMaybeDecompress());
5858
else if (sec->name == ".debug_info" &&
5959
!(objSections[i].sh_flags & ELF::SHF_GROUP)) {
6060
// In DWARF v5, -fdebug-types-section places type units in .debug_info
@@ -66,7 +66,7 @@ template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
6666
// need to perform a lightweight parsing. We drop the SHF_GROUP flag when
6767
// the InputSection was created, so we need to retrieve sh_flags from the
6868
// associated ELF section header.
69-
infoSection.Data = toStringRef(sec->data());
69+
infoSection.Data = toStringRef(sec->contentMaybeDecompress());
7070
infoSection.sec = sec;
7171
}
7272
}

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
21232123
if (!isa<Defined>(sym) || !sym->includeInDynsym())
21242124
return;
21252125

2126-
StringRef partName = reinterpret_cast<const char *>(s->rawData.data());
2126+
StringRef partName = reinterpret_cast<const char *>(s->content().data());
21272127
for (Partition &part : partitions) {
21282128
if (part.name == partName) {
21292129
sym->partition = part.getNumber();

lld/ELF/EhFrame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class EhReader {
4242
private:
4343
template <class P> void failOn(const P *loc, const Twine &msg) {
4444
fatal("corrupted .eh_frame: " + msg + "\n>>> defined in " +
45-
isec->getObjMsg((const uint8_t *)loc - isec->rawData.data()));
45+
isec->getObjMsg((const uint8_t *)loc - isec->content().data()));
4646
}
4747

4848
uint8_t readByte();

lld/ELF/ICF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, ArrayRef<RelTy> ra,
313313
template <class ELFT>
314314
bool ICF<ELFT>::equalsConstant(const InputSection *a, const InputSection *b) {
315315
if (a->flags != b->flags || a->getSize() != b->getSize() ||
316-
a->rawData != b->rawData)
316+
a->content() != b->content())
317317
return false;
318318

319319
// If two sections have different output sections, we cannot merge them.
@@ -492,7 +492,7 @@ template <class ELFT> void ICF<ELFT>::run() {
492492
// Initially, we use hash values to partition sections.
493493
parallelForEach(sections, [&](InputSection *s) {
494494
// Set MSB to 1 to avoid collisions with unique IDs.
495-
s->eqClass[0] = xxHash64(s->rawData) | (1U << 31);
495+
s->eqClass[0] = xxHash64(s->content()) | (1U << 31);
496496
});
497497

498498
// Perform 2 rounds of relocation hash propagation. 2 is an empirical value to

lld/ELF/InputFiles.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,9 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
809809
// simply handle such sections as non-mergeable ones. Degrading like this
810810
// is acceptable because section merging is optional.
811811
if (auto *ms = dyn_cast<MergeInputSection>(s)) {
812-
s = makeThreadLocal<InputSection>(ms->file, ms->flags, ms->type,
813-
ms->alignment, ms->data(), ms->name);
812+
s = makeThreadLocal<InputSection>(
813+
ms->file, ms->flags, ms->type, ms->alignment,
814+
ms->contentMaybeDecompress(), ms->name);
814815
sections[info] = s;
815816
}
816817

@@ -877,10 +878,10 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
877878
using Elf_Note = typename ELFT::Note;
878879

879880
uint32_t featuresSet = 0;
880-
ArrayRef<uint8_t> data = sec.rawData;
881+
ArrayRef<uint8_t> data = sec.content();
881882
auto reportFatal = [&](const uint8_t *place, const char *msg) {
882883
fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
883-
Twine::utohexstr(place - sec.rawData.data()) + "): " + msg);
884+
Twine::utohexstr(place - sec.content().data()) + "): " + msg);
884885
};
885886
while (!data.empty()) {
886887
// Read one NOTE record.

lld/ELF/InputSection.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
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.
62-
if (sectionKind == SectionBase::Merge && rawData.size() > UINT32_MAX)
62+
if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX)
6363
error(toString(this) + ": section too large");
6464

6565
// The ELF spec states that a value of 0 means the section has
@@ -105,14 +105,15 @@ size_t InputSectionBase::getSize() const {
105105
return s->getSize();
106106
if (compressed)
107107
return size;
108-
return rawData.size() - bytesDropped;
108+
return content().size() - bytesDropped;
109109
}
110110

111111
template <class ELFT>
112112
static void decompressAux(const InputSectionBase &sec, uint8_t *out,
113113
size_t size) {
114-
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.rawData.data());
115-
auto compressed = sec.rawData.slice(sizeof(typename ELFT::Chdr));
114+
auto *hdr =
115+
reinterpret_cast<const typename ELFT::Chdr *>(sec.content().data());
116+
auto compressed = sec.content().slice(sizeof(typename ELFT::Chdr));
116117
if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
117118
? compression::zlib::decompress(compressed, out, size)
118119
: compression::zstd::decompress(compressed, out, size))
@@ -170,7 +171,7 @@ uint64_t SectionBase::getOffset(uint64_t offset) const {
170171
// Second, InputSection::copyRelocations on .eh_frame. Some pieces may be
171172
// discarded due to GC/ICF. We should compute the output section offset.
172173
const EhInputSection *es = cast<EhInputSection>(this);
173-
if (!es->rawData.empty())
174+
if (!es->content().empty())
174175
if (InputSection *isec = es->getParent())
175176
return isec->outSecOff + es->getParentOffset(offset);
176177
return offset;
@@ -209,12 +210,12 @@ template <typename ELFT> void InputSectionBase::parseCompressedHeader() {
209210
flags &= ~(uint64_t)SHF_COMPRESSED;
210211

211212
// New-style header
212-
if (rawData.size() < sizeof(typename ELFT::Chdr)) {
213+
if (content().size() < sizeof(typename ELFT::Chdr)) {
213214
error(toString(this) + ": corrupted compressed section");
214215
return;
215216
}
216217

217-
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(rawData.data());
218+
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data());
218219
if (hdr->ch_type == ELFCOMPRESS_ZLIB) {
219220
if (!compression::zlib::isAvailable())
220221
error(toString(this) + " is compressed with ELFCOMPRESS_ZLIB, but lld is "
@@ -356,7 +357,7 @@ template <class ELFT, class RelTy>
356357
void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
357358
const TargetInfo &target = *elf::target;
358359
InputSectionBase *sec = getRelocatedSection();
359-
(void)sec->data(); // uncompress if needed
360+
(void)sec->contentMaybeDecompress(); // uncompress if needed
360361

361362
for (const RelTy &rel : rels) {
362363
RelType type = rel.getType(config->isMips64EL);
@@ -409,7 +410,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
409410
}
410411

411412
int64_t addend = getAddend<ELFT>(rel);
412-
const uint8_t *bufLoc = sec->rawData.begin() + rel.r_offset;
413+
const uint8_t *bufLoc = sec->content().begin() + rel.r_offset;
413414
if (!RelTy::IsRela)
414415
addend = target.getImplicitAddend(bufLoc, type);
415416

@@ -1103,8 +1104,8 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
11031104
// If this is a compressed section, uncompress section contents directly
11041105
// to the buffer.
11051106
if (compressed) {
1106-
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(rawData.data());
1107-
auto compressed = rawData.slice(sizeof(typename ELFT::Chdr));
1107+
auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data());
1108+
auto compressed = content().slice(sizeof(typename ELFT::Chdr));
11081109
size_t size = this->size;
11091110
if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
11101111
? compression::zlib::decompress(compressed, buf, size)
@@ -1118,8 +1119,8 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
11181119

11191120
// Copy section contents from source object file to output file
11201121
// and then apply relocations.
1121-
memcpy(buf, rawData.data(), rawData.size());
1122-
relocate<ELFT>(buf, buf + rawData.size());
1122+
memcpy(buf, content().data(), content().size());
1123+
relocate<ELFT>(buf, buf + content().size());
11231124
}
11241125

11251126
void InputSection::replace(InputSection *other) {
@@ -1166,7 +1167,7 @@ template <class ELFT> void EhInputSection::split() {
11661167

11671168
template <class ELFT, class RelTy>
11681169
void EhInputSection::split(ArrayRef<RelTy> rels) {
1169-
ArrayRef<uint8_t> d = rawData;
1170+
ArrayRef<uint8_t> d = content();
11701171
const char *msg = nullptr;
11711172
unsigned relI = 0;
11721173
while (!d.empty()) {
@@ -1190,7 +1191,7 @@ void EhInputSection::split(ArrayRef<RelTy> rels) {
11901191

11911192
// Find the first relocation that points to [off,off+size). Relocations
11921193
// have been sorted by r_offset.
1193-
const uint64_t off = d.data() - rawData.data();
1194+
const uint64_t off = d.data() - content().data();
11941195
while (relI != rels.size() && rels[relI].r_offset < off)
11951196
++relI;
11961197
unsigned firstRel = -1;
@@ -1201,7 +1202,7 @@ void EhInputSection::split(ArrayRef<RelTy> rels) {
12011202
}
12021203
if (msg)
12031204
errorOrWarn("corrupted .eh_frame: " + Twine(msg) + "\n>>> defined in " +
1204-
getObjMsg(d.data() - rawData.data()));
1205+
getObjMsg(d.data() - content().data()));
12051206
}
12061207

12071208
// Return the offset in an output section for a given input offset.
@@ -1286,13 +1287,13 @@ void MergeInputSection::splitIntoPieces() {
12861287
assert(pieces.empty());
12871288

12881289
if (flags & SHF_STRINGS)
1289-
splitStrings(toStringRef(data()), entsize);
1290+
splitStrings(toStringRef(contentMaybeDecompress()), entsize);
12901291
else
1291-
splitNonStrings(data(), entsize);
1292+
splitNonStrings(contentMaybeDecompress(), entsize);
12921293
}
12931294

12941295
SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) {
1295-
if (rawData.size() <= offset)
1296+
if (content().size() <= offset)
12961297
fatal(toString(this) + ": offset is outside the section");
12971298
return partition_point(
12981299
pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];

lld/ELF/InputSection.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ class InputSectionBase : public SectionBase {
164164
}
165165
}
166166

167-
ArrayRef<uint8_t> data() const {
167+
ArrayRef<uint8_t> content() const { return rawData; }
168+
ArrayRef<uint8_t> contentMaybeDecompress() const {
168169
if (compressed)
169170
decompress();
170171
return rawData;
@@ -228,9 +229,9 @@ class InputSectionBase : public SectionBase {
228229

229230

230231
template <typename T> llvm::ArrayRef<T> getDataAs() const {
231-
size_t s = rawData.size();
232+
size_t s = content().size();
232233
assert(s % sizeof(T) == 0);
233-
return llvm::makeArrayRef<T>((const T *)rawData.data(), s / sizeof(T));
234+
return llvm::makeArrayRef<T>((const T *)content().data(), s / sizeof(T));
234235
}
235236

236237
protected:
@@ -288,8 +289,8 @@ class MergeInputSection : public InputSectionBase {
288289
llvm::CachedHashStringRef getData(size_t i) const {
289290
size_t begin = pieces[i].inputOff;
290291
size_t end =
291-
(pieces.size() - 1 == i) ? rawData.size() : pieces[i + 1].inputOff;
292-
return {toStringRef(rawData.slice(begin, end - begin)), pieces[i].hash};
292+
(pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff;
293+
return {toStringRef(content().slice(begin, end - begin)), pieces[i].hash};
293294
}
294295

295296
// Returns the SectionPiece at a given input section offset.
@@ -313,7 +314,7 @@ struct EhSectionPiece {
313314
: inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
314315

315316
ArrayRef<uint8_t> data() const {
316-
return {sec->rawData.data() + this->inputOff, size};
317+
return {sec->content().data() + this->inputOff, size};
317318
}
318319

319320
size_t inputOff;

lld/ELF/MarkLive.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ template <class ELFT> class MarkLive {
7575
template <class ELFT>
7676
static uint64_t getAddend(InputSectionBase &sec,
7777
const typename ELFT::Rel &rel) {
78-
return target->getImplicitAddend(sec.rawData.begin() + rel.r_offset,
78+
return target->getImplicitAddend(sec.content().begin() + rel.r_offset,
7979
rel.getType(config->isMips64EL));
8080
}
8181

0 commit comments

Comments
 (0)