RISCV PC to GP-relative relaxations vs section movement

Dmitry Bushev dmitry.bushev@syntacore.com
Wed Oct 19 16:19:33 GMT 2022


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