newlib: Simplify errno access

Message ID 20250830191908.450705-1-paul@crapouillou.net
State New
Headers
Series newlib: Simplify errno access |

Commit Message

Paul Cercueil Aug. 30, 2025, 7:19 p.m. UTC
  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(-)
  

Comments

Corinna Vinschen Nov. 19, 2025, 3:43 p.m. UTC | #1
Hi Paul,

On Aug 30 21:19, Paul Cercueil wrote:
> 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.

You can't do that unconditionally.  The __errno function is exported
from the Cygwin DLL and actually used by applications.  So the __errno
function must exist and return a useful value for backward compat, even
if it's not used by newly compiled code.


Thanks,
Corinna
  

Patch

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.