Skip to content

[NFCI][ELF] Introduce explicit Computed state for DynamicReloc #150799

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

Conversation

jrtc27
Copy link
Collaborator

@jrtc27 jrtc27 commented Jul 26, 2025

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.

jrtc27 added 2 commits July 26, 2025 22:05
Created using spr 1.3.5
@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2025

@llvm/pr-subscribers-lld-elf

Author: Jessica Clarke (jrtc27)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/150799.diff

2 Files Affected:

  • (modified) lld/ELF/SyntheticSections.cpp (+3-1)
  • (modified) lld/ELF/SyntheticSections.h (+4)
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;
   }
 

@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2025

@llvm/pr-subscribers-lld

Author: Jessica Clarke (jrtc27)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/150799.diff

2 Files Affected:

  • (modified) lld/ELF/SyntheticSections.cpp (+3-1)
  • (modified) lld/ELF/SyntheticSections.h (+4)
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;
   }
 

@jrtc27
Copy link
Collaborator Author

jrtc27 commented Jul 27, 2025

NB: This is a transient state, #150812 removes this member. This makes the history a bit odd, but I think it makes it clearer what's going on to include this in the history.

@hstk30-hw hstk30-hw requested a review from MaskRay July 27, 2025 15:45
Copy link
Member

@arichardson arichardson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much better.

Created using spr 1.3.5
@jrtc27 jrtc27 changed the base branch from users/jrtc27/spr/main.nfcielf-introduce-explicit-computed-state-for-dynamicreloc to main July 30, 2025 16:03
@jrtc27 jrtc27 merged commit 0d81d3c into main Jul 30, 2025
@jrtc27 jrtc27 deleted the users/jrtc27/spr/nfcielf-introduce-explicit-computed-state-for-dynamicreloc branch July 30, 2025 16:03
@Endilll Endilll removed request for a team and Endilll July 30, 2025 16:10
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jul 30, 2025
…eloc

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: llvm/llvm-project#150799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants