https://sourceware.org/bugzilla/show_bug.cgi?id=34979

--- Comment #2 from Sourceware Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Kito Cheng <[email protected]>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=20a3bfdbee7e4615e96d779744e25e272beb1f4c

commit 20a3bfdbee7e4615e96d779744e25e272beb1f4c
Author: Zhuo Zhi <[email protected]>
Date:   Wed Nov 11 14:32:09 2026 +0100

    RISC-V: Add no-op relax pass 0 to update section VMAs before shortening
[PR34979]

    When ldelf_map_segments invokes lang_relax_sections (true) after either
    bfd_elf_discard_info edits .eh_frame or bfd_elf_map_sections_to_segments
    increases elf_program_header_size (growing SIZEOF_HEADERS),
    lang_size_sections_1 walks the linker script top-down and updates each
    output section's VMA immediately before invoking bfd_relax_section on
    its input sections.

    As a result, when relaxing an earlier output section (such as .text)
    that calls a symbol in a later output section, the earlier output
    section already has its new VMA (shifted forward by the SIZEOF_HEADERS
    increase), whereas the later output section still holds its stale VMA
    from the previous iteration.

    Although _bfd_riscv_relax_call and _bfd_riscv_relax_jal already reserve
    max_alignment slack for alignment-induced section movement, max_alignment
    can be smaller than the SIZEOF_HEADERS growth (for example, max_alignment
    is 8 bytes while 2 * sizeof(Elf64_Phdr) is 112 bytes).  The temporary
    stale-VMA discrepancy causes _bfd_riscv_relax_call to underestimate the
    forward PC-relative offset and irreversibly shorten auipc+jalr into jal,
    which then overflows R_RISCV_JAL once the target output section's VMA is
    updated:

      relocation truncated to fit: R_RISCV_JAL against symbol `...'

    Per Alan Modra's suggestion, prepend a no-op "layout" pass at pass 0 in
    bfd_elfNN_riscv_init_relax_passes so that all output section VMAs are
    recalculated consistently before the "shorten" pass runs, without adding
    an extra sizing pass to other targets.

    bfd/
            PR ld/34979
            * elfnn-riscv.c (struct riscv_relax_pass): Document NULL select.
            (riscv_relax_pass_layout): New no-op relax pass.
            (bfd_elfNN_riscv_init_relax_passes): Register
            riscv_relax_pass_layout as pass 0.
            (_bfd_riscv_relax_section): Return early when pass->select is
            NULL.

    ld/
            PR ld/34979
            * testsuite/ld-riscv-elf/pr34979.s: New test source.
            * testsuite/ld-riscv-elf/pr34979-rv32.d: New test.
            * testsuite/ld-riscv-elf/pr34979-rv64.d: New test.
            * testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run pr34979-rv32 and
            pr34979-rv64.

    Signed-off-by: Zhuo Zhi <[email protected]>

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to