public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Re: RISCV PC to GP-relative relaxations vs section movement
@ 2022-10-19 16:19 Dmitry Bushev
  2022-10-20  3:22 ` Nelson Chu
  0 siblings, 1 reply; 2+ messages in thread
From: Dmitry Bushev @ 2022-10-19 16:19 UTC (permalink / raw)
  To: binutils

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: RISCV PC to GP-relative relaxations vs section movement
  2022-10-19 16:19 RISCV PC to GP-relative relaxations vs section movement Dmitry Bushev
@ 2022-10-20  3:22 ` Nelson Chu
  0 siblings, 0 replies; 2+ messages in thread
From: Nelson Chu @ 2022-10-20  3:22 UTC (permalink / raw)
  To: Dmitry Bushev; +Cc: binutils, Jim Wilson, Palmer Dabbelt

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-20  3:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-19 16:19 RISCV PC to GP-relative relaxations vs section movement Dmitry Bushev
2022-10-20  3:22 ` Nelson Chu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).