RISC-V: Ensure all LO relocations are checked before relaxing HI
Patch ensures all associated LO relocations are validated before relaxing PC-relative HI relocations in binutils for RISC-V, adhering to psABI.
This patch addresses a bug in binutils’ RISC-V support where PC-relative HI relocations could be relaxed without verifying that all corresponding LO relocations were marked for relaxation. This violates the RISC-V ELF PSABI, which forbids partial relaxation of such groups and can lead to errors like ‘%pcrel_lo missing matching %pcrel_hi’. The fix involves collecting all associated LO relocations before relaxing the HI and performing safety checks, maintaining performance by scanning only once per section.
- proposer
Submitted a patch to fix RISC-V linker relaxation issues by ensuring all LO relocations are validated before relaxing the corresponding HI relocation.
“PC-relative relaxation can delete the HI without checking whether all referring LOs are marked with R_RISCV_RELAX, causing "%pcrel_lo missing matching %pcrel_hi". The psABI forbids relaxing only part of a group: https://riscv-non-isa.github.io/riscv-elf-psabi-doc/#_linker_relaxation_types”
In Details
This patch for binutils targets the RISC-V linker relaxation mechanism. It addresses a specific violation of the RISC-V ELF PSABI concerning PC-relative relocations. The issue arises when a R_RISCV_PCREL_HI relocation is relaxed without ensuring that all its paired R_RISCV_PCREL_LO relocations are also marked with R_RISCV_RELAX. The proposed solution involves a more robust check, collecting and validating all associated LO relocations before proceeding with the HI relaxation, which is a prerequisite for other relaxation features like GOT load relaxation.
- linker relaxation
- A linker optimization technique that adjusts the size of code or data at link time to accommodate certain relocation types, often improving code density or performance.
- R_RISCV_PCREL_HI
- A RISC-V relocation type for the high part of a PC-relative address.
- R_RISCV_PCREL_LO
- A RISC-V relocation type for the low part of a PC-relative address.
- R_RISCV_RELAX
- A RISC-V relocation type indicating that a relocation is eligible for linker relaxation.
- PSABI
- The Procedure Call Standard Application Binary Interface, defining conventions for function calls, data types, and other low-level details for a specific architecture and ABI.
- GOT
- Global Offset Table, a data structure used in position-independent code (PIC) to store addresses of global variables and functions.