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.

0 commit comments

Comments
 (0)