[lld] [ELF] -r: Synthesize R_RISCV_ALIGN at input section start (PR #151639)
Daniel Thornburgh via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 6 13:42:00 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:
Ah, I think I forgot to leave a comment about the overall structure. If I'm reading this correctly, there are already two distinct phases:
- Calling `maybeSynthesizeAlign` with each input section in turn
- Calling `maybeSynthesizeAlign` with a null section argument after all sections have been processed.
The implementation also immediately bifurcates based on which phase `maybeSynthesizeAlign` is called with. That suggests that these are two completely distinct operations, not a single parameterized one; accordingly, it's conceptually cleaner to have them be separate functions altogether. Then the outer function would be something like `finalizeSynthesizeAligns`.
https://github.com/llvm/llvm-project/pull/151639
More information about the llvm-commits
mailing list