Skip to content

Commit 66c08d9

Browse files
MaskRaytru
authored andcommitted
[ELF] Add Relocs and invokeOnRelocs. NFC
Relocs is to simplify CREL support (#98115) while invokeOnRelocs simplifies some relsOrRelas call sites that will use the CREL iterator. (cherry picked from commit 6efc377)
1 parent 28e2baa commit 66c08d9

File tree

7 files changed

+45
-35
lines changed

7 files changed

+45
-35
lines changed

lld/ELF/ICF.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ template <class ELFT> class ICF {
103103
void segregate(size_t begin, size_t end, uint32_t eqClassBase, bool constant);
104104

105105
template <class RelTy>
106-
bool constantEq(const InputSection *a, ArrayRef<RelTy> relsA,
107-
const InputSection *b, ArrayRef<RelTy> relsB);
106+
bool constantEq(const InputSection *a, Relocs<RelTy> relsA,
107+
const InputSection *b, Relocs<RelTy> relsB);
108108

109109
template <class RelTy>
110-
bool variableEq(const InputSection *a, ArrayRef<RelTy> relsA,
111-
const InputSection *b, ArrayRef<RelTy> relsB);
110+
bool variableEq(const InputSection *a, Relocs<RelTy> relsA,
111+
const InputSection *b, Relocs<RelTy> relsB);
112112

113113
bool equalsConstant(const InputSection *a, const InputSection *b);
114114
bool equalsVariable(const InputSection *a, const InputSection *b);
@@ -235,8 +235,8 @@ void ICF<ELFT>::segregate(size_t begin, size_t end, uint32_t eqClassBase,
235235
// Compare two lists of relocations.
236236
template <class ELFT>
237237
template <class RelTy>
238-
bool ICF<ELFT>::constantEq(const InputSection *secA, ArrayRef<RelTy> ra,
239-
const InputSection *secB, ArrayRef<RelTy> rb) {
238+
bool ICF<ELFT>::constantEq(const InputSection *secA, Relocs<RelTy> ra,
239+
const InputSection *secB, Relocs<RelTy> rb) {
240240
if (ra.size() != rb.size())
241241
return false;
242242
auto rai = ra.begin(), rae = ra.end(), rbi = rb.begin();
@@ -333,8 +333,8 @@ bool ICF<ELFT>::equalsConstant(const InputSection *a, const InputSection *b) {
333333
// relocations point to the same section in terms of ICF.
334334
template <class ELFT>
335335
template <class RelTy>
336-
bool ICF<ELFT>::variableEq(const InputSection *secA, ArrayRef<RelTy> ra,
337-
const InputSection *secB, ArrayRef<RelTy> rb) {
336+
bool ICF<ELFT>::variableEq(const InputSection *secA, Relocs<RelTy> ra,
337+
const InputSection *secB, Relocs<RelTy> rb) {
338338
assert(ra.size() == rb.size());
339339

340340
auto rai = ra.begin(), rae = ra.end(), rbi = rb.begin();
@@ -441,7 +441,7 @@ void ICF<ELFT>::forEachClass(llvm::function_ref<void(size_t, size_t)> fn) {
441441
// hash.
442442
template <class RelTy>
443443
static void combineRelocHashes(unsigned cnt, InputSection *isec,
444-
ArrayRef<RelTy> rels) {
444+
Relocs<RelTy> rels) {
445445
uint32_t hash = isec->eqClass[cnt % 2];
446446
for (RelTy rel : rels) {
447447
Symbol &s = isec->file->getRelocTargetSym(rel);

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
911911
// So, we handle relocations for non-alloc sections directly in this
912912
// function as a performance optimization.
913913
template <class ELFT, class RelTy>
914-
void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
914+
void InputSection::relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels) {
915915
const unsigned bits = sizeof(typename ELFT::uint) * 8;
916916
const TargetInfo &target = *elf::target;
917917
const auto emachine = config->emachine;

lld/ELF/InputSection.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions;
3737

3838
// Returned by InputSectionBase::relsOrRelas. At least one member is empty.
3939
template <class ELFT> struct RelsOrRelas {
40-
ArrayRef<typename ELFT::Rel> rels;
41-
ArrayRef<typename ELFT::Rela> relas;
40+
Relocs<typename ELFT::Rel> rels;
41+
Relocs<typename ELFT::Rela> relas;
4242
bool areRelocsRel() const { return rels.size(); }
4343
};
4444

45+
#define invokeOnRelocs(sec, f, ...) \
46+
{ \
47+
const RelsOrRelas<ELFT> rs = (sec).template relsOrRelas<ELFT>(); \
48+
if (rs.areRelocsRel()) \
49+
f(__VA_ARGS__, rs.rels); \
50+
else \
51+
f(__VA_ARGS__, rs.relas); \
52+
}
53+
4554
// This is the base class of all sections that lld handles. Some are sections in
4655
// input files, some are sections in the produced output file and some exist
4756
// just as a convenience for implementing special ways of combining some
@@ -407,7 +416,7 @@ class InputSection : public InputSectionBase {
407416
InputSectionBase *getRelocatedSection() const;
408417

409418
template <class ELFT, class RelTy>
410-
void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
419+
void relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels);
411420

412421
// Points to the canonical section. If ICF folds two sections, repl pointer of
413422
// one section points to the other.

lld/ELF/Relocations.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,9 @@ class RelocationScanner {
475475
uint64_t relOff) const;
476476
void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
477477
int64_t addend) const;
478-
template <class ELFT, class RelTy> void scanOne(RelTy *&i);
479-
template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels);
478+
template <class ELFT, class RelTy>
479+
void scanOne(typename Relocs<RelTy>::const_iterator &i);
480+
template <class ELFT, class RelTy> void scan(Relocs<RelTy> rels);
480481
};
481482
} // namespace
482483

@@ -1433,7 +1434,8 @@ static unsigned handleTlsRelocation(RelType type, Symbol &sym,
14331434
return 0;
14341435
}
14351436

1436-
template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
1437+
template <class ELFT, class RelTy>
1438+
void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
14371439
const RelTy &rel = *i;
14381440
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
14391441
Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex);
@@ -1574,7 +1576,7 @@ static void checkPPC64TLSRelax(InputSectionBase &sec, ArrayRef<RelTy> rels) {
15741576
}
15751577

15761578
template <class ELFT, class RelTy>
1577-
void RelocationScanner::scan(ArrayRef<RelTy> rels) {
1579+
void RelocationScanner::scan(Relocs<RelTy> rels) {
15781580
// Not all relocations end up in Sec->Relocations, but a lot do.
15791581
sec->relocations.reserve(rels.size());
15801582

@@ -1592,7 +1594,7 @@ void RelocationScanner::scan(ArrayRef<RelTy> rels) {
15921594

15931595
end = static_cast<const void *>(rels.end());
15941596
for (auto i = rels.begin(); i != end;)
1595-
scanOne<ELFT>(i);
1597+
scanOne<ELFT, RelTy>(i);
15961598

15971599
// Sort relocations by offset for more efficient searching for
15981600
// R_RISCV_PCREL_HI20 and R_PPC64_ADDR64.
@@ -2409,11 +2411,7 @@ template <class ELFT> void elf::checkNoCrossRefs() {
24092411
if (!isd)
24102412
continue;
24112413
parallelForEach(isd->sections, [&](InputSection *sec) {
2412-
const RelsOrRelas<ELFT> rels = sec->template relsOrRelas<ELFT>();
2413-
if (rels.areRelocsRel())
2414-
scanCrossRefs<ELFT>(noxref, osec, sec, rels.rels);
2415-
else
2416-
scanCrossRefs<ELFT>(noxref, osec, sec, rels.relas);
2414+
invokeOnRelocs(*sec, scanCrossRefs<ELFT>, noxref, osec, sec);
24172415
});
24182416
}
24192417
}

lld/ELF/Relocations.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ class ThunkCreator {
205205
uint32_t pass = 0;
206206
};
207207

208+
template <class RelTy> struct Relocs : ArrayRef<RelTy> {
209+
Relocs() = default;
210+
Relocs(ArrayRef<RelTy> a) : ArrayRef<RelTy>(a) {}
211+
};
212+
208213
// Return a int64_t to make sure we get the sign extension out of the way as
209214
// early as possible.
210215
template <class ELFT>
@@ -217,14 +222,15 @@ static inline int64_t getAddend(const typename ELFT::Rela &rel) {
217222
}
218223

219224
template <typename RelTy>
220-
ArrayRef<RelTy> sortRels(ArrayRef<RelTy> rels, SmallVector<RelTy, 0> &storage) {
225+
inline Relocs<RelTy> sortRels(Relocs<RelTy> rels,
226+
SmallVector<RelTy, 0> &storage) {
221227
auto cmp = [](const RelTy &a, const RelTy &b) {
222228
return a.r_offset < b.r_offset;
223229
};
224230
if (!llvm::is_sorted(rels, cmp)) {
225231
storage.assign(rels.begin(), rels.end());
226232
llvm::stable_sort(storage, cmp);
227-
rels = storage;
233+
rels = Relocs<RelTy>(storage);
228234
}
229235
return rels;
230236
}

lld/ELF/SyntheticSections.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,10 +3203,10 @@ template <class ELFT> DebugNamesSection<ELFT>::DebugNamesSection() {
32033203
template <class ELFT>
32043204
template <class RelTy>
32053205
void DebugNamesSection<ELFT>::getNameRelocs(
3206-
InputSection *sec, ArrayRef<RelTy> rels,
3207-
DenseMap<uint32_t, uint32_t> &relocs) {
3206+
const InputFile &file, DenseMap<uint32_t, uint32_t> &relocs,
3207+
Relocs<RelTy> rels) {
32083208
for (const RelTy &rel : rels) {
3209-
Symbol &sym = sec->file->getRelocTargetSym(rel);
3209+
Symbol &sym = file.getRelocTargetSym(rel);
32103210
relocs[rel.r_offset] = sym.getVA(getAddend<ELFT>(rel));
32113211
}
32123212
}
@@ -3216,11 +3216,7 @@ template <class ELFT> void DebugNamesSection<ELFT>::finalizeContents() {
32163216
auto relocs = std::make_unique<DenseMap<uint32_t, uint32_t>[]>(numChunks);
32173217
parallelFor(0, numChunks, [&](size_t i) {
32183218
InputSection *sec = inputSections[i];
3219-
auto rels = sec->template relsOrRelas<ELFT>();
3220-
if (rels.areRelocsRel())
3221-
getNameRelocs(sec, rels.rels, relocs.get()[i]);
3222-
else
3223-
getNameRelocs(sec, rels.relas, relocs.get()[i]);
3219+
invokeOnRelocs(*sec, getNameRelocs, *sec->file, relocs.get()[i]);
32243220

32253221
// Relocate CU offsets with .debug_info + X relocations.
32263222
OutputChunk &chunk = chunks.get()[i];

lld/ELF/SyntheticSections.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,9 @@ class DebugNamesSection final : public DebugNamesBaseSection {
916916
void writeTo(uint8_t *buf) override;
917917

918918
template <class RelTy>
919-
void getNameRelocs(InputSection *sec, ArrayRef<RelTy> rels,
920-
llvm::DenseMap<uint32_t, uint32_t> &relocs);
919+
void getNameRelocs(const InputFile &file,
920+
llvm::DenseMap<uint32_t, uint32_t> &relocs,
921+
Relocs<RelTy> rels);
921922

922923
private:
923924
static void readOffsets(InputChunk &inputChunk, OutputChunk &chunk,

0 commit comments

Comments
 (0)