diff mbox series

[v1,07/11] tools/nolibc: x86_64: shrink _start with _start_c

Message ID e041d383165f05b964c9b1ce7e5aeb5d0a3a1e09.1687976753.git.falcon@tinylab.org (mailing list archive)
State Handled Elsewhere
Headers show
Series tools/nolibc: shrink arch support | expand

Checks

Context Check Description
conchuod/tree_selection fail Failed to apply to next/pending-fixes, riscv/for-next or riscv/master

Commit Message

Zhangjin Wu June 28, 2023, 6:59 p.m. UTC
Let's move most of the _start operations to _start_c().

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/arch-x86_64.h | 27 ++++++---------------------
 1 file changed, 6 insertions(+), 21 deletions(-)
diff mbox series

Patch

diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
index 4c0192815a59..116dcd8e1413 100644
--- a/tools/include/nolibc/arch-x86_64.h
+++ b/tools/include/nolibc/arch-x86_64.h
@@ -153,9 +153,6 @@ 
 	_ret;									\
 })
 
-char **environ __attribute__((weak));
-const unsigned long *_auxv __attribute__((weak));
-
 /* startup code */
 /*
  * x86-64 System V ABI mandates:
@@ -167,25 +164,13 @@  void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr
 {
 	__asm__ volatile (
 #ifdef _NOLIBC_STACKPROTECTOR
-		"call __stack_chk_init\n"   /* initialize stack protector                          */
+		"call __stack_chk_init\n"	/* initialize stack protector				*/
 #endif
-		"pop %rdi\n"                /* argc   (first arg, %rdi)                            */
-		"mov %rsp, %rsi\n"          /* argv[] (second arg, %rsi)                           */
-		"lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx)             */
-		"mov %rdx, environ\n"       /* save environ                                        */
-		"xor %ebp, %ebp\n"          /* zero the stack frame                                */
-		"mov %rdx, %rax\n"          /* search for auxv (follows NULL after last env)       */
-		"0:\n"
-		"add $8, %rax\n"            /* search for auxv using rax, it follows the           */
-		"cmp -8(%rax), %rbp\n"      /* ... NULL after last env (rbp is zero here)          */
-		"jnz 0b\n"
-		"mov %rax, _auxv\n"         /* save it into _auxv                                  */
-		"and $-16, %rsp\n"          /* x86 ABI : esp must be 16-byte aligned before call   */
-		"call main\n"               /* main() returns the status code, we'll exit with it. */
-		"mov %eax, %edi\n"          /* retrieve exit code (32 bit)                         */
-		"mov $60, %eax\n"           /* NR_exit == 60                                       */
-		"syscall\n"                 /* really exit                                         */
-		"hlt\n"                     /* ensure it does not return                           */
+		"xor  %ebp, %ebp\n"		/* zero the stack frame					*/
+		"mov  %rsp, %rdi\n"		/* save stack pointer to rdi, as arg1 of _start_c	*/
+		"and  $-16, %rsp\n"		/* rsp must be 16-byte aligned before call		*/
+		"call _start_c\n"		/* transfer to c runtime				*/
+		"hlt\n"				/* ensure it does not return				*/
 	);
 	__builtin_unreachable();
 }