Re: [PATCH v2 04/12] tools/nolibc: crt.h: add _start_c

From: Thomas Weißschuh
Date: Sun Jul 09 2023 - 17:01:12 EST


On 2023-07-09 20:49:10+0200, Thomas Weißschuh wrote:
> On 2023-07-08 23:29:58+0800, Zhangjin Wu wrote:

> [..]

> > ---
> > 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 @@

> [..]

> > 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.

I looked into this some more.

The diff below allows it to accept different signatures for main().
(Maybe you can improve the naming)

Implicit return values seem to be handled by the compiler automatically.
In C89 mode we get garbage values, in C2X/C23 we get zero.
As per the respective C specs.


diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
index b269294e9664..dba40bc9413f 100644
--- a/tools/include/nolibc/crt.h
+++ b/tools/include/nolibc/crt.h
@@ -13,7 +13,6 @@
char **environ __attribute__((weak));
const unsigned long *_auxv __attribute__((weak));

-int main(int argc, char *argv[], char **envp);
static void exit(int);

void _start_c(long *sp)
@@ -21,6 +20,7 @@ void _start_c(long *sp)
int argc, i;
char **argv;
char **envp;
+ int _nolibc_main_alias(int, char**, char**) __asm__("main");

/*
* sp : argc <-- argument count, required by main()
@@ -54,7 +54,7 @@ void _start_c(long *sp)
_auxv = (void *)(envp + i + 1);

/* go to application */
- exit(main(argc, argv, envp));
+ exit(_nolibc_main_alias(argc, argv, envp));
}

#endif /* _NOLIBC_CRT_H */