public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: binutils@sourceware.org
Cc: liuzhensong <liuzhensong@loongson.cn>,
	WANG Xuerui <i.swmail@xen0n.name>,
	 caiyinyu <caiyinyu@loongson.cn>
Subject: Re: [PATCH] LoongArch: Use copy relocation for %pc_lo12 against external symbol
Date: Wed, 31 Aug 2022 21:41:56 +0800	[thread overview]
Message-ID: <bb99494e9d2f646169510ff3d0c8924966958354.camel@xry111.site> (raw)
In-Reply-To: <20220831132259.71417-1-xry111@xry111.site>

Some self review:

On Wed, 2022-08-31 at 21:22 +0800, Xi Ruoyao wrote:
> -             /* For pcalau12i + jirl.  */
> -             h->needs_plt = 1;
> -             if (h->plt.refcount < 0)
> -               h->plt.refcount = 0;
> -             h->plt.refcount++;
> +             /* Check if it's a jirl instruction.  */
> +             bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
> +             uint32_t insn = 0;
> +
> +             if (contents || bfd_malloc_and_get_section (abfd, sec, &contents))
> +               memcpy(&insn, contents + rel->r_offset, sizeof(insn));
> +
> +             if ((insn & 0xfc000000) == 0x4c000000)

This is tricky.  But in commit 42bd525 we already started to rely on
undocumented %pc_{hi20,lo12} behavior: if you just apply them "as
documented" to the pcalau12i/jirl pairs the result will be absolutely
wrong.  And 42bd525 behavior is not fully correct: if you just write

pcalau12i $t0, %pc_hi20(data)
ld.d, $t0, $t0, %pc_lo12(data)

With 42bd525, the BFD linker generates a "PLT for data" (absolutely
nonsense), and destroys the ld.d instruction.  All of these buggy
behavior happens silently, the user will only find something wrong when
the linked program crashes.

I'm not sure if checking JIRL (i. e. the behavior of a relocation now
depends on the instruction where it's applied) is a good idea.  Maybe we
should use the following instead of pcalau12i/jirl:

pcaddu18i $t0, %b16_hi20(func)
jirl $ra, $t0, %b16(func)

("b16_hi20" is a hypothetical thing here.)  This will make things less
tricky, and also expand the range to 128 GiB.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  reply	other threads:[~2022-08-31 13:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-31 13:22 Xi Ruoyao
2022-08-31 13:41 ` Xi Ruoyao [this message]
2022-09-01  1:38   ` liuzhensong
2022-09-01  1:38     ` liuzhensong
2022-09-01  2:12     ` Xi Ruoyao
2022-09-01  2:31       ` liuzhensong
2022-09-01  2:31         ` liuzhensong
2022-09-01  1:27 ` liuzhensong
2022-09-01  1:27   ` liuzhensong
2022-09-01  1:41   ` Xi Ruoyao
2022-09-01  2:09     ` liuzhensong
2022-09-01  7:42     ` Fangrui Song

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=bb99494e9d2f646169510ff3d0c8924966958354.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=binutils@sourceware.org \
    --cc=caiyinyu@loongson.cn \
    --cc=i.swmail@xen0n.name \
    --cc=liuzhensong@loongson.cn \
    /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).