[lld] [ELF] -r: Synthesize R_RISCV_ALIGN at input section start (PR #151639)

Daniel Thornburgh via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 14:38:28 PDT 2025


================
@@ -959,6 +970,116 @@ bool RISCV::relaxOnce(int pass) const {
   return changed;
 }
 
+// If the section alignment is >= 4, advance `dot` to insert NOPs and synthesize
+// an ALIGN relocation. Otherwise, return false to use default handling.
+template <class ELFT, class RelTy>
+bool RISCV::synthesizeAlignOne(uint64_t &dot, InputSection *sec,
+                               Relocs<RelTy> rels) {
+  if (!baseSec) {
+    // Record the first section with RELAX relocations.
+    for (auto rel : rels) {
+      if (rel.getType(false) == R_RISCV_RELAX) {
+        baseSec = sec;
+        break;
+      }
+    }
+  } else if (sec->addralign >= 4) {
+    // If the alignment is >= 4 and the section does not start with an ALIGN
+    // relocation, synthesize one.
+    bool alignRel = false;
+    for (auto rel : rels)
+      if (rel.r_offset == 0 && rel.getType(false) == R_RISCV_ALIGN)
+        alignRel = true;
+    if (!alignRel) {
+      synthesizedAligns.emplace_back(dot - baseSec->getVA(),
+                                     sec->addralign - 2);
+      dot += sec->addralign - 2;
+      return true;
+    }
+  }
+  return false;
+}
+
+// Finalize the relocation section by appending synthesized ALIGN relocations
+// after processing all input sections.
+template <class ELFT, class RelTy>
+void RISCV::synthesizeAlignEnd(uint64_t &dot, InputSection *sec,
----------------
mysterymath wrote:

This seems like it should be called something with `finalize` in it; that's overwhelmingly been the pattern I've seen in LLD for naming later passes that perform tasks recorded as earlier passes do their work. `finalizeSynthesizedAligns/Alignment`?

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


More information about the llvm-commits mailing list