From 7b89a038a27a307f01dfd72095210cc152bcd864 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 22 Mar 2026 19:11:25 +0000 Subject: [PATCH] lib: c: add fallback errno storage for single-threaded programs --- lib/libc/core/errno/errno.c | 10 ++++++++++ lib/libc/include/errno.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/lib/libc/core/errno/errno.c b/lib/libc/core/errno/errno.c index 2e5989c..1da6d85 100644 --- a/lib/libc/core/errno/errno.c +++ b/lib/libc/core/errno/errno.c @@ -1,6 +1,15 @@ #include +#include #include +static int __errno = 32; + +int __attribute__((weak)) * __errno_location(void) +{ + kern_log("using builtin errno"); + return &__errno; +} + #if defined(BUILD_STATIC) int __set_errno(int err) { @@ -12,6 +21,7 @@ int __set_errno(int err) int __set_errno(int err) { /* TODO */ + *__errno_location() = err; return -1; } #endif diff --git a/lib/libc/include/errno.h b/lib/libc/include/errno.h index 5379f80..12c74a2 100644 --- a/lib/libc/include/errno.h +++ b/lib/libc/include/errno.h @@ -1,6 +1,8 @@ #ifndef ERRNO_H_ #define ERRNO_H_ +#define errno (*__errno_location()) + #define SUCCESS 0 /* Success */ #define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ @@ -142,4 +144,6 @@ extern int __set_errno(int err); extern int __errno_from_kern_status(unsigned int err); +extern int __attribute__((weak)) * __errno_location(void); + #endif