RISC-V: Fix code alignment after relaxable code
Series of patches to fix RISC-V code alignment issues caused by instruction relaxation and compressed instruction encoding.
This series of three patches aims to resolve issues with code alignment in the RISC-V assembler, particularly when instruction relaxation or RVC (RISC-V Compressed) instructions are involved. The assembler’s current logic only considers the immediate relaxation and RVC state, failing to account for how the linker might shift alignment points due to preceding relaxable code. This can lead to misaligned labels or linker errors. The patches introduce changes to ensure proper alignment by emitting specific relocations and accounting for worst-case scenarios.
- proposer
Submits a 3-patch series to fix RISC-V code alignment issues arising from relaxation and RVC code.
In Details
This patch series addresses a subtle bug in the RISC-V assembler (gas) concerning code alignment. The core problem is that gas determines alignment padding based solely on the relaxation and RVC state at the alignment point itself. However, if relaxable code (like RVC instructions or R_RISCV_ALIGN relocations) precedes an alignment directive in norelax mode, the linker can still adjust the exact code addresses, effectively moving the alignment point. This series introduces three patches: 1) ensures R_RISCV_ALIGN is emitted in norelax sections following relaxable code; 2) accounts…
- RVC
- RISC-V Compressed. A standard extension to the RISC-V ISA that provides 16-bit compressed instruction encodings for common operations, improving code density.
- Relaxation
- A process during assembly and linking where the assembler or linker can adjust instruction encodings or code addresses to optimize for space or performance. This can change the exact byte offsets of code.
- R_RISCV_ALIGN
- A relocation type in RISC-V used by the assembler and linker to enforce alignment. It typically pads with no-operation (nop) instructions to meet a specified alignment boundary.
- norelax code
- Code segments where the assembler and linker are instructed not to perform relaxation, meaning their layout and addresses are fixed.
- Alignment directive
- An assembler command (e.g.,
.balign) that specifies the required memory alignment for the subsequent code or data.