Skip to content

Commit fd5d7c5

Browse files
authored
ELF: Split relocateAlloc to relocateAlloc and relocateEh. NFC
relocateAlloc can be called with either InputSection (including SyntheticSection like GotSection) or EhInputSection. Introduce relocateEh so that we can remove some boilerplate and replace relocateAlloc's parameter type with `InputSection`. Pull Request: llvm/llvm-project#160031
1 parent 42bb5a5 commit fd5d7c5

File tree

13 files changed

+45
-60
lines changed

13 files changed

+45
-60
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class AArch64 : public TargetInfo {
8282
void relocate(uint8_t *loc, const Relocation &rel,
8383
uint64_t val) const override;
8484
RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
85-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
85+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
8686
void applyBranchToBranchOpt() const override;
8787

8888
private:
@@ -939,12 +939,8 @@ static bool needsGotForMemtag(const Relocation &rel) {
939939
return rel.sym->isTagged() && needsGot(rel.expr);
940940
}
941941

942-
void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
943-
uint64_t secAddr = sec.getOutputSection()->addr;
944-
if (auto *s = dyn_cast<InputSection>(&sec))
945-
secAddr += s->outSecOff;
946-
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
947-
secAddr += ehIn->getParent()->outSecOff;
942+
void AArch64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
943+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
948944
AArch64Relaxer relaxer(ctx, sec.relocs());
949945
for (size_t i = 0, size = sec.relocs().size(); i != size; ++i) {
950946
const Relocation &rel = sec.relocs()[i];

lld/ELF/Arch/LoongArch.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LoongArch final : public TargetInfo {
4141
bool relaxOnce(int pass) const override;
4242
bool synthesizeAlign(uint64_t &dot, InputSection *sec) override;
4343
RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
44-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
44+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
4545
void finalizeRelax(int passes) const override;
4646

4747
private:
@@ -1395,13 +1395,9 @@ static bool pairForGotRels(ArrayRef<Relocation> relocs) {
13951395
return i == size;
13961396
}
13971397

1398-
void LoongArch::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
1398+
void LoongArch::relocateAlloc(InputSection &sec, uint8_t *buf) const {
13991399
const unsigned bits = ctx.arg.is64 ? 64 : 32;
1400-
uint64_t secAddr = sec.getOutputSection()->addr;
1401-
if (auto *s = dyn_cast<InputSection>(&sec))
1402-
secAddr += s->outSecOff;
1403-
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
1404-
secAddr += ehIn->getParent()->outSecOff;
1400+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
14051401
bool isExtreme = false, isRelax = false;
14061402
const MutableArrayRef<Relocation> relocs = sec.relocs();
14071403
const bool isPairForGotRels = pairForGotRels(relocs);

lld/ELF/Arch/PPC.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class PPC final : public TargetInfo {
4949
uint64_t val) const override;
5050
RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
5151
int getTlsGdRelaxSkip(RelType type) const override;
52-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
52+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
5353

5454
private:
5555
void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
@@ -496,10 +496,8 @@ void PPC::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
496496
}
497497
}
498498

499-
void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
500-
uint64_t secAddr = sec.getOutputSection()->addr;
501-
if (auto *s = dyn_cast<InputSection>(&sec))
502-
secAddr += s->outSecOff;
499+
void PPC::relocateAlloc(InputSection &sec, uint8_t *buf) const {
500+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
503501
for (const Relocation &rel : sec.relocs()) {
504502
uint8_t *loc = buf + rel.offset;
505503
const uint64_t val =

lld/ELF/Arch/PPC64.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class PPC64 final : public TargetInfo {
190190
RelExpr adjustGotPcExpr(RelType type, int64_t addend,
191191
const uint8_t *loc) const override;
192192
void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) const;
193-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
193+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
194194

195195
bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
196196
uint8_t stOther) const override;
@@ -1561,12 +1561,8 @@ void PPC64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
15611561
}
15621562
}
15631563

1564-
void PPC64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
1565-
uint64_t secAddr = sec.getOutputSection()->addr;
1566-
if (auto *s = dyn_cast<InputSection>(&sec))
1567-
secAddr += s->outSecOff;
1568-
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
1569-
secAddr += ehIn->getParent()->outSecOff;
1564+
void PPC64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
1565+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
15701566
uint64_t lastPPCRelaxedRelocOff = -1;
15711567
for (const Relocation &rel : sec.relocs()) {
15721568
uint8_t *loc = buf + rel.offset;

lld/ELF/Arch/RISCV.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class RISCV final : public TargetInfo {
4343
const uint8_t *loc) const override;
4444
void relocate(uint8_t *loc, const Relocation &rel,
4545
uint64_t val) const override;
46-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
46+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
4747
bool relaxOnce(int pass) const override;
4848
template <class ELFT, class RelTy>
4949
bool synthesizeAlignForInput(uint64_t &dot, InputSection *sec,
@@ -603,12 +603,8 @@ static void tlsdescToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
603603
}
604604
}
605605

606-
void RISCV::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
607-
uint64_t secAddr = sec.getOutputSection()->addr;
608-
if (auto *s = dyn_cast<InputSection>(&sec))
609-
secAddr += s->outSecOff;
610-
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
611-
secAddr += ehIn->getParent()->outSecOff;
606+
void RISCV::relocateAlloc(InputSection &sec, uint8_t *buf) const {
607+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
612608
uint64_t tlsdescVal = 0;
613609
bool tlsdescRelax = false, isToLe = false;
614610
const ArrayRef<Relocation> relocs = sec.relocs();

lld/ELF/Arch/X86.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class X86 : public TargetInfo {
3737
uint64_t val) const override;
3838

3939
RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
40-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
40+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
4141

4242
private:
4343
void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
@@ -491,10 +491,8 @@ void X86::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
491491
memcpy(loc - 2, inst, sizeof(inst));
492492
}
493493

494-
void X86::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
495-
uint64_t secAddr = sec.getOutputSection()->addr;
496-
if (auto *s = dyn_cast<InputSection>(&sec))
497-
secAddr += s->outSecOff;
494+
void X86::relocateAlloc(InputSection &sec, uint8_t *buf) const {
495+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
498496
for (const Relocation &rel : sec.relocs()) {
499497
uint8_t *loc = buf + rel.offset;
500498
const uint64_t val =

lld/ELF/Arch/X86_64.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class X86_64 : public TargetInfo {
4444
unsigned size) const override;
4545
RelExpr adjustGotPcExpr(RelType type, int64_t addend,
4646
const uint8_t *loc) const override;
47-
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
47+
void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
4848
bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
4949
uint8_t stOther) const override;
5050
bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
@@ -1146,12 +1146,8 @@ bool X86_64::adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
11461146
return false;
11471147
}
11481148

1149-
void X86_64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
1150-
uint64_t secAddr = sec.getOutputSection()->addr;
1151-
if (auto *s = dyn_cast<InputSection>(&sec))
1152-
secAddr += s->outSecOff;
1153-
else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
1154-
secAddr += ehIn->getParent()->outSecOff;
1149+
void X86_64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
1150+
uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
11551151
for (const Relocation &rel : sec.relocs()) {
11561152
if (rel.expr == R_NONE) // See deleteFallThruJmpInsn
11571153
continue;

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf,
11711171
}
11721172

11731173
template <class ELFT>
1174-
void InputSectionBase::relocate(Ctx &ctx, uint8_t *buf, uint8_t *bufEnd) {
1174+
void InputSection::relocate(Ctx &ctx, uint8_t *buf, uint8_t *bufEnd) {
11751175
if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack))
11761176
adjustSplitStackFunctionPrologues<ELFT>(ctx, buf, bufEnd);
11771177

lld/ELF/InputSection.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,6 @@ class InputSectionBase : public SectionBase {
262262
return {*this, sym, offset};
263263
}
264264

265-
// Each section knows how to relocate itself. These functions apply
266-
// relocations, assuming that Buf points to this section's copy in
267-
// the mmap'ed output buffer.
268-
template <class ELFT> void relocate(Ctx &, uint8_t *buf, uint8_t *bufEnd);
269265
uint64_t getRelocTargetVA(Ctx &, const Relocation &r, uint64_t p) const;
270266

271267
// The native ELF reloc data type is not very convenient to handle.
@@ -443,8 +439,12 @@ class InputSection : public InputSectionBase {
443439

444440
InputSectionBase *getRelocatedSection() const;
445441

442+
// Each section knows how to relocate itself. These functions apply
443+
// relocations, assuming that `buf` points to this section's copy in
444+
// the mmap'ed output buffer.
446445
template <class ELFT, class RelTy>
447446
void relocateNonAlloc(Ctx &, uint8_t *buf, Relocs<RelTy> rels);
447+
template <class ELFT> void relocate(Ctx &, uint8_t *buf, uint8_t *bufEnd);
448448

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

lld/ELF/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
15491549
sec->file->ppc64SmallCodeModelTocRelocs = true;
15501550

15511551
// Record the TOC entry (.toc + addend) as not relaxable. See the comment in
1552-
// InputSectionBase::relocateAlloc().
1552+
// PPC64::relocateAlloc().
15531553
if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) &&
15541554
cast<Defined>(sym).section->name == ".toc")
15551555
ctx.ppc64noTocRelax.insert({&sym, addend});

0 commit comments

Comments
 (0)