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

From: Willy Tarreau
Date: Thu Jul 13 2023 - 02:34:35 EST


Hi Zhangjin,

I haven't reviewed the rest yet but regarding this point:

On Thu, Jul 13, 2023 at 02:12:27PM +0800, Zhangjin Wu wrote:
> > > + /* 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++;

Or just:

_auxv = (void*)environ;
while (*_auxv++)
;

or:

for (_auxv = (void*)environ; *_auxv++; )
;

Please also have a look at the output code, because at low optimization
levels, compilers sometimes produce a better code with a local variable
than with a global variable in a loop. Thus I wouldn't be that much
surprised if at -O0 or -O1 you'd see slightly more compact code using:

/* find envp */
environ = argv + argc + 1;

/* find auxv */
for (auxv = environ; *auxv++)
;
_auxv = auxv;

than:
/* find envp */
envp = argv + argc + 1;
environ = envp;

/* find auxv */
for (_auxv = environ; *_auxv++)
;

Since it's going to become generic code, it's worth running a few tests
to see how to best polish it.

Thanks,
Willy