| Issue |
235639
|
| Summary |
[RISCV][MC] .prefalign with integer fill byte breaks minimum function alignment under linker relaxation
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
leidian977
|
## Summary
On RISC-V, `.prefalign` supports two documented fill forms
(see `docs/Extensions.md`):
```gas
.prefalign <log2_align>, <end_sym>, nop
.prefalign <log2_align>, <end_sym>, <fill_byte>
```
When the integer `fill_byte` form is used, the preferred-alignment padding is
emitted with a **fixed size and no `R_RISCV_ALIGN` relocation**. After linking,
a function whose minimum alignment is requested by a preceding `.p2align` can
end up at an address that violates that minimum alignment.
This is the same failure mode as #233842, but it is **not covered** by the
pending fix #234690: `MCAssembler::relaxPrefAlign()` only consults the new
`MCAsmBackend::relaxPrefAlign()` hook when `F.getPrefAlignEmitNops()` is true,
so the integer-fill form keeps its fixed, unrelocatable padding. I verified
this locally by applying the #234690 patch: the `nop` form becomes correctly
aligned, while the identical input using an integer fill byte remains
misaligned.
Note that lld processes `R_RISCV_ALIGN` relocations even with `--no-relax`, so
the minimum alignment is violated in **both** link modes.
## Reproducer
```bash
llvm-mc -triple=riscv32 -mattr=+c,+relax -filetype=obj fill.s -o fill.o
ld.lld --relax -Ttext=0x10000 -e _start fill.o -o out.relax
ld.lld --no-relax -Ttext=0x10000 -e _start fill.o -o out.norelax
```
`fill.s`:
```gas
.text
.globl _start, big
.type _start,@function
_start:
call big
call big
call big
c.nop
ret
.size _start, .-_start
.p2align 2 # minimum alignment 4
.prefalign 6, .Lbig_end, 204 # preferred alignment 64, integer fill
.type big,@function
big:
.rept 16
nop
.endr
ret
.Lbig_end:
.size big, .Lbig_end-big
```
## Actual behavior
```
$ llvm-nm out.relax
0001002e T big # 2 mod 4
$ llvm-nm out.norelax
0001003e T big # 2 mod 4
```
`big` violates the 4-byte minimum alignment in both cases.
The object contains exactly one alignment relocation, covering only the
`.p2align 2`:
```
Section (3) .rela.text {
0x0 R_RISCV_CALL_PLT big 0x0
0x0 R_RISCV_RELAX - 0x0
0x8 R_RISCV_CALL_PLT big 0x0
0x8 R_RISCV_RELAX - 0x0
0x10 R_RISCV_CALL_PLT big 0x0
0x10 R_RISCV_RELAX - 0x0
0x1C R_RISCV_ALIGN - 0x2
}
```
Nothing protects the 34 fixed fill bytes (offsets `0x1e`–`0x40`) emitted for
the `.prefalign`.
## Control experiments
| Input | Build | Options | Address of `big` | Meets `align 4`? |
|---|---|---|---:|---|
| integer fill | trunk | `--relax` | `0x1002e` | No |
| integer fill | trunk | `--no-relax` | `0x1003e` | No |
| integer fill | trunk + #234690 | `--relax` | `0x1002e` | No |
| integer fill | trunk + #234690 | `--no-relax` | `0x1003e` | No |
| `nop` fill | trunk + #234690 | `--relax` | `0x10040` | Yes |
| `nop` fill | trunk + #234690 | `--no-relax` | `0x10040` | Yes |
With the patch applied, the `nop` form gains a second relocation
(`0x1E R_RISCV_ALIGN - 0x3E`) covering the prefalign padding; the integer-fill
form gains none.
Also reproduced on `riscv64` (`big` at `0x10032` / `0x1003e`, both 2 mod 4).
## Expected behavior
`docs/Extensions.md` documents both fill forms as first-class syntax and
states: "To also enforce a minimum alignment, emit a `.p2align` before
`.prefalign`." The minimum alignment requested by the `.p2align` must survive
linking for the integer-fill form as well.
## Suspected mechanism
`MCAssembler::relaxPrefAlign()` emits `FT_PrefAlign` padding with a fixed size
and no fixups. #234690 adds backend notification for this padding, but gates it
on `F.getPrefAlignEmitNops()` (`llvm/lib/MC/MCAssembler.cpp`), so the
integer-fill form never receives an `R_RISCV_ALIGN` relocation. Linker-side
padding adjustment (which happens even with `--no-relax`) then shifts the start
of the fixed fill region without being able to shrink it, breaking the
alignment the preceding `.p2align` was supposed to guarantee.
Tested on trunk (assertions build, `cc2acc33a5b2`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs