Skip to content

Commit ab12b43

Browse files
authored
[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: #150812
1 parent 1522ad9 commit ab12b43

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

lld/ELF/SyntheticSections.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,17 +1643,10 @@ uint64_t DynamicReloc::getOffset() const {
16431643
}
16441644

16451645
int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
1646-
switch (kind) {
1647-
case Computed:
1648-
llvm_unreachable("addend already computed");
1649-
case AddendOnly:
1650-
case AgainstSymbol: {
1651-
uint64_t ca = inputSec->getRelocTargetVA(
1652-
ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
1653-
return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
1654-
}
1655-
}
1656-
llvm_unreachable("Unknown DynamicReloc::Kind enum");
1646+
assert(!isFinal && "addend already computed");
1647+
uint64_t ca = inputSec->getRelocTargetVA(
1648+
ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
1649+
return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
16571650
}
16581651

16591652
uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
@@ -1734,17 +1727,17 @@ void RelocationBaseSection::finalizeContents() {
17341727
}
17351728
}
17361729

1737-
void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
1730+
void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
17381731
r_offset = getOffset();
17391732
r_sym = getSymIndex(symt);
17401733
addend = computeAddend(ctx);
1741-
kind = Computed; // Catch errors
1734+
isFinal = true; // Catch errors
17421735
}
17431736

17441737
void RelocationBaseSection::computeRels() {
17451738
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
17461739
parallelForEach(relocs, [&ctx = ctx, symTab](DynamicReloc &rel) {
1747-
rel.computeRaw(ctx, symTab);
1740+
rel.finalize(ctx, symTab);
17481741
});
17491742

17501743
auto irelative = std::stable_partition(

lld/ELF/SyntheticSections.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ class StringTableSection final : public SyntheticSection {
421421
class DynamicReloc {
422422
public:
423423
enum Kind {
424-
/// The resulting dynamic relocation has already had its addend computed.
425-
/// Calling computeAddend() is an error. Only for internal use.
426-
Computed,
427424
/// The resulting dynamic relocation will not reference a symbol: #sym is
428425
/// only used to compute the addend with InputSection::getRelocTargetVA().
429426
/// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64).
@@ -438,7 +435,8 @@ class DynamicReloc {
438435
uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend,
439436
RelExpr expr)
440437
: sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), type(type),
441-
addend(addend), kind(kind), expr(expr) {}
438+
addend(addend), isAgainstSymbol(kind == AgainstSymbol), isFinal(false),
439+
expr(expr) {}
442440
/// This constructor records a relative relocation with no symbol.
443441
DynamicReloc(RelType type, const InputSectionBase *inputSec,
444442
uint64_t offsetInSec, int64_t addend = 0)
@@ -447,17 +445,14 @@ class DynamicReloc {
447445

448446
uint64_t getOffset() const;
449447
uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
450-
bool needsDynSymIndex() const {
451-
assert(kind != Computed && "cannot check kind after computeRaw");
452-
return kind == AgainstSymbol;
453-
}
448+
bool needsDynSymIndex() const { return isAgainstSymbol; }
454449

455450
/// Computes the addend of the dynamic relocation. Note that this is not the
456451
/// same as the #addend member variable as it may also include the symbol
457452
/// address/the address of the corresponding GOT entry/etc.
458453
int64_t computeAddend(Ctx &) const;
459454

460-
void computeRaw(Ctx &, SymbolTableBaseSection *symt);
455+
void finalize(Ctx &, SymbolTableBaseSection *symt);
461456

462457
Symbol *sym;
463458
const InputSectionBase *inputSec;
@@ -470,7 +465,15 @@ class DynamicReloc {
470465
int64_t addend;
471466

472467
private:
473-
Kind kind;
468+
/// Whether this was constructed with a Kind of AgainstSymbol.
469+
LLVM_PREFERRED_TYPE(bool)
470+
uint8_t isAgainstSymbol : 1;
471+
472+
/// The resulting dynamic relocation has already had its addend computed.
473+
/// Calling computeAddend() is an error.
474+
LLVM_PREFERRED_TYPE(bool)
475+
uint8_t isFinal : 1;
476+
474477
// The kind of expression used to calculate the added (required e.g. for
475478
// relative GOT relocations).
476479
RelExpr expr;

0 commit comments

Comments
 (0)