Re: [PATCH v3 02/11] tools/nolibc: add new crt.h with _start_c

From: Thomas Weißschuh
Date: Thu Jul 13 2023 - 02:24:16 EST


On 2023-07-13 14:12:27+0800, Zhangjin Wu wrote:
> > On 2023-07-12 17:17:39+0800, Zhangjin Wu wrote:

> [..]

> > > 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@xxxxxxxxxxx>
> > > + */
> > > +
> > > +#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 **);
> >
> > What's the advantage of the typedef over using the pointer type inline?
> >
>
> With the extra comment added, this is not required anymore, will remove
> this typedef.
>
> > > +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");
> >
> > What about the stackprotector initialization?
> > It would really fit great into this series.
> >
>
> Ok, which gcc version supports stackprotector? seems the test even skip
> on gcc 10, I will find one to verify the code change.

For crosscompilation I use the crosstools from kernel.org at
https://mirrors.edge.kernel.org/pub/tools/crosstool/

It seems to be at least in their gcc 9.5.0.
And also in their 11.2 which I use mostly at the moment.

> > > + /*
> > > + * 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);
> >
> > Bit of a weird mismatch between array syntax and pointer arithmetic.
> >
>
> This 'argc = *sp;' may be better ;-)
>
> > > +
> > > + /* find envp */
> > > + envp = argv + argc + 1;
> > > + environ = envp;
> >
> > Is envp really needed? Could just be assigned directly to environ.
> >
>
> Ok, let's save one variable, envp is used to be consistent with the
> frequenty declaration of main().
>
> > > +
> > > + /* find auxv */
> > > + i = 0;
> > > + while (envp[i])
> > > + i++;
> > > + _auxv = (void *)(envp + i + 1);
> >
> > Could be simplified a bit:
> >
> > _auxv = (void *) envp;
> > while (_auxv)
> > _auxv++;
> >
>
> Yeah, it is better, but needs a little change.
>
> _auxv = (void *) envp;
> while (*_auxv)
> _auxv++;
> _auxv++;

Good catch, thanks.

> [..]

Thomas