unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
@ 2022-11-17 12:43 Adhemerval Zanella via Libc-alpha
  2022-11-17 16:07 ` H.J. Lu via Libc-alpha
  2022-11-21 14:47 ` Andreas Schwab via Libc-alpha
  0 siblings, 2 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2022-11-17 12:43 UTC (permalink / raw)
  To: libc-alpha, Fangrui Song, H . J . Lu

lld does not implement all the linker optimization to avoid the GOT
relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
The current 'movl main@GOT(%ebx), %eax' will then create a GOT
relocation when building with lld, which make static-pie status to
not being able to start the provided main function.

The change uses a __wrap_main local symbol, which in turn calls main
(similar as used by aarch64 and s390x).

Checked on i686-linux-gnu with binutils and lld.
---
 sysdeps/i386/start.S | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
index 4ec04bdfd7..d593c4de00 100644
--- a/sysdeps/i386/start.S
+++ b/sysdeps/i386/start.S
@@ -98,11 +98,10 @@ ENTRY (_start)
 	pushl main@GOT(%ebx)
 # else
 	/* Avoid relocation in static PIE since _start is called before
-	   it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
-	   since main may be in a shared object.  Linker will convert
-	   "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
+	   it is relocated.  This also avoid rely on linker optimization to
+	   transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
 	   if main is defined locally.  */
-	movl main@GOT(%ebx), %eax
+	leal __wrap_main@GOTOFF(%ebx), %eax
 	pushl %eax
 # endif
 
@@ -130,6 +129,12 @@ ENTRY (_start)
 1:	movl	(%esp), %ebx
 	ret
 #endif
+
+#if defined PIC && !defined SHARED
+__wrap_main:
+	_CET_ENDBR
+	jmp	main
+#endif
 END (_start)
 
 /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 12:43 [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation Adhemerval Zanella via Libc-alpha
@ 2022-11-17 16:07 ` H.J. Lu via Libc-alpha
  2022-11-17 16:41   ` Adhemerval Zanella Netto via Libc-alpha
  2022-11-21 14:47 ` Andreas Schwab via Libc-alpha
  1 sibling, 1 reply; 7+ messages in thread
From: H.J. Lu via Libc-alpha @ 2022-11-17 16:07 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Fangrui Song

On Thu, Nov 17, 2022 at 4:43 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> lld does not implement all the linker optimization to avoid the GOT
> relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
> The current 'movl main@GOT(%ebx), %eax' will then create a GOT
> relocation when building with lld, which make static-pie status to
> not being able to start the provided main function.
>
> The change uses a __wrap_main local symbol, which in turn calls main
> (similar as used by aarch64 and s390x).
>
> Checked on i686-linux-gnu with binutils and lld.
> ---
>  sysdeps/i386/start.S | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
> index 4ec04bdfd7..d593c4de00 100644
> --- a/sysdeps/i386/start.S
> +++ b/sysdeps/i386/start.S
> @@ -98,11 +98,10 @@ ENTRY (_start)
>         pushl main@GOT(%ebx)
>  # else
>         /* Avoid relocation in static PIE since _start is called before
> -          it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
> -          since main may be in a shared object.  Linker will convert
> -          "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
> +          it is relocated.  This also avoid rely on linker optimization to
> +          transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
>            if main is defined locally.  */
> -       movl main@GOT(%ebx), %eax
> +       leal __wrap_main@GOTOFF(%ebx), %eax
>         pushl %eax
>  # endif
>
> @@ -130,6 +129,12 @@ ENTRY (_start)
>  1:     movl    (%esp), %ebx
>         ret
>  #endif
> +
> +#if defined PIC && !defined SHARED
> +__wrap_main:
> +       _CET_ENDBR
> +       jmp     main

Shouldn't it be "jmp main@PLT"?

> +#endif
>  END (_start)
>
>  /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
> --
> 2.34.1
>


-- 
H.J.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 16:07 ` H.J. Lu via Libc-alpha
@ 2022-11-17 16:41   ` Adhemerval Zanella Netto via Libc-alpha
  2022-11-17 16:52     ` H.J. Lu via Libc-alpha
  0 siblings, 1 reply; 7+ messages in thread
From: Adhemerval Zanella Netto via Libc-alpha @ 2022-11-17 16:41 UTC (permalink / raw)
  To: H.J. Lu; +Cc: libc-alpha, Fangrui Song



On 17/11/22 13:07, H.J. Lu wrote:
> On Thu, Nov 17, 2022 at 4:43 AM Adhemerval Zanella
> <adhemerval.zanella@linaro.org> wrote:
>>
>> lld does not implement all the linker optimization to avoid the GOT
>> relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
>> The current 'movl main@GOT(%ebx), %eax' will then create a GOT
>> relocation when building with lld, which make static-pie status to
>> not being able to start the provided main function.
>>
>> The change uses a __wrap_main local symbol, which in turn calls main
>> (similar as used by aarch64 and s390x).
>>
>> Checked on i686-linux-gnu with binutils and lld.
>> ---
>>  sysdeps/i386/start.S | 13 +++++++++----
>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
>> index 4ec04bdfd7..d593c4de00 100644
>> --- a/sysdeps/i386/start.S
>> +++ b/sysdeps/i386/start.S
>> @@ -98,11 +98,10 @@ ENTRY (_start)
>>         pushl main@GOT(%ebx)
>>  # else
>>         /* Avoid relocation in static PIE since _start is called before
>> -          it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
>> -          since main may be in a shared object.  Linker will convert
>> -          "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
>> +          it is relocated.  This also avoid rely on linker optimization to
>> +          transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
>>            if main is defined locally.  */
>> -       movl main@GOT(%ebx), %eax
>> +       leal __wrap_main@GOTOFF(%ebx), %eax
>>         pushl %eax
>>  # endif
>>
>> @@ -130,6 +129,12 @@ ENTRY (_start)
>>  1:     movl    (%esp), %ebx
>>         ret
>>  #endif
>> +
>> +#if defined PIC && !defined SHARED
>> +__wrap_main:
>> +       _CET_ENDBR
>> +       jmp     main
> 
> Shouldn't it be "jmp main@PLT"?

My understanding is for static build we can handle main as hidden, so there
is no need use a PLT relocation here.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 16:41   ` Adhemerval Zanella Netto via Libc-alpha
@ 2022-11-17 16:52     ` H.J. Lu via Libc-alpha
  2022-11-17 17:28       ` Adhemerval Zanella Netto via Libc-alpha
  0 siblings, 1 reply; 7+ messages in thread
From: H.J. Lu via Libc-alpha @ 2022-11-17 16:52 UTC (permalink / raw)
  To: Adhemerval Zanella Netto; +Cc: libc-alpha, Fangrui Song

On Thu, Nov 17, 2022 at 8:42 AM Adhemerval Zanella Netto
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 17/11/22 13:07, H.J. Lu wrote:
> > On Thu, Nov 17, 2022 at 4:43 AM Adhemerval Zanella
> > <adhemerval.zanella@linaro.org> wrote:
> >>
> >> lld does not implement all the linker optimization to avoid the GOT
> >> relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
> >> The current 'movl main@GOT(%ebx), %eax' will then create a GOT
> >> relocation when building with lld, which make static-pie status to
> >> not being able to start the provided main function.
> >>
> >> The change uses a __wrap_main local symbol, which in turn calls main
> >> (similar as used by aarch64 and s390x).
> >>
> >> Checked on i686-linux-gnu with binutils and lld.
> >> ---
> >>  sysdeps/i386/start.S | 13 +++++++++----
> >>  1 file changed, 9 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
> >> index 4ec04bdfd7..d593c4de00 100644
> >> --- a/sysdeps/i386/start.S
> >> +++ b/sysdeps/i386/start.S
> >> @@ -98,11 +98,10 @@ ENTRY (_start)
> >>         pushl main@GOT(%ebx)
> >>  # else
> >>         /* Avoid relocation in static PIE since _start is called before
> >> -          it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
> >> -          since main may be in a shared object.  Linker will convert
> >> -          "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
> >> +          it is relocated.  This also avoid rely on linker optimization to
> >> +          transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
> >>            if main is defined locally.  */
> >> -       movl main@GOT(%ebx), %eax
> >> +       leal __wrap_main@GOTOFF(%ebx), %eax
> >>         pushl %eax
> >>  # endif
> >>
> >> @@ -130,6 +129,12 @@ ENTRY (_start)
> >>  1:     movl    (%esp), %ebx
> >>         ret
> >>  #endif
> >> +
> >> +#if defined PIC && !defined SHARED
> >> +__wrap_main:
> >> +       _CET_ENDBR
> >> +       jmp     main
> >
> > Shouldn't it be "jmp main@PLT"?
>
> My understanding is for static build we can handle main as hidden, so there
> is no need use a PLT relocation here.

The original comments have

  it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
  since main may be in a shared object.  Linker will convert

If this is only used for static build, we can use "leal
main@GOTOFF(%ebx), %eax".

-- 
H.J.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 16:52     ` H.J. Lu via Libc-alpha
@ 2022-11-17 17:28       ` Adhemerval Zanella Netto via Libc-alpha
  2022-11-17 18:01         ` H.J. Lu via Libc-alpha
  0 siblings, 1 reply; 7+ messages in thread
From: Adhemerval Zanella Netto via Libc-alpha @ 2022-11-17 17:28 UTC (permalink / raw)
  To: H.J. Lu; +Cc: libc-alpha, Fangrui Song



On 17/11/22 13:52, H.J. Lu wrote:
> On Thu, Nov 17, 2022 at 8:42 AM Adhemerval Zanella Netto
> <adhemerval.zanella@linaro.org> wrote:
>>
>>
>>
>> On 17/11/22 13:07, H.J. Lu wrote:
>>> On Thu, Nov 17, 2022 at 4:43 AM Adhemerval Zanella
>>> <adhemerval.zanella@linaro.org> wrote:
>>>>
>>>> lld does not implement all the linker optimization to avoid the GOT
>>>> relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
>>>> The current 'movl main@GOT(%ebx), %eax' will then create a GOT
>>>> relocation when building with lld, which make static-pie status to
>>>> not being able to start the provided main function.
>>>>
>>>> The change uses a __wrap_main local symbol, which in turn calls main
>>>> (similar as used by aarch64 and s390x).
>>>>
>>>> Checked on i686-linux-gnu with binutils and lld.
>>>> ---
>>>>  sysdeps/i386/start.S | 13 +++++++++----
>>>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
>>>> index 4ec04bdfd7..d593c4de00 100644
>>>> --- a/sysdeps/i386/start.S
>>>> +++ b/sysdeps/i386/start.S
>>>> @@ -98,11 +98,10 @@ ENTRY (_start)
>>>>         pushl main@GOT(%ebx)
>>>>  # else
>>>>         /* Avoid relocation in static PIE since _start is called before
>>>> -          it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
>>>> -          since main may be in a shared object.  Linker will convert
>>>> -          "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
>>>> +          it is relocated.  This also avoid rely on linker optimization to
>>>> +          transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
>>>>            if main is defined locally.  */
>>>> -       movl main@GOT(%ebx), %eax
>>>> +       955774751b71c4bc94029dd541ad9d34634ec995 __wrap_main@GOTOFF(%ebx), %eax
>>>>         pushl %eax
>>>>  # endif
>>>>
>>>> @@ -130,6 +129,12 @@ ENTRY (_start)
>>>>  1:     movl    (%esp), %ebx
>>>>         ret
>>>>  #endif
>>>> +
>>>> +#if defined PIC && !defined SHARED
>>>> +__wrap_main:
>>>> +       _CET_ENDBR
>>>> +       jmp     main
>>>
>>> Shouldn't it be "jmp main@PLT"?
>>
>> My understanding is for static build we can handle main as hidden, so there
>> is no need use a PLT relocation here.
> 
> The original comments have
> 
>   it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
>   since main may be in a shared object.  Linker will convert
> 
> If this is only used for static build, we can use "leal
> main@GOTOFF(%ebx), %eax".
> 

It does not work with binutils (and that's why you installed 955774751b71c4):

$ make test t=elf/tst-main1
[...]
csu/crt1.o(.text+0x1e): unresolvable R_386_GOTOFF relocation against symbol `main'
collect2: error: ld returned 1 exit status

The start.o (PIC and !SHARED) is used for crt1.o.  With 'main' requirement, leal
does work for building tst-main1, so maybe one option could to add a .global main
to pull the symbol.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 17:28       ` Adhemerval Zanella Netto via Libc-alpha
@ 2022-11-17 18:01         ` H.J. Lu via Libc-alpha
  0 siblings, 0 replies; 7+ messages in thread
From: H.J. Lu via Libc-alpha @ 2022-11-17 18:01 UTC (permalink / raw)
  To: Adhemerval Zanella Netto; +Cc: libc-alpha, Fangrui Song

On Thu, Nov 17, 2022 at 9:28 AM Adhemerval Zanella Netto
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 17/11/22 13:52, H.J. Lu wrote:
> > On Thu, Nov 17, 2022 at 8:42 AM Adhemerval Zanella Netto
> > <adhemerval.zanella@linaro.org> wrote:
> >>
> >>
> >>
> >> On 17/11/22 13:07, H.J. Lu wrote:
> >>> On Thu, Nov 17, 2022 at 4:43 AM Adhemerval Zanella
> >>> <adhemerval.zanella@linaro.org> wrote:
> >>>>
> >>>> lld does not implement all the linker optimization to avoid the GOT
> >>>> relocation as done by binutils (bfd/elf32-i386.c:elf_i386_convert_load_reloc).
> >>>> The current 'movl main@GOT(%ebx), %eax' will then create a GOT
> >>>> relocation when building with lld, which make static-pie status to
> >>>> not being able to start the provided main function.
> >>>>
> >>>> The change uses a __wrap_main local symbol, which in turn calls main
> >>>> (similar as used by aarch64 and s390x).
> >>>>
> >>>> Checked on i686-linux-gnu with binutils and lld.
> >>>> ---
> >>>>  sysdeps/i386/start.S | 13 +++++++++----
> >>>>  1 file changed, 9 insertions(+), 4 deletions(-)
> >>>>
> >>>> diff --git a/sysdeps/i386/start.S b/sysdeps/i386/start.S
> >>>> index 4ec04bdfd7..d593c4de00 100644
> >>>> --- a/sysdeps/i386/start.S
> >>>> +++ b/sysdeps/i386/start.S
> >>>> @@ -98,11 +98,10 @@ ENTRY (_start)
> >>>>         pushl main@GOT(%ebx)
> >>>>  # else
> >>>>         /* Avoid relocation in static PIE since _start is called before
> >>>> -          it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
> >>>> -          since main may be in a shared object.  Linker will convert
> >>>> -          "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax"
> >>>> +          it is relocated.  This also avoid rely on linker optimization to
> >>>> +          transform 'movl main@GOT(%ebx), %eax' to 'leal main@GOTOFF(%ebx)'
> >>>>            if main is defined locally.  */
> >>>> -       movl main@GOT(%ebx), %eax
> >>>> +       955774751b71c4bc94029dd541ad9d34634ec995 __wrap_main@GOTOFF(%ebx), %eax
> >>>>         pushl %eax
> >>>>  # endif
> >>>>
> >>>> @@ -130,6 +129,12 @@ ENTRY (_start)
> >>>>  1:     movl    (%esp), %ebx
> >>>>         ret
> >>>>  #endif
> >>>> +
> >>>> +#if defined PIC && !defined SHARED
> >>>> +__wrap_main:
> >>>> +       _CET_ENDBR
> >>>> +       jmp     main
> >>>
> >>> Shouldn't it be "jmp main@PLT"?
> >>
> >> My understanding is for static build we can handle main as hidden, so there
> >> is no need use a PLT relocation here.
> >
> > The original comments have
> >
> >   it is relocated.  Don't use "leal main@GOTOFF(%ebx), %eax"
> >   since main may be in a shared object.  Linker will convert
> >
> > If this is only used for static build, we can use "leal
> > main@GOTOFF(%ebx), %eax".
> >
>
> It does not work with binutils (and that's why you installed 955774751b71c4):
>
> $ make test t=elf/tst-main1
> [...]
> csu/crt1.o(.text+0x1e): unresolvable R_386_GOTOFF relocation against symbol `main'
> collect2: error: ld returned 1 exit status
>
> The start.o (PIC and !SHARED) is used for crt1.o.  With 'main' requirement, leal
> does work for building tst-main1, so maybe one option could to add a .global main
> to pull the symbol.

That is because it is also used for dynamic executables.  I think PLT
should be used.


-- 
H.J.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 12:43 [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation Adhemerval Zanella via Libc-alpha
  2022-11-17 16:07 ` H.J. Lu via Libc-alpha
@ 2022-11-21 14:47 ` Andreas Schwab via Libc-alpha
  1 sibling, 0 replies; 7+ messages in thread
From: Andreas Schwab via Libc-alpha @ 2022-11-21 14:47 UTC (permalink / raw)
  To: Adhemerval Zanella via Libc-alpha
  Cc: Fangrui Song, H . J . Lu, Adhemerval Zanella

On Nov 17 2022, Adhemerval Zanella via Libc-alpha wrote:

> +	   it is relocated.  This also avoid rely on linker optimization to
                                     avoids relying

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-11-21 14:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-17 12:43 [PATCH] i386: Avoid avoid rely on linker optimization to avoid relocation Adhemerval Zanella via Libc-alpha
2022-11-17 16:07 ` H.J. Lu via Libc-alpha
2022-11-17 16:41   ` Adhemerval Zanella Netto via Libc-alpha
2022-11-17 16:52     ` H.J. Lu via Libc-alpha
2022-11-17 17:28       ` Adhemerval Zanella Netto via Libc-alpha
2022-11-17 18:01         ` H.J. Lu via Libc-alpha
2022-11-21 14:47 ` Andreas Schwab via Libc-alpha

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