Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas@t-8ch.de>
To: Zhangjin Wu <falcon@tinylab.org>
Cc: w@1wt.eu, arnd@arndb.de, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH v2 04/12] tools/nolibc: crt.h: add _start_c
Date: Sun, 9 Jul 2023 20:49:10 +0200	[thread overview]
Message-ID: <4e23cc1c-2fe0-413e-9fe1-a9428c0861b9@t-8ch.de> (raw)
In-Reply-To: <ccc8c9b850c03ef236ab05e919fea2bf9af2556a.1688828139.git.falcon@tinylab.org>

On 2023-07-08 23:29:58+0800, Zhangjin Wu wrote:
> 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.

I like it!

A quick test indicates that the initialization of the stackprotectors
could also be moved into the C function.

It also seems like a good opportunity to add some tests for
argv/environment variable passing.

And as general note to the full series I think that splitting the arch
files is not necessary and confusing.

> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  tools/include/nolibc/crt.h | 44 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
> 
> diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
> index 221b7c5346ca..b269294e9664 100644
> --- a/tools/include/nolibc/crt.h
> +++ b/tools/include/nolibc/crt.h
> @@ -13,4 +13,48 @@
>  char **environ __attribute__((weak));

The old code seems to avoid putting "environ" into the global symbol
namespace. Could this declaration be moved into the function like in
getenv()?

>  const unsigned long *_auxv __attribute__((weak));
>  
> +int main(int argc, char *argv[], char **envp);

This will lead to conflicting declarations if the users use a different
signature. I'm not (yet?) sure how to work around this.

Also how is the case handled where main() returns "void"?
I'm not sure how this is currently handled or if the compiler takes core
of returning 0 in this case.

> +static void exit(int);
> +
> +void _start_c(long *sp)
> +{
> +	int argc, i;
> +	char **argv;
> +	char **envp;
> +
> +	/*
> +	 * 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(main(argc, argv, envp));
> +}
> +
>  #endif /* _NOLIBC_CRT_H */
> -- 
> 2.25.1
> 

  reply	other threads:[~2023-07-09 18:49 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-08 15:25 [PATCH v2 00/12] tools/nolibc: shrink arch support Zhangjin Wu
2023-07-08 15:26 ` [PATCH v2 01/12] tools/nolibc: rename arch-<ARCH>.h to <ARCH>/arch.h Zhangjin Wu
2023-07-09  9:56   ` Willy Tarreau
2023-07-10  7:23     ` Zhangjin Wu
2023-07-10 15:51       ` Thomas Weißschuh
2023-07-11  7:41         ` Willy Tarreau
2023-07-11 16:38           ` Zhangjin Wu
2023-07-11 19:28             ` Willy Tarreau
2023-07-08 15:27 ` [PATCH v2 02/12] tools/nolibc: split arch.h to crt.h and sys.h Zhangjin Wu
2023-07-08 15:28 ` [PATCH v2 03/12] tools/nolibc: sys.h: remove the old sys_stat support Zhangjin Wu
2023-07-08 15:29 ` [PATCH v2 04/12] tools/nolibc: crt.h: add _start_c Zhangjin Wu
2023-07-09 18:49   ` Thomas Weißschuh [this message]
2023-07-09 21:00     ` Thomas Weißschuh
2023-07-10  9:26     ` Zhangjin Wu
2023-07-10 15:24       ` Thomas Weißschuh
2023-07-10 16:45         ` Zhangjin Wu
2023-07-08 15:31 ` [PATCH v2 05/12] tools/nolibc: arm/crt.h: shrink _start with _start_c Zhangjin Wu
2023-07-08 15:32 ` [PATCH v2 06/12] tools/nolibc: aarch64/crt.h: " Zhangjin Wu
2023-07-08 15:33 ` [PATCH v2 07/12] tools/nolibc: i386/crt.h: " Zhangjin Wu
2023-07-08 15:34 ` [PATCH v2 08/12] tools/nolibc: x86_64/crt.h: " Zhangjin Wu
2023-07-08 15:35 ` [PATCH v2 09/12] tools/nolibc: mips/crt.h: " Zhangjin Wu
2023-07-08 15:36 ` [PATCH v2 10/12] tools/nolibc: loongarch/crt.h: " Zhangjin Wu
2023-07-08 15:37 ` [PATCH v2 11/12] tools/nolibc: riscv/crt.h: " Zhangjin Wu
2023-07-08 15:38 ` [PATCH v2 12/12] tools/nolibc: s390/crt.h: " Zhangjin Wu

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=4e23cc1c-2fe0-413e-9fe1-a9428c0861b9@t-8ch.de \
    --to=thomas@t-8ch.de \
    --cc=arnd@arndb.de \
    --cc=falcon@tinylab.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --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