[PATCH] newlib: Simplify errno access

Paul Cercueil paul@crapouillou.net
Sat Aug 30 19:19:08 GMT 2025


I see no reason why every single errno access (read or write) should
trigger a function call to just return an offset into the reent struct.

This seems to have been needed in the past, but it does not seem to be
needed anymore.

Drop the __errno() function, and set the 'errno' macro to point to the
corresponding offset into the _reent struct.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
 newlib/libc/errno/errno.c            | 12 ------------
 newlib/libc/include/sys/errno.h      |  6 ++----
 newlib/libc/machine/cris/sys/errno.h |  6 ++----
 3 files changed, 4 insertions(+), 20 deletions(-)

diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index 384b07f2c..22e8087dc 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -7,16 +7,4 @@
 
 #ifdef _REENT_THREAD_LOCAL
 _Thread_local int _tls_errno;
-#else /* !_REENT_THREAD_LOCAL */
-
-#ifndef _REENT_ONLY
-
-int *
-__errno ()
-{
-  return &_REENT_ERRNO(_REENT);
-}
-
-#endif
-
 #endif /* _REENT_THREAD_LOCAL */
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 7199db0d2..a9610eb78 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -1,6 +1,5 @@
 /* errno is not a global variable, because that would make using it
-   non-reentrant.  Instead, its address is returned by the function
-   __errno.  */
+   non-reentrant.  Instead, it is an offset into the _reent struct. */
 
 #ifndef _SYS_ERRNO_H_
 #ifdef __cplusplus
@@ -15,8 +14,7 @@ extern "C" {
 #else /* _REENT_THREAD_LOCAL */
 
 #ifndef _REENT_ONLY
-#define errno (*__errno())
-extern int *__errno (void);
+#define errno _REENT_ERRNO(_REENT)
 #endif
 
 #endif /* _REENT_THREAD_LOCAL */
diff --git a/newlib/libc/machine/cris/sys/errno.h b/newlib/libc/machine/cris/sys/errno.h
index c8113ed73..ffd5c529d 100644
--- a/newlib/libc/machine/cris/sys/errno.h
+++ b/newlib/libc/machine/cris/sys/errno.h
@@ -3,8 +3,7 @@
    be those of the Linux/CRIS kernel.  */
 
 /* errno is not a global variable, because that would make using it
-   non-reentrant.  Instead, its address is returned by the function
-   __errno.  */
+   non-reentrant.  Instead, it is an offset into the _reent struct. */
 
 #ifndef _SYS_ERRNO_H_
 #ifdef __cplusplus
@@ -15,8 +14,7 @@ extern "C" {
 #include <sys/reent.h>
 
 #ifndef _REENT_ONLY
-#define errno (*__errno())
-extern int *__errno (void);
+#define errno _REENT_ERRNO(_REENT)
 #endif
 
 /* Please don't use these variables directly.
-- 
2.50.1



More information about the Newlib mailing list