diff options
author | Fangrui Song <i@maskray.me> | 2025-08-07 19:16:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-07 19:16:58 -0700 |
commit | 3769ce013be2879bf0b329c14a16f5cb766f26ce (patch) | |
tree | 11fc3a8008c7da474cef97e889440772eb0ce709 /llvm/lib/MC | |
parent | c9f3a706e7a3d265d995424ac8f3f082ffaf980e (diff) | |
download | llvm-3769ce013be2879bf0b329c14a16f5cb766f26ce.zip llvm-3769ce013be2879bf0b329c14a16f5cb766f26ce.tar.gz llvm-3769ce013be2879bf0b329c14a16f5cb766f26ce.tar.bz2 |
MC: Refine ALIGN relocation conditions
Each section now tracks the index of the first linker-relaxable
fragment, enabling two changes:
* Delete redundant ALIGN relocations before the first linker-relaxable
instruction in a section. The primary example is the offset 0
R_RISCV_ALIGN relocation for a text section aligned by 4.
* For alignments larger than the NOP size after the first
linker-relaxable instruction, ALIGN relocations are now generated, even in
norelax regions. This fixes the issue #150159.
The new test llvm/test/MC/RISCV/Relocations/align-after-relax.s
verifies the required ALIGN in a norelax region following
linker-relaxable instructions.
By using a fragment index within the subsection (which is less than or
equal to the section's index), the implementation may generate redundant
ALIGN relocations in lower-numbered subsections before the first
linker-relaxable instruction.
align-option-relax.s demonstrates the ALIGN optimization.
Add an initial `call` to a few tests to prevent the ALIGN optimization.
---
When the alignment exceeds 2, we insert $alignment-2 bytes of NOPs, even
in non-RVC code. This enables non-RVC code following RVC code to handle
a 2-byte adjustment without requiring an additional state in MCSection
or AsmParser.
```
.globl _start
_start:
// GNU ld can relax this to 6505 lui a0, 0x1
// LLD hasn't implemented this transformation.
lui a0, %hi(foo)
.option push
.option norelax
.option norvc
// Now we generate R_RISCV_ALIGN with addend 2, even if this is a norvc region.
.balign 4
b0:
.word 0x3a393837
.option pop
foo:
```
Pull Request: https://github.com/llvm/llvm-project/pull/150816
Diffstat (limited to 'llvm/lib/MC')
-rw-r--r-- | llvm/lib/MC/MCObjectStreamer.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/MC/MCSection.cpp | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index 8c27958..d0c6144 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -443,7 +443,7 @@ void MCObjectStreamer::emitInstToData(const MCInst &Inst, // MCAssembler::relaxAlign. auto *Sec = F->getParent(); if (!Sec->isLinkerRelaxable()) - Sec->setLinkerRelaxable(); + Sec->setFirstLinkerRelaxable(F->getLayoutOrder()); // Do not add data after a linker-relaxable instruction. The difference // between a new label and a label at or before the linker-relaxable // instruction cannot be resolved at assemble-time. diff --git a/llvm/lib/MC/MCSection.cpp b/llvm/lib/MC/MCSection.cpp index 27ca131..9ed6fd1 100644 --- a/llvm/lib/MC/MCSection.cpp +++ b/llvm/lib/MC/MCSection.cpp @@ -20,7 +20,7 @@ using namespace llvm; MCSection::MCSection(StringRef Name, bool IsText, bool IsBss, MCSymbol *Begin) : Begin(Begin), HasInstructions(false), IsRegistered(false), IsText(IsText), - IsBss(IsBss), LinkerRelaxable(false), Name(Name) { + IsBss(IsBss), Name(Name) { DummyFragment.setParent(this); } |