public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] i386: Avoid avoid rely on linker optimization to avoid relocation
@ 2022-11-17 18:13 Adhemerval Zanella
  2022-11-17 19:54 ` H.J. Lu
  0 siblings, 1 reply; 3+ messages in thread
From: Adhemerval Zanella @ 2022-11-17 18:13 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..23e4f2b012 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@PLT
+#endif
 END (_start)
 
 /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
-- 
2.34.1


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

* Re: [PATCH v2] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 18:13 [PATCH v2] i386: Avoid avoid rely on linker optimization to avoid relocation Adhemerval Zanella
@ 2022-11-17 19:54 ` H.J. Lu
  2022-11-17 20:53   ` Fangrui Song
  0 siblings, 1 reply; 3+ messages in thread
From: H.J. Lu @ 2022-11-17 19:54 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Fangrui Song

On Thu, Nov 17, 2022 at 10:13 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..23e4f2b012 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@PLT
> +#endif
>  END (_start)
>
>  /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
> --
> 2.34.1
>

LGTM.

Thanks.

-- 
H.J.

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

* Re: [PATCH v2] i386: Avoid avoid rely on linker optimization to avoid relocation
  2022-11-17 19:54 ` H.J. Lu
@ 2022-11-17 20:53   ` Fangrui Song
  0 siblings, 0 replies; 3+ messages in thread
From: Fangrui Song @ 2022-11-17 20:53 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: H.J. Lu, libc-alpha

On 2022-11-17, H.J. Lu wrote:
>On Thu, Nov 17, 2022 at 10:13 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..23e4f2b012 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@PLT
>> +#endif
>>  END (_start)
>>
>>  /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
>> --
>> 2.34.1
>>
>
>LGTM.
>
>Thanks.

LGTM, too.

Reviewed-by: Fangrui Song <maskray@google.com>

BTW: do you know when start.o/start.os is assembled without -DPIC?

I find that in both --enable-static-pie and --disable-static-pie modes,
both start.o and start.os use -DPIC. start.os uses an additional -DSHARED.

Is the non-`#ifdef __PIC__` path dead?

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

end of thread, other threads:[~2022-11-17 20:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-17 18:13 [PATCH v2] i386: Avoid avoid rely on linker optimization to avoid relocation Adhemerval Zanella
2022-11-17 19:54 ` H.J. Lu
2022-11-17 20:53   ` Fangrui Song

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