[lld] 0d81d3c - [NFCI][ELF] Introduce explicit Computed state for DynamicReloc
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 30 09:03:57 PDT 2025
Author: Jessica Clarke
Date: 2025-07-30T17:03:54+01:00
New Revision: 0d81d3c59a9f10cb4b7a1f6086ae42b7f59ad6d3
URL: https://github.com/llvm/llvm-project/commit/0d81d3c59a9f10cb4b7a1f6086ae42b7f59ad6d3
DIFF: https://github.com/llvm/llvm-project/commit/0d81d3c59a9f10cb4b7a1f6086ae42b7f59ad6d3.diff
LOG: [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: https://github.com/llvm/llvm-project/pull/150799
Added:
Modified:
lld/ELF/SyntheticSections.cpp
lld/ELF/SyntheticSections.h
Removed:
################################################################################
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index efec41a737b62..16f1ab36a88b1 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1647,6 +1647,8 @@ uint64_t DynamicReloc::getOffset() const {
int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
switch (kind) {
+ case Computed:
+ llvm_unreachable("addend already computed");
case AddendOnly:
assert(sym == nullptr);
return addend;
@@ -1748,7 +1750,7 @@ void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
r_offset = getOffset();
r_sym = getSymIndex(symt);
addend = computeAddend(ctx);
- kind = AddendOnly; // Catch errors
+ kind = Computed; // Catch errors
}
void RelocationBaseSection::computeRels() {
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index 5f01513630597..8e069e3dd9565 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -419,6 +419,9 @@ class StringTableSection final : public SyntheticSection {
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 does not reference a symbol (#sym must
/// be nullptr) and uses #addend as the result of computeAddend(ctx).
AddendOnly,
@@ -461,6 +464,7 @@ class DynamicReloc {
uint64_t getOffset() const;
uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
bool needsDynSymIndex() const {
+ assert(kind != Computed && "cannot check kind after computeRaw");
return kind == AgainstSymbol || kind == AgainstSymbolWithTargetVA;
}
More information about the llvm-commits
mailing list