diff mbox series

[RFC,07/23] hurd: Pass the data pointer to _hurd_stack_setup explicitly

Message ID 20240103171502.1358371-8-bugaevc@gmail.com
State New
Headers show
Series aarch64-gnu port | expand

Commit Message

Sergey Bugaev Jan. 3, 2024, 5:14 p.m. UTC
Instead of relying on the stack frame layout to figure out where the stack
pointer was prior to the _hurd_stack_setup () call, just pass the pointer
as an argument explicitly. This is less brittle and much more portable.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/hurd/i386/static-start.S   |  3 +++
 sysdeps/mach/hurd/x86/init-first.c      | 16 +++++++---------
 sysdeps/mach/hurd/x86_64/static-start.S |  1 +
 3 files changed, 11 insertions(+), 9 deletions(-)

Comments

Samuel Thibault Jan. 4, 2024, 10:47 p.m. UTC | #1
Applied, thanks!

Sergey Bugaev, le mer. 03 janv. 2024 20:14:40 +0300, a ecrit:
> Instead of relying on the stack frame layout to figure out where the stack
> pointer was prior to the _hurd_stack_setup () call, just pass the pointer
> as an argument explicitly. This is less brittle and much more portable.
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  sysdeps/mach/hurd/i386/static-start.S   |  3 +++
>  sysdeps/mach/hurd/x86/init-first.c      | 16 +++++++---------
>  sysdeps/mach/hurd/x86_64/static-start.S |  1 +
>  3 files changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/sysdeps/mach/hurd/i386/static-start.S b/sysdeps/mach/hurd/i386/static-start.S
> index d83505b2..3ffcb47d 100644
> --- a/sysdeps/mach/hurd/i386/static-start.S
> +++ b/sysdeps/mach/hurd/i386/static-start.S
> @@ -19,7 +19,10 @@
>  	.text
>  	.globl _start
>  _start:
> +	pushl %esp
>  	call _hurd_stack_setup
> +	/* No need to "addl %4, %esp", since _hurd_stack_setup
> +	 * returns with an already adjusted stack pointer.  */
>  	xorl %edx, %edx
>  	jmp _start1
>  
> diff --git a/sysdeps/mach/hurd/x86/init-first.c b/sysdeps/mach/hurd/x86/init-first.c
> index bb051418..6f71d71b 100644
> --- a/sysdeps/mach/hurd/x86/init-first.c
> +++ b/sysdeps/mach/hurd/x86/init-first.c
> @@ -197,7 +197,7 @@ strong_alias (posixland_init, __libc_init_first);
>     which should not exist at all.  */
>  void
>  inhibit_stack_protector
> -_hurd_stack_setup (void)
> +_hurd_stack_setup (void **argptr)
>  {
>    /* This is the very first C code that runs in a statically linked
>       executable -- calling this function is the first thing that _start in
> @@ -206,14 +206,12 @@ _hurd_stack_setup (void)
>  
>       _start1 expects the arguments, environment, and a Hurd data block to be
>       located at the top of the stack.  The data may already be located there,
> -     or we may need to receive it from the exec server.  */
> -  void *caller = __builtin_extract_return_addr (__builtin_return_address (0));
> -  /* If the arguments and environment are already located on the stack, this is
> -     where they are, just above our call frame.  Note that this may not be a
> -     valid pointer in case we're supposed to receive the arguments from the exec
> -     server, so we can not dereference it yet.  */
> -  void **p = (void **) __builtin_frame_address (0) + 2;
> +     or we may need to receive it from the exec server.  If the data is located
> +     on the stack (just above our call frame), argptr points to it.  Note that
> +     this may not be a valid pointer in case we're supposed to receive the
> +     arguments from the exec server, so we can not dereference it yet.  */
>  
> +  void *caller = __builtin_extract_return_addr (__builtin_return_address (0));
>    /* Init the essential things.  */
>    first_init ();
>  
> @@ -245,7 +243,7 @@ _hurd_stack_setup (void)
>       the stack pointer to the data (which is somewhere on the current stack
>       anyway).  This way, _start1 find the data on the top of the stack, just as
>       it expects to.  */
> -  _hurd_startup (p, &doinit);
> +  _hurd_startup (argptr, &doinit);
>    __builtin_unreachable ();
>  }
>  #endif
> diff --git a/sysdeps/mach/hurd/x86_64/static-start.S b/sysdeps/mach/hurd/x86_64/static-start.S
> index 9b9db937..0ec00905 100644
> --- a/sysdeps/mach/hurd/x86_64/static-start.S
> +++ b/sysdeps/mach/hurd/x86_64/static-start.S
> @@ -25,6 +25,7 @@ _start:
>  	leaq __strlen_sse2(%rip), %rax
>  	movq %rax, strlen@GOTPCREL(%rip)
>  
> +	movq %rsp, %rdi
>  	call _hurd_stack_setup
>  	xorq %rdx, %rdx
>  	jmp _start1
> -- 
> 2.43.0
> 
>
diff mbox series

Patch

diff --git a/sysdeps/mach/hurd/i386/static-start.S b/sysdeps/mach/hurd/i386/static-start.S
index d83505b2..3ffcb47d 100644
--- a/sysdeps/mach/hurd/i386/static-start.S
+++ b/sysdeps/mach/hurd/i386/static-start.S
@@ -19,7 +19,10 @@ 
 	.text
 	.globl _start
 _start:
+	pushl %esp
 	call _hurd_stack_setup
+	/* No need to "addl %4, %esp", since _hurd_stack_setup
+	 * returns with an already adjusted stack pointer.  */
 	xorl %edx, %edx
 	jmp _start1
 
diff --git a/sysdeps/mach/hurd/x86/init-first.c b/sysdeps/mach/hurd/x86/init-first.c
index bb051418..6f71d71b 100644
--- a/sysdeps/mach/hurd/x86/init-first.c
+++ b/sysdeps/mach/hurd/x86/init-first.c
@@ -197,7 +197,7 @@  strong_alias (posixland_init, __libc_init_first);
    which should not exist at all.  */
 void
 inhibit_stack_protector
-_hurd_stack_setup (void)
+_hurd_stack_setup (void **argptr)
 {
   /* This is the very first C code that runs in a statically linked
      executable -- calling this function is the first thing that _start in
@@ -206,14 +206,12 @@  _hurd_stack_setup (void)
 
      _start1 expects the arguments, environment, and a Hurd data block to be
      located at the top of the stack.  The data may already be located there,
-     or we may need to receive it from the exec server.  */
-  void *caller = __builtin_extract_return_addr (__builtin_return_address (0));
-  /* If the arguments and environment are already located on the stack, this is
-     where they are, just above our call frame.  Note that this may not be a
-     valid pointer in case we're supposed to receive the arguments from the exec
-     server, so we can not dereference it yet.  */
-  void **p = (void **) __builtin_frame_address (0) + 2;
+     or we may need to receive it from the exec server.  If the data is located
+     on the stack (just above our call frame), argptr points to it.  Note that
+     this may not be a valid pointer in case we're supposed to receive the
+     arguments from the exec server, so we can not dereference it yet.  */
 
+  void *caller = __builtin_extract_return_addr (__builtin_return_address (0));
   /* Init the essential things.  */
   first_init ();
 
@@ -245,7 +243,7 @@  _hurd_stack_setup (void)
      the stack pointer to the data (which is somewhere on the current stack
      anyway).  This way, _start1 find the data on the top of the stack, just as
      it expects to.  */
-  _hurd_startup (p, &doinit);
+  _hurd_startup (argptr, &doinit);
   __builtin_unreachable ();
 }
 #endif
diff --git a/sysdeps/mach/hurd/x86_64/static-start.S b/sysdeps/mach/hurd/x86_64/static-start.S
index 9b9db937..0ec00905 100644
--- a/sysdeps/mach/hurd/x86_64/static-start.S
+++ b/sysdeps/mach/hurd/x86_64/static-start.S
@@ -25,6 +25,7 @@  _start:
 	leaq __strlen_sse2(%rip), %rax
 	movq %rax, strlen@GOTPCREL(%rip)
 
+	movq %rsp, %rdi
 	call _hurd_stack_setup
 	xorq %rdx, %rdx
 	jmp _start1