RISCV PC to GP-relative relaxations vs section movement

Nelson Chu nelson@rivosinc.com
Thu Oct 20 03:22:02 GMT 2022


Hi,

Probably caused by,
" warning: link_issue_err has a LOAD segment with RWX permissions" +
DATA_SEGMENT_ALIGN in the linker script.

Your custom linker script just has a LOAD segment containing every rwx
section, so it seems no need to have the data segment alignment.  Or
you probably can define more segments to separate rwx sections though.
Or, your custom linker script should re-dfine the gp value, since I
think the gp estimation in the default linker script is assuming gp
won't cover range cross DATA_SEGMENT_ALIGN.  Although the .init_array
section is placed after the DATA_SEGMENT_ALIGN, I am not sure if there
may be some unexpected behaviors when setting the DATA_SEGMENT_ALIGN
in the LOAD segment.

Thanks
Nelson

On Thu, Oct 20, 2022 at 12:21 AM Dmitry Bushev
<dmitry.bushev@syntacore.com> wrote:
>
> Hi!
>
> I discovered one serious issue in the gnu ld riscv backend with custom
> linker script. It is about pc-to-gp relaxation. In some cases linker
> reports error:
>
> relocation truncated to fit: R_RISCV_GPREL_I against symbol ...
>
> I attached minimal reproduction (program source, makefile, default and
> slightly modified linker scripts). Everything works fine if linked using
> default linker script. But with modified one it fails. The only
> difference from default script is explicit memory region specification,
> which is essential for embedded environments, so I cannot remove it.
>
> I've done some investigation and root caused the issue:
>
> 1) On relaxation stage, linker sees, that it can relax pc-relative
> relocation to gp-relative, because symbol address is close enough to
> global pointer (fits in 12 bits immediate offset).
> 2) Later due to code size change during relaxation linker rearranges
> sections which causes symbol address shift. Global pointer also shifts.
> 3) On ldwrite() relocations are written, but due to symbol address and
> global pointer relative shift, the offset happens to not fit into 12 bit
> immediate anymore, which causes linker to report the problem.
>
> I am trying to fix this. Problem is: fix shall be somewhere in
> RISCV-specific code, but most of the linker section motion logic is
> platform-independent and it is not clear for me how to interfere in this
> process in the cleanest possible way.
>
> Will appreciate any help.
>
> Best regards,
> Dmitry


More information about the Binutils mailing list