public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Nelson Chu <nelson@rivosinc.com>
To: Dmitry Bushev <dmitry.bushev@syntacore.com>
Cc: binutils@sourceware.org, Jim Wilson <jim.wilson.gcc@gmail.com>,
	 Palmer Dabbelt <palmer@rivosinc.com>
Subject: Re: RISCV PC to GP-relative relaxations vs section movement
Date: Thu, 20 Oct 2022 11:22:02 +0800	[thread overview]
Message-ID: <CAPpQWtAH=ZcP9LL=9u7_mM4oW2mHOjx6o3oLc4K2fZvdXejR3g@mail.gmail.com> (raw)
In-Reply-To: <f1f328fc-f052-f33f-c21f-34e8bfb71110@syntacore.com>

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

      reply	other threads:[~2022-10-20  3:22 UTC|newest]

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

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='CAPpQWtAH=ZcP9LL=9u7_mM4oW2mHOjx6o3oLc4K2fZvdXejR3g@mail.gmail.com' \
    --to=nelson@rivosinc.com \
    --cc=binutils@sourceware.org \
    --cc=dmitry.bushev@syntacore.com \
    --cc=jim.wilson.gcc@gmail.com \
    --cc=palmer@rivosinc.com \
    /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).