[NFCI][ELF] Store DynamicReloc Kind as two bools
Aside from Computed, Kind is now just AddendOnly and AgainstSymbol, so it's really just a bool reflecting whether the resulting ELF relocation should reference the symbol or not. Refactor DynamicReloc's storage to reflect this, splitting Computed out into its own orthogonal isFinal bool. As part of this, rename computeRaw to finalize to reflect that it's side-effecting. This also allows needsDynSymIndex() to work even after finalize(), so drop the existing assertion. A future commit will refact the DynamicReloc API to take isAgainstSymbol directly now the enum serves little purpose, as a more invasive, mechanical change. For this commit we keep DynamicReloc::Kind as the external API. Reviewers: MaskRay, arichardson Reviewed By: MaskRay, arichardson Pull Request: https://github.com/llvm/llvm-project/pull/150812
This commit is contained in:
parent
1522ad9569
commit
ab12b43047
@ -1643,17 +1643,10 @@ uint64_t DynamicReloc::getOffset() const {
|
||||
}
|
||||
|
||||
int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
|
||||
switch (kind) {
|
||||
case Computed:
|
||||
llvm_unreachable("addend already computed");
|
||||
case AddendOnly:
|
||||
case AgainstSymbol: {
|
||||
uint64_t ca = inputSec->getRelocTargetVA(
|
||||
ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
|
||||
return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
|
||||
}
|
||||
}
|
||||
llvm_unreachable("Unknown DynamicReloc::Kind enum");
|
||||
assert(!isFinal && "addend already computed");
|
||||
uint64_t ca = inputSec->getRelocTargetVA(
|
||||
ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
|
||||
return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
|
||||
}
|
||||
|
||||
uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
|
||||
@ -1734,17 +1727,17 @@ void RelocationBaseSection::finalizeContents() {
|
||||
}
|
||||
}
|
||||
|
||||
void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
|
||||
void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
|
||||
r_offset = getOffset();
|
||||
r_sym = getSymIndex(symt);
|
||||
addend = computeAddend(ctx);
|
||||
kind = Computed; // Catch errors
|
||||
isFinal = true; // Catch errors
|
||||
}
|
||||
|
||||
void RelocationBaseSection::computeRels() {
|
||||
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
|
||||
parallelForEach(relocs, [&ctx = ctx, symTab](DynamicReloc &rel) {
|
||||
rel.computeRaw(ctx, symTab);
|
||||
rel.finalize(ctx, symTab);
|
||||
});
|
||||
|
||||
auto irelative = std::stable_partition(
|
||||
|
@ -421,9 +421,6 @@ private:
|
||||
class DynamicReloc {
|
||||
public:
|
||||
enum Kind {
|
||||
/// The resulting dynamic relocation has already had its addend computed.
|
||||
/// Calling computeAddend() is an error. Only for internal use.
|
||||
Computed,
|
||||
/// 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).
|
||||
@ -438,7 +435,8 @@ public:
|
||||
uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend,
|
||||
RelExpr expr)
|
||||
: sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), type(type),
|
||||
addend(addend), kind(kind), expr(expr) {}
|
||||
addend(addend), isAgainstSymbol(kind == AgainstSymbol), 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)
|
||||
@ -447,17 +445,14 @@ public:
|
||||
|
||||
uint64_t getOffset() const;
|
||||
uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
|
||||
bool needsDynSymIndex() const {
|
||||
assert(kind != Computed && "cannot check kind after computeRaw");
|
||||
return kind == AgainstSymbol;
|
||||
}
|
||||
bool needsDynSymIndex() const { return isAgainstSymbol; }
|
||||
|
||||
/// Computes the addend of the dynamic relocation. Note that this is not the
|
||||
/// same as the #addend member variable as it may also include the symbol
|
||||
/// address/the address of the corresponding GOT entry/etc.
|
||||
int64_t computeAddend(Ctx &) const;
|
||||
|
||||
void computeRaw(Ctx &, SymbolTableBaseSection *symt);
|
||||
void finalize(Ctx &, SymbolTableBaseSection *symt);
|
||||
|
||||
Symbol *sym;
|
||||
const InputSectionBase *inputSec;
|
||||
@ -470,7 +465,15 @@ public:
|
||||
int64_t addend;
|
||||
|
||||
private:
|
||||
Kind kind;
|
||||
/// Whether this was constructed with a Kind of AgainstSymbol.
|
||||
LLVM_PREFERRED_TYPE(bool)
|
||||
uint8_t isAgainstSymbol : 1;
|
||||
|
||||
/// The resulting dynamic relocation has already had its addend computed.
|
||||
/// Calling computeAddend() is an error.
|
||||
LLVM_PREFERRED_TYPE(bool)
|
||||
uint8_t isFinal : 1;
|
||||
|
||||
// The kind of expression used to calculate the added (required e.g. for
|
||||
// relative GOT relocations).
|
||||
RelExpr expr;
|
||||
|
Loading…
x
Reference in New Issue
Block a user