diff mbox series

Allow memset local PLT reference for RISC-V.

Message ID 20200622211034.659739-1-alistair.francis@wdc.com
State New
Headers show
Series Allow memset local PLT reference for RISC-V. | expand

Commit Message

Alistair Francis June 22, 2020, 9:10 p.m. UTC
This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
"Allow memset local PLT reference for powerpc soft-float.".

GCC 10.1 results in the localplt test failing for RISC-V.

From the original commit for power-pc:
    Since memset is documented as a function GCC may always implicitly
    generate calls to, it seems reasonable to allow that local PLT
    reference (just like those for libgcc functions that GCC implicitly
    generates calls to and that are also exported from libc.so), which
    this patch does.
---
 sysdeps/unix/sysv/linux/riscv/localplt.data | 1 +
 1 file changed, 1 insertion(+)

Comments

Andreas Schwab June 23, 2020, 7:19 a.m. UTC | #1
On Jun 22 2020, Alistair Francis via Libc-alpha wrote:

> This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
> "Allow memset local PLT reference for powerpc soft-float.".
>
> GCC 10.1 results in the localplt test failing for RISC-V.

Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?

Andreas.
Alistair Francis June 24, 2020, 10:25 p.m. UTC | #2
On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
>
> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
> > "Allow memset local PLT reference for powerpc soft-float.".
> >
> > GCC 10.1 results in the localplt test failing for RISC-V.
>
> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?

I'm not sure.

I  suspect it's the same reason it didn't work in the commit this is
based off: a26e2e9feab87d4f745c31411458b048742ac733
"Allow memset local PLT reference for powerpc soft-float.".

The error is part of glibc's check scripts. I'm assuming memset is in
the binary which produces a check failure as we don't expect it to be.
I'm not sure where sysdeps/generic/symbol-hacks.h is involved in this
process though.

Alistair

>
> Andreas.
>
> --
> Andreas Schwab, schwab@linux-m68k.org
> GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
> "And now for something completely different."
Vineet Gupta June 25, 2020, 12:20 a.m. UTC | #3
On 6/23/20 12:19 AM, Andreas Schwab wrote:
> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
> 
>> This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
>> "Allow memset local PLT reference for powerpc soft-float.".
>>
>> GCC 10.1 results in the localplt test failing for RISC-V.
> 
> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?

FWIW, for ARC port, Adhemerval suggested a similar workaround [1] but my stab at
it didn't work either [2]

[1] https://sourceware.org/pipermail/libc-alpha/2020-June/114838.html
[2] https://sourceware.org/pipermail/libc-alpha/2020-June/114911.html
Florian Weimer June 25, 2020, 7:20 a.m. UTC | #4
* Alistair Francis via Libc-alpha:

> On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>>
>> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
>>
>> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
>> > "Allow memset local PLT reference for powerpc soft-float.".
>> >
>> > GCC 10.1 results in the localplt test failing for RISC-V.
>>
>> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
>
> I'm not sure.

Which function contains the memset PLT reference?  “objdump
--disassemble --reloc” should show it.  Does this function come from
libgcc, by chance?

Thanks,
Florian
Vineet Gupta June 25, 2020, 6:16 p.m. UTC | #5
On 6/25/20 12:20 AM, Florian Weimer via Libc-alpha wrote:
> * Alistair Francis via Libc-alpha:
> 
>> On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>>>
>>> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
>>>
>>>> This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
>>>> "Allow memset local PLT reference for powerpc soft-float.".
>>>>
>>>> GCC 10.1 results in the localplt test failing for RISC-V.
>>>
>>> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
>>
>> I'm not sure.
> 
> Which function contains the memset PLT reference?  “objdump
> --disassemble --reloc” should show it.  Does this function come from
> libgcc, by chance?

yes this is libgcc. I saw this on ARC too (with gcc 10)

   1b2d0:	ld	r12,[pcl,0xe4d40] ;100010 <memset@@GLIBC_2.32+0x98c94>
   1b2d8:	j.d	[r12]
   1b2dc:	mov	r12,pcl

000366c8 <__mpn_mul>:
…
   36748:	bl.d	-111736	;1b2d0 <.plt+0x40>
Florian Weimer June 25, 2020, 6:41 p.m. UTC | #6
* Vineet Gupta:

> On 6/25/20 12:20 AM, Florian Weimer via Libc-alpha wrote:
>> * Alistair Francis via Libc-alpha:
>> 
>>> On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>>>>
>>>> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
>>>>
>>>>> This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
>>>>> "Allow memset local PLT reference for powerpc soft-float.".
>>>>>
>>>>> GCC 10.1 results in the localplt test failing for RISC-V.
>>>>
>>>> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
>>>
>>> I'm not sure.
>> 
>> Which function contains the memset PLT reference?  “objdump
>> --disassemble --reloc” should show it.  Does this function come from
>> libgcc, by chance?
>
> yes this is libgcc. I saw this on ARC too (with gcc 10)
>
>    1b2d0:	ld	r12,[pcl,0xe4d40] ;100010 <memset@@GLIBC_2.32+0x98c94>
>    1b2d8:	j.d	[r12]
>    1b2dc:	mov	r12,pcl
>
> 000366c8 <__mpn_mul>:
> …
>    36748:	bl.d	-111736	;1b2d0 <.plt+0x40>

Uhm, how does this end up in libgcc?

Thanks,
Florian
Palmer Dabbelt June 25, 2020, 9:27 p.m. UTC | #7
On Mon, 22 Jun 2020 14:10:34 PDT (-0700), libc-alpha@sourceware.org wrote:
> This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
> "Allow memset local PLT reference for powerpc soft-float.".
>
> GCC 10.1 results in the localplt test failing for RISC-V.
>
> From the original commit for power-pc:
>     Since memset is documented as a function GCC may always implicitly
>     generate calls to, it seems reasonable to allow that local PLT
>     reference (just like those for libgcc functions that GCC implicitly
>     generates calls to and that are also exported from libc.so), which
>     this patch does.
> ---
>  sysdeps/unix/sysv/linux/riscv/localplt.data | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/sysdeps/unix/sysv/linux/riscv/localplt.data b/sysdeps/unix/sysv/linux/riscv/localplt.data
> index 0ed8650b65..0a235592c3 100644
> --- a/sysdeps/unix/sysv/linux/riscv/localplt.data
> +++ b/sysdeps/unix/sysv/linux/riscv/localplt.data
> @@ -5,6 +5,7 @@ libc.so: calloc
>  libc.so: free
>  libc.so: malloc
>  libc.so: memalign
> +libc.so: memset ?
>  libc.so: realloc
>  # The TLS-enabled version of these functions is interposed from libc.so.
>  ld.so: _dl_signal_error

Acked-by: Palmer Dabbelt <palmerdabbelt@google.com>

Thanks!
Vineet Gupta June 26, 2020, 3:58 a.m. UTC | #8
On 6/25/20 11:41 AM, Florian Weimer via Libc-alpha wrote:

>>
>> 000366c8 <__mpn_mul>:
>> …
>>    36748:	bl.d	-111736	;1b2d0 <.plt+0x40>
> 
> Uhm, how does this end up in libgcc?

Never mind, I must not have been awoke when I wrote this. But I'm sure at some
point with gcc-10 I saw a memset PLT call, but now forget how it triggered.

-Vineet
Florian Weimer June 29, 2020, 9:11 a.m. UTC | #9
* Alistair Francis via Libc-alpha:

> On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>>
>> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
>>
>> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
>> > "Allow memset local PLT reference for powerpc soft-float.".
>> >
>> > GCC 10.1 results in the localplt test failing for RISC-V.
>>
>> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
>
> I'm not sure.
>
> I  suspect it's the same reason it didn't work in the commit this is
> based off: a26e2e9feab87d4f745c31411458b048742ac733
> "Allow memset local PLT reference for powerpc soft-float.".
>
> The error is part of glibc's check scripts. I'm assuming memset is in
> the binary which produces a check failure as we don't expect it to be.
> I'm not sure where sysdeps/generic/symbol-hacks.h is involved in this
> process though.

Please post disassembly showing the location of the PLT calls.  Which
functions have them?

Thanks,
Florian
Alistair Francis June 29, 2020, 3:58 p.m. UTC | #10
On Mon, Jun 29, 2020 at 2:11 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Alistair Francis via Libc-alpha:
>
> > On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
> >>
> >> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
> >>
> >> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
> >> > "Allow memset local PLT reference for powerpc soft-float.".
> >> >
> >> > GCC 10.1 results in the localplt test failing for RISC-V.
> >>
> >> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
> >
> > I'm not sure.
> >
> > I  suspect it's the same reason it didn't work in the commit this is
> > based off: a26e2e9feab87d4f745c31411458b048742ac733
> > "Allow memset local PLT reference for powerpc soft-float.".
> >
> > The error is part of glibc's check scripts. I'm assuming memset is in
> > the binary which produces a check failure as we don't expect it to be.
> > I'm not sure where sysdeps/generic/symbol-hacks.h is involved in this
> > process though.
>
> Please post disassembly showing the location of the PLT calls.  Which
> functions have them?

Sorry, I was waiting on my build to finish (I forgot to keep the
artifacts the first time).

I'm assuming you wanted the objdump of libc.a?

I see this (which I'm guessing is the PLT call) in libc.a

00000012 <.LVL4>:
    *ptr++ = 0ul;
  12:   00000097                auipc   ra,0x0
                        12: R_RISCV_CALL        memset
                        12: R_RISCV_RELAX       *ABS*
  16:   000080e7                jalr    ra # 12 <.LVL4>

Which is called from the __sigblock() function.

Alistair

>
> Thanks,
> Florian
>
Alistair Francis June 29, 2020, 4 p.m. UTC | #11
On Mon, Jun 29, 2020 at 8:58 AM Alistair Francis <alistair23@gmail.com> wrote:
>
> On Mon, Jun 29, 2020 at 2:11 AM Florian Weimer <fweimer@redhat.com> wrote:
> >
> > * Alistair Francis via Libc-alpha:
> >
> > > On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
> > >>
> > >> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
> > >>
> > >> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
> > >> > "Allow memset local PLT reference for powerpc soft-float.".
> > >> >
> > >> > GCC 10.1 results in the localplt test failing for RISC-V.
> > >>
> > >> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
> > >
> > > I'm not sure.
> > >
> > > I  suspect it's the same reason it didn't work in the commit this is
> > > based off: a26e2e9feab87d4f745c31411458b048742ac733
> > > "Allow memset local PLT reference for powerpc soft-float.".
> > >
> > > The error is part of glibc's check scripts. I'm assuming memset is in
> > > the binary which produces a check failure as we don't expect it to be.
> > > I'm not sure where sysdeps/generic/symbol-hacks.h is involved in this
> > > process though.
> >
> > Please post disassembly showing the location of the PLT calls.  Which
> > functions have them?
>
> Sorry, I was waiting on my build to finish (I forgot to keep the
> artifacts the first time).
>
> I'm assuming you wanted the objdump of libc.a?
>
> I see this (which I'm guessing is the PLT call) in libc.a
>
> 00000012 <.LVL4>:
>     *ptr++ = 0ul;
>   12:   00000097                auipc   ra,0x0
>                         12: R_RISCV_CALL        memset
>                         12: R_RISCV_RELAX       *ABS*
>   16:   000080e7                jalr    ra # 12 <.LVL4>
>
> Which is called from the __sigblock() function.

Ah, I just checked libgcc, it has the memset PLT call:

0000020e <.L27>:
 20e:   0605                    addi    a2,a2,1
 210:   060a                    slli    a2,a2,0x2
 212:   4581                    li      a1,0
 214:   00000097                auipc   ra,0x0
                        214: R_RISCV_CALL_PLT   memset
                        214: R_RISCV_RELAX      *ABS*
 218:   000080e7                jalr    ra # 214 <.L27+0x6>

There are a few of them in libgcc.a as well.

Alistair

>
> Alistair
>
> >
> > Thanks,
> > Florian
> >
Florian Weimer June 29, 2020, 4:11 p.m. UTC | #12
* Alistair Francis:

> On Mon, Jun 29, 2020 at 2:11 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Alistair Francis via Libc-alpha:
>>
>> > On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>> >>
>> >> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
>> >>
>> >> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
>> >> > "Allow memset local PLT reference for powerpc soft-float.".
>> >> >
>> >> > GCC 10.1 results in the localplt test failing for RISC-V.
>> >>
>> >> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
>> >
>> > I'm not sure.
>> >
>> > I  suspect it's the same reason it didn't work in the commit this is
>> > based off: a26e2e9feab87d4f745c31411458b048742ac733
>> > "Allow memset local PLT reference for powerpc soft-float.".
>> >
>> > The error is part of glibc's check scripts. I'm assuming memset is in
>> > the binary which produces a check failure as we don't expect it to be.
>> > I'm not sure where sysdeps/generic/symbol-hacks.h is involved in this
>> > process though.
>>
>> Please post disassembly showing the location of the PLT calls.  Which
>> functions have them?
>
> Sorry, I was waiting on my build to finish (I forgot to keep the
> artifacts the first time).
>
> I'm assuming you wanted the objdump of libc.a?

libc_pic.a.  libc.a is built for static linking and does not use the
symbol hacks.

If the PLT reference comes from libgcc.a, it won't show up in
libc_pic.a, though.  It will only be present in libc.so.

Thanks,
Florian
Alistair Francis June 29, 2020, 4:15 p.m. UTC | #13
On Mon, Jun 29, 2020 at 9:12 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Alistair Francis:
>
> > On Mon, Jun 29, 2020 at 2:11 AM Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * Alistair Francis via Libc-alpha:
> >>
> >> > On Tue, Jun 23, 2020 at 12:19 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
> >> >>
> >> >> On Jun 22 2020, Alistair Francis via Libc-alpha wrote:
> >> >>
> >> >> > This is similar to commit a26e2e9feab87d4f745c31411458b048742ac733
> >> >> > "Allow memset local PLT reference for powerpc soft-float.".
> >> >> >
> >> >> > GCC 10.1 results in the localplt test failing for RISC-V.
> >> >>
> >> >> Why doesn't the redirect in sysdeps/generic/symbol-hacks.h work?
> >> >
> >> > I'm not sure.
> >> >
> >> > I  suspect it's the same reason it didn't work in the commit this is
> >> > based off: a26e2e9feab87d4f745c31411458b048742ac733
> >> > "Allow memset local PLT reference for powerpc soft-float.".
> >> >
> >> > The error is part of glibc's check scripts. I'm assuming memset is in
> >> > the binary which produces a check failure as we don't expect it to be.
> >> > I'm not sure where sysdeps/generic/symbol-hacks.h is involved in this
> >> > process though.
> >>
> >> Please post disassembly showing the location of the PLT calls.  Which
> >> functions have them?
> >
> > Sorry, I was waiting on my build to finish (I forgot to keep the
> > artifacts the first time).
> >
> > I'm assuming you wanted the objdump of libc.a?
>
> libc_pic.a.  libc.a is built for static linking and does not use the
> symbol hacks.
>
> If the PLT reference comes from libgcc.a, it won't show up in
> libc_pic.a, though.  It will only be present in libc.so.

Ah ok.

There is also a PLT memset in libc_pic.a

 1bc:   009aa023                sw      s1,0(s5)
          memset (result->__data, '\0',
 1c0:   865e                    mv      a2,s7
 1c2:   4581                    li      a1,0
 1c4:   008a8513                addi    a0,s5,8
 1c8:   00000097                auipc   ra,0x0
                        1c8: R_RISCV_CALL_PLT   __GI_memset
                        1c8: R_RISCV_RELAX      *ABS*
 1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>

Alistair

>
> Thanks,
> Florian
>
Florian Weimer June 29, 2020, 4:18 p.m. UTC | #14
* Alistair Francis:

> Ah, I just checked libgcc, it has the memset PLT call:
>
> 0000020e <.L27>:
>  20e:   0605                    addi    a2,a2,1
>  210:   060a                    slli    a2,a2,0x2
>  212:   4581                    li      a1,0
>  214:   00000097                auipc   ra,0x0
>                         214: R_RISCV_CALL_PLT   memset
>                         214: R_RISCV_RELAX      *ABS*
>  218:   000080e7                jalr    ra # 214 <.L27+0x6>
>
> There are a few of them in libgcc.a as well.

And that's linked into libc.so?

I would suggest to figure out the name of the function (not sure why
objdump doesn't print it in your case), and add that to the comment
localplt.data.

Thanks,
Florian
Alistair Francis June 29, 2020, 4:25 p.m. UTC | #15
On Mon, Jun 29, 2020 at 9:30 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Alistair Francis:
>
> > There is also a PLT memset in libc_pic.a
> >
> >  1bc:   009aa023                sw      s1,0(s5)
> >           memset (result->__data, '\0',
> >  1c0:   865e                    mv      a2,s7
> >  1c2:   4581                    li      a1,0
> >  1c4:   008a8513                addi    a0,s5,8
> >  1c8:   00000097                auipc   ra,0x0
> >                         1c8: R_RISCV_CALL_PLT   __GI_memset
> >                         1c8: R_RISCV_RELAX      *ABS*
> >  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>
>
> That's weird.  What does a direct call (to a hidden symbol) look like,
> relocation-wise.

I'm not sure. How can I figure out?

Alistair

>
> Thanks,
> Florian
>
Florian Weimer June 29, 2020, 4:30 p.m. UTC | #16
* Alistair Francis:

> There is also a PLT memset in libc_pic.a
>
>  1bc:   009aa023                sw      s1,0(s5)
>           memset (result->__data, '\0',
>  1c0:   865e                    mv      a2,s7
>  1c2:   4581                    li      a1,0
>  1c4:   008a8513                addi    a0,s5,8
>  1c8:   00000097                auipc   ra,0x0
>                         1c8: R_RISCV_CALL_PLT   __GI_memset
>                         1c8: R_RISCV_RELAX      *ABS*
>  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>

That's weird.  What does a direct call (to a hidden symbol) look like,
relocation-wise.

Thanks,
Florian
Florian Weimer June 29, 2020, 4:39 p.m. UTC | #17
* Alistair Francis:

> On Mon, Jun 29, 2020 at 9:30 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Alistair Francis:
>>
>> > There is also a PLT memset in libc_pic.a
>> >
>> >  1bc:   009aa023                sw      s1,0(s5)
>> >           memset (result->__data, '\0',
>> >  1c0:   865e                    mv      a2,s7
>> >  1c2:   4581                    li      a1,0
>> >  1c4:   008a8513                addi    a0,s5,8
>> >  1c8:   00000097                auipc   ra,0x0
>> >                         1c8: R_RISCV_CALL_PLT   __GI_memset
>> >                         1c8: R_RISCV_RELAX      *ABS*
>> >  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>
>>
>> That's weird.  What does a direct call (to a hidden symbol) look like,
>> relocation-wise.
>
> I'm not sure. How can I figure out?

Look at different internal calls, e.g. __twalk_r and __libc_updwtmp (for
two variants, one with a hidden prototype and one without).

Thanks,
Florian
Alistair Francis June 29, 2020, 4:39 p.m. UTC | #18
On Mon, Jun 29, 2020 at 9:39 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Alistair Francis:
>
> > On Mon, Jun 29, 2020 at 9:30 AM Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * Alistair Francis:
> >>
> >> > There is also a PLT memset in libc_pic.a
> >> >
> >> >  1bc:   009aa023                sw      s1,0(s5)
> >> >           memset (result->__data, '\0',
> >> >  1c0:   865e                    mv      a2,s7
> >> >  1c2:   4581                    li      a1,0
> >> >  1c4:   008a8513                addi    a0,s5,8
> >> >  1c8:   00000097                auipc   ra,0x0
> >> >                         1c8: R_RISCV_CALL_PLT   __GI_memset
> >> >                         1c8: R_RISCV_RELAX      *ABS*
> >> >  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>
> >>
> >> That's weird.  What does a direct call (to a hidden symbol) look like,
> >> relocation-wise.
> >
> > I'm not sure. How can I figure out?
>
> Look at different internal calls, e.g. __twalk_r and __libc_updwtmp (for
> two variants, one with a hidden prototype and one without).

Here is what I see from the libc_pic.a lib.

__twalk_r

00000000         *UND*  00000000 __GI___twalk_r
...
  __twalk_r (loaded, do_release_shlib, handle);
 1b2:   4088                    lw      a0,0(s1)
 1b4:   8622                    mv      a2,s0
 1b6:   00000597                auipc   a1,0x0
                        1b6: R_RISCV_PCREL_HI20 do_release_shlib
                        1b6: R_RISCV_RELAX      *ABS*
 1ba:   00058593                mv      a1,a1
                        1ba: R_RISCV_PCREL_LO12_I       .L0
                        1ba: R_RISCV_RELAX      *ABS*
 1be:   00000097                auipc   ra,0x0
                        1be: R_RISCV_CALL       __GI___twalk_r
                        1be: R_RISCV_RELAX      *ABS*
 1c2:   000080e7                jalr    ra # 1be <.L52+0xc>

__libc_updwtmp

000009c0 g     F .text  00000124 .hidden __libc_updwtmp
...
00000064 <.LVL8>:
  64:   0141                    addi    sp,sp,16
  __libc_updwtmp (file_name, utmp);
  66:   00000317                auipc   t1,0x0
                        66: R_RISCV_CALL        __libc_updwtmp
                        66: R_RISCV_RELAX       *ABS*
  6a:   00030067                jr      t1 # 66 <.LVL8+0x2>

Alistair

>
> Thanks,
> Florian
>
Florian Weimer June 29, 2020, 4:53 p.m. UTC | #19
* Alistair Francis:

> On Mon, Jun 29, 2020 at 9:39 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Alistair Francis:
>>
>> > On Mon, Jun 29, 2020 at 9:30 AM Florian Weimer <fweimer@redhat.com> wrote:
>> >>
>> >> * Alistair Francis:
>> >>
>> >> > There is also a PLT memset in libc_pic.a
>> >> >
>> >> >  1bc:   009aa023                sw      s1,0(s5)
>> >> >           memset (result->__data, '\0',
>> >> >  1c0:   865e                    mv      a2,s7
>> >> >  1c2:   4581                    li      a1,0
>> >> >  1c4:   008a8513                addi    a0,s5,8
>> >> >  1c8:   00000097                auipc   ra,0x0
>> >> >                         1c8: R_RISCV_CALL_PLT   __GI_memset
>> >> >                         1c8: R_RISCV_RELAX      *ABS*
>> >> >  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>
>> >>
>> >> That's weird.  What does a direct call (to a hidden symbol) look like,
>> >> relocation-wise.
>> >
>> > I'm not sure. How can I figure out?
>>
>> Look at different internal calls, e.g. __twalk_r and __libc_updwtmp (for
>> two variants, one with a hidden prototype and one without).
>
> Here is what I see from the libc_pic.a lib.
>
> __twalk_r
>
> 00000000         *UND*  00000000 __GI___twalk_r
> ...
>   __twalk_r (loaded, do_release_shlib, handle);
>  1b2:   4088                    lw      a0,0(s1)
>  1b4:   8622                    mv      a2,s0
>  1b6:   00000597                auipc   a1,0x0
>                         1b6: R_RISCV_PCREL_HI20 do_release_shlib
>                         1b6: R_RISCV_RELAX      *ABS*
>  1ba:   00058593                mv      a1,a1
>                         1ba: R_RISCV_PCREL_LO12_I       .L0
>                         1ba: R_RISCV_RELAX      *ABS*
>  1be:   00000097                auipc   ra,0x0
>                         1be: R_RISCV_CALL       __GI___twalk_r
>                         1be: R_RISCV_RELAX      *ABS*
>  1c2:   000080e7                jalr    ra # 1be <.L52+0xc>
>
> __libc_updwtmp
>
> 000009c0 g     F .text  00000124 .hidden __libc_updwtmp
> ...
> 00000064 <.LVL8>:
>   64:   0141                    addi    sp,sp,16
>   __libc_updwtmp (file_name, utmp);
>   66:   00000317                auipc   t1,0x0
>                         66: R_RISCV_CALL        __libc_updwtmp
>                         66: R_RISCV_RELAX       *ABS*
>   6a:   00030067                jr      t1 # 66 <.LVL8+0x2>

Okay, then the memcpy symbol hack does not work on RISC-V, and it's
worth fixing that.

It would be interesting to build iconv/gconv_open.c with --save-temps
and see what exactly ends up in the assembler file (and also what the
preprocessed sources look like).

Thanks,
Florian
Andreas Schwab June 29, 2020, 5 p.m. UTC | #20
On Jun 29 2020, Alistair Francis wrote:

> There is also a PLT memset in libc_pic.a
>
>  1bc:   009aa023                sw      s1,0(s5)
>           memset (result->__data, '\0',
>  1c0:   865e                    mv      a2,s7
>  1c2:   4581                    li      a1,0
>  1c4:   008a8513                addi    a0,s5,8
>  1c8:   00000097                auipc   ra,0x0
>                         1c8: R_RISCV_CALL_PLT   __GI_memset
>                         1c8: R_RISCV_RELAX      *ABS*
>  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>

That will be relaxed by the linker.

Andreas.
Andreas Schwab June 29, 2020, 5:01 p.m. UTC | #21
On Jun 29 2020, Florian Weimer wrote:

> Okay, then the memcpy symbol hack does not work on RISC-V

Why do you think so?

Andreas.
Florian Weimer June 29, 2020, 5:05 p.m. UTC | #22
* Andreas Schwab:

> On Jun 29 2020, Florian Weimer wrote:
>
>> Okay, then the memcpy symbol hack does not work on RISC-V
>
> Why do you think so?

I think we should see R_RISCV_CALL instead of R_RISCV_CALL_PLT, like
here:

 1be:   00000097                auipc   ra,0x0
                        1be: R_RISCV_CALL       __GI___twalk_r
                        1be: R_RISCV_RELAX      *ABS*

Thanks,
Florian
Florian Weimer June 29, 2020, 5:13 p.m. UTC | #23
* Andreas Schwab:

> On Jun 29 2020, Alistair Francis wrote:
>
>> There is also a PLT memset in libc_pic.a
>>
>>  1bc:   009aa023                sw      s1,0(s5)
>>           memset (result->__data, '\0',
>>  1c0:   865e                    mv      a2,s7
>>  1c2:   4581                    li      a1,0
>>  1c4:   008a8513                addi    a0,s5,8
>>  1c8:   00000097                auipc   ra,0x0
>>                         1c8: R_RISCV_CALL_PLT   __GI_memset
>>                         1c8: R_RISCV_RELAX      *ABS*
>>  1cc:   000080e7                jalr    ra # 1c8 <.LVL39+0x14>
>
> That will be relaxed by the linker.

But so far, the guidance has been *not* to rely on linker relaxation.

And as the __twalk_r shows, RISC-V actually has real local calls which
don't need linker relaxation.  So I still think something is very
unusual with this target.

Thanks,
Florian
Andreas Schwab June 29, 2020, 5:44 p.m. UTC | #24
On Jun 29 2020, Florian Weimer wrote:

> I think we should see R_RISCV_CALL instead of R_RISCV_CALL_PLT, like
> here:

No, the compiler doesn't know about our symbol hack.

Andreas.
Andreas Schwab June 29, 2020, 5:46 p.m. UTC | #25
On Jun 29 2020, Florian Weimer wrote:

> But so far, the guidance has been *not* to rely on linker relaxation.

This is RISC-V, it does everything through linker relaxation.

Andreas.
Jim Wilson June 30, 2020, 12:26 a.m. UTC | #26
On Mon, Jun 29, 2020 at 10:05 AM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
> I think we should see R_RISCV_CALL instead of R_RISCV_CALL_PLT, like
> here:

There is a proposal to deprecate one of R_RISCV_CALL and
R_RISCV_CALL_PLT, because whether you get a call to a plt or not
depends on the symbol info, not the relocation type.  We don't
actually need two different relocs for this.  Old ABIs like x86 have
two relocs, but new ABIs like aarch64 have only one reloc for calls.
So it was a mistake in the RISC-V ABI to define two relocs.  The LLVM
RISC-V port actually handles the two relocs exactly the same.  GNU ld
handles them slightly differently, but that is something that needs to
be fixed when we deprecate one.  I think that the RISC-V gcc port
always emits the R_RISCV_CALL_PLT reloc when PIC, and always emits
R_RISCV_CALL when not-PIC.  Then the linker decides for
R_RISCV_CALL_PLT whether we actually need a plt or not.

Anyways, if you want to know where the PLT call is coming from, you
can't rely on the relocs.  R_RISCV_CALL_PLT is not necessarily a plt
call.

Jim
Maciej W. Rozycki July 1, 2020, 12:45 a.m. UTC | #27
On Mon, 29 Jun 2020, Jim Wilson wrote:

> Anyways, if you want to know where the PLT call is coming from, you
> can't rely on the relocs.  R_RISCV_CALL_PLT is not necessarily a plt
> call.

 If a PLT entry has been created, then the linker must have considered the 
symbol referred preemptible, whether legitimately or not.

 Rather than scratching one's head I would suggest running the relevant LD 
invocation under GDB to find out what really happens there, which may be 
as easy as setting a breakpoint on `riscv_elf_finish_dynamic_symbol' with 
the right condition on the hash entry so as to stop on `memset' only, and 
then working backwards with a watchpoint (on a host system that does not 
use ASLR) to find out what sets `h->plt.offset'.  There'll be the answer.

 HTH,

  Maciej
diff mbox series

Patch

diff --git a/sysdeps/unix/sysv/linux/riscv/localplt.data b/sysdeps/unix/sysv/linux/riscv/localplt.data
index 0ed8650b65..0a235592c3 100644
--- a/sysdeps/unix/sysv/linux/riscv/localplt.data
+++ b/sysdeps/unix/sysv/linux/riscv/localplt.data
@@ -5,6 +5,7 @@  libc.so: calloc
 libc.so: free
 libc.so: malloc
 libc.so: memalign
+libc.so: memset ?
 libc.so: realloc
 # The TLS-enabled version of these functions is interposed from libc.so.
 ld.so: _dl_signal_error