public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Dmitry Bushev <dmitry.bushev@syntacore.com>
To: binutils@sourceware.org
Subject: Re: RISCV PC to GP-relative relaxations vs section movement
Date: Wed, 19 Oct 2022 19:19:33 +0300	[thread overview]
Message-ID: <f1f328fc-f052-f33f-c21f-34e8bfb71110@syntacore.com> (raw)

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

             reply	other threads:[~2022-10-19 16:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-19 16:19 Dmitry Bushev [this message]
2022-10-20  3:22 ` Nelson Chu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f1f328fc-f052-f33f-c21f-34e8bfb71110@syntacore.com \
    --to=dmitry.bushev@syntacore.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).