Skip to content

Conversation

@jrtc27
Copy link
Collaborator

@jrtc27 jrtc27 commented Dec 8, 2025

Other than the ordering requirements that remain between sections, this
abstracts the details of how these sections are implemented.

Note that isNeeded already checks relocsVec for both section types, so
finalizeSynthetic can call it before mergeRels just fine.

Created using spr 1.3.5
@llvmbot
Copy link
Member

llvmbot commented Dec 8, 2025

@llvm/pr-subscribers-lld-elf

Author: Jessica Clarke (jrtc27)

Changes

Other than the ordering requirements that remain between sections, this
abstracts the details of how these sections are implemented.

Note that isNeeded already checks relocsVec for both section types, so
finalizeSynthetic can call it before mergeRels just fine.


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

3 Files Affected:

  • (modified) lld/ELF/SyntheticSections.cpp (+5)
  • (modified) lld/ELF/SyntheticSections.h (+4-3)
  • (modified) lld/ELF/Writer.cpp (+3-14)
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index d6e214f9d0f48..c81f649861a73 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1707,6 +1707,9 @@ void RelocationBaseSection::partitionRels() {
 }
 
 void RelocationBaseSection::finalizeContents() {
+  mergeRels();
+  // Compute DT_RELACOUNT to be used by part.dynamic.
+  partitionRels();
   SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
 
   // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
@@ -1796,6 +1799,8 @@ void RelrBaseSection::mergeRels() {
   relocsVec.clear();
 }
 
+void RelrBaseSection::finalizeContents() { mergeRels(); }
+
 template <class ELFT>
 AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
     Ctx &ctx, StringRef name, unsigned concurrency)
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index 2b5897c9a40b0..8971d5f6c2349 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -530,14 +530,14 @@ class RelocationBaseSection : public SyntheticSection {
   }
   size_t getSize() const override { return relocs.size() * this->entsize; }
   size_t getRelativeRelocCount() const { return numRelativeRelocs; }
-  void mergeRels();
-  void partitionRels();
   void finalizeContents() override;
 
   int32_t dynamicTag, sizeDynamicTag;
   SmallVector<DynamicReloc, 0> relocs;
 
 protected:
+  void mergeRels();
+  void partitionRels();
   void computeRels();
   // Used when parallel relocation scanning adds relocations. The elements
   // will be moved into relocs by mergeRel().
@@ -608,14 +608,15 @@ class RelrBaseSection : public SyntheticSection {
     isec.addReloc({expr, addendRelType, offsetInSec, addend, &sym});
     addReloc<shard>({&isec, isec.relocs().size() - 1});
   }
-  void mergeRels();
   bool isNeeded() const override {
     return !relocs.empty() ||
            llvm::any_of(relocsVec, [](auto &v) { return !v.empty(); });
   }
+  void finalizeContents() override;
   SmallVector<RelativeReloc, 0> relocs;
 
 protected:
+  void mergeRels();
   SmallVector<SmallVector<RelativeReloc, 0>, 0> relocsVec;
 };
 
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index db5626e701ad6..57202f42cce5b 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2111,20 +2111,9 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
     // Dynamic section must be the last one in this list and dynamic
     // symbol table section (dynSymTab) must be the first one.
     for (Partition &part : ctx.partitions) {
-      if (part.relaDyn) {
-        part.relaDyn->mergeRels();
-        // Compute DT_RELACOUNT to be used by part.dynamic.
-        part.relaDyn->partitionRels();
-        finalizeSynthetic(ctx, part.relaDyn.get());
-      }
-      if (part.relrDyn) {
-        part.relrDyn->mergeRels();
-        finalizeSynthetic(ctx, part.relrDyn.get());
-      }
-      if (part.relrAuthDyn) {
-        part.relrAuthDyn->mergeRels();
-        finalizeSynthetic(ctx, part.relrAuthDyn.get());
-      }
+      finalizeSynthetic(ctx, part.relaDyn.get());
+      finalizeSynthetic(ctx, part.relrDyn.get());
+      finalizeSynthetic(ctx, part.relrAuthDyn.get());
 
       finalizeSynthetic(ctx, part.dynSymTab.get());
       finalizeSynthetic(ctx, part.gnuHashTab.get());

@llvmbot
Copy link
Member

llvmbot commented Dec 8, 2025

@llvm/pr-subscribers-lld

Author: Jessica Clarke (jrtc27)

Changes

Other than the ordering requirements that remain between sections, this
abstracts the details of how these sections are implemented.

Note that isNeeded already checks relocsVec for both section types, so
finalizeSynthetic can call it before mergeRels just fine.


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

3 Files Affected:

  • (modified) lld/ELF/SyntheticSections.cpp (+5)
  • (modified) lld/ELF/SyntheticSections.h (+4-3)
  • (modified) lld/ELF/Writer.cpp (+3-14)
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index d6e214f9d0f48..c81f649861a73 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1707,6 +1707,9 @@ void RelocationBaseSection::partitionRels() {
 }
 
 void RelocationBaseSection::finalizeContents() {
+  mergeRels();
+  // Compute DT_RELACOUNT to be used by part.dynamic.
+  partitionRels();
   SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
 
   // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
@@ -1796,6 +1799,8 @@ void RelrBaseSection::mergeRels() {
   relocsVec.clear();
 }
 
+void RelrBaseSection::finalizeContents() { mergeRels(); }
+
 template <class ELFT>
 AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
     Ctx &ctx, StringRef name, unsigned concurrency)
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index 2b5897c9a40b0..8971d5f6c2349 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -530,14 +530,14 @@ class RelocationBaseSection : public SyntheticSection {
   }
   size_t getSize() const override { return relocs.size() * this->entsize; }
   size_t getRelativeRelocCount() const { return numRelativeRelocs; }
-  void mergeRels();
-  void partitionRels();
   void finalizeContents() override;
 
   int32_t dynamicTag, sizeDynamicTag;
   SmallVector<DynamicReloc, 0> relocs;
 
 protected:
+  void mergeRels();
+  void partitionRels();
   void computeRels();
   // Used when parallel relocation scanning adds relocations. The elements
   // will be moved into relocs by mergeRel().
@@ -608,14 +608,15 @@ class RelrBaseSection : public SyntheticSection {
     isec.addReloc({expr, addendRelType, offsetInSec, addend, &sym});
     addReloc<shard>({&isec, isec.relocs().size() - 1});
   }
-  void mergeRels();
   bool isNeeded() const override {
     return !relocs.empty() ||
            llvm::any_of(relocsVec, [](auto &v) { return !v.empty(); });
   }
+  void finalizeContents() override;
   SmallVector<RelativeReloc, 0> relocs;
 
 protected:
+  void mergeRels();
   SmallVector<SmallVector<RelativeReloc, 0>, 0> relocsVec;
 };
 
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index db5626e701ad6..57202f42cce5b 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2111,20 +2111,9 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
     // Dynamic section must be the last one in this list and dynamic
     // symbol table section (dynSymTab) must be the first one.
     for (Partition &part : ctx.partitions) {
-      if (part.relaDyn) {
-        part.relaDyn->mergeRels();
-        // Compute DT_RELACOUNT to be used by part.dynamic.
-        part.relaDyn->partitionRels();
-        finalizeSynthetic(ctx, part.relaDyn.get());
-      }
-      if (part.relrDyn) {
-        part.relrDyn->mergeRels();
-        finalizeSynthetic(ctx, part.relrDyn.get());
-      }
-      if (part.relrAuthDyn) {
-        part.relrAuthDyn->mergeRels();
-        finalizeSynthetic(ctx, part.relrAuthDyn.get());
-      }
+      finalizeSynthetic(ctx, part.relaDyn.get());
+      finalizeSynthetic(ctx, part.relrDyn.get());
+      finalizeSynthetic(ctx, part.relrAuthDyn.get());
 
       finalizeSynthetic(ctx, part.dynSymTab.get());
       finalizeSynthetic(ctx, part.gnuHashTab.get());

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.

3 participants