Skip to content

[NFC][ELF] Replace DynamicReloc::Kind with the equivalent bool in APIs #150813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions lld/ELF/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,11 +886,11 @@ static void addPltEntry(Ctx &ctx, PltSection &plt, GotPltSection &gotPlt,
plt.addEntry(sym);
gotPlt.addEntry(sym);
if (sym.isPreemptible)
rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx),
DynamicReloc::AgainstSymbol, sym, 0, R_ADDEND});
rel.addReloc(
{type, &gotPlt, sym.getGotPltOffset(ctx), true, sym, 0, R_ADDEND});
else
rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx),
DynamicReloc::AddendOnly, sym, 0, R_ABS});
rel.addReloc(
{type, &gotPlt, sym.getGotPltOffset(ctx), false, sym, 0, R_ABS});
}

void elf::addGotEntry(Ctx &ctx, Symbol &sym) {
Expand All @@ -899,9 +899,8 @@ void elf::addGotEntry(Ctx &ctx, Symbol &sym) {

// If preemptible, emit a GLOB_DAT relocation.
if (sym.isPreemptible) {
ctx.mainPart->relaDyn->addReloc({ctx.target->gotRel, ctx.in.got.get(), off,
DynamicReloc::AgainstSymbol, sym, 0,
R_ADDEND});
ctx.mainPart->relaDyn->addReloc(
{ctx.target->gotRel, ctx.in.got.get(), off, true, sym, 0, R_ADDEND});
return;
}

Expand All @@ -922,15 +921,13 @@ static void addGotAuthEntry(Ctx &ctx, Symbol &sym) {
// If preemptible, emit a GLOB_DAT relocation.
if (sym.isPreemptible) {
ctx.mainPart->relaDyn->addReloc({R_AARCH64_AUTH_GLOB_DAT, ctx.in.got.get(),
off, DynamicReloc::AgainstSymbol, sym, 0,
R_ADDEND});
off, true, sym, 0, R_ADDEND});
return;
}

// Signed GOT requires dynamic relocation.
ctx.in.got->getPartition(ctx).relaDyn->addReloc(
{R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off, DynamicReloc::AddendOnly,
sym, 0, R_ABS});
{R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off, false, sym, 0, R_ABS});
}

static void addTpOffsetGotEntry(Ctx &ctx, Symbol &sym) {
Expand Down Expand Up @@ -1161,9 +1158,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
sec->addReloc({expr, type, offset, addend, &sym});
part.relrAuthDyn->relocs.push_back({sec, sec->relocs().size() - 1});
} else {
part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset,
DynamicReloc::AddendOnly, sym, addend,
R_ABS});
part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset, false,
sym, addend, R_ABS});
}
return;
}
Expand Down
23 changes: 10 additions & 13 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,8 @@ void MipsGotSection::build() {
// for the TP-relative offset as we don't know how much other data will
// be allocated before us in the static TLS block.
if (s->isPreemptible || ctx.arg.shared)
ctx.mainPart->relaDyn->addReloc({ctx.target->tlsGotRel, this, offset,
DynamicReloc::AgainstSymbol, *s, 0,
R_ABS});
ctx.mainPart->relaDyn->addReloc(
{ctx.target->tlsGotRel, this, offset, true, *s, 0, R_ABS});
}
for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
Symbol *s = p.first;
Expand Down Expand Up @@ -1112,15 +1111,15 @@ void MipsGotSection::build() {
for (size_t pi = 0; pi < pageCount; ++pi) {
uint64_t offset = (l.second.firstIndex + pi) * ctx.arg.wordsize;
ctx.mainPart->relaDyn->addReloc(
{ctx.target->relativeRel, this, offset, DynamicReloc::AddendOnly,
*l.second.repSym, int64_t(pi * 0x10000), RE_MIPS_OSEC_LOCAL_PAGE});
{ctx.target->relativeRel, this, offset, false, *l.second.repSym,
int64_t(pi * 0x10000), RE_MIPS_OSEC_LOCAL_PAGE});
}
}
for (const std::pair<GotEntry, size_t> &p : got.local16) {
uint64_t offset = p.second * ctx.arg.wordsize;
ctx.mainPart->relaDyn->addReloc({ctx.target->relativeRel, this, offset,
DynamicReloc::AddendOnly, *p.first.first,
p.first.second, R_ABS});
false, *p.first.first, p.first.second,
R_ABS});
}
}
}
Expand Down Expand Up @@ -1674,20 +1673,18 @@ RelocationBaseSection::RelocationBaseSection(Ctx &ctx, StringRef name,
void RelocationBaseSection::addSymbolReloc(
RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
int64_t addend, std::optional<RelType> addendRelType) {
addReloc(DynamicReloc::AgainstSymbol, dynType, isec, offsetInSec, sym, addend,
R_ADDEND, addendRelType ? *addendRelType : ctx.target->noneRel);
addReloc(true, dynType, isec, offsetInSec, sym, addend, R_ADDEND,
addendRelType ? *addendRelType : ctx.target->noneRel);
}

void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(
RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
RelType addendRelType) {
// No need to write an addend to the section for preemptible symbols.
if (sym.isPreemptible)
addReloc({dynType, &isec, offsetInSec, DynamicReloc::AgainstSymbol, sym, 0,
R_ADDEND});
addReloc({dynType, &isec, offsetInSec, true, sym, 0, R_ADDEND});
else
addReloc(DynamicReloc::AddendOnly, dynType, isec, offsetInSec, sym, 0,
R_ABS, addendRelType);
addReloc(false, dynType, isec, offsetInSec, sym, 0, R_ABS, addendRelType);
}

void RelocationBaseSection::mergeRels() {
Expand Down
27 changes: 9 additions & 18 deletions lld/ELF/SyntheticSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,27 +420,17 @@ class StringTableSection final : public SyntheticSection {

class DynamicReloc {
public:
enum Kind {
/// The resulting dynamic relocation will not reference a symbol: #sym is
/// only used to compute the addend with InputSection::getRelocTargetVA().
/// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64).
AddendOnly,
/// The resulting dynamic relocation references symbol #sym from the dynamic
/// symbol table and uses InputSection::getRelocTargetVA() for the final
/// addend.
AgainstSymbol,
};
/// This constructor records a normal relocation.
DynamicReloc(RelType type, const InputSectionBase *inputSec,
uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend,
RelExpr expr)
uint64_t offsetInSec, bool isAgainstSymbol, Symbol &sym,
int64_t addend, RelExpr expr)
: sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), type(type),
addend(addend), isAgainstSymbol(kind == AgainstSymbol), isFinal(false),
addend(addend), isAgainstSymbol(isAgainstSymbol), isFinal(false),
expr(expr) {}
/// This constructor records a relative relocation with no symbol.
DynamicReloc(RelType type, const InputSectionBase *inputSec,
uint64_t offsetInSec, int64_t addend = 0)
: DynamicReloc(type, inputSec, offsetInSec, AddendOnly,
: DynamicReloc(type, inputSec, offsetInSec, false,
*inputSec->getCtx().dummySym, addend, R_ADDEND) {}

uint64_t getOffset() const;
Expand Down Expand Up @@ -518,8 +508,8 @@ class RelocationBaseSection : public SyntheticSection {
uint64_t offsetInSec, Symbol &sym, int64_t addend,
RelType addendRelType, RelExpr expr) {
assert(expr != R_ADDEND && "expected non-addend relocation expression");
addReloc<shard>(DynamicReloc::AddendOnly, dynType, isec, offsetInSec, sym,
addend, expr, addendRelType);
addReloc<shard>(false, dynType, isec, offsetInSec, sym, addend, expr,
addendRelType);
}
/// Add a dynamic relocation using the target address of \p sym as the addend
/// if \p sym is non-preemptible. Otherwise add a relocation against \p sym.
Expand All @@ -528,14 +518,15 @@ class RelocationBaseSection : public SyntheticSection {
uint64_t offsetInSec, Symbol &sym,
RelType addendRelType);
template <bool shard = false>
void addReloc(DynamicReloc::Kind kind, RelType dynType, InputSectionBase &sec,
void addReloc(bool isAgainstSymbol, RelType dynType, InputSectionBase &sec,
uint64_t offsetInSec, Symbol &sym, int64_t addend, RelExpr expr,
RelType addendRelType) {
// Write the addends to the relocated address if required. We skip
// it if the written value would be zero.
if (ctx.arg.writeAddends && (expr != R_ADDEND || addend != 0))
sec.addReloc({expr, addendRelType, offsetInSec, addend, &sym});
addReloc<shard>({dynType, &sec, offsetInSec, kind, sym, addend, expr});
addReloc<shard>(
{dynType, &sec, offsetInSec, isAgainstSymbol, sym, addend, expr});
}
bool isNeeded() const override {
return !relocs.empty() ||
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,8 +1586,8 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
if (isInt<32>(reloc.sym->getVA(ctx, reloc.addend)))
return false;
part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec,
reloc.offset, DynamicReloc::AddendOnly,
*reloc.sym, reloc.addend, R_ABS});
reloc.offset, false, *reloc.sym,
reloc.addend, R_ABS});
return true;
});
changed |= (it != part.relrAuthDyn->relocs.end());
Expand Down
Loading