| Issue |
235645
|
| Summary |
[RISCV][MC] .prefalign computes body size from the current section only, silently under-aligning functions split across sections
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
leidian977
|
## Summary
`.prefalign` determines the alignment from the *body size* — the number of
bytes between the padded start and `end_sym` (docs/Extensions.md). The body
size is computed in `MCAssembler::relaxPrefAlign()` by walking fragments from
the `.prefalign` to the fragment of `end_sym`. That walk follows a single
`MCSection`'s fragment list, so **any bytes of the body that live in other
sections are silently omitted** from the computation.
This is reachable from valid code generation: with `-basic-block-sections=all`
(or any hand-written assembly that switches sections inside the body), llc
emits `.prefalign` in the entry section while part of the function body — and
then the end symbol — lives in `unique` sections of the same name. The
computed alignment is then based on the entry block alone, and the function
entry is silently aligned to a smaller power of two than the documented rule
requires.
This is independent of the pending fix #234690 for #233842: that patch adds an
`R_RISCV_ALIGN` relocation for the padding but does not change the body-size
computation. I verified the bug still reproduces with the #234690 patch
applied.
## Reproducer
Two inputs with **identical bodies** (10 + 16 = 26 bytes) and identical
`.prefalign 5`; the only difference is whether the last 16 bytes sit in a
`unique` section:
```bash
llvm-mc -triple=riscv64 -mattr=+c,+relax -filetype=obj split.s -o split.o
llvm-objdump -d split.o
```
`split.s` (miscompiled):
```gas
.section .text.f,"ax",@progbits
.globl g, f
.type g,@function
g:
nop
nop
nop
ret
.size g, .-g
.p2align 2
.prefalign 5, .Lf_end, nop
.type f,@function
f:
nop
nop
nop
j .Lpart2
.section .text.f,"ax",@progbits,unique,9
.Lpart2:
.rept 8
nop
.endr
.section .text.f,"ax",@progbits
.Lf_end:
.size f, .Lf_end-f
```
Same input with the `.Lpart2` block kept in the first section (delete the two
`.section` directives) serves as the control.
## Actual behavior
| Input | Address of `f` | Implied computed align |
|---|---:|---:|
| body split across sections (`split.s`) | `0x10` | 16 (entry block only: `bit_ceil(10)`) |
| identical body, single section (control) | `0x20` | 32 (`bit_ceil(26)`) |
The split version emits **zero padding bytes** for the `.prefalign`: the walk
from the `FT_PrefAlign` fragment to `.Lf_end`'s fragment only sees the 10
bytes of the entry block; the 16 bytes in `.text.f unique,9` are in a
different `MCSection` and never visited.
Reproduces identically on `riscv32`, and identically with the #234690 patch
applied.
## End-to-end reproduction from IR
`llc -basic-block-sections=all` emits exactly this section layout for any
multi-block function with a `prefalign` attribute:
```llvm
target triple = "riscv64-unknown-elf"
define void @g() noinline align 4 { ... } ; 0x22 bytes
define i32 @f(ptr %p, i32 %n) align 4 prefalign(64) { ... } ; 3 blocks
```
```bash
llc -mtriple=riscv64 -mattr=+c,+relax -basic-block-sections=all \
-filetype=asm f.ll -o f.s
llvm-mc -triple=riscv64 -mattr=+c,+relax -filetype=obj f.s -o f.o
ld.lld --relax -T script.lds -e g f.o -o f.out
```
with `script.lds` concatenating the parts:
```
SECTIONS { .text 0x10000 : { *(.text.g) *(.text.f) } }
```
Result: `f` is placed at `0x10030` (16-byte aligned). The body spans
14 + 8 + 6 = 28 bytes across the three `.text.f` input sections, so the
documented rule requires 32-byte alignment (`0x10040`).
## Expected behavior
Either the body size accounts for all sections between the directive and
`end_sym`, or the assembler rejects/diagnoses a `.prefalign` whose body
crosses a section boundary. Silently computing a smaller alignment violates
the documented contract.
## Suspected mechanism
`llvm/lib/MC/MCAssembler.cpp`, `MCAssembler::relaxPrefAlign()`:
```cpp
for (auto *Cur = F.getNext(); Cur != EndFrag; Cur = Cur->getNext())
BodySize += computeFragmentSize(*Cur);
```
`MCSection` iterators stay within one section; fragments of `unique` sections
are unreachable from this walk even though `end_sym` resolves back into the
original section.
Tested on trunk (assertions build, `cc2acc33a5b2`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs