public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: liuzhensong <liuzhensong@loongson.cn>
To: Xi Ruoyao <xry111@xry111.site>, binutils@sourceware.org
Cc: WANG Xuerui <i.swmail@xen0n.name>, chenglulu@loongson.cn
Subject: Re: [PATCH] LoongArch: Use copy relocation for %pc_lo12 against external symbol
Date: Thu, 1 Sep 2022 09:38:46 +0800	[thread overview]
Message-ID: <3b43c733-6011-8c7c-4a1e-3faa9f91f711@loongson.cn> (raw)
In-Reply-To: <bb99494e9d2f646169510ff3d0c8924966958354.camel@xry111.site>


在 2022/8/31 下午9:41, Xi Ruoyao 写道:
> 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)

Do you have a test case?

>
> 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.

It doesn't make sense for only "pcaddu18i + jirl" to access 128G.What we 
need is a jump that can access ±2G, just like any other pc-relative instructions can access ±2G.


WARNING: multiple messages have this Message-ID
From: liuzhensong <liuzhensong@loongson.cn>
To: Xi Ruoyao <xry111@xry111.site>, binutils@sourceware.org
Cc: WANG Xuerui <i.swmail@xen0n.name>, chenglulu@loongson.cn
Subject: Re: [PATCH] LoongArch: Use copy relocation for %pc_lo12 against external symbol
Date: Thu, 1 Sep 2022 09:38:46 +0800	[thread overview]
Message-ID: <3b43c733-6011-8c7c-4a1e-3faa9f91f711@loongson.cn> (raw)
Message-ID: <20220901013846.SfjWQdSfsD84gJbn26f79lcS1dWuoBLMh3kt8pl0w00@z> (raw)
In-Reply-To: <bb99494e9d2f646169510ff3d0c8924966958354.camel@xry111.site>

[-- Attachment #1: Type: text/plain, Size: 2043 bytes --]


在 2022/8/31 下午9:41, Xi Ruoyao 写道:
> 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)

Do you have a test case?

>
> 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.

It doesn't make sense for only "pcaddu18i + jirl" to access 128G.What we 
need is a jump that can access ±2G, just like any other pc-relative instructions can access ±2G.


  reply	other threads:[~2022-09-01  1:38 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
2022-09-01  1:38   ` liuzhensong [this message]
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=3b43c733-6011-8c7c-4a1e-3faa9f91f711@loongson.cn \
    --to=liuzhensong@loongson.cn \
    --cc=binutils@sourceware.org \
    --cc=chenglulu@loongson.cn \
    --cc=i.swmail@xen0n.name \
    --cc=xry111@xry111.site \
    /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).