ld: Recalculate section addresses before relaxing when need_layout is set
Patch recalculates section addresses before relaxation to fix RISC-V relocation errors caused by stale VMAs.
This patch addresses a relocation error on RISC-V by ensuring section addresses are consistently recalculated before the relaxation process begins. When need_layout is set (due to changes in .eh_frame or segment headers), previous relaxation passes used stale section VMAs. This led to incorrect distance calculations for instructions like auipc+jalr, causing relocation truncation. The fix recalculates addresses upfront so all sections start relaxation with accurate VMAs, preventing the issue.
- proposer
Proposes recalculating section addresses before relaxation when `need_layout` is true to fix RISC-V relocation errors.
“Recalculate section addresses prior to the relaxation loop when need_layout is set so that all output sections start relaxation with consistent VMAs.”
- reviewer
Requests a test case to be added to the patch.
“Could you add test case to the patch?”
- reviewer
Questions the necessity of an extra sizing pass for other targets and suggests an alternative approach if the fix is RISC-V specific.
“I would need to be convinced that an extra sizing pass is of benefit to other targets. If you really need to do this for riscv then I think it should be done by increasing link_info.relax_pass, then not doing anything on pass 0.”
Technical Tradeoffs
- Introducing an extra pass to recalculate section addresses might increase link time for some targets, but it resolves critical relocation errors on RISC-V.
- The proposed solution might be RISC-V specific, and its applicability or benefit to other architectures is questioned.
In Details
This patch in the GNU linker (ld) aims to resolve a RISC-V specific relocation error (R_RISCV_JAL overflow) that occurs when section addresses are not properly updated before relaxation. The issue arises when need_layout flag is set, indicating that section placement might change. The linker's relaxation process uses section VMAs to calculate offsets for PC-relative branches. If these VMAs are stale (from before elf_ldlang_before_allocation updates), the calculated distance can exceed the instruction's range. The fix ensures VMAs are recalculated via lang_size_sections_1 before entering…
- ld
- The GNU linker, responsible for combining object files and libraries into an executable or shared library.
- relaxation
- A linker optimization process where initial address assignments are refined to allow for more compact code, particularly for PC-relative branches and symbol resolution.
- need_layout
- A flag within the linker that indicates whether the layout of sections (their addresses and sizes) needs to be re-evaluated and potentially adjusted.
- VMA
- Virtual Memory Address, the address at which a program's memory is expected to be loaded and run.
- RISC-V
- An open-source instruction set architecture (ISA) that is modular and extensible.
- R_RISCV_JAL
- A relocation type specific to RISC-V architecture, used for JAL (Jump and Link) instructions, indicating the target address for a relative jump.