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,
"Thomas Weißschuh" <linux@weissschuh.net>
Subject: [PATCH v3 02/11] tools/nolibc: add new crt.h with _start_c
Date: Wed, 12 Jul 2023 17:17:39 +0800 [thread overview]
Message-ID: <ef5b9900a84bdbbc59eb4319e3260a6e29d24f68.1689150149.git.falcon@tinylab.org> (raw)
In-Reply-To: <cover.1689150149.git.falcon@tinylab.org>
As the environ and _auxv support added for nolibc, the assembly _start
function becomes more and more complex and therefore makes the porting
of nolibc to new architectures harder and harder.
To simplify portability, this C version of _start_c() is added to do
most of the assembly start operations in C, which reduces the complexity
a lot and will eventually simplify the porting of nolibc to the new
architectures.
The new _start_c() only requires a stack pointer argument, it will find
argv, envp and _auxv for us, and then call main(), finally, it exit()
with main's return status. With this new _start_c(), the future new
architectures only require to add very few assembly instructions.
As suggested by Thomas, users may use a different signature of main
(e.g. void main(void)), a _nolibc_main alias is added for main to
silence the warning about potential conflicting types.
Suggested-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/lkml/90fdd255-32f4-4caf-90ff-06456b53dac3@t-8ch.de/
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
tools/include/nolibc/Makefile | 1 +
tools/include/nolibc/crt.h | 59 +++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+)
create mode 100644 tools/include/nolibc/crt.h
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index 64d67b080744..909b6eb500fe 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -27,6 +27,7 @@ nolibc_arch := $(patsubst arm64,aarch64,$(ARCH))
arch_file := arch-$(nolibc_arch).h
all_files := \
compiler.h \
+ crt.h \
ctype.h \
errno.h \
nolibc.h \
diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
new file mode 100644
index 000000000000..f9db2389acd2
--- /dev/null
+++ b/tools/include/nolibc/crt.h
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * C Run Time support for NOLIBC
+ * Copyright (C) 2023 Zhangjin Wu <falcon@tinylab.org>
+ */
+
+#ifndef _NOLIBC_CRT_H
+#define _NOLIBC_CRT_H
+
+char **environ __attribute__((weak));
+const unsigned long *_auxv __attribute__((weak));
+
+typedef int (_nolibc_main_fn)(int, char **, char **);
+static void exit(int);
+
+void _start_c(long *sp)
+{
+ int argc, i;
+ char **argv;
+ char **envp;
+ /* silence potential warning: conflicting types for 'main' */
+ _nolibc_main_fn _nolibc_main __asm__ ("main");
+
+ /*
+ * sp : argc <-- argument count, required by main()
+ * argv: argv[0] <-- argument vector, required by main()
+ * argv[1]
+ * ...
+ * argv[argc-1]
+ * null
+ * envp: envp[0] <-- environment variables, required by main() and getenv()
+ * envp[1]
+ * ...
+ * null
+ * _auxv: auxv[0] <-- auxiliary vector, required by getauxval()
+ * auxv[1]
+ * ...
+ * null
+ */
+
+ /* assign argc and argv */
+ argc = sp[0];
+ argv = (void *)(sp + 1);
+
+ /* find envp */
+ envp = argv + argc + 1;
+ environ = envp;
+
+ /* find auxv */
+ i = 0;
+ while (envp[i])
+ i++;
+ _auxv = (void *)(envp + i + 1);
+
+ /* go to application */
+ exit(_nolibc_main(argc, argv, envp));
+}
+
+#endif /* _NOLIBC_CRT_H */
--
2.25.1
next prev parent reply other threads:[~2023-07-12 9:18 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 ` Zhangjin Wu [this message]
2023-07-12 20:39 ` [PATCH v3 02/11] tools/nolibc: add new crt.h with _start_c 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 ` [PATCH v3 06/11] tools/nolibc: x86_64: " Zhangjin Wu
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=ef5b9900a84bdbbc59eb4319e3260a6e29d24f68.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=linux@weissschuh.net \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®