Skip to content

Commit 0d81d3c

Browse files
authored
[NFCI][ELF] Introduce explicit Computed state for DynamicReloc
Currently we set the kind to AddendOnly in computeRaw() in order to catch cases where we're not treating the DynamicReloc as computed. Specifically, computeAddend() will then assert that sym is nullptr, so can catch any subsequent calls for relocations that have sym set. However, if the DynamicReloc was already AddendOnly (or MipsMultiGotPage), we will silently allow this, which does work correctly, but is not the intended use. We also cannot catch cases where needsDynSymIndex() is called after this point, which would give a misleading value if the kind were previously against a symbol. By introducing a new (internal) Computed kind we can be explicit and add more rigorous assertions, rather than abusing AddendOnly. Reviewers: arichardson, MaskRay Reviewed By: arichardson, MaskRay Pull Request: #150799
1 parent 58e6bc8 commit 0d81d3c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lld/ELF/SyntheticSections.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,8 @@ uint64_t DynamicReloc::getOffset() const {
16471647

16481648
int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
16491649
switch (kind) {
1650+
case Computed:
1651+
llvm_unreachable("addend already computed");
16501652
case AddendOnly:
16511653
assert(sym == nullptr);
16521654
return addend;
@@ -1748,7 +1750,7 @@ void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
17481750
r_offset = getOffset();
17491751
r_sym = getSymIndex(symt);
17501752
addend = computeAddend(ctx);
1751-
kind = AddendOnly; // Catch errors
1753+
kind = Computed; // Catch errors
17521754
}
17531755

17541756
void RelocationBaseSection::computeRels() {

lld/ELF/SyntheticSections.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ class StringTableSection final : public SyntheticSection {
419419
class DynamicReloc {
420420
public:
421421
enum Kind {
422+
/// The resulting dynamic relocation has already had its addend computed.
423+
/// Calling computeAddend() is an error. Only for internal use.
424+
Computed,
422425
/// The resulting dynamic relocation does not reference a symbol (#sym must
423426
/// be nullptr) and uses #addend as the result of computeAddend(ctx).
424427
AddendOnly,
@@ -461,6 +464,7 @@ class DynamicReloc {
461464
uint64_t getOffset() const;
462465
uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
463466
bool needsDynSymIndex() const {
467+
assert(kind != Computed && "cannot check kind after computeRaw");
464468
return kind == AgainstSymbol || kind == AgainstSymbolWithTargetVA;
465469
}
466470

0 commit comments

Comments
 (0)