csu: C standard requires errno on main() entry

Reported by:	Greg Becker <greg.becker@klarasystems.com>
Reviewed by:	emaste, kevans, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Also see:	https://reviews.freebsd.org/D50998
Differential revision:	https://reviews.freebsd.org/D50997
This commit is contained in:
Konstantin Belousov
2025-06-24 02:46:43 +03:00
parent 1609b4d456
commit 6458400813
+9
View File
@@ -28,6 +28,7 @@
#include <sys/param.h>
#include <sys/elf.h>
#include <sys/elf_common.h>
#include <errno.h>
#include <stdlib.h>
#include "libc_private.h"
@@ -169,6 +170,13 @@ __libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void),
}
handle_static_init(argc, argv, env);
/*
* C17 4.3 paragraph 3:
* The value of errno in the initial thread is zero at program
* startup.
*/
errno = 0;
exit(mainX(argc, argv, env));
}
@@ -194,5 +202,6 @@ __libc_start1_gcrt(int argc, char *argv[], char *env[],
monstartup(eprolp, etextp);
handle_static_init(argc, argv, env);
errno = 0;
exit(mainX(argc, argv, env));
}