[llvm-branch-commits] [NFCI][ELF] Introduce explicit Computed state for DynamicReloc (PR #150799)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Jul 26 14:05:44 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-elf

Author: Jessica Clarke (jrtc27)

<details>
<summary>Changes</summary>

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
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;
   }
 

``````````

</details>


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


More information about the llvm-branch-commits mailing list