[lld] ae1ba61 - [ELF] Replace uncompressed InputSectionBase::data() with rawData. NFC
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 21 00:39:32 PST 2022
Author: Fangrui Song
Date: 2022-02-21T00:39:26-08:00
New Revision: ae1ba6194f09b7e310fd49cf18a2829dcbeb7f6b
URL: https://github.com/llvm/llvm-project/commit/ae1ba6194f09b7e310fd49cf18a2829dcbeb7f6b
DIFF: https://github.com/llvm/llvm-project/commit/ae1ba6194f09b7e310fd49cf18a2829dcbeb7f6b.diff
LOG: [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.
Added:
Modified:
lld/ELF/AArch64ErrataFix.cpp
lld/ELF/ARMErrataFix.cpp
lld/ELF/Arch/X86_64.cpp
lld/ELF/Driver.cpp
lld/ELF/EhFrame.cpp
lld/ELF/ICF.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputSection.cpp
lld/ELF/InputSection.h
lld/ELF/MarkLive.cpp
lld/ELF/Relocations.cpp
lld/ELF/SyntheticSections.cpp
lld/ELF/Writer.cpp
Removed:
################################################################################
diff --git a/lld/ELF/AArch64ErrataFix.cpp b/lld/ELF/AArch64ErrataFix.cpp
index 9c9bd41d6e6c4..c0774be3fd2c9 100644
--- a/lld/ELF/AArch64ErrataFix.cpp
+++ b/lld/ELF/AArch64ErrataFix.cpp
@@ -349,7 +349,7 @@ static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,
}
uint64_t patchOff = 0;
- const uint8_t *buf = isec->data().begin();
+ const uint8_t *buf = isec->rawData.begin();
const ulittle32_t *instBuf = reinterpret_cast<const ulittle32_t *>(buf + off);
uint32_t instr1 = *instBuf++;
uint32_t instr2 = *instBuf++;
@@ -408,7 +408,7 @@ uint64_t Patch843419Section::getLDSTAddr() const {
void Patch843419Section::writeTo(uint8_t *buf) {
// Copy the instruction that we will be replacing with a branch in the
// patchee Section.
- write32le(buf, read32le(patchee->data().begin() + patcheeOffset));
+ write32le(buf, read32le(patchee->rawData.begin() + patcheeOffset));
// Apply any relocation transferred from the original patchee section.
relocateAlloc(buf, buf + getSize());
@@ -591,7 +591,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
auto dataSym = std::next(codeSym);
uint64_t off = (*codeSym)->value;
uint64_t limit =
- (dataSym == mapSyms.end()) ? isec->data().size() : (*dataSym)->value;
+ (dataSym == mapSyms.end()) ? isec->rawData.size() : (*dataSym)->value;
while (off < limit) {
uint64_t startAddr = isec->getVA(off);
diff --git a/lld/ELF/ARMErrataFix.cpp b/lld/ELF/ARMErrataFix.cpp
index bf277b298587d..bfeeb5db9f33b 100644
--- a/lld/ELF/ARMErrataFix.cpp
+++ b/lld/ELF/ARMErrataFix.cpp
@@ -265,7 +265,7 @@ static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off,
}
ScanResult scanRes = {0, 0, nullptr};
- const uint8_t *buf = isec->data().begin();
+ const uint8_t *buf = isec->rawData.begin();
// ARMv7-A Thumb 32-bit instructions are encoded 2 consecutive
// little-endian halfwords.
const ulittle16_t *instBuf = reinterpret_cast<const ulittle16_t *>(buf + off);
@@ -497,7 +497,7 @@ ARMErr657417Patcher::patchInputSectionDescription(
while (thumbSym != mapSyms.end()) {
auto nonThumbSym = std::next(thumbSym);
uint64_t off = (*thumbSym)->value;
- uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->data().size()
+ uint64_t limit = (nonThumbSym == mapSyms.end()) ? isec->rawData.size()
: (*nonThumbSym)->value;
while (off < limit) {
diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index ebf0a479b62a1..8c2333cd9bd8c 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -263,7 +263,7 @@ bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
Relocation &r = is.relocations[rIndex];
// Check if the relocation corresponds to a direct jmp.
- const uint8_t *secContents = is.data().data();
+ const uint8_t *secContents = is.rawData.data();
// If it is not a direct jmp instruction, there is nothing to do here.
if (*(secContents + r.offset - 1) != 0xe9)
return false;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 4f5f58dadbd06..4910a7d5a1633 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1923,7 +1923,7 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
if (!isa<Defined>(sym) || !sym->includeInDynsym())
return;
- StringRef partName = reinterpret_cast<const char *>(s->data().data());
+ StringRef partName = reinterpret_cast<const char *>(s->rawData.data());
for (Partition &part : partitions) {
if (part.name == partName) {
sym->partition = part.getNumber();
diff --git a/lld/ELF/EhFrame.cpp b/lld/ELF/EhFrame.cpp
index 9ac2ed772073a..794fe04346a99 100644
--- a/lld/ELF/EhFrame.cpp
+++ b/lld/ELF/EhFrame.cpp
@@ -42,7 +42,7 @@ class EhReader {
private:
template <class P> void failOn(const P *loc, const Twine &msg) {
fatal("corrupted .eh_frame: " + msg + "\n>>> defined in " +
- isec->getObjMsg((const uint8_t *)loc - isec->data().data()));
+ isec->getObjMsg((const uint8_t *)loc - isec->rawData.data()));
}
uint8_t readByte();
diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp
index 4c2c0a76f01db..76beff491f52e 100644
--- a/lld/ELF/ICF.cpp
+++ b/lld/ELF/ICF.cpp
@@ -312,7 +312,7 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, ArrayRef<RelTy> ra,
template <class ELFT>
bool ICF<ELFT>::equalsConstant(const InputSection *a, const InputSection *b) {
if (a->flags != b->flags || a->getSize() != b->getSize() ||
- a->data() != b->data())
+ a->rawData != b->rawData)
return false;
// If two sections have
diff erent output sections, we cannot merge them.
@@ -491,7 +491,7 @@ template <class ELFT> void ICF<ELFT>::run() {
// Initially, we use hash values to partition sections.
parallelForEach(sections, [&](InputSection *s) {
// Set MSB to 1 to avoid collisions with unique IDs.
- s->eqClass[0] = xxHash64(s->data()) | (1U << 31);
+ s->eqClass[0] = xxHash64(s->rawData) | (1U << 31);
});
// Perform 2 rounds of relocation hash propagation. 2 is an empirical value to
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 4f5c69f4e08ac..e4509c1f78880 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -789,10 +789,10 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
using Elf_Note = typename ELFT::Note;
uint32_t featuresSet = 0;
- ArrayRef<uint8_t> data = sec.data();
+ ArrayRef<uint8_t> data = sec.rawData;
auto reportFatal = [&](const uint8_t *place, const char *msg) {
fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
- Twine::utohexstr(place - sec.data().data()) + "): " + msg);
+ Twine::utohexstr(place - sec.rawData.data()) + "): " + msg);
};
while (!data.empty()) {
// Read one NOTE record.
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 01a10a797d5eb..7f6d275e327d4 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -367,6 +367,7 @@ template <class ELFT, class RelTy>
void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
const TargetInfo &target = *elf::target;
InputSectionBase *sec = getRelocatedSection();
+ (void)sec->data(); // uncompress if needed
for (const RelTy &rel : rels) {
RelType type = rel.getType(config->isMips64EL);
@@ -419,7 +420,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
}
int64_t addend = getAddend<ELFT>(rel);
- const uint8_t *bufLoc = sec->data().begin() + rel.r_offset;
+ const uint8_t *bufLoc = sec->rawData.begin() + rel.r_offset;
if (!RelTy::IsRela)
addend = target.getImplicitAddend(bufLoc, type);
@@ -1423,7 +1424,7 @@ void MergeInputSection::splitIntoPieces() {
}
SectionPiece *MergeInputSection::getSectionPiece(uint64_t offset) {
- if (this->data().size() <= offset)
+ if (this->rawData.size() <= offset)
fatal(toString(this) + ": offset is outside the section");
// If Offset is not at beginning of a section piece, it is not in the map.
diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index e4fb354592a09..a6413ff2e11ba 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -215,18 +215,18 @@ class InputSectionBase : public SectionBase {
template <typename T> llvm::ArrayRef<T> getDataAs() const {
- size_t s = data().size();
+ size_t s = rawData.size();
assert(s % sizeof(T) == 0);
- return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
+ return llvm::makeArrayRef<T>((const T *)rawData.data(), s / sizeof(T));
}
+ mutable ArrayRef<uint8_t> rawData;
+
protected:
template <typename ELFT>
void parseCompressedHeader();
void uncompress() const;
- mutable ArrayRef<uint8_t> rawData;
-
// This field stores the uncompressed size of the compressed data in rawData,
// 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).
@@ -277,8 +277,8 @@ class MergeInputSection : public InputSectionBase {
llvm::CachedHashStringRef getData(size_t i) const {
size_t begin = pieces[i].inputOff;
size_t end =
- (pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
- return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
+ (pieces.size() - 1 == i) ? rawData.size() : pieces[i + 1].inputOff;
+ return {toStringRef(rawData.slice(begin, end - begin)), pieces[i].hash};
}
// Returns the SectionPiece at a given input section offset.
@@ -300,7 +300,7 @@ struct EhSectionPiece {
: inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
ArrayRef<uint8_t> data() const {
- return {sec->data().data() + this->inputOff, size};
+ return {sec->rawData.data() + this->inputOff, size};
}
size_t inputOff;
diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index a55758b20f99c..b197dd45d765b 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -74,7 +74,7 @@ template <class ELFT> class MarkLive {
template <class ELFT>
static uint64_t getAddend(InputSectionBase &sec,
const typename ELFT::Rel &rel) {
- return target->getImplicitAddend(sec.data().begin() + rel.r_offset,
+ return target->getImplicitAddend(sec.rawData.begin() + rel.r_offset,
rel.getType(config->isMips64EL));
}
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 20433b4552edd..8bc52ed2e3771 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -497,7 +497,7 @@ int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
if (pairTy == R_MIPS_NONE)
return 0;
- const uint8_t *buf = sec.data().data();
+ const uint8_t *buf = sec.rawData.data();
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
// To make things worse, paired relocations might not be contiguous in
@@ -524,7 +524,7 @@ int64_t RelocationScanner::computeAddend(const RelTy &rel, RelExpr expr,
if (RelTy::IsRela) {
addend = getAddend<ELFT>(rel);
} else {
- const uint8_t *buf = sec.data().data();
+ const uint8_t *buf = sec.rawData.data();
addend = target.getImplicitAddend(buf + rel.r_offset, type);
}
@@ -1326,7 +1326,7 @@ template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
maybeReportUndefined(cast<Undefined>(sym), sec, offset))
return;
- const uint8_t *relocatedAddr = sec.data().begin() + offset;
+ const uint8_t *relocatedAddr = sec.rawData.begin() + offset;
RelExpr expr = target.getRelExpr(type, sym, relocatedAddr);
// Ignore R_*_NONE and other marker relocations.
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 37b6877d699d1..2a93e66e7c029 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -112,7 +112,7 @@ std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() {
create = true;
std::string filename = toString(sec->file);
- const size_t size = sec->data().size();
+ const size_t size = sec->rawData.size();
// Older version of BFD (such as the default FreeBSD linker) concatenate
// .MIPS.abiflags instead of merging. To allow for this case (or potential
// zero padding) we ignore everything after the first Elf_Mips_ABIFlags
@@ -121,7 +121,7 @@ std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() {
Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
return nullptr;
}
- auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
+ auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->rawData.data());
if (s->version != 0) {
error(filename + ": unexpected .MIPS.abiflags version " +
Twine(s->version));
@@ -184,7 +184,7 @@ std::unique_ptr<MipsOptionsSection<ELFT>> MipsOptionsSection<ELFT>::create() {
sec->markDead();
std::string filename = toString(sec->file);
- ArrayRef<uint8_t> d = sec->data();
+ ArrayRef<uint8_t> d = sec->rawData;
while (!d.empty()) {
if (d.size() < sizeof(Elf_Mips_Options)) {
@@ -240,12 +240,12 @@ std::unique_ptr<MipsReginfoSection<ELFT>> MipsReginfoSection<ELFT>::create() {
for (InputSectionBase *sec : sections) {
sec->markDead();
- if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
+ if (sec->rawData.size() != sizeof(Elf_Mips_RegInfo)) {
error(toString(sec->file) + ": invalid size of .reginfo section");
return nullptr;
}
- auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
+ auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->rawData.data());
reginfo.ri_gprmask |= r->ri_gprmask;
sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
};
@@ -3535,7 +3535,7 @@ void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {
for (InputSection *isec : executableSections) {
assert(isec->getParent() != nullptr);
if (InputSection *d = findExidxSection(isec)) {
- memcpy(buf + offset, d->data().data(), d->data().size());
+ memcpy(buf + offset, d->rawData.data(), d->rawData.size());
d->relocateAlloc(buf + d->outSecOff, buf + d->outSecOff + d->getSize());
offset += d->getSize();
} else {
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 0282d7d6b5a78..cd43e79b82760 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1695,7 +1695,7 @@ static void fixSymbolsAfterShrinking() {
if (!inputSec || !inputSec->bytesDropped)
return;
- const size_t OldSize = inputSec->data().size();
+ const size_t OldSize = inputSec->rawData.size();
const size_t NewSize = OldSize - inputSec->bytesDropped;
if (def->value > NewSize && def->value <= OldSize) {
More information about the llvm-commits
mailing list