All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhangjin Wu <falcon@tinylab.org>
To: w@1wt.eu
Cc: falcon@tinylab.org, arnd@arndb.de, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, thomas@t-8ch.de
Subject: [PATCH v3 06/11] tools/nolibc: x86_64: shrink _start with _start_c
Date: Wed, 12 Jul 2023 17:22:00 +0800	[thread overview]
Message-ID: <8bdc96799300a5c5a40320d4f66ad54e97b4b9de.1689150149.git.falcon@tinylab.org> (raw)
In-Reply-To: <cover.1689150149.git.falcon@tinylab.org>

move most of the _start operations to _start_c().

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

diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
index e980ca5417d0..0048adf6b11f 100644
--- a/tools/include/nolibc/arch-x86_64.h
+++ b/tools/include/nolibc/arch-x86_64.h
@@ -8,6 +8,7 @@
 #define _NOLIBC_ARCH_X86_64_H
 
 #include "compiler.h"
+#include "crt.h"
 
 /* Syscalls for x86_64 :
  *   - registers are 64-bit
@@ -153,9 +154,6 @@
 	_ret;                                                                 \
 })
 
-char **environ __attribute__((weak));
-const unsigned long *_auxv __attribute__((weak));
-
 /* startup code */
 /*
  * x86-64 System V ABI mandates:
@@ -167,25 +165,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();
 }
-- 
2.25.1


  parent reply	other threads:[~2023-07-12  9:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-12  9:15 [PATCH v3 00/11] tools/nolibc: shrink arch support Zhangjin Wu
2023-07-12  9:16 ` [PATCH v3 01/11] tools/nolibc: remove the old sys_stat support Zhangjin Wu
2023-07-15  8:59   ` Willy Tarreau
2023-07-15 10:39     ` Zhangjin Wu
2023-07-15 11:45       ` Willy Tarreau
2023-07-12  9:17 ` [PATCH v3 02/11] tools/nolibc: add new crt.h with _start_c Zhangjin Wu
2023-07-12 20:39   ` Thomas Weißschuh
2023-07-13  6:12     ` Zhangjin Wu
2023-07-13  6:23       ` Zhangjin Wu
2023-07-13  6:24       ` Thomas Weißschuh
2023-07-13  6:34       ` Willy Tarreau
2023-07-14  5:58         ` Zhangjin Wu
2023-07-14  6:34           ` Thomas Weißschuh
2023-07-14  9:47             ` Zhangjin Wu
2023-07-15  7:24               ` Thomas Weißschuh
2023-07-15  9:23                 ` Zhangjin Wu
2023-07-15  9:57           ` Willy Tarreau
2023-07-12  9:18 ` [PATCH v3 03/11] tools/nolibc: arm: shrink _start " Zhangjin Wu
2023-07-12  9:19 ` [PATCH v3 04/11] tools/nolibc: aarch64: " Zhangjin Wu
2023-07-12  9:20 ` [PATCH v3 05/11] tools/nolibc: i386: " Zhangjin Wu
2023-07-12  9:22 ` Zhangjin Wu [this message]
2023-07-12  9:23 ` [PATCH v3 07/11] tools/nolibc: mips: " Zhangjin Wu
2023-07-12  9:24 ` [PATCH v3 08/11] tools/nolibc: loongarch: " Zhangjin Wu
2023-07-12  9:25 ` [PATCH v3 09/11] tools/nolibc: riscv: " Zhangjin Wu
2023-07-12  9:26 ` [PATCH v3 10/11] tools/nolibc: s390: " Zhangjin Wu
2023-07-12  9:27 ` [PATCH v3 11/11] tools/nolibc: arch-*.h: add missing space after ',' Zhangjin Wu
2023-07-15 10:01 ` [PATCH v3 00/11] tools/nolibc: shrink arch support Willy Tarreau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8bdc96799300a5c5a40320d4f66ad54e97b4b9de.1689150149.git.falcon@tinylab.org \
    --to=falcon@tinylab.org \
    --cc=arnd@arndb.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=thomas@t-8ch.de \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.