Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e3b708

Browse files
authored
[ELF] Simplify RelocScan after #163138 (#178375)
1 parent a21001a commit 5e3b708

4 files changed

Lines changed: 22 additions & 26 deletions

File tree

lld/ELF/Arch/Mips.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,7 @@ void MIPS<ELFT>::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
632632

633633
uint32_t symIdx = rel.getSymbol(ctx.arg.isMips64EL);
634634
Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIdx);
635-
RelExpr expr =
636-
ctx.target->getRelExpr(type, sym, sec.content().data() + rel.r_offset);
635+
RelExpr expr = getRelExpr(type, sym, sec.content().data() + rel.r_offset);
637636
if (expr == R_NONE)
638637
continue;
639638
if (sym.isUndefined() && symIdx != 0 &&
@@ -657,7 +656,7 @@ void MIPS<ELFT>::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
657656
for (auto *ri = &rel; ri != rels.end(); ++ri) {
658657
if (ri->getType(ctx.arg.isMips64EL) == pairTy &&
659658
ri->getSymbol(ctx.arg.isMips64EL) == symIdx) {
660-
addend += ctx.target->getImplicitAddend(buf + ri->r_offset, pairTy);
659+
addend += getImplicitAddend(buf + ri->r_offset, pairTy);
661660
found = true;
662661
break;
663662
}

lld/ELF/Arch/PPC64.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ class PPC64 final : public TargetInfo {
181181
uint64_t pltEntryAddr) const override;
182182
template <class ELFT, class RelTy>
183183
void scanSectionImpl(InputSectionBase &, Relocs<RelTy>);
184-
template <class ELFT> void scanSection1(InputSectionBase &);
185184
void scanSection(InputSectionBase &) override;
186185
void relocate(uint8_t *loc, const Relocation &rel,
187186
uint64_t val) const override;
@@ -1312,8 +1311,7 @@ void PPC64::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
13121311
uint32_t symIdx = rel.getSymbol(false);
13131312
Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIdx);
13141313
RelType type = rel.getType(false);
1315-
RelExpr expr =
1316-
ctx.target->getRelExpr(type, sym, sec.content().data() + offset);
1314+
RelExpr expr = getRelExpr(type, sym, sec.content().data() + offset);
13171315
if (expr == R_NONE)
13181316
continue;
13191317
if (sym.isUndefined() && symIdx != 0 &&
@@ -1373,19 +1371,11 @@ void PPC64::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
13731371
}
13741372
}
13751373

1376-
template <class ELFT> void PPC64::scanSection1(InputSectionBase &sec) {
1377-
auto relocs = sec.template relsOrRelas<ELFT>();
1378-
if (relocs.areRelocsCrel())
1379-
scanSectionImpl<ELFT>(sec, relocs.crels);
1380-
else
1381-
scanSectionImpl<ELFT>(sec, relocs.relas);
1382-
}
1383-
13841374
void PPC64::scanSection(InputSectionBase &sec) {
13851375
if (ctx.arg.isLE)
1386-
scanSection1<ELF64LE>(sec);
1376+
elf::scanSection1<PPC64, ELF64LE>(*this, sec);
13871377
else
1388-
scanSection1<ELF64BE>(sec);
1378+
elf::scanSection1<PPC64, ELF64BE>(*this, sec);
13891379
}
13901380

13911381
void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {

lld/ELF/Arch/RISCV.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class RISCV final : public TargetInfo {
4242
uint64_t pltEntryAddr) const override;
4343
template <class ELFT, class RelTy>
4444
void scanSectionImpl(InputSectionBase &, Relocs<RelTy>);
45-
template <class ELFT> void scanSection1(InputSectionBase &);
4645
void scanSection(InputSectionBase &) override;
4746
RelType getDynRel(RelType type) const override;
4847
RelExpr getRelExpr(RelType type, const Symbol &s,
@@ -1537,16 +1536,11 @@ void RISCV::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {
15371536
});
15381537
}
15391538

1540-
template <class ELFT> void RISCV::scanSection1(InputSectionBase &sec) {
1541-
const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
1542-
if (rels.areRelocsCrel())
1543-
scanSectionImpl<ELFT>(sec, rels.crels);
1544-
else
1545-
scanSectionImpl<ELFT>(sec, rels.relas);
1546-
}
1547-
15481539
void RISCV::scanSection(InputSectionBase &sec) {
1549-
invokeELFT(scanSection1, sec);
1540+
if (ctx.arg.is64)
1541+
elf::scanSection1<RISCV, ELF64LE>(*this, sec);
1542+
else
1543+
elf::scanSection1<RISCV, ELF32LE>(*this, sec);
15501544
}
15511545

15521546
namespace lld::elf {

lld/ELF/RelocScan.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ void RelocScan::scan(typename Relocs<RelTy>::const_iterator &it, RelType type,
121121

122122
process(expr, type, offset, sym, addend);
123123
}
124+
125+
// Dispatch to target-specific scanSectionImpl based on relocation format.
126+
template <class Target, class ELFT>
127+
void scanSection1(Target &target, InputSectionBase &sec) {
128+
const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
129+
if (rels.areRelocsCrel())
130+
target.template scanSectionImpl<ELFT>(sec, rels.crels);
131+
else if (rels.areRelocsRel())
132+
target.template scanSectionImpl<ELFT>(sec, rels.rels);
133+
else
134+
target.template scanSectionImpl<ELFT>(sec, rels.relas);
135+
}
136+
124137
} // namespace lld::elf
125138

126139
#endif

0 commit comments

Comments
 (0)