Skip to content

Commit ae1ba61

Browse files
committed
[ELF] Replace uncompressed InputSectionBase::data() with rawData. NFC
In many call sites we know uncompression cannot happen (non-SHF_ALLOC, or the data (even if compressed) must have been uncompressed by a previous pass). Prefer rawData in these cases. data() increases code size and prevents optimization on rawData.
1 parent 41cb504 commit ae1ba61

13 files changed

+33
-32
lines changed

lld/ELF/AArch64ErrataFix.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,
349349
}
350350

351351
uint64_t patchOff = 0;
352-
const uint8_t *buf = isec->data().begin();
352+
const uint8_t *buf = isec->rawData.begin();
353353
const ulittle32_t *instBuf = reinterpret_cast<const ulittle32_t *>(buf + off);
354354
uint32_t instr1 = *instBuf++;
355355
uint32_t instr2 = *instBuf++;
@@ -408,7 +408,7 @@ uint64_t Patch843419Section::getLDSTAddr() const {
408408
void Patch843419Section::writeTo(uint8_t *buf) {
409409
// Copy the instruction that we will be replacing with a branch in the
410410
// patchee Section.
411-
write32le(buf, read32le(patchee->data().begin() + patcheeOffset));
411+
write32le(buf, read32le(patchee->rawData.begin() + patcheeOffset));
412412

413413
// Apply any relocation transferred from the original patchee section.
414414
relocateAlloc(buf, buf + getSize());
@@ -591,7 +591,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
591591
auto dataSym = std::next(codeSym);
592592
uint64_t off = (*codeSym)->value;
593593
uint64_t limit =
594-
(dataSym == mapSyms.end()) ? isec->data().size() : (*dataSym)->value;
594+
(dataSym == mapSyms.end()) ? isec->rawData.size() : (*dataSym)->value;
595595

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

lld/ELF/ARMErrataFix.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off,
265265
}
266266

267267
ScanResult scanRes = {0, 0, nullptr};
268-
const uint8_t *buf = isec->data().begin();
268+
const uint8_t *buf = isec->rawData.begin();
269269
// ARMv7-A Thumb 32-bit instructions are encoded 2 consecutive
270270
// little-endian halfwords.
271271
const ulittle16_t *instBuf = reinterpret_cast<const ulittle16_t *>(buf + off);
@@ -497,7 +497,7 @@ ARMErr657417Patcher::patchInputSectionDescription(
497497
while (thumbSym != mapSyms.end()) {
498498
auto nonThumbSym = std::next(thumbSym);
499499
uint64_t off = (*thumbSym)->value;
500-
uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->data().size()
500+
uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->rawData.size()
501501
: (*nonThumbSym)->value;
502502

503503
while (off < limit) {

lld/ELF/Arch/X86_64.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
263263
Relocation &r = is.relocations[rIndex];
264264

265265
// Check if the relocation corresponds to a direct jmp.
266-
const uint8_t *secContents = is.data().data();
266+
const uint8_t *secContents = is.rawData.data();
267267
// If it is not a direct jmp instruction, there is nothing to do here.
268268
if (*(secContents + r.offset - 1) != 0xe9)
269269
return false;

lld/ELF/Driver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
19231923
if (!isa<Defined>(sym) || !sym->includeInDynsym())
19241924
return;
19251925

1926-
StringRef partName = reinterpret_cast<const char *>(s->data().data());
1926+
StringRef partName = reinterpret_cast<const char *>(s->rawData.data());
19271927
for (Partition &part : partitions) {
19281928
if (part.name == partName) {
19291929
sym->partition = part.getNumber();

lld/ELF/EhFrame.cpp

+1-1
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->data().data()));
45+
isec->getObjMsg((const uint8_t *)loc - isec->rawData.data()));
4646
}
4747

4848
uint8_t readByte();

lld/ELF/ICF.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, ArrayRef<RelTy> ra,
312312
template <class ELFT>
313313
bool ICF<ELFT>::equalsConstant(const InputSection *a, const InputSection *b) {
314314
if (a->flags != b->flags || a->getSize() != b->getSize() ||
315-
a->data() != b->data())
315+
a->rawData != b->rawData)
316316
return false;
317317

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

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

lld/ELF/InputFiles.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,10 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
789789
using Elf_Note = typename ELFT::Note;
790790

791791
uint32_t featuresSet = 0;
792-
ArrayRef<uint8_t> data = sec.data();
792+
ArrayRef<uint8_t> data = sec.rawData;
793793
auto reportFatal = [&](const uint8_t *place, const char *msg) {
794794
fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
795-
Twine::utohexstr(place - sec.data().data()) + "): " + msg);
795+
Twine::utohexstr(place - sec.rawData.data()) + "): " + msg);
796796
};
797797
while (!data.empty()) {
798798
// Read one NOTE record.

lld/ELF/InputSection.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ template <class ELFT, class RelTy>
367367
void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
368368
const TargetInfo &target = *elf::target;
369369
InputSectionBase *sec = getRelocatedSection();
370+
(void)sec->data(); // uncompress if needed
370371

371372
for (const RelTy &rel : rels) {
372373
RelType type = rel.getType(config->isMips64EL);
@@ -419,7 +420,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
419420
}
420421

421422
int64_t addend = getAddend<ELFT>(rel);
422-
const uint8_t *bufLoc = sec->data().begin() + rel.r_offset;
423+
const uint8_t *bufLoc = sec->rawData.begin() + rel.r_offset;
423424
if (!RelTy::IsRela)
424425
addend = target.getImplicitAddend(bufLoc, type);
425426

@@ -1423,7 +1424,7 @@ void MergeInputSection::splitIntoPieces() {
14231424
}
14241425

14251426
SectionPiece *MergeInputSection::getSectionPiece(uint64_t offset) {
1426-
if (this->data().size() <= offset)
1427+
if (this->rawData.size() <= offset)
14271428
fatal(toString(this) + ": offset is outside the section");
14281429

14291430
// If Offset is not at beginning of a section piece, it is not in the map.

lld/ELF/InputSection.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,18 @@ class InputSectionBase : public SectionBase {
215215

216216

217217
template <typename T> llvm::ArrayRef<T> getDataAs() const {
218-
size_t s = data().size();
218+
size_t s = rawData.size();
219219
assert(s % sizeof(T) == 0);
220-
return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
220+
return llvm::makeArrayRef<T>((const T *)rawData.data(), s / sizeof(T));
221221
}
222222

223+
mutable ArrayRef<uint8_t> rawData;
224+
223225
protected:
224226
template <typename ELFT>
225227
void parseCompressedHeader();
226228
void uncompress() const;
227229

228-
mutable ArrayRef<uint8_t> rawData;
229-
230230
// This field stores the uncompressed size of the compressed data in rawData,
231231
// or -1 if rawData is not compressed (either because the section wasn't
232232
// compressed in the first place, or because we ended up uncompressing it).
@@ -277,8 +277,8 @@ class MergeInputSection : public InputSectionBase {
277277
llvm::CachedHashStringRef getData(size_t i) const {
278278
size_t begin = pieces[i].inputOff;
279279
size_t end =
280-
(pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
281-
return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
280+
(pieces.size() - 1 == i) ? rawData.size() : pieces[i + 1].inputOff;
281+
return {toStringRef(rawData.slice(begin, end - begin)), pieces[i].hash};
282282
}
283283

284284
// Returns the SectionPiece at a given input section offset.
@@ -300,7 +300,7 @@ struct EhSectionPiece {
300300
: inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
301301

302302
ArrayRef<uint8_t> data() const {
303-
return {sec->data().data() + this->inputOff, size};
303+
return {sec->rawData.data() + this->inputOff, size};
304304
}
305305

306306
size_t inputOff;

lld/ELF/MarkLive.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ template <class ELFT> class MarkLive {
7474
template <class ELFT>
7575
static uint64_t getAddend(InputSectionBase &sec,
7676
const typename ELFT::Rel &rel) {
77-
return target->getImplicitAddend(sec.data().begin() + rel.r_offset,
77+
return target->getImplicitAddend(sec.rawData.begin() + rel.r_offset,
7878
rel.getType(config->isMips64EL));
7979
}
8080

lld/ELF/Relocations.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
497497
if (pairTy == R_MIPS_NONE)
498498
return 0;
499499

500-
const uint8_t *buf = sec.data().data();
500+
const uint8_t *buf = sec.rawData.data();
501501
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
502502

503503
// To make things worse, paired relocations might not be contiguous in
@@ -524,7 +524,7 @@ int64_t RelocationScanner::computeAddend(const RelTy &rel, RelExpr expr,
524524
if (RelTy::IsRela) {
525525
addend = getAddend<ELFT>(rel);
526526
} else {
527-
const uint8_t *buf = sec.data().data();
527+
const uint8_t *buf = sec.rawData.data();
528528
addend = target.getImplicitAddend(buf + rel.r_offset, type);
529529
}
530530

@@ -1326,7 +1326,7 @@ template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
13261326
maybeReportUndefined(cast<Undefined>(sym), sec, offset))
13271327
return;
13281328

1329-
const uint8_t *relocatedAddr = sec.data().begin() + offset;
1329+
const uint8_t *relocatedAddr = sec.rawData.begin() + offset;
13301330
RelExpr expr = target.getRelExpr(type, sym, relocatedAddr);
13311331

13321332
// Ignore R_*_NONE and other marker relocations.

lld/ELF/SyntheticSections.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() {
112112
create = true;
113113

114114
std::string filename = toString(sec->file);
115-
const size_t size = sec->data().size();
115+
const size_t size = sec->rawData.size();
116116
// Older version of BFD (such as the default FreeBSD linker) concatenate
117117
// .MIPS.abiflags instead of merging. To allow for this case (or potential
118118
// zero padding) we ignore everything after the first Elf_Mips_ABIFlags
@@ -121,7 +121,7 @@ std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() {
121121
Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
122122
return nullptr;
123123
}
124-
auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124+
auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->rawData.data());
125125
if (s->version != 0) {
126126
error(filename + ": unexpected .MIPS.abiflags version " +
127127
Twine(s->version));
@@ -184,7 +184,7 @@ std::unique_ptr<MipsOptionsSection<ELFT>> MipsOptionsSection<ELFT>::create() {
184184
sec->markDead();
185185

186186
std::string filename = toString(sec->file);
187-
ArrayRef<uint8_t> d = sec->data();
187+
ArrayRef<uint8_t> d = sec->rawData;
188188

189189
while (!d.empty()) {
190190
if (d.size() < sizeof(Elf_Mips_Options)) {
@@ -240,12 +240,12 @@ std::unique_ptr<MipsReginfoSection<ELFT>> MipsReginfoSection<ELFT>::create() {
240240
for (InputSectionBase *sec : sections) {
241241
sec->markDead();
242242

243-
if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243+
if (sec->rawData.size() != sizeof(Elf_Mips_RegInfo)) {
244244
error(toString(sec->file) + ": invalid size of .reginfo section");
245245
return nullptr;
246246
}
247247

248-
auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248+
auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->rawData.data());
249249
reginfo.ri_gprmask |= r->ri_gprmask;
250250
sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
251251
};
@@ -3535,7 +3535,7 @@ void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {
35353535
for (InputSection *isec : executableSections) {
35363536
assert(isec->getParent() != nullptr);
35373537
if (InputSection *d = findExidxSection(isec)) {
3538-
memcpy(buf + offset, d->data().data(), d->data().size());
3538+
memcpy(buf + offset, d->rawData.data(), d->rawData.size());
35393539
d->relocateAlloc(buf + d->outSecOff, buf + d->outSecOff + d->getSize());
35403540
offset += d->getSize();
35413541
} else {

lld/ELF/Writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ static void fixSymbolsAfterShrinking() {
16951695
if (!inputSec || !inputSec->bytesDropped)
16961696
return;
16971697

1698-
const size_t OldSize = inputSec->data().size();
1698+
const size_t OldSize = inputSec->rawData.size();
16991699
const size_t NewSize = OldSize - inputSec->bytesDropped;
17001700

17011701
if (def->value > NewSize && def->value <= OldSize) {

0 commit comments

Comments
 (0)