[lld] [llvm] [RISCV] Fix alignment when mixing rvc/norvc relax/norelax code (PR #150159)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 26 17:49:42 PDT 2025


MaskRay wrote:

I think the example is missing a `.balign 4` at `_start`. Without the alignment, the initial alignment is 2, <= the minimum nop size (2 due to the C extension), and assemblers (both GAS and LLVM) will not emit a R_RISCV_ALIGN relocation.

ELF assembles have the subsection feature. While I haven't tested, I think the patch will not work with

```
        .text 1
        .option push
        .option norelax
        .option norvc
        .balign 8
SHOULD_ALIGN_8_HERE:
        .word 0x12345678
        .option pop

        .text 0
_start:
.balign 4
        lui a0, %hi(foo)
        addi a0, a0, %lo(foo)
        mul a0, a1, a4
```

I think we need to modify MCSection::LinkerRelaxable. It should be set to a value that indicates the `LayoutOrder` of the first linker-relaxable fragment. Then, relaxAlign should be updated to verify if the FT_Align fragment appears after this first linker-relaxable fragment.


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


More information about the llvm-commits mailing list